1 /*
2 * Copyright (c) 2021 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 #include "task_dispatcher_tools.h"
16
17 namespace OHOS {
18 namespace STtools {
19 using namespace AppExecFwk;
20 using namespace EventFwk;
21
TaskList(TestDispatcher dispatcher,std::shared_ptr<Context> & context,std::string name)22 TaskList::TaskList(TestDispatcher dispatcher, std::shared_ptr<Context> &context, std::string name)
23 : dispatcher(dispatcher), context(context), name(name){};
setDispatcherName(std::string name)24 TaskList &TaskList::setDispatcherName(std::string name)
25 {
26 this->name = name;
27 return *this;
28 }
setDispatcher(TestDispatcher dispatcher)29 TaskList &TaskList::setDispatcher(TestDispatcher dispatcher)
30 {
31 this->dispatcher = dispatcher;
32 return *this;
33 };
setTaskPriority(TaskPriority taskPriority)34 TaskList &TaskList::setTaskPriority(TaskPriority taskPriority)
35 {
36 this->taskPriority = taskPriority;
37 return *this;
38 }
addOperation(TestOperation operation)39 TaskList &TaskList::addOperation(TestOperation operation)
40 {
41 this->dispatchList.push_back(operation);
42 return *this;
43 };
setContext(std::shared_ptr<Context> & context)44 TaskList &TaskList::setContext(std::shared_ptr<Context> &context)
45 {
46 this->context = context;
47 return *this;
48 };
getDispatcher()49 std::shared_ptr<TaskDispatcher> TaskList::getDispatcher()
50 {
51 makeDispatcher();
52 return taskDispatcher;
53 };
addFunc(std::shared_ptr<Runnable> runnable)54 TaskList &TaskList::addFunc(std::shared_ptr<Runnable> runnable)
55 {
56 funcList.push(runnable);
57 return *this;
58 };
addDelay(long delay)59 TaskList &TaskList::addDelay(long delay)
60 {
61 delayTimes.push(delay);
62 return *this;
63 };
addApply(long apply)64 TaskList &TaskList::addApply(long apply)
65 {
66 applyTimes.push(apply);
67 return *this;
68 };
addRevokeTask(unsigned int num)69 TaskList &TaskList::addRevokeTask(unsigned int num)
70 {
71 revokeTaskNumber.push(num);
72 return *this;
73 }
addWaitTime(long time)74 TaskList &TaskList::addWaitTime(long time)
75 {
76 groupWaitTimes.push(time);
77 return *this;
78 }
executedTask()79 bool TaskList::executedTask()
80 {
81 bool result = true;
82 makeDispatcher();
83 if (taskDispatcher == nullptr) {
84 return false;
85 }
86 for (auto operation : dispatchList) {
87 switch (operation) {
88 case TestOperation::SYNC:
89 executeSyncDispatch();
90 break;
91 case TestOperation::ASYNC:
92 executeAsyncDispatch();
93 break;
94 case TestOperation::DELAY:
95 executeDelayDispatch();
96 break;
97 case TestOperation::SYNC_BARRIER:
98 executeSyncDispatchBarrier();
99 break;
100 case TestOperation::ASYNC_BARRIER:
101 executeAsyncDispatchBarrier();
102 break;
103 case TestOperation::CREATE_GROUP:
104 executeCreateDispatchGroup();
105 break;
106 case TestOperation::ASYNC_GROUP:
107 executeAsyncGroupDispatch();
108 break;
109 case TestOperation::GROUP_WAIT:
110 result = executeGroupDispatchWait();
111 break;
112 case TestOperation::GROUP_NOTIFY:
113 executeGroupDispatchNotify();
114 break;
115 case TestOperation::APPLY:
116 executeApplyDispatch();
117 break;
118 case TestOperation::REVOCABLE:
119 result = executeRevokeTask();
120 break;
121 default:
122 result = false;
123 break;
124 }
125 }
126 return result;
127 };
128
makeDispatcher()129 void TaskList::makeDispatcher()
130 {
131 if (context == nullptr) {
132 taskDispatcher = nullptr;
133 return;
134 }
135 if (taskDispatcher != nullptr) {
136 return;
137 }
138 switch (dispatcher) {
139 case TestDispatcher::GLOBAL:
140 taskDispatcher = context->GetGlobalTaskDispatcher(taskPriority);
141 break;
142 case TestDispatcher::PARALLEL:
143 taskDispatcher = context->CreateParallelTaskDispatcher(name, taskPriority);
144 break;
145 case TestDispatcher::SERIAL:
146 taskDispatcher = context->CreateSerialTaskDispatcher(name, taskPriority);
147 break;
148 case TestDispatcher::UI:
149 taskDispatcher = context->GetUITaskDispatcher();
150 break;
151 case TestDispatcher::MAIN:
152 taskDispatcher = context->GetMainTaskDispatcher();
153 break;
154 default:
155 break;
156 }
157 return;
158 };
159
executeSyncDispatch()160 void TaskList::executeSyncDispatch()
161 {
162 HILOG_INFO("TaskList::executeSyncDispatch");
163 if (funcList.size() == 0) {
164 return;
165 }
166 HILOG_INFO("TaskList::executeSyncDispatch execute");
167 auto runnable = funcList.front();
168 funcList.pop();
169 taskDispatcher->SyncDispatch(runnable);
170 return;
171 };
executeAsyncDispatch()172 void TaskList::executeAsyncDispatch()
173 {
174 HILOG_INFO("TaskList::executeAsyncDispatch");
175 if (funcList.size() == 0) {
176 return;
177 }
178 HILOG_INFO("TaskList::executeAsyncDispatch execute");
179 auto runnable = funcList.front();
180 funcList.pop();
181 std::shared_ptr<Revocable> revocable = taskDispatcher->AsyncDispatch(runnable);
182 revocableList.push_back(revocable);
183 return;
184 };
executeDelayDispatch()185 void TaskList::executeDelayDispatch()
186 {
187 HILOG_INFO("TaskList::executeDelayDispatch");
188 if (funcList.size() == 0 || delayTimes.size() == 0) {
189 return;
190 }
191 HILOG_INFO("TaskList::executeDelayDispatch execute");
192 auto runnable = funcList.front();
193 funcList.pop();
194 std::shared_ptr<Revocable> revocable = taskDispatcher->DelayDispatch(runnable, delayTimes.front());
195 delayTimes.pop();
196 revocableList.push_back(revocable);
197 return;
198 };
executeSyncDispatchBarrier()199 void TaskList::executeSyncDispatchBarrier()
200 {
201 HILOG_INFO("TaskList::executeSyncDispatchBarrier");
202 if (funcList.size() == 0) {
203 return;
204 }
205 HILOG_INFO("TaskList::executeSyncDispatchBarrier execute");
206 auto runnable = funcList.front();
207 funcList.pop();
208 taskDispatcher->SyncDispatchBarrier(runnable);
209 return;
210 };
executeAsyncDispatchBarrier()211 void TaskList::executeAsyncDispatchBarrier()
212 {
213 HILOG_INFO("TaskList::executeAsyncDispatchBarrier");
214 if (funcList.size() == 0) {
215 return;
216 }
217 HILOG_INFO("TaskList::executeAsyncDispatchBarrier execute");
218 auto runnable = funcList.front();
219 funcList.pop();
220 taskDispatcher->AsyncDispatchBarrier(runnable);
221 return;
222 };
executeCreateDispatchGroup()223 void TaskList::executeCreateDispatchGroup()
224 {
225 HILOG_INFO("TaskList::executeCreateDispatchGroup execute");
226 group = taskDispatcher->CreateDispatchGroup();
227 return;
228 };
executeAsyncGroupDispatch()229 void TaskList::executeAsyncGroupDispatch()
230 {
231 HILOG_INFO("TaskList::executeAsyncGroupDispatch");
232 if (funcList.size() == 0) {
233 return;
234 }
235 HILOG_INFO("TaskList::executeAsyncGroupDispatch execute");
236 auto runnable = funcList.front();
237 funcList.pop();
238 std::shared_ptr<Revocable> revocable = taskDispatcher->AsyncGroupDispatch(group, runnable);
239 revocableList.push_back(revocable);
240 return;
241 };
executeGroupDispatchWait()242 bool TaskList::executeGroupDispatchWait()
243 {
244 HILOG_INFO("TaskList::executeGroupDispatchWait");
245 if (groupWaitTimes.size() == 0) {
246 return false;
247 }
248 HILOG_INFO("TaskList::executeGroupDispatchWait execute");
249 long waitTimeout = groupWaitTimes.front();
250 groupWaitTimes.pop();
251 return taskDispatcher->GroupDispatchWait(group, waitTimeout);
252 };
executeGroupDispatchNotify()253 void TaskList::executeGroupDispatchNotify()
254 {
255 HILOG_INFO("TaskList::executeGroupDispatchNotify");
256 if (funcList.size() == 0) {
257 return;
258 }
259 HILOG_INFO("TaskList::executeGroupDispatchNotify execute");
260 auto runnable = funcList.front();
261 funcList.pop();
262 taskDispatcher->GroupDispatchNotify(group, runnable);
263 };
executeApplyDispatch()264 void TaskList::executeApplyDispatch()
265 {
266 HILOG_INFO("TaskList::executeApplyDispatch");
267 if (funcList.size() == 0 || applyTimes.size() == 0) {
268 return;
269 }
270 HILOG_INFO("TaskList::executeApplyDispatch execute");
271 auto func = funcList.front();
272 funcList.pop();
273 auto runnable = std::make_shared<IteratableTask<long>>([func](long applyTime) { (*func.get())(); });
274 taskDispatcher->ApplyDispatch(runnable, applyTimes.front());
275 applyTimes.pop();
276 };
executeRevokeTask()277 bool TaskList::executeRevokeTask()
278 {
279 HILOG_INFO("TaskList::executeRevokeTask");
280 if (revokeTaskNumber.size() == 0 || revocableList.size() == 0) {
281 if (revokeTaskNumber.size() > 0) {
282 revokeTaskNumber.pop();
283 }
284 return false;
285 }
286 if (revocableList.size() < revokeTaskNumber.front()) {
287 revokeTaskNumber.pop();
288 return false;
289 }
290 HILOG_INFO("TaskList::executeRevokeTask execute");
291 bool result = revocableList[revokeTaskNumber.front() - 1]->Revoke();
292 revokeTaskNumber.pop();
293 return result;
294 }
295 } // namespace STtools
296 } // namespace OHOS