1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef CHRE_PLATFORM_LINUX_TASK_UTIL_TASK_MANAGER_H_ 18 #define CHRE_PLATFORM_LINUX_TASK_UTIL_TASK_MANAGER_H_ 19 20 #include <chrono> 21 #include <condition_variable> 22 #include <mutex> 23 #include <optional> 24 #include <thread> 25 26 #include "chre/platform/linux/task_util/task.h" 27 #include "chre/util/non_copyable.h" 28 #include "chre/util/priority_queue.h" 29 #include "chre/util/singleton.h" 30 31 namespace chre { 32 33 using task_manager_internal::Task; 34 35 /** 36 * A class to manage a thread that executes arbitrary tasks. These tasks can 37 * repeat or be a singular execution. The manager will always execute the next 38 * task in chronological order. 39 */ 40 class TaskManager : public NonCopyable { 41 public: 42 /** 43 * Construct a new Task Manager object. 44 */ 45 TaskManager(); 46 47 /** 48 * Destroy the Task Manager object. 49 */ 50 ~TaskManager(); 51 52 /** 53 * Adds a task to the queue for execution. The manager calls the function func 54 * during execution. If repeatInterval > 0, the task will repeat every 55 * repeatInterval milliseconds. If repeatInterval == 0, the task will be 56 * executed only once. 57 * 58 * @param func the function to call. 59 * @param repeatInterval the interval to repeat. 60 * @return the ID of the Task object or an empty 61 * Optional<> when there is an error. 62 */ 63 std::optional<uint32_t> addTask( 64 const Task::TaskFunction &func, 65 std::chrono::milliseconds repeatInterval = std::chrono::milliseconds(0)); 66 67 /** 68 * Cancels the task with the taskId. 69 * 70 * @param taskId the ID of the task. 71 * @return bool success. 72 */ 73 bool cancelTask(uint32_t taskId); 74 75 /** 76 * Empties the task queue without execution. This call is blocking. 77 */ 78 void flushTasks(); 79 80 private: 81 /** 82 * The run function for the execution thread. 83 */ 84 void run(); 85 86 /** 87 * The queue of tasks. 88 * 89 * We use a chre::PriorityQueue here instead of a std::priority_queue because 90 * the chre::PriorityQueue allows container iterator access and the other does 91 * not. 92 */ 93 PriorityQueue<Task, std::greater<Task>> mQueue; 94 95 /** 96 * The current task being executed. 97 */ 98 Task *mCurrentTask; 99 100 /** 101 * The execution thread. 102 */ 103 std::thread mThread; 104 105 /** 106 * If true, continue executing in the thread. If false, stop executing in the 107 * thread. 108 */ 109 bool mContinueRunningThread; 110 111 /** 112 * The ID counter for Tasks; keeps the Task's ID unique. 113 */ 114 uint32_t mCurrentId; 115 116 /** 117 * The mutex to protect access to the queue. 118 */ 119 std::mutex mMutex; 120 121 /** 122 * The condition variable to signal to the execution thread to process more 123 * tasks (the queue is not empty). 124 */ 125 std::condition_variable mConditionVariable; 126 }; 127 128 // Provide an alias to the TaskManager singleton. 129 typedef Singleton<TaskManager> TaskManagerSingleton; 130 131 } // namespace chre 132 133 #endif // CHRE_PLATFORM_LINUX_TASK_UTIL_TASK_MANAGER_H_ 134