Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / TAO / tao / RTCORBA / Thread_Pool.h
blob0827cc25574c6d3b9c446b43a952fb8fd6a85d1c
1 // -*- C++ -*-
3 //=============================================================================
4 /**
5 * @file Thread_Pool.h
7 * @author Irfan Pyarali
8 * @author Johnny Willemsen
9 */
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)
21 # 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"
29 #include "ace/Task.h"
30 #include "ace/Null_Mutex.h"
32 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
34 class TAO_Thread_Lane;
36 /**
37 * @class TAO_RT_New_Leader_Generator
39 * @brief Class for creating dynamic threads.
41 * \nosubgrouping
43 **/
44 class TAO_RTCORBA_Export TAO_RT_New_Leader_Generator
45 : public TAO_New_Leader_Generator
47 public:
48 /// Constructor.
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 ();
55 private:
56 /// Lane associated with this leader generator.
57 TAO_Thread_Lane &lane_;
60 /**
61 * @class TAO_Thread_Pool_Threads
63 * @brief Class representing a static thread running in a thread lane.
65 * \nosubgrouping
67 **/
68 class TAO_Thread_Pool_Threads : public ACE_Task_Base
70 public:
71 /// Constructor.
72 TAO_Thread_Pool_Threads (TAO_Thread_Lane &lane);
74 /// Method executed when a thread is spawned.
75 int svc ();
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);
84 protected:
85 /// Do the real work
86 virtual int run (TAO_ORB_Core &orb_core);
88 /// Lane to which this thread belongs to.
89 TAO_Thread_Lane &lane_;
92 /**
93 * @class TAO_Dynamic_Thread_Pool_Threads
95 * @brief Class representing a dynamic thread running in a thread lane.
97 * \nosubgrouping
99 **/
100 class TAO_Dynamic_Thread_Pool_Threads : public TAO_Thread_Pool_Threads
102 public:
103 /// Constructor.
104 TAO_Dynamic_Thread_Pool_Threads (TAO_Thread_Lane &lane);
106 protected:
107 /// Do the real work
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.
118 * \nosubgrouping
121 class TAO_RTCORBA_Export TAO_Thread_Lane
123 public:
124 /// Constructor.
125 TAO_Thread_Lane (TAO_Thread_Pool &pool,
126 CORBA::ULong id,
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);
133 /// Destructor.
134 ~TAO_Thread_Lane ();
136 /// Open the lane.
137 void open ();
139 /// Finalize the resources.
140 void finalize ();
142 /// Shutdown the reactor.
143 void shutdown_reactor ();
145 /// Wait for threads to exit.
146 void wait ();
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
161 /// thread.
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
165 * is shutting down.
166 * @retval true A new thread is created
167 * @retval false No thread could be created
169 bool new_dynamic_thread ();
171 /// @name Accessors
172 // @{
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;
189 // @}
191 private:
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,
197 long thread_flags);
199 /// Create @a number_of_threads of dynamic threads. Can be called
200 /// multiple times.
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
213 bool shutdown_;
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
247 * manager.
249 * \nosubgrouping
252 class TAO_RTCORBA_Export TAO_Thread_Pool
254 public:
255 /// Constructor (for pools without lanes).
256 TAO_Thread_Pool (TAO_Thread_Pool_Manager &manager,
257 CORBA::ULong id,
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,
270 CORBA::ULong id,
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);
280 /// Destructor.
281 ~TAO_Thread_Pool ();
283 /// Open the pool.
284 void open ();
286 /// Finalize the resources.
287 void finalize ();
289 /// Shutdown the reactor.
290 void shutdown_reactor ();
292 /// Wait for threads to exit.
293 void wait ();
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;
307 /// @name Accessors
308 // @{
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;
320 // @}
322 private:
323 TAO_Thread_Pool_Manager &manager_;
324 CORBA::ULong id_;
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_;
335 bool with_lanes_;
338 class TAO_ORB_Core;
341 * @class TAO_Thread_Pool_Manager
343 * @brief Class for managing thread pools.
345 * \nosubgrouping
348 class TAO_RTCORBA_Export TAO_Thread_Pool_Manager
350 public:
351 /// Constructor.
352 TAO_Thread_Pool_Manager (TAO_ORB_Core &orb_core);
354 /// Destructor.
355 ~TAO_Thread_Pool_Manager ();
357 /// Finalize the resources.
358 void finalize ();
360 /// Shutdown the reactor.
361 void shutdown_reactor ();
363 /// Wait for threads to exit.
364 void wait ();
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;
400 /// @name Accessors
401 // @{
402 TAO_ORB_Core &orb_core () const;
403 // @}
405 private:
406 /// @name Helpers
407 // @{
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);
431 // @}
433 private:
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 */