3 //=============================================================================
7 * @author Irfan Pyarali
8 * @author Johnny Willemsen
10 // ===================================================================
12 #ifndef TAO_THREAD_POOL_H
13 #define TAO_THREAD_POOL_H
15 #include /**/ "ace/pre.h"
16 #include "tao/orbconf.h"
18 #if defined (TAO_HAS_CORBA_MESSAGING) && TAO_HAS_CORBA_MESSAGING != 0
20 #if !defined (ACE_LACKS_PRAGMA_ONCE)
22 #endif /* ACE_LACKS_PRAGMA_ONCE */
24 #include "tao/RTCORBA/RTCORBA_includeC.h"
25 #include "tao/RTCORBA/RT_ORBInitializer.h"
26 #include "ace/Hash_Map_Manager.h"
27 #include "tao/Thread_Lane_Resources.h"
28 #include "tao/New_Leader_Generator.h"
30 #include "ace/Null_Mutex.h"
32 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
34 class TAO_Thread_Lane
;
37 * @class TAO_RT_New_Leader_Generator
39 * @brief Class for creating dynamic threads.
44 class TAO_RTCORBA_Export TAO_RT_New_Leader_Generator
45 : public TAO_New_Leader_Generator
49 TAO_RT_New_Leader_Generator (TAO_Thread_Lane
&lane
);
51 /// Leader/Follower class uses this method to notify the system that
52 /// we are out of leaders.
53 bool no_leaders_available ();
56 /// Lane associated with this leader generator.
57 TAO_Thread_Lane
&lane_
;
61 * @class TAO_Thread_Pool_Threads
63 * @brief Class representing a static thread running in a thread lane.
68 class TAO_Thread_Pool_Threads
: public ACE_Task_Base
72 TAO_Thread_Pool_Threads (TAO_Thread_Lane
&lane
);
74 /// Method executed when a thread is spawned.
77 /// Accessor to the lane to which this thread belongs to.
78 TAO_Thread_Lane
&lane () const;
80 /// Set TSS resources for the current thread.
81 static void set_tss_resources (TAO_ORB_Core
&orb_core
,
82 TAO_Thread_Lane
&thread_lane
);
86 virtual int run (TAO_ORB_Core
&orb_core
);
88 /// Lane to which this thread belongs to.
89 TAO_Thread_Lane
&lane_
;
93 * @class TAO_Dynamic_Thread_Pool_Threads
95 * @brief Class representing a dynamic thread running in a thread lane.
100 class TAO_Dynamic_Thread_Pool_Threads
: public TAO_Thread_Pool_Threads
104 TAO_Dynamic_Thread_Pool_Threads (TAO_Thread_Lane
&lane
);
108 virtual int run (TAO_ORB_Core
&orb_core
);
111 class TAO_Thread_Pool
;
114 * @class TAO_Thread_Lane
116 * @brief Class representing the thread lane inside a thread pool.
121 class TAO_RTCORBA_Export TAO_Thread_Lane
125 TAO_Thread_Lane (TAO_Thread_Pool
&pool
,
127 CORBA::Short lane_priority
,
128 CORBA::ULong static_threads
,
129 CORBA::ULong dynamic_threads
,
130 TAO_RT_ORBInitializer::TAO_RTCORBA_DT_LifeSpan lifespan
,
131 ACE_Time_Value
const &dynamic_thread_time
);
139 /// Finalize the resources.
142 /// Shutdown the reactor.
143 void shutdown_reactor ();
145 /// Wait for threads to exit.
148 /// Does @a mprofile belong to us?
149 int is_collocated (const TAO_MProfile
&mprofile
);
151 /// Create the static threads - only called once.
152 int create_static_threads ();
154 /// Mark that this lane is shutting down, we then don't create any
155 /// dynamic threads anymore. When the pool is shutting down the leader
156 /// follower loop is called which can cause a request to create a
157 /// new dynamic thread but we shouldn't create a new one.
158 void shutting_down ();
160 /// Called by the TAO_RT_New_Leader_Generator to request a new dynamic
163 * It can be that no thread can be created because the number of
164 * threads is equal to the maximum we can have or the Thread Lane
166 * @retval true A new thread is created
167 * @retval false No thread could be created
169 bool new_dynamic_thread ();
173 TAO_Thread_Pool
&pool () const;
174 CORBA::ULong
id () const;
176 CORBA::Short
lane_priority () const;
177 CORBA::ULong
static_threads () const;
178 CORBA::ULong
dynamic_threads () const;
180 CORBA::ULong
current_threads () const;
182 CORBA::Short
native_priority () const;
184 TAO_Thread_Lane_Resources
&resources ();
186 TAO_RT_ORBInitializer::TAO_RTCORBA_DT_LifeSpan
lifespan () const;
188 ACE_Time_Value
const &dynamic_thread_time () const;
192 /// Validate lane's priority and map it to a native value.
193 void validate_and_map_priority ();
195 int create_threads_i (TAO_Thread_Pool_Threads
&thread_pool
,
196 CORBA::ULong number_of_threads
,
199 /// Create @a number_of_threads of dynamic threads. Can be called
201 int create_dynamic_threads (CORBA::ULong number_of_threads
);
203 /// The Thread Pool to which this lane belongs.
204 TAO_Thread_Pool
&pool_
;
206 /// The id of this lane
207 CORBA::ULong
const id_
;
209 CORBA::Short lane_priority_
;
211 /// This boolean is set when we are shutting down, then we will not create
212 /// any new dynamic threads
215 /// Number of static threads
216 CORBA::ULong
const static_threads_number_
;
218 /// Maximum number of threads we are allowed to create
219 CORBA::ULong
const dynamic_threads_number_
;
221 /// Array with all static threads
222 TAO_Thread_Pool_Threads static_threads_
;
224 /// Array with all dynamic threads
225 TAO_Dynamic_Thread_Pool_Threads dynamic_threads_
;
227 TAO_RT_New_Leader_Generator new_thread_generator_
;
229 TAO_Thread_Lane_Resources resources_
;
231 CORBA::Short native_priority_
;
233 TAO_RT_ORBInitializer::TAO_RTCORBA_DT_LifeSpan
const lifespan_
;
235 ACE_Time_Value
const dynamic_thread_time_
;
237 /// Lock to guard all members of the lane
238 mutable TAO_SYNCH_MUTEX lock_
;
241 class TAO_Thread_Pool_Manager
;
244 * @class TAO_Thread_Pool
246 * @brief Class representing the thread pool inside a thread pool
252 class TAO_RTCORBA_Export TAO_Thread_Pool
255 /// Constructor (for pools without lanes).
256 TAO_Thread_Pool (TAO_Thread_Pool_Manager
&manager
,
258 CORBA::ULong stack_size
,
259 CORBA::ULong static_threads
,
260 CORBA::ULong dynamic_threads
,
261 CORBA::Short default_priority
,
262 CORBA::Boolean allow_request_buffering
,
263 CORBA::ULong max_buffered_requests
,
264 CORBA::ULong max_request_buffer_size
,
265 TAO_RT_ORBInitializer::TAO_RTCORBA_DT_LifeSpan lifespan
,
266 ACE_Time_Value
const &dynamic_thread_time
);
268 /// Constructor (for pools with lanes).
269 TAO_Thread_Pool (TAO_Thread_Pool_Manager
&manager
,
271 CORBA::ULong stack_size
,
272 const RTCORBA::ThreadpoolLanes
&lanes
,
273 CORBA::Boolean allow_borrowing
,
274 CORBA::Boolean allow_request_buffering
,
275 CORBA::ULong max_buffered_requests
,
276 CORBA::ULong max_request_buffer_size
,
277 TAO_RT_ORBInitializer::TAO_RTCORBA_DT_LifeSpan lifespan
,
278 ACE_Time_Value
const &dynamic_thread_time
);
286 /// Finalize the resources.
289 /// Shutdown the reactor.
290 void shutdown_reactor ();
292 /// Wait for threads to exit.
295 /// Mark this thread pool that we are shutting down.
296 void shutting_down ();
298 /// Does @a mprofile belong to us?
299 int is_collocated (const TAO_MProfile
&mprofile
);
301 /// Create the static threads - only called once.
302 int create_static_threads ();
304 /// Check if this thread pool has (explicit) lanes.
305 bool with_lanes () const;
309 TAO_Thread_Pool_Manager
&manager () const;
310 CORBA::ULong
id () const;
312 CORBA::ULong
stack_size () const;
313 CORBA::Boolean
allow_borrowing () const;
314 CORBA::Boolean
allow_request_buffering () const;
315 CORBA::ULong
max_buffered_requests () const;
316 CORBA::ULong
max_request_buffer_size () const;
318 TAO_Thread_Lane
**lanes ();
319 CORBA::ULong
number_of_lanes () const;
323 TAO_Thread_Pool_Manager
&manager_
;
326 CORBA::ULong stack_size_
;
327 CORBA::Boolean allow_borrowing_
;
328 CORBA::Boolean allow_request_buffering_
;
329 CORBA::ULong max_buffered_requests_
;
330 CORBA::ULong max_request_buffer_size_
;
331 ACE_Time_Value
const dynamic_thread_time_
;
333 TAO_Thread_Lane
**lanes_
;
334 CORBA::ULong number_of_lanes_
;
341 * @class TAO_Thread_Pool_Manager
343 * @brief Class for managing thread pools.
348 class TAO_RTCORBA_Export TAO_Thread_Pool_Manager
352 TAO_Thread_Pool_Manager (TAO_ORB_Core
&orb_core
);
355 ~TAO_Thread_Pool_Manager ();
357 /// Finalize the resources.
360 /// Shutdown the reactor.
361 void shutdown_reactor ();
363 /// Wait for threads to exit.
366 /// Does @a mprofile belong to us?
367 int is_collocated (const TAO_MProfile
&mprofile
);
369 /// Create a threadpool without lanes.
370 RTCORBA::ThreadpoolId
371 create_threadpool (CORBA::ULong stacksize
,
372 CORBA::ULong static_threads
,
373 CORBA::ULong dynamic_threads
,
374 RTCORBA::Priority default_priority
,
375 CORBA::Boolean allow_request_buffering
,
376 CORBA::ULong max_buffered_requests
,
377 CORBA::ULong max_request_buffer_size
,
378 TAO_RT_ORBInitializer::TAO_RTCORBA_DT_LifeSpan lifespan
,
379 ACE_Time_Value
const &dynamic_thread_time
);
381 /// Create a threadpool with lanes.
382 RTCORBA::ThreadpoolId
383 create_threadpool_with_lanes (CORBA::ULong stacksize
,
384 const RTCORBA::ThreadpoolLanes
& lanes
,
385 CORBA::Boolean allow_borrowing
,
386 CORBA::Boolean allow_request_buffering
,
387 CORBA::ULong max_buffered_requests
,
388 CORBA::ULong max_request_buffer_size
,
389 TAO_RT_ORBInitializer::TAO_RTCORBA_DT_LifeSpan lifespan
,
390 ACE_Time_Value
const &dynamic_thread_time
);
392 /// Destroy a threadpool.
393 void destroy_threadpool (RTCORBA::ThreadpoolId threadpool
);
395 TAO_Thread_Pool
*get_threadpool (RTCORBA::ThreadpoolId thread_pool_id
);
397 /// Collection of thread pools.
398 typedef ACE_Hash_Map_Manager
<RTCORBA::ThreadpoolId
, TAO_Thread_Pool
*, ACE_Null_Mutex
> THREAD_POOLS
;
402 TAO_ORB_Core
&orb_core () const;
408 RTCORBA::ThreadpoolId
409 create_threadpool_i (CORBA::ULong stacksize
,
410 CORBA::ULong static_threads
,
411 CORBA::ULong dynamic_threads
,
412 RTCORBA::Priority default_priority
,
413 CORBA::Boolean allow_request_buffering
,
414 CORBA::ULong max_buffered_requests
,
415 CORBA::ULong max_request_buffer_size
,
416 TAO_RT_ORBInitializer::TAO_RTCORBA_DT_LifeSpan lifespan
,
417 ACE_Time_Value
const &dynamic_thread_time
);
419 RTCORBA::ThreadpoolId
420 create_threadpool_with_lanes_i (CORBA::ULong stacksize
,
421 const RTCORBA::ThreadpoolLanes
& lanes
,
422 CORBA::Boolean allow_borrowing
,
423 CORBA::Boolean allow_request_buffering
,
424 CORBA::ULong max_buffered_requests
,
425 CORBA::ULong max_request_buffer_size
,
426 TAO_RT_ORBInitializer::TAO_RTCORBA_DT_LifeSpan lifespan
,
427 ACE_Time_Value
const &dynamic_thread_time
);
429 RTCORBA::ThreadpoolId
430 create_threadpool_helper (TAO_Thread_Pool
*thread_pool
);
434 TAO_ORB_Core
&orb_core_
;
436 THREAD_POOLS thread_pools_
;
437 RTCORBA::ThreadpoolId thread_pool_id_counter_
;
438 TAO_SYNCH_MUTEX lock_
;
441 TAO_END_VERSIONED_NAMESPACE_DECL
443 #if defined (__ACE_INLINE__)
444 #include "tao/RTCORBA/Thread_Pool.inl"
445 #endif /* __ACE_INLINE__ */
447 #endif /* TAO_HAS_CORBA_MESSAGING && TAO_HAS_CORBA_MESSAGING != 0 */
449 #include /**/ "ace/post.h"
451 #endif /* TAO_THREAD_POOL_H */