/* -*- C++ -*- */ //============================================================================= /** * @file Proactor.h * * $Id$ * * @author Irfan Pyarali * @author Tim Harrison * @author Alexander Babu Arulanthu * @author Alexander Libman */ //============================================================================= #ifndef ACE_PROACTOR_H #define ACE_PROACTOR_H #include "ace/pre.h" #include "ace/config-all.h" #include "ace/ACE_export.h" #include "ace/OS.h" #if !defined (ACE_LACKS_PRAGMA_ONCE) #pragma once #endif /* ACE_LACKS_PRAGMA_ONCE */ #if ((defined (ACE_WIN32) && !defined (ACE_HAS_WINCE)) || (defined (ACE_HAS_AIO_CALLS))) // This only works on Win32 platforms and on Unix platforms supporting // POSIX aio calls. # include "ace/Asynch_IO.h" # include "ace/Asynch_IO_Impl.h" # include "ace/Thread_Manager.h" # include "ace/Timer_Queue.h" # include "ace/Timer_List.h" # include "ace/Timer_Heap.h" # include "ace/Timer_Wheel.h" // Forward declarations. class ACE_Proactor_Impl; class ACE_Proactor_Timer_Handler; /** * @class ACE_Proactor_Handle_Timeout_Upcall * * @brief Functor for . * * This class implements the functor required by the Timer * Queue to call on ACE_Handlers. */ class ACE_Export ACE_Proactor_Handle_Timeout_Upcall { /// Type def for the timer queue. typedef ACE_Timer_Queue_T TIMER_QUEUE; /// The main Proactor class has special permissions. friend class ACE_Proactor; public: /// Constructor. ACE_Proactor_Handle_Timeout_Upcall (void); /// This method is called when the timer expires. int timeout (TIMER_QUEUE &timer_queue, ACE_Handler *handler, const void *arg, const ACE_Time_Value &cur_time); /// This method is called when the timer is canceled. int cancellation (TIMER_QUEUE &timer_queue, ACE_Handler *handler); /// This method is called when the timer queue is destroyed and the /// timer is still contained in it. int deletion (TIMER_QUEUE &timer_queue, ACE_Handler *handler, const void *arg); protected: /// Set the proactor. This will fail, if one is already set! int proactor (ACE_Proactor &proactor); /// Handle to the proactor. This is needed for posting a timer result /// to the Proactor's completion queue. ACE_Proactor *proactor_; }; /** * @class ACE_Proactor * * @brief A manager for asynchronous event demultiplexing. * * See the Proactor pattern description at * http://www.cs.wustl.edu/~schmidt/proactor.ps.gz for more * details. */ class ACE_Export ACE_Proactor { // = Here are the private typedefs that the uses. typedef ACE_Timer_Queue_Iterator_T TIMER_QUEUE_ITERATOR; typedef ACE_Timer_List_T TIMER_LIST; typedef ACE_Timer_List_Iterator_T TIMER_LIST_ITERATOR; typedef ACE_Timer_Heap_T TIMER_HEAP; typedef ACE_Timer_Heap_Iterator_T TIMER_HEAP_ITERATOR; typedef ACE_Timer_Wheel_T TIMER_WHEEL; typedef ACE_Timer_Wheel_Iterator_T TIMER_WHEEL_ITERATOR; // = Friendship. /// Timer handler runs a thread and manages the timers, on behalf of /// the Proactor. friend class ACE_Proactor_Timer_Handler; public: /// Public type. typedef ACE_Timer_Queue_T TIMER_QUEUE; /** * Constructor. If is 0, the correct implementation * object will be created. flag determines * whether the implementation object should be deleted by the * Proactor or not. If is 0, a new TIMER_QUEUE is created. */ ACE_Proactor (ACE_Proactor_Impl *implementation = 0, int delete_implementation = 0, TIMER_QUEUE *tq = 0); /// Virtual destruction. virtual ~ACE_Proactor (void); /// Get pointer to a process-wide . should /// be part of another method. static ACE_Proactor *instance (size_t threads = 0); /// Set pointer to a process-wide and return existing /// pointer. static ACE_Proactor *instance (ACE_Proactor * proactor, int delete_proactor = 0); /// Delete the dynamically allocated Singleton. static void close_singleton (void); /// Cleanup method, used by the to destroy the /// singleton. static void cleanup (void *instance, void *arg); /// Name of dll in which the singleton instance lives. static const ACE_TCHAR *dll_name (void); /// Name of component--ACE_Proactor in this case. static const ACE_TCHAR *name (void); // = Proactor event loop management methods. /// Run the event loop until the method /// returns -1 or the method is invoked. static int run_event_loop (void); /** * Run the event loop until the method * returns -1, the method is invoked, or the * expires. */ static int run_event_loop (ACE_Time_Value &tv); /** * Instruct the to terminate its event * loop. * This method wakes up all the threads blocked on waiting for * completions and end the event loop. */ static int end_event_loop (void); /** * Resets the static so that the * method can be restarted. */ static int reset_event_loop (void); /** * The singleton proactor is used by the . * Therefore, we must check for the reconfiguration request and * handle it after handling an event. */ static int check_reconfiguration (ACE_Proactor *); /// Report if the event loop is finished. static int event_loop_done (void); /// Close the associated @c ACE_Proactor_Impl implementation object. /** * If @arg delete_implementation was specified to the @c open() method, * the implementation object is also deleted. */ virtual int close (void); /** * You can add a hook to various run_event methods and the hook will * be called after handling every proactor event. If this function * returns 0, proactor_run_event_loop will check for the return value of * handle_events. If it is -1, the the proactor_run_event_loop will return * (pre-maturely.) */ typedef int (*PROACTOR_EVENT_HOOK)(ACE_Proactor *); // These methods work with an instance of a proactor. /** * Run the event loop until the * * method returns -1 or the method is invoked. */ virtual int proactor_run_event_loop (PROACTOR_EVENT_HOOK = 0); /** * Run the event loop until the * method returns -1, the * method is invoked, * or the * expires. */ virtual int proactor_run_event_loop (ACE_Time_Value &tv, PROACTOR_EVENT_HOOK = 0); /** * Instruct the ACE_Proactor to terminate its event loop * and notifies the ACE_Proactor so that it can wake up * and close down gracefully. */ virtual int proactor_end_event_loop (void); /// Report if the ACE_Proactor event loop is finished. virtual int proactor_event_loop_done (void); /// Resets the static so that the /// method can be restarted. virtual int proactor_reset_event_loop (void); /// This method adds the to the I/O completion port. This /// function is a no-op function for Unix systems and returns 0; virtual int register_handle (ACE_HANDLE handle, const void *completion_key); // = Timer management. /** * Schedule a that will expire after