3 //=============================================================================
7 * $Id: Proactor.h 80826 2008-03-04 14:51:23Z wotte $
9 * @author Irfan Pyarali <irfan@cs.wustl.edu>
10 * @author Tim Harrison <harrison@cs.wustl.edu>
11 * @author Alexander Babu Arulanthu <alex@cs.wustl.edu>
12 * @author Alexander Libman <alibman@ihug.com.au>
14 //=============================================================================
16 #ifndef ACE_PROACTOR_H
17 #define ACE_PROACTOR_H
19 #include /**/ "ace/pre.h"
21 #include /**/ "ace/config-all.h"
22 #include /**/ "ace/ACE_export.h"
24 #if !defined (ACE_LACKS_PRAGMA_ONCE)
26 #endif /* ACE_LACKS_PRAGMA_ONCE */
28 #if defined (ACE_HAS_WIN32_OVERLAPPED_IO) || defined (ACE_HAS_AIO_CALLS)
29 // This only works on Win32 platforms and on Unix platforms supporting
32 # include "ace/Asynch_IO.h"
33 # include "ace/Asynch_IO_Impl.h"
34 # include "ace/Thread_Manager.h"
35 # include "ace/Timer_Queue.h"
36 # include "ace/Timer_List.h"
37 # include "ace/Timer_Heap.h"
38 # include "ace/Timer_Wheel.h"
40 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
42 // Forward declarations.
43 class ACE_Proactor_Impl
;
44 class ACE_Proactor_Timer_Handler
;
47 * @class ACE_Proactor_Handle_Timeout_Upcall
49 * @brief Functor for ACE_Timer_Queue.
51 * This class implements the functor required by the Timer
52 * Queue to call <handle_timeout> on ACE_Handlers.
54 class ACE_Proactor_Handle_Timeout_Upcall
57 /// Type def for the timer queue.
58 typedef ACE_Timer_Queue_T
<ACE_Handler
*,
59 ACE_Proactor_Handle_Timeout_Upcall
,
60 ACE_SYNCH_RECURSIVE_MUTEX
>
63 /// The main Proactor class has special permissions.
64 friend class ACE_Proactor
;
68 ACE_Proactor_Handle_Timeout_Upcall (void);
70 /// This method is called when a timer is registered.
71 int registration (TIMER_QUEUE
&timer_queue
,
75 /// This method is called before the timer expires.
76 int preinvoke (TIMER_QUEUE
&timer_queue
,
80 const ACE_Time_Value
&cur_time
,
81 const void *&upcall_act
);
83 /// This method is called when the timer expires.
84 int timeout (TIMER_QUEUE
&timer_queue
,
88 const ACE_Time_Value
&cur_time
);
90 /// This method is called after the timer expires.
91 int postinvoke (TIMER_QUEUE
&timer_queue
,
95 const ACE_Time_Value
&cur_time
,
96 const void *upcall_act
);
98 /// This method is called when a handler is canceled.
99 int cancel_type (TIMER_QUEUE
&timer_queue
,
100 ACE_Handler
*handler
,
101 int dont_call_handle_close
,
102 int &requires_reference_counting
);
104 /// This method is called when a timer is canceled.
105 int cancel_timer (TIMER_QUEUE
&timer_queue
,
106 ACE_Handler
*handler
,
107 int dont_call_handle_close
,
108 int requires_reference_counting
);
110 /// This method is called when the timer queue is destroyed and the
111 /// timer is still contained in it.
112 int deletion (TIMER_QUEUE
&timer_queue
,
113 ACE_Handler
*handler
,
117 /// Set the proactor. This will fail, if one is already set!
118 int proactor (ACE_Proactor
&proactor
);
120 /// Handle to the proactor. This is needed for posting a timer result
121 /// to the Proactor's completion queue.
122 ACE_Proactor
*proactor_
;
126 * @class ACE_Proactor
128 * @brief A manager for asynchronous event demultiplexing.
130 * See the Proactor pattern description at
131 * http://www.cs.wustl.edu/~schmidt/proactor.ps.gz for more
134 class ACE_Export ACE_Proactor
136 // = Here are the private typedefs that the ACE_Proactor uses.
138 typedef ACE_Timer_Queue_Iterator_T
<ACE_Handler
*,
139 ACE_Proactor_Handle_Timeout_Upcall
,
140 ACE_SYNCH_RECURSIVE_MUTEX
>
141 TIMER_QUEUE_ITERATOR
;
142 typedef ACE_Timer_List_T
<ACE_Handler
*,
143 ACE_Proactor_Handle_Timeout_Upcall
,
144 ACE_SYNCH_RECURSIVE_MUTEX
>
146 typedef ACE_Timer_List_Iterator_T
<ACE_Handler
*,
147 ACE_Proactor_Handle_Timeout_Upcall
,
148 ACE_SYNCH_RECURSIVE_MUTEX
>
150 typedef ACE_Timer_Heap_T
<ACE_Handler
*,
151 ACE_Proactor_Handle_Timeout_Upcall
,
152 ACE_SYNCH_RECURSIVE_MUTEX
>
154 typedef ACE_Timer_Heap_Iterator_T
<ACE_Handler
*,
155 ACE_Proactor_Handle_Timeout_Upcall
,
156 ACE_SYNCH_RECURSIVE_MUTEX
>
158 typedef ACE_Timer_Wheel_T
<ACE_Handler
*,
159 ACE_Proactor_Handle_Timeout_Upcall
,
160 ACE_SYNCH_RECURSIVE_MUTEX
>
162 typedef ACE_Timer_Wheel_Iterator_T
<ACE_Handler
*,
163 ACE_Proactor_Handle_Timeout_Upcall
,
164 ACE_SYNCH_RECURSIVE_MUTEX
>
165 TIMER_WHEEL_ITERATOR
;
169 /// Timer handler runs a thread and manages the timers, on behalf of
171 friend class ACE_Proactor_Timer_Handler
;
175 typedef ACE_Timer_Queue_T
<ACE_Handler
*,
176 ACE_Proactor_Handle_Timeout_Upcall
,
177 ACE_SYNCH_RECURSIVE_MUTEX
>
181 * Constructor. If @a implementation is 0, the correct implementation
182 * object will be created. @a delete_implementation flag determines
183 * whether the implementation object should be deleted by the
184 * Proactor or not. If @a tq is 0, a new TIMER_QUEUE is created.
186 ACE_Proactor (ACE_Proactor_Impl
*implementation
= 0,
187 bool delete_implementation
= false,
188 TIMER_QUEUE
*tq
= 0);
191 ~ACE_Proactor (void);
193 /// Get pointer to a process-wide ACE_Proactor. @a threads should
194 /// be part of another method.
195 static ACE_Proactor
*instance (size_t threads
= 0);
197 /// Set pointer to a process-wide ACE_Proactor and return existing
199 static ACE_Proactor
*instance (ACE_Proactor
* proactor
,
200 bool delete_proactor
= false);
202 /// Delete the dynamically allocated Singleton.
203 static void close_singleton (void);
205 /// Cleanup method, used by the ACE_Object_Manager to destroy the
207 static void cleanup (void *instance
, void *arg
);
209 /// Name of dll in which the singleton instance lives.
210 static const ACE_TCHAR
*dll_name (void);
212 /// Name of component--ACE_Proactor in this case.
213 static const ACE_TCHAR
*name (void);
215 // = Proactor event loop management methods.
217 /// Run the event loop until the <ACE_Proactor::handle_events> method
218 /// returns -1 or the <end_event_loop> method is invoked.
219 static int run_event_loop (void);
222 * Run the event loop until the <ACE_Proactor::handle_events> method
223 * returns -1, the <end_event_loop> method is invoked, or the
224 * ACE_Time_Value expires, in which case 0 is returned.
226 static int run_event_loop (ACE_Time_Value
&tv
);
229 * Instruct the <ACE_Proactor::instance> to terminate its event
231 * This method wakes up all the threads blocked on waiting for
232 * completions and end the event loop.
234 static int end_event_loop (void);
237 * Resets the <ACE_Proactor::end_event_loop_> static so that the
238 * <run_event_loop> method can be restarted.
240 static int reset_event_loop (void);
243 * The singleton proactor is used by the ACE_Service_Config.
244 * Therefore, we must check for the reconfiguration request and
245 * handle it after handling an event.
247 static int check_reconfiguration (ACE_Proactor
*);
249 /// Report if the <ACE_Proactor::instance> event loop is finished.
250 static int event_loop_done (void);
252 /// Close the associated @c ACE_Proactor_Impl implementation object.
254 * If @arg delete_implementation was specified to the @c open() method,
255 * the implementation object is also deleted.
260 * You can add a hook to various run_event methods and the hook will
261 * be called after handling every proactor event. If this function
262 * returns 0, proactor_run_event_loop will check for the return value of
263 * handle_events. If it is -1, the the proactor_run_event_loop will return
266 typedef int (*PROACTOR_EVENT_HOOK
)(ACE_Proactor
*);
268 // These methods work with an instance of a proactor.
270 * Run the event loop until the
271 * <ACE_Proactor::handle_events>
272 * method returns -1 or the <end_proactor_event_loop> method is invoked.
274 int proactor_run_event_loop (PROACTOR_EVENT_HOOK
= 0);
277 * Run the event loop until the <ACE_Proactor::handle_events>
278 * method returns -1, the
279 * <end_proactor_event_loop> method is invoked,
280 * or the ACE_Time_Value
281 * expires, in which case a 0 is returned.
283 int proactor_run_event_loop (ACE_Time_Value
&tv
,
284 PROACTOR_EVENT_HOOK
= 0);
287 * Instruct the ACE_Proactor to terminate its event loop
288 * and notifies the ACE_Proactor so that it can wake up
289 * and close down gracefully.
291 int proactor_end_event_loop (void);
293 /// Report if the ACE_Proactor event loop is finished.
294 int proactor_event_loop_done (void);
296 /// Resets the <ACE_Proactor::end_event_loop_> static so that the
297 /// <run_event_loop> method can be restarted.
298 int proactor_reset_event_loop (void);
301 /// This method adds the @a handle to the I/O completion port. This
302 /// function is a no-op function for Unix systems and returns 0;
303 int register_handle (ACE_HANDLE handle
,
304 const void *completion_key
);
306 // = Timer management.
308 * Schedule a @a handler that will expire after <time>. If it
309 * expires then @a act is passed in as the value to the @a handler's
310 * <handle_timeout> callback method. This method returns a
311 * <timer_id>. This <timer_id> can be used to cancel a timer before
312 * it expires. The cancellation ensures that <timer_ids> are unique
313 * up to values of greater than 2 billion timers. As long as timers
314 * don't stay around longer than this there should be no problems
315 * with accidentally deleting the wrong timer. Returns -1 on
316 * failure (which is guaranteed never to be a valid <timer_id>).
318 long schedule_timer (ACE_Handler
&handler
,
320 const ACE_Time_Value
&time
);
322 long schedule_repeating_timer (ACE_Handler
&handler
,
324 const ACE_Time_Value
&interval
);
326 // Same as above except @a interval it is used to reschedule the
327 // @a handler automatically.
329 /// This combines the above two methods into one. Mostly for backward
331 long schedule_timer (ACE_Handler
&handler
,
333 const ACE_Time_Value
&time
,
334 const ACE_Time_Value
&interval
);
336 /// Cancel all timers associated with this @a handler. Returns number
337 /// of timers cancelled.
338 int cancel_timer (ACE_Handler
&handler
,
339 int dont_call_handle_close
= 1);
342 * Cancel the single <ACE_Handler> that matches the @a timer_id value
343 * (which was returned from the <schedule> method). If @a act is
344 * non-NULL then it will be set to point to the ``magic cookie''
345 * argument passed in when the <Handler> was registered. This makes
346 * it possible to free up the memory and avoid memory leaks.
347 * Returns 1 if cancellation succeeded and 0 if the @a timer_id
350 int cancel_timer (long timer_id
,
351 const void **act
= 0,
352 int dont_call_handle_close
= 1);
355 * Dispatch a single set of events, waiting up to a specified time limit
357 * @param wait_time the time to wait for an event to occur. This is
358 * a relative time. On successful return, the time is updated to
359 * reflect the amount of time spent waiting for event(s) to occur.
360 * @return Returns 0 if no events occur before the wait_time expires.
361 * Returns 1 when a completion is dispatched. On error, returns -1
362 * and sets errno accordingly.
364 int handle_events (ACE_Time_Value
&wait_time
);
367 * Block indefinitely until at least one event is dispatched.
368 * @return Returns 1 when a completion is dispatched. On error, returns -1
369 * and sets errno accordingly.
371 int handle_events (void);
373 /// Add wakeup dispatch threads (reinit).
374 int wake_up_dispatch_threads (void);
376 /// Close all dispatch threads.
377 int close_dispatch_threads (int wait
);
379 /// Get number of thread used as a parameter to CreatIoCompletionPort.
380 size_t number_of_threads (void) const;
382 /// Set number of thread used as a parameter to CreatIoCompletionPort.
383 void number_of_threads (size_t threads
);
386 TIMER_QUEUE
*timer_queue (void) const;
389 void timer_queue (TIMER_QUEUE
*timer_queue
);
392 * Get the event handle.
393 * It is a no-op in POSIX platforms and it returns
394 * ACE_INVALID_HANDLE.
396 ACE_HANDLE
get_handle (void) const;
398 /// Get the implementation class.
399 ACE_Proactor_Impl
*implementation (void) const;
401 // = Factory methods for the operations
403 // Note that the user does not have to use or know about these
406 /// Create the correct implementation class for doing
407 /// Asynch_Read_Stream.
408 ACE_Asynch_Read_Stream_Impl
*create_asynch_read_stream (void);
410 /// Create the correct implementation class for doing
411 /// Asynch_Write_Stream.
412 ACE_Asynch_Write_Stream_Impl
*create_asynch_write_stream (void);
414 /// Create the correct implementation class for doing
415 /// Asynch_Read_File.
416 ACE_Asynch_Read_File_Impl
*create_asynch_read_file (void);
418 /// Create the correct implementation class for doing
419 /// Asynch_Write_File.
420 ACE_Asynch_Write_File_Impl
*create_asynch_write_file (void);
422 /// Create the correct implementation class for doing Asynch_Accept.
423 ACE_Asynch_Accept_Impl
*create_asynch_accept (void);
425 /// Create the correct implementation class for doing Asynch_Connect.
426 ACE_Asynch_Connect_Impl
*create_asynch_connect (void);
428 /// Create the correct implementation class for doing
429 /// Asynch_Transmit_File.
430 ACE_Asynch_Transmit_File_Impl
*create_asynch_transmit_file (void);
432 /// Create the correct implementation class for doing
433 /// Asynch_Read_Dgram.
434 ACE_Asynch_Read_Dgram_Impl
*create_asynch_read_dgram (void);
436 /// Create the correct implementation class for doing
437 /// Asynch_Write_Dgram.
438 ACE_Asynch_Write_Dgram_Impl
*create_asynch_write_dgram (void);
440 // = Factory methods for the results
442 // Note that the user does not have to use or know about these
443 // methods unless they want to "fake" results.
445 /// Create the correct implementation class for
446 /// ACE_Asynch_Read_Stream::Result class.
447 ACE_Asynch_Read_Stream_Result_Impl
*
448 create_asynch_read_stream_result (ACE_Handler::Proxy_Ptr
&handler_proxy
,
450 ACE_Message_Block
&message_block
,
451 u_long bytes_to_read
,
453 ACE_HANDLE event
= ACE_INVALID_HANDLE
,
455 int signal_number
= ACE_SIGRTMIN
);
457 /// Create the correct implementation class for
458 /// ACE_Asynch_Write_Stream::Result.
459 ACE_Asynch_Write_Stream_Result_Impl
*
460 create_asynch_write_stream_result (ACE_Handler::Proxy_Ptr
&handler_proxy
,
462 ACE_Message_Block
&message_block
,
463 u_long bytes_to_write
,
465 ACE_HANDLE event
= ACE_INVALID_HANDLE
,
467 int signal_number
= ACE_SIGRTMIN
);
469 /// Create the correct implementation class for
470 /// ACE_Asynch_Read_File::Result.
471 ACE_Asynch_Read_File_Result_Impl
*
472 create_asynch_read_file_result (ACE_Handler::Proxy_Ptr
&handler_proxy
,
474 ACE_Message_Block
&message_block
,
475 u_long bytes_to_read
,
479 ACE_HANDLE event
= ACE_INVALID_HANDLE
,
481 int signal_number
= ACE_SIGRTMIN
);
483 /// Create the correct implementation class for
484 /// ACE_Asynch_Write_File::Result.
485 ACE_Asynch_Write_File_Result_Impl
*
486 create_asynch_write_file_result (ACE_Handler::Proxy_Ptr
&handler_proxy
,
488 ACE_Message_Block
&message_block
,
489 u_long bytes_to_write
,
493 ACE_HANDLE event
= ACE_INVALID_HANDLE
,
495 int signal_number
= ACE_SIGRTMIN
);
497 /// Create the correct implementation class for
498 /// ACE_Asynch_Read_Dgram::Result.
499 ACE_Asynch_Read_Dgram_Result_Impl
*
500 create_asynch_read_dgram_result (ACE_Handler::Proxy_Ptr
&handler_proxy
,
502 ACE_Message_Block
*message_block
,
503 size_t bytes_to_read
,
507 ACE_HANDLE event
= ACE_INVALID_HANDLE
,
509 int signal_number
= ACE_SIGRTMIN
);
511 /// Create the correct implementation class for
512 /// ACE_Asynch_Write_Dgram::Result.
513 ACE_Asynch_Write_Dgram_Result_Impl
*
514 create_asynch_write_dgram_result (ACE_Handler::Proxy_Ptr
&handler_proxy
,
516 ACE_Message_Block
*message_block
,
517 size_t bytes_to_write
,
520 ACE_HANDLE event
= ACE_INVALID_HANDLE
,
522 int signal_number
= ACE_SIGRTMIN
);
524 /// Create the correct implementation class for ACE_Asynch_Accept::Result.
525 ACE_Asynch_Accept_Result_Impl
*
526 create_asynch_accept_result (ACE_Handler::Proxy_Ptr
&handler_proxy
,
527 ACE_HANDLE listen_handle
,
528 ACE_HANDLE accept_handle
,
529 ACE_Message_Block
&message_block
,
530 u_long bytes_to_read
,
532 ACE_HANDLE event
= ACE_INVALID_HANDLE
,
534 int signal_number
= ACE_SIGRTMIN
);
536 /// Create the correct implementation class for ACE_Asynch_Connect::Result
537 ACE_Asynch_Connect_Result_Impl
*
538 create_asynch_connect_result (ACE_Handler::Proxy_Ptr
&handler_proxy
,
539 ACE_HANDLE connect_handle
,
541 ACE_HANDLE event
= ACE_INVALID_HANDLE
,
543 int signal_number
= ACE_SIGRTMIN
);
545 /// Create the correct implementation class for
546 /// ACE_Asynch_Transmit_File::Result.
547 ACE_Asynch_Transmit_File_Result_Impl
*
548 create_asynch_transmit_file_result (ACE_Handler::Proxy_Ptr
&handler_proxy
,
551 ACE_Asynch_Transmit_File::Header_And_Trailer
*header_and_trailer
,
552 u_long bytes_to_write
,
555 u_long bytes_per_send
,
558 ACE_HANDLE event
= ACE_INVALID_HANDLE
,
560 int signal_number
= ACE_SIGRTMIN
);
563 * Create a timer result object which can be used with the Timer
564 * mechanism of the Proactor.
565 * If @a signal_number is -1, <POSIX_SIG_Proactor> will create a
566 * Timer object with a meaningful signal number, choosing the
567 * largest signal number from the signal mask of the Proactor.
569 ACE_Asynch_Result_Impl
*
570 create_asynch_timer (ACE_Handler::Proxy_Ptr
&handler_proxy
,
572 const ACE_Time_Value
&tv
,
573 ACE_HANDLE event
= ACE_INVALID_HANDLE
,
575 int signal_number
= ACE_SIGRTMIN
);
580 * Post <how_many> completions to the completion port so that all
581 * threads can wake up. This is used in conjunction with the
584 static int post_wakeup_completions (int how_many
);
587 * Post <how_many> completions to the completion port so that all
588 * threads can wake up. This is used in conjunction with the
589 * <proactor_run_event_loop>.
591 int proactor_post_wakeup_completions (int how_many
);
593 /// Set the implementation class.
594 void implementation (ACE_Proactor_Impl
*implementation
);
596 /// Delegation/implementation class that all methods will be
598 ACE_Proactor_Impl
*implementation_
;
600 /// Flag used to indicate whether we are responsible for cleaning up
601 /// the implementation instance.
602 bool delete_implementation_
;
604 /// Pointer to a process-wide ACE_Proactor.
605 static ACE_Proactor
*proactor_
;
607 /// Must delete the <proactor_> if true.
608 static bool delete_proactor_
;
610 /// Handles timeout events.
611 ACE_Proactor_Timer_Handler
*timer_handler_
;
613 /// This will manage the thread in the Timer_Handler.
614 ACE_Thread_Manager thr_mgr_
;
617 TIMER_QUEUE
*timer_queue_
;
619 /// Flag on whether to delete the timer queue.
620 int delete_timer_queue_
;
622 /// Terminate the proactor event loop.
623 sig_atomic_t end_event_loop_
;
625 /// Number of threads in the event loop.
626 sig_atomic_t event_loop_thread_count_
;
628 /// Mutex to protect work with lists.
629 ACE_SYNCH_MUTEX mutex_
;
633 /// Deny access since member-wise won't work...
634 ACE_Proactor (const ACE_Proactor
&);
635 ACE_Proactor
&operator= (const ACE_Proactor
&);
638 ACE_END_VERSIONED_NAMESPACE_DECL
640 # if defined (__ACE_INLINE__)
641 # include "ace/Proactor.inl"
642 # endif /* __ACE_INLINE__ */
644 #else /* NOT WIN32 or POSIX with AIO features. */
646 # include "ace/os_include/os_stddef.h"
647 # include "ace/os_include/os_signal.h"
649 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
651 class ACE_Time_Value
;
653 class ACE_Export ACE_Proactor
656 class Timer_Queue
{};
657 ACE_Proactor (size_t /* number_of_threads */ = 0,
658 Timer_Queue
* /* tq */ = 0) {}
659 ~ACE_Proactor (void) {}
660 int handle_events (void) { return -1; }
661 int handle_events (ACE_Time_Value
&) { return -1; }
663 /// Placeholder to enable compilation on non-Win32 platforms
664 static ACE_Proactor
*instance (size_t threads
= 0);
666 /// Placeholder to enable compilation on non-Win32 platforms
667 static ACE_Proactor
*instance (ACE_Proactor
*);
669 /// Placeholder to enable compilation on non-Win32 platforms
670 static void close_singleton (void);
672 /// Placeholder to enable compilation on non-Win32 platforms
673 static int run_event_loop (void);
675 /// Placeholder to enable compilation on non-Win32 platforms
676 static int run_event_loop (ACE_Time_Value
&tv
);
678 /// Placeholder to enable compilation on non-Win32 platforms
679 static int end_event_loop (void);
681 /// Placeholder to enable compilation on non-Win32 platforms
682 static sig_atomic_t event_loop_done (void);
685 ACE_END_VERSIONED_NAMESPACE_DECL
687 #endif /* ACE_HAS_WIN32_OVERLAPPED_IO || ACE_HAS_AIO_CALLS */
689 #include /**/ "ace/post.h"
691 #endif /* ACE_PROACTOR_H */