(Vector) Use mutex instead of ConditionalWait for EventhThreadCallback 21/304221/3
authorEunki Hong <eunkiki.hong@samsung.com>
Fri, 12 Jan 2024 06:25:03 +0000 (15:25 +0900)
committerEunki Hong <eunkiki.hong@samsung.com>
Mon, 22 Jan 2024 08:18:37 +0000 (08:18 +0000)
Since worker thread can call AddEventTriggerCallback freely,

we should make some lock between workerthread and
main thread(RemoveEventTriggerCallbacks, GetNextEventCallback, ~VectorAnimationThread).

ConditionalWait used only for AsyncTask rasterization, and sleep thread.
We'd better seperate those job locker.

Change-Id: I543ba7f0bdf54036a8127d1815dda18cafca54e7
Signed-off-by: Eunki Hong <eunkiki.hong@samsung.com>
dali-toolkit/internal/visuals/animated-vector-image/vector-animation-thread.cpp
dali-toolkit/internal/visuals/animated-vector-image/vector-animation-thread.h

index 8f70ae3..3ee2d5d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2023 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2024 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -45,6 +45,7 @@ VectorAnimationThread::VectorAnimationThread()
   mWorkingTasks(),
   mSleepThread(MakeCallback(this, &VectorAnimationThread::OnAwakeFromSleep)),
   mConditionalWait(),
+  mEventTriggerMutex(),
   mNeedToSleep(false),
   mDestroyThread(false),
   mLogFactory(Dali::Adaptor::Get().GetLogFactory()),
@@ -61,11 +62,18 @@ VectorAnimationThread::~VectorAnimationThread()
   // Stop the thread
   {
     ConditionalWait::ScopedLock lock(mConditionalWait);
-    mDestroyThread = true;
-    mNeedToSleep   = false;
+    // Wait until some event thread trigger relative job finished.
+    {
+      Mutex::ScopedLock lock(mEventTriggerMutex);
+      mDestroyThread = true;
+    }
+    mNeedToSleep = false;
     mConditionalWait.Notify(lock);
   }
 
+  // Stop event trigger
+  mEventTrigger.reset();
+
   DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationThread::~VectorAnimationThread: Join [%p]\n", this);
 
   Join();
@@ -153,21 +161,27 @@ void VectorAnimationThread::OnAwakeFromSleep()
 
 void VectorAnimationThread::AddEventTriggerCallback(CallbackBase* callback)
 {
-  ConditionalWait::ScopedLock lock(mConditionalWait);
-  mTriggerEventCallbacks.push_back(callback);
-
-  if(!mEventTriggered)
+  Mutex::ScopedLock lock(mEventTriggerMutex);
+  if(!mDestroyThread)
   {
-    mEventTrigger->Trigger();
-    mEventTriggered = true;
+    mTriggerEventCallbacks.push_back(callback);
+
+    if(!mEventTriggered)
+    {
+      mEventTrigger->Trigger();
+      mEventTriggered = true;
+    }
   }
 }
 
 void VectorAnimationThread::RemoveEventTriggerCallbacks(CallbackBase* callback)
 {
-  ConditionalWait::ScopedLock lock(mConditionalWait);
-  auto                        iter = std::remove(mTriggerEventCallbacks.begin(), mTriggerEventCallbacks.end(), callback);
-  mTriggerEventCallbacks.erase(iter, mTriggerEventCallbacks.end());
+  Mutex::ScopedLock lock(mEventTriggerMutex);
+  if(!mDestroyThread)
+  {
+    auto iter = std::remove(mTriggerEventCallbacks.begin(), mTriggerEventCallbacks.end(), callback);
+    mTriggerEventCallbacks.erase(iter, mTriggerEventCallbacks.end());
+  }
 }
 
 void VectorAnimationThread::Run()
@@ -270,16 +284,18 @@ void VectorAnimationThread::OnEventCallbackTriggered()
 
 CallbackBase* VectorAnimationThread::GetNextEventCallback()
 {
-  ConditionalWait::ScopedLock lock(mConditionalWait);
-
-  if(!mTriggerEventCallbacks.empty())
+  Mutex::ScopedLock lock(mEventTriggerMutex);
+  if(!mDestroyThread)
   {
-    auto          iter     = mTriggerEventCallbacks.begin();
-    CallbackBase* callback = *iter;
-    mTriggerEventCallbacks.erase(iter);
-    return callback;
+    if(!mTriggerEventCallbacks.empty())
+    {
+      auto          iter     = mTriggerEventCallbacks.begin();
+      CallbackBase* callback = *iter;
+      mTriggerEventCallbacks.erase(iter);
+      return callback;
+    }
+    mEventTriggered = false;
   }
-  mEventTriggered = false;
   return nullptr;
 }
 
index a5108d5..7421cc8 100644 (file)
@@ -19,6 +19,7 @@
 
 // EXTERNAL INCLUDES
 #include <dali/devel-api/threading/conditional-wait.h>
+#include <dali/devel-api/threading/mutex.h>
 #include <dali/devel-api/threading/thread.h>
 #include <dali/integration-api/adaptor-framework/log-factory-interface.h>
 #include <dali/integration-api/adaptor-framework/trace-factory-interface.h>
@@ -165,6 +166,7 @@ private:
   std::vector<CallbackBase*>           mTriggerEventCallbacks{}; // Callbacks are not owned
   SleepThread                          mSleepThread;
   ConditionalWait                      mConditionalWait;
+  Mutex                                mEventTriggerMutex;
   std::unique_ptr<EventThreadCallback> mEventTrigger{};
   bool                                 mNeedToSleep;
   bool                                 mDestroyThread;