Remote Call Framework 3.4
ThreadPool.hpp
1 
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2023, Delta V Software. All rights reserved.
6 // https://www.deltavsoft.com
7 //
8 // RCF is distributed under dual licenses - closed source or GPL.
9 // Consult your particular license for conditions of use.
10 //
11 // If you have not purchased a commercial license, you are using RCF under GPL terms.
12 //
13 // Version: 3.4
14 // Contact: support <at> deltavsoft.com
15 //
16 //******************************************************************************
17 
18 #ifndef INCLUDE_RCF_THREADMANAGER_HPP
19 #define INCLUDE_RCF_THREADMANAGER_HPP
20 
21 #include <vector>
22 
23 #include <cstdint>
24 #include <functional>
25 #include <map>
26 #include <memory>
27 
28 #include <RCF/AsioFwd.hpp>
29 #include <RCF/Export.hpp>
30 #include <RCF/ThreadLibrary.hpp>
31 #include <RCF/Timer.hpp>
32 
33 namespace RCF {
34 
35  class RcfServer;
36  typedef std::function<void(int)> Task;
37  class TaskEntry;
38  typedef std::function<void()> StopFunctor;
39 
40  class ThreadPool;
41  typedef std::shared_ptr<ThreadPool> ThreadPoolPtr;
42 
43  class AsioMuxer;
44 
45  typedef std::shared_ptr<ThreadPool> ThreadPoolPtr;
46  class ShouldStop;
47 
48  class RCF_EXPORT ThreadInfo
49  {
50  public:
51  ThreadInfo(ThreadPool & threadPool);
52  void touch();
53  void notifyBusy();
54  ThreadPool & getThreadPool();
55 
56  private:
57  friend class ThreadPool;
58  friend class ShouldStop;
59 
60  ThreadPool & mThreadPool;
61  bool mBusy = false;
62  bool mStopFlag = false;
63  bool mAlreadyRemovedFromThreadPool = false;
64  RCF::Timer mTouchTimer;
65  };
66 
67  typedef std::shared_ptr<ThreadInfo> ThreadInfoPtr;
68 
69  enum MuxerType
70  {
71  Mt_None,
72  Mt_Asio
73  };
74 
75  static const MuxerType DefaultMuxerType = Mt_Asio;
76 
78  class RCF_EXPORT ThreadPool :
79  public std::enable_shared_from_this<ThreadPool>
80  {
81  public:
82 
83  typedef std::function<void()> ThreadInitFunctor;
84  typedef std::function<void()> ThreadDeinitFunctor;
85 
86  // *** SWIG BEGIN ***
87 
88  ThreadPool(std::size_t fixedThreadCount);
89  ThreadPool(std::size_t threadMinCount, std::size_t threadMaxCount);
90 
92  void setThreadMinCount(std::size_t threadMinCount);
93 
95  std::size_t getThreadMinCount() const;
96 
98  void setThreadMaxCount(std::size_t threadMaxCount);
99 
101  std::size_t getThreadMaxCount() const;
102 
106  void setThreadIdleTimeoutMs(std::uint32_t threadIdleTimeoutMs);
107 
109  std::uint32_t getThreadIdleTimeoutMs() const;
110 
114  void setReserveLastThread(bool reserveLastThread);
115  bool getReserveLastThread() const;
116 
118  void setThreadName(const std::string & threadName);
119 
121  std::string getThreadName() const;
122 
123 
124  // *** SWIG END ***
125 
126  ~ThreadPool();
127 
128  void start();
129  void stop();
130  bool isStarted();
131 
133  void addThreadInitFunctor(
134  ThreadInitFunctor threadInitFunctor);
135 
137  void addThreadDeinitFunctor(
138  ThreadDeinitFunctor threadDeinitFunctor);
139 
140  AsioIoService * getIoService();
141 
142  void notifyBusy();
143 
144  std::size_t getThreadCount();
145 
146  void setTask(Task task);
147  void setStopFunctor(StopFunctor stopFunctor);
148 
149  void enableMuxerType(MuxerType muxerType);
150  void resetMuxers();
151 
152 
153  bool shouldStop() const;
154 
155  private:
156 
157  void onInit();
158  void onDeinit();
159  void setMyThreadName();
160 
161  bool launchThread(std::size_t howManyThreads = 1);
162 
163  void notifyReady();
164 
165  void repeatTask(
166  RCF::ThreadInfoPtr threadInfoPtr,
167  int timeoutMs);
168 
169  void cycle(int timeoutMs, ShouldStop & shouldStop);
170 
171  friend class TaskEntry;
172  friend class RcfServer;
173 
174  mutable Mutex mInitDeinitMutex;
175  std::vector<ThreadInitFunctor> mThreadInitFunctors;
176  std::vector<ThreadDeinitFunctor> mThreadDeinitFunctors;
177  std::string mThreadName;
178  std::shared_ptr<AsioMuxer> mAsioIoServicePtr;
179 
180  bool mStarted;
181  std::size_t mThreadMinCount;
182  std::size_t mThreadMaxCount;
183  bool mReserveLastThread;
184  std::uint32_t mThreadIdleTimeoutMs;
185 
186  Task mTask;
187  StopFunctor mStopFunctor;
188 
189  bool mStopFlag;
190 
191  typedef std::map<ThreadInfoPtr, ThreadPtr> ThreadMap;
192 
193  Mutex mThreadsMutex;
194  ThreadMap mThreads;
195  std::size_t mBusyCount;
196  Condition mAllThreadsStopped;
197  };
198 
199  class ThreadTouchGuard
200  {
201  public:
202  ThreadTouchGuard();
203  ~ThreadTouchGuard();
204  private:
205  ThreadInfoPtr mThreadInfoPtr;
206  };
207 
208  class ShouldStop
209  {
210  public:
211 
212  ShouldStop(
213  ThreadInfoPtr threadInfoPtr);
214 
215  bool operator()() const;
216 
217  private:
218  friend class ThreadPool;
219 
220  ThreadInfoPtr mThreadInfoPtr;
221  };
222 
223  RCF_EXPORT void setWin32ThreadName(const std::string & threadName);
224 
225 } // namespace RCF
226 
227 #endif // ! INCLUDE_RCF_THREADMANAGER_HPP
Provides RCF server-side functionality.
Definition: RcfServer.hpp:53
Represents a server-side thread pool.
Definition: ThreadPool.hpp:78
Definition: AmiIoHandler.hpp:23