2 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/system/common/async-task-manager-impl.h>
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>
28 #include <unordered_map>
38 constexpr auto FORCE_TRIGGER_THRESHOLD = 128u; ///< Trigger TasksCompleted() forcely if the number of completed task contain too much.
40 constexpr auto DEFAULT_NUMBER_OF_ASYNC_THREADS = size_t{8u};
41 constexpr auto NUMBER_OF_ASYNC_THREADS_ENV = "DALI_ASYNC_MANAGER_THREAD_POOL_SIZE";
43 // The number of threads for low priority task.
44 constexpr auto DEFAULT_NUMBER_OF_LOW_PRIORITY_THREADS = size_t{6u};
45 constexpr auto NUMBER_OF_LOW_PRIORITY_THREADS_ENV = "DALI_ASYNC_MANAGER_LOW_PRIORITY_THREAD_POOL_SIZE";
47 size_t GetNumberOfThreads(const char* environmentVariable, size_t defaultValue)
49 auto numberString = EnvironmentVariable::GetEnvironmentVariable(environmentVariable);
50 auto numberOfThreads = numberString ? std::strtoul(numberString, nullptr, 10) : 0;
51 constexpr auto MAX_NUMBER_OF_THREADS = 16u;
52 DALI_ASSERT_DEBUG(numberOfThreads <= MAX_NUMBER_OF_THREADS);
53 return (numberOfThreads > 0 && numberOfThreads <= MAX_NUMBER_OF_THREADS) ? numberOfThreads : defaultValue;
56 size_t GetNumberOfLowPriorityThreads(const char* environmentVariable, size_t defaultValue, size_t maxValue)
58 auto numberString = EnvironmentVariable::GetEnvironmentVariable(environmentVariable);
59 auto numberOfThreads = numberString ? std::strtoul(numberString, nullptr, 10) : 0;
60 DALI_ASSERT_DEBUG(numberOfThreads <= maxValue);
61 return (numberOfThreads > 0 && numberOfThreads <= maxValue) ? numberOfThreads : std::min(defaultValue, maxValue);
64 #if defined(DEBUG_ENABLED)
65 Debug::Filter* gAsyncTasksManagerLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_ASYNC_TASK_MANAGER");
67 uint32_t gThreadId = 0u; // Only for debug
70 } // unnamed namespace
74 AsyncTaskThread::AsyncTaskThread(AsyncTaskManager& asyncTaskManager)
76 mAsyncTaskManager(asyncTaskManager),
77 mLogFactory(Dali::Adaptor::Get().GetLogFactory()),
78 mTraceFactory(Dali::Adaptor::Get().GetTraceFactory()),
79 mDestroyThread(false),
80 mIsThreadStarted(false),
85 AsyncTaskThread::~AsyncTaskThread()
89 ConditionalWait::ScopedLock lock(mConditionalWait);
90 mDestroyThread = true;
91 mConditionalWait.Notify(lock);
97 bool AsyncTaskThread::Request()
102 mIsThreadStarted = true;
106 // Lock while adding task to the queue
107 ConditionalWait::ScopedLock lock(mConditionalWait);
111 mIsThreadIdle = false;
113 // wake up the thread
114 mConditionalWait.Notify(lock);
122 void AsyncTaskThread::Run()
124 #if defined(DEBUG_ENABLED)
125 uint32_t threadId = gThreadId++;
128 snprintf(temp, 100, "AsyncTaskThread[%u]", threadId);
132 SetThreadName("AsyncTaskThread");
134 mLogFactory.InstallLogFunction();
135 mTraceFactory.InstallTraceFunction();
137 while(!mDestroyThread)
139 AsyncTaskPtr task = mAsyncTaskManager.PopNextTaskToProcess();
142 ConditionalWait::ScopedLock lock(mConditionalWait);
145 mIsThreadIdle = true;
146 DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::General, "Thread[%u] wait\n", threadId);
147 mConditionalWait.Wait(lock);
148 DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::General, "Thread[%u] awake\n", threadId);
153 DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::General, "Thread[%u] Process task [%p]\n", threadId, task.Get());
155 DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::General, "Thread[%u] Complete task [%p]\n", threadId, task.Get());
158 mAsyncTaskManager.CompleteTask(std::move(task));
164 // AsyncTaskManager::CacheImpl
166 struct AsyncTaskManager::CacheImpl
168 CacheImpl(AsyncTaskManager& manager)
174 // Insert / Erase task cache API.
177 * @brief Insert cache that input task.
178 * @pre Mutex be locked.
180 template<typename CacheContainer, typename Iterator>
181 static void InsertTaskCache(CacheContainer& cacheMap, AsyncTaskPtr task, Iterator iterator)
183 auto& cacheContainer = cacheMap[task.Get()]; // Get or Create cache container.
184 cacheContainer.insert(cacheContainer.end(), iterator);
188 * @brief Erase cache that input task.
189 * @pre Mutex be locked.
191 template<typename CacheContainer, typename Iterator>
192 static void EraseTaskCache(CacheContainer& cacheMap, AsyncTaskPtr task, Iterator iterator)
194 auto mapIter = cacheMap.find(task.Get());
195 if(mapIter != cacheMap.end())
197 auto& cacheContainer = (*mapIter).second;
198 auto cacheIter = std::find(cacheContainer.begin(), cacheContainer.end(), iterator);
200 if(cacheIter != cacheContainer.end())
202 cacheContainer.erase(cacheIter);
203 if(cacheContainer.empty())
205 cacheMap.erase(mapIter);
212 * @brief Erase all cache that input task.
213 * @pre Mutex be locked.
215 template<typename CacheContainer>
216 static void EraseAllTaskCache(CacheContainer& cacheMap, AsyncTaskPtr task)
218 auto mapIter = cacheMap.find(task.Get());
219 if(mapIter != cacheMap.end())
221 cacheMap.erase(mapIter);
226 AsyncTaskManager& mManager; ///< Owner of this CacheImpl.
228 // Keep cache iterators as list since we take tasks by FIFO as default.
229 using TaskCacheContainer = std::unordered_map<const AsyncTask*, std::list<AsyncTaskContainer::iterator>>;
230 using RunningTaskCacheContainer = std::unordered_map<const AsyncTask*, std::list<AsyncRunningTaskContainer::iterator>>;
231 using CompletedTaskCacheContainer = std::unordered_map<const AsyncTask*, std::list<AsyncCompletedTaskContainer::iterator>>;
233 TaskCacheContainer mWaitingTasksCache; ///< The cache of tasks and iterator for waiting to async process. Must be locked under mWaitingTasksMutex.
234 RunningTaskCacheContainer mRunningTasksCache; ///< The cache of tasks and iterator for running tasks. Must be locked under mRunningTasksMutex.
235 CompletedTaskCacheContainer mCompletedTasksCache; ///< The cache of tasks and iterator for completed async process. Must be locked under mCompletedTasksMutex.
240 Dali::AsyncTaskManager AsyncTaskManager::Get()
242 Dali::AsyncTaskManager manager;
243 SingletonService singletonService(SingletonService::Get());
246 // Check whether the async task manager is already created
247 Dali::BaseHandle handle = singletonService.GetSingleton(typeid(Dali::AsyncTaskManager));
250 // If so, downcast the handle of singleton
251 manager = Dali::AsyncTaskManager(dynamic_cast<Internal::Adaptor::AsyncTaskManager*>(handle.GetObjectPtr()));
256 // If not, create the async task manager and register it as a singleton
257 Internal::Adaptor::AsyncTaskManager* internalAsyncTaskManager = new Internal::Adaptor::AsyncTaskManager();
258 manager = Dali::AsyncTaskManager(internalAsyncTaskManager);
259 singletonService.Register(typeid(manager), manager);
265 AsyncTaskManager::AsyncTaskManager()
266 : mTasks(GetNumberOfThreads(NUMBER_OF_ASYNC_THREADS_ENV, DEFAULT_NUMBER_OF_ASYNC_THREADS), [&]() { return TaskHelper(*this); }),
267 mAvaliableLowPriorityTaskCounts(GetNumberOfLowPriorityThreads(NUMBER_OF_LOW_PRIORITY_THREADS_ENV, DEFAULT_NUMBER_OF_LOW_PRIORITY_THREADS, mTasks.GetElementCount())),
268 mWaitingHighProirityTaskCounts(0u),
269 mCacheImpl(new CacheImpl(*this)),
270 mTrigger(new EventThreadCallback(MakeCallback(this, &AsyncTaskManager::TasksCompleted))),
271 mProcessorRegistered(false)
275 AsyncTaskManager::~AsyncTaskManager()
277 if(mProcessorRegistered && Dali::Adaptor::IsAvailable())
279 mProcessorRegistered = false;
280 Dali::Adaptor::Get().UnregisterProcessor(*this);
286 // Remove cache impl after all threads are join.
289 // Remove tasks after CacheImpl removed
290 mWaitingTasks.clear();
291 mRunningTasks.clear();
292 mCompletedTasks.clear();
295 void AsyncTaskManager::AddTask(AsyncTaskPtr task)
299 // Lock while adding task to the queue
300 Mutex::ScopedLock lock(mWaitingTasksMutex);
302 DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::Verbose, "AddTask [%p]\n", task.Get());
304 // push back into waiting queue.
305 auto waitingIter = mWaitingTasks.insert(mWaitingTasks.end(), task);
306 CacheImpl::InsertTaskCache(mCacheImpl->mWaitingTasksCache, task, waitingIter);
308 if(task->GetPriorityType() == AsyncTask::PriorityType::HIGH)
310 // Increase the number of waiting tasks for high priority.
311 ++mWaitingHighProirityTaskCounts;
316 Mutex::ScopedLock lock(mRunningTasksMutex); // We can lock this mutex under mWaitingTasksMutex.
318 // Finish all Running threads are working
319 if(mRunningTasks.size() >= mTasks.GetElementCount())
326 size_t count = mTasks.GetElementCount();
328 while(index++ < count)
330 auto processHelperIt = mTasks.GetNext();
331 DALI_ASSERT_ALWAYS(processHelperIt != mTasks.End());
332 if(processHelperIt->Request())
336 // If all threads are busy, then it's ok just to push the task because they will try to get the next job.
339 // Register Process (Since mTrigger execute too late timing if event thread running a lots of events.)
340 if(!mProcessorRegistered && Dali::Adaptor::IsAvailable())
342 Dali::Adaptor::Get().RegisterProcessor(*this);
343 mProcessorRegistered = true;
349 void AsyncTaskManager::RemoveTask(AsyncTaskPtr task)
353 DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::Verbose, "RemoveTask [%p]\n", task.Get());
355 // Check whether we need to unregister processor.
356 // If there is some non-empty queue exist, we don't need to unregister processor.
357 bool needCheckUnregisterProcessor = true;
360 // Lock while remove task from the queue
361 Mutex::ScopedLock lock(mWaitingTasksMutex);
363 auto mapIter = mCacheImpl->mWaitingTasksCache.find(task.Get());
364 if(mapIter != mCacheImpl->mWaitingTasksCache.end())
366 for(auto& iterator : mapIter->second)
368 DALI_ASSERT_DEBUG((*iterator) == task);
369 if((*iterator)->GetPriorityType() == AsyncTask::PriorityType::HIGH)
371 // Decrease the number of waiting tasks for high priority.
372 --mWaitingHighProirityTaskCounts;
374 mWaitingTasks.erase(iterator);
376 CacheImpl::EraseAllTaskCache(mCacheImpl->mWaitingTasksCache, task);
379 if(!mWaitingTasks.empty())
381 needCheckUnregisterProcessor = false;
386 // Lock while remove task from the queue
387 Mutex::ScopedLock lock(mRunningTasksMutex);
389 auto mapIter = mCacheImpl->mRunningTasksCache.find(task.Get());
390 if(mapIter != mCacheImpl->mRunningTasksCache.end())
392 for(auto& iterator : mapIter->second)
394 DALI_ASSERT_DEBUG((*iterator).first == task);
395 // We cannot erase container. Just mark as canceled.
396 // Note : mAvaliableLowPriorityTaskCounts will be increased after process finished.
397 (*iterator).second = RunningTaskState::CANCELED;
401 if(!mRunningTasks.empty())
403 needCheckUnregisterProcessor = false;
408 // Lock while remove task from the queue
409 Mutex::ScopedLock lock(mCompletedTasksMutex);
411 auto mapIter = mCacheImpl->mCompletedTasksCache.find(task.Get());
412 if(mapIter != mCacheImpl->mCompletedTasksCache.end())
414 for(auto& iterator : mapIter->second)
416 DALI_ASSERT_DEBUG(iterator->first == task);
417 mCompletedTasks.erase(iterator);
419 CacheImpl::EraseAllTaskCache(mCacheImpl->mCompletedTasksCache, task);
422 if(!mCompletedTasks.empty())
424 needCheckUnregisterProcessor = false;
428 // UnregisterProcessor required to lock mutex. Call this API only if required.
429 if(needCheckUnregisterProcessor)
431 UnregisterProcessor();
436 AsyncTaskPtr AsyncTaskManager::PopNextCompletedTask()
438 // Lock while popping task out from the queue
439 Mutex::ScopedLock lock(mCompletedTasksMutex);
441 AsyncTaskPtr nextCompletedTask = nullptr;
443 while(!mCompletedTasks.empty())
445 DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::Verbose, "PopNextCompletedTask, completed task count : [%zu]\n", mCompletedTasks.size());
447 auto next = mCompletedTasks.begin();
448 AsyncTaskPtr nextTask = next->first;
449 CompletedTaskState taskState = next->second;
450 CacheImpl::EraseTaskCache(mCacheImpl->mCompletedTasksCache, nextTask, next);
451 mCompletedTasks.erase(next);
453 DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::General, "Completed task [%p] (callback required? : %d)\n", nextTask.Get(), taskState == CompletedTaskState::REQUIRE_CALLBACK);
455 if(taskState == CompletedTaskState::REQUIRE_CALLBACK)
457 nextCompletedTask = nextTask;
462 DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::General, "Pickup completed [%p]\n", nextCompletedTask.Get());
464 return nextCompletedTask;
467 void AsyncTaskManager::UnregisterProcessor()
469 if(mProcessorRegistered && Dali::Adaptor::IsAvailable())
471 DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::Verbose, "UnregisterProcessor begin\n");
472 // Keep processor at least 1 task exist.
473 // Please be careful the order of mutex, to avoid dead lock.
474 // TODO : Should we lock all mutex rightnow?
475 Mutex::ScopedLock lockWait(mWaitingTasksMutex);
476 if(mWaitingTasks.empty())
478 Mutex::ScopedLock lockRunning(mRunningTasksMutex); // We can lock this mutex under mWaitingTasksMutex.
479 if(mRunningTasks.empty())
481 Mutex::ScopedLock lockComplete(mCompletedTasksMutex); // We can lock this mutex under mWaitingTasksMutex and mRunningTasksMutex.
482 if(mCompletedTasks.empty())
484 mProcessorRegistered = false;
485 Dali::Adaptor::Get().UnregisterProcessor(*this);
489 DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::Verbose, "UnregisterProcessor end (registed? %d)\n", mProcessorRegistered);
493 void AsyncTaskManager::TasksCompleted()
495 DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::Verbose, "TasksCompleted begin\n");
496 while(AsyncTaskPtr task = PopNextCompletedTask())
498 DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::Verbose, "Execute callback [%p]\n", task.Get());
499 CallbackBase::Execute(*(task->GetCompletedCallback()), task);
502 UnregisterProcessor();
503 DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::Verbose, "TasksCompleted end\n");
506 void AsyncTaskManager::Process(bool postProcessor)
511 /// Worker thread called
512 AsyncTaskPtr AsyncTaskManager::PopNextTaskToProcess()
514 // Lock while popping task out from the queue
515 Mutex::ScopedLock lock(mWaitingTasksMutex);
517 DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::Verbose, "PopNextTaskToProcess, waiting task count : [%zu]\n", mWaitingTasks.size());
519 // pop out the next task from the queue
520 AsyncTaskPtr nextTask = nullptr;
522 // Fast cut if all waiting tasks are LOW priority, and we cannot excute low task anymore.
523 if(mWaitingHighProirityTaskCounts == 0u && !mWaitingTasks.empty())
526 Mutex::ScopedLock lock(mRunningTasksMutex); // We can lock this mutex under mWaitingTasksMutex.
528 if(mAvaliableLowPriorityTaskCounts == 0u)
530 // There are no avaliabe tasks to run now. Return nullptr.
535 for(auto iter = mWaitingTasks.begin(), endIter = mWaitingTasks.end(); iter != endIter; ++iter)
537 if((*iter)->IsReady())
539 const auto priorityType = (*iter)->GetPriorityType();
540 bool taskAvaliable = priorityType == AsyncTask::PriorityType::HIGH; // Task always valid if it's priority is high
544 Mutex::ScopedLock lock(mRunningTasksMutex); // We can lock this mutex under mWaitingTasksMutex.
546 taskAvaliable = (mAvaliableLowPriorityTaskCounts > 0u); // priority is low, but we can use it.
555 // Lock while popping task out from the queue
556 Mutex::ScopedLock lock(mRunningTasksMutex); // We can lock this mutex under mWaitingTasksMutex.
558 DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::Verbose, "Waiting -> Running [%p]\n", nextTask.Get());
560 auto runningIter = mRunningTasks.insert(mRunningTasks.end(), std::make_pair(nextTask, RunningTaskState::RUNNING));
561 CacheImpl::InsertTaskCache(mCacheImpl->mRunningTasksCache, nextTask, runningIter);
563 CacheImpl::EraseTaskCache(mCacheImpl->mWaitingTasksCache, nextTask, iter);
564 mWaitingTasks.erase(iter);
566 // Decrease avaliable task counts if it is low priority
567 if(priorityType == AsyncTask::PriorityType::LOW)
569 // We are under running task mutex. We can decrease it.
570 --mAvaliableLowPriorityTaskCounts;
574 if(priorityType == AsyncTask::PriorityType::HIGH)
576 // Decrease the number of waiting tasks for high priority.
577 --mWaitingHighProirityTaskCounts;
584 DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::General, "Pickup process [%p]\n", nextTask.Get());
589 /// Worker thread called
590 void AsyncTaskManager::CompleteTask(AsyncTaskPtr&& task)
596 bool needTrigger = (task->GetCallbackInvocationThread() == AsyncTask::ThreadType::MAIN_THREAD);
598 // Lock while check validation of task.
600 Mutex::ScopedLock lock(mRunningTasksMutex);
602 auto mapIter = mCacheImpl->mRunningTasksCache.find(task.Get());
603 if(mapIter != mCacheImpl->mRunningTasksCache.end())
605 const auto cacheIter = mapIter->second.begin();
606 DALI_ASSERT_ALWAYS(cacheIter != mapIter->second.end());
608 const auto iter = *cacheIter;
609 DALI_ASSERT_DEBUG(iter->first == task);
610 if(iter->second == RunningTaskState::RUNNING)
612 // This task is valid.
617 DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::Verbose, "CompleteTask [%p] (is notify? : %d)\n", task.Get(), notify);
620 // We should execute this tasks complete callback out of mutex
621 if(notify && task->GetCallbackInvocationThread() == AsyncTask::ThreadType::WORKER_THREAD)
623 DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::Verbose, "Execute callback on worker thread [%p]\n", task.Get());
624 CallbackBase::Execute(*(task->GetCompletedCallback()), task);
627 // Lock while adding task to the queue
629 Mutex::ScopedLock lock(mRunningTasksMutex);
631 auto mapIter = mCacheImpl->mRunningTasksCache.find(task.Get());
632 if(mapIter != mCacheImpl->mRunningTasksCache.end())
634 const auto cacheIter = mapIter->second.begin();
635 DALI_ASSERT_ALWAYS(cacheIter != mapIter->second.end());
637 const auto iter = *cacheIter;
638 const auto priorityType = iter->first->GetPriorityType();
639 // Increase avaliable task counts if it is low priority
640 if(priorityType == AsyncTask::PriorityType::LOW)
642 // We are under running task mutex. We can increase it.
643 ++mAvaliableLowPriorityTaskCounts;
646 // Move task into completed, for ensure that AsyncTask destroy at main thread.
648 Mutex::ScopedLock lock(mCompletedTasksMutex); // We can lock this mutex under mRunningTasksMutex.
650 const bool callbackRequired = notify && (task->GetCallbackInvocationThread() == AsyncTask::ThreadType::MAIN_THREAD);
652 DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::Verbose, "Running -> Completed [%p] (callback required? : %d)\n", task.Get(), callbackRequired);
654 auto completedIter = mCompletedTasks.insert(mCompletedTasks.end(), std::make_pair(task, callbackRequired ? CompletedTaskState::REQUIRE_CALLBACK : CompletedTaskState::SKIP_CALLBACK));
655 CacheImpl::InsertTaskCache(mCacheImpl->mCompletedTasksCache, task, completedIter);
657 CacheImpl::EraseTaskCache(mCacheImpl->mRunningTasksCache, task, iter);
658 mRunningTasks.erase(iter);
662 needTrigger |= (mCompletedTasks.size() >= FORCE_TRIGGER_THRESHOLD);
665 // Now, task is invalidate.
671 // Wake up the main thread
674 DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::Verbose, "Trigger main thread\n");
680 // AsyncTaskManager::TaskHelper
682 AsyncTaskManager::TaskHelper::TaskHelper(AsyncTaskManager& asyncTaskManager)
683 : TaskHelper(std::unique_ptr<AsyncTaskThread>(new AsyncTaskThread(asyncTaskManager)), asyncTaskManager)
687 AsyncTaskManager::TaskHelper::TaskHelper(TaskHelper&& rhs)
688 : TaskHelper(std::move(rhs.mProcessor), rhs.mAsyncTaskManager)
692 AsyncTaskManager::TaskHelper::TaskHelper(std::unique_ptr<AsyncTaskThread> processor, AsyncTaskManager& asyncTaskManager)
693 : mProcessor(std::move(processor)),
694 mAsyncTaskManager(asyncTaskManager)
698 bool AsyncTaskManager::TaskHelper::Request()
700 return mProcessor->Request();
702 } // namespace Adaptor
704 } // namespace Internal