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 #ifndef FFRT_WORKER_MANAGER_HPP 17 #define FFRT_WORKER_MANAGER_HPP 18 19 #include <thread> 20 #include <list> 21 #include <mutex> 22 #include <shared_mutex> 23 #include <condition_variable> 24 #include <unordered_map> 25 26 #include "eu/worker_thread.h" 27 #include "eu/thread_group.h" 28 #include "eu/cpu_monitor.h" 29 #include "sync/sync.h" 30 #include "dfx/log/ffrt_log_api.h" 31 32 namespace ffrt { 33 struct WorkerGroupCtl { 34 std::unique_ptr<ThreadGroup> tg; 35 uint64_t tgRefCount = 0; 36 mutable std::shared_mutex tgMutex; 37 size_t workerStackSize = 0; 38 std::unordered_map<WorkerThread*, std::unique_ptr<WorkerThread>> threads; 39 }; 40 41 class WorkerManager { 42 public: GetWorkerNum()43 uint64_t GetWorkerNum() 44 { 45 return workerNum.load(); 46 } 47 ~WorkerManager()48 virtual ~WorkerManager() 49 { 50 }; 51 52 ThreadGroup* JoinTG(QoS& qos); 53 void LeaveTG(QoS& qos); 54 void JoinRtg(QoS& qos); 55 56 virtual bool IncWorker(const QoS& qos) = 0; 57 virtual bool DecWorker() = 0; 58 virtual void NotifyTaskAdded(const QoS& qos) = 0; 59 virtual void NotifyLocalTaskAdded(const QoS& qos) = 0; 60 virtual void NotifyWorkers(const QoS& qos, int number) = 0; 61 virtual std::mutex* GetSleepCtl(int qos) = 0; 62 virtual CPUMonitor* GetCPUMonitor() = 0; 63 GetGroupCtl()64 WorkerGroupCtl* GetGroupCtl() 65 { 66 return groupCtl; 67 } 68 protected: 69 ThreadGroup tg; 70 WorkerGroupCtl groupCtl[QoS::MaxNum()]; 71 std::atomic_uint64_t workerNum = 0; 72 }; 73 } // namespace ffrt 74 #endif 75