Fix coverity issue (copy data during iteration)
[platform/core/uifw/dali-adaptor.git] / dali / internal / system / common / async-task-manager-impl.cpp
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/system/common/async-task-manager-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/devel-api/adaptor-framework/environment-variable.h>
23 #include <dali/devel-api/adaptor-framework/thread-settings.h>
24 #include <dali/devel-api/common/singleton-service.h>
25 #include <dali/integration-api/adaptor-framework/adaptor.h>
26 #include <dali/integration-api/debug.h>
27
28 #include <unordered_map>
29
30 namespace Dali
31 {
32 namespace Internal
33 {
34 namespace Adaptor
35 {
36 namespace
37 {
38 constexpr auto DEFAULT_NUMBER_OF_ASYNC_THREADS = size_t{8u};
39 constexpr auto NUMBER_OF_ASYNC_THREADS_ENV     = "DALI_ASYNC_MANAGER_THREAD_POOL_SIZE";
40
41 // The number of threads for low priority task.
42 constexpr auto DEFAULT_NUMBER_OF_LOW_PRIORITY_THREADS = size_t{6u};
43 constexpr auto NUMBER_OF_LOW_PRIORITY_THREADS_ENV     = "DALI_ASYNC_MANAGER_LOW_PRIORITY_THREAD_POOL_SIZE";
44
45 size_t GetNumberOfThreads(const char* environmentVariable, size_t defaultValue)
46 {
47   auto           numberString          = EnvironmentVariable::GetEnvironmentVariable(environmentVariable);
48   auto           numberOfThreads       = numberString ? std::strtoul(numberString, nullptr, 10) : 0;
49   constexpr auto MAX_NUMBER_OF_THREADS = 16u;
50   DALI_ASSERT_DEBUG(numberOfThreads <= MAX_NUMBER_OF_THREADS);
51   return (numberOfThreads > 0 && numberOfThreads <= MAX_NUMBER_OF_THREADS) ? numberOfThreads : defaultValue;
52 }
53
54 size_t GetNumberOfLowPriorityThreads(const char* environmentVariable, size_t defaultValue, size_t maxValue)
55 {
56   auto numberString    = EnvironmentVariable::GetEnvironmentVariable(environmentVariable);
57   auto numberOfThreads = numberString ? std::strtoul(numberString, nullptr, 10) : 0;
58   DALI_ASSERT_DEBUG(numberOfThreads <= maxValue);
59   return (numberOfThreads > 0 && numberOfThreads <= maxValue) ? numberOfThreads : std::min(defaultValue, maxValue);
60 }
61
62 #if defined(DEBUG_ENABLED)
63 Debug::Filter* gAsyncTasksManagerLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_ASYNC_TASK_MANAGER");
64
65 uint32_t gThreadId = 0u; // Only for debug
66 #endif
67
68 } // unnamed namespace
69
70 // AsyncTaskThread
71
72 AsyncTaskThread::AsyncTaskThread(AsyncTaskManager& asyncTaskManager)
73 : mConditionalWait(),
74   mAsyncTaskManager(asyncTaskManager),
75   mLogFactory(Dali::Adaptor::Get().GetLogFactory()),
76   mDestroyThread(false),
77   mIsThreadStarted(false),
78   mIsThreadIdle(true)
79 {
80 }
81
82 AsyncTaskThread::~AsyncTaskThread()
83 {
84   // Stop the thread
85   {
86     ConditionalWait::ScopedLock lock(mConditionalWait);
87     mDestroyThread = true;
88     mConditionalWait.Notify(lock);
89   }
90
91   Join();
92 }
93
94 bool AsyncTaskThread::Request()
95 {
96   if(!mIsThreadStarted)
97   {
98     Start();
99     mIsThreadStarted = true;
100   }
101
102   {
103     // Lock while adding task to the queue
104     ConditionalWait::ScopedLock lock(mConditionalWait);
105
106     if(mIsThreadIdle)
107     {
108       mIsThreadIdle = false;
109
110       // wake up the thread
111       mConditionalWait.Notify(lock);
112       return true;
113     }
114   }
115
116   return false;
117 }
118
119 void AsyncTaskThread::Run()
120 {
121 #if defined(DEBUG_ENABLED)
122   uint32_t threadId = gThreadId++;
123   {
124     char temp[100];
125     snprintf(temp, 100, "AsyncTaskThread[%u]", threadId);
126     SetThreadName(temp);
127   }
128 #else
129   SetThreadName("AsyncTaskThread");
130 #endif
131   mLogFactory.InstallLogFunction();
132
133   while(!mDestroyThread)
134   {
135     AsyncTaskPtr task = mAsyncTaskManager.PopNextTaskToProcess();
136     if(!task)
137     {
138       ConditionalWait::ScopedLock lock(mConditionalWait);
139       if(!mDestroyThread)
140       {
141         mIsThreadIdle = true;
142         DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::General, "Thread[%u] wait\n", threadId);
143         mConditionalWait.Wait(lock);
144         DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::General, "Thread[%u] awake\n", threadId);
145       }
146     }
147     else
148     {
149       DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::General, "Thread[%u] Process task [%p]\n", threadId, task.Get());
150       task->Process();
151       DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::General, "Thread[%u] Complete task [%p]\n", threadId, task.Get());
152       if(!mDestroyThread)
153       {
154         mAsyncTaskManager.CompleteTask(task);
155       }
156     }
157   }
158 }
159
160 // AsyncTaskManager::CacheImpl
161
162 struct AsyncTaskManager::CacheImpl
163 {
164   CacheImpl(AsyncTaskManager& manager)
165   : mManager(manager)
166   {
167   }
168
169 public:
170   // Insert / Erase task cache API.
171
172   /**
173    * @brief Insert cache that input task.
174    * @pre Mutex be locked.
175    */
176   template<typename CacheContainer, typename Iterator>
177   static void InsertTaskCache(CacheContainer& cacheMap, AsyncTaskPtr task, Iterator iterator)
178   {
179     auto& cacheContainer = cacheMap[task.Get()]; // Get or Create cache container.
180     cacheContainer.insert(cacheContainer.end(), iterator);
181   }
182
183   /**
184    * @brief Erase cache that input task.
185    * @pre Mutex be locked.
186    */
187   template<typename CacheContainer, typename Iterator>
188   static void EraseTaskCache(CacheContainer& cacheMap, AsyncTaskPtr task, Iterator iterator)
189   {
190     auto mapIter = cacheMap.find(task.Get());
191     if(mapIter != cacheMap.end())
192     {
193       auto& cacheContainer = (*mapIter).second;
194       auto  cacheIter      = std::find(cacheContainer.begin(), cacheContainer.end(), iterator);
195
196       if(cacheIter != cacheContainer.end())
197       {
198         cacheContainer.erase(cacheIter);
199         if(cacheContainer.empty())
200         {
201           cacheMap.erase(mapIter);
202         }
203       }
204     }
205   }
206
207   /**
208    * @brief Erase all cache that input task.
209    * @pre Mutex be locked.
210    */
211   template<typename CacheContainer>
212   static void EraseAllTaskCache(CacheContainer& cacheMap, AsyncTaskPtr task)
213   {
214     auto mapIter = cacheMap.find(task.Get());
215     if(mapIter != cacheMap.end())
216     {
217       cacheMap.erase(mapIter);
218     }
219   }
220
221 public:
222   AsyncTaskManager& mManager; ///< Owner of this CacheImpl.
223
224   // Keep cache iterators as list since we take tasks by FIFO as default.
225   using TaskCacheContainer        = std::unordered_map<const AsyncTask*, std::list<AsyncTaskContainer::iterator>>;
226   using RunningTaskCacheContainer = std::unordered_map<const AsyncTask*, std::list<AsyncRunningTaskContainer::iterator>>;
227
228   TaskCacheContainer        mWaitingTasksCache;   ///< The cache of tasks and iterator for waiting to async process. Must be locked under mWaitingTasksMutex.
229   RunningTaskCacheContainer mRunningTasksCache;   ///< The cache of tasks and iterator for running tasks. Must be locked under mRunningTasksMutex.
230   TaskCacheContainer        mCompletedTasksCache; ///< The cache of tasks and iterator for completed async process. Must be locked under mCompletedTasksMutex.
231 };
232
233 // AsyncTaskManager
234
235 Dali::AsyncTaskManager AsyncTaskManager::Get()
236 {
237   Dali::AsyncTaskManager manager;
238   SingletonService       singletonService(SingletonService::Get());
239   if(singletonService)
240   {
241     // Check whether the async task manager is already created
242     Dali::BaseHandle handle = singletonService.GetSingleton(typeid(Dali::AsyncTaskManager));
243     if(handle)
244     {
245       // If so, downcast the handle of singleton
246       manager = Dali::AsyncTaskManager(dynamic_cast<Internal::Adaptor::AsyncTaskManager*>(handle.GetObjectPtr()));
247     }
248
249     if(!manager)
250     {
251       // If not, create the async task manager and register it as a singleton
252       Internal::Adaptor::AsyncTaskManager* internalAsyncTaskManager = new Internal::Adaptor::AsyncTaskManager();
253       manager                                                       = Dali::AsyncTaskManager(internalAsyncTaskManager);
254       singletonService.Register(typeid(manager), manager);
255     }
256   }
257   return manager;
258 }
259
260 AsyncTaskManager::AsyncTaskManager()
261 : mTasks(GetNumberOfThreads(NUMBER_OF_ASYNC_THREADS_ENV, DEFAULT_NUMBER_OF_ASYNC_THREADS), [&]() { return TaskHelper(*this); }),
262   mAvaliableLowPriorityTaskCounts(GetNumberOfLowPriorityThreads(NUMBER_OF_LOW_PRIORITY_THREADS_ENV, DEFAULT_NUMBER_OF_LOW_PRIORITY_THREADS, mTasks.GetElementCount())),
263   mWaitingHighProirityTaskCounts(0u),
264   mCacheImpl(new CacheImpl(*this)),
265   mTrigger(new EventThreadCallback(MakeCallback(this, &AsyncTaskManager::TasksCompleted))),
266   mProcessorRegistered(false)
267 {
268 }
269
270 AsyncTaskManager::~AsyncTaskManager()
271 {
272   if(mProcessorRegistered && Dali::Adaptor::IsAvailable())
273   {
274     mProcessorRegistered = false;
275     Dali::Adaptor::Get().UnregisterProcessor(*this);
276   }
277
278   mTasks.Clear();
279 }
280
281 void AsyncTaskManager::AddTask(AsyncTaskPtr task)
282 {
283   if(task)
284   {
285     // Lock while adding task to the queue
286     Mutex::ScopedLock lock(mWaitingTasksMutex);
287
288     DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::Verbose, "AddTask [%p]\n", task.Get());
289
290     // push back into waiting queue.
291     auto waitingIter = mWaitingTasks.insert(mWaitingTasks.end(), task);
292     CacheImpl::InsertTaskCache(mCacheImpl->mWaitingTasksCache, task, waitingIter);
293
294     if(task->GetPriorityType() == AsyncTask::PriorityType::HIGH)
295     {
296       // Increase the number of waiting tasks for high priority.
297       ++mWaitingHighProirityTaskCounts;
298     }
299
300     {
301       // For thread safety
302       Mutex::ScopedLock lock(mRunningTasksMutex); // We can lock this mutex under mWaitingTasksMutex.
303
304       // Finish all Running threads are working
305       if(mRunningTasks.size() >= mTasks.GetElementCount())
306       {
307         return;
308       }
309     }
310   }
311
312   size_t count = mTasks.GetElementCount();
313   size_t index = 0;
314   while(index++ < count)
315   {
316     auto processHelperIt = mTasks.GetNext();
317     DALI_ASSERT_ALWAYS(processHelperIt != mTasks.End());
318     if(processHelperIt->Request())
319     {
320       break;
321     }
322     // If all threads are busy, then it's ok just to push the task because they will try to get the next job.
323   }
324
325   // Register Process (Since mTrigger execute too late timing if event thread running a lots of events.)
326   if(!mProcessorRegistered && Dali::Adaptor::IsAvailable())
327   {
328     Dali::Adaptor::Get().RegisterProcessor(*this);
329     mProcessorRegistered = true;
330   }
331
332   return;
333 }
334
335 void AsyncTaskManager::RemoveTask(AsyncTaskPtr task)
336 {
337   if(task)
338   {
339     DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::Verbose, "RemoveTask [%p]\n", task.Get());
340
341     // Check whether we need to unregister processor.
342     // If there is some non-empty queue exist, we don't need to unregister processor.
343     bool needCheckUnregisterProcessor = true;
344
345     {
346       // Lock while remove task from the queue
347       Mutex::ScopedLock lock(mWaitingTasksMutex);
348
349       auto mapIter = mCacheImpl->mWaitingTasksCache.find(task.Get());
350       if(mapIter != mCacheImpl->mWaitingTasksCache.end())
351       {
352         for(auto& iterator : mapIter->second)
353         {
354           DALI_ASSERT_DEBUG((*iterator) == task);
355           if((*iterator)->GetPriorityType() == AsyncTask::PriorityType::HIGH)
356           {
357             // Decrease the number of waiting tasks for high priority.
358             --mWaitingHighProirityTaskCounts;
359           }
360           mWaitingTasks.erase(iterator);
361         }
362         CacheImpl::EraseAllTaskCache(mCacheImpl->mWaitingTasksCache, task);
363       }
364
365       if(!mWaitingTasks.empty())
366       {
367         needCheckUnregisterProcessor = false;
368       }
369     }
370
371     {
372       // Lock while remove task from the queue
373       Mutex::ScopedLock lock(mRunningTasksMutex);
374
375       auto mapIter = mCacheImpl->mRunningTasksCache.find(task.Get());
376       if(mapIter != mCacheImpl->mRunningTasksCache.end())
377       {
378         for(auto& iterator : mapIter->second)
379         {
380           DALI_ASSERT_DEBUG((*iterator).first == task);
381           // We cannot erase container. Just mark as canceled.
382           // Note : mAvaliableLowPriorityTaskCounts will be increased after process finished.
383           (*iterator).second = RunningTaskState::CANCELED;
384         }
385       }
386
387       if(!mRunningTasks.empty())
388       {
389         needCheckUnregisterProcessor = false;
390       }
391     }
392
393     {
394       // Lock while remove task from the queue
395       Mutex::ScopedLock lock(mCompletedTasksMutex);
396
397       auto mapIter = mCacheImpl->mCompletedTasksCache.find(task.Get());
398       if(mapIter != mCacheImpl->mCompletedTasksCache.end())
399       {
400         for(auto& iterator : mapIter->second)
401         {
402           DALI_ASSERT_DEBUG((*iterator) == task);
403           mCompletedTasks.erase(iterator);
404         }
405         CacheImpl::EraseAllTaskCache(mCacheImpl->mCompletedTasksCache, task);
406       }
407
408       if(!mCompletedTasks.empty())
409       {
410         needCheckUnregisterProcessor = false;
411       }
412     }
413
414     // UnregisterProcessor required to lock mutex. Call this API only if required.
415     if(needCheckUnregisterProcessor)
416     {
417       UnregisterProcessor();
418     }
419   }
420 }
421
422 AsyncTaskPtr AsyncTaskManager::PopNextCompletedTask()
423 {
424   // Lock while popping task out from the queue
425   Mutex::ScopedLock lock(mCompletedTasksMutex);
426
427   if(mCompletedTasks.empty())
428   {
429     return AsyncTaskPtr();
430   }
431
432   DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::Verbose, "PopNextCompletedTask, completed task count : [%zu]\n", mCompletedTasks.size());
433
434   auto         next     = mCompletedTasks.begin();
435   AsyncTaskPtr nextTask = *next;
436   CacheImpl::EraseTaskCache(mCacheImpl->mCompletedTasksCache, nextTask, next);
437   mCompletedTasks.erase(next);
438
439   DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::General, "Pickup completed [%p]\n", nextTask.Get());
440
441   return nextTask;
442 }
443
444 void AsyncTaskManager::UnregisterProcessor()
445 {
446   if(mProcessorRegistered && Dali::Adaptor::IsAvailable())
447   {
448     DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::Verbose, "UnregisterProcessor begin\n");
449     // Keep processor at least 1 task exist.
450     // Please be careful the order of mutex, to avoid dead lock.
451     // TODO : Should we lock all mutex rightnow?
452     Mutex::ScopedLock lockWait(mWaitingTasksMutex);
453     if(mWaitingTasks.empty())
454     {
455       Mutex::ScopedLock lockRunning(mRunningTasksMutex); // We can lock this mutex under mWaitingTasksMutex.
456       if(mRunningTasks.empty())
457       {
458         Mutex::ScopedLock lockComplete(mCompletedTasksMutex); // We can lock this mutex under mWaitingTasksMutex and mRunningTasksMutex.
459         if(mCompletedTasks.empty())
460         {
461           mProcessorRegistered = false;
462           Dali::Adaptor::Get().UnregisterProcessor(*this);
463         }
464       }
465     }
466     DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::Verbose, "UnregisterProcessor end (registed? %d)\n", mProcessorRegistered);
467   }
468 }
469
470 void AsyncTaskManager::TasksCompleted()
471 {
472   DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::Verbose, "TasksCompleted begin\n");
473   while(AsyncTaskPtr task = PopNextCompletedTask())
474   {
475     DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::Verbose, "Execute callback [%p]\n", task.Get());
476     CallbackBase::Execute(*(task->GetCompletedCallback()), task);
477   }
478
479   UnregisterProcessor();
480   DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::Verbose, "TasksCompleted end\n");
481 }
482
483 void AsyncTaskManager::Process(bool postProcessor)
484 {
485   TasksCompleted();
486 }
487
488 /// Worker thread called
489 AsyncTaskPtr AsyncTaskManager::PopNextTaskToProcess()
490 {
491   // Lock while popping task out from the queue
492   Mutex::ScopedLock lock(mWaitingTasksMutex);
493
494   DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::Verbose, "PopNextTaskToProcess, waiting task count : [%zu]\n", mWaitingTasks.size());
495
496   // pop out the next task from the queue
497   AsyncTaskPtr nextTask = nullptr;
498
499   // Fast cut if all waiting tasks are LOW priority, and we cannot excute low task anymore.
500   if(mWaitingHighProirityTaskCounts == 0u && !mWaitingTasks.empty())
501   {
502     // For thread safety
503     Mutex::ScopedLock lock(mRunningTasksMutex); // We can lock this mutex under mWaitingTasksMutex.
504
505     if(mAvaliableLowPriorityTaskCounts == 0u)
506     {
507       // There are no avaliabe tasks to run now. Return nullptr.
508       return nextTask;
509     }
510   }
511
512   for(auto iter = mWaitingTasks.begin(), endIter = mWaitingTasks.end(); iter != endIter; ++iter)
513   {
514     if((*iter)->IsReady())
515     {
516       const auto priorityType  = (*iter)->GetPriorityType();
517       bool       taskAvaliable = priorityType == AsyncTask::PriorityType::HIGH; // Task always valid if it's priority is high
518       if(!taskAvaliable)
519       {
520         // For thread safety
521         Mutex::ScopedLock lock(mRunningTasksMutex); // We can lock this mutex under mWaitingTasksMutex.
522
523         taskAvaliable = (mAvaliableLowPriorityTaskCounts > 0u); // priority is low, but we can use it.
524       }
525
526       if(taskAvaliable)
527       {
528         nextTask = *iter;
529
530         // Add Running queue
531         {
532           // Lock while popping task out from the queue
533           Mutex::ScopedLock lock(mRunningTasksMutex); // We can lock this mutex under mWaitingTasksMutex.
534
535           DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::Verbose, "Waiting -> Running [%p]\n", nextTask.Get());
536
537           auto runningIter = mRunningTasks.insert(mRunningTasks.end(), std::make_pair(nextTask, RunningTaskState::RUNNING));
538           CacheImpl::InsertTaskCache(mCacheImpl->mRunningTasksCache, nextTask, runningIter);
539
540           // Decrease avaliable task counts if it is low priority
541           if(priorityType == AsyncTask::PriorityType::LOW)
542           {
543             // We are under running task mutex. We can decrease it.
544             --mAvaliableLowPriorityTaskCounts;
545           }
546         }
547
548         if(priorityType == AsyncTask::PriorityType::HIGH)
549         {
550           // Decrease the number of waiting tasks for high priority.
551           --mWaitingHighProirityTaskCounts;
552         }
553
554         CacheImpl::EraseTaskCache(mCacheImpl->mWaitingTasksCache, nextTask, iter);
555         mWaitingTasks.erase(iter);
556         break;
557       }
558     }
559   }
560
561   DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::General, "Pickup process [%p]\n", nextTask.Get());
562
563   return nextTask;
564 }
565
566 /// Worker thread called
567 void AsyncTaskManager::CompleteTask(AsyncTaskPtr task)
568 {
569   bool notify = false;
570
571   // Lock while adding task to the queue
572   if(task)
573   {
574     Mutex::ScopedLock lock(mRunningTasksMutex);
575
576     auto mapIter = mCacheImpl->mRunningTasksCache.find(task.Get());
577     if(mapIter != mCacheImpl->mRunningTasksCache.end())
578     {
579       const auto cacheIter = mapIter->second.begin();
580       DALI_ASSERT_ALWAYS(cacheIter != mapIter->second.end());
581
582       const auto iter = *cacheIter;
583       DALI_ASSERT_DEBUG(iter->first == task);
584       if(iter->second == RunningTaskState::RUNNING)
585       {
586         // This task is valid.
587         notify = true;
588       }
589
590       const auto priorityType = iter->first->GetPriorityType();
591       // Increase avaliable task counts if it is low priority
592       if(priorityType == AsyncTask::PriorityType::LOW)
593       {
594         // We are under running task mutex. We can increase it.
595         ++mAvaliableLowPriorityTaskCounts;
596       }
597       CacheImpl::EraseTaskCache(mCacheImpl->mRunningTasksCache, task, iter);
598       mRunningTasks.erase(iter);
599     }
600
601     DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::Verbose, "CompleteTask [%p] (is notify? : %d)\n", task.Get(), notify);
602
603     // We should move the task to compeleted task under mRunningTaskMutex.
604     if(notify && task->GetCallbackInvocationThread() == AsyncTask::ThreadType::MAIN_THREAD)
605     {
606       Mutex::ScopedLock lock(mCompletedTasksMutex); // We can lock this mutex under mRunningTasksMutex.
607
608       DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::Verbose, "Running -> Completed [%p]\n", task.Get());
609
610       auto completedIter = mCompletedTasks.insert(mCompletedTasks.end(), task);
611       CacheImpl::InsertTaskCache(mCacheImpl->mCompletedTasksCache, task, completedIter);
612     }
613   }
614
615   // We should execute this tasks complete callback out of mutex
616   if(notify)
617   {
618     if(task->GetCallbackInvocationThread() == AsyncTask::ThreadType::MAIN_THREAD)
619     {
620       // wake up the main thread
621       DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::Verbose, "Trigger main thread\n");
622       mTrigger->Trigger();
623     }
624     else // task->GetCallbackInvocationThread() == AsyncTask::ThreadType::WORKER_THREAD
625     {
626       DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::Verbose, "Execute callback on worker thread [%p]\n", task.Get());
627       CallbackBase::Execute(*(task->GetCompletedCallback()), task);
628     }
629   }
630 }
631
632 // AsyncTaskManager::TaskHelper
633
634 AsyncTaskManager::TaskHelper::TaskHelper(AsyncTaskManager& asyncTaskManager)
635 : TaskHelper(std::unique_ptr<AsyncTaskThread>(new AsyncTaskThread(asyncTaskManager)), asyncTaskManager)
636 {
637 }
638
639 AsyncTaskManager::TaskHelper::TaskHelper(TaskHelper&& rhs)
640 : TaskHelper(std::move(rhs.mProcessor), rhs.mAsyncTaskManager)
641 {
642 }
643
644 AsyncTaskManager::TaskHelper::TaskHelper(std::unique_ptr<AsyncTaskThread> processor, AsyncTaskManager& asyncTaskManager)
645 : mProcessor(std::move(processor)),
646   mAsyncTaskManager(asyncTaskManager)
647 {
648 }
649
650 bool AsyncTaskManager::TaskHelper::Request()
651 {
652   return mProcessor->Request();
653 }
654 } // namespace Adaptor
655
656 } // namespace Internal
657
658 } // namespace Dali