• 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 #ifndef OHOS_IPC_IPC_WORK_THREAD_POOL_H
17 #define OHOS_IPC_IPC_WORK_THREAD_POOL_H
18 
19 #include <functional>
20 #include <thread>
21 #include <map>
22 #include <atomic>
23 #include <mutex>
24 #include <ipc_workthread.h>
25 #include "hilog/log.h"
26 #include "log_tags.h"
27 
28 namespace OHOS {
29 #ifdef CONFIG_IPC_SINGLE
30 namespace IPC_SINGLE {
31 #endif
32 
33 class IPCWorkThreadPool {
34 public:
35     IPCWorkThreadPool(const IPCWorkThreadPool &) = delete;
36 
37     IPCWorkThreadPool(IPCWorkThreadPool &&) = delete;
38 
39     ~IPCWorkThreadPool();
40 
41     IPCWorkThreadPool &operator = (const IPCWorkThreadPool &) = delete;
42 
43     IPCWorkThreadPool &operator = (IPCWorkThreadPool &&) = delete;
44 
45     bool SpawnThread(int policy = IPCWorkThread::SPAWN_PASSIVE, int proto = IRemoteObject::IF_PROT_DEFAULT);
46 
47     bool RemoveThread(const std::string &threadName);
48 
49     void StopAllThreads();
50 
51     explicit IPCWorkThreadPool(int maxThreadNum);
52 
53     int GetMaxThreadNum() const;
54 
55     void UpdateMaxThreadNum(int maxThreadNum);
56     int GetSocketIdleThreadNum() const;
57     int GetSocketTotalThreadNum() const;
58 
59 private:
60     static constexpr int PROTO_NUM = 2;
61     std::string MakeThreadName(int proto);
62     std::map<std::string, sptr<IPCWorkThread>> threads_;
63     std::atomic<int> threadSequence_;
64     int maxThreadNum_;
65     int idleThreadNum_;
66     int idleSocketThreadNum_;
67     std::mutex mutex_;
68     static constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_ID_IPC, "IPCWorkThreadPool" };
69 };
70 #ifdef CONFIG_IPC_SINGLE
71 } // namespace IPC_SINGLE
72 #endif
73 } // namespace OHOS
74 #endif // OHOS_IPC_IPC_WORK_THREAD_POOL_H
75