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
16 #include "spec_task_dispatcher.h"
17 #include "task_handler_libevent_adapter.h"
18
19 namespace OHOS {
20 namespace AppExecFwk {
21 std::string SpecTaskDispatcher::DISPATCHER_TAG = "SpecTaskDispatcher";
22 std::string SpecTaskDispatcher::ASYNC_DISPATCHER_TAG = DISPATCHER_TAG + "::asyncDispatch";
23 std::string SpecTaskDispatcher::SYNC_DISPATCHER_TAG = DISPATCHER_TAG + "::syncDispatch";
24 std::string SpecTaskDispatcher::DELAY_DISPATCHER_TAG = DISPATCHER_TAG + "::delayDispatch";
SpecTaskDispatcher(std::shared_ptr<SpecDispatcherConfig> config,std::shared_ptr<EventRunner> runner)25 SpecTaskDispatcher::SpecTaskDispatcher(
26 std::shared_ptr<SpecDispatcherConfig> config, std::shared_ptr<EventRunner> runner)
27 : BaseTaskDispatcher(config->GetName(), config->GetPriority())
28 {
29 handler_ = std::make_shared<TaskHandlerLibeventAdapter>(runner);
30 }
31
SyncDispatch(const std::shared_ptr<Runnable> & runnable)32 ErrCode SpecTaskDispatcher::SyncDispatch(const std::shared_ptr<Runnable> &runnable)
33 {
34 HILOG_INFO("SpecTaskDispatcher::SyncDispatch start");
35 if (handler_ == nullptr) {
36 HILOG_ERROR("SpecTaskDispatcher::SyncDispatch handler is nullptr");
37 return ERR_APPEXECFWK_CHECK_FAILED;
38 }
39 if (Check(runnable) != ERR_OK) {
40 HILOG_ERROR("SpecTaskDispatcher::SyncDispatch check failed");
41 return ERR_APPEXECFWK_CHECK_FAILED;
42 }
43
44 std::shared_ptr<Task> innerTask = std::make_shared<Task>(runnable, GetPriority(), shared_from_this());
45 TracePointBeforePost(innerTask, false, SYNC_DISPATCHER_TAG);
46 HILOG_INFO("SpecTaskDispatcher::SyncDispatch into new sync task");
47 handler_->DispatchSync(runnable);
48 TracePointAfterPost(innerTask, false, DISPATCHER_TAG);
49
50 HILOG_INFO("SpecTaskDispatcher::SyncDispatch end");
51 return ERR_OK;
52 }
53
AsyncDispatch(const std::shared_ptr<Runnable> & runnable)54 std::shared_ptr<Revocable> SpecTaskDispatcher::AsyncDispatch(const std::shared_ptr<Runnable> &runnable)
55 {
56 HILOG_INFO("SpecTaskDispatcher::AsyncDispatch start");
57 if (handler_ == nullptr) {
58 HILOG_ERROR("SpecTaskDispatcher::AsyncDispatch handler is nullptr");
59 return nullptr;
60 }
61 if (Check(runnable) != ERR_OK) {
62 HILOG_ERROR("SpecTaskDispatcher::AsyncDispatch check failed");
63 return nullptr;
64 }
65
66 std::shared_ptr<Task> innerTask = std::make_shared<Task>(runnable, GetPriority(), shared_from_this());
67 TracePointBeforePost(innerTask, true, ASYNC_DISPATCHER_TAG);
68 HILOG_INFO("SpecTaskDispatcher::AsyncDispatch into new async task");
69 handler_->Dispatch(runnable);
70 HILOG_INFO("SpecTaskDispatcher::AsyncDispatch end");
71 return innerTask;
72 }
73
DelayDispatch(const std::shared_ptr<Runnable> & runnable,long delayMs)74 std::shared_ptr<Revocable> SpecTaskDispatcher::DelayDispatch(const std::shared_ptr<Runnable> &runnable, long delayMs)
75 {
76 HILOG_INFO("SpecTaskDispatcher::DelayDispatch start");
77 if (handler_ == nullptr) {
78 HILOG_ERROR("SpecTaskDispatcher::DelayDispatch handler is nullptr");
79 return nullptr;
80 }
81 if (Check(runnable) != ERR_OK) {
82 HILOG_ERROR("SpecTaskDispatcher::DelayDispatch check failed");
83 return nullptr;
84 }
85
86 std::shared_ptr<Task> innerTask = std::make_shared<Task>(runnable, GetPriority(), shared_from_this());
87 TracePointBeforePost(innerTask, true, DELAY_DISPATCHER_TAG);
88 handler_->Dispatch(runnable, delayMs);
89 HILOG_INFO("SpecTaskDispatcher::DelayDispatch end");
90 return innerTask;
91 }
92 } // namespace AppExecFwk
93 } // namespace OHOS
94