1 /*
2 * Copyright (C) 2021 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 "ipc_thread_pool.h"
17
18 #include <dlfcn.h>
19 #include <sys/types.h>
20 #include <unistd.h>
21
22 #include "ipc_debug.h"
23 #include "log_tags.h"
24
25 namespace OHOS {
26 #ifdef CONFIG_IPC_SINGLE
27 namespace IPC_SINGLE {
28 #endif
29
30 static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_IPC_COMMON, "IPCWorkThreadPool" };
31
32 static void *g_selfSoHandler = nullptr;
33
34 // this func is called when ipc_single and ipc_core before loading
InitIpcSo()35 extern "C" __attribute__((constructor)) void InitIpcSo()
36 {
37 if (g_selfSoHandler == nullptr) {
38 Dl_info info;
39 // dladdr func return value description
40 // On success, these functions return a nonzero value.
41 // If the address specified in addr could not be matched to a shared object, then these functions return 0
42 int ret = dladdr(reinterpret_cast<void *>(InitIpcSo), &info);
43 if (ret == 0) {
44 ZLOGE(LOG_LABEL, "dladdr func call failed");
45 return;
46 }
47 g_selfSoHandler = dlopen(info.dli_fname, RTLD_LAZY);
48 if (g_selfSoHandler == nullptr) {
49 const char *error = dlerror();
50 ZLOGE(LOG_LABEL, "dlopen failed, dlerror:%{public}s", error != nullptr ? error : "unknown");
51 }
52 }
53 }
54
IPCWorkThreadPool(int maxThreadNum)55 IPCWorkThreadPool::IPCWorkThreadPool(int maxThreadNum)
56 : threadSequence_(0), maxThreadNum_(maxThreadNum + maxThreadNum), idleThreadNum_(maxThreadNum),
57 idleSocketThreadNum_(maxThreadNum)
58 {
59 }
60
~IPCWorkThreadPool()61 IPCWorkThreadPool::~IPCWorkThreadPool()
62 {
63 StopAllThreads();
64 }
65
StopAllThreads()66 void IPCWorkThreadPool::StopAllThreads()
67 {
68 std::lock_guard<std::mutex> lock(mutex_);
69 for (auto it = threads_.begin(); it != threads_.end(); it++) {
70 it->second->StopWorkThread();
71 }
72 threads_.clear();
73 }
74
SpawnThread(int policy,int proto)75 bool IPCWorkThreadPool::SpawnThread(int policy, int proto)
76 {
77 std::lock_guard<std::mutex> lock(mutex_);
78 if (!(proto == IRemoteObject::IF_PROT_DEFAULT && idleThreadNum_ > 0) &&
79 !(proto == IRemoteObject::IF_PROT_DATABUS && idleSocketThreadNum_ > 0)) {
80 return false;
81 }
82 int threadIndex = 0;
83 std::string threadName = MakeThreadName(proto, threadIndex);
84 ZLOGD(LOG_LABEL, "name:%{public}s", threadName.c_str());
85
86 if (threads_.find(threadName) == threads_.end()) {
87 sptr<IPCWorkThread> newThread = sptr<IPCWorkThread>::MakeSptr(threadName);
88 if (newThread == nullptr) {
89 ZLOGE(LOG_LABEL, "create IPCWorkThread object failed");
90 return false;
91 }
92 threads_[threadName] = newThread;
93 if (proto == IRemoteObject::IF_PROT_DEFAULT) {
94 idleThreadNum_--;
95 ZLOGD(LOG_LABEL, "now idleThreadNum:%{public}d", idleThreadNum_);
96 }
97 if (proto == IRemoteObject::IF_PROT_DATABUS) {
98 idleSocketThreadNum_--;
99 ZLOGD(LOG_LABEL, "now idleSocketThreadNum:%{public}d", idleSocketThreadNum_);
100 }
101 newThread->Start(policy, proto, threadIndex);
102 return true;
103 }
104 return false;
105 }
106
MakeThreadName(int proto,int & threadIndex)107 std::string IPCWorkThreadPool::MakeThreadName(int proto, int &threadIndex)
108 {
109 int sequence = threadSequence_.fetch_add(1, std::memory_order_relaxed);
110 threadIndex = sequence;
111 return IPCWorkThread::MakeBasicThreadName(proto, sequence);
112 }
113
RemoveThread(const std::string & threadName)114 bool IPCWorkThreadPool::RemoveThread(const std::string &threadName)
115 {
116 std::lock_guard<std::mutex> lock(mutex_);
117 auto it = threads_.find(threadName);
118 if (it != threads_.end()) {
119 sptr<IPCWorkThread> workThread = it->second;
120 if (workThread == nullptr) {
121 return false;
122 }
123 if (workThread->proto_ == IRemoteObject::IF_PROT_DEFAULT) {
124 idleThreadNum_++;
125 } else if (workThread->proto_ == IRemoteObject::IF_PROT_DATABUS) {
126 idleSocketThreadNum_++;
127 }
128 threads_.erase(it);
129 ZLOGD(LOG_LABEL, "now idleThreadNum:%{public}d", idleSocketThreadNum_);
130 return true;
131 }
132 return false;
133 }
134
GetSocketIdleThreadNum() const135 int IPCWorkThreadPool::GetSocketIdleThreadNum() const
136 {
137 return idleSocketThreadNum_;
138 }
139
GetSocketTotalThreadNum() const140 int IPCWorkThreadPool::GetSocketTotalThreadNum() const
141 {
142 return maxThreadNum_ / PROTO_NUM;
143 }
144
GetMaxThreadNum() const145 int IPCWorkThreadPool::GetMaxThreadNum() const
146 {
147 return maxThreadNum_ / PROTO_NUM;
148 }
149
UpdateMaxThreadNum(int maxThreadNum)150 void IPCWorkThreadPool::UpdateMaxThreadNum(int maxThreadNum)
151 {
152 /*
153 * not support delete thread, because thread is in using
154 */
155 int totalNum = maxThreadNum + maxThreadNum;
156 std::lock_guard<std::mutex> lock(mutex_);
157 if (totalNum <= maxThreadNum_) {
158 return;
159 }
160 int diff = totalNum - maxThreadNum_;
161 maxThreadNum_ = totalNum;
162 idleThreadNum_ += diff / PROTO_NUM;
163 idleSocketThreadNum_ += diff / PROTO_NUM;
164 }
165 #ifdef CONFIG_IPC_SINGLE
166 } // namespace IPC_SINGLE
167 #endif
168 } // namespace OHOS
169