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