3 //=============================================================================
7 * @author Irfan Pyarali
9 //=============================================================================
12 #ifndef ACE_REACTOR_IMPL_H
13 #define ACE_REACTOR_IMPL_H
14 #include /**/ "ace/pre.h"
16 // Timer Queue is a complicated template class. A simple forward
17 // declaration will not work
18 #include "ace/Timer_Queuefwd.h"
20 #if !defined (ACE_LACKS_PRAGMA_ONCE)
22 #endif /* ACE_LACKS_PRAGMA_ONCE */
24 // Event_Handler.h contains the definition of ACE_Reactor_Mask
25 #include "ace/Event_Handler.h"
26 #include "ace/Countdown_Time.h"
27 #include "ace/Synch_Traits.h"
29 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
33 class ACE_Reactor_Impl
;
35 class ACE_Sig_Handler
;
39 * @class ACE_Reactor_Notify
41 * @internal This class is for ACE internal use only.
43 * @brief Abstract class for unblocking an ACE_Reactor_Impl from its
46 class ACE_Export ACE_Reactor_Notify
: public ACE_Event_Handler
49 virtual int open (ACE_Reactor_Impl
*,
50 ACE_Timer_Queue
*timer_queue
= 0,
51 int disable_notify
= 0) = 0;
52 virtual int close () = 0;
55 * Called by a thread when it wants to unblock the Reactor_Impl.
56 * This wakeups the Reactor_Impl if currently blocked. Pass over
57 * both the Event_Handler *and* the @a mask to allow the caller to
58 * dictate which Event_Handler method the Reactor_Impl will
59 * invoke. The ACE_Time_Value indicates how long to blocking
60 * trying to notify the Reactor_Impl. If @a timeout == 0, the
61 * caller will block until action is possible, else will wait until
62 * the relative time specified in *@a timeout elapses).
64 virtual int notify (ACE_Event_Handler
*eh
= 0,
65 ACE_Reactor_Mask mask
= ACE_Event_Handler::EXCEPT_MASK
,
66 ACE_Time_Value
*timeout
= 0) = 0;
68 /// Handles pending threads (if any) that are waiting to unblock the
70 virtual int dispatch_notifications (int &number_of_active_handles
,
71 ACE_Handle_Set
&rd_mask
) = 0;
73 /// Returns the ACE_HANDLE of the notify pipe on which the reactor
74 /// is listening for notifications so that other threads can unblock
76 virtual ACE_HANDLE
notify_handle () = 0;
78 /// Verify whether the buffer has dispatchable info or not.
79 virtual int is_dispatchable (ACE_Notification_Buffer
&buffer
)= 0;
81 /// Handle one of the notify call on the handle. This could be
82 /// because of a thread trying to unblock the Reactor_Impl
83 virtual int dispatch_notify (ACE_Notification_Buffer
&buffer
) = 0;
85 /// Read one of the notify call on the @a handle into the
86 /// @a buffer. This could be because of a thread trying to unblock
88 virtual int read_notify_pipe (ACE_HANDLE handle
,
89 ACE_Notification_Buffer
&buffer
) = 0;
91 * Set the maximum number of times that the @c handle_input method
92 * will iterate and dispatch the ACE_Event_Handlers that are
93 * passed in via the notify queue before breaking out of the event
94 * loop. By default, this is set to -1, which means "iterate until
95 * the queue is empty." Setting this to a value like "1 or 2" will
96 * increase "fairness" (and thus prevent starvation) at the expense
97 * of slightly higher dispatching overhead.
99 virtual void max_notify_iterations (int) = 0;
102 * Get the maximum number of times that the @c handle_input method
103 * will iterate and dispatch the ACE_Event_Handlers that are
104 * passed in via the notify queue before breaking out of its event
107 virtual int max_notify_iterations () = 0;
110 * Purge any notifications pending in this reactor for the specified
111 * ACE_Event_Handler object. Returns the number of notifications
112 * purged. Returns -1 on error.
114 virtual int purge_pending_notifications (ACE_Event_Handler
* = 0,
115 ACE_Reactor_Mask
= ACE_Event_Handler::ALL_EVENTS_MASK
) = 0;
117 /// Dump the state of an object.
118 virtual void dump () const = 0;
122 * @class ACE_Reactor_Impl
124 * @brief An abstract class for implementing the Reactor Pattern.
126 class ACE_Export ACE_Reactor_Impl
129 /// Close down and release all resources.
130 virtual ~ACE_Reactor_Impl ();
133 virtual int open (size_t size
,
134 bool restart
= false,
135 ACE_Sig_Handler
* = 0,
136 ACE_Timer_Queue
* = 0,
137 int disable_notify_pipe
= 0,
138 ACE_Reactor_Notify
* = 0) = 0;
141 * Returns 0, if the size of the current message has been put in
142 * @a size Returns -1, if not. ACE_HANDLE allows the reactor to
143 * check if the caller is valid.
145 virtual int current_info (ACE_HANDLE
, size_t & /* size */) = 0;
147 /// Use a user specified signal handler instead.
148 virtual int set_sig_handler (ACE_Sig_Handler
*signal_handler
) = 0;
150 /// Set a user-specified timer queue.
151 virtual int timer_queue (ACE_Timer_Queue
*tq
) = 0;
153 /// Return the current ACE_Timer_Queue.
154 virtual ACE_Timer_Queue
*timer_queue () const = 0;
156 /// Close down and release all resources.
157 virtual int close () = 0;
159 // = Event loop drivers.
161 * Returns non-zero if there are I/O events "ready" for dispatching,
162 * but does not actually dispatch the event handlers. By default,
163 * don't block while checking this, i.e., "poll".
165 virtual int work_pending (const ACE_Time_Value
&max_wait_time
= ACE_Time_Value::zero
) = 0;
168 * This event loop driver blocks for up to @a max_wait_time before
169 * returning. It will return earlier if events occur. Note that
170 * @a max_wait_time can be 0, in which case this method blocks
171 * indefinitely until events occur.
173 * @a max_wait_time is decremented to reflect how much time this call
174 * took. For instance, if a time value of 3 seconds is passed to
175 * handle_events and an event occurs after 2 seconds,
176 * @a max_wait_time will equal 1 second. This can be used if an
177 * application wishes to handle events for some fixed amount of
180 * Returns the total number of ACE_Event_Handlers that were
181 * dispatched, 0 if the @a max_wait_time elapsed without dispatching
182 * any handlers, or -1 if an error occurs.
184 * The only difference between alertable_handle_events() and
185 * handle_events() is that in the alertable case, the eventloop will
186 * return when the system queues an I/O completion routine or an
187 * Asynchronous Procedure Call.
189 virtual int handle_events (ACE_Time_Value
*max_wait_time
= 0) = 0;
190 virtual int alertable_handle_events (ACE_Time_Value
*max_wait_time
= 0) = 0;
193 * This method is just like the one above, except the
194 * @a max_wait_time value is a reference and can therefore never be
197 * The only difference between alertable_handle_events() and
198 * handle_events() is that in the alertable case, the eventloop will
199 * return when the system queues an I/O completion routine or an
200 * Asynchronous Procedure Call.
202 virtual int handle_events (ACE_Time_Value
&max_wait_time
) = 0;
203 virtual int alertable_handle_events (ACE_Time_Value
&max_wait_time
) = 0;
205 // = Event handling control.
208 * Return the status of Reactor. If this function returns 0, the reactor is
209 * actively handling events. If it returns non-zero, handling_events() and
210 * handle_alertable_events() return -1 immediately.
212 virtual int deactivated () = 0;
215 * Control whether the Reactor will handle any more incoming events or not.
216 * If @a do_stop == 1, the Reactor will be disabled. By default, a reactor
217 * is in active state and can be deactivated/reactived as wish.
219 virtual void deactivate (int do_stop
) = 0;
221 // = Register and remove Handlers.
223 /// Register @a event_handler with @a mask. The I/O handle will always
224 /// come from get_handle() on the @a event_handler.
225 virtual int register_handler (ACE_Event_Handler
*event_handler
,
226 ACE_Reactor_Mask mask
) = 0;
228 /// Register @a event_handler with @a mask. The I/O handle is provided
229 /// through the @a io_handle parameter.
230 virtual int register_handler (ACE_HANDLE io_handle
,
231 ACE_Event_Handler
*event_handler
,
232 ACE_Reactor_Mask mask
) = 0;
234 #if defined (ACE_WIN32)
236 // Originally this interface was available for all platforms, but
237 // because ACE_HANDLE is an int on non-Win32 platforms, compilers
238 // are not able to tell the difference between
239 // register_handler(ACE_Event_Handler*,ACE_Reactor_Mask) and
240 // register_handler(ACE_Event_Handler*,ACE_HANDLE). Therefore, we
241 // have restricted this method to Win32 only.
244 * Register an @a event_handler that will be notified when
245 * event_handle is signaled. Since no event mask is passed
246 * through this interface, it is assumed that the @a event_handler
247 * being passed in is an event handle and not an I/O handle.
249 virtual int register_handler (ACE_Event_Handler
*event_handler
,
250 ACE_HANDLE event_handle
= ACE_INVALID_HANDLE
) = 0;
252 #endif /* ACE_WIN32 */
255 * Register an @a event_handler that will be notified when
256 * event_handle is signaled. @a mask specifies the network events
257 * that the @a event_handler is interested in.
259 virtual int register_handler (ACE_HANDLE event_handle
,
260 ACE_HANDLE io_handle
,
261 ACE_Event_Handler
*event_handler
,
262 ACE_Reactor_Mask mask
) = 0;
264 /// Register @a event_handler with all the @a handles in the Handle_Set.
265 virtual int register_handler (const ACE_Handle_Set
&handles
,
266 ACE_Event_Handler
*event_handler
,
267 ACE_Reactor_Mask mask
) = 0;
270 * Register @a new_sh to handle the signal @a signum using the
271 * @a new_disp. Returns the @a old_sh that was previously registered
272 * (if any), along with the @a old_disp of the signal handler.
274 virtual int register_handler (int signum
,
275 ACE_Event_Handler
*new_sh
,
276 ACE_Sig_Action
*new_disp
= 0,
277 ACE_Event_Handler
**old_sh
= 0,
278 ACE_Sig_Action
*old_disp
= 0) = 0;
280 /// Registers @a new_sh to handle a set of signals @a sigset using the
282 virtual int register_handler (const ACE_Sig_Set
&sigset
,
283 ACE_Event_Handler
*new_sh
,
284 ACE_Sig_Action
*new_disp
= 0) = 0;
287 * Removes @a event_handler. Note that the I/O handle will be
288 * obtained using get_handle() method of @a event_handler . If
289 * @a mask == ACE_Event_Handler::DONT_CALL then the handle_close()
290 * method of the @a event_handler is not invoked.
292 virtual int remove_handler (ACE_Event_Handler
*event_handler
,
293 ACE_Reactor_Mask mask
) = 0;
296 * Removes @a handle. If @a mask == ACE_Event_Handler::DONT_CALL
297 * then the handle_close() method of the associated event_handler
300 virtual int remove_handler (ACE_HANDLE handle
,
301 ACE_Reactor_Mask mask
) = 0;
304 * Removes all handles in @a handle_set. If @a mask ==
305 * ACE_Event_Handler::DONT_CALL then the handle_close() method of
306 * the associated event_handlers is not invoked.
308 virtual int remove_handler (const ACE_Handle_Set
&handle_set
,
309 ACE_Reactor_Mask mask
) = 0;
312 * Remove the ACE_Event_Handler currently associated with @a signum.
313 * Install the new disposition (if given) and return the previous
314 * disposition (if desired by the caller). Returns 0 on success and
315 * -1 if @a signum is invalid.
317 virtual int remove_handler (int signum
,
318 ACE_Sig_Action
*new_disp
,
319 ACE_Sig_Action
*old_disp
= 0,
320 int sigkey
= -1) = 0;
322 /// Calls remove_handler() for every signal in @a sigset.
323 virtual int remove_handler (const ACE_Sig_Set
&sigset
) = 0;
325 // = Suspend and resume Handlers.
327 /// Suspend @a event_handler temporarily. Uses
328 /// ACE_Event_Handler::get_handle() to get the handle.
329 virtual int suspend_handler (ACE_Event_Handler
*event_handler
) = 0;
331 /// Suspend @a handle temporarily.
332 virtual int suspend_handler (ACE_HANDLE handle
) = 0;
334 /// Suspend all @a handles in handle set temporarily.
335 virtual int suspend_handler (const ACE_Handle_Set
&handles
) = 0;
337 /// Suspend all <handles> temporarily.
338 virtual int suspend_handlers () = 0;
340 /// Resume @a event_handler. Uses ACE_Event_Handler::get_handle() to
342 virtual int resume_handler (ACE_Event_Handler
*event_handler
) = 0;
344 /// Resume @a handle.
345 virtual int resume_handler (ACE_HANDLE handle
) = 0;
347 /// Resume all @a handles in handle set.
348 virtual int resume_handler (const ACE_Handle_Set
&handles
) = 0;
350 /// Resume all handles.
351 virtual int resume_handlers () = 0;
353 /// Does the reactor allow the application to resume the handle on
354 /// its own ie. can it pass on the control of handle resumption to
356 virtual int resumable_handler () = 0;
358 /// Return true if we any event associations were made by the reactor
359 /// for the handles that it waits on, false otherwise.
360 virtual bool uses_event_associations () = 0;
362 // If we need to reset handles returned from accept/connect.
364 // = Timer management.
367 * Schedule an ACE_Event_Handler that will expire after an amount
368 * of time. The return value of this method, a timer_id value,
369 * uniquely identifies the event_handler in the ACE_Reactor's
370 * internal list of timers.
371 * This timer_id value can be used to cancel the timer
372 * with the cancel_timer() call.
374 * @see cancel_timer()
375 * @see reset_timer_interval()
377 * @param event_handler event handler to schedule on reactor
378 * @param arg argument passed to the handle_timeout() method of event_handler
379 * @param delay time interval after which the timer will expire
380 * @param interval time interval after which the timer will be automatically rescheduled
381 * @return -1 on failure, a timer_id value on success
383 virtual long schedule_timer (ACE_Event_Handler
*event_handler
,
385 const ACE_Time_Value
&delay
,
386 const ACE_Time_Value
&interval
= ACE_Time_Value::zero
) = 0;
389 * Resets the interval of the timer represented by @a timer_id to
390 * @a interval, which is specified in relative time to the current
391 * gettimeofday(). If @a interval is equal to
392 * ACE_Time_Value::zero, the timer will become a non-rescheduling
393 * timer. Returns 0 if successful, -1 if not.
395 virtual int reset_timer_interval (long timer_id
,
396 const ACE_Time_Value
&interval
) = 0;
398 /// Cancel all Event_Handlers that match the address of
399 /// @a event_handler. Returns number of handlers cancelled.
400 virtual int cancel_timer (ACE_Event_Handler
*event_handler
,
401 int dont_call_handle_close
= 1) = 0;
404 * Cancel the single Event_Handler that matches the @a timer_id value
405 * (which was returned from the schedule method). If arg is
406 * non-NULL then it will be set to point to the ``magic cookie''
407 * argument passed in when the Event_Handler was registered. This
408 * makes it possible to free up the memory and avoid memory leaks.
409 * Returns 1 if cancellation succeeded and 0 if the @a timer_id
412 virtual int cancel_timer (long timer_id
,
413 const void **arg
= 0,
414 int dont_call_handle_close
= 1) = 0;
416 // = High-level Event_Handler scheduling operations
418 /// Add @a masks_to_be_added to the @a event_handler's entry.
419 /// @a event_handler must already have been registered.
420 virtual int schedule_wakeup (ACE_Event_Handler
*event_handler
,
421 ACE_Reactor_Mask masks_to_be_added
) = 0;
423 /// Add @a masks_to_be_added to the @a handle's entry. <event_handler>
424 /// associated with @a handle must already have been registered.
425 virtual int schedule_wakeup (ACE_HANDLE handle
,
426 ACE_Reactor_Mask masks_to_be_added
) = 0;
428 /// Clear @a masks_to_be_cleared from the @a event_handler's entry.
429 virtual int cancel_wakeup (ACE_Event_Handler
*event_handler
,
430 ACE_Reactor_Mask masks_to_be_cleared
) = 0;
432 /// Clear @a masks_to_be_cleared from the @a handle's entry.
433 virtual int cancel_wakeup (ACE_HANDLE handle
,
434 ACE_Reactor_Mask masks_to_be_cleared
) = 0;
436 // = Notification methods.
439 * Notify @a event_handler of @a mask event. The ACE_Time_Value
440 * indicates how long to blocking trying to notify. If @a timeout ==
441 * 0, the caller will block until action is possible, else will wait
442 * until the relative time specified in @a timeout elapses).
444 virtual int notify (ACE_Event_Handler
*event_handler
= 0,
445 ACE_Reactor_Mask mask
= ACE_Event_Handler::EXCEPT_MASK
,
446 ACE_Time_Value
* = 0) = 0;
449 * Set the maximum number of times that ACE_Reactor_Impl will
450 * iterate and dispatch the ACE_Event_Handlers that are passed in
451 * via the notify queue before breaking out of its
452 * ACE_Message_Queue::dequeue() loop. By default, this is set to
453 * -1, which means "iterate until the queue is empty." Setting this
454 * to a value like "1 or 2" will increase "fairness" (and thus
455 * prevent starvation) at the expense of slightly higher dispatching
458 virtual void max_notify_iterations (int) = 0;
461 * Get the maximum number of times that the ACE_Reactor_Impl will
462 * iterate and dispatch the ACE_Event_Handlers that are passed in
463 * via the notify queue before breaking out of its
464 * ACE_Message_Queue::dequeue() loop.
466 virtual int max_notify_iterations () = 0;
469 * Purge any notifications pending in this reactor for the specified
470 * ACE_Event_Handler object. Returns the number of notifications
471 * purged. Returns -1 on error.
473 virtual int purge_pending_notifications (ACE_Event_Handler
* = 0,
474 ACE_Reactor_Mask
= ACE_Event_Handler::ALL_EVENTS_MASK
) = 0;
477 * Return the Event_Handler associated with @a handle. Return 0 if
478 * @a handle is not registered.
480 virtual ACE_Event_Handler
*find_handler (ACE_HANDLE handle
) = 0;
483 * Check to see if @a handle is associated with a valid Event_Handler
484 * bound to @a mask. Return the @a event_handler associated with this
485 * handler if @a event_handler != 0.
487 virtual int handler (ACE_HANDLE handle
,
488 ACE_Reactor_Mask mask
,
489 ACE_Event_Handler
**event_handler
= 0) = 0;
492 * Check to see if @a signum is associated with a valid Event_Handler
493 * bound to a signal. Return the @a event_handler associated with
494 * this handler if @a event_handler != 0.
496 virtual int handler (int signum
,
497 ACE_Event_Handler
** = 0) = 0;
499 /// Returns true if Reactor has been successfully initialized, else
501 virtual bool initialized () = 0;
503 /// Returns the current size of the Reactor's internal descriptor
505 virtual size_t size () const = 0;
507 /// Returns a reference to the Reactor's internal lock.
508 virtual ACE_Lock
&lock () = 0;
510 /// Wake up all threads in waiting in the event loop
511 virtual void wakeup_all_threads () = 0;
513 /// Transfers ownership of Reactor_Impl to the @a new_owner.
514 virtual int owner (ACE_thread_t new_owner
, ACE_thread_t
*old_owner
= 0) = 0;
516 /// Return the ID of the "owner" thread.
517 virtual int owner (ACE_thread_t
*owner
) = 0;
519 /// Get the existing restart value.
520 virtual bool restart () = 0;
522 /// Set a new value for restart and return the original value.
523 virtual bool restart (bool r
) = 0;
525 /// Set position of the owner thread.
526 virtual void requeue_position (int) = 0;
528 /// Get position of the owner thread.
529 virtual int requeue_position () = 0;
531 // = Low-level wait_set mask manipulation methods.
533 /// GET/SET/ADD/CLR the dispatch mask "bit" bound with the
534 /// @a event_handler and @a mask.
535 virtual int mask_ops (ACE_Event_Handler
*event_handler
,
536 ACE_Reactor_Mask mask
,
539 /// GET/SET/ADD/CLR the dispatch MASK "bit" bound with the @a handle
541 virtual int mask_ops (ACE_HANDLE handle
,
542 ACE_Reactor_Mask mask
,
545 // = Low-level ready_set mask manipulation methods.
546 /// GET/SET/ADD/CLR the ready "bit" bound with the @a event_handler
548 virtual int ready_ops (ACE_Event_Handler
*event_handler
,
549 ACE_Reactor_Mask mask
,
552 /// GET/SET/ADD/CLR the ready "bit" bound with the @a handle and @a mask.
553 virtual int ready_ops (ACE_HANDLE handle
,
557 /// Dump the state of an object.
558 virtual void dump () const = 0;
560 /// Declare the dynamic allocation hooks.
561 ACE_ALLOC_HOOK_DECLARE
;
564 ACE_END_VERSIONED_NAMESPACE_DECL
566 #include /**/ "ace/post.h"
567 #endif /* ACE_REACTOR_IMPL_H */