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 #ifndef OHOS_IPC_IPC_WORK_THREAD_H
17 #define OHOS_IPC_IPC_WORK_THREAD_H
18 
19 #include <string>
20 #include <thread>
21 #include "refbase.h"
22 #include "hilog/log.h"
23 #include "iremote_invoker.h"
24 #include "log_tags.h"
25 
26 namespace OHOS {
27 #ifdef CONFIG_IPC_SINGLE
28 namespace IPC_SINGLE {
29 #endif
30 
31 class IPCWorkThread : public virtual RefBase {
32 public:
33     enum {
34         SPAWN_PASSIVE,
35         SPAWN_ACTIVE,
36         PROCESS_PASSIVE,
37         PROCESS_ACTIVE,
38 #ifdef CONFIG_ACTV_BINDER
39         ACTV_PASSIVE,
40         ACTV_ACTIVE,
41 #endif
42     };
43 
44     static std::string MakeBasicThreadName(int proto, int threadIndex);
45     explicit IPCWorkThread(std::string threadName);
46 
47     ~IPCWorkThread();
48 
49     void Start(int policy, int proto, int threadIndex);
50 
51     void StopWorkThread();
52     int proto_ = IRemoteObject::IF_PROT_DEFAULT;
53 
54 private:
55     int policy_ = SPAWN_PASSIVE;
56     std::thread thread_;
57     std::string threadName_;
58     static void *ThreadHandler(void *args);
59     static void JoinThread(int proto, int policy);
60     static constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_ID_IPC_COMMON, "IPCWorkThread" };
61 };
62 #ifdef CONFIG_IPC_SINGLE
63 } // namespace IPC_SINGLE
64 #endif
65 } // namespace OHOS
66 #endif // OHOS_IPC_IPC_WORK_THREAD_H
67