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