00001 // -*- C++ -*- 00002 00003 //============================================================================= 00004 /** 00005 * @file Reactor_Impl.h 00006 * 00007 * Reactor_Impl.h,v 4.51 2006/02/10 10:17:22 jwillemsen Exp 00008 * 00009 * @author Irfan Pyarali 00010 */ 00011 //============================================================================= 00012 00013 00014 #ifndef ACE_REACTOR_IMPL_H 00015 #define ACE_REACTOR_IMPL_H 00016 #include /**/ "ace/pre.h" 00017 00018 // Timer Queue is a complicated template class. A simple forward 00019 // declaration will not work 00020 #include "ace/Timer_Queuefwd.h" 00021 00022 #if !defined (ACE_LACKS_PRAGMA_ONCE) 00023 # pragma once 00024 #endif /* ACE_LACKS_PRAGMA_ONCE */ 00025 00026 // Event_Handler.h contains the definition of ACE_Reactor_Mask 00027 #include "ace/Event_Handler.h" 00028 #include "ace/Countdown_Time.h" 00029 00030 ACE_BEGIN_VERSIONED_NAMESPACE_DECL 00031 00032 // Forward decls 00033 class ACE_Handle_Set; 00034 class ACE_Reactor_Impl; 00035 class ACE_Sig_Action; 00036 class ACE_Sig_Handler; 00037 class ACE_Sig_Set; 00038 00039 /** 00040 * @class ACE_Reactor_Notify 00041 * 00042 * @internal This class is for ACE internal use only. 00043 * 00044 * @brief Abstract class for unblocking an <ACE_Reactor_Impl> from its 00045 * event loop. 00046 */ 00047 class ACE_Export ACE_Reactor_Notify : public ACE_Event_Handler 00048 { 00049 public: 00050 // = Initialization and termination methods. 00051 virtual int open (ACE_Reactor_Impl *, 00052 ACE_Timer_Queue *timer_queue = 0, 00053 int disable_notify = 0) = 0; 00054 virtual int close (void) = 0; 00055 00056 /** 00057 * Called by a thread when it wants to unblock the <Reactor_Impl>. 00058 * This wakeups the <Reactor_Impl> if currently blocked. Pass over 00059 * both the <Event_Handler> *and* the <mask> to allow the caller to 00060 * dictate which <Event_Handler> method the <Reactor_Impl> will 00061 * invoke. The <ACE_Time_Value> indicates how long to blocking 00062 * trying to notify the <Reactor_Impl>. If <timeout> == 0, the 00063 * caller will block until action is possible, else will wait until 00064 * the relative time specified in *<timeout> elapses). 00065 */ 00066 virtual int notify (ACE_Event_Handler *eh = 0, 00067 ACE_Reactor_Mask mask = ACE_Event_Handler::EXCEPT_MASK, 00068 ACE_Time_Value *timeout = 0) = 0; 00069 00070 /// Handles pending threads (if any) that are waiting to unblock the 00071 /// <Reactor_Impl>. 00072 virtual int dispatch_notifications (int &number_of_active_handles, 00073 ACE_Handle_Set &rd_mask) = 0; 00074 00075 /// Returns the ACE_HANDLE of the notify pipe on which the reactor 00076 /// is listening for notifications so that other threads can unblock 00077 /// the <Reactor_Impl> 00078 virtual ACE_HANDLE notify_handle (void) = 0; 00079 00080 /// Verify whether the buffer has dispatchable info or not. 00081 virtual int is_dispatchable (ACE_Notification_Buffer &buffer)= 0; 00082 00083 /// Handle one of the notify call on the <handle>. This could be 00084 /// because of a thread trying to unblock the <Reactor_Impl> 00085 virtual int dispatch_notify (ACE_Notification_Buffer &buffer) = 0; 00086 00087 /// Read one of the notify call on the <handle> into the 00088 /// <buffer>. This could be because of a thread trying to unblock 00089 /// the <Reactor_Impl> 00090 virtual int read_notify_pipe (ACE_HANDLE handle, 00091 ACE_Notification_Buffer &buffer) = 0; 00092 /** 00093 * Set the maximum number of times that the <handle_input> method 00094 * will iterate and dispatch the <ACE_Event_Handlers> that are 00095 * passed in via the notify queue before breaking out of the event 00096 * loop. By default, this is set to -1, which means "iterate until 00097 * the queue is empty." Setting this to a value like "1 or 2" will 00098 * increase "fairness" (and thus prevent starvation) at the expense 00099 * of slightly higher dispatching overhead. 00100 */ 00101 virtual void max_notify_iterations (int) = 0; 00102 00103 /** 00104 * Get the maximum number of times that the <handle_input> method 00105 * will iterate and dispatch the <ACE_Event_Handlers> that are 00106 * passed in via the notify queue before breaking out of its event 00107 * loop. 00108 */ 00109 virtual int max_notify_iterations (void) = 0; 00110 00111 /** 00112 * Purge any notifications pending in this reactor for the specified 00113 * ACE_Event_Handler object. Returns the number of notifications 00114 * purged. Returns -1 on error. 00115 */ 00116 virtual int purge_pending_notifications (ACE_Event_Handler * = 0, 00117 ACE_Reactor_Mask = ACE_Event_Handler::ALL_EVENTS_MASK) = 0; 00118 00119 /// Dump the state of an object. 00120 virtual void dump (void) const = 0; 00121 }; 00122 00123 /** 00124 * @class ACE_Reactor_Impl 00125 * 00126 * @brief An abstract class for implementing the Reactor Pattern. 00127 */ 00128 class ACE_Export ACE_Reactor_Impl 00129 { 00130 public: 00131 /// Close down and release all resources. 00132 virtual ~ACE_Reactor_Impl (void); 00133 00134 /// Initialization. 00135 virtual int open (size_t size, 00136 int restart = 0, 00137 ACE_Sig_Handler * = 0, 00138 ACE_Timer_Queue * = 0, 00139 int disable_notify_pipe = 0, 00140 ACE_Reactor_Notify * = 0) = 0; 00141 00142 /** 00143 * Returns 0, if the size of the current message has been put in 00144 * <size> Returns -1, if not. ACE_HANDLE allows the reactor to 00145 * check if the caller is valid. 00146 */ 00147 virtual int current_info (ACE_HANDLE, size_t & /* size */) = 0; 00148 00149 /// Use a user specified signal handler instead. 00150 virtual int set_sig_handler (ACE_Sig_Handler *signal_handler) = 0; 00151 00152 /// Set a user-specified timer queue. 00153 virtual int timer_queue (ACE_Timer_Queue *tq) = 0; 00154 00155 /// Return the current ACE_Timer_Queue. 00156 virtual ACE_Timer_Queue *timer_queue (void) const = 0; 00157 00158 /// Close down and release all resources. 00159 virtual int close (void) = 0; 00160 00161 // = Event loop drivers. 00162 /** 00163 * Returns non-zero if there are I/O events "ready" for dispatching, 00164 * but does not actually dispatch the event handlers. By default, 00165 * don't block while checking this, i.e., "poll". 00166 */ 00167 virtual int work_pending (const ACE_Time_Value &max_wait_time = ACE_Time_Value::zero) = 0; 00168 00169 /** 00170 * This event loop driver blocks for up to <max_wait_time> before 00171 * returning. It will return earlier if events occur. Note that 00172 * <max_wait_time> can be 0, in which case this method blocks 00173 * indefinitely until events occur. 00174 * 00175 * <max_wait_time> is decremented to reflect how much time this call 00176 * took. For instance, if a time value of 3 seconds is passed to 00177 * handle_events and an event occurs after 2 seconds, 00178 * <max_wait_time> will equal 1 second. This can be used if an 00179 * application wishes to handle events for some fixed amount of 00180 * time. 00181 * 00182 * Returns the total number of ACE_Event_Handlers that were 00183 * dispatched, 0 if the <max_wait_time> elapsed without dispatching 00184 * any handlers, or -1 if an error occurs. 00185 * 00186 * The only difference between <alertable_handle_events> and 00187 * <handle_events> is that in the alertable case, the eventloop will 00188 * return when the system queues an I/O completion routine or an 00189 * Asynchronous Procedure Call. 00190 */ 00191 virtual int handle_events (ACE_Time_Value *max_wait_time = 0) = 0; 00192 virtual int alertable_handle_events (ACE_Time_Value *max_wait_time = 0) = 0; 00193 00194 /** 00195 * This method is just like the one above, except the 00196 * <max_wait_time> value is a reference and can therefore never be 00197 * NULL. 00198 * 00199 * The only difference between <alertable_handle_events> and 00200 * <handle_events> is that in the alertable case, the eventloop will 00201 * return when the system queues an I/O completion routine or an 00202 * Asynchronous Procedure Call. 00203 */ 00204 virtual int handle_events (ACE_Time_Value &max_wait_time) = 0; 00205 virtual int alertable_handle_events (ACE_Time_Value &max_wait_time) = 0; 00206 00207 // = Event handling control. 00208 00209 /** 00210 * Return the status of Reactor. If this function returns 0, the reactor is 00211 * actively handling events. If it returns non-zero, <handling_events> and 00212 * <handle_alertable_events> return -1 immediately. 00213 */ 00214 virtual int deactivated (void) = 0; 00215 00216 /** 00217 * Control whether the Reactor will handle any more incoming events or not. 00218 * If <do_stop> == 1, the Reactor will be disabled. By default, a reactor 00219 * is in active state and can be deactivated/reactived as wish. 00220 */ 00221 virtual void deactivate (int do_stop) = 0; 00222 00223 // = Register and remove Handlers. 00224 00225 /// Register <event_handler> with <mask>. The I/O handle will always 00226 /// come from <get_handle> on the <event_handler>. 00227 virtual int register_handler (ACE_Event_Handler *event_handler, 00228 ACE_Reactor_Mask mask) = 0; 00229 00230 /// Register <event_handler> with <mask>. The I/O handle is provided 00231 /// through the <io_handle> parameter. 00232 virtual int register_handler (ACE_HANDLE io_handle, 00233 ACE_Event_Handler *event_handler, 00234 ACE_Reactor_Mask mask) = 0; 00235 00236 #if defined (ACE_WIN32) 00237 00238 // Originally this interface was available for all platforms, but 00239 // because ACE_HANDLE is an int on non-Win32 platforms, compilers 00240 // are not able to tell the difference between 00241 // register_handler(ACE_Event_Handler*,ACE_Reactor_Mask) and 00242 // register_handler(ACE_Event_Handler*,ACE_HANDLE). Therefore, we 00243 // have restricted this method to Win32 only. 00244 00245 /** 00246 * Register an <event_handler> that will be notified when 00247 * <event_handle> is signaled. Since no event mask is passed 00248 * through this interface, it is assumed that the <event_handle> 00249 * being passed in is an event handle and not an I/O handle. 00250 */ 00251 virtual int register_handler (ACE_Event_Handler *event_handler, 00252 ACE_HANDLE event_handle = ACE_INVALID_HANDLE) = 0; 00253 00254 #endif /* ACE_WIN32 */ 00255 00256 /** 00257 * Register an <event_handler> that will be notified when 00258 * <event_handle> is signaled. <mask> specifies the network events 00259 * that the <event_handler> is interested in. 00260 */ 00261 virtual int register_handler (ACE_HANDLE event_handle, 00262 ACE_HANDLE io_handle, 00263 ACE_Event_Handler *event_handler, 00264 ACE_Reactor_Mask mask) = 0; 00265 00266 /// Register <event_handler> with all the <handles> in the <Handle_Set>. 00267 virtual int register_handler (const ACE_Handle_Set &handles, 00268 ACE_Event_Handler *event_handler, 00269 ACE_Reactor_Mask mask) = 0; 00270 00271 /** 00272 * Register <new_sh> to handle the signal <signum> using the 00273 * <new_disp>. Returns the <old_sh> that was previously registered 00274 * (if any), along with the <old_disp> of the signal handler. 00275 */ 00276 virtual int register_handler (int signum, 00277 ACE_Event_Handler *new_sh, 00278 ACE_Sig_Action *new_disp = 0, 00279 ACE_Event_Handler **old_sh = 0, 00280 ACE_Sig_Action *old_disp = 0) = 0; 00281 00282 /// Registers <new_sh> to handle a set of signals <sigset> using the 00283 /// <new_disp>. 00284 virtual int register_handler (const ACE_Sig_Set &sigset, 00285 ACE_Event_Handler *new_sh, 00286 ACE_Sig_Action *new_disp = 0) = 0; 00287 00288 /** 00289 * Removes <event_handler>. Note that the I/O handle will be 00290 * obtained using <get_handle> method of <event_handler> . If 00291 * <mask> == <ACE_Event_Handler::DONT_CALL> then the <handle_close> 00292 * method of the <event_handler> is not invoked. 00293 */ 00294 virtual int remove_handler (ACE_Event_Handler *event_handler, 00295 ACE_Reactor_Mask mask) = 0; 00296 00297 /** 00298 * Removes <handle>. If <mask> == <ACE_Event_Handler::DONT_CALL> 00299 * then the <handle_close> method of the associated <event_handler> 00300 * is not invoked. 00301 */ 00302 virtual int remove_handler (ACE_HANDLE handle, 00303 ACE_Reactor_Mask mask) = 0; 00304 00305 /** 00306 * Removes all handles in <handle_set>. If <mask> == 00307 * <ACE_Event_Handler::DONT_CALL> then the <handle_close> method of 00308 * the associated <event_handler>s is not invoked. 00309 */ 00310 virtual int remove_handler (const ACE_Handle_Set &handle_set, 00311 ACE_Reactor_Mask mask) = 0; 00312 00313 /** 00314 * Remove the ACE_Event_Handler currently associated with <signum>. 00315 * Install the new disposition (if given) and return the previous 00316 * disposition (if desired by the caller). Returns 0 on success and 00317 * -1 if <signum> is invalid. 00318 */ 00319 virtual int remove_handler (int signum, 00320 ACE_Sig_Action *new_disp, 00321 ACE_Sig_Action *old_disp = 0, 00322 int sigkey = -1) = 0; 00323 00324 /// Calls <remove_handler> for every signal in <sigset>. 00325 virtual int remove_handler (const ACE_Sig_Set &sigset) = 0; 00326 00327 // = Suspend and resume Handlers. 00328 00329 /// Suspend <event_handler> temporarily. Use 00330 /// <ACE_Event_Handler::get_handle> to get the handle. 00331 virtual int suspend_handler (ACE_Event_Handler *event_handler) = 0; 00332 00333 /// Suspend <handle> temporarily. 00334 virtual int suspend_handler (ACE_HANDLE handle) = 0; 00335 00336 /// Suspend all <handles> in handle set temporarily. 00337 virtual int suspend_handler (const ACE_Handle_Set &handles) = 0; 00338 00339 /// Suspend all <handles> temporarily. 00340 virtual int suspend_handlers (void) = 0; 00341 00342 /// Resume <event_handler>. Use <ACE_Event_Handler::get_handle> to 00343 /// get the handle. 00344 virtual int resume_handler (ACE_Event_Handler *event_handler) = 0; 00345 00346 /// Resume <handle>. 00347 virtual int resume_handler (ACE_HANDLE handle) = 0; 00348 00349 /// Resume all <handles> in handle set. 00350 virtual int resume_handler (const ACE_Handle_Set &handles) = 0; 00351 00352 /// Resume all <handles>. 00353 virtual int resume_handlers (void) = 0; 00354 00355 /// Does the reactor allow the application to resume the handle on 00356 /// its own ie. can it pass on the control of handle resumption to 00357 /// the application 00358 virtual int resumable_handler (void) = 0; 00359 00360 /// Return 1 if we any event associations were made by the reactor 00361 /// for the handles that it waits on, 0 otherwise. 00362 virtual int uses_event_associations (void) = 0; 00363 00364 // If we need to reset handles returned from accept/connect. 00365 00366 // = Timer management. 00367 00368 /** 00369 * Schedule an ACE_Event_Handler that will expire after an amount 00370 * of time. The return value of this method, a timer_id value, 00371 * uniquely identifies the event_handler in the ACE_Reactor's 00372 * internal list of timers. 00373 * This timer_id value can be used to cancel the timer 00374 * with the cancel_timer() call. 00375 * 00376 * @see cancel_timer() 00377 * @see reset_timer_interval() 00378 * 00379 * @param event_handler event handler to schedule on reactor 00380 * @param arg argument passed to the handle_timeout() method of event_handler 00381 * @param delay time interval after which the timer will expire 00382 * @param interval time interval after which the timer will be automatically rescheduled 00383 * @return -1 on failure, a timer_id value on success 00384 */ 00385 virtual long schedule_timer (ACE_Event_Handler *event_handler, 00386 const void *arg, 00387 const ACE_Time_Value &delay, 00388 const ACE_Time_Value &interval = ACE_Time_Value::zero) = 0; 00389 00390 /** 00391 * Resets the interval of the timer represented by <timer_id> to 00392 * <interval>, which is specified in relative time to the current 00393 * <gettimeofday>. If <interval> is equal to 00394 * <ACE_Time_Value::zero>, the timer will become a non-rescheduling 00395 * timer. Returns 0 if successful, -1 if not. 00396 */ 00397 virtual int reset_timer_interval (long timer_id, 00398 const ACE_Time_Value &interval) = 0; 00399 00400 /// Cancel all Event_Handlers that match the address of 00401 /// <event_handler>. Returns number of handlers cancelled. 00402 virtual int cancel_timer (ACE_Event_Handler *event_handler, 00403 int dont_call_handle_close = 1) = 0; 00404 00405 /** 00406 * Cancel the single Event_Handler that matches the <timer_id> value 00407 * (which was returned from the schedule method). If arg is 00408 * non-NULL then it will be set to point to the ``magic cookie'' 00409 * argument passed in when the Event_Handler was registered. This 00410 * makes it possible to free up the memory and avoid memory leaks. 00411 * Returns 1 if cancellation succeeded and 0 if the <timer_id> 00412 * wasn't found. 00413 */ 00414 virtual int cancel_timer (long timer_id, 00415 const void **arg = 0, 00416 int dont_call_handle_close = 1) = 0; 00417 00418 // = High-level Event_Handler scheduling operations 00419 00420 /// Add <masks_to_be_added> to the <event_handler>'s entry. 00421 /// <event_handler> must already have been registered. 00422 virtual int schedule_wakeup (ACE_Event_Handler *event_handler, 00423 ACE_Reactor_Mask masks_to_be_added) = 0; 00424 00425 /// Add <masks_to_be_added> to the <handle>'s entry. <event_handler> 00426 /// associated with <handle> must already have been registered. 00427 virtual int schedule_wakeup (ACE_HANDLE handle, 00428 ACE_Reactor_Mask masks_to_be_added) = 0; 00429 00430 /// Clear <masks_to_be_cleared> from the <event_handler>'s entry. 00431 virtual int cancel_wakeup (ACE_Event_Handler *event_handler, 00432 ACE_Reactor_Mask masks_to_be_cleared) = 0; 00433 00434 /// Clear <masks_to_be_cleared> from the <handle>'s entry. 00435 virtual int cancel_wakeup (ACE_HANDLE handle, 00436 ACE_Reactor_Mask masks_to_be_cleared) = 0; 00437 00438 // = Notification methods. 00439 00440 /** 00441 * Notify <event_handler> of <mask> event. The <ACE_Time_Value> 00442 * indicates how long to blocking trying to notify. If <timeout> == 00443 * 0, the caller will block until action is possible, else will wait 00444 * until the relative time specified in <timeout> elapses). 00445 */ 00446 virtual int notify (ACE_Event_Handler *event_handler = 0, 00447 ACE_Reactor_Mask mask = ACE_Event_Handler::EXCEPT_MASK, 00448 ACE_Time_Value * = 0) = 0; 00449 00450 /** 00451 * Set the maximum number of times that ACE_Reactor_Impl will 00452 * iterate and dispatch the <ACE_Event_Handlers> that are passed in 00453 * via the notify queue before breaking out of its 00454 * <ACE_Message_Queue::dequeue> loop. By default, this is set to 00455 * -1, which means "iterate until the queue is empty." Setting this 00456 * to a value like "1 or 2" will increase "fairness" (and thus 00457 * prevent starvation) at the expense of slightly higher dispatching 00458 * overhead. 00459 */ 00460 virtual void max_notify_iterations (int) = 0; 00461 00462 /** 00463 * Get the maximum number of times that the ACE_Reactor_Impl will 00464 * iterate and dispatch the <ACE_Event_Handlers> that are passed in 00465 * via the notify queue before breaking out of its 00466 * <ACE_Message_Queue::dequeue> loop. 00467 */ 00468 virtual int max_notify_iterations (void) = 0; 00469 00470 /** 00471 * Purge any notifications pending in this reactor for the specified 00472 * ACE_Event_Handler object. Returns the number of notifications 00473 * purged. Returns -1 on error. 00474 */ 00475 virtual int purge_pending_notifications (ACE_Event_Handler * = 0, 00476 ACE_Reactor_Mask = ACE_Event_Handler::ALL_EVENTS_MASK) = 0; 00477 00478 /** 00479 * Return the Event_Handler associated with <handle>. Return 0 if 00480 * <handle> is not registered. 00481 */ 00482 virtual ACE_Event_Handler *find_handler (ACE_HANDLE handle) = 0; 00483 00484 /** 00485 * Check to see if <handle> is associated with a valid Event_Handler 00486 * bound to <mask>. Return the <event_handler> associated with this 00487 * <handler> if <event_handler> != 0. 00488 */ 00489 virtual int handler (ACE_HANDLE handle, 00490 ACE_Reactor_Mask mask, 00491 ACE_Event_Handler **event_handler = 0) = 0; 00492 00493 /** 00494 * Check to see if <signum> is associated with a valid Event_Handler 00495 * bound to a signal. Return the <event_handler> associated with 00496 * this <handler> if <event_handler> != 0. 00497 */ 00498 virtual int handler (int signum, 00499 ACE_Event_Handler ** = 0) = 0; 00500 00501 /// Returns true if Reactor has been successfully initialized, else 00502 /// false. 00503 virtual int initialized (void) = 0; 00504 00505 /// Returns the current size of the Reactor's internal descriptor 00506 /// table. 00507 virtual size_t size (void) const = 0; 00508 00509 /// Returns a reference to the Reactor's internal lock. 00510 virtual ACE_Lock &lock (void) = 0; 00511 00512 /// Wake up all threads in waiting in the event loop 00513 virtual void wakeup_all_threads (void) = 0; 00514 00515 /// Transfers ownership of Reactor_Impl to the <new_owner>. 00516 virtual int owner (ACE_thread_t new_owner, ACE_thread_t *old_owner = 0) = 0; 00517 00518 /// Return the ID of the "owner" thread. 00519 virtual int owner (ACE_thread_t *owner) = 0; 00520 00521 /// Get the existing restart value. 00522 virtual int restart (void) = 0; 00523 00524 /// Set a new value for restart and return the original value. 00525 virtual int restart (int r) = 0; 00526 00527 /// Set position of the owner thread. 00528 virtual void requeue_position (int) = 0; 00529 00530 /// Get position of the owner thread. 00531 virtual int requeue_position (void) = 0; 00532 00533 // = Low-level wait_set mask manipulation methods. 00534 00535 /// GET/SET/ADD/CLR the dispatch mask "bit" bound with the 00536 /// <event_handler> and <mask>. 00537 virtual int mask_ops (ACE_Event_Handler *event_handler, 00538 ACE_Reactor_Mask mask, 00539 int ops) = 0; 00540 00541 /// GET/SET/ADD/CLR the dispatch MASK "bit" bound with the <handle> 00542 /// and <mask>. 00543 virtual int mask_ops (ACE_HANDLE handle, 00544 ACE_Reactor_Mask mask, 00545 int ops) = 0; 00546 00547 // = Low-level ready_set mask manipulation methods. 00548 /// GET/SET/ADD/CLR the ready "bit" bound with the <event_handler> 00549 /// and <mask>. 00550 virtual int ready_ops (ACE_Event_Handler *event_handler, 00551 ACE_Reactor_Mask mask, 00552 int ops) = 0; 00553 00554 /// GET/SET/ADD/CLR the ready "bit" bound with the <handle> and <mask>. 00555 virtual int ready_ops (ACE_HANDLE handle, 00556 ACE_Reactor_Mask, 00557 int ops) = 0; 00558 00559 /// Dump the state of an object. 00560 virtual void dump (void) const = 0; 00561 00562 /// Declare the dynamic allocation hooks. 00563 ACE_ALLOC_HOOK_DECLARE; 00564 }; 00565 00566 ACE_END_VERSIONED_NAMESPACE_DECL 00567 00568 #include /**/ "ace/post.h" 00569 #endif /* ACE_REACTOR_IMPL_H */