00001 // -*- C++ -*- 00002 00003 //============================================================================= 00004 /** 00005 * @file CSD_TP_Strategy.h 00006 * 00007 * $Id: CSD_TP_Strategy.h 81882 2008-06-10 14:59:20Z johnc $ 00008 * 00009 * @author Tim Bradley <bradley_t@ociweb.com> 00010 */ 00011 //============================================================================= 00012 00013 #ifndef TAO_CSD_TP_STRATEGY_H 00014 #define TAO_CSD_TP_STRATEGY_H 00015 00016 #include /**/ "ace/pre.h" 00017 00018 #include "tao/CSD_ThreadPool/CSD_TP_Export.h" 00019 00020 #include "tao/CSD_ThreadPool/CSD_TP_Task.h" 00021 #include "tao/CSD_ThreadPool/CSD_TP_Servant_State_Map.h" 00022 00023 #if !defined (ACE_LACKS_PRAGMA_ONCE) 00024 # pragma once 00025 #endif /* ACE_LACKS_PRAGMA_ONCE */ 00026 00027 #include "tao/CSD_Framework/CSD_Strategy_Base.h" 00028 #include "tao/Intrusive_Ref_Count_Handle_T.h" 00029 00030 00031 TAO_BEGIN_VERSIONED_NAMESPACE_DECL 00032 00033 namespace TAO 00034 { 00035 namespace CSD 00036 { 00037 00038 class TP_Strategy; 00039 typedef TAO_Intrusive_Ref_Count_Handle<TP_Strategy> TP_Strategy_Handle; 00040 00041 class TP_Custom_Request_Operation; 00042 00043 /** 00044 * @class TP_Strategy 00045 * 00046 * @brief A simple custom Thread-Pool servant dispatching strategy class. 00047 * 00048 * This class represents a concrete implementation of a "Custom 00049 * Servant Dispatching Strategy". This implementation is being called 00050 * the "Thread Pool Strategy" reference implementation. 00051 * 00052 * A custom servant dispatching strategy object can be applied to a 00053 * POA object in order to carry out the servant dispatching duties 00054 * for that POA. 00055 * 00056 */ 00057 class TAO_CSD_TP_Export TP_Strategy 00058 : public Strategy_Base 00059 { 00060 public: 00061 00062 /// Constructor. 00063 TP_Strategy(Thread_Counter num_threads = 1, 00064 bool serialize_servants = true); 00065 00066 /// Virtual Destructor. 00067 virtual ~TP_Strategy(); 00068 00069 /// Set the number of threads in the pool (must be > 0). 00070 void set_num_threads(Thread_Counter num_threads); 00071 00072 /// Turn on/off serialization of servants. 00073 void set_servant_serialization(bool serialize_servants); 00074 00075 /// Return codes for the custom dispatch_request() methods. 00076 enum CustomRequestOutcome 00077 { 00078 /// The request was successfully put on the request queue. 00079 REQUEST_DISPATCHED, 00080 /// The request has been executed/completed by a worker thread. 00081 REQUEST_EXECUTED, 00082 /// The request was removed from the queue and cancelled. 00083 REQUEST_CANCELLED, 00084 /// The request queue rejected the request 00085 REQUEST_REJECTED 00086 }; 00087 00088 /// Inject a synchronous, custom request into the request queue. 00089 /// This will block the calling thread until the request is handled 00090 /// (dispatched or cancelled) or rejected. 00091 /// Will return REQUEST_EXECUTED, REQUEST_CANCELLED, or REQUEST_REJECTED. 00092 CustomRequestOutcome custom_synch_request 00093 (TP_Custom_Request_Operation* op); 00094 00095 /// Inject an asynchronous, custom request into the request queue. 00096 /// This will return control to the calling thread once the request 00097 /// has been placed into the queue (or rejected). 00098 /// Will return REQUEST_DISPATCHED or REQUEST_REJECTED. 00099 CustomRequestOutcome custom_asynch_request 00100 (TP_Custom_Request_Operation* op); 00101 00102 /// Cancel all requests that are targeted for the provided servant. 00103 /// This is requested on the user application level. 00104 void cancel_requests(PortableServer::Servant servant); 00105 00106 protected: 00107 00108 /// Handle the dispatching of a remote request. 00109 /// 00110 /// This will cause a new "request" object to be created and pushed 00111 /// on to a "request queue". The worker threads are responsible for 00112 /// servicing the queue, and performing the actual dispatch logic. 00113 virtual Strategy_Base::DispatchResult dispatch_remote_request_i 00114 (TAO_ServerRequest& server_request, 00115 const PortableServer::ObjectId& object_id, 00116 PortableServer::POA_ptr poa, 00117 const char* operation, 00118 PortableServer::Servant servant); 00119 00120 /// Handle the dispatching of a collocated request. 00121 /// 00122 /// This will cause a new "request" object to be created and pushed 00123 /// on to a "request queue". The worker threads are responsible for 00124 /// servicing the queue, and performing the actual dispatch logic. 00125 virtual Strategy_Base::DispatchResult dispatch_collocated_request_i 00126 (TAO_ServerRequest& server_request, 00127 const PortableServer::ObjectId& object_id, 00128 PortableServer::POA_ptr poa, 00129 const char* operation, 00130 PortableServer::Servant servant); 00131 00132 /// Event - The POA has been activated. 00133 /// This will activate the worker thread(s). 00134 /// Returns true if the worker threads were activated successfully. 00135 /// Otherwise, returns false. 00136 virtual bool poa_activated_event_i(TAO_ORB_Core& orb_core); 00137 00138 /// Event - The POA has been deactivated. 00139 /// This will shutdown the worker thread(s). 00140 virtual void poa_deactivated_event_i(); 00141 00142 /// Event - A servant has been activated 00143 virtual void servant_activated_event_i 00144 (PortableServer::Servant servant, 00145 const PortableServer::ObjectId& oid); 00146 00147 /// Event - A servant has been deactivated 00148 virtual void servant_deactivated_event_i 00149 (PortableServer::Servant servant, 00150 const PortableServer::ObjectId& oid); 00151 00152 private: 00153 00154 /** 00155 * Helper method that is responsible for looking up the servant 00156 * state object in the servant state map *if* the "serialize 00157 * servants" flag is set to true. In the case where the 00158 * "serialize servants" flag is set to false, then a "nil" 00159 * servant state handle object is returned. 00160 * 00161 * @param servant - input - a pointer to the servant object. 00162 * 00163 * @returns a handle to a servant state object. 00164 * 00165 * @throw PortableServer::POA::ServantNotActive if the servant 00166 * state cannot be determined. 00167 */ 00168 TP_Servant_State::HandleType get_servant_state 00169 (PortableServer::Servant servant); 00170 00171 00172 /// This is the active object used by the worker threads. 00173 /// The request queue is owned/managed by the task object. 00174 /// The strategy object puts requests into the task's request 00175 /// queue, and the worker threads service the queued requests 00176 /// by performing the actual servant request dispatching logic. 00177 TP_Task task_; 00178 00179 /// The number of worker threads to use for the task. 00180 Thread_Counter num_threads_; 00181 00182 /// The "serialize servants" flag. 00183 bool serialize_servants_; 00184 00185 /// The map of servant state objects - only used when the 00186 /// "serialize servants" flag is set to true. 00187 TP_Servant_State_Map servant_state_map_; 00188 }; 00189 00190 } 00191 } 00192 00193 TAO_END_VERSIONED_NAMESPACE_DECL 00194 00195 #if defined (__ACE_INLINE__) 00196 # include "tao/CSD_ThreadPool/CSD_TP_Strategy.inl" 00197 #endif /* __ACE_INLINE__ */ 00198 00199 #include /**/ "ace/post.h" 00200 00201 #endif /* TAO_CSD_TP_STRATEGY_H */