diff options
author | Sergey Sharybin <sergey.vfx@gmail.com> | 2020-07-30 12:12:13 +0300 |
---|---|---|
committer | Sergey Sharybin <sergey.vfx@gmail.com> | 2020-07-31 12:24:03 +0300 |
commit | de21ddf821ce073031aa0154874cebcfad7cb481 (patch) | |
tree | 8b76d624c393e789abc15455e021062cd9ab7609 /source/blender/blenlib/intern/session_uuid.c | |
parent | cd579c49960510b9979842284b94b35c9175e059 (diff) |
BLI: Initial implementation of generic session UUID API
Covers basics like generation of new UUID.
Also contains code needed to use the SessionUUID as a key in the Map.
Diffstat (limited to 'source/blender/blenlib/intern/session_uuid.c')
-rw-r--r-- | source/blender/blenlib/intern/session_uuid.c | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/source/blender/blenlib/intern/session_uuid.c b/source/blender/blenlib/intern/session_uuid.c new file mode 100644 index 00000000000..51d1177a332 --- /dev/null +++ b/source/blender/blenlib/intern/session_uuid.c @@ -0,0 +1,78 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +/** \file + * \ingroup bli + */ + +#include "BLI_session_uuid.h" + +#include "BLI_utildefines.h" + +#include "atomic_ops.h" + +/* Special value which indicates the UUID has not been assigned yet. */ +#define BLI_SESSION_UUID_NONE 0 + +static const SessionUUID global_session_uuid_none = {BLI_SESSION_UUID_NONE}; + +/* Denotes last used UUID. + * It might eventually overflow, and easiest is to add more bits to it. */ +static SessionUUID global_session_uuid = global_session_uuid_none; + +SessionUUID BLI_session_uuid_generate(void) +{ + SessionUUID result; + result.uuid_ = atomic_add_and_fetch_uint64(&global_session_uuid.uuid_, 1); + if (!BLI_session_uuid_is_generated(&result)) { + /* Happens when the UUID overflows. + * + * Just request the UUID once again, hoping that there are no 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 collissions after such overflow. */ + result.uuid_ = atomic_add_and_fetch_uint64(&global_session_uuid.uuid_, 1); + } + return result; +} + +bool BLI_session_uuid_is_generated(const SessionUUID *uuid) +{ + return !BLI_session_uuid_is_equal(uuid, &global_session_uuid_none); +} + +bool BLI_session_uuid_is_equal(const SessionUUID *lhs, const SessionUUID *rhs) +{ + return lhs->uuid_ == rhs->uuid_; +} + +uint64_t BLI_session_uuid_hash_uint64(const SessionUUID *uuid) +{ + return uuid->uuid_; +} + +uint BLI_session_uuid_ghash_hash(const void *uuid_v) +{ + const SessionUUID *uuid = (const SessionUUID *)uuid_v; + return uuid->uuid_ & 0xffffffff; +} + +bool BLI_session_uuid_ghash_compare(const void *lhs_v, const void *rhs_v) +{ + const SessionUUID *lhs = (const SessionUUID *)lhs_v; + const SessionUUID *rhs = (const SessionUUID *)rhs_v; + return BLI_session_uuid_is_equal(lhs, rhs); +} |