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 class FFRTUtils final { 40 public: 41 /** 42 * Submit an FFRT atomization task without blocking the current thread. 43 * 44 * @param task FFRT task. 45 */ 46 static void SubmitTask(const FFRTTask& task); 47 48 /** 49 * Submit an FFRT task blocks the current thread and waits for the task to complete. 50 * 51 * @param task FFRT task. 52 */ 53 static void SubmitTaskSync(const FFRTTask& task); 54 55 /** 56 * Submit an FFRT serial task without blocking the current thread. 57 * 58 * @param task FFRT task. 59 */ 60 static void SubmitQueueTasks(const std::vector<FFRTTask>& tasks, FFRTQueue& queue); 61 62 /** 63 * Submit the FFRT delayed task without blocking the current thread. 64 * <p> 65 * When the delay time is reached, the task starts to be executed. 66 * 67 * @param task FFRT task. 68 * @param delayMs Delay time, in milliseconds. 69 * @param queue FFRT task execution queue. 70 * 71 * @return FFRT task handle. 72 */ 73 static FFRTHandle SubmitDelayTask(FFRTTask& task, uint32_t delayMs, FFRTQueue& queue); 74 75 /** 76 * Submit an FFRT timeout task without blocking the current thread. 77 * <p> 78 * When the timeout period is reached, the task will be canceled. 79 * 80 * @param task FFRT task. 81 * @param timeoutMs Timeout interval, in milliseconds. 82 * 83 * @return true: The task is executed successfully. false: The task execution times out. 84 */ 85 static bool SubmitTimeoutTask(const FFRTTask& task, uint32_t timeoutMs); 86 87 /** 88 * Cancel the FFRT task. 89 * <p> 90 * You cannot cancel a completed task. 91 * 92 * @param handle FFRT task. 93 */ 94 static void CancelTask(FFRTHandle& handle, FFRTQueue& queue); 95 96 /** 97 * The mutex for FFRT tasks. 98 */ 99 class Mutex { 100 public: 101 Mutex(); 102 ~Mutex(); 103 void Lock(); 104 bool TryLock(); 105 void Unlock(); 106 107 private: 108 ffrt::mutex* mutex_; 109 }; 110 }; 111 } // namespace PowerMgr 112 } // namespace OHOS 113 114 #endif // POWERMGR_FFRT_UTILS_H 115