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 OHOS_APPEXECFWK_WORKER_POOL_H 16 #define OHOS_APPEXECFWK_WORKER_POOL_H 17 18 #include <atomic> 19 #include <map> 20 #include <memory> 21 #include <mutex> 22 #include <string> 23 #include <vector> 24 25 #include "app_log_wrapper.h" 26 #include "runnable.h" 27 #include "task.h" 28 #include "thread_factory.h" 29 #include "worker_pool_config.h" 30 #include "work_thread.h" 31 32 namespace OHOS { 33 namespace AppExecFwk { 34 class WorkerPool { 35 public: 36 WorkerPool(const std::shared_ptr<WorkerPoolConfig> &config); 37 virtual ~WorkerPool(); 38 39 long GetKeepAliveTime(void) const; 40 41 int GetCoreThreadCount(void) const; 42 43 int GetMaxThreadCount(void) const; 44 45 int GetWorkCount(void) const; 46 47 std::map<std::string, long> GetWorkerThreadsInfo(void); 48 49 bool AddWorker(const std::shared_ptr<Delegate> &delegate, const std::shared_ptr<Task> &task); 50 51 void CreateGuardThread(); 52 53 protected: 54 void ClosePool(bool interrupt); 55 56 void OnWorkerExit(const std::shared_ptr<WorkerThread> &worker, bool isInterrupted); 57 58 virtual void AfterRun(const std::shared_ptr<Task> &task); 59 60 virtual void BeforeRun(const std::shared_ptr<Task> &task); 61 62 void DecrementThread(void); 63 64 bool CompareAndDecNum(int expectCount); 65 66 private: 67 bool Init(const std::shared_ptr<WorkerPoolConfig> &config); 68 69 bool CheckConfigParams(const std::shared_ptr<WorkerPoolConfig> &config); 70 71 bool CheckThreadCount(int maxThreadCount, int coreThreadCount); 72 73 bool CheckMaxThreadCount(int maxThreadCount); 74 75 bool CheckCoreThreadCount(int coreThreadCount); 76 77 void InterruptWorkers(void); 78 79 static unsigned int GetWorkingThreadNum(unsigned int ctl); 80 81 static bool IsRunning(int ctl); 82 83 static int GetStateFromControl(unsigned int ctl); 84 85 static int CombineToControl(unsigned int state, unsigned int count); 86 87 void AdvanceStateTo(unsigned int target); 88 89 bool CompareAndIncThreadNum(int expect); 90 91 bool CompareAndDecThreadNum(int expect); 92 93 bool CompareAndSet(std::atomic<int> &atomicInt, int left, int right); 94 95 private: 96 static const int THREAD_UPPER_LIMIT; 97 static const int MAX_THREAD_LOWER_LIMIT; 98 static const int CORE_THREAD_LOWER_LIMIT; 99 static const int COUNT_BITS; 100 static const unsigned int CAPACITY; 101 static const int RUNNING; 102 static const int CLOSING; 103 static const int INTERRUPT; 104 static const int CLEANED; 105 static const int CLOSED; 106 107 int thread_limit_ = 0; 108 int core_thread_limit_ = 0; 109 long alive_time_Limit_ = 0; 110 111 std::vector<std::shared_ptr<WorkerThread>> pool_; 112 std::atomic<int> control_; 113 std::mutex poolLock_; 114 std::shared_ptr<ThreadFactory> factory_; 115 std::atomic<bool> initFlag_; 116 std::vector<std::shared_ptr<WorkerThread>> exitPool_; 117 std::atomic<bool> stop_; 118 std::shared_ptr<std::thread> guardThread_; 119 std::mutex exitPoolLock_; 120 std::condition_variable exit_; 121 122 std::mutex exitGuardLock_; 123 std::condition_variable exitGuard_; 124 }; 125 } // namespace AppExecFwk 126 } // namespace OHOS 127 #endif