1 /* 2 * Copyright (c) 2024 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 DISTRIBUTEDDATAMGR_PASTEBOARD_FFRT_UTILS_H 16 #define DISTRIBUTEDDATAMGR_PASTEBOARD_FFRT_UTILS_H 17 18 #include "api/visibility.h" 19 #include "ffrt_inner.h" 20 21 namespace OHOS { 22 namespace MiscServices { 23 /** 24 * Defines the task of the FFRT. 25 */ 26 using FFRTTask = std::function<void()>; 27 28 /** 29 * Defines the task handle of the FFRT. 30 */ 31 using FFRTHandle = ffrt::task_handle; 32 33 /** 34 * Defines the task queue of the FFRT.。 35 */ 36 using FFRTQueue = ffrt::queue; 37 38 /** 39 * The mutex for FFRT tasks. 40 */ 41 using FFRTMutex = ffrt::mutex; 42 43 class API_EXPORT FFRTUtils final { 44 public: 45 /** 46 * Submit an FFRT atomization task without blocking the current thread. 47 * 48 * @param task FFRT task. 49 */ 50 static void SubmitTask(const FFRTTask &task); 51 52 /** 53 * Submit an FFRT serial task without blocking the current thread. 54 * 55 * @param task FFRT task. 56 */ 57 static void SubmitQueueTasks(const std::vector<FFRTTask> &tasks, FFRTQueue &queue); 58 59 /** 60 * Submit the FFRT delayed task without blocking the current thread. 61 * <p> 62 * When the delay time is reached, the task starts to be executed. 63 * 64 * @param task FFRT task. 65 * @param delayMs Delay time, in milliseconds. 66 * @param queue FFRT task execution queue. 67 * 68 * @return FFRT task handle. 69 */ 70 static FFRTHandle SubmitDelayTask(FFRTTask &task, uint32_t delayMs, FFRTQueue &queue); 71 72 /** 73 * Submit the FFRT delayed task without blocking the current thread. 74 * <p> 75 * When the delay time is reached, the task starts to be executed. 76 * 77 * @param task FFRT task. 78 * @param delayMs Delay time, in milliseconds. 79 * @param queue Shared_ptr of FFRT task execution queue. 80 * 81 * @return FFRT task handle. 82 */ 83 static FFRTHandle SubmitDelayTask(FFRTTask &task, uint32_t delayMs, std::shared_ptr<FFRTQueue> queue); 84 85 /** 86 * Submit an FFRT timeout task without blocking the current thread. 87 * <p> 88 * When the timeout period is reached, the task will be canceled. 89 * 90 * @param task FFRT task. 91 * @param timeoutMs Timeout interval, in milliseconds. 92 * 93 * @return true: The task is executed successfully. false: The task execution times out. 94 */ 95 static bool SubmitTimeoutTask(const FFRTTask &task, uint32_t timeoutMs); 96 97 /** 98 * Cancel the FFRT task. 99 * <p> 100 * You cannot cancel a completed task. 101 * 102 * @param handle FFRT task. 103 */ 104 static int CancelTask(FFRTHandle &handle, FFRTQueue &queue); 105 106 /** 107 * Cancel the FFRT task. 108 * <p> 109 * You cannot cancel a completed task. 110 * 111 * @param handle FFRT task. 112 * @param queue Shared_ptr of FFRT task cancel queue. 113 */ 114 static int CancelTask(FFRTHandle &handle, std::shared_ptr<FFRTQueue> &queue); 115 }; 116 117 class API_EXPORT FFRTTimer { 118 public: 119 FFRTTimer(); 120 FFRTTimer(const char *timer_name); 121 ~FFRTTimer(); 122 void Clear(); 123 void CancelAllTimer(); 124 void CancelTimer(const std::string &timerId); 125 void SetTimer(const std::string &timerId, FFRTTask &task, uint32_t delayMs = 0); 126 uint32_t GetTaskId(const std::string &timerId); 127 128 private: 129 /* inner functions must be called when mutex_ is locked */ 130 void CancelAllTimerInner(); 131 void CancelTimerInner(const std::string &timerId); 132 133 FFRTMutex mutex_; 134 FFRTQueue queue_; 135 std::unordered_map<std::string, FFRTHandle> handleMap_; 136 std::unordered_map<std::string, uint32_t> taskId_; 137 }; 138 } // namespace MiscServices 139 } // namespace OHOS 140 141 #endif //DISTRIBUTEDDATAMGR_PASTEBOARD_FFRT_UTILS_H 142