• 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 "c/executor_task.h"
22 #include "ffrt_inner.h"
23 
24 
25 namespace OHOS {
26 namespace PowerMgr {
27 /**
28  * Defines the task of the FFRT.
29  */
30 using FFRTTask = std::function<void()>;
31 
32 /**
33  * Defines the task handle of the FFRT.
34  */
35 using FFRTHandle = ffrt::task_handle;
36 
37 /**
38  * Defines the task queue of the FFRT.。
39  */
40 using FFRTQueue = ffrt::queue;
41 
42 /**
43  * The mutex for FFRT tasks.
44  */
45 using FFRTMutex = ffrt::mutex;
46 
47 class FFRTUtils final {
48 public:
49     /**
50      * Submit an FFRT atomization task without blocking the current thread.
51      *
52      * @param task FFRT task.
53      */
54     static void SubmitTask(const FFRTTask& task);
55 
56     /**
57      * Submit an FFRT task blocks the current thread and waits for the task to complete.
58      *
59      * @param task FFRT task.
60      */
61     static void SubmitTaskSync(const FFRTTask& task);
62 
63     /**
64      * Submit an FFRT serial task without blocking the current thread.
65      *
66      * @param task FFRT task.
67      */
68     static void SubmitQueueTasks(const std::vector<FFRTTask>& tasks, FFRTQueue& queue);
69 
70     /**
71      * Submit the FFRT delayed task without blocking the current thread.
72      * <p>
73      * When the delay time is reached, the task starts to be executed.
74      *
75      * @param task FFRT task.
76      * @param delayMs Delay time, in milliseconds.
77      * @param queue FFRT task execution queue.
78      *
79      * @return FFRT task handle.
80      */
81     static FFRTHandle SubmitDelayTask(FFRTTask& task, uint32_t delayMs, FFRTQueue& queue);
82 
83     /**
84      * Submit the FFRT delayed task without blocking the current thread.
85      * <p>
86      * When the delay time is reached, the task starts to be executed.
87      *
88      * @param task FFRT task.
89      * @param delayMs Delay time, in milliseconds.
90      * @param queue Shared_ptr of FFRT task execution queue.
91      *
92      * @return FFRT task handle.
93      */
94     static FFRTHandle SubmitDelayTask(FFRTTask& task, uint32_t delayMs, std::shared_ptr<FFRTQueue> queue);
95 
96     /**
97      * Submit an FFRT timeout task without blocking the current thread.
98      * <p>
99      * When the timeout period is reached, the task will be canceled.
100      *
101      * @param task FFRT task.
102      * @param timeoutMs Timeout interval, in milliseconds.
103      *
104      * @return true: The task is executed successfully. false: The task execution times out.
105      */
106     static bool SubmitTimeoutTask(const FFRTTask& task, uint32_t timeoutMs);
107 
108     /**
109      * Cancel the FFRT task.
110      * <p>
111      * You cannot cancel a completed task.
112      *
113      * @param handle FFRT task.
114      */
115     static int CancelTask(FFRTHandle& handle, FFRTQueue& queue);
116 
117     /**
118      * Cancel the FFRT task.
119      * <p>
120      * You cannot cancel a completed task.
121      *
122      * @param handle FFRT task.
123      * @param queue Shared_ptr of FFRT task cancel queue.
124      */
125     static int CancelTask(FFRTHandle& handle, std::shared_ptr<FFRTQueue> queue);
126 };
127 
128 enum FFRTTimerId {
129     TIMER_ID_SLEEP,
130     TIMER_ID_USER_ACTIVITY_OFF,
131     TIMER_ID_USER_ACTIVITY_TIMEOUT,
132     TIMER_ID_SCREEN_TIMEOUT_CHECK,
133     TIMER_ID_PRE_BRIGHT_AUTH,
134     TIMER_ID_PROXIMITY_SCREEN_OFF,
135     TIMER_ID_PROXIMITY_SCREEN_SWITCH_TO_SUB,
136 };
137 
138 class FFRTMutexMap {
139 public:
140     FFRTMutexMap() = default;
141     ~FFRTMutexMap() = default;
142     void Lock(uint32_t mutexId);
143     void Unlock(uint32_t mutexId);
144 private:
145     std::unordered_map<uint32_t, FFRTMutex> mutexMap_;
146 };
147 
148 class FFRTTimer {
149 public:
150     FFRTTimer();
151     FFRTTimer(const char *timer_name);
152     ~FFRTTimer();
153     void Clear();
154     void CancelAllTimer();
155     void CancelTimer(uint32_t timerId);
156     void SetTimer(uint32_t timerId, FFRTTask& task, uint32_t delayMs = 0);
157     uint32_t GetTaskId(uint32_t timerId);
158     const void* GetTaskHandlePtr(uint32_t timerId);
159 
160 private:
161     /* inner functions must be called when mutex_ is locked */
162     void CancelAllTimerInner();
163     void CancelTimerInner(uint32_t timerId);
164 
165     FFRTMutex mutex_;
166     FFRTQueue queue_;
167     std::unordered_map<uint32_t, FFRTHandle> handleMap_;
168     std::unordered_map<uint32_t, uint32_t> taskId_;
169 };
170 } // namespace PowerMgr
171 } // namespace OHOS
172 
173 #endif // POWERMGR_FFRT_UTILS_H
174