1 #ifndef DALI_INTERNAL_ASYNC_TASK_MANAGER_H
2 #define DALI_INTERNAL_ASYNC_TASK_MANAGER_H
5 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 #include <dali/devel-api/adaptor-framework/event-thread-callback.h>
22 #include <dali/devel-api/threading/conditional-wait.h>
23 #include <dali/devel-api/threading/mutex.h>
24 #include <dali/devel-api/threading/thread.h>
25 #include <dali/integration-api/adaptor-framework/log-factory-interface.h>
26 #include <dali/integration-api/processor-interface.h>
27 #include <dali/public-api/object/base-object.h>
31 #include <dali/public-api/adaptor-framework/async-task-manager.h>
32 #include <dali/public-api/adaptor-framework/round-robin-container-view.h>
40 class AsyncTaskManager;
43 * The worker thread for async process
45 class AsyncTaskThread : public Thread
51 AsyncTaskThread(AsyncTaskManager& asyncTaskManager);
56 ~AsyncTaskThread() override;
59 * @brief Request the thread to process the task.
60 * @return True if the request is successed, otherwise false.
66 * The entry function of the worker thread.
72 AsyncTaskThread(const AsyncTaskThread& thread) = delete;
75 AsyncTaskThread& operator=(const AsyncTaskThread& thread) = delete;
78 ConditionalWait mConditionalWait;
79 AsyncTaskManager& mAsyncTaskManager;
80 const Dali::LogFactoryInterface& mLogFactory; ///< The log factory
82 bool mIsThreadStarted;
87 * The manager for async task
89 class AsyncTaskManager : public Dali::BaseObject, public Integration::Processor
95 * @return The AsyncTaskManager object
97 static Dali::AsyncTaskManager Get();
107 ~AsyncTaskManager() override;
110 * @copydoc Dali::AsyncTaskManager::AddTask()
112 void AddTask(AsyncTaskPtr task);
115 * @copydoc Dali::AsyncTaskManager::RemoveTask()
117 void RemoveTask(AsyncTaskPtr task);
120 * Pop the next task out from the queue.
122 * @return The next task to be processed.
124 AsyncTaskPtr PopNextTaskToProcess();
127 * Pop the next task out from the completed queue, called by main thread.
129 * @return The next task in the completed queue.
131 AsyncTaskPtr PopNextCompletedTask();
134 * Pop the next task out from the running queue and add this task to the completed queue.
136 * @param[in] task The task added to the queue.
138 void CompleteTask(AsyncTaskPtr task);
141 * @brief Unregister a previously registered processor
143 void UnregisterProcessor();
146 * Execute the callback registered by tasks in the completed queue
148 void TasksCompleted();
151 * @copydoc Dali::Integration::Processor::Process()
153 void Process(bool postProcessor) override;
157 * @brief Helper class to keep the relation between AsyncTaskThread and corresponding container
163 * @brief Create an TaskHelper.
165 * @param[in] asyncTaskManager Reference to the AsyncTaskManager
167 TaskHelper(AsyncTaskManager& asyncTaskManager);
170 * @brief Request the thread to process the task.
171 * @return True if the request succeeds, otherwise false.
176 TaskHelper(const TaskHelper&) = delete;
177 TaskHelper& operator=(const TaskHelper&) = delete;
179 TaskHelper(TaskHelper&& rhs);
180 TaskHelper& operator=(TaskHelper&& rhs) = delete;
184 * @brief Main constructor that used by all other constructors
186 TaskHelper(std::unique_ptr<AsyncTaskThread> processor, AsyncTaskManager& asyncTaskManager);
189 std::unique_ptr<AsyncTaskThread> mProcessor;
190 AsyncTaskManager& mAsyncTaskManager;
195 AsyncTaskManager(const AsyncTaskManager& manager);
198 AsyncTaskManager& operator=(const AsyncTaskManager& manager);
201 std::vector<AsyncTaskPtr> mWaitingTasks; //The queue of the tasks waiting to async process
202 std::vector<AsyncTaskPtr> mCompletedTasks; //The queue of the tasks with the async process
204 using AsyncTaskPair = std::pair<AsyncTaskPtr, bool>;
205 std::vector<AsyncTaskPair> mRunningTasks; ///< The queue of the running tasks
207 RoundRobinContainerView<TaskHelper> mTasks;
209 uint32_t mAvaliableLowPriorityTaskCounts; ///< The number of tasks that can be processed for proirity type LOW.
210 ///< Be used to select next wating task determining algorithm.
213 std::unique_ptr<EventThreadCallback> mTrigger;
214 bool mProcessorRegistered;
217 } // namespace Adaptor
219 } // namespace Internal
221 inline Internal::Adaptor::AsyncTaskManager& GetImplementation(Dali::AsyncTaskManager& obj)
223 DALI_ASSERT_ALWAYS(obj && "AsyncTaskManager is empty");
225 Dali::BaseObject& handle = obj.GetBaseObject();
227 return static_cast<Internal::Adaptor::AsyncTaskManager&>(handle);
230 inline const Internal::Adaptor::AsyncTaskManager& GetImplementation(const Dali::AsyncTaskManager& obj)
232 DALI_ASSERT_ALWAYS(obj && "AsyncTaskManager is empty");
234 const Dali::BaseObject& handle = obj.GetBaseObject();
236 return static_cast<const Internal::Adaptor::AsyncTaskManager&>(handle);