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 #include "eu/execute_unit.h"
17
18 #include "internal_inc/config.h"
19 #include "eu/cpuworker_manager.h"
20 #include "util/singleton_register.h"
21 #include "eu/co_routine_factory.h"
22
23 namespace ffrt {
ExecuteUnit()24 ExecuteUnit::ExecuteUnit()
25 {
26 }
27
~ExecuteUnit()28 ExecuteUnit::~ExecuteUnit()
29 {
30 for (auto& w : wManager) {
31 if (w != nullptr) {
32 delete w;
33 w = nullptr;
34 }
35 }
36 }
37
Instance()38 ExecuteUnit& ExecuteUnit::Instance()
39 {
40 return SingletonRegister<ExecuteUnit>::Instance();
41 }
42
RegistInsCb(SingleInsCB<ExecuteUnit>::Instance && cb)43 void ExecuteUnit::RegistInsCb(SingleInsCB<ExecuteUnit>::Instance &&cb)
44 {
45 SingletonRegister<ExecuteUnit>::RegistInsCb(std::move(cb));
46 }
47
BindTG(const DevType dev,QoS & qos)48 ThreadGroup* ExecuteUnit::BindTG(const DevType dev, QoS& qos)
49 {
50 return wManager[static_cast<size_t>(dev)]->JoinTG(qos);
51 }
52
BindWG(const DevType dev,QoS & qos)53 void ExecuteUnit::BindWG(const DevType dev, QoS& qos)
54 {
55 return wManager[static_cast<size_t>(dev)]->JoinRtg(qos);
56 }
57
UnbindTG(const DevType dev,QoS & qos)58 void ExecuteUnit::UnbindTG(const DevType dev, QoS& qos)
59 {
60 wManager[static_cast<size_t>(dev)]->LeaveTG(qos);
61 }
62
CreateWorkerManager()63 void ExecuteUnit::CreateWorkerManager()
64 {
65 ffrt::CoRoutineInstance(CoStackAttr::Instance()->size);
66 auto create = [&](const DevType dev) {
67 WorkerManager* manager = nullptr;
68 switch (dev) {
69 case DevType::CPU:
70 manager = InitManager();
71 break;
72 default:
73 break;
74 }
75
76 if (manager == nullptr) {
77 FFRT_LOGE("create workerManager fail, devType %d", static_cast<size_t>(dev));
78 return;
79 }
80
81 wManager[static_cast<size_t>(dev)] = manager;
82 };
83
84 for (size_t dev = 0; dev < static_cast<size_t>(DevType::DEVMAX); ++dev) {
85 create(static_cast<DevType>(dev));
86 }
87 }
88 } // namespace ffrt
89