• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 the FFRT delayed task without blocking the current thread.
77      * <p>
78      * When the delay time is reached, the task starts to be executed.
79      *
80      * @param task FFRT task.
81      * @param delayMs Delay time, in milliseconds.
82      * @param queue Shared_ptr of FFRT task execution queue.
83      *
84      * @return FFRT task handle.
85      */
86     static FFRTHandle SubmitDelayTask(FFRTTask& task, uint32_t delayMs, std::shared_ptr<FFRTQueue> queue);
87 
88     /**
89      * Submit an FFRT timeout task without blocking the current thread.
90      * <p>
91      * When the timeout period is reached, the task will be canceled.
92      *
93      * @param task FFRT task.
94      * @param timeoutMs Timeout interval, in milliseconds.
95      *
96      * @return true: The task is executed successfully. false: The task execution times out.
97      */
98     static bool SubmitTimeoutTask(const FFRTTask& task, uint32_t timeoutMs);
99 
100     /**
101      * Cancel the FFRT task.
102      * <p>
103      * You cannot cancel a completed task.
104      *
105      * @param handle FFRT task.
106      */
107     static void CancelTask(FFRTHandle& handle, FFRTQueue& queue);
108 
109     /**
110      * Cancel the FFRT task.
111      * <p>
112      * You cannot cancel a completed task.
113      *
114      * @param handle FFRT task.
115      * @param queue Shared_ptr of FFRT task cancel queue.
116      */
117     static void CancelTask(FFRTHandle& handle, std::shared_ptr<FFRTQueue> queue);
118 
119     /**
120      * The mutex for FFRT tasks.
121      */
122     class Mutex {
123     public:
124         Mutex();
125         ~Mutex();
126         void Lock();
127         bool TryLock();
128         void Unlock();
129 
130     private:
131         ffrt::mutex* mutex_;
132     };
133 };
134 } // namespace PowerMgr
135 } // namespace OHOS
136 
137 #endif // POWERMGR_FFRT_UTILS_H
138