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 <condition_variable> 23 #include <unordered_map> 24 25 #include "eu/worker_thread.h" 26 #include "eu/thread_group.h" 27 #include "sync/sync.h" 28 #include "dfx/log/ffrt_log_api.h" 29 30 namespace ffrt { 31 struct WorkerGroupCtl { 32 std::unique_ptr<ThreadGroup> tg; 33 uint64_t tgRefCount = 0; 34 mutable fast_mutex tgMutex; 35 std::unordered_map<WorkerThread*, std::unique_ptr<WorkerThread>> threads; 36 }; 37 38 class WorkerManager { 39 public: ~WorkerManager()40 virtual ~WorkerManager() 41 { 42 }; 43 44 ThreadGroup* JoinTG(QoS& qos); 45 void LeaveTG(QoS& qos); 46 void JoinRtg(QoS& qos); 47 48 virtual bool IncWorker(const QoS& qos) = 0; 49 virtual bool DecWorker() = 0; 50 virtual void NotifyTaskAdded(const QoS& qos) = 0; 51 virtual std::mutex* GetSleepCtl(int qos) = 0; 52 GetGroupCtl()53 WorkerGroupCtl* GetGroupCtl() 54 { 55 return groupCtl; 56 } 57 protected: 58 ThreadGroup tg; 59 WorkerGroupCtl groupCtl[QoS::Max()]; 60 }; 61 } // namespace ffrt 62 #endif 63