1 #ifndef DALI_ASYNC_TASK_MANAGER_H
2 #define DALI_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/public-api/common/intrusive-ptr.h>
22 #include <dali/public-api/object/base-handle.h>
23 #include <dali/public-api/signals/callback.h>
27 #include <dali/public-api/dali-adaptor-common.h>
31 namespace Internal DALI_INTERNAL
35 class AsyncTaskManager;
37 } // namespace DALI_INTERNAL
40 using AsyncTaskPtr = IntrusivePtr<AsyncTask>;
43 * The async tasks to be processed in the worker thread.
46 class DALI_ADAPTOR_API AsyncTask : public RefObject
49 // The Type of invocation thread
57 * @brief The priority of this task what user think.
58 * To avoid long term tasks (like remote image download) block whole threads,
59 * Let user set the priority type of this task.
61 * Low priority task means it doesn't need to process by FIFO logic.
62 * So we make that Low priority don't take whole threads.
64 * Task selection algorithm defined internally.
66 * @note Task cannot change the priority type after create.
70 enum class PriorityType
72 HIGH = 0, ///< Highest priority to process task. @SINCE_2_2.17
73 LOW = 1, ///< Lowest priority to process task. @SINCE_2_2.17
75 PRIORITY_COUNT, ///< The number of priority type. @SINCE_2_2.17
77 DEFAULT = HIGH, ///< Default priority value if nothing defined. @SINCE_2_2.17
83 * @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.
84 * @param[in] priority The proirity type of this task.
85 * @param[in] threadType The thread type of invocation callback.
87 AsyncTask(CallbackBase* callback, PriorityType priority = PriorityType::DEFAULT, ThreadType threadType = AsyncTask::ThreadType::MAIN_THREAD);
90 * Get the complated callback
93 CallbackBase* GetCompletedCallback();
96 * Get the thread of the invocation callback
98 * @return the type of invocation callback.
100 ThreadType GetCallbackInvocationThread();
103 * Get the priority of this task
105 * @return the type of priority.
107 PriorityType GetPriorityType() const;
113 virtual ~AsyncTask() = default;
119 virtual void Process() = 0;
122 * Whether the task is ready to process.
124 * @return True if the task is ready to process.
126 virtual bool IsReady() = 0;
129 std::unique_ptr<CallbackBase> mCompletedCallback;
130 const PriorityType mPriorityType;
131 ThreadType mThreadType;
134 AsyncTask(const AsyncTask& task) = delete;
137 AsyncTask& operator=(const AsyncTask& task) = delete;
141 * The manager for async task
144 class DALI_ADAPTOR_API AsyncTaskManager : public BaseHandle
160 * @brief Gets the singleton of AsyncTaskManager object.
163 * @return A handle to the AsyncTaskManager
165 static AsyncTaskManager Get();
168 * @brief Add the async task into the waiting queue, called by main thread.
171 * @param[in] task The task added to the queue.
173 void AddTask(AsyncTaskPtr task);
176 * @brief Remove the task from the waiting queue, called by main thread.
179 * @param[in] task The task pointer.
181 void RemoveTask(AsyncTaskPtr task);
186 * @brief Allows the creation of a AsyncTaskManager handle from an internal pointer.
188 * @note Not intended for application developers
190 * @param[in] impl A pointer to the object
192 explicit DALI_INTERNAL AsyncTaskManager(Internal::Adaptor::AsyncTaskManager* impl);