`UUID` generally stands for "universally unique identifier". The session identifier that we use is neither universally unique, nor does it follow the standard. Therefor, the term "session uuid" is confusing and should be replaced. In #116888 we briefly talked about a better name and ended up with "session uid". The reason for "uid" instead of "id" is that the latter is a very overloaded term in Blender already. This patch changes all uses of "uuid" to "uid" where it's used in the context of a "session uid". It's not always trivial to see whether a specific mention of "uuid" refers to an actual uuid or something else. Therefore, I might have missed some renames. I can't think of an automated way to differentiate the case. BMesh also uses the term "uuid" sometimes in a the wrong context (e.g. `UUIDFaceStepItem`) but there it also does not mean "session uid", so it's *not* changed by this patch. Pull Request: https://projects.blender.org/blender/blender/pulls/117350
67 lines
1.8 KiB
C
67 lines
1.8 KiB
C
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
|
*
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
|
|
|
/** \file
|
|
* \ingroup bli
|
|
*/
|
|
|
|
#include "BLI_session_uid.h"
|
|
|
|
#include "BLI_utildefines.h"
|
|
|
|
#include "atomic_ops.h"
|
|
|
|
/* Special value which indicates the UID has not been assigned yet. */
|
|
#define BLI_session_uid_NONE 0
|
|
|
|
static const SessionUID global_session_uid_none = {BLI_session_uid_NONE};
|
|
|
|
/* Denotes last used UID.
|
|
* It might eventually overflow, and easiest is to add more bits to it. */
|
|
static SessionUID global_session_uid = {BLI_session_uid_NONE};
|
|
|
|
SessionUID BLI_session_uid_generate(void)
|
|
{
|
|
SessionUID result;
|
|
result.uid_ = atomic_add_and_fetch_uint64(&global_session_uid.uid_, 1);
|
|
if (!BLI_session_uid_is_generated(&result)) {
|
|
/* Happens when the UID overflows.
|
|
*
|
|
* Just request the UID once again, hoping that there are not a lot of high-priority threads
|
|
* which will overflow the counter once again between the previous call and this one.
|
|
*
|
|
* NOTE: It is possible to have collisions after such overflow. */
|
|
result.uid_ = atomic_add_and_fetch_uint64(&global_session_uid.uid_, 1);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
bool BLI_session_uid_is_generated(const SessionUID *uid)
|
|
{
|
|
return !BLI_session_uid_is_equal(uid, &global_session_uid_none);
|
|
}
|
|
|
|
bool BLI_session_uid_is_equal(const SessionUID *lhs, const SessionUID *rhs)
|
|
{
|
|
return lhs->uid_ == rhs->uid_;
|
|
}
|
|
|
|
uint64_t BLI_session_uid_hash_uint64(const SessionUID *uid)
|
|
{
|
|
return uid->uid_;
|
|
}
|
|
|
|
uint BLI_session_uid_ghash_hash(const void *uid_v)
|
|
{
|
|
const SessionUID *uid = (const SessionUID *)uid_v;
|
|
return uid->uid_ & 0xffffffff;
|
|
}
|
|
|
|
bool BLI_session_uid_ghash_compare(const void *lhs_v, const void *rhs_v)
|
|
{
|
|
const SessionUID *lhs = (const SessionUID *)lhs_v;
|
|
const SessionUID *rhs = (const SessionUID *)rhs_v;
|
|
return !BLI_session_uid_is_equal(lhs, rhs);
|
|
}
|