• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "dfx/log/ffrt_log_api.h"
17 #include "internal_inc/osal.h"
18 
19 namespace {
20 constexpr int PROCESS_NAME_BUFFER_LENGTH = 1024;
21 char g_processName[PROCESS_NAME_BUFFER_LENGTH] {};
22 std::atomic<bool> g_exitFlag { false };
23 std::shared_mutex g_exitMtx;
24 std::once_flag g_processNameInitFlag;
25 }
26 
27 namespace ffrt {
GetExitFlag()28 bool GetExitFlag()
29 {
30     return g_exitFlag.load();
31 }
32 
GetExitMtx()33 std::shared_mutex& GetExitMtx()
34 {
35     return g_exitMtx;
36 }
37 
GetCurrentProcessName()38 const char* GetCurrentProcessName()
39 {
40     std::call_once(g_processNameInitFlag, []() {
41         GetProcessName(g_processName, PROCESS_NAME_BUFFER_LENGTH);
42         if (strlen(g_processName) == 0) {
43             FFRT_LOGW("Get process name failed");
44         }
45     });
46     return g_processName;
47 }
48 
49 class ProcessExitManager {
50 public:
Instance()51     static ProcessExitManager& Instance()
52     {
53         static ProcessExitManager instance;
54         return instance;
55     }
56 
57     ProcessExitManager(const ProcessExitManager&) = delete;
58     ProcessExitManager& operator=(const ProcessExitManager&) = delete;
59 
60 private:
ProcessExitManager()61     ProcessExitManager() {}
62 
~ProcessExitManager()63     ~ProcessExitManager()
64     {
65         FFRT_LOGW("ProcessExitManager destruction enter");
66         std::unique_lock lock(g_exitMtx);
67         g_exitFlag.store(true);
68     }
69 };
70 
FFRTFacade()71 FFRTFacade::FFRTFacade()
72 {
73     DependenceManager::Instance();
74     ProcessExitManager::Instance();
75     InitWhiteListFlag();
76 }
77 } // namespace FFRT