Merge "[AT-SPI] Rework intercepting key events" into devel/master
[platform/core/uifw/dali-adaptor.git] / dali / internal / system / common / async-task-manager-impl.cpp
index 1ff0f02..2f2a2bf 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.
@@ -24,7 +24,6 @@
 #include <dali/devel-api/common/singleton-service.h>
 #include <dali/integration-api/adaptor-framework/adaptor.h>
 #include <dali/integration-api/debug.h>
-#include <dali/integration-api/trace.h>
 
 #include <unordered_map>
 
@@ -68,8 +67,6 @@ Debug::Filter* gAsyncTasksManagerLogFilter = Debug::Filter::New(Debug::NoLogging
 uint32_t gThreadId = 0u; // Only for debug
 #endif
 
-DALI_INIT_TRACE_FILTER(gTraceFilter, DALI_TRACE_PERFORMANCE_MARKER, false);
-
 /**
  * @brief Get the Task Name.
  * Note that we can get const char* from std::string_view as data() since it will be const class.
@@ -167,9 +164,7 @@ void AsyncTaskThread::Run()
     else
     {
       DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::General, "Thread[%u] Process task [%p][%s]\n", threadId, task.Get(), GetTaskName(task));
-      DALI_TRACE_BEGIN(gTraceFilter, GetTaskName(task));
       task->Process();
-      DALI_TRACE_END(gTraceFilter, GetTaskName(task));
       DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::General, "Thread[%u] Complete task [%p][%s]\n", threadId, task.Get(), GetTaskName(task));
       if(!mDestroyThread)
       {
@@ -716,7 +711,7 @@ void AsyncTaskManager::RemoveTask(AsyncTaskPtr task)
         for(auto& iterator : mapIter->second)
         {
           DALI_ASSERT_DEBUG((*iterator) == task);
-          if((*iterator)->GetPriorityType() == AsyncTask::PriorityType::HIGH)
+          if((*iterator)->GetPriorityType() == AsyncTask::PriorityType::HIGH && mWaitingHighProirityTaskCounts > 0u)
           {
             // Decrease the number of waiting tasks for high priority.
             --mWaitingHighProirityTaskCounts;
@@ -1062,7 +1057,7 @@ AsyncTaskPtr AsyncTaskManager::PopNextTaskToProcess()
           }
         }
 
-        if(priorityType == AsyncTask::PriorityType::HIGH)
+        if(priorityType == AsyncTask::PriorityType::HIGH && mWaitingHighProirityTaskCounts > 0u)
         {
           // Decrease the number of waiting tasks for high priority.
           --mWaitingHighProirityTaskCounts;
@@ -1080,55 +1075,61 @@ AsyncTaskPtr AsyncTaskManager::PopNextTaskToProcess()
 /// Worker thread called
 void AsyncTaskManager::CompleteTask(AsyncTaskPtr&& task)
 {
-  bool notify = false;
-
   if(task)
   {
     bool needTrigger = false;
 
-    // Lock while check validation of task.
+    // Check now whether we need to execute callback or not, for worker thread cases.
+    if(task->GetCallbackInvocationThread() == AsyncTask::ThreadType::WORKER_THREAD)
     {
-      Mutex::ScopedLock lock(mRunningTasksMutex);
+      bool notify = false;
 
-      auto mapIter = mCacheImpl->mRunningTasksCache.find(task.Get());
-      if(mapIter != mCacheImpl->mRunningTasksCache.end())
+      // Lock while check validation of task.
       {
-        const auto cacheIter = mapIter->second.begin();
-        DALI_ASSERT_ALWAYS(cacheIter != mapIter->second.end());
+        Mutex::ScopedLock lock(mRunningTasksMutex);
 
-        const auto iter = *cacheIter;
-        DALI_ASSERT_DEBUG(iter->first == task);
-        if(iter->second == RunningTaskState::RUNNING)
+        auto mapIter = mCacheImpl->mRunningTasksCache.find(task.Get());
+        if(mapIter != mCacheImpl->mRunningTasksCache.end())
         {
-          // This task is valid.
-          notify = true;
-        }
-      }
+          const auto cacheIter = mapIter->second.begin();
+          DALI_ASSERT_ALWAYS(cacheIter != mapIter->second.end());
 
-      DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::Verbose, "CompleteTask [%p][%s] (is notify? : %d)\n", task.Get(), GetTaskName(task), notify);
-    }
+          const auto iter = *cacheIter;
+          DALI_ASSERT_DEBUG(iter->first == task);
+          if(iter->second == RunningTaskState::RUNNING)
+          {
+            // This task is valid.
+            notify = true;
+          }
+        }
 
-    // We should execute this tasks complete callback out of mutex
-    if(notify && task->GetCallbackInvocationThread() == AsyncTask::ThreadType::WORKER_THREAD)
-    {
-      DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::Verbose, "Execute callback on worker thread [%p][%s]\n", task.Get(), GetTaskName(task));
-      CallbackBase::Execute(*(task->GetCompletedCallback()), task);
+        DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::Verbose, "CompleteTask [%p][%s] (is notify? : %d)\n", task.Get(), GetTaskName(task), notify);
+      }
 
-      // We need to remove task trace now.
-      if(mTasksCompletedImpl->IsTasksCompletedCallbackExist())
+      // We should execute this tasks complete callback out of mutex
+      if(notify)
       {
-        mTasksCompletedImpl->RemoveTaskTrace(task);
+        DALI_LOG_INFO(gAsyncTasksManagerLogFilter, Debug::Verbose, "Execute callback on worker thread [%p][%s]\n", task.Get(), GetTaskName(task));
+        CallbackBase::Execute(*(task->GetCompletedCallback()), task);
 
-        if(mTasksCompletedImpl->IsExecuteCallbackExist())
+        // We need to remove task trace now.
+        if(mTasksCompletedImpl->IsTasksCompletedCallbackExist())
         {
-          // We need to call EmitCompletedTasks(). Trigger main thread.
-          needTrigger = true;
+          mTasksCompletedImpl->RemoveTaskTrace(task);
+
+          if(mTasksCompletedImpl->IsExecuteCallbackExist())
+          {
+            // We need to call EmitCompletedTasks(). Trigger main thread.
+            needTrigger = true;
+          }
         }
       }
     }
 
     // Lock while adding task to the queue
     {
+      bool notify = false;
+
       Mutex::ScopedLock lock(mRunningTasksMutex);
 
       auto mapIter = mCacheImpl->mRunningTasksCache.find(task.Get());
@@ -1137,7 +1138,15 @@ void AsyncTaskManager::CompleteTask(AsyncTaskPtr&& task)
         const auto cacheIter = mapIter->second.begin();
         DALI_ASSERT_ALWAYS(cacheIter != mapIter->second.end());
 
-        const auto iter         = *cacheIter;
+        const auto iter = *cacheIter;
+
+        DALI_ASSERT_DEBUG(iter->first == task);
+        if(iter->second == RunningTaskState::RUNNING)
+        {
+          // This task is valid.
+          notify = true;
+        }
+
         const auto priorityType = iter->first->GetPriorityType();
         // Increase avaliable task counts if it is low priority
         if(priorityType == AsyncTask::PriorityType::LOW)