[Bf-blender-cvs] [63c47124509] functions: lazy init: thread safe registration and utility to list all

Jacques Lucke noreply at git.blender.org
Wed Jun 5 10:07:17 CEST 2019


Commit: 63c47124509b32aa02ff8f41be465c6106475260
Author: Jacques Lucke
Date:   Wed Jun 5 09:47:27 2019 +0200
Branches: functions
https://developer.blender.org/rB63c47124509b32aa02ff8f41be465c6106475260

lazy init: thread safe registration and utility to list all

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

M	source/blender/blenlib/BLI_lazy_init.h
M	source/blender/blenlib/BLI_lazy_init.hpp
M	source/blender/blenlib/intern/BLI_lazy_init.cpp

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

diff --git a/source/blender/blenlib/BLI_lazy_init.h b/source/blender/blenlib/BLI_lazy_init.h
index a3a626202a7..274072fa9d9 100644
--- a/source/blender/blenlib/BLI_lazy_init.h
+++ b/source/blender/blenlib/BLI_lazy_init.h
@@ -1,9 +1,15 @@
+#ifndef __BLI_LAZY_INIT_H__
+#define __BLI_LAZY_INIT_H__
+
 #ifdef __cplusplus
 extern "C" {
 #endif
 
 void BLI_lazy_init_free_all(void);
+void BLI_lazy_init_list_all(void);
 
 #ifdef __cplusplus
 }
 #endif
+
+#endif /* __BLI_LAZY_INIT_H__ */
diff --git a/source/blender/blenlib/BLI_lazy_init.hpp b/source/blender/blenlib/BLI_lazy_init.hpp
index 74e4a09f189..f63621d44c1 100644
--- a/source/blender/blenlib/BLI_lazy_init.hpp
+++ b/source/blender/blenlib/BLI_lazy_init.hpp
@@ -6,9 +6,11 @@
 #include <functional>
 #include <BLI_optional.hpp>
 
+#include "BLI_lazy_init.h"
+
 namespace BLI {
 
-void register_lazy_init_free_func(std::function<void()> free_func);
+void register_lazy_init_free_func(std::function<void()> free_func, const char *name);
 
 }  // namespace BLI
 
@@ -17,7 +19,7 @@ void register_lazy_init_free_func(std::function<void()> free_func);
   static builder_ret_type &func_name##_builder(void) \
   { \
     static BLI::Optional<builder_ret_type> value = func_name##_impl(); \
-    BLI::register_lazy_init_free_func([]() { value.reset(); }); \
+    BLI::register_lazy_init_free_func([]() { value.reset(); }, #func_name); \
     return value.value(); \
   } \
   final_ret_type func_name(void) \
diff --git a/source/blender/blenlib/intern/BLI_lazy_init.cpp b/source/blender/blenlib/intern/BLI_lazy_init.cpp
index 6e7de60e0b7..0b0414e31ef 100644
--- a/source/blender/blenlib/intern/BLI_lazy_init.cpp
+++ b/source/blender/blenlib/intern/BLI_lazy_init.cpp
@@ -1,24 +1,39 @@
+#include <mutex>
+
 #include "BLI_lazy_init.hpp"
 #include "BLI_lazy_init.h"
 #include "BLI_small_stack.hpp"
 
-typedef std::function<void()> FreeFunc;
+struct FreeFunc {
+  std::function<void()> func;
+  const char *name;
+};
+
 BLI::SmallStack<FreeFunc> free_functions;
+std::mutex store_free_func_mutex;
 
 void BLI_lazy_init_free_all()
 {
   while (!free_functions.empty()) {
     FreeFunc free_object = free_functions.pop();
-    free_object();
+    free_object.func();
   }
   free_functions.clear_and_make_small();
 }
 
+void BLI_lazy_init_list_all()
+{
+  for (FreeFunc &func : free_functions) {
+    std::cout << func.name << "\n";
+  }
+}
+
 namespace BLI {
 
-void register_lazy_init_free_func(std::function<void()> free_func)
+void register_lazy_init_free_func(std::function<void()> free_func, const char *name)
 {
-  free_functions.push(free_func);
+  std::lock_guard<std::mutex> lock(store_free_func_mutex);
+  free_functions.push({free_func, name});
 }
 
 }  // namespace BLI



More information about the Bf-blender-cvs mailing list