3 //=============================================================================
5 * @file POSIX_Proactor.h
7 * $Id: POSIX_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 Roger Tragin <r.tragin@computer.org>
13 * @author Alexander Libman <alibman@baltimore.com>
15 //=============================================================================
17 #ifndef ACE_POSIX_PROACTOR_H
18 #define ACE_POSIX_PROACTOR_H
20 #include /**/ "ace/config-all.h"
22 #if !defined (ACE_LACKS_PRAGMA_ONCE)
24 #endif /* ACE_LACKS_PRAGMA_ONCE */
26 #if defined (ACE_HAS_AIO_CALLS)
27 // POSIX implementation of Proactor depends on the <aio_> family of
30 #include "ace/Proactor_Impl.h"
31 #include "ace/Free_List.h"
33 #include "ace/POSIX_Asynch_IO.h"
34 #include "ace/Asynch_Pseudo_Task.h"
36 #define ACE_AIO_MAX_SIZE 2048
37 #define ACE_AIO_DEFAULT_SIZE 1024
39 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
42 * @class ACE_POSIX_Proactor
44 * @brief POSIX implementation of the Proactor.
46 * There are two different strategies by which Proactor can get
47 * to know the completion of <aio> operations. One is based on
48 * Asynchronous I/O Control Blocks (AIOCB) where a list of
49 * AIOCBs are stored and completion status of the corresponding
50 * operations are queried on them. The other one is based on
51 * POSIX Real Time signals. This class abstracts out the common
52 * code needed for both the strategies. ACE_POSIX_AIOCB_Proactor and
53 * ACE_POSIX_SIG_Proactor specialize this class for each strategy.
55 class ACE_Export ACE_POSIX_Proactor
: public ACE_Proactor_Impl
63 /// Aio_suspend() based
66 /// Signals notifications
69 /// SUN specific aiowait()
72 /// Callback notifications
77 enum SystemType
// open for future extention
79 ACE_OS_UNDEFINED
= 0x0000,
80 ACE_OS_WIN
= 0x0100, // for future
81 ACE_OS_WIN_NT
= ACE_OS_WIN
| 0x0001,
82 ACE_OS_WIN_2000
= ACE_OS_WIN
| 0x0002,
83 ACE_OS_SUN
= 0x0200, // Sun Solaris family
84 ACE_OS_SUN_55
= ACE_OS_SUN
| 0x0001,
85 ACE_OS_SUN_56
= ACE_OS_SUN
| 0x0002,
86 ACE_OS_SUN_57
= ACE_OS_SUN
| 0x0004,
87 ACE_OS_SUN_58
= ACE_OS_SUN
| 0x0008,
88 ACE_OS_HPUX
= 0x0400, // HPUX family
89 ACE_OS_HPUX_11
= ACE_OS_HPUX
| 0x0001,
90 ACE_OS_LINUX
= 0x0800, // Linux family
91 ACE_OS_FREEBSD
= 0x1000, // FreeBSD family
92 ACE_OS_IRIX
= 0x2000, // SGI IRIX family
93 ACE_OS_OPENBSD
= 0x4000 // OpenBSD familty
101 virtual Proactor_Type
get_impl_type (void);
103 /// Virtual destructor.
104 virtual ~ACE_POSIX_Proactor (void);
106 /// Close down the Proactor.
107 virtual int close (void);
110 * Dispatch a single set of events. If @a wait_time elapses before
111 * any events occur, return 0. Return 1 on success i.e., when a
112 * completion is dispatched, non-zero (-1) on errors and errno is
115 virtual int handle_events (ACE_Time_Value
&wait_time
) = 0;
118 * Block indefinitely until at least one event is dispatched.
119 * Dispatch a single set of events.Return 1 on success i.e., when a
120 * completion is dispatched, non-zero (-1) on errors and errno is
123 virtual int handle_events (void) = 0;
126 * Post a result to the completion port of the Proactor. If errors
127 * occur, the result will be deleted by this method. If successful,
128 * the result will be deleted by the Proactor when the result is
129 * removed from the completion port. Therefore, the result should
130 * have been dynamically allocated and should be orphaned by the
131 * user once this method is called.
133 virtual int post_completion (ACE_POSIX_Asynch_Result
*result
) = 0;
135 virtual int start_aio (ACE_POSIX_Asynch_Result
*result
, Opcode op
) = 0;
137 virtual int cancel_aio (ACE_HANDLE h
) = 0;
139 /// Task to process pseudo-asynchronous operations
140 ACE_Asynch_Pseudo_Task
&get_asynch_pseudo_task ();
142 /// This function is a no-op function for Unix systems. Returns 0.
143 virtual int register_handle (ACE_HANDLE handle
,
144 const void *completion_key
);
146 /// @@ This is a no-op on POSIX platforms. Returns 0.
147 int wake_up_dispatch_threads (void);
149 /// @@ This is a no-op on POSIX platforms. Returns 0.
150 int close_dispatch_threads (int wait
);
152 /// @@ This is a no-op on POSIX platforms. Returns 0.
153 size_t number_of_threads (void) const;
154 void number_of_threads (size_t threads
);
156 /// This is a no-op in POSIX. Returns ACE_INVALID_HANDLE.
157 virtual ACE_HANDLE
get_handle (void) const;
159 // Methods used to create Asynch IO factory and result objects. We
160 // create the right objects here in these methods.
162 virtual ACE_Asynch_Read_Stream_Impl
*create_asynch_read_stream (void);
163 virtual ACE_Asynch_Read_Stream_Result_Impl
*
164 create_asynch_read_stream_result (const ACE_Handler::Proxy_Ptr
&handler_proxy
,
166 ACE_Message_Block
&message_block
,
167 size_t bytes_to_read
,
169 ACE_HANDLE event
= ACE_INVALID_HANDLE
,
171 int signal_number
= ACE_SIGRTMIN
);
173 virtual ACE_Asynch_Write_Stream_Impl
*create_asynch_write_stream (void);
174 virtual ACE_Asynch_Write_Stream_Result_Impl
*
175 create_asynch_write_stream_result (const ACE_Handler::Proxy_Ptr
&handler_proxy
,
177 ACE_Message_Block
&message_block
,
178 size_t bytes_to_write
,
180 ACE_HANDLE event
= ACE_INVALID_HANDLE
,
182 int signal_number
= ACE_SIGRTMIN
);
184 virtual ACE_Asynch_Read_File_Impl
*create_asynch_read_file (void);
185 virtual ACE_Asynch_Read_File_Result_Impl
*
186 create_asynch_read_file_result (const ACE_Handler::Proxy_Ptr
&handler_proxy
,
188 ACE_Message_Block
&message_block
,
189 size_t bytes_to_read
,
193 ACE_HANDLE event
= ACE_INVALID_HANDLE
,
195 int signal_number
= ACE_SIGRTMIN
);
197 virtual ACE_Asynch_Write_File_Impl
*create_asynch_write_file (void);
198 virtual ACE_Asynch_Write_File_Result_Impl
*
199 create_asynch_write_file_result (const ACE_Handler::Proxy_Ptr
&handler_proxy
,
201 ACE_Message_Block
&message_block
,
202 size_t bytes_to_write
,
206 ACE_HANDLE event
= ACE_INVALID_HANDLE
,
208 int signal_number
= ACE_SIGRTMIN
);
210 virtual ACE_Asynch_Read_Dgram_Impl
*create_asynch_read_dgram (void);
211 virtual ACE_Asynch_Read_Dgram_Result_Impl
*
212 create_asynch_read_dgram_result (const ACE_Handler::Proxy_Ptr
&handler_proxy
,
214 ACE_Message_Block
*message_block
,
215 size_t bytes_to_read
,
219 ACE_HANDLE event
= ACE_INVALID_HANDLE
,
221 int signal_number
= ACE_SIGRTMIN
);
223 virtual ACE_Asynch_Write_Dgram_Impl
*create_asynch_write_dgram (void);
224 virtual ACE_Asynch_Write_Dgram_Result_Impl
*
225 create_asynch_write_dgram_result (const ACE_Handler::Proxy_Ptr
&handler_proxy
,
227 ACE_Message_Block
*message_block
,
228 size_t bytes_to_write
,
231 ACE_HANDLE event
= ACE_INVALID_HANDLE
,
233 int signal_number
= ACE_SIGRTMIN
);
235 virtual ACE_Asynch_Accept_Impl
*create_asynch_accept (void);
236 virtual ACE_Asynch_Accept_Result_Impl
*
237 create_asynch_accept_result (const ACE_Handler::Proxy_Ptr
&handler_proxy
,
238 ACE_HANDLE listen_handle
,
239 ACE_HANDLE accept_handle
,
240 ACE_Message_Block
&message_block
,
241 size_t bytes_to_read
,
243 ACE_HANDLE event
= ACE_INVALID_HANDLE
,
245 int signal_number
= ACE_SIGRTMIN
);
247 virtual ACE_Asynch_Connect_Impl
*create_asynch_connect (void);
248 virtual ACE_Asynch_Connect_Result_Impl
*
249 create_asynch_connect_result (const ACE_Handler::Proxy_Ptr
&handler_proxy
,
250 ACE_HANDLE connect_handle
,
252 ACE_HANDLE event
= ACE_INVALID_HANDLE
,
254 int signal_number
= ACE_SIGRTMIN
);
256 virtual ACE_Asynch_Transmit_File_Impl
*create_asynch_transmit_file (void);
257 virtual ACE_Asynch_Transmit_File_Result_Impl
*
258 create_asynch_transmit_file_result (const ACE_Handler::Proxy_Ptr
&handler_proxy
,
261 ACE_Asynch_Transmit_File::Header_And_Trailer
*header_and_trailer
,
262 size_t bytes_to_write
,
265 size_t bytes_per_send
,
268 ACE_HANDLE event
= ACE_INVALID_HANDLE
,
270 int signal_number
= ACE_SIGRTMIN
);
272 /// Create a timer result object which can be used with the Timer
273 /// mechanism of the Proactor.
274 virtual ACE_Asynch_Result_Impl
*
275 create_asynch_timer (const ACE_Handler::Proxy_Ptr
&handler_proxy
,
277 const ACE_Time_Value
&tv
,
278 ACE_HANDLE event
= ACE_INVALID_HANDLE
,
280 int signal_number
= ACE_SIGRTMIN
);
284 ACE_POSIX_Proactor (void);
287 * Protect against structured exceptions caused by user code when
288 * dispatching handles. The <completion_key> is not very useful
289 * compared to <AST> that can be associated each asynchronous
290 * operation. <completion_key> is implemented right now for the
293 void application_specific_code (ACE_POSIX_Asynch_Result
*asynch_result
,
294 size_t bytes_transferred
,
295 const void *completion_key
,
299 * Post <how_many> completions to the completion port so that all
300 * threads can wake up. This is used in conjunction with the
303 virtual int post_wakeup_completions (int how_many
);
306 /// Handler to handle the wakeups. This works in conjunction with the
307 /// <ACE_Proactor::run_event_loop>.
308 ACE_Handler wakeup_handler_
;
312 /// Task to process pseudo-asynchronous accept/connect
313 ACE_Asynch_Pseudo_Task pseudo_task_
;
317 // Forward declarations.
318 class ACE_AIOCB_Notify_Pipe_Manager
;
321 * @class ACE_POSIX_AIOCB_Proactor
323 * @brief This Proactor makes use of Asynchronous I/O Control Blocks
324 * (AIOCB) to notify/get the completion status of the <aio_>
327 class ACE_Export ACE_POSIX_AIOCB_Proactor
: public ACE_POSIX_Proactor
330 /// Handler needs to call application specific code.
331 friend class ACE_AIOCB_Notify_Pipe_Manager
;
333 /// This class does the registering of Asynch Operations with the
334 /// Proactor which is necessary in the AIOCB strategy.
335 friend class ACE_POSIX_Asynch_Operation
;
336 friend class ACE_POSIX_Asynch_Accept
;
337 friend class ACE_POSIX_Asynch_Connect
;
341 /// Constructor defines max number asynchronous operations
342 /// which can be started at the same time
343 ACE_POSIX_AIOCB_Proactor (size_t nmaxop
= ACE_AIO_DEFAULT_SIZE
);
345 virtual Proactor_Type
get_impl_type (void);
348 virtual ~ACE_POSIX_AIOCB_Proactor (void);
350 /// Close down the Proactor.
351 virtual int close (void);
354 * Dispatch a single set of events. If @a wait_time elapses before
355 * any events occur, return 0. Return 1 on success i.e., when a
356 * completion is dispatched, non-zero (-1) on errors and errno is
359 virtual int handle_events (ACE_Time_Value
&wait_time
);
362 * Block indefinitely until at least one event is dispatched.
363 * Dispatch a single set of events. If @a wait_time elapses before
364 * any events occur, return 0. Return 1 on success i.e., when a
365 * completion is dispatched, non-zero (-1) on errors and errno is
368 virtual int handle_events (void);
370 /// Post a result to the completion port of the Proactor.
371 virtual int post_completion (ACE_POSIX_Asynch_Result
*result
);
373 virtual int start_aio (ACE_POSIX_Asynch_Result
*result
,
374 ACE_POSIX_Proactor::Opcode op
);
377 * This method should be called from
378 * ACE_POSIX_Asynch_Operation::cancel()
379 * instead of usual ::aio_cancel.
380 * For all deferred AIO requests with handle "h"
381 * it removes its from the lists and notifies user.
382 * For all running AIO requests with handle "h"
383 * it calls ::aio_cancel. According to the POSIX standards
384 * we will receive ECANCELED for all ::aio_canceled AIO requests
385 * later on return from ::aio_suspend
387 virtual int cancel_aio (ACE_HANDLE h
);
391 /// Special constructor for ACE_SUN_Proactor
392 /// and ACE_POSIX_SIG_Proactor
393 ACE_POSIX_AIOCB_Proactor (size_t nmaxop
,
394 ACE_POSIX_Proactor::Proactor_Type ptype
);
396 /// Check AIO for completion, error and result status
397 /// Return: 1 - AIO completed , 0 - not completed yet
398 virtual int get_result_status (ACE_POSIX_Asynch_Result
*asynch_result
,
400 size_t &transfer_count
);
402 /// Create aiocb list
403 int create_result_aiocb_list (void);
405 /// Call this method from derived class when virtual table is
407 int delete_result_aiocb_list (void);
409 /// Call these methods from derived class when virtual table is
411 void create_notify_manager (void);
412 void delete_notify_manager (void);
414 /// Define the maximum number of asynchronous I/O requests
415 /// for the current OS
416 void check_max_aio_num (void) ;
418 /// To identify requests from Notify_Pipe_Manager
419 void set_notify_handle (ACE_HANDLE h
);
422 * Dispatch a single set of events. If <milli_seconds> elapses
423 * before any events occur, return 0. Return 1 if a completion
424 * dispatched. Return -1 on errors.
426 int handle_events_i (u_long milli_seconds
);
428 /// Start deferred AIO if necessary
429 int start_deferred_aio (void);
431 /// Cancel running or deferred AIO
432 virtual int cancel_aiocb (ACE_POSIX_Asynch_Result
* result
);
434 /// Extract the results of aio.
435 ACE_POSIX_Asynch_Result
*find_completed_aio (int &error_status
,
436 size_t &transfer_count
,
440 /// Find free slot to store result and aiocb pointer
441 virtual ssize_t
allocate_aio_slot (ACE_POSIX_Asynch_Result
*result
);
443 /// Initiate an aio operation.
444 virtual int start_aio_i (ACE_POSIX_Asynch_Result
*result
);
446 /// Notify queue of "post_completed" ACE_POSIX_Asynch_Results
447 /// called from post_completion method
448 virtual int notify_completion (int sig_num
);
450 /// Put "post_completed" result into the internal queue
451 int putq_result (ACE_POSIX_Asynch_Result
*result
);
453 /// Get "post_completed" result from the internal queue
454 ACE_POSIX_Asynch_Result
* getq_result (void);
456 /// Clear the internal results queue
457 int clear_result_queue (void);
459 /// Process the internal results queue
460 int process_result_queue (void);
463 /// This class takes care of doing <accept> when we use
464 /// AIO_CONTROL_BLOCKS strategy.
465 ACE_AIOCB_Notify_Pipe_Manager
*aiocb_notify_pipe_manager_
;
467 /// Use a dynamically allocated array to keep track of all the aio's
468 /// issued currently.
470 ACE_POSIX_Asynch_Result
**result_list_
;
472 /// To maintain the maximum size of the array (list).
473 size_t aiocb_list_max_size_
;
475 /// To maintain the current size of the array (list).
476 size_t aiocb_list_cur_size_
;
478 /// Mutex to protect work with lists.
479 ACE_SYNCH_MUTEX mutex_
;
481 /// The purpose of this member is only to identify asynchronous request
482 /// from NotifyManager. We will reserve for it always slot 0
483 /// in the list of aiocb's to be sure that don't lose notifications.
484 ACE_HANDLE notify_pipe_read_handle_
;
486 /// Number of ACE_POSIX_Asynch_Result's waiting for start
487 /// i.e. deferred AIOs
488 size_t num_deferred_aiocb_
;
490 /// Number active,i.e. running requests
491 size_t num_started_aio_
;
493 /// Queue which keeps "post_completed" ACE_POSIX_Asynch_Result's
494 ACE_Unbounded_Queue
<ACE_POSIX_Asynch_Result
*> result_queue_
;
497 #if defined(ACE_HAS_POSIX_REALTIME_SIGNALS)
499 * @class ACE_POSIX_SIG_Proactor
501 * @brief This Proactor implementation does completion event detection using
502 * POSIX Real Time signals. @c sigtimedwait() or @c sigwaitinfo() is
503 * used to wait for completions.
504 * The real-time signals that are going to be used with this
505 * Proactor should be given apriori in the constructor, so that
506 * those signals can be masked from asynchronous delivery.
508 class ACE_Export ACE_POSIX_SIG_Proactor
: public ACE_POSIX_AIOCB_Proactor
512 * This class does the registering of Asynch Operations with the
513 * Proactor which is necessary in the SIG strategy, because we need
514 * to store the signal number.
516 friend class ACE_POSIX_SIG_Asynch_Operation
;
520 * This constructor masks only the <ACE_SIGRTMIN>
521 * real-time signal. Only this signal should be used to issue
522 * asynchronous operations using this Proctor.
524 ACE_POSIX_SIG_Proactor (size_t nmaxop
= ACE_AIO_DEFAULT_SIZE
);
526 virtual Proactor_Type
get_impl_type (void);
529 * This constructor should be used to tell the Proactor to mask and
530 * wait for the real-time signals specified in this set. Only these
531 * signals should be used by the asynchronous operations when they
534 ACE_POSIX_SIG_Proactor (const sigset_t mask_set
,
535 size_t nmaxop
= ACE_AIO_DEFAULT_SIZE
);
538 virtual ~ACE_POSIX_SIG_Proactor (void);
541 * Dispatch a single set of events. If @a wait_time elapses before
542 * any events occur, return 0. Return 1 on success i.e., when a
543 * completion is dispatched, non-zero (-1) on errors and errno is
546 virtual int handle_events (ACE_Time_Value
&wait_time
);
549 * Block indefinitely until at least one event is dispatched.
550 * Dispatch a single set of events. If <wait_time> elapses before
551 * any events occur, return 0. Return 1 on success i.e., when a
552 * completion is dispatched, non-zero (-1) on errors and errno is
555 virtual int handle_events (void);
557 /// Post a result to the completion port of the Proactor.
558 /// now it is implemented in base ACE_POSIX_AIOCB_Proactor class
559 ///virtual int post_completion (ACE_POSIX_Asynch_Result *result);
562 * If @a signal_number is -1, check with the Proactor and use one of
563 * the signals that is present in the mask set (i.e., the signals for
564 * which the Proactor will be waiting) of the Proactor. If there are
565 * more than one signal, the higher numbered signal will be chosen.
567 virtual ACE_Asynch_Result_Impl
*create_asynch_timer
568 (const ACE_Handler::Proxy_Ptr
&handler_proxy
,
570 const ACE_Time_Value
&tv
,
571 ACE_HANDLE event
= ACE_INVALID_HANDLE
,
573 int signal_number
= ACE_SIGRTMIN
);
576 /// To setup the handler for a real-time signbal.
577 int setup_signal_handler (int signal_number
) const;
579 /// Insures that RT_completion_signals_ are blocked in the calling thread.
580 int block_signals (void) const;
583 * Dispatch a single set of events. @a timeout is a pointer to a
584 * relative time representing the maximum amount of time to wait for
585 * an event to occur. If 0, wait indefinitely.
587 * @retval 0 A timeout occurred before any event was detected.
588 * @retval 1 A completion was dispatched.
589 * @retval -1 An error occurred; errno contains an error code.
591 virtual int handle_events_i (const ACE_Time_Value
*timeout
);
593 /// Find free slot to store result and aiocb pointer
594 virtual ssize_t
allocate_aio_slot (ACE_POSIX_Asynch_Result
*result
);
596 /// Notify queue of "post_completed" ACE_POSIX_Asynch_Results
597 /// called from post_completion method
598 virtual int notify_completion (int sig_num
);
601 * These signals are used for completion notification by the
602 * Proactor. The signals specified while issuing asynchronous
603 * operations are stored here in this set. These signals are masked
604 * for a thread when it calls handle_events().
606 sigset_t RT_completion_signals_
;
610 #endif /* ACE_HAS_POSIX_REALTIME_SIGNALS */
613 * @class ACE_POSIX_Asynch_Timer
615 * @brief This class is posted to the completion port when a timer
616 * expires. When the @c complete() method of this object is
617 * called, the handler's @c handle_timeout() method will be
620 class ACE_Export ACE_POSIX_Asynch_Timer
: public ACE_POSIX_Asynch_Result
623 /// The factory method for this class is with the POSIX_Proactor
625 friend class ACE_POSIX_Proactor
;
626 #if defined(ACE_HAS_POSIX_REALTIME_SIGNALS)
627 friend class ACE_POSIX_SIG_Proactor
;
632 ACE_POSIX_Asynch_Timer (const ACE_Handler::Proxy_Ptr
&handler_proxy
,
634 const ACE_Time_Value
&tv
,
635 ACE_HANDLE event
= ACE_INVALID_HANDLE
,
637 int signal_number
= ACE_SIGRTMIN
);
640 virtual ~ACE_POSIX_Asynch_Timer (void) {}
642 /// This method calls the handler's handle_timeout method.
643 virtual void complete (size_t bytes_transferred
,
645 const void *completion_key
,
648 /// Time value requested by caller
649 ACE_Time_Value time_
;
652 ACE_END_VERSIONED_NAMESPACE_DECL
654 #if defined (__ACE_INLINE__)
655 #include "ace/POSIX_Proactor.inl"
656 #endif /* __ACE_INLINE__ */
658 #endif /* ACE_HAS_AIO_CALLS && ACE_HAS_POSIX_REALTIME_SIGNALS */
659 #endif /* ACE_POSIX_PROACTOR_H */