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