Thread_Pool.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 //=============================================================================
00004 /**
00005  *  @file    Thread_Pool.h
00006  *
00007  *  $Id: Thread_Pool.h 80216 2007-12-10 08:15:33Z johnnyw $
00008  *
00009  *  @author Irfan Pyarali
00010  *  @author Johnny Willemsen
00011  */
00012 // ===================================================================
00013 
00014 #ifndef TAO_THREAD_POOL_H
00015 #define TAO_THREAD_POOL_H
00016 
00017 #include /**/ "ace/pre.h"
00018 #include "tao/orbconf.h"
00019 
00020 #if defined (TAO_HAS_CORBA_MESSAGING) && TAO_HAS_CORBA_MESSAGING != 0
00021 
00022 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00023 # pragma once
00024 #endif /* ACE_LACKS_PRAGMA_ONCE */
00025 
00026 #include "tao/RTCORBA/RTCORBA_includeC.h"
00027 #include "tao/RTCORBA/RT_ORBInitializer.h"
00028 #include "ace/Hash_Map_Manager.h"
00029 #include "tao/Thread_Lane_Resources.h"
00030 #include "tao/New_Leader_Generator.h"
00031 #include "ace/Task.h"
00032 #include "ace/Null_Mutex.h"
00033 
00034 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00035 
00036 class TAO_Thread_Lane;
00037 
00038 /**
00039  * @class TAO_RT_New_Leader_Generator
00040  *
00041  * @brief Class for creating dynamic threads.
00042  *
00043  * \nosubgrouping
00044  *
00045  **/
00046 class TAO_RTCORBA_Export TAO_RT_New_Leader_Generator
00047   : public TAO_New_Leader_Generator
00048 {
00049 public:
00050 
00051   /// Constructor.
00052   TAO_RT_New_Leader_Generator (TAO_Thread_Lane &lane);
00053 
00054   /// Leader/Follower class uses this method to notify the system that
00055   /// we are out of leaders.
00056   void no_leaders_available (void);
00057 
00058 private:
00059 
00060   /// Lane associated with this leader generator.
00061   TAO_Thread_Lane &lane_;
00062 };
00063 
00064 /**
00065  * @class TAO_Thread_Pool_Threads
00066  *
00067  * @brief Class representing a static thread running in a thread lane.
00068  *
00069  * \nosubgrouping
00070  *
00071  **/
00072 class TAO_Thread_Pool_Threads : public ACE_Task_Base
00073 {
00074 public:
00075 
00076   /// Constructor.
00077   TAO_Thread_Pool_Threads (TAO_Thread_Lane &lane);
00078 
00079   /// Method executed when a thread is spawned.
00080   int svc (void);
00081 
00082   /// Accessor to the lane to which this thread belongs to.
00083   TAO_Thread_Lane &lane (void) const;
00084 
00085   /// Set TSS resources for the current thread.
00086   static void set_tss_resources (TAO_ORB_Core &orb_core,
00087                                  TAO_Thread_Lane &thread_lane);
00088 
00089 protected:
00090   /// Do the real work
00091   virtual int run (TAO_ORB_Core &orb_core);
00092 
00093   /// Lane to which this thread belongs to.
00094   TAO_Thread_Lane &lane_;
00095 };
00096 
00097 /**
00098  * @class TAO_Dynamic_Thread_Pool_Threads
00099  *
00100  * @brief Class representing a dynamic thread running in a thread lane.
00101  *
00102  * \nosubgrouping
00103  *
00104  **/
00105 class TAO_Dynamic_Thread_Pool_Threads : public TAO_Thread_Pool_Threads
00106 {
00107 public:
00108   /// Constructor.
00109   TAO_Dynamic_Thread_Pool_Threads (TAO_Thread_Lane &lane);
00110 
00111 protected:
00112   /// Do the real work
00113   virtual int run (TAO_ORB_Core &orb_core);
00114 };
00115 
00116 class TAO_Thread_Pool;
00117 
00118 /**
00119  * @class TAO_Thread_Lane
00120  *
00121  * @brief Class representing the thread lane inside a thread pool.
00122  *
00123  * \nosubgrouping
00124  *
00125  **/
00126 class TAO_RTCORBA_Export TAO_Thread_Lane
00127 {
00128 public:
00129   /// Constructor.
00130   TAO_Thread_Lane (TAO_Thread_Pool &pool,
00131                    CORBA::ULong id,
00132                    CORBA::Short lane_priority,
00133                    CORBA::ULong static_threads,
00134                    CORBA::ULong dynamic_threads,
00135                    TAO_RT_ORBInitializer::TAO_RTCORBA_DT_LifeSpan lifespan,
00136                    ACE_Time_Value const &dynamic_thread_time);
00137 
00138   /// Destructor.
00139   ~TAO_Thread_Lane (void);
00140 
00141   /// Open the lane.
00142   void open (void);
00143 
00144   /// Finalize the resources.
00145   void finalize (void);
00146 
00147   /// Shutdown the reactor.
00148   void shutdown_reactor (void);
00149 
00150   /// Wait for threads to exit.
00151   void wait (void);
00152 
00153   /// Does @a mprofile belong to us?
00154   int is_collocated (const TAO_MProfile &mprofile);
00155 
00156   /// Create the static threads - only called once.
00157   int create_static_threads (void);
00158 
00159   /// Mark that this lane is shutting down, we then don't create any
00160   /// dynamic threads anymore. When the pool is shutting down the leader
00161   /// follower loop is called which can cause a request to create a
00162   /// new dynamic thread but we shouldn't create a new one.
00163   void shutting_down (void);
00164 
00165   /// Called by the TAO_RT_New_Leader_Generator to request a new dynamic
00166   /// thread.
00167   /**
00168    * It can be that no thread can be created because the number of
00169    * threads is equal to the maximum we can have or the Thread Lane
00170    * is shutting down.
00171    * @retval true A new thread is created
00172    * @retval false No thread could be created
00173    */
00174   bool new_dynamic_thread (void);
00175 
00176   /// @name Accessors
00177   // @{
00178   TAO_Thread_Pool &pool (void) const;
00179   CORBA::ULong id (void) const;
00180 
00181   CORBA::Short lane_priority (void) const;
00182   CORBA::ULong static_threads (void) const;
00183   CORBA::ULong dynamic_threads (void) const;
00184 
00185   CORBA::ULong current_threads (void) const;
00186 
00187   CORBA::Short native_priority (void) const;
00188 
00189   TAO_Thread_Lane_Resources &resources (void);
00190 
00191   TAO_RT_ORBInitializer::TAO_RTCORBA_DT_LifeSpan lifespan (void) const;
00192 
00193   ACE_Time_Value const &dynamic_thread_time (void) const;
00194   // @}
00195 
00196 private:
00197 
00198   /// Validate lane's priority and map it to a native value.
00199   void validate_and_map_priority (void);
00200 
00201   int create_threads_i (TAO_Thread_Pool_Threads &thread_pool,
00202                         CORBA::ULong number_of_threads,
00203                         long thread_flags);
00204 
00205   /// Create @a number_of_threads of dynamic threads.  Can be called
00206   /// multiple times.
00207   int create_dynamic_threads (CORBA::ULong number_of_threads);
00208 
00209   /// The Thread Pool to which this lane belongs.
00210   TAO_Thread_Pool &pool_;
00211 
00212   /// The id of this lane
00213   CORBA::ULong const id_;
00214 
00215   CORBA::Short lane_priority_;
00216 
00217   /// This boolean is set when we are shutting down, then we will not create
00218   /// any new dynamic threads
00219   bool shutdown_;
00220 
00221   /// Number of static threads
00222   CORBA::ULong const static_threads_number_;
00223 
00224   /// Maximum number of threads we are allowed to create
00225   CORBA::ULong const dynamic_threads_number_;
00226 
00227   /// Array with all static threads
00228   TAO_Thread_Pool_Threads static_threads_;
00229 
00230   /// Array with all dynamic threads
00231   TAO_Dynamic_Thread_Pool_Threads dynamic_threads_;
00232 
00233   TAO_RT_New_Leader_Generator new_thread_generator_;
00234 
00235   TAO_Thread_Lane_Resources resources_;
00236 
00237   CORBA::Short native_priority_;
00238 
00239   TAO_RT_ORBInitializer::TAO_RTCORBA_DT_LifeSpan const lifespan_;
00240 
00241   ACE_Time_Value const dynamic_thread_time_;
00242 
00243   /// Lock to guard all members of the lane
00244   mutable TAO_SYNCH_MUTEX lock_;
00245 };
00246 
00247 class TAO_Thread_Pool_Manager;
00248 
00249 /**
00250  * @class TAO_Thread_Pool
00251  *
00252  * @brief Class representing the thread pool inside a thread pool
00253  * manager.
00254  *
00255  * \nosubgrouping
00256  *
00257  **/
00258 class TAO_RTCORBA_Export TAO_Thread_Pool
00259 {
00260 public:
00261 
00262   /// Constructor (for pools without lanes).
00263   TAO_Thread_Pool (TAO_Thread_Pool_Manager &manager,
00264                    CORBA::ULong id,
00265                    CORBA::ULong stack_size,
00266                    CORBA::ULong static_threads,
00267                    CORBA::ULong dynamic_threads,
00268                    CORBA::Short default_priority,
00269                    CORBA::Boolean allow_request_buffering,
00270                    CORBA::ULong max_buffered_requests,
00271                    CORBA::ULong max_request_buffer_size,
00272                    TAO_RT_ORBInitializer::TAO_RTCORBA_DT_LifeSpan lifespan,
00273                    ACE_Time_Value const &dynamic_thread_time);
00274 
00275   /// Constructor (for pools with lanes).
00276   TAO_Thread_Pool (TAO_Thread_Pool_Manager &manager,
00277                    CORBA::ULong id,
00278                    CORBA::ULong stack_size,
00279                    const RTCORBA::ThreadpoolLanes &lanes,
00280                    CORBA::Boolean allow_borrowing,
00281                    CORBA::Boolean allow_request_buffering,
00282                    CORBA::ULong max_buffered_requests,
00283                    CORBA::ULong max_request_buffer_size,
00284                    TAO_RT_ORBInitializer::TAO_RTCORBA_DT_LifeSpan lifespan,
00285                    ACE_Time_Value const &dynamic_thread_time);
00286 
00287   /// Destructor.
00288   ~TAO_Thread_Pool (void);
00289 
00290   /// Open the pool.
00291   void open (void);
00292 
00293   /// Finalize the resources.
00294   void finalize (void);
00295 
00296   /// Shutdown the reactor.
00297   void shutdown_reactor (void);
00298 
00299   /// Wait for threads to exit.
00300   void wait (void);
00301 
00302   /// Mark this thread pool that we are shutting down.
00303   void shutting_down (void);
00304 
00305   /// Does @a mprofile belong to us?
00306   int is_collocated (const TAO_MProfile &mprofile);
00307 
00308   /// Create the static threads - only called once.
00309   int create_static_threads (void);
00310 
00311   /// Check if this thread pool has (explicit) lanes.
00312   bool with_lanes (void) const;
00313 
00314   /// @name Accessors
00315   // @{
00316 
00317   TAO_Thread_Pool_Manager &manager (void) const;
00318   CORBA::ULong id (void) const;
00319 
00320   CORBA::ULong stack_size (void) const;
00321   CORBA::Boolean allow_borrowing (void) const;
00322   CORBA::Boolean allow_request_buffering (void) const;
00323   CORBA::ULong max_buffered_requests (void) const;
00324   CORBA::ULong max_request_buffer_size (void) const;
00325 
00326   TAO_Thread_Lane **lanes (void);
00327   CORBA::ULong number_of_lanes (void) const;
00328   // @}
00329 
00330 private:
00331 
00332   TAO_Thread_Pool_Manager &manager_;
00333   CORBA::ULong id_;
00334 
00335   CORBA::ULong stack_size_;
00336   CORBA::Boolean allow_borrowing_;
00337   CORBA::Boolean allow_request_buffering_;
00338   CORBA::ULong max_buffered_requests_;
00339   CORBA::ULong max_request_buffer_size_;
00340   TAO_RT_ORBInitializer::TAO_RTCORBA_DT_LifeSpan const lifespan_;
00341   ACE_Time_Value const dynamic_thread_time_;
00342 
00343   TAO_Thread_Lane **lanes_;
00344   CORBA::ULong number_of_lanes_;
00345   bool with_lanes_;
00346 };
00347 
00348 class TAO_ORB_Core;
00349 
00350 /**
00351  * @class TAO_Thread_Pool_Manager
00352  *
00353  * @brief Class for managing thread pools.
00354  *
00355  * \nosubgrouping
00356  *
00357  **/
00358 class TAO_RTCORBA_Export TAO_Thread_Pool_Manager
00359 {
00360 public:
00361 
00362   /// Constructor.
00363   TAO_Thread_Pool_Manager (TAO_ORB_Core &orb_core);
00364 
00365   /// Destructor.
00366   ~TAO_Thread_Pool_Manager (void);
00367 
00368   /// Finalize the resources.
00369   void finalize (void);
00370 
00371   /// Shutdown the reactor.
00372   void shutdown_reactor (void);
00373 
00374   /// Wait for threads to exit.
00375   void wait (void);
00376 
00377   /// Does @a mprofile belong to us?
00378   int is_collocated (const TAO_MProfile &mprofile);
00379 
00380   /// Create a threadpool without lanes.
00381   RTCORBA::ThreadpoolId
00382   create_threadpool (CORBA::ULong stacksize,
00383                      CORBA::ULong static_threads,
00384                      CORBA::ULong dynamic_threads,
00385                      RTCORBA::Priority default_priority,
00386                      CORBA::Boolean allow_request_buffering,
00387                      CORBA::ULong max_buffered_requests,
00388                      CORBA::ULong max_request_buffer_size,
00389                      TAO_RT_ORBInitializer::TAO_RTCORBA_DT_LifeSpan lifespan,
00390                      ACE_Time_Value const &dynamic_thread_time);
00391 
00392   /// Create a threadpool with lanes.
00393   RTCORBA::ThreadpoolId
00394   create_threadpool_with_lanes (CORBA::ULong stacksize,
00395                                 const RTCORBA::ThreadpoolLanes & lanes,
00396                                 CORBA::Boolean allow_borrowing,
00397                                 CORBA::Boolean allow_request_buffering,
00398                                 CORBA::ULong max_buffered_requests,
00399                                 CORBA::ULong max_request_buffer_size,
00400                                 TAO_RT_ORBInitializer::TAO_RTCORBA_DT_LifeSpan lifespan,
00401                                 ACE_Time_Value const &dynamic_thread_time);
00402 
00403   /// Destroy a threadpool.
00404   void destroy_threadpool (RTCORBA::ThreadpoolId threadpool);
00405 
00406   TAO_Thread_Pool *get_threadpool (RTCORBA::ThreadpoolId thread_pool_id);
00407 
00408   /// Collection of thread pools.
00409   typedef ACE_Hash_Map_Manager<RTCORBA::ThreadpoolId, TAO_Thread_Pool *, ACE_Null_Mutex> THREAD_POOLS;
00410 
00411   /// @name Accessors
00412   // @{
00413   TAO_ORB_Core &orb_core (void) const;
00414   // @}
00415 
00416 private:
00417 
00418   /// @name Helpers
00419   // @{
00420 
00421   RTCORBA::ThreadpoolId
00422   create_threadpool_i (CORBA::ULong stacksize,
00423                        CORBA::ULong static_threads,
00424                        CORBA::ULong dynamic_threads,
00425                        RTCORBA::Priority default_priority,
00426                        CORBA::Boolean allow_request_buffering,
00427                        CORBA::ULong max_buffered_requests,
00428                        CORBA::ULong max_request_buffer_size,
00429                        TAO_RT_ORBInitializer::TAO_RTCORBA_DT_LifeSpan lifespan,
00430                        ACE_Time_Value const &dynamic_thread_time);
00431 
00432   RTCORBA::ThreadpoolId
00433   create_threadpool_with_lanes_i (CORBA::ULong stacksize,
00434                                   const RTCORBA::ThreadpoolLanes & lanes,
00435                                   CORBA::Boolean allow_borrowing,
00436                                   CORBA::Boolean allow_request_buffering,
00437                                   CORBA::ULong max_buffered_requests,
00438                                   CORBA::ULong max_request_buffer_size,
00439                                   TAO_RT_ORBInitializer::TAO_RTCORBA_DT_LifeSpan lifespan,
00440                                   ACE_Time_Value const &dynamic_thread_time);
00441 
00442   RTCORBA::ThreadpoolId
00443   create_threadpool_helper (TAO_Thread_Pool *thread_pool);
00444   // @}
00445 
00446 private:
00447 
00448   TAO_ORB_Core &orb_core_;
00449 
00450   THREAD_POOLS thread_pools_;
00451   RTCORBA::ThreadpoolId thread_pool_id_counter_;
00452   TAO_SYNCH_MUTEX lock_;
00453 };
00454 
00455 TAO_END_VERSIONED_NAMESPACE_DECL
00456 
00457 #if defined (__ACE_INLINE__)
00458 #include "tao/RTCORBA/Thread_Pool.inl"
00459 #endif /* __ACE_INLINE__ */
00460 
00461 #endif /* TAO_HAS_CORBA_MESSAGING && TAO_HAS_CORBA_MESSAGING != 0 */
00462 
00463 #include /**/ "ace/post.h"
00464 
00465 #endif /* TAO_THREAD_POOL_H */

Generated on Tue Feb 2 17:42:49 2010 for TAO_RTCORBA by  doxygen 1.4.7