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_skeleton.h"
17
18 #include <memory>
19
20 #include "binder_invoker.h"
21 #include "hilog/log_c.h"
22 #include "hilog/log_cpp.h"
23 #include "invoker_factory.h"
24 #include "ipc_debug.h"
25 #include "ipc_object_proxy.h"
26 #include "iremote_invoker.h"
27 #include "iremote_object.h"
28 #include "log_tags.h"
29 #include "new"
30 #include "pthread.h"
31
32 namespace OHOS {
33 #ifdef CONFIG_IPC_SINGLE
34 namespace IPC_SINGLE {
35 #endif
36 using namespace OHOS::HiviewDFX;
37 pthread_key_t IPCThreadSkeleton::TLSKey_ = 0;
38 pthread_once_t IPCThreadSkeleton::TLSKeyOnce_ = PTHREAD_ONCE_INIT;
39 std::recursive_mutex IPCThreadSkeleton::mutex_;
40
41 static constexpr HiLogLabel LABEL = { LOG_CORE, LOG_ID_IPC, "IPCThreadSkeleton" };
TlsDestructor(void * args)42 void IPCThreadSkeleton::TlsDestructor(void *args)
43 {
44 std::lock_guard<std::recursive_mutex> lockGuard(mutex_);
45 auto *current = static_cast<IPCThreadSkeleton *>(args);
46 auto it = current->invokers_.find(IRemoteObject::IF_PROT_BINDER);
47 if (it != current->invokers_.end()) {
48 ZLOGW(LABEL, "thread exit, flush commands");
49 BinderInvoker *invoker = reinterpret_cast<BinderInvoker *>(it->second);
50 invoker->FlushCommands(nullptr);
51 invoker->ExitCurrentThread();
52 }
53 delete current;
54 }
55
MakeTlsKey()56 void IPCThreadSkeleton::MakeTlsKey()
57 {
58 pthread_key_create(&TLSKey_, IPCThreadSkeleton::TlsDestructor);
59 }
60
GetCurrent()61 IPCThreadSkeleton *IPCThreadSkeleton::GetCurrent()
62 {
63 IPCThreadSkeleton *current = nullptr;
64
65 pthread_once(&TLSKeyOnce_, IPCThreadSkeleton::MakeTlsKey);
66
67 void *curTLS = pthread_getspecific(TLSKey_);
68 if (curTLS != nullptr) {
69 current = reinterpret_cast<IPCThreadSkeleton *>(curTLS);
70 } else {
71 current = new (std::nothrow) IPCThreadSkeleton();
72 }
73
74 return current;
75 }
76
IPCThreadSkeleton()77 IPCThreadSkeleton::IPCThreadSkeleton()
78 {
79 pthread_setspecific(TLSKey_, this);
80 }
81
~IPCThreadSkeleton()82 IPCThreadSkeleton::~IPCThreadSkeleton()
83 {
84 std::lock_guard<std::recursive_mutex> lockGuard(mutex_);
85 ZLOGE(LABEL, "IPCThreadSkeleton delete");
86 for (auto it = invokers_.begin(); it != invokers_.end();) {
87 delete it->second;
88 it = invokers_.erase(it);
89 }
90 }
91
GetRemoteInvoker(int proto)92 IRemoteInvoker *IPCThreadSkeleton::GetRemoteInvoker(int proto)
93 {
94 IPCThreadSkeleton *current = IPCThreadSkeleton::GetCurrent();
95 IRemoteInvoker *invoker = nullptr;
96 if (current == nullptr) {
97 return nullptr;
98 }
99 std::lock_guard<std::recursive_mutex> lockGuard(mutex_);
100 auto it = current->invokers_.find(proto);
101 if (it != current->invokers_.end()) {
102 invoker = it->second;
103 } else {
104 InvokerFactory &factory = InvokerFactory::Get();
105 invoker = factory.newInstance(proto);
106 if (invoker == nullptr) {
107 ZLOGE(LABEL, "invoker is NULL proto = %{public}d", proto);
108 return nullptr;
109 }
110
111 // non-thread safe, add lock to protect it.
112 current->invokers_.insert(std::make_pair(proto, invoker));
113 }
114
115 return invoker;
116 }
117
GetActiveInvoker()118 IRemoteInvoker *IPCThreadSkeleton::GetActiveInvoker()
119 {
120 IRemoteInvoker *binderInvoker = IPCThreadSkeleton::GetRemoteInvoker(IRemoteObject::IF_PROT_BINDER);
121 if ((binderInvoker != nullptr) && (binderInvoker->GetStatus() == IRemoteInvoker::ACTIVE_INVOKER)) {
122 return binderInvoker;
123 }
124 #ifndef CONFIG_IPC_SINGLE
125 IRemoteInvoker *dbinderInvoker = IPCThreadSkeleton::GetRemoteInvoker(IRemoteObject::IF_PROT_DATABUS);
126 if ((dbinderInvoker != nullptr) && (dbinderInvoker->GetStatus() == IRemoteInvoker::ACTIVE_INVOKER)) {
127 return dbinderInvoker;
128 }
129 #endif
130 return nullptr;
131 }
132
GetProxyInvoker(IRemoteObject * object)133 IRemoteInvoker *IPCThreadSkeleton::GetProxyInvoker(IRemoteObject *object)
134 {
135 if (object == nullptr) {
136 ZLOGE(LABEL, "proxy is invalid");
137 return nullptr;
138 }
139 if (!object->IsProxyObject()) {
140 return nullptr;
141 }
142
143 IPCObjectProxy *proxy = reinterpret_cast<IPCObjectProxy *>(object);
144 return IPCThreadSkeleton::GetRemoteInvoker(proxy->GetProto());
145 }
146
GetDefaultInvoker()147 IRemoteInvoker *IPCThreadSkeleton::GetDefaultInvoker()
148 {
149 return GetRemoteInvoker(IRemoteObject::IF_PROT_DEFAULT);
150 }
151
JoinWorkThread(int proto)152 void IPCThreadSkeleton::JoinWorkThread(int proto)
153 {
154 IRemoteInvoker *invoker = GetRemoteInvoker(proto);
155 if (invoker != nullptr) {
156 invoker->JoinThread(true);
157 }
158 }
159
StopWorkThread(int proto)160 void IPCThreadSkeleton::StopWorkThread(int proto)
161 {
162 IRemoteInvoker *invoker = GetRemoteInvoker(proto);
163 if (invoker != nullptr) {
164 invoker->StopWorkThread();
165 }
166 }
167 #ifdef CONFIG_IPC_SINGLE
168 } // namespace IPC_SINGLE
169 #endif
170 } // namespace OHOS
171