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
16 #include "core/common/task_runner_adapter_impl.h"
17
18 namespace OHOS::Ace {
Initialize(bool useCurrentEventRunner,const std::string & name)19 void TaskRunnerAdapterImpl::Initialize(bool useCurrentEventRunner, const std::string& name)
20 {
21 if (useCurrentEventRunner) {
22 eventRunner_ = OHOS::AppExecFwk::EventRunner::Current();
23 } else if (name == "") {
24 eventRunner_ = OHOS::AppExecFwk::EventRunner::GetMainEventRunner();
25 } else {
26 eventRunner_ = OHOS::AppExecFwk::EventRunner::Create(name);
27 }
28 eventHandler_ = std::make_shared<OHOS::AppExecFwk::EventHandler>(eventRunner_);
29 }
30
PostTask(std::function<void ()> task,const std::string & name,PriorityType priorityType)31 void TaskRunnerAdapterImpl::PostTask(std::function<void()> task, const std::string& name, PriorityType priorityType)
32 {
33 eventHandler_->PostTask(std::move(task), name, 0, ConvertPriority(priorityType));
34 }
35
PostTaskForTime(std::function<void ()> task,uint32_t targetTime,const std::string & caller)36 void TaskRunnerAdapterImpl::PostTaskForTime(std::function<void()> task, uint32_t targetTime, const std::string& caller)
37 {
38 eventHandler_->PostTimingTask(std::move(task), targetTime, "");
39 }
40
PostDelayedTask(std::function<void ()> task,uint32_t delay,const std::string & name,PriorityType priorityType)41 void TaskRunnerAdapterImpl::PostDelayedTask(
42 std::function<void()> task, uint32_t delay, const std::string& name, PriorityType priorityType)
43 {
44 eventHandler_->PostTask(std::move(task), name, delay, ConvertPriority(priorityType));
45 }
46
RunsTasksOnCurrentThread()47 bool TaskRunnerAdapterImpl::RunsTasksOnCurrentThread()
48 {
49 return eventRunner_->IsCurrentRunnerThread();
50 }
51
RemoveTask(const std::string & name)52 void TaskRunnerAdapterImpl::RemoveTask(const std::string &name)
53 {
54 eventHandler_->RemoveTask(name);
55 }
56
ConvertPriority(PriorityType priorityType)57 AppExecFwk::EventQueue::Priority TaskRunnerAdapterImpl::ConvertPriority(PriorityType priorityType)
58 {
59 switch (priorityType) {
60 case PriorityType::VIP:
61 return AppExecFwk::EventQueue::Priority::VIP;
62 case PriorityType::IMMEDIATE:
63 return AppExecFwk::EventQueue::Priority::IMMEDIATE;
64 case PriorityType::HIGH:
65 return AppExecFwk::EventQueue::Priority::HIGH;
66 case PriorityType::LOW:
67 return AppExecFwk::EventQueue::Priority::LOW;
68 case PriorityType::IDLE:
69 return AppExecFwk::EventQueue::Priority::IDLE;
70 default:
71 LOGW("unknown priority type");
72 return AppExecFwk::EventQueue::Priority::LOW;
73 }
74 }
75 } // namespace OHOS::Ace
76