• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_context.h"
16 #include "app_log_wrapper.h"
17 
18 namespace OHOS {
19 namespace AppExecFwk {
20 
TaskDispatcherContext()21 TaskDispatcherContext::TaskDispatcherContext()
22 {
23     globalDispatchers_.resize(PRIORITY_COUNT);
24     config_ = std::make_shared<DefaultWorkerPoolConfig>();
25     if (config_ == nullptr) {
26         APP_LOGE("TaskDispatcherContext::TaskDispatcherContext config is nullptr");
27         executor_ = nullptr;
28     } else {
29         executor_ = std::make_shared<TaskExecutor>(config_);
30         if (executor_ != nullptr) {
31             executor_->CreateGuardThread();
32         }
33     }
34 }
TaskDispatcherContext(const std::shared_ptr<TaskExecutor> & executor)35 TaskDispatcherContext::TaskDispatcherContext(const std::shared_ptr<TaskExecutor> &executor)
36 {
37     globalDispatchers_.resize(PRIORITY_COUNT);
38     config_ = std::make_shared<DefaultWorkerPoolConfig>();
39     executor_ = executor;
40 }
41 
~TaskDispatcherContext()42 TaskDispatcherContext::~TaskDispatcherContext()
43 {
44     if (executor_) {
45         APP_LOGI("TaskDispatcherContext::~TaskDispatcherContext() terminate");
46         executor_->Terminate(false);
47     }
48     APP_LOGI("TaskDispatcherContext::~TaskDispatcherContext end");
49 }
50 
GetWorkerPoolConfig() const51 std::shared_ptr<WorkerPoolConfig> TaskDispatcherContext::GetWorkerPoolConfig() const
52 {
53     return config_;
54 }
55 
GetWorkerThreadsInfo() const56 std::map<std::string, long> TaskDispatcherContext::GetWorkerThreadsInfo() const
57 {
58     APP_LOGI("TaskDispatcherContext::GetWorkerThreadsInfo called");
59     if (executor_ != nullptr) {
60         return executor_->GetWorkerThreadsInfo();
61     }
62     std::map<std::string, long> map;
63     APP_LOGE("TaskDispatcherContext::GetWorkerThreadsInfo executor is nullptr");
64     return map;
65 }
66 
GetSerialDispatchers() const67 std::map<std::shared_ptr<SerialTaskDispatcher>, std::string> TaskDispatcherContext::GetSerialDispatchers() const
68 {
69     APP_LOGI("TaskDispatcherContext::GetSerialDispatchers called");
70     return serialDispatchers_;
71 }
72 
GetWaitingTasksCount() const73 int TaskDispatcherContext::GetWaitingTasksCount() const
74 {
75     APP_LOGI("TaskDispatcherContext::GetWaitingTasksCount called");
76     if (executor_ != nullptr) {
77         return executor_->GetPendingTasksSize();
78     }
79     APP_LOGE("TaskDispatcherContext::GetWaitingTasksCount executor is nullptr");
80     return 0;
81 }
82 
GetTaskCounter() const83 long TaskDispatcherContext::GetTaskCounter() const
84 {
85     APP_LOGI("TaskDispatcherContext::GetTaskCounter called");
86     if (executor_ != nullptr) {
87         return executor_->GetTaskCounter();
88     }
89     APP_LOGE("TaskDispatcherContext::GetTaskCounter executor is nullptr");
90     return 0;
91 }
92 
CreateSerialDispatcher(const std::string & name,TaskPriority priority)93 std::shared_ptr<SerialTaskDispatcher> TaskDispatcherContext::CreateSerialDispatcher(
94     const std::string &name, TaskPriority priority)
95 {
96     APP_LOGI("TaskDispatcherContext::CreateSerialDispatcher start");
97     if (executor_ == nullptr) {
98         APP_LOGE("TaskDispatcherContext::CreateSerialDispatcher executor is nullptr");
99         return nullptr;
100     }
101     std::shared_ptr<SerialTaskDispatcher> serialDispatcher =
102         std::make_shared<SerialTaskDispatcher>(name, priority, executor_);
103     serialDispatchers_.insert(std::pair<std::shared_ptr<SerialTaskDispatcher>, std::string>(serialDispatcher, name));
104     APP_LOGI("TaskDispatcherContext::CreateSerialDispatcher end");
105     return serialDispatcher;
106 }
107 
CreateParallelDispatcher(const std::string & name,TaskPriority priority)108 std::shared_ptr<ParallelTaskDispatcher> TaskDispatcherContext::CreateParallelDispatcher(
109     const std::string &name, TaskPriority priority)
110 {
111     APP_LOGI("TaskDispatcherContext::CreateParallelDispatcher start");
112     if (executor_ == nullptr) {
113         APP_LOGE("TaskDispatcherContext::CreateParallelDispatcher executor is nullptr");
114         return nullptr;
115     }
116     std::shared_ptr<ParallelTaskDispatcher> parallelTaskDispatcher =
117         std::make_shared<ParallelTaskDispatcher>(name, priority, executor_);
118     APP_LOGI("TaskDispatcherContext::CreateParallelDispatcher end");
119     return parallelTaskDispatcher;
120 }
121 
MapPriorityIndex(TaskPriority priority) const122 int TaskDispatcherContext::MapPriorityIndex(TaskPriority priority) const
123 {
124     switch (priority) {
125         case TaskPriority::HIGH:
126             return HIGH_PRIORITY_INDEX;
127         case TaskPriority::DEFAULT:
128             return DEFAULT_PRIORITY_INDEX;
129         case TaskPriority::LOW:
130             return LOW_PRIORITY_INDEX;
131         default:
132             return DEFAULT_PRIORITY_INDEX;
133     }
134     APP_LOGE("TaskDispatcherContext.mapPriorityIndex unhandled priority=%{public}d", priority);
135 
136     return DEFAULT_PRIORITY_INDEX;
137 }
138 
GetGlobalTaskDispatcher(TaskPriority priority)139 std::shared_ptr<TaskDispatcher> TaskDispatcherContext::GetGlobalTaskDispatcher(TaskPriority priority)
140 {
141     APP_LOGI("TaskDispatcherContext::GetGlobalTaskDispatcher start");
142     std::unique_lock<std::mutex> lock(mtx_);
143     int index = MapPriorityIndex(priority);
144     std::shared_ptr<TaskDispatcher> dispatcher = globalDispatchers_[index];
145     if (dispatcher == nullptr) {
146         APP_LOGI("TaskDispatcherContext::GetGlobalTaskDispatcher dispatcher is nullptr ");
147         if (executor_ == nullptr) {
148             APP_LOGE("TaskDispatcherContext::GetGlobalTaskDispatcher executor_ is nullptr ");
149             return nullptr;
150         }
151         dispatcher = std::make_shared<GlobalTaskDispatcher>(priority, executor_);
152         if (globalDispatchers_[index] == nullptr) {
153             APP_LOGI("TaskDispatcherContext::GetGlobalTaskDispatcher dispatcher compareAndSet ");
154             globalDispatchers_.insert((globalDispatchers_.begin() + index), dispatcher);
155         }
156     }
157     APP_LOGI("TaskDispatcherContext::GetGlobalTaskDispatcher end");
158     return dispatcher;
159 }
160 
Shutdown(bool force)161 ErrCode TaskDispatcherContext::Shutdown(bool force)
162 {
163     APP_LOGI("TaskDispatcherContext::Shutdown start");
164     if (executor_ == nullptr) {
165         APP_LOGE("TaskDispatcherContext::Shutdown executor_ is nullptr");
166         return ERR_APPEXECFWK_CHECK_FAILED;
167     }
168     executor_->Terminate(force);
169     APP_LOGI("TaskDispatcherContext::Shutdown end");
170     return ERR_OK;
171 }
172 }  // namespace AppExecFwk
173 }  // namespace OHOS
174