• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <unordered_map>
19 
20 #include "api/visibility.h"
21 #include "ffrt_inner.h"
22 
23 namespace OHOS {
24 namespace MiscServices {
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 API_EXPORT 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 serial task without blocking the current thread.
56  *
57  * @param task FFRT task.
58  */
59     static void SubmitQueueTasks(const std::vector<FFRTTask> &tasks, FFRTQueue &queue);
60 
61     /**
62  * Submit the FFRT delayed task without blocking the current thread.
63  * <p>
64  * When the delay time is reached, the task starts to be executed.
65  *
66  * @param task FFRT task.
67  * @param delayMs Delay time, in milliseconds.
68  * @param queue FFRT task execution queue.
69  *
70  * @return FFRT task handle.
71  */
72     static FFRTHandle SubmitDelayTask(FFRTTask &task, uint32_t delayMs, FFRTQueue &queue);
73 
74     /**
75  * Submit the FFRT delayed task without blocking the current thread.
76  * <p>
77  * When the delay time is reached, the task starts to be executed.
78  *
79  * @param task FFRT task.
80  * @param delayMs Delay time, in milliseconds.
81  * @param queue Shared_ptr of FFRT task execution queue.
82  *
83  * @return FFRT task handle.
84  */
85     static FFRTHandle SubmitDelayTask(FFRTTask &task, uint32_t delayMs, std::shared_ptr<FFRTQueue> queue);
86 
87     /**
88  * Submit an FFRT timeout task without blocking the current thread.
89  * <p>
90  * When the timeout period is reached, the task will be canceled.
91  *
92  * @param task FFRT task.
93  * @param timeoutMs Timeout interval, in milliseconds.
94  *
95  * @return true: The task is executed successfully. false: The task execution times out.
96  */
97     static bool SubmitTimeoutTask(const FFRTTask &task, uint32_t timeoutMs);
98 
99     /**
100  * Cancel the FFRT task.
101  * <p>
102  * You cannot cancel a completed task.
103  *
104  * @param handle FFRT task.
105  */
106     static int CancelTask(FFRTHandle &handle, FFRTQueue &queue);
107 
108     /**
109  * Cancel the FFRT task.
110  * <p>
111  * You cannot cancel a completed task.
112  *
113  * @param handle FFRT task.
114  * @param queue Shared_ptr of FFRT task cancel queue.
115  */
116     static int CancelTask(FFRTHandle &handle, std::shared_ptr<FFRTQueue> &queue);
117 };
118 
119 class API_EXPORT FFRTTimer {
120 public:
121     FFRTTimer();
122     FFRTTimer(const std::string &timerName);
123     ~FFRTTimer();
124     void Clear();
125     void CancelAllTimer();
126     void CancelTimer(const std::string &timerId);
127     void SetTimer(const std::string &timerId, FFRTTask &task, uint32_t delayMs = 0);
128     uint32_t GetTaskId(const std::string &timerId);
129 
130 private:
131     /* inner functions must be called when mutex_ is locked */
132     void CancelAllTimerInner();
133     void CancelTimerInner(const std::string &timerId);
134 
135     FFRTMutex mutex_;
136     FFRTQueue queue_;
137     std::unordered_map<std::string, FFRTHandle> handleMap_;
138     std::unordered_map<std::string, uint32_t> taskId_;
139 };
140 
141 class API_EXPORT FFRTPool {
142 public:
143    static std::shared_ptr<FFRTTimer> GetTimer(const std::string &name);
144    static void Clear();
145 
146 private:
147    static std::unordered_map<std::string, std::shared_ptr<FFRTTimer>> ffrtPool_;
148    static std::mutex mutex_;
149 };
150 } // namespace MiscServices
151 } // namespace OHOS
152 
153 #endif //DISTRIBUTEDDATAMGR_PASTEBOARD_FFRT_UTILS_H
154