• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2024 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_THREAD_SKELETON_H
17 #define OHOS_IPC_IPC_THREAD_SKELETON_H
18 
19 #include <mutex>
20 #include <pthread.h>
21 #include <unordered_map>
22 
23 #include "binder_invoker.h"
24 #include "iremote_invoker.h"
25 
26 namespace OHOS {
27 #ifdef CONFIG_IPC_SINGLE
28 namespace IPC_SINGLE {
29 #endif
30 
31 enum class ThreadType {
32     NORMAL_THREAD = 0xB0B0B0B0,
33     IPC_THREAD = 0xB1B1B1B1,
34 };
35 
36 class IPCThreadSkeleton {
37 public:
38     IPCThreadSkeleton();
39 
40     ~IPCThreadSkeleton();
41 
42     static void TlsDestructor(void *args);
43     static void MakeTlsKey();
44 
45     static IPCThreadSkeleton *GetCurrent();
46 
47     static IRemoteInvoker *GetRemoteInvoker(int proto);
48 
49     static IRemoteInvoker *GetDefaultInvoker();
50 
51     static IRemoteInvoker *GetActiveInvoker();
52 
53     static IRemoteInvoker *GetProxyInvoker(IRemoteObject *object);
54 
55     static pthread_key_t GetTlsKey();
56 
57     static void GetVaildInstance(IPCThreadSkeleton *&instance);
58 
59     static void SaveThreadName(const std::string &name);
60 
61     static bool UpdateSendRequestCount(int delta);
62 
63     static bool IsInstanceException(std::atomic<uint32_t> &flag);
64 
65     static bool SetThreadType(ThreadType type);
66 
67     static ThreadType GetThreadType();
68 
69     bool IsSendRequesting();
70 
71     // Joint Current thread into IPC Work Group
72     void JoinWorkThread(int proto);
73     // Quit current thread from IPC work group.
74     void StopWorkThread(int proto);
75 
76     static constexpr uint32_t INVOKER_USE_MAGIC = 0x5A5A5A5A;
77     static constexpr uint32_t INVOKER_IDLE_MAGIC = 0xA5A5A5A5;
78 
79 private:
80     static pthread_key_t TLSKey_;
81     static pthread_once_t TLSKeyOnce_;
82     std::atomic<uint32_t> exitFlag_ = INVOKER_USE_MAGIC;
83     std::atomic<uint32_t> usingFlag_ = INVOKER_IDLE_MAGIC;
84     static constexpr uint32_t INVOKER_MAX_COUNT = 2;
85     IRemoteInvoker *invokers_[INVOKER_MAX_COUNT] = { nullptr, nullptr };
86     const pid_t tid_;
87     std::atomic<int32_t> sendRequestCount_ = 0;
88     std::string threadName_;
89     ThreadType threadType_ = ThreadType::NORMAL_THREAD;
90     uint64_t ffrtTaskId_ = 0;
91 };
92 #ifdef CONFIG_IPC_SINGLE
93 } // namespace IPC_SINGLE
94 #endif
95 } // namespace OHOS
96 #endif // OHOS_IPC_IPC_THREAD_SKELETON_H
97