Remote Call Framework 3.1
ThreadPool.hpp
1 
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2019, 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.1
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 
133  void addThreadInitFunctor(
134  ThreadInitFunctor threadInitFunctor);
135 
136  void addThreadDeinitFunctor(
137  ThreadDeinitFunctor threadDeinitFunctor);
138 
139  AsioIoService * getIoService();
140 
141  void notifyBusy();
142 
143  std::size_t getThreadCount();
144 
145  void setTask(Task task);
146  void setStopFunctor(StopFunctor stopFunctor);
147 
148  void enableMuxerType(MuxerType muxerType);
149  void resetMuxers();
150 
151 
152  bool shouldStop() const;
153 
154  private:
155 
156  void onInit();
157  void onDeinit();
158  void setMyThreadName();
159 
160  bool launchThread(std::size_t howManyThreads = 1);
161 
162  void notifyReady();
163 
164  void repeatTask(
165  RCF::ThreadInfoPtr threadInfoPtr,
166  int timeoutMs);
167 
168  void cycle(int timeoutMs, ShouldStop & shouldStop);
169 
170  friend class TaskEntry;
171  friend class RcfServer;
172 
173  mutable Mutex mInitDeinitMutex;
174  std::vector<ThreadInitFunctor> mThreadInitFunctors;
175  std::vector<ThreadDeinitFunctor> mThreadDeinitFunctors;
176  std::string mThreadName;
177  std::shared_ptr<AsioMuxer> mAsioIoServicePtr;
178 
179  bool mStarted;
180  std::size_t mThreadMinCount;
181  std::size_t mThreadMaxCount;
182  bool mReserveLastThread;
183  std::uint32_t mThreadIdleTimeoutMs;
184 
185  Task mTask;
186  StopFunctor mStopFunctor;
187 
188  bool mStopFlag;
189 
190  typedef std::map<ThreadInfoPtr, ThreadPtr> ThreadMap;
191 
192  Mutex mThreadsMutex;
193  ThreadMap mThreads;
194  std::size_t mBusyCount;
195  Condition mAllThreadsStopped;
196  };
197 
198  class ThreadTouchGuard
199  {
200  public:
201  ThreadTouchGuard();
202  ~ThreadTouchGuard();
203  private:
204  ThreadInfoPtr mThreadInfoPtr;
205  };
206 
207  class ShouldStop
208  {
209  public:
210 
211  ShouldStop(
212  ThreadInfoPtr threadInfoPtr);
213 
214  bool operator()() const;
215 
216  private:
217  friend class ThreadPool;
218 
219  ThreadInfoPtr mThreadInfoPtr;
220  };
221 
222  RCF_EXPORT void setWin32ThreadName(const std::string & threadName);
223 
224 } // namespace RCF
225 
226 #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