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_CPUWORKER_MANAGER_HPP 17 #define FFRT_CPUWORKER_MANAGER_HPP 18 19 #include "eu/worker_manager.h" 20 #include "eu/cpu_worker.h" 21 #include "eu/cpu_monitor.h" 22 #include "eu/cpu_manager_interface.h" 23 24 25 namespace ffrt { 26 constexpr int MANAGER_DESTRUCT_TIMESOUT = 1000000; 27 28 struct WorkerSleepCtl { 29 std::mutex mutex; 30 std::condition_variable cv; 31 }; 32 33 class CPUWorkerManager : public WorkerManager { 34 public: 35 CPUWorkerManager(); 36 ~CPUWorkerManager()37 ~CPUWorkerManager() override 38 { 39 tearDown = true; 40 for (auto qos = QoS::Min(); qos < QoS::Max(); ++qos) { 41 int try_cnt = MANAGER_DESTRUCT_TIMESOUT; 42 while (try_cnt--) { 43 sleepCtl[qos].cv.notify_all(); 44 { 45 usleep(1); 46 std::unique_lock lock(groupCtl[qos].tgMutex); 47 if (groupCtl[qos].threads.empty()) { 48 break; 49 } 50 } 51 } 52 53 if (try_cnt <= 0) { 54 FFRT_LOGE("erase qos[%d] threads failed", qos); 55 } 56 } 57 } 58 59 void NotifyTaskAdded(const QoS& qos) override; 60 GetSleepCtl(int qos)61 std::mutex* GetSleepCtl(int qos) override 62 { 63 return &sleepCtl[qos].mutex; 64 } 65 66 private: 67 bool WorkerTearDown(); 68 bool IncWorker(const QoS& qos) override; DecWorker()69 bool DecWorker() override 70 {return false;} 71 void WakeupWorkers(const QoS& qos); 72 int GetTaskCount(const QoS& qos); 73 void WorkerRetired(WorkerThread* thread); 74 TaskCtx* PickUpTask(WorkerThread* thread); 75 void NotifyTaskPicked(const WorkerThread* thread); 76 WorkerAction WorkerIdleAction(const WorkerThread* thread); 77 void WorkerJoinTg(const QoS& qos, pid_t pid); 78 void WorkerLeaveTg(const QoS& qos, pid_t pid); 79 void WorkerSetup(WorkerThread* thread, const QoS& qos); 80 81 CPUMonitor monitor; 82 WorkerSleepCtl sleepCtl[QoS::Max()]; 83 bool tearDown = false; 84 }; 85 } // namespace ffrt 86 #endif 87