1 /*
2 * Copyright (c) 2021-2022 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 "parallel_task_dispatcher_base.h"
16
17 #include "hilog_wrapper.h"
18 #include "appexecfwk_errors.h"
19
20 namespace OHOS {
21 namespace AppExecFwk {
22 std::string ParallelTaskDispatcherBase::DISPATCHER_TAG = "ParallelTaskDispatcherBase";
23 std::string ParallelTaskDispatcherBase::ASYNC_DISPATCHER_TAG = DISPATCHER_TAG + "::asyncDispatch";
24 std::string ParallelTaskDispatcherBase::SYNC_DISPATCHER_TAG = DISPATCHER_TAG + "::syncDispatch";
25 std::string ParallelTaskDispatcherBase::DELAY_DISPATCHER_TAG = DISPATCHER_TAG + "::delayDispatch";
26 std::string ParallelTaskDispatcherBase::ASYNC_GROUP_DISPATCHER_TAG = DISPATCHER_TAG + "::asyncGroupDispatch";
ParallelTaskDispatcherBase(TaskPriority taskPriority,std::shared_ptr<TaskExecutor> & executor,const std::string & dispatcherName)27 ParallelTaskDispatcherBase::ParallelTaskDispatcherBase(
28 TaskPriority taskPriority, std::shared_ptr<TaskExecutor> &executor, const std::string &dispatcherName)
29 : BaseTaskDispatcher(dispatcherName, taskPriority)
30 {
31 executor_ = executor;
32 }
33
InterceptedExecute(std::shared_ptr<Task> & task)34 ErrCode ParallelTaskDispatcherBase::InterceptedExecute(std::shared_ptr<Task> &task)
35 {
36 HILOG_DEBUG("ParallelTaskDispatcherBase::InterceptedExecute start");
37 if (executor_ == nullptr) {
38 HILOG_ERROR("ParallelTaskDispatcherBase::InterceptedExecute executor_ is nullptr");
39 return ERR_APPEXECFWK_CHECK_FAILED;
40 }
41
42 if ((GetInterceptor() != nullptr) &&
43 GetInterceptor()->Intercept(task) == ERR_APPEXECFWK_INTERCEPT_TASK_EXECUTE_SUCCESS) {
44 HILOG_ERROR("ParallelTaskDispatcherBase::InterceptedExecute intercept task execute success");
45 return ERR_APPEXECFWK_INTERCEPT_TASK_EXECUTE_SUCCESS;
46 }
47
48 executor_->Execute(task);
49 HILOG_INFO("ParallelTaskDispatcherBase::InterceptedExecute end");
50 return ERR_OK;
51 }
52
SyncDispatch(const std::shared_ptr<Runnable> & runnable)53 ErrCode ParallelTaskDispatcherBase::SyncDispatch(const std::shared_ptr<Runnable> &runnable)
54 {
55 HILOG_INFO("ParallelTaskDispatcherBase::SyncDispatch start");
56 if (Check(runnable) != ERR_OK) {
57 HILOG_ERROR("ParallelTaskDispatcherBase::SyncDispatch check failed");
58 return ERR_APPEXECFWK_CHECK_FAILED;
59 }
60
61 std::shared_ptr<SyncTask> innerSyncTask = std::make_shared<SyncTask>(runnable, GetPriority(), shared_from_this());
62 if (innerSyncTask == nullptr) {
63 HILOG_ERROR("ParallelTaskDispatcherBase::SyncDispatch innerSyncTask is nullptr");
64 return ERR_APPEXECFWK_CHECK_FAILED;
65 }
66 std::shared_ptr<Task> innerTask = std::static_pointer_cast<Task>(innerSyncTask);
67 if (innerTask == nullptr) {
68 HILOG_ERROR("ParallelTaskDispatcherBase::SyncDispatch innerTask is nullptr");
69 return ERR_APPEXECFWK_CHECK_FAILED;
70 }
71 HILOG_INFO("ParallelTaskDispatcherBase::SyncDispatch into new sync task");
72 ErrCode execute = InterceptedExecute(innerTask);
73 if (execute != ERR_OK) {
74 HILOG_ERROR("ParallelTaskDispatcherBase::SyncDispatch execute failed");
75 return execute;
76 }
77 innerSyncTask->WaitTask();
78 HILOG_INFO("ParallelTaskDispatcherBase::SyncDispatch end");
79 return ERR_OK;
80 }
81
AsyncDispatch(const std::shared_ptr<Runnable> & runnable)82 std::shared_ptr<Revocable> ParallelTaskDispatcherBase::AsyncDispatch(const std::shared_ptr<Runnable> &runnable)
83 {
84 HILOG_INFO("ParallelTaskDispatcherBase::AsyncDispatch start");
85 if (Check(runnable) != ERR_OK) {
86 HILOG_ERROR("ParallelTaskDispatcherBase::AsyncDispatch check failed.");
87 return nullptr;
88 }
89
90 std::shared_ptr<Task> innerTask = std::make_shared<Task>(runnable, GetPriority(), shared_from_this());
91 if (innerTask == nullptr) {
92 HILOG_ERROR("ParallelTaskDispatcherBase::AsyncDispatch innerTask is nullptr.");
93 return nullptr;
94 }
95 TracePointBeforePost(innerTask, true, ASYNC_DISPATCHER_TAG);
96 HILOG_INFO("ParallelTaskDispatcherBase::AsyncDispatch into new async task");
97 ErrCode execute = InterceptedExecute(innerTask);
98 if (execute != ERR_OK) {
99 HILOG_ERROR("ParallelTaskDispatcherBase::AsyncDispatch execute failed");
100 return nullptr;
101 }
102 HILOG_INFO("ParallelTaskDispatcherBase::AsyncDispatch end");
103 return innerTask;
104 }
105
DelayDispatch(const std::shared_ptr<Runnable> & runnable,long delayMs)106 std::shared_ptr<Revocable> ParallelTaskDispatcherBase::DelayDispatch(
107 const std::shared_ptr<Runnable> &runnable, long delayMs)
108 {
109 HILOG_INFO("ParallelTaskDispatcherBase::DelayDispatch start");
110 if (Check(runnable) != ERR_OK) {
111 HILOG_ERROR("ParallelTaskDispatcherBase::DelayDispatch Check failed");
112 return nullptr;
113 }
114
115 if (executor_ == nullptr) {
116 HILOG_ERROR("ParallelTaskDispatcherBase::DelayDispatch executor_ is nullptr");
117 return nullptr;
118 }
119
120 std::shared_ptr<Task> innerTask = std::make_shared<Task>(runnable, GetPriority(), shared_from_this());
121 if (innerTask == nullptr) {
122 HILOG_ERROR("ParallelTaskDispatcherBase::DelayDispatch innerTask is nullptr");
123 return nullptr;
124 }
125 TracePointBeforePost(innerTask, true, DELAY_DISPATCHER_TAG);
126 std::function<void()> callback = std::bind(&ParallelTaskDispatcherBase::InterceptedExecute, shared_from_this(),
127 innerTask);
128 bool executeFlag = executor_->DelayExecute(callback, delayMs);
129 if (!executeFlag) {
130 HILOG_ERROR("ParallelTaskDispatcherBase::DelayDispatch execute failed");
131 return nullptr;
132 }
133 HILOG_INFO("ParallelTaskDispatcherBase::DelayDispatch end");
134 return innerTask;
135 }
136
AsyncGroupDispatch(const std::shared_ptr<Group> & group,const std::shared_ptr<Runnable> & runnable)137 std::shared_ptr<Revocable> ParallelTaskDispatcherBase::AsyncGroupDispatch(
138 const std::shared_ptr<Group> &group, const std::shared_ptr<Runnable> &runnable)
139 {
140 HILOG_INFO("ParallelTaskDispatcherBase::AsyncGroupDispatch start");
141 if (group == nullptr) {
142 HILOG_ERROR("ParallelTaskDispatcherBase::AsyncGroupDispatch group is nullptr");
143 return nullptr;
144 }
145 if (Check(runnable) != ERR_OK) {
146 HILOG_ERROR("ParallelTaskDispatcherBase::AsyncGroupDispatch Check failed");
147 return nullptr;
148 }
149
150 std::shared_ptr<GroupImpl> groupImpl = CastToGroupImpl(group);
151 if (groupImpl == nullptr) {
152 HILOG_ERROR("ParallelTaskDispatcherBase::AsyncGroupDispatch groupImpl is nullptr");
153 return nullptr;
154 }
155 groupImpl->Associate();
156
157 std::shared_ptr<Task> innerTask = std::make_shared<Task>(runnable, GetPriority(), shared_from_this());
158 if (innerTask == nullptr) {
159 HILOG_ERROR("ParallelTaskDispatcherBase::AsyncGroupDispatch innerTask is nullptr");
160 return nullptr;
161 }
162 TracePointBeforePost(innerTask, true, ASYNC_GROUP_DISPATCHER_TAG);
163 std::shared_ptr<MyTaskListener> ptrlistener = std::make_shared<MyTaskListener>();
164 if (ptrlistener == nullptr) {
165 HILOG_ERROR("ParallelTaskDispatcherBase::AsyncGroupDispatch ptrlistener is nullptr");
166 return nullptr;
167 }
168 const std::function<void()> onTaskDone = std::bind(&GroupImpl::NotifyTaskDone, groupImpl);
169 ptrlistener->Callback(onTaskDone);
170 innerTask->AddTaskListener(ptrlistener);
171 ErrCode execute = InterceptedExecute(innerTask);
172 if (execute != ERR_OK) {
173 HILOG_ERROR("ParallelTaskDispatcherBase::AsyncGroupDispatch execute failed");
174 return nullptr;
175 }
176 HILOG_INFO("ParallelTaskDispatcherBase::AsyncGroupDispatch end");
177 return innerTask;
178 }
179
GetInterceptor()180 std::shared_ptr<TaskExecuteInterceptor> ParallelTaskDispatcherBase::GetInterceptor()
181 {
182 return nullptr;
183 }
184 } // namespace AppExecFwk
185 } // namespace OHOS
186