Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / TAO / tao / Dynamic_TP / DTP_Thread_Pool.h
blob9dbc121e046ee71c3d16d2a8ddf64ab0d1334709
1 // -*- C++ -*-
3 //=============================================================================
4 /**
5 * @file DTP_Thread_Pool.h
7 * @author Irfan Pyarali
8 * @author Johnny Willemsen
9 * @author Phil Mesnier
11 // ===================================================================
13 #ifndef TAO_DTP_THREAD_POOL_H
14 #define TAO_DTP_THREAD_POOL_H
16 #include /**/ "ace/pre.h"
17 #include "tao/orbconf.h"
19 #if defined (TAO_HAS_CORBA_MESSAGING) && TAO_HAS_CORBA_MESSAGING != 0
21 #if !defined (ACE_LACKS_PRAGMA_ONCE)
22 # pragma once
23 #endif /* ACE_LACKS_PRAGMA_ONCE */
25 #include "ace/Hash_Map_Manager.h"
26 #include "tao/Thread_Lane_Resources.h"
27 #include "tao/Dynamic_TP/dynamic_tp_export.h"
28 #include "tao/Dynamic_TP/DTP_Config.h"
29 #include "tao/New_Leader_Generator.h"
30 #include "ace/Task.h"
31 #include "ace/Null_Mutex.h"
33 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
35 class TAO_ORB_Core;
36 class TAO_DTP_Thread_Pool;
37 class TAO_DTP_Thread_Pool_Manager;
39 /**
40 * @class TAO_DTP_New_Leader_Generator
42 * @brief Class for creating dynamic threads.
44 * \nosubgrouping
46 **/
47 class TAO_Dynamic_TP_Export TAO_DTP_New_Leader_Generator
48 : public TAO_New_Leader_Generator
50 public:
51 /// Constructor.
52 TAO_DTP_New_Leader_Generator (TAO_DTP_Thread_Pool &lane);
54 /// Leader/Follower class uses this method to notify the system that
55 /// we are out of leaders.
56 bool no_leaders_available ();
58 private:
59 /// Pool associated with this leader generator.
60 TAO_DTP_Thread_Pool &pool_;
63 /**
64 * @class TAO_DTP_Thread_Pool_Threads
66 * @brief Class representing a static thread running in a thread lane.
68 * \nosubgrouping
70 **/
71 class TAO_DTP_Thread_Pool_Threads : public ACE_Task_Base
73 public:
74 /// Constructor.
75 TAO_DTP_Thread_Pool_Threads (TAO_DTP_Thread_Pool &pool);
77 /// Method executed when a thread is spawned.
78 int svc ();
80 /// Accessor to the pool to which this thread belongs to.
81 TAO_DTP_Thread_Pool &pool () const;
83 protected:
84 /// Do the real work
85 virtual int run (TAO_ORB_Core &orb_core);
87 /// Pool to which this thread belongs to.
88 TAO_DTP_Thread_Pool &pool_;
91 /**
92 * @class TAO_DTP_Termination_Waiter
94 * @brief A thread pool helper that simply waits for the detached
95 * threads to signal their termination so ORB shutdown can be orderly
97 **/
98 class TAO_DTP_Termination_Waiter : public ACE_Task_Base
100 public:
101 /// Constructor.
102 TAO_DTP_Termination_Waiter (TAO_DTP_Thread_Pool &pool);
104 /// Method executed when a thread is spawned.
105 int svc ();
107 protected:
108 /// Pool to which this thread belongs to.
109 TAO_DTP_Thread_Pool &pool_;
113 * @class TAO_DTP_Thread_Pool
115 * @brief Class representing the thread pool inside a thread pool
116 * manager.
118 * \nosubgrouping
121 class TAO_Dynamic_TP_Export TAO_DTP_Thread_Pool
123 public:
124 friend class TAO_DTP_Thread_Pool_Threads;
125 friend class TAO_DTP_Termination_Waiter;
127 TAO_DTP_Thread_Pool (TAO_DTP_Thread_Pool_Manager &manager,
128 CORBA::ULong id,
129 TAO_DTP_Definition &definition);
131 /// Destructor.
132 ~TAO_DTP_Thread_Pool ();
134 /// Open the pool.
135 void open ();
137 /// Wait for threads to exit.
138 void wait ();
140 /// Mark this thread pool that we are shutting down.
141 void shutting_down ();
143 /// Create the initial threads - only called once.
144 int create_initial_threads ();
146 /// Called by the TAO_DTP_New_Leader_Generator to request a new dynamic
147 /// thread.
149 * It can be that no thread can be created because the number of
150 * threads is equal to the maximum we can have or the Thread Lane
151 * is shutting down.
152 * @retval true A new thread is created
153 * @retval false No thread could be created
155 bool new_dynamic_thread ();
157 /// Called by the run loop to determine if to expire a thread or not
158 /// when the dynamic timeout is reached.
159 bool above_minimum ();
161 /// @name Accessors
162 // @{
163 bool use_timeouts () const;
164 const ACE_Time_Value& dynamic_thread_time () const;
166 TAO_DTP_Thread_Pool_Manager &manager () const;
167 CORBA::ULong id () const;
168 CORBA::ULong current_threads () const;
169 void add_active ();
170 bool remove_active (bool force);
172 // @}
174 private:
175 int create_threads_i (size_t count);
177 TAO_DTP_Thread_Pool_Manager &manager_;
179 CORBA::ULong id_;
181 /// This boolean is set when we are shutting down, then we will not create
182 /// any new dynamic threads
183 bool shutdown_;
185 TAO_DTP_Definition definition_;
186 TAO_DTP_Thread_Pool_Threads threads_;
187 TAO_DTP_Termination_Waiter waiter_;
189 CORBA::ULong active_count_;
191 TAO_DTP_New_Leader_Generator new_thread_generator_;
193 /// Lock to guard all members of the pool
194 mutable TAO_SYNCH_MUTEX lock_;
196 /// synchronzing new threads with the requester of threads
197 TAO_SYNCH_MUTEX activation_lock_;
198 TAO_SYNCH_CONDITION activation_cond_;
200 /// synchronzing new threads with the requester of threads
201 TAO_SYNCH_MUTEX termination_lock_;
202 TAO_SYNCH_CONDITION termination_cond_;
206 * @class TAO_DTP_Thread_Pool_Manager
208 * @brief Class for managing thread pools.
210 * \nosubgrouping
213 class TAO_Dynamic_TP_Export TAO_DTP_Thread_Pool_Manager
215 public:
216 /// Constructor.
217 TAO_DTP_Thread_Pool_Manager (TAO_ORB_Core &orb_core);
219 /// Destructor.
220 ~TAO_DTP_Thread_Pool_Manager ();
222 /// Wait for threads to exit.
223 void wait ();
225 /// Create a threadpool without lanes.
226 CORBA::ULong create_threadpool (TAO_DTP_Definition &def);
228 /// Destroy a threadpool.
229 void destroy_threadpool (CORBA::ULong threadpool);
231 /// Collection of thread pools.
232 typedef ACE_Hash_Map_Manager<CORBA::ULong, TAO_DTP_Thread_Pool *,
233 ACE_Null_Mutex> THREAD_POOLS;
235 /// @name Accessors
236 // @{
237 TAO_ORB_Core &orb_core () const;
238 // @}
240 private:
241 /// @name Helpers
242 // @{
243 CORBA::ULong
244 create_threadpool_i (TAO_DTP_Definition &def);
246 CORBA::ULong
247 create_threadpool_helper (TAO_DTP_Thread_Pool *thread_pool);
248 // @}
250 private:
251 TAO_ORB_Core &orb_core_;
253 THREAD_POOLS thread_pools_;
254 CORBA::ULong thread_pool_id_counter_;
255 TAO_SYNCH_MUTEX lock_;
258 TAO_END_VERSIONED_NAMESPACE_DECL
260 #if defined (__ACE_INLINE__)
261 #include "tao/Dynamic_TP/DTP_Thread_Pool.inl"
262 #endif /* __ACE_INLINE__ */
264 #endif /* TAO_HAS_CORBA_MESSAGING && TAO_HAS_CORBA_MESSAGING != 0 */
266 #include /**/ "ace/post.h"
268 #endif /* TAO_THREAD_POOL_H */