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"
28 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
32 class ACE_Reactor_Impl
;
34 class ACE_Sig_Handler
;
38 * @class ACE_Reactor_Notify
40 * @internal This class is for ACE internal use only.
42 * @brief Abstract class for unblocking an ACE_Reactor_Impl from its
45 class ACE_Export ACE_Reactor_Notify
: public ACE_Event_Handler
48 virtual int open (ACE_Reactor_Impl
*,
49 ACE_Timer_Queue
*timer_queue
= 0,
50 int disable_notify
= 0) = 0;
51 virtual int close (void) = 0;
54 * Called by a thread when it wants to unblock the Reactor_Impl.
55 * This wakeups the Reactor_Impl if currently blocked. Pass over
56 * both the Event_Handler *and* the @a mask to allow the caller to
57 * dictate which Event_Handler method the Reactor_Impl will
58 * invoke. The ACE_Time_Value indicates how long to blocking
59 * trying to notify the Reactor_Impl. If @a timeout == 0, the
60 * caller will block until action is possible, else will wait until
61 * the relative time specified in *@a timeout elapses).
63 virtual int notify (ACE_Event_Handler
*eh
= 0,
64 ACE_Reactor_Mask mask
= ACE_Event_Handler::EXCEPT_MASK
,
65 ACE_Time_Value
*timeout
= 0) = 0;
67 /// Handles pending threads (if any) that are waiting to unblock the
69 virtual int dispatch_notifications (int &number_of_active_handles
,
70 ACE_Handle_Set
&rd_mask
) = 0;
72 /// Returns the ACE_HANDLE of the notify pipe on which the reactor
73 /// is listening for notifications so that other threads can unblock
75 virtual ACE_HANDLE
notify_handle (void) = 0;
77 /// Verify whether the buffer has dispatchable info or not.
78 virtual int is_dispatchable (ACE_Notification_Buffer
&buffer
)= 0;
80 /// Handle one of the notify call on the handle. This could be
81 /// because of a thread trying to unblock the Reactor_Impl
82 virtual int dispatch_notify (ACE_Notification_Buffer
&buffer
) = 0;
84 /// Read one of the notify call on the @a handle into the
85 /// @a buffer. This could be because of a thread trying to unblock
87 virtual int read_notify_pipe (ACE_HANDLE handle
,
88 ACE_Notification_Buffer
&buffer
) = 0;
90 * Set the maximum number of times that the @c handle_input method
91 * will iterate and dispatch the ACE_Event_Handlers that are
92 * passed in via the notify queue before breaking out of the event
93 * loop. By default, this is set to -1, which means "iterate until
94 * the queue is empty." Setting this to a value like "1 or 2" will
95 * increase "fairness" (and thus prevent starvation) at the expense
96 * of slightly higher dispatching overhead.
98 virtual void max_notify_iterations (int) = 0;
101 * Get the maximum number of times that the @c handle_input method
102 * will iterate and dispatch the ACE_Event_Handlers that are
103 * passed in via the notify queue before breaking out of its event
106 virtual int max_notify_iterations (void) = 0;
109 * Purge any notifications pending in this reactor for the specified
110 * ACE_Event_Handler object. Returns the number of notifications
111 * purged. Returns -1 on error.
113 virtual int purge_pending_notifications (ACE_Event_Handler
* = 0,
114 ACE_Reactor_Mask
= ACE_Event_Handler::ALL_EVENTS_MASK
) = 0;
116 /// Dump the state of an object.
117 virtual void dump (void) const = 0;
121 * @class ACE_Reactor_Impl
123 * @brief An abstract class for implementing the Reactor Pattern.
125 class ACE_Export ACE_Reactor_Impl
128 /// Close down and release all resources.
129 virtual ~ACE_Reactor_Impl (void);
132 virtual int open (size_t size
,
133 bool restart
= false,
134 ACE_Sig_Handler
* = 0,
135 ACE_Timer_Queue
* = 0,
136 int disable_notify_pipe
= 0,
137 ACE_Reactor_Notify
* = 0) = 0;
140 * Returns 0, if the size of the current message has been put in
141 * @a size Returns -1, if not. ACE_HANDLE allows the reactor to
142 * check if the caller is valid.
144 virtual int current_info (ACE_HANDLE
, size_t & /* size */) = 0;
146 /// Use a user specified signal handler instead.
147 virtual int set_sig_handler (ACE_Sig_Handler
*signal_handler
) = 0;
149 /// Set a user-specified timer queue.
150 virtual int timer_queue (ACE_Timer_Queue
*tq
) = 0;
152 /// Return the current ACE_Timer_Queue.
153 virtual ACE_Timer_Queue
*timer_queue (void) const = 0;
155 /// Close down and release all resources.
156 virtual int close (void) = 0;
158 // = Event loop drivers.
160 * Returns non-zero if there are I/O events "ready" for dispatching,
161 * but does not actually dispatch the event handlers. By default,
162 * don't block while checking this, i.e., "poll".
164 virtual int work_pending (const ACE_Time_Value
&max_wait_time
= ACE_Time_Value::zero
) = 0;
167 * This event loop driver blocks for up to @a max_wait_time before
168 * returning. It will return earlier if events occur. Note that
169 * @a max_wait_time can be 0, in which case this method blocks
170 * indefinitely until events occur.
172 * @a max_wait_time is decremented to reflect how much time this call
173 * took. For instance, if a time value of 3 seconds is passed to
174 * handle_events and an event occurs after 2 seconds,
175 * @a max_wait_time will equal 1 second. This can be used if an
176 * application wishes to handle events for some fixed amount of
179 * Returns the total number of ACE_Event_Handlers that were
180 * dispatched, 0 if the @a max_wait_time elapsed without dispatching
181 * any handlers, or -1 if an error occurs.
183 * The only difference between alertable_handle_events() and
184 * handle_events() is that in the alertable case, the eventloop will
185 * return when the system queues an I/O completion routine or an
186 * Asynchronous Procedure Call.
188 virtual int handle_events (ACE_Time_Value
*max_wait_time
= 0) = 0;
189 virtual int alertable_handle_events (ACE_Time_Value
*max_wait_time
= 0) = 0;
192 * This method is just like the one above, except the
193 * @a max_wait_time value is a reference and can therefore never be
196 * The only difference between alertable_handle_events() and
197 * handle_events() is that in the alertable case, the eventloop will
198 * return when the system queues an I/O completion routine or an
199 * Asynchronous Procedure Call.
201 virtual int handle_events (ACE_Time_Value
&max_wait_time
) = 0;
202 virtual int alertable_handle_events (ACE_Time_Value
&max_wait_time
) = 0;
204 // = Event handling control.
207 * Return the status of Reactor. If this function returns 0, the reactor is
208 * actively handling events. If it returns non-zero, handling_events() and
209 * handle_alertable_events() return -1 immediately.
211 virtual int deactivated (void) = 0;
214 * Control whether the Reactor will handle any more incoming events or not.
215 * If @a do_stop == 1, the Reactor will be disabled. By default, a reactor
216 * is in active state and can be deactivated/reactived as wish.
218 virtual void deactivate (int do_stop
) = 0;
220 // = Register and remove Handlers.
222 /// Register @a event_handler with @a mask. The I/O handle will always
223 /// come from get_handle() on the @a event_handler.
224 virtual int register_handler (ACE_Event_Handler
*event_handler
,
225 ACE_Reactor_Mask mask
) = 0;
227 /// Register @a event_handler with @a mask. The I/O handle is provided
228 /// through the @a io_handle parameter.
229 virtual int register_handler (ACE_HANDLE io_handle
,
230 ACE_Event_Handler
*event_handler
,
231 ACE_Reactor_Mask mask
) = 0;
233 #if defined (ACE_WIN32)
235 // Originally this interface was available for all platforms, but
236 // because ACE_HANDLE is an int on non-Win32 platforms, compilers
237 // are not able to tell the difference between
238 // register_handler(ACE_Event_Handler*,ACE_Reactor_Mask) and
239 // register_handler(ACE_Event_Handler*,ACE_HANDLE). Therefore, we
240 // have restricted this method to Win32 only.
243 * Register an @a event_handler that will be notified when
244 * event_handle is signaled. Since no event mask is passed
245 * through this interface, it is assumed that the @a event_handler
246 * being passed in is an event handle and not an I/O handle.
248 virtual int register_handler (ACE_Event_Handler
*event_handler
,
249 ACE_HANDLE event_handle
= ACE_INVALID_HANDLE
) = 0;
251 #endif /* ACE_WIN32 */
254 * Register an @a event_handler that will be notified when
255 * event_handle is signaled. @a mask specifies the network events
256 * that the @a event_handler is interested in.
258 virtual int register_handler (ACE_HANDLE event_handle
,
259 ACE_HANDLE io_handle
,
260 ACE_Event_Handler
*event_handler
,
261 ACE_Reactor_Mask mask
) = 0;
263 /// Register @a event_handler with all the @a handles in the Handle_Set.
264 virtual int register_handler (const ACE_Handle_Set
&handles
,
265 ACE_Event_Handler
*event_handler
,
266 ACE_Reactor_Mask mask
) = 0;
269 * Register @a new_sh to handle the signal @a signum using the
270 * @a new_disp. Returns the @a old_sh that was previously registered
271 * (if any), along with the @a old_disp of the signal handler.
273 virtual int register_handler (int signum
,
274 ACE_Event_Handler
*new_sh
,
275 ACE_Sig_Action
*new_disp
= 0,
276 ACE_Event_Handler
**old_sh
= 0,
277 ACE_Sig_Action
*old_disp
= 0) = 0;
279 /// Registers @a new_sh to handle a set of signals @a sigset using the
281 virtual int register_handler (const ACE_Sig_Set
&sigset
,
282 ACE_Event_Handler
*new_sh
,
283 ACE_Sig_Action
*new_disp
= 0) = 0;
286 * Removes @a event_handler. Note that the I/O handle will be
287 * obtained using get_handle() method of @a event_handler . If
288 * @a mask == ACE_Event_Handler::DONT_CALL then the handle_close()
289 * method of the @a event_handler is not invoked.
291 virtual int remove_handler (ACE_Event_Handler
*event_handler
,
292 ACE_Reactor_Mask mask
) = 0;
295 * Removes @a handle. If @a mask == ACE_Event_Handler::DONT_CALL
296 * then the handle_close() method of the associated event_handler
299 virtual int remove_handler (ACE_HANDLE handle
,
300 ACE_Reactor_Mask mask
) = 0;
303 * Removes all handles in @a handle_set. If @a mask ==
304 * ACE_Event_Handler::DONT_CALL then the handle_close() method of
305 * the associated event_handlers is not invoked.
307 virtual int remove_handler (const ACE_Handle_Set
&handle_set
,
308 ACE_Reactor_Mask mask
) = 0;
311 * Remove the ACE_Event_Handler currently associated with @a signum.
312 * Install the new disposition (if given) and return the previous
313 * disposition (if desired by the caller). Returns 0 on success and
314 * -1 if @a signum is invalid.
316 virtual int remove_handler (int signum
,
317 ACE_Sig_Action
*new_disp
,
318 ACE_Sig_Action
*old_disp
= 0,
319 int sigkey
= -1) = 0;
321 /// Calls remove_handler() for every signal in @a sigset.
322 virtual int remove_handler (const ACE_Sig_Set
&sigset
) = 0;
324 // = Suspend and resume Handlers.
326 /// Suspend @a event_handler temporarily. Uses
327 /// ACE_Event_Handler::get_handle() to get the handle.
328 virtual int suspend_handler (ACE_Event_Handler
*event_handler
) = 0;
330 /// Suspend @a handle temporarily.
331 virtual int suspend_handler (ACE_HANDLE handle
) = 0;
333 /// Suspend all @a handles in handle set temporarily.
334 virtual int suspend_handler (const ACE_Handle_Set
&handles
) = 0;
336 /// Suspend all <handles> temporarily.
337 virtual int suspend_handlers (void) = 0;
339 /// Resume @a event_handler. Uses ACE_Event_Handler::get_handle() to
341 virtual int resume_handler (ACE_Event_Handler
*event_handler
) = 0;
343 /// Resume @a handle.
344 virtual int resume_handler (ACE_HANDLE handle
) = 0;
346 /// Resume all @a handles in handle set.
347 virtual int resume_handler (const ACE_Handle_Set
&handles
) = 0;
349 /// Resume all handles.
350 virtual int resume_handlers (void) = 0;
352 /// Does the reactor allow the application to resume the handle on
353 /// its own ie. can it pass on the control of handle resumption to
355 virtual int resumable_handler (void) = 0;
357 /// Return true if we any event associations were made by the reactor
358 /// for the handles that it waits on, false otherwise.
359 virtual bool uses_event_associations (void) = 0;
361 // If we need to reset handles returned from accept/connect.
363 // = Timer management.
366 * Schedule an ACE_Event_Handler that will expire after an amount
367 * of time. The return value of this method, a timer_id value,
368 * uniquely identifies the event_handler in the ACE_Reactor's
369 * internal list of timers.
370 * This timer_id value can be used to cancel the timer
371 * with the cancel_timer() call.
373 * @see cancel_timer()
374 * @see reset_timer_interval()
376 * @param event_handler event handler to schedule on reactor
377 * @param arg argument passed to the handle_timeout() method of event_handler
378 * @param delay time interval after which the timer will expire
379 * @param interval time interval after which the timer will be automatically rescheduled
380 * @return -1 on failure, a timer_id value on success
382 virtual long schedule_timer (ACE_Event_Handler
*event_handler
,
384 const ACE_Time_Value
&delay
,
385 const ACE_Time_Value
&interval
= ACE_Time_Value::zero
) = 0;
388 * Resets the interval of the timer represented by @a timer_id to
389 * @a interval, which is specified in relative time to the current
390 * gettimeofday(). If @a interval is equal to
391 * ACE_Time_Value::zero, the timer will become a non-rescheduling
392 * timer. Returns 0 if successful, -1 if not.
394 virtual int reset_timer_interval (long timer_id
,
395 const ACE_Time_Value
&interval
) = 0;
397 /// Cancel all Event_Handlers that match the address of
398 /// @a event_handler. Returns number of handlers cancelled.
399 virtual int cancel_timer (ACE_Event_Handler
*event_handler
,
400 int dont_call_handle_close
= 1) = 0;
403 * Cancel the single Event_Handler that matches the @a timer_id value
404 * (which was returned from the schedule method). If arg is
405 * non-NULL then it will be set to point to the ``magic cookie''
406 * argument passed in when the Event_Handler was registered. This
407 * makes it possible to free up the memory and avoid memory leaks.
408 * Returns 1 if cancellation succeeded and 0 if the @a timer_id
411 virtual int cancel_timer (long timer_id
,
412 const void **arg
= 0,
413 int dont_call_handle_close
= 1) = 0;
415 // = High-level Event_Handler scheduling operations
417 /// Add @a masks_to_be_added to the @a event_handler's entry.
418 /// @a event_handler must already have been registered.
419 virtual int schedule_wakeup (ACE_Event_Handler
*event_handler
,
420 ACE_Reactor_Mask masks_to_be_added
) = 0;
422 /// Add @a masks_to_be_added to the @a handle's entry. <event_handler>
423 /// associated with @a handle must already have been registered.
424 virtual int schedule_wakeup (ACE_HANDLE handle
,
425 ACE_Reactor_Mask masks_to_be_added
) = 0;
427 /// Clear @a masks_to_be_cleared from the @a event_handler's entry.
428 virtual int cancel_wakeup (ACE_Event_Handler
*event_handler
,
429 ACE_Reactor_Mask masks_to_be_cleared
) = 0;
431 /// Clear @a masks_to_be_cleared from the @a handle's entry.
432 virtual int cancel_wakeup (ACE_HANDLE handle
,
433 ACE_Reactor_Mask masks_to_be_cleared
) = 0;
435 // = Notification methods.
438 * Notify @a event_handler of @a mask event. The ACE_Time_Value
439 * indicates how long to blocking trying to notify. If @a timeout ==
440 * 0, the caller will block until action is possible, else will wait
441 * until the relative time specified in @a timeout elapses).
443 virtual int notify (ACE_Event_Handler
*event_handler
= 0,
444 ACE_Reactor_Mask mask
= ACE_Event_Handler::EXCEPT_MASK
,
445 ACE_Time_Value
* = 0) = 0;
448 * Set the maximum number of times that ACE_Reactor_Impl will
449 * iterate and dispatch the ACE_Event_Handlers that are passed in
450 * via the notify queue before breaking out of its
451 * ACE_Message_Queue::dequeue() loop. By default, this is set to
452 * -1, which means "iterate until the queue is empty." Setting this
453 * to a value like "1 or 2" will increase "fairness" (and thus
454 * prevent starvation) at the expense of slightly higher dispatching
457 virtual void max_notify_iterations (int) = 0;
460 * Get the maximum number of times that the ACE_Reactor_Impl will
461 * iterate and dispatch the ACE_Event_Handlers that are passed in
462 * via the notify queue before breaking out of its
463 * ACE_Message_Queue::dequeue() loop.
465 virtual int max_notify_iterations (void) = 0;
468 * Purge any notifications pending in this reactor for the specified
469 * ACE_Event_Handler object. Returns the number of notifications
470 * purged. Returns -1 on error.
472 virtual int purge_pending_notifications (ACE_Event_Handler
* = 0,
473 ACE_Reactor_Mask
= ACE_Event_Handler::ALL_EVENTS_MASK
) = 0;
476 * Return the Event_Handler associated with @a handle. Return 0 if
477 * @a handle is not registered.
479 virtual ACE_Event_Handler
*find_handler (ACE_HANDLE handle
) = 0;
482 * Check to see if @a handle is associated with a valid Event_Handler
483 * bound to @a mask. Return the @a event_handler associated with this
484 * handler if @a event_handler != 0.
486 virtual int handler (ACE_HANDLE handle
,
487 ACE_Reactor_Mask mask
,
488 ACE_Event_Handler
**event_handler
= 0) = 0;
491 * Check to see if @a signum is associated with a valid Event_Handler
492 * bound to a signal. Return the @a event_handler associated with
493 * this handler if @a event_handler != 0.
495 virtual int handler (int signum
,
496 ACE_Event_Handler
** = 0) = 0;
498 /// Returns true if Reactor has been successfully initialized, else
500 virtual bool initialized (void) = 0;
502 /// Returns the current size of the Reactor's internal descriptor
504 virtual size_t size (void) const = 0;
506 /// Returns a reference to the Reactor's internal lock.
507 virtual ACE_Lock
&lock (void) = 0;
509 /// Wake up all threads in waiting in the event loop
510 virtual void wakeup_all_threads (void) = 0;
512 /// Transfers ownership of Reactor_Impl to the @a new_owner.
513 virtual int owner (ACE_thread_t new_owner
, ACE_thread_t
*old_owner
= 0) = 0;
515 /// Return the ID of the "owner" thread.
516 virtual int owner (ACE_thread_t
*owner
) = 0;
518 /// Get the existing restart value.
519 virtual bool restart (void) = 0;
521 /// Set a new value for restart and return the original value.
522 virtual bool restart (bool r
) = 0;
524 /// Set position of the owner thread.
525 virtual void requeue_position (int) = 0;
527 /// Get position of the owner thread.
528 virtual int requeue_position (void) = 0;
530 // = Low-level wait_set mask manipulation methods.
532 /// GET/SET/ADD/CLR the dispatch mask "bit" bound with the
533 /// @a event_handler and @a mask.
534 virtual int mask_ops (ACE_Event_Handler
*event_handler
,
535 ACE_Reactor_Mask mask
,
538 /// GET/SET/ADD/CLR the dispatch MASK "bit" bound with the @a handle
540 virtual int mask_ops (ACE_HANDLE handle
,
541 ACE_Reactor_Mask mask
,
544 // = Low-level ready_set mask manipulation methods.
545 /// GET/SET/ADD/CLR the ready "bit" bound with the @a event_handler
547 virtual int ready_ops (ACE_Event_Handler
*event_handler
,
548 ACE_Reactor_Mask mask
,
551 /// GET/SET/ADD/CLR the ready "bit" bound with the @a handle and @a mask.
552 virtual int ready_ops (ACE_HANDLE handle
,
556 /// Dump the state of an object.
557 virtual void dump (void) const = 0;
559 /// Declare the dynamic allocation hooks.
560 ACE_ALLOC_HOOK_DECLARE
;
563 ACE_END_VERSIONED_NAMESPACE_DECL
565 #include /**/ "ace/post.h"
566 #endif /* ACE_REACTOR_IMPL_H */