3 //=============================================================================
5 * @file DTP_Thread_Pool.h
7 * @author Irfan Pyarali
8 * @author Johnny Willemsen
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)
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"
31 #include "ace/Null_Mutex.h"
33 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
36 class TAO_DTP_Thread_Pool
;
37 class TAO_DTP_Thread_Pool_Manager
;
40 * @class TAO_DTP_New_Leader_Generator
42 * @brief Class for creating dynamic threads.
47 class TAO_Dynamic_TP_Export TAO_DTP_New_Leader_Generator
48 : public TAO_New_Leader_Generator
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 ();
59 /// Pool associated with this leader generator.
60 TAO_DTP_Thread_Pool
&pool_
;
64 * @class TAO_DTP_Thread_Pool_Threads
66 * @brief Class representing a static thread running in a thread lane.
71 class TAO_DTP_Thread_Pool_Threads
: public ACE_Task_Base
75 TAO_DTP_Thread_Pool_Threads (TAO_DTP_Thread_Pool
&pool
);
77 /// Method executed when a thread is spawned.
80 /// Accessor to the pool to which this thread belongs to.
81 TAO_DTP_Thread_Pool
&pool () const;
85 virtual int run (TAO_ORB_Core
&orb_core
);
87 /// Pool to which this thread belongs to.
88 TAO_DTP_Thread_Pool
&pool_
;
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
98 class TAO_DTP_Termination_Waiter
: public ACE_Task_Base
102 TAO_DTP_Termination_Waiter (TAO_DTP_Thread_Pool
&pool
);
104 /// Method executed when a thread is spawned.
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
121 class TAO_Dynamic_TP_Export TAO_DTP_Thread_Pool
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
,
129 TAO_DTP_Definition
&definition
);
132 ~TAO_DTP_Thread_Pool ();
137 /// Wait for threads to exit.
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
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
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 ();
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;
170 bool remove_active (bool force
);
175 int create_threads_i (size_t count
);
177 TAO_DTP_Thread_Pool_Manager
&manager_
;
181 /// This boolean is set when we are shutting down, then we will not create
182 /// any new dynamic threads
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.
213 class TAO_Dynamic_TP_Export TAO_DTP_Thread_Pool_Manager
217 TAO_DTP_Thread_Pool_Manager (TAO_ORB_Core
&orb_core
);
220 ~TAO_DTP_Thread_Pool_Manager ();
222 /// Wait for threads to exit.
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
;
237 TAO_ORB_Core
&orb_core () const;
244 create_threadpool_i (TAO_DTP_Definition
&def
);
247 create_threadpool_helper (TAO_DTP_Thread_Pool
*thread_pool
);
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 */