Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
protect the dict initialization against multithreading
authorMartin Quinson <martin.quinson@ens-rennes.fr>
Wed, 22 Feb 2023 11:37:36 +0000 (12:37 +0100)
committerMartin Quinson <martin.quinson@ens-rennes.fr>
Wed, 22 Feb 2023 11:37:36 +0000 (12:37 +0100)
src/xbt/dict.cpp
teshsuite/s4u/CMakeLists.txt

index bbc4599..3d8703f 100644 (file)
@@ -17,6 +17,7 @@
 #include <algorithm>
 #include <cstdio>
 #include <cstring>
+#include <mutex>
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_dict, xbt, "Dictionaries provide the same functionalities as hash tables");
 
@@ -33,9 +34,14 @@ static void xbt_dict_postexit()
 static void xbt_dict_preinit()
 {
   if (dict_elm_mallocator == nullptr) {
-    dict_elm_mallocator =
-        xbt_mallocator_new(256, dict_elm_mallocator_new_f, dict_elm_mallocator_free_f, dict_elm_mallocator_reset_f);
-    atexit(xbt_dict_postexit);
+    static std::mutex init_mutex;
+    init_mutex.lock();
+    if (dict_elm_mallocator == nullptr) { // Just in case someone initialized it in between
+      dict_elm_mallocator =
+          xbt_mallocator_new(256, dict_elm_mallocator_new_f, dict_elm_mallocator_free_f, dict_elm_mallocator_reset_f);
+      atexit(xbt_dict_postexit);
+    }
+    init_mutex.unlock();
   }
 }
 
index 70317ad..83969f8 100644 (file)
@@ -17,7 +17,7 @@ foreach(x actor actor-autorestart actor-suspend
         storage_client_server listen_async pid
         trace-integration
         seal-platform
-             vm-live-migration vm-suicide issue71)
+        vm-live-migration vm-suicide issue71)
 
   if(NOT DEFINED ${x}_sources)
       set(${x}_sources ${x}/${x}.cpp)