Do not add or get singletone container if it is removing 69/320369/1
authorEunki, Hong <eunkiki.hong@samsung.com>
Thu, 14 Nov 2024 05:53:11 +0000 (14:53 +0900)
committerEunki, Hong <eunkiki.hong@samsung.com>
Thu, 14 Nov 2024 05:55:21 +0000 (14:55 +0900)
Let we block singletone class don't try to get singletone handle
during it is removing.

Change-Id: I3cbb3e484693c80573fa766ffea4035ab862c044
Signed-off-by: Eunki, Hong <eunkiki.hong@samsung.com>
dali/internal/event/common/thread-local-storage.cpp
dali/internal/event/common/thread-local-storage.h

index 3f1ea4339a8119eee650b25cbcacbee31690ef7a..e0706714578e4bedf46c719d12e9e8bad3ebfa3b 100644 (file)
@@ -55,7 +55,8 @@ thread_local bool                isShuttingDown = false;
 } // namespace
 
 ThreadLocalStorage::ThreadLocalStorage(Core* core)
-: mCore(core)
+: mCore(core),
+  mSingletoneContainerChanging(false)
 {
   DALI_ASSERT_ALWAYS(threadLocal == nullptr && "Cannot create more than one ThreadLocalStorage object");
 
@@ -63,7 +64,15 @@ ThreadLocalStorage::ThreadLocalStorage(Core* core)
   isShuttingDown = false;
 }
 
-ThreadLocalStorage::~ThreadLocalStorage() = default;
+ThreadLocalStorage::~ThreadLocalStorage()
+{
+  if(DALI_LIKELY(!mSingletoneContainerChanging))
+  {
+    // Turn on this flag as true, and don't return to false agian.
+    mSingletoneContainerChanging = true;
+    mSingletonContainer.clear();
+  }
+}
 
 void ThreadLocalStorage::Remove()
 {
@@ -198,27 +207,38 @@ void ThreadLocalStorage::Register(const std::type_info& info, BaseHandle singlet
 {
   if(singleton)
   {
-    DALI_LOG_SINGLETON_SERVICE(Debug::General, "Singleton Added: %s\n", info.name());
-    mSingletonContainer.push_back(SingletonPair(info.name(), singleton));
+    if(DALI_LIKELY(!mSingletoneContainerChanging))
+    {
+      DALI_LOG_SINGLETON_SERVICE(Debug::General, "Singleton Added: %s\n", info.name());
+      mSingletonContainer.push_back(SingletonPair(info.name(), singleton));
+    }
   }
 }
 
 void ThreadLocalStorage::UnregisterAll()
 {
-  mSingletonContainer.clear();
+  if(DALI_LIKELY(!mSingletoneContainerChanging))
+  {
+    mSingletoneContainerChanging = true;
+    mSingletonContainer.clear();
+    mSingletoneContainerChanging = false;
+  }
 }
 
 BaseHandle ThreadLocalStorage::GetSingleton(const std::type_info& info) const
 {
   BaseHandle object;
 
-  const SingletonContainer::const_iterator end = mSingletonContainer.end();
-  for(SingletonContainer::const_iterator iter = mSingletonContainer.begin(); iter != end; ++iter)
+  if(DALI_LIKELY(!mSingletoneContainerChanging))
   {
-    // comparing the addresses as these are allocated statically per library
-    if((*iter).first == info.name())
+    const SingletonContainer::const_iterator end = mSingletonContainer.end();
+    for(SingletonContainer::const_iterator iter = mSingletonContainer.begin(); iter != end; ++iter)
     {
-      object = (*iter).second;
+      // comparing the addresses as these are allocated statically per library
+      if((*iter).first == info.name())
+      {
+        object = (*iter).second;
+      }
     }
   }
 
index 8d4d53771ae53ba5c28288a9085c8061ad406856..04a95a05ba3d29b29eb3d9b7f2b38f082b9f936d 100644 (file)
@@ -244,6 +244,8 @@ private:
   using SingletonConstIter = SingletonContainer::const_iterator;
 
   SingletonContainer mSingletonContainer; ///< The container to look up singleton by its type name
+
+  bool mSingletoneContainerChanging : 1; ///< Flag if singleton container is changing now.
 };
 
 } // namespace Internal