1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef POWERMGR_FFRT_UTILS_H 16 #define POWERMGR_FFRT_UTILS_H 17 18 #include <functional> 19 #include <vector> 20 21 #include "ffrt_inner.h" 22 23 namespace OHOS { 24 namespace PowerMgr { 25 /** 26 * Defines the task of the FFRT. 27 */ 28 using FFRTTask = std::function<void()>; 29 30 /** 31 * Defines the task handle of the FFRT. 32 */ 33 using FFRTHandle = ffrt::task_handle; 34 35 /** 36 * Defines the task queue of the FFRT.。 37 */ 38 using FFRTQueue = ffrt::queue; 39 40 /** 41 * The mutex for FFRT tasks. 42 */ 43 using FFRTMutex = ffrt::mutex; 44 45 class FFRTUtils final { 46 public: 47 /** 48 * Submit an FFRT atomization task without blocking the current thread. 49 * 50 * @param task FFRT task. 51 */ 52 static void SubmitTask(const FFRTTask& task); 53 54 /** 55 * Submit an FFRT task blocks the current thread and waits for the task to complete. 56 * 57 * @param task FFRT task. 58 */ 59 static void SubmitTaskSync(const FFRTTask& task); 60 61 /** 62 * Submit an FFRT serial task without blocking the current thread. 63 * 64 * @param task FFRT task. 65 */ 66 static void SubmitQueueTasks(const std::vector<FFRTTask>& tasks, FFRTQueue& queue); 67 68 /** 69 * Submit the FFRT delayed task without blocking the current thread. 70 * <p> 71 * When the delay time is reached, the task starts to be executed. 72 * 73 * @param task FFRT task. 74 * @param delayMs Delay time, in milliseconds. 75 * @param queue FFRT task execution queue. 76 * 77 * @return FFRT task handle. 78 */ 79 static FFRTHandle SubmitDelayTask(FFRTTask& task, uint32_t delayMs, FFRTQueue& queue); 80 81 /** 82 * Submit the FFRT delayed task without blocking the current thread. 83 * <p> 84 * When the delay time is reached, the task starts to be executed. 85 * 86 * @param task FFRT task. 87 * @param delayMs Delay time, in milliseconds. 88 * @param queue Shared_ptr of FFRT task execution queue. 89 * 90 * @return FFRT task handle. 91 */ 92 static FFRTHandle SubmitDelayTask(FFRTTask& task, uint32_t delayMs, std::shared_ptr<FFRTQueue> queue); 93 94 /** 95 * Submit an FFRT timeout task without blocking the current thread. 96 * <p> 97 * When the timeout period is reached, the task will be canceled. 98 * 99 * @param task FFRT task. 100 * @param timeoutMs Timeout interval, in milliseconds. 101 * 102 * @return true: The task is executed successfully. false: The task execution times out. 103 */ 104 static bool SubmitTimeoutTask(const FFRTTask& task, uint32_t timeoutMs); 105 106 /** 107 * Cancel the FFRT task. 108 * <p> 109 * You cannot cancel a completed task. 110 * 111 * @param handle FFRT task. 112 */ 113 static int CancelTask(FFRTHandle& handle, FFRTQueue& queue); 114 115 /** 116 * Cancel the FFRT task. 117 * <p> 118 * You cannot cancel a completed task. 119 * 120 * @param handle FFRT task. 121 * @param queue Shared_ptr of FFRT task cancel queue. 122 */ 123 static int CancelTask(FFRTHandle& handle, std::shared_ptr<FFRTQueue> queue); 124 }; 125 126 enum FFRTTimerId { 127 TIMER_ID_SLEEP, 128 TIMER_ID_HIBERNATE, 129 TIMER_ID_USER_ACTIVITY_OFF, 130 TIMER_ID_USER_ACTIVITY_TIMEOUT, 131 TIMER_ID_SCREEN_TIMEOUT_CHECK, 132 TIMER_ID_PRE_BRIGHT_AUTH, 133 TIMER_ID_PROXIMITY_SCREEN_OFF, 134 }; 135 136 class FFRTMutexMap { 137 public: 138 FFRTMutexMap() = default; 139 ~FFRTMutexMap() = default; 140 void Lock(uint32_t mutexId); 141 void Unlock(uint32_t mutexId); 142 private: 143 std::unordered_map<uint32_t, FFRTMutex> mutexMap_; 144 }; 145 146 class FFRTTimer { 147 public: 148 FFRTTimer(); 149 FFRTTimer(const char *timer_name); 150 ~FFRTTimer(); 151 void Clear(); 152 void CancelAllTimer(); 153 void CancelTimer(uint32_t timerId); 154 void SetTimer(uint32_t timerId, FFRTTask& task); 155 void SetTimer(uint32_t timerId, FFRTTask& task, uint32_t delayMs); 156 uint32_t GetTaskId(uint32_t timerId); 157 private: 158 /* inner functions must be called when mutex_ is locked */ 159 void CancelAllTimerInner(); 160 void CancelTimerInner(uint32_t timerId); 161 162 FFRTMutex mutex_; 163 FFRTQueue queue_; 164 std::unordered_map<uint32_t, FFRTHandle> handleMap_; 165 std::unordered_map<uint32_t, uint32_t> taskId_; 166 }; 167 } // namespace PowerMgr 168 } // namespace OHOS 169 170 #endif // POWERMGR_FFRT_UTILS_H 171