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