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 
Instance()28 ExecuteUnit& ExecuteUnit::Instance()
29 {
30     return SingletonRegister<ExecuteUnit>::Instance();
31 }
32 
RegistInsCb(SingleInsCB<ExecuteUnit>::Instance && cb)33 void ExecuteUnit::RegistInsCb(SingleInsCB<ExecuteUnit>::Instance &&cb)
34 {
35     SingletonRegister<ExecuteUnit>::RegistInsCb(std::move(cb));
36 }
37 
BindTG(const DevType dev,QoS & qos)38 ThreadGroup* ExecuteUnit::BindTG(const DevType dev, QoS& qos)
39 {
40     return wManager[static_cast<size_t>(dev)]->JoinTG(qos);
41 }
42 
BindWG(const DevType dev,QoS & qos)43 void ExecuteUnit::BindWG(const DevType dev, QoS& qos)
44 {
45     return wManager[static_cast<size_t>(dev)]->JoinRtg(qos);
46 }
47 
UnbindTG(const DevType dev,QoS & qos)48 void ExecuteUnit::UnbindTG(const DevType dev, QoS& qos)
49 {
50     wManager[static_cast<size_t>(dev)]->LeaveTG(qos);
51 }
52 
CreateWorkerManager()53 void ExecuteUnit::CreateWorkerManager()
54 {
55     ffrt::CoRoutineInstance(CoStackAttr::Instance()->size);
56     auto create = [&](const DevType dev) {
57         std::unique_ptr<WorkerManager> manager;
58         switch (dev) {
59             case DevType::CPU:
60                 manager = InitManager();
61                 break;
62             default:
63                 break;
64         }
65 
66         if (!manager) {
67             FFRT_LOGE("create workerManager fail, devType %d", static_cast<size_t>(dev));
68             return;
69         }
70 
71         wManager[static_cast<size_t>(dev)] = std::move(manager);
72     };
73 
74     for (size_t dev = 0; dev < static_cast<size_t>(DevType::DEVMAX); ++dev) {
75         create(static_cast<DevType>(dev));
76     }
77 }
78 } // namespace ffrt
79