Add GetCallbackInvocationThread() for AsyncTaskMananager 26/285926/9
authortscholb <scholb.kim@samsung.com>
Thu, 22 Dec 2022 10:09:52 +0000 (19:09 +0900)
committertscholb <scholb.kim@samsung.com>
Fri, 6 Jan 2023 01:01:22 +0000 (10:01 +0900)
When AsyncTaskManager use other thread, no need to wake up main thread.

Change-Id: I2b4dfb797e0981002776b345b43f988dd5eb83f9

dali/internal/system/common/async-task-manager-impl.cpp
dali/public-api/adaptor-framework/async-task-manager.cpp
dali/public-api/adaptor-framework/async-task-manager.h

index 828d98e..a1ed1ce 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2022 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2023 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.
@@ -292,24 +292,36 @@ AsyncTaskPtr AsyncTaskManager::PopNextCompletedTask()
 void AsyncTaskManager::CompleteTask(AsyncTaskPtr task)
 {
   // Lock while adding task to the queue
-  Mutex::ScopedLock lock(mMutex);
-  for(auto iter = mRunningTasks.begin(), endIter = mRunningTasks.end(); iter != endIter; ++iter)
   {
-    if((*iter).first == task)
+    Mutex::ScopedLock lock(mMutex);
+    for(auto iter = mRunningTasks.begin(), endIter = mRunningTasks.end(); iter != endIter; ++iter)
     {
-      if(!(*iter).second)
+      if((*iter).first == task)
       {
-        mCompletedTasks.push_back(task);
-      }
+        if(!(*iter).second)
+        {
+          if(task->GetCallbackInvocationThread() == AsyncTask::ThreadType::MAIN_THREAD)
+          {
+            mCompletedTasks.push_back(task);
+          }
+        }
 
-      // Delete this task in running queue
-      mRunningTasks.erase(iter);
-      break;
+        // Delete this task in running queue
+        mRunningTasks.erase(iter);
+        break;
+      }
     }
   }
 
   // wake up the main thread
-  mTrigger->Trigger();
+  if(task->GetCallbackInvocationThread() == AsyncTask::ThreadType::MAIN_THREAD)
+  {
+    mTrigger->Trigger();
+  }
+  else
+  {
+    CallbackBase::Execute(*(task->GetCompletedCallback()), task);
+  }
 }
 
 void AsyncTaskManager::UnregisterProcessor()
index 20a1a6c..2a2d58b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2022 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2023 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.
@@ -22,9 +22,9 @@
 
 namespace Dali
 {
-
-AsyncTask::AsyncTask(CallbackBase* callback)
-: mCompletedCallback(std::unique_ptr<CallbackBase>(callback))
+AsyncTask::AsyncTask(CallbackBase* callback, ThreadType threadType)
+: mCompletedCallback(std::unique_ptr<CallbackBase>(callback)),
+  mThreadType(threadType)
 {
 }
 
@@ -33,6 +33,11 @@ CallbackBase* AsyncTask::GetCompletedCallback()
   return mCompletedCallback.get();
 }
 
+AsyncTask::ThreadType AsyncTask::GetCallbackInvocationThread()
+{
+  return mThreadType;
+}
+
 AsyncTaskManager::AsyncTaskManager() = default;
 
 AsyncTaskManager::~AsyncTaskManager() = default;
index 02021c1..dd3ebc0 100644 (file)
@@ -2,7 +2,7 @@
 #define DALI_ASYNC_TASK_MANAGER_H
 
 /*
- * Copyright (c) 2022 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2023 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.
@@ -46,12 +46,20 @@ using AsyncTaskPtr = IntrusivePtr<AsyncTask>;
 class DALI_ADAPTOR_API AsyncTask : public RefObject
 {
 public:
+  // The Type of invocation thread
+  enum class ThreadType
+  {
+    MAIN_THREAD,
+    WORKER_THREAD
+  };
+
   /**
    * Constructor
    * @SINCE_2_2.3
-   * @param[in] callback The callback to up the main thread. The ownership of callback is taken by this class
+   * @param[in] callback The callback to invoke on task completion, either on the main thread on the worker thread. The ownership of callback is taken by this class.
+   * @param[in] threadType The thread type of invocation callback.
    */
-  AsyncTask(CallbackBase* callback);
+  AsyncTask(CallbackBase* callback, ThreadType threadType = AsyncTask::ThreadType::MAIN_THREAD);
 
   /**
    * Get the complated callback
@@ -60,6 +68,13 @@ public:
   CallbackBase* GetCompletedCallback();
 
   /**
+   * Get the thread of the invocation callback
+   * @SINCE_2_2.9
+   * @return the type of invocation callback.
+   */
+  ThreadType GetCallbackInvocationThread();
+
+  /**
    * Destructor.
    * @SINCE_2_2.3
    */
@@ -80,6 +95,7 @@ public:
 
 private:
   std::unique_ptr<CallbackBase> mCompletedCallback;
+  ThreadType                    mThreadType;
 
   // Undefined
   AsyncTask(const AsyncTask& task) = delete;