RCFProto
 All Classes Functions Typedefs
ThreadPool.hpp
1 
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2013, 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: 2.0
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 <boost/bind.hpp>
25 #include <boost/cstdint.hpp>
26 #include <boost/enable_shared_from_this.hpp>
27 #include <boost/function.hpp>
28 #include <boost/shared_ptr.hpp>
29 
30 #include <RCF/AsioFwd.hpp>
31 #include <RCF/Export.hpp>
32 #include <RCF/ThreadLibrary.hpp>
33 #include <RCF/Timer.hpp>
34 #include <RCF/Tools.hpp>
35 
36 namespace boost {
37  namespace asio {
38  class io_service;
39  }
40 }
41 
42 namespace RCF {
43 
44  class RcfServer;
45  typedef boost::function1<void, int> Task;
46  class TaskEntry;
47  typedef boost::function0<void> StopFunctor;
48 
49  class ThreadPool;
50  typedef boost::shared_ptr<ThreadPool> ThreadPoolPtr;
51 
52  class AsioMuxer;
53 
54  typedef boost::shared_ptr<ThreadPool> ThreadPoolPtr;
55  class ShouldStop;
56 
57  class RCF_EXPORT ThreadInfo
58  {
59  public:
60  ThreadInfo(ThreadPool & threadPool);
61  void touch();
62  void notifyBusy();
63  ThreadPool & getThreadPool();
64 
65  private:
66  friend class ThreadPool;
67  friend class ShouldStop;
68 
69  ThreadPool & mThreadPool;
70  bool mBusy;
71  bool mStopFlag;
72  RCF::Timer mTouchTimer;
73  };
74 
75  typedef boost::shared_ptr<ThreadInfo> ThreadInfoPtr;
76 
77  enum MuxerType
78  {
79  Mt_None,
80  Mt_Asio
81  };
82 
83  static const MuxerType DefaultMuxerType = Mt_Asio;
84 
86  class RCF_EXPORT ThreadPool :
87  public boost::enable_shared_from_this<ThreadPool>
88  {
89  public:
90 
91  typedef boost::function0<void> ThreadInitFunctor;
92  typedef boost::function0<void> ThreadDeinitFunctor;
93 
94  // *** SWIG BEGIN ***
95 
96  ThreadPool(std::size_t fixedThreadCount);
97  ThreadPool(std::size_t threadMinCount, std::size_t threadMaxCount);
98 
100  void setThreadMinCount(std::size_t threadMinCount);
101 
103  std::size_t getThreadMinCount() const;
104 
106  void setThreadMaxCount(std::size_t threadMaxCount);
107 
109  std::size_t getThreadMaxCount() const;
110 
114  void setThreadIdleTimeoutMs(boost::uint32_t threadIdleTimeoutMs);
115 
117  boost::uint32_t getThreadIdleTimeoutMs() const;
118 
122  void setReserveLastThread(bool reserveLastThread);
123  bool getReserveLastThread() const;
124 
126  void setThreadName(const std::string & threadName);
127 
129  std::string getThreadName() const;
130 
131 
132  // *** SWIG END ***
133 
134  ~ThreadPool();
135 
136  void start();
137  void stop();
138  bool isStarted();
139 
140  void addThreadInitFunctor(
141  ThreadInitFunctor threadInitFunctor);
142 
143  void addThreadDeinitFunctor(
144  ThreadDeinitFunctor threadDeinitFunctor);
145 
146  AsioIoService * getIoService();
147 
148  void notifyBusy();
149 
150  std::size_t getThreadCount();
151 
152  void setTask(Task task);
153  void setStopFunctor(StopFunctor stopFunctor);
154 
155  void enableMuxerType(MuxerType muxerType);
156  void resetMuxers();
157 
158 
159  bool shouldStop() const;
160 
161  private:
162 
163  void onInit();
164  void onDeinit();
165  void setMyThreadName();
166 
167  bool launchThread(std::size_t howManyThreads = 1);
168 
169  void notifyReady();
170 
171  void repeatTask(
172  RCF::ThreadInfoPtr threadInfoPtr,
173  int timeoutMs);
174 
175  void cycle(int timeoutMs, ShouldStop & shouldStop);
176 
177  friend class TaskEntry;
178  friend class RcfServer;
179 
180  mutable Mutex mInitDeinitMutex;
181  std::vector<ThreadInitFunctor> mThreadInitFunctors;
182  std::vector<ThreadDeinitFunctor> mThreadDeinitFunctors;
183  std::string mThreadName;
184  boost::shared_ptr<AsioMuxer> mAsioIoServicePtr;
185 
186  bool mStarted;
187  std::size_t mThreadMinCount;
188  std::size_t mThreadMaxCount;
189  bool mReserveLastThread;
190  boost::uint32_t mThreadIdleTimeoutMs;
191 
192  Task mTask;
193  StopFunctor mStopFunctor;
194 
195  bool mStopFlag;
196 
197  typedef std::map<ThreadInfoPtr, ThreadPtr> ThreadMap;
198 
199  Mutex mThreadsMutex;
200  ThreadMap mThreads;
201  std::size_t mBusyCount;
202  };
203 
204  class ThreadTouchGuard
205  {
206  public:
207  ThreadTouchGuard();
208  ~ThreadTouchGuard();
209  private:
210  ThreadInfoPtr mThreadInfoPtr;
211  };
212 
213  class ShouldStop
214  {
215  public:
216 
217  ShouldStop(
218  ThreadInfoPtr threadInfoPtr);
219 
220  bool operator()() const;
221 
222  private:
223  friend class ThreadPool;
224 
225  ThreadInfoPtr mThreadInfoPtr;
226  };
227 
228  RCF_EXPORT void setWin32ThreadName(const std::string & threadName);
229 
230 } // namespace RCF
231 
232 #endif // ! INCLUDE_RCF_THREADMANAGER_HPP