Add TasksCompleted signal at AsyncTaskManager
[platform/core/uifw/dali-adaptor.git] / dali / public-api / adaptor-framework / async-task-manager.h
index dd3ebc0..bc893c7 100644 (file)
@@ -54,12 +54,37 @@ public:
   };
 
   /**
+   * @brief The priority of this task what user think.
+   * To avoid long term tasks (like remote image download) block whole threads,
+   * Let user set the priority type of this task.
+   *
+   * Low priority task means it doesn't need to process by FIFO logic.
+   * So we make that Low priority don't take whole threads.
+   *
+   * Task selection algorithm defined internally.
+   *
+   * @note Task cannot change the priority type after create.
+   *
+   * @SINCE_2_2.17
+   */
+  enum class PriorityType
+  {
+    HIGH = 0, ///< Highest priority to process task. @SINCE_2_2.17
+    LOW  = 1, ///< Lowest priority to process task. @SINCE_2_2.17
+
+    PRIORITY_COUNT, ///< The number of priority type. @SINCE_2_2.17
+
+    DEFAULT = HIGH, ///< Default priority value if nothing defined. @SINCE_2_2.17
+  };
+
+  /**
    * Constructor
    * @SINCE_2_2.3
    * @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.
+   * @param[in] priority The proirity type of this task.
    * @param[in] threadType The thread type of invocation callback.
    */
-  AsyncTask(CallbackBase* callback, ThreadType threadType = AsyncTask::ThreadType::MAIN_THREAD);
+  AsyncTask(CallbackBase* callback, PriorityType priority = PriorityType::DEFAULT, ThreadType threadType = AsyncTask::ThreadType::MAIN_THREAD);
 
   /**
    * Get the complated callback
@@ -72,7 +97,14 @@ public:
    * @SINCE_2_2.9
    * @return the type of invocation callback.
    */
-  ThreadType GetCallbackInvocationThread();
+  ThreadType GetCallbackInvocationThread() const;
+
+  /**
+   * Get the priority of this task
+   * @SINCE_2_2.17
+   * @return the type of priority.
+   */
+  PriorityType GetPriorityType() const;
 
   /**
    * Destructor.
@@ -95,6 +127,7 @@ public:
 
 private:
   std::unique_ptr<CallbackBase> mCompletedCallback;
+  const PriorityType            mPriorityType;
   ThreadType                    mThreadType;
 
   // Undefined
@@ -148,6 +181,81 @@ public:
   void RemoveTask(AsyncTaskPtr task);
 
 public:
+  using TasksCompletedId = uint32_t;
+
+  enum CompletedCallbackTraceMask
+  {
+    THREAD_MASK_MAIN   = 1u << 0, ///< Trace only main thread tasks.
+    THREAD_MASK_WORKER = 1u << 1, ///< Trace only worker thread tasks.
+
+    PRIORITY_MASK_HIGH = 1u << 2, ///< Trace only high priority tasks.
+    PRIORITY_MASK_LOW  = 1u << 3, ///< Trace only low priority tasks.
+
+    THREAD_MASK_ALL   = THREAD_MASK_MAIN | THREAD_MASK_WORKER,
+    PRIORITY_MASK_ALL = PRIORITY_MASK_HIGH | PRIORITY_MASK_LOW,
+
+    // Useful preset of task mask.
+
+    MAIN_THREAD_TASKS   = THREAD_MASK_MAIN | PRIORITY_MASK_ALL,
+    WORKER_THREAD_TASKS = THREAD_MASK_WORKER | PRIORITY_MASK_ALL,
+    HIGH_PRIORITY_TASKS = THREAD_MASK_ALL | PRIORITY_MASK_HIGH,
+    LOW_PRIORITY_TASKS  = THREAD_MASK_ALL | PRIORITY_MASK_LOW,
+
+    MAIN_THREAD_HIGH_PRIORITY_TASKS = THREAD_MASK_MAIN | PRIORITY_MASK_HIGH,
+    MAIN_THREAD_LOW_PRIORITY_TASKS  = THREAD_MASK_MAIN | PRIORITY_MASK_LOW,
+
+    ALL_TASKS = THREAD_MASK_ALL | PRIORITY_MASK_ALL,
+
+    DEFAULT = ALL_TASKS,
+  };
+
+  /**
+   * @brief Set the async tasks completed callback.
+   * Inputed callback will be emitted after all tasks what user added are completed.
+   *
+   * Usage example:
+   *
+   *   void OnTasksCompleted(TasksCompletedId id);
+   *   auto id0 = AsyncTaskManager::Get().SetCompletedCallback(MakeCallback(OnTasksCompleted), CompletedCallbackTraceMask::MASK_ALL);
+   *   // OnTasksCompleted(id0); called at next Idler.
+   *
+   *   AsyncTaskManager::Get().AddTask(task1);
+   *   auto id1 = AsyncTaskManager::Get().SetCompletedCallback(MakeCallback(OnTasksCompleted), CompletedCallbackTraceMask::MASK_ALL);
+   *   // OnTasksCompleted(id1); called after task1 completed.
+   *
+   *   AsyncTaskManager::Get().AddTask(task2WhichIsLowPriority);
+   *   AsyncTaskManager::Get().AddTask(task3WhichIsWorkerThread);
+   *   AsyncTaskManager::Get().AddTask(task4);
+   *   auto id2 = AsyncTaskManager::Get().SetCompletedCallback(MakeCallback(OnTasksCompleted), static_cast<CompletedCallbackTraceMask>(CompletedCallbackTraceMask::THREAD_MASK_MAIN | CompletedCallbackTraceMask::PRIORITY_MASK_HIGH));
+   *   // OnTasksCompleted(id2); called after task1 and task4 completed.
+   *
+   *   AsyncTaskManager::Get().RemoveCompletedCallback(id1);
+   *   // OnTasksCompleted(id1); will not be called.
+   *
+   * @note The ownership of callback will be hold AsyncTaskManager itself.
+   * @note The callback will be emmited at Process() timming.
+   *
+   * @SINCE_2_2.52
+   * @param[in] callback The callback base when all AsyncTasks completed.
+   *                     This callback will be void return, and single input argument ; TasksCompletedId.
+   * @param[in] mask Mask info s.t. what kind of async task we want to detact.
+   *                 For example, if we set this value as MASK_ALL & ~PRIORITY_MASK_LOW, we will ignore low priority tasks.
+   *                 Default is MASK_ALL.
+   * @return The unique id for callback. It can be used when we want to remove callback.
+   */
+  TasksCompletedId SetCompletedCallback(CallbackBase* callback, CompletedCallbackTraceMask mask = CompletedCallbackTraceMask::DEFAULT);
+
+  /**
+   * @brief Remove the async tasks completed callback.
+   * @note It will not execute setted callback.
+   *
+   * @SINCE_2_2.52
+   * @param[in] tasksCompletedId The id for callback that want to remove.
+   * @return True if we success to removed. False if it already removed, or callback already emitted.
+   */
+  bool RemoveCompletedCallback(TasksCompletedId tasksCompletedId);
+
+public:
   /// @cond internal
   /**
    * @brief Allows the creation of a AsyncTaskManager handle from an internal pointer.