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_workthread.h"
17
18 #include <cmath>
19 #include <cstddef>
20 #include <memory>
21 #include <pthread.h>
22 #include <sys/prctl.h>
23 #include <sys/syscall.h>
24 #include <unistd.h>
25 #include "hilog/log_cpp.h"
26 #include "iosfwd"
27 #include "ipc_debug.h"
28 #include "ipc_process_skeleton.h"
29 #include "ipc_thread_skeleton.h"
30 #include "iremote_invoker.h"
31 #include "string"
32 #include "type_traits"
33 #include "unistd.h"
34
35 namespace OHOS {
36 #ifdef CONFIG_IPC_SINGLE
37 namespace IPC_SINGLE {
38 #endif
39
40 static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_IPC, "IPCWorkThread" };
41
IPCWorkThread(std::string threadName)42 IPCWorkThread::IPCWorkThread(std::string threadName) : threadName_(std::move(threadName)) {}
43
~IPCWorkThread()44 IPCWorkThread::~IPCWorkThread()
45 {
46 StopWorkThread();
47 }
48
ThreadHandler(void * args)49 void *IPCWorkThread::ThreadHandler(void *args)
50 {
51 IPCWorkThread *threadObj = (IPCWorkThread *)args;
52 IRemoteInvoker *invoker = IPCThreadSkeleton::GetRemoteInvoker(threadObj->proto_);
53 threadObj->threadName_ += "_" + std::to_string(syscall(SYS_gettid));
54 int32_t ret = prctl(PR_SET_NAME, threadObj->threadName_.c_str());
55 if (ret != 0) {
56 ZLOGE(LOG_LABEL, "set thread name: %{public}s fail, ret: %{public}d",
57 threadObj->threadName_.c_str(), ret);
58 }
59 ZLOGD(LOG_LABEL, "proto_=%{public}d,policy_=%{public}d, name: %{public}s, ret: %{public}d",
60 threadObj->proto_, threadObj->policy_, threadObj->threadName_.c_str(), ret);
61 if (invoker != nullptr) {
62 switch (threadObj->policy_) {
63 case SPAWN_PASSIVE:
64 invoker->JoinThread(false);
65 break;
66 case SPAWN_ACTIVE:
67 invoker->JoinThread(true);
68 break;
69 case PROCESS_PASSIVE:
70 invoker->JoinProcessThread(false);
71 break;
72 case PROCESS_ACTIVE:
73 invoker->JoinProcessThread(true);
74 break;
75 default:
76 ZLOGE(LOG_LABEL, "policy_ = %{public}d", threadObj->policy_);
77 break;
78 }
79 }
80
81 IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
82 if (current != nullptr) {
83 current->OnThreadTerminated(threadObj->threadName_);
84 }
85 return nullptr;
86 }
87
StopWorkThread()88 void IPCWorkThread::StopWorkThread()
89 {
90 IRemoteInvoker *invoker = IPCThreadSkeleton::GetRemoteInvoker(proto_);
91 if (invoker != nullptr) {
92 invoker->StopWorkThread();
93 }
94 }
95
Start(int policy,int proto,std::string threadName)96 void IPCWorkThread::Start(int policy, int proto, std::string threadName)
97 {
98 policy_ = policy;
99 proto_ = proto;
100 threadName_ = threadName;
101 pthread_t threadId;
102 int ret = pthread_create(&threadId, NULL, &IPCWorkThread::ThreadHandler, this);
103 if (ret != 0) {
104 ZLOGE(LOG_LABEL, "create thread failed");
105 }
106 ZLOGD(LOG_LABEL, "create thread, policy=%d, proto=%d", policy, proto);
107 if (pthread_detach(threadId) != 0) {
108 ZLOGE(LOG_LABEL, "detach error");
109 }
110 }
111 #ifdef CONFIG_IPC_SINGLE
112 } // namespace IPC_SINGLE
113 #endif
114 } // namespace OHOS
115