Make enum for RunningTaskState + Clean code
[platform/core/uifw/dali-adaptor.git] / dali / internal / system / common / async-task-manager-impl.h
index 2007779..8a67d53 100644 (file)
@@ -2,7 +2,7 @@
 #define DALI_INTERNAL_ASYNC_TASK_MANAGER_H
 
 /*
- * Copyright (c) 2022 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2023 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,6 +24,7 @@
 #include <dali/devel-api/threading/thread.h>
 #include <dali/integration-api/adaptor-framework/log-factory-interface.h>
 #include <dali/integration-api/processor-interface.h>
+#include <dali/public-api/common/list-wrapper.h>
 #include <dali/public-api/object/base-object.h>
 #include <memory>
 
@@ -117,13 +118,6 @@ public:
   void RemoveTask(AsyncTaskPtr task);
 
   /**
-   * Pop the next task out from the queue.
-   *
-   * @return The next task to be processed.
-   */
-  AsyncTaskPtr PopNextTaskToProcess();
-
-  /**
    * Pop the next task out from the completed queue, called by main thread.
    *
    * @return The next task in the completed queue.
@@ -131,13 +125,6 @@ public:
   AsyncTaskPtr PopNextCompletedTask();
 
   /**
-   * Pop the next task out from the running queue and add this task to the completed queue.
-   *
-   * @param[in] task The task added to the queue.
-   */
-  void CompleteTask(AsyncTaskPtr task);
-
-  /**
    * @brief Unregister a previously registered processor
    */
   void UnregisterProcessor();
@@ -147,6 +134,22 @@ public:
    */
   void TasksCompleted();
 
+public: // Worker thread called method
+  /**
+   * Pop the next task out from the queue.
+   *
+   * @return The next task to be processed.
+   */
+  AsyncTaskPtr PopNextTaskToProcess();
+
+  /**
+   * Pop the next task out from the running queue and add this task to the completed queue.
+   *
+   * @param[in] task The task added to the queue.
+   */
+  void CompleteTask(AsyncTaskPtr task);
+
+protected: // Implementation of Processor
   /**
    * @copydoc Dali::Integration::Processor::Process()
    */
@@ -190,6 +193,15 @@ private:
     AsyncTaskManager&                mAsyncTaskManager;
   };
 
+  /**
+   * @brief State of running task
+   */
+  enum RunningTaskState
+  {
+    RUNNING  = 0, ///< Running task
+    CANCELED = 1, ///< Canceled by user
+  };
+
 private:
   // Undefined
   AsyncTaskManager(const AsyncTaskManager& manager);
@@ -198,17 +210,35 @@ private:
   AsyncTaskManager& operator=(const AsyncTaskManager& manager);
 
 private:
-  std::vector<AsyncTaskPtr> mWaitingTasks;   //The queue of the tasks waiting to async process
-  std::vector<AsyncTaskPtr> mCompletedTasks; //The queue of the tasks with the async process
+  // Keep Task as list since we take tasks by FIFO as default.
+  using AsyncTaskContainer = std::list<AsyncTaskPtr>;
+
+  using AsyncTaskPair             = std::pair<AsyncTaskPtr, RunningTaskState>;
+  using AsyncRunningTaskContainer = std::list<AsyncTaskPair>;
 
-  using AsyncTaskPair = std::pair<AsyncTaskPtr, bool>;
-  std::vector<AsyncTaskPair> mRunningTasks; ///< The queue of the running tasks
+  AsyncTaskContainer        mWaitingTasks;   ///< The queue of the tasks waiting to async process. Must be locked under mWaitingTasksMutex.
+  AsyncRunningTaskContainer mRunningTasks;   ///< The queue of the running tasks. Must be locked under mRunningTasksMutex.
+  AsyncTaskContainer        mCompletedTasks; ///< The queue of the tasks with the async process. Must be locked under mCompletedTasksMutex.
 
   RoundRobinContainerView<TaskHelper> mTasks;
 
-  Dali::Mutex                          mMutex;
+  uint32_t mAvaliableLowPriorityTaskCounts; ///< The number of tasks that can be processed for priority type LOW.
+                                            ///< Be used to select next wating task determining algorithm.
+                                            ///< Note : For thread safety, Please set/get this value under mRunningTasksMutex scope.
+  uint32_t mWaitingHighProirityTaskCounts;  ///< The number of tasks that waiting now for priority type HIGH.
+                                            ///< Be used to select next wating task determining algorithm.
+                                            ///< Note : For thread safety, Please set/get this value under mWaitingTasksMutex scope.
+
+  Dali::Mutex mWaitingTasksMutex;   ///< Mutex for mWaitingTasks. We can lock mRunningTasksMutex and mCompletedTasksMutex under this scope.
+  Dali::Mutex mRunningTasksMutex;   ///< Mutex for mRunningTasks. We can lock mCompletedTasksMutex under this scope.
+  Dali::Mutex mCompletedTasksMutex; ///< Mutex for mCompletedTasks. We cannot lock any mutex under this scope.
+
+  struct CacheImpl;
+  std::unique_ptr<CacheImpl> mCacheImpl; ///< Cache interface for AsyncTaskManager.
+
   std::unique_ptr<EventThreadCallback> mTrigger;
-  bool                                 mProcessorRegistered;
+
+  bool mProcessorRegistered : 1;
 };
 
 } // namespace Adaptor