Thread_Pool.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 //=============================================================================
00004 /**
00005  *  @file    Thread_Pool.h
00006  *
00007  *  Thread_Pool.h,v 1.18 2006/03/10 07:19:16 jtc Exp
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 #define TAO_RTCORBA_SAFE_INCLUDE
00027 #include "tao/RTCORBA/RTCORBAC.h"
00028 #undef TAO_RTCORBA_SAFE_INCLUDE
00029 
00030 #include "ace/Hash_Map_Manager.h"
00031 #include "tao/Thread_Lane_Resources.h"
00032 #include "tao/New_Leader_Generator.h"
00033 #include "ace/Task.h"
00034 #include "ace/Null_Mutex.h"
00035 
00036 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00037 
00038 class TAO_Thread_Lane;
00039 
00040 /**
00041  * @class TAO_RT_New_Leader_Generator
00042  *
00043  * @brief Class for creating dynamic threads.
00044  *
00045  * \nosubgrouping
00046  *
00047  **/
00048 class TAO_RTCORBA_Export TAO_RT_New_Leader_Generator
00049   : public TAO_New_Leader_Generator
00050 {
00051 public:
00052 
00053   /// Constructor.
00054   TAO_RT_New_Leader_Generator (TAO_Thread_Lane &lane);
00055 
00056   /// Leader/Follower class uses this method to notify the system that
00057   /// we are out of leaders.
00058   void no_leaders_available (void);
00059 
00060 private:
00061 
00062   /// Lane associated with this leader generator.
00063   TAO_Thread_Lane &lane_;
00064 };
00065 
00066 /**
00067  * @class TAO_Thread_Pool_Threads
00068  *
00069  * @brief Class representing a static thread running in a thread lane.
00070  *
00071  * \nosubgrouping
00072  *
00073  **/
00074 class TAO_Thread_Pool_Threads : public ACE_Task_Base
00075 {
00076 public:
00077 
00078   /// Constructor.
00079   TAO_Thread_Pool_Threads (TAO_Thread_Lane &lane);
00080 
00081   /// Method executed when a thread is spawned.
00082   int svc (void);
00083 
00084   /// Accessor to the lane to which this thread belongs to.
00085   TAO_Thread_Lane &lane (void) const;
00086 
00087   /// Set TSS resources for the current thread.
00088   static void set_tss_resources (TAO_ORB_Core &orb_core,
00089                                  TAO_Thread_Lane &thread_lane);
00090 
00091 protected:
00092   /// Do the real work
00093   virtual int run (TAO_ORB_Core &orb_core ACE_ENV_ARG_PARAMETER);
00094 
00095   /// Lane to which this thread belongs to.
00096   TAO_Thread_Lane &lane_;
00097 };
00098 
00099 /**
00100  * @class TAO_Dynamic_Thread_Pool_Threads
00101  *
00102  * @brief Class representing a dynamic thread running in a thread lane.
00103  *
00104  * \nosubgrouping
00105  *
00106  **/
00107 class TAO_Dynamic_Thread_Pool_Threads : public TAO_Thread_Pool_Threads
00108 {
00109 public:
00110   /// Constructor.
00111   TAO_Dynamic_Thread_Pool_Threads (TAO_Thread_Lane &lane);
00112 
00113 protected:
00114   /// Do the real work
00115   virtual int run (TAO_ORB_Core &orb_core ACE_ENV_ARG_PARAMETER);
00116 };
00117 
00118 class TAO_Thread_Pool;
00119 
00120 /**
00121  * @class TAO_Thread_Lane
00122  *
00123  * @brief Class representing the thread lane inside a thread pool.
00124  *
00125  * \nosubgrouping
00126  *
00127  **/
00128 class TAO_RTCORBA_Export TAO_Thread_Lane
00129 {
00130 public:
00131 
00132   /// Constructor.
00133   TAO_Thread_Lane (TAO_Thread_Pool &pool,
00134                    CORBA::ULong id,
00135                    CORBA::Short lane_priority,
00136                    CORBA::ULong static_threads,
00137                    CORBA::ULong dynamic_threads,
00138                    ACE_Time_Value const &dynamic_thread_idle_timeout
00139                    ACE_ENV_ARG_DECL);
00140 
00141   /// Destructor.
00142   ~TAO_Thread_Lane (void);
00143 
00144   /// Open the lane.
00145   void open (ACE_ENV_SINGLE_ARG_DECL);
00146 
00147   /// Finalize the resources.
00148   void finalize (void);
00149 
00150   /// Shutdown the reactor.
00151   void shutdown_reactor (void);
00152 
00153   /// Wait for threads to exit.
00154   void wait (void);
00155 
00156   /// Does @a mprofile belong to us?
00157   int is_collocated (const TAO_MProfile &mprofile);
00158 
00159   /// Create the static threads - only called once.
00160   int create_static_threads (void);
00161 
00162   /// Mark that this lane is shutting down, we then don't create any
00163   /// dynamic threads anymore. When the pool is shutting down the leader
00164   /// follower loop is called which can cause a request to create a
00165   /// new dynamic thread but we shouldn't create a new one.
00166   void shutting_down (void);
00167 
00168   /// Called by the TAO_RT_New_Leader_Generator to request a new dynamic
00169   /// thread.
00170   /**
00171    * It can be that no thread can be created because the number of
00172    * threads is equal to the maximum we can have or the Thread Lane
00173    * is shutting down.
00174    * @retval true A new thread is created
00175    * @retval false No thread could be created
00176    */
00177   bool new_dynamic_thread (void);
00178 
00179   /// @name Accessors
00180   // @{
00181 
00182   TAO_Thread_Pool &pool (void) const;
00183   CORBA::ULong id (void) const;
00184 
00185   CORBA::Short lane_priority (void) const;
00186   CORBA::ULong static_threads (void) const;
00187   CORBA::ULong dynamic_threads (void) const;
00188 
00189   CORBA::ULong current_threads (void) const;
00190 
00191   CORBA::Short native_priority (void) const;
00192 
00193   TAO_Thread_Lane_Resources &resources (void);
00194 
00195   ACE_Time_Value const &dynamic_thread_idle_timeout (void) const;
00196   // @}
00197 
00198 private:
00199 
00200   /// Validate lane's priority and map it to a native value.
00201   void validate_and_map_priority (ACE_ENV_SINGLE_ARG_DECL);
00202 
00203   int create_threads_i (TAO_Thread_Pool_Threads &thread_pool,
00204                         CORBA::ULong number_of_threads,
00205                         long thread_flags);
00206 
00207   /// Create @a number_of_threads of dynamic threads.  Can be called
00208   /// multiple times.
00209   int create_dynamic_threads (CORBA::ULong number_of_threads);
00210 
00211   /// The Thread Pool to which this lane belongs.
00212   TAO_Thread_Pool &pool_;
00213 
00214   /// The id of this lane
00215   CORBA::ULong const id_;
00216 
00217   CORBA::Short lane_priority_;
00218 
00219   /// This boolean is set when we are shutting down, then we will not create
00220   /// any new dynamic threads
00221   bool shutdown_;
00222 
00223   /// Number of static threads
00224   CORBA::ULong const static_threads_number_;
00225 
00226   /// Maximum number of threads we are allowed to create
00227   CORBA::ULong const dynamic_threads_number_;
00228 
00229   /// Array with all static threads
00230   TAO_Thread_Pool_Threads static_threads_;
00231 
00232   /// Array with all dynamic threads
00233   TAO_Dynamic_Thread_Pool_Threads dynamic_threads_;
00234 
00235   TAO_RT_New_Leader_Generator new_thread_generator_;
00236 
00237   TAO_Thread_Lane_Resources resources_;
00238 
00239   CORBA::Short native_priority_;
00240 
00241   ACE_Time_Value const dynamic_thread_idle_timeout_;
00242 
00243   /// Lock to guard all members of the lane
00244   mutable ACE_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                    ACE_Time_Value const &dynamic_thread_idle_timeout
00273                    ACE_ENV_ARG_DECL);
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                    ACE_Time_Value const &dynamic_thread_idle_timeout
00285                    ACE_ENV_ARG_DECL);
00286 
00287   /// Destructor.
00288   ~TAO_Thread_Pool (void);
00289 
00290   /// Open the pool.
00291   void open (ACE_ENV_SINGLE_ARG_DECL);
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 
00331 private:
00332 
00333   TAO_Thread_Pool_Manager &manager_;
00334   CORBA::ULong id_;
00335 
00336   CORBA::ULong stack_size_;
00337   CORBA::Boolean allow_borrowing_;
00338   CORBA::Boolean allow_request_buffering_;
00339   CORBA::ULong max_buffered_requests_;
00340   CORBA::ULong max_request_buffer_size_;
00341   ACE_Time_Value const dynamic_thread_idle_timeout_;
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                      ACE_Time_Value const &dynamic_thread_idle_timeout
00390                      ACE_ENV_ARG_DECL)
00391     ACE_THROW_SPEC ((CORBA::SystemException));
00392 
00393   /// Create a threadpool with lanes.
00394   RTCORBA::ThreadpoolId
00395   create_threadpool_with_lanes (CORBA::ULong stacksize,
00396                                 const RTCORBA::ThreadpoolLanes & lanes,
00397                                 CORBA::Boolean allow_borrowing,
00398                                 CORBA::Boolean allow_request_buffering,
00399                                 CORBA::ULong max_buffered_requests,
00400                                 CORBA::ULong max_request_buffer_size,
00401                                 ACE_Time_Value const &dynamic_thread_idle_timeout
00402                                 ACE_ENV_ARG_DECL)
00403     ACE_THROW_SPEC ((CORBA::SystemException));
00404 
00405   /// Destroy a threadpool.
00406   void destroy_threadpool (RTCORBA::ThreadpoolId threadpool
00407                            ACE_ENV_ARG_DECL)
00408     ACE_THROW_SPEC ((CORBA::SystemException,
00409                      RTCORBA::RTORB::InvalidThreadpool));
00410 
00411   TAO_Thread_Pool *get_threadpool (RTCORBA::ThreadpoolId thread_pool_id ACE_ENV_ARG_DECL);
00412 
00413   /// Collection of thread pools.
00414   typedef ACE_Hash_Map_Manager<RTCORBA::ThreadpoolId, TAO_Thread_Pool *, ACE_Null_Mutex> THREAD_POOLS;
00415 
00416   /// @name Accessors
00417   // @{
00418 
00419   TAO_ORB_Core &orb_core (void) const;
00420 
00421   // @}
00422 
00423 private:
00424 
00425   /// @name Helpers
00426   // @{
00427 
00428   RTCORBA::ThreadpoolId
00429   create_threadpool_i (CORBA::ULong stacksize,
00430                        CORBA::ULong static_threads,
00431                        CORBA::ULong dynamic_threads,
00432                        RTCORBA::Priority default_priority,
00433                        CORBA::Boolean allow_request_buffering,
00434                        CORBA::ULong max_buffered_requests,
00435                        CORBA::ULong max_request_buffer_size,
00436                        ACE_Time_Value const &dynamic_thread_idle_timeout
00437                        ACE_ENV_ARG_DECL)
00438     ACE_THROW_SPEC ((CORBA::SystemException));
00439 
00440   RTCORBA::ThreadpoolId
00441   create_threadpool_with_lanes_i (CORBA::ULong stacksize,
00442                                   const RTCORBA::ThreadpoolLanes & lanes,
00443                                   CORBA::Boolean allow_borrowing,
00444                                   CORBA::Boolean allow_request_buffering,
00445                                   CORBA::ULong max_buffered_requests,
00446                                   CORBA::ULong max_request_buffer_size,
00447                                   ACE_Time_Value const &dynamic_thread_idle_timeout
00448                                   ACE_ENV_ARG_DECL)
00449     ACE_THROW_SPEC ((CORBA::SystemException));
00450 
00451   RTCORBA::ThreadpoolId
00452   create_threadpool_helper (TAO_Thread_Pool *thread_pool
00453                             ACE_ENV_ARG_DECL)
00454     ACE_THROW_SPEC ((CORBA::SystemException));
00455 
00456   // @}
00457 
00458 private:
00459 
00460   TAO_ORB_Core &orb_core_;
00461 
00462   THREAD_POOLS thread_pools_;
00463   RTCORBA::ThreadpoolId thread_pool_id_counter_;
00464   ACE_SYNCH_MUTEX lock_;
00465 };
00466 
00467 TAO_END_VERSIONED_NAMESPACE_DECL
00468 
00469 #if defined (__ACE_INLINE__)
00470 #include "tao/RTCORBA/Thread_Pool.inl"
00471 #endif /* __ACE_INLINE__ */
00472 
00473 #endif /* TAO_HAS_CORBA_MESSAGING && TAO_HAS_CORBA_MESSAGING != 0 */
00474 
00475 #include /**/ "ace/post.h"
00476 
00477 #endif /* TAO_THREAD_POOL_H */

Generated on Thu Nov 9 12:58:07 2006 for TAO_RTCORBA by doxygen 1.3.6