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 #include "util/ffrt_facade.h"
16 #include "core/version_ctx.h"
17 #include "dfx/log/ffrt_log_api.h"
18 #include "internal_inc/osal.h"
19 #include "sched/execute_ctx.h"
20 #include "tm/io_task.h"
21 #include "tm/queue_task.h"
22 #include "tm/uv_task.h"
23 #include "util/slab.h"
24 #include "util/white_list.h"
25
26 namespace {
27 constexpr int PROCESS_NAME_BUFFER_LENGTH = 1024;
28 char g_processName[PROCESS_NAME_BUFFER_LENGTH] {};
29 std::atomic<bool> g_initFlag { false };
30 std::atomic<bool> g_exitFlag { false };
31 std::atomic<bool> g_delayedWorkerExitFlag { false };
32 std::shared_mutex g_exitMtx;
33 std::once_flag g_processNameInitFlag;
34 }
35
36 namespace ffrt {
GetExitFlag()37 bool GetExitFlag()
38 {
39 return g_exitFlag.load();
40 }
41
GetInitFlag()42 bool GetInitFlag()
43 {
44 return g_initFlag.load();
45 }
46
GetExitMtx()47 std::shared_mutex& GetExitMtx()
48 {
49 return g_exitMtx;
50 }
51
GetCurrentProcessName()52 const char* GetCurrentProcessName()
53 {
54 std::call_once(g_processNameInitFlag, []() {
55 GetProcessName(g_processName, PROCESS_NAME_BUFFER_LENGTH);
56 if (strlen(g_processName) == 0) {
57 FFRT_LOGW("Get process name failed");
58 }
59 });
60 return g_processName;
61 }
62
GetDelayedWorkerExitFlag()63 bool GetDelayedWorkerExitFlag()
64 {
65 return g_delayedWorkerExitFlag;
66 }
67
SetDelayedWorkerExitFlag()68 void SetDelayedWorkerExitFlag()
69 {
70 g_delayedWorkerExitFlag.store(true);
71 }
72
73 class ProcessExitManager {
74 public:
Instance()75 static ProcessExitManager& Instance()
76 {
77 static ProcessExitManager instance;
78 return instance;
79 }
80
81 ProcessExitManager(const ProcessExitManager&) = delete;
82 ProcessExitManager& operator=(const ProcessExitManager&) = delete;
83
84 private:
ProcessExitManager()85 ProcessExitManager() {}
86
~ProcessExitManager()87 ~ProcessExitManager()
88 {
89 FFRT_LOGW("ProcessExitManager destruction enter");
90 std::lock_guard lock(g_exitMtx);
91 g_exitFlag.store(true);
92 }
93 };
94
Instance()95 FFRTFacade& FFRTFacade::Instance()
96 {
97 static FFRTFacade facade;
98 return facade;
99 }
FFRTFacade()100 FFRTFacade::FFRTFacade()
101 {
102 // control construct sequences of singletons
103 #ifdef FFRT_OH_TRACE_ENABLE
104 TraceAdapter::Instance();
105 #endif
106 SimpleAllocator<QueueTask>::Instance();
107 SimpleAllocator<IOTask>::Instance();
108 SimpleAllocator<UVTask>::Instance();
109 SimpleAllocator<VersionCtx>::Instance();
110 SimpleAllocator<WaitUntilEntry>::Instance();
111 TaskFactory<CPUEUTask>::Instance();
112 TaskFactory<QueueTask>::Instance();
113 TaskFactory<IOTask>::Instance();
114 TaskFactory<UVTask>::Instance();
115 DependenceManager::Instance();
116 QSimpleAllocator<CoRoutine>::Instance(CoStackAttr::Instance()->size);
117 CoRoutineFactory::Instance();
118 TimerManager::Instance();
119 Scheduler::Instance();
120 #ifdef FFRT_WORKER_MONITOR
121 WorkerMonitor::GetInstance();
122 #endif
123 /* By calling `FuncManager::Instance()` we force the construction
124 * of FunManager singleton static object to complete before static object `SExecuteUnit` construction.
125 * This implies that the destruction of `SExecuteUnit` will happen before `FuncManager`.
126 * And the destructor of `SExecuteUnit` waits for all threads/CPUWorkers to finish. This way
127 * we prevent use-after-free on `func_map` in `FuncManager`, when accessed by
128 * `CPUWorker` objects while being destructed. Note that `CPUWorker` destruction
129 * is managed by `unique_ptr` and we don't know exactly when it happens.
130 */
131 FuncManager::Instance();
132 /* Note that by constructing `DependenceManager` before `DelayedWorker`,
133 * we make sure that the lifetime of `DependenceManager` is longer than `DelayedWorker`.
134 * That is necessary because `DelayedWorker` may create async tasks (CPUWorker objects) which
135 * call `SDependenceManager::onTaskDone`. When that happens `SDependenceManager` must still
136 * be alive. `DelayedWorker` destructor waits for all async tasks to complete first, so the
137 * order will be (completion of async tasks) -> `~DelayedWorker()` -> `~SDependenceManager`.
138 */
139 DelayedWorker::GetInstance();
140 /* Same argument as above for ExecuteUnit. ExecuteUnit destructor is what waits on all
141 * threads to be done and delays destruction of main objects. It also initiates the
142 * tearDown. We must avoid the situation where detached threads or timer tasks
143 * are calling `SDependenceManager::onTaskDone` on destroyed SDependenceManager object.
144 */
145 ExecuteUnit::Instance();
146 /* Ensure that IOPoller is destructed after SExecuteUnit.
147 * We need to make sure that the runner in IOPoller is not
148 * going to call `SExecuteUnit::WakeupWorkers`
149 * or `ffrt::SExecuteUnit::PokeImpl`, while SExecuteUnit
150 * is being destroyed.
151 */
152 IOPoller::Instance();
153 ProcessExitManager::Instance();
154 g_initFlag.store(true);
155 InitWhiteListFlag();
156 }
157 } // namespace FFRT
158