[Bf-blender-cvs] [de21ddf821c] blender-v2.90-release: BLI: Initial implementation of generic session UUID API

Sergey Sharybin noreply at git.blender.org
Fri Jul 31 11:50:40 CEST 2020


Commit: de21ddf821ce073031aa0154874cebcfad7cb481
Author: Sergey Sharybin
Date:   Thu Jul 30 11:12:13 2020 +0200
Branches: blender-v2.90-release
https://developer.blender.org/rBde21ddf821ce073031aa0154874cebcfad7cb481

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.

===================================================================

A	source/blender/blenlib/BLI_session_uuid.h
M	source/blender/blenlib/CMakeLists.txt
A	source/blender/blenlib/intern/session_uuid.c
A	tests/gtests/blenlib/BLI_session_uuid_test.cc
M	tests/gtests/blenlib/CMakeLists.txt

===================================================================

diff --git a/source/blender/blenlib/BLI_session_uuid.h b/source/blender/blenlib/BLI_session_uuid.h
new file mode 100644
index 00000000000..35d358b8e7c
--- /dev/null
+++ b/source/blender/blenlib/BLI_session_uuid.h
@@ -0,0 +1,71 @@
+/*
+ * 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.
+ */
+
+#ifndef __BLI_SESSION_UUID_H__
+#define __BLI_SESSION_UUID_H__
+
+/** \file
+ * \ingroup bli
+ */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "DNA_session_uuid_types.h"
+
+/* Generate new UUID which is unique throughout the Blender session. */
+SessionUUID BLI_session_uuid_generate(void);
+
+/* Check whether the UUID is properly generated. */
+bool BLI_session_uuid_is_generated(const SessionUUID *uuid);
+
+/* Check whether two UUIDs are identical. */
+bool BLI_session_uuid_is_equal(const SessionUUID *lhs, const SessionUUID *rhs);
+
+uint64_t BLI_session_uuid_hash_uint64(const SessionUUID *uuid);
+
+/* Utility functions to make it possible to create GHash/GSet with UUID as a key. */
+uint BLI_session_uuid_ghash_hash(const void *uuid_v);
+bool BLI_session_uuid_ghash_compare(const void *lhs_v, const void *rhs_v);
+
+#ifdef __cplusplus
+}
+#endif
+
+#ifdef __cplusplus
+
+namespace blender {
+
+inline const bool operator==(const SessionUUID &lhs, const SessionUUID &rhs)
+{
+  return BLI_session_uuid_is_equal(&lhs, &rhs);
+}
+
+template<typename T> struct DefaultHash;
+
+template<> struct DefaultHash<SessionUUID> {
+  uint64_t operator()(const SessionUUID &value) const
+  {
+    return BLI_session_uuid_hash_uint64(&value);
+  }
+};
+
+}  // namespace blender
+
+#endif
+
+#endif /* __BLI_SESSION_UUID_H__ */
diff --git a/source/blender/blenlib/CMakeLists.txt b/source/blender/blenlib/CMakeLists.txt
index c6e04d4147a..d7b279c9bbf 100644
--- a/source/blender/blenlib/CMakeLists.txt
+++ b/source/blender/blenlib/CMakeLists.txt
@@ -110,6 +110,7 @@ set(SRC
   intern/rct.c
   intern/scanfill.c
   intern/scanfill_utils.c
+  intern/session_uuid.c
   intern/smallhash.c
   intern/sort.c
   intern/sort_utils.c
@@ -239,6 +240,7 @@ set(SRC
   BLI_rect.h
   BLI_resource_collector.hh
   BLI_scanfill.h
+  BLI_session_uuid.h
   BLI_set.hh
   BLI_set_slots.hh
   BLI_smallhash.h
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);
+}
diff --git a/tests/gtests/blenlib/BLI_session_uuid_test.cc b/tests/gtests/blenlib/BLI_session_uuid_test.cc
new file mode 100644
index 00000000000..1a5f17be06c
--- /dev/null
+++ b/tests/gtests/blenlib/BLI_session_uuid_test.cc
@@ -0,0 +1,20 @@
+/* Apache License, Version 2.0 */
+
+#include "testing/testing.h"
+
+#include "BLI_session_uuid.h"
+
+TEST(SessionUUID, GenerateBasic)
+{
+  {
+    const SessionUUID uuid = BLI_session_uuid_generate();
+    EXPECT_TRUE(BLI_session_uuid_is_generated(&uuid));
+  }
+
+  {
+    const SessionUUID uuid1 = BLI_session_uuid_generate();
+    const SessionUUID uuid2 = BLI_session_uuid_generate();
+
+    EXPECT_FALSE(BLI_session_uuid_is_equal(&uuid1, &uuid2));
+  }
+}
diff --git a/tests/gtests/blenlib/CMakeLists.txt b/tests/gtests/blenlib/CMakeLists.txt
index d151dacd7a4..937279bceb9 100644
--- a/tests/gtests/blenlib/CMakeLists.txt
+++ b/tests/gtests/blenlib/CMakeLists.txt
@@ -68,6 +68,7 @@ BLENDER_TEST(BLI_memiter "bf_blenlib")
 BLENDER_TEST(BLI_memory_utils "bf_blenlib")
 BLENDER_TEST(BLI_path_util "${BLI_path_util_extra_libs}")
 BLENDER_TEST(BLI_polyfill_2d "bf_blenlib")
+BLENDER_TEST(BLI_session_uuid "bf_blenlib")
 BLENDER_TEST(BLI_set "bf_blenlib")
 BLENDER_TEST(BLI_span "bf_blenlib")
 BLENDER_TEST(BLI_stack "bf_blenlib")



More information about the Bf-blender-cvs mailing list