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 #ifndef FOUNDATION_APPEXECFWK_OHOS_TASK_DISPATCHER_CONTEXT_H 16 #define FOUNDATION_APPEXECFWK_OHOS_TASK_DISPATCHER_CONTEXT_H 17 18 #include <string> 19 #include <vector> 20 #include <list> 21 #include <iostream> 22 #include <assert.h> 23 #include <map> 24 #include "default_worker_pool_config.h" 25 #include "global_task_dispatcher.h" 26 #include "parallel_task_dispatcher.h" 27 #include "serial_task_dispatcher.h" 28 #include "task_dispatcher.h" 29 #include "task_executor.h" 30 #include "task_priority.h" 31 #include "worker_pool_config.h" 32 33 namespace OHOS { 34 namespace AppExecFwk { 35 /** 36 * Dispatcher management for all kinds dispatchers and executor. 37 */ 38 class TaskDispatcherContext { 39 public: 40 /** 41 * Constructs the object. 42 * 43 */ 44 TaskDispatcherContext(); 45 46 /** 47 * Constructs the object with parameter executor. 48 * 49 * @param executor the TaskExecutor to set. 50 * 51 */ 52 TaskDispatcherContext(const std::shared_ptr<TaskExecutor> &executor); 53 54 ~TaskDispatcherContext(); 55 56 /** 57 * Gets the worker pool configuration. 58 * 59 * @return The worker pool configuration. 60 * 61 */ 62 std::shared_ptr<WorkerPoolConfig> GetWorkerPoolConfig() const; 63 64 /** 65 * Gets WorkerThread information in WorkerPool. 66 * 67 * @return The WorkerThread information in WorkerPool. 68 * 69 */ 70 std::map<std::string, long> GetWorkerThreadsInfo() const; 71 72 /** 73 * Gets the serialDispatchers. 74 * 75 * @return The serialDispatchers. 76 * 77 */ 78 std::map<std::shared_ptr<SerialTaskDispatcher>, std::string> GetSerialDispatchers() const; 79 80 /** 81 * Gets waiting tasks count of TaskExecutor. 82 * 83 * @return The waiting tasks count of TaskExecutor. 84 * 85 */ 86 int GetWaitingTasksCount() const; 87 88 /** 89 * Gets finished tasks count of TaskExecutor. 90 * 91 * @return The finished tasks count of TaskExecutor. 92 * 93 */ 94 long GetTaskCounter() const; 95 96 /** 97 * Creates a serial dispatcher. 98 * 99 * @param name The dispatcher name 100 * @param priority The priority of tasks 101 * 102 * @return a new object of SerialTaskDispatcher 103 * 104 */ 105 std::shared_ptr<SerialTaskDispatcher> CreateSerialDispatcher(const std::string &name, TaskPriority priority); 106 107 /** 108 * Creates a parallel dispatcher. 109 * 110 * @param name The dispatcher name 111 * @param priority The priority of tasks 112 * 113 * @return a new object of ParallelTaskDispatcher 114 * 115 */ 116 std::shared_ptr<ParallelTaskDispatcher> CreateParallelDispatcher(const std::string &name, TaskPriority priority); 117 118 /** 119 * Gets the global task dispatcher. 120 * 121 * @param priority The priority 122 * 123 * @return A global task dispatcher which cannot be null. 124 * 125 */ 126 std::shared_ptr<TaskDispatcher> GetGlobalTaskDispatcher(TaskPriority priority); 127 128 /** 129 * Shutdown all about task dispatcher. 130 * 131 * @param force Indicate whether force close. 132 * 133 */ 134 ErrCode Shutdown(bool force); 135 136 private: 137 static constexpr int HIGH_PRIORITY_INDEX = 0; 138 139 static constexpr int DEFAULT_PRIORITY_INDEX = 1; 140 141 static constexpr int LOW_PRIORITY_INDEX = 2; 142 143 static constexpr int PRIORITY_COUNT = 3; 144 145 mutable std::mutex mtx_; 146 147 std::map<std::shared_ptr<SerialTaskDispatcher>, std::string> serialDispatchers_; 148 149 std::vector<std::shared_ptr<TaskDispatcher>> globalDispatchers_; 150 151 std::shared_ptr<WorkerPoolConfig> config_ = nullptr; 152 153 std::shared_ptr<TaskExecutor> executor_ = nullptr; 154 155 private: 156 int MapPriorityIndex(TaskPriority priority) const; 157 }; 158 159 } // namespace AppExecFwk 160 } // namespace OHOS 161 #endif