3 //==========================================================================
5 * @file Event_Handler.h
7 * @author Douglas C. Schmidt <d.schmidt@vanderbilt.edu>
9 //==========================================================================
11 #ifndef ACE_EVENT_HANDLER_H
12 #define ACE_EVENT_HANDLER_H
13 #include /**/ "ace/pre.h"
15 #include /**/ "ace/ACE_export.h"
17 #if !defined (ACE_LACKS_PRAGMA_ONCE)
19 #endif /* ACE_LACKS_PRAGMA_ONCE */
21 #include "ace/os_include/os_signal.h"
22 #include "ace/OS_NS_Thread.h"
25 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
27 // Forward declaration.
28 class ACE_Message_Block
;
30 class ACE_Reactor_Timer_Interface
;
31 class ACE_Thread_Manager
;
34 typedef unsigned long ACE_Reactor_Mask
;
37 * @class ACE_Event_Handler
40 * Provides an abstract interface for handling various types of
41 * I/O, timer, and signal events.
43 * Subclasses read/write input/output on an I/O descriptor,
44 * handle an exception raised on an I/O descriptor, handle a
45 * timer's expiration, or handle a signal.
47 class ACE_Export ACE_Event_Handler
55 #if defined (ACE_USE_POLL)
58 EXCEPT_MASK
= POLLPRI
,
59 #else /* USE SELECT */
61 WRITE_MASK
= (1 << 1),
62 EXCEPT_MASK
= (1 << 2),
63 #endif /* ACE_USE_POLL */
64 ACCEPT_MASK
= (1 << 3),
65 CONNECT_MASK
= (1 << 4),
66 TIMER_MASK
= (1 << 5),
68 GROUP_QOS_MASK
= (1 << 7),
69 SIGNAL_MASK
= (1 << 8),
70 ALL_EVENTS_MASK
= READ_MASK
|
79 RWE_MASK
= READ_MASK
|
85 /// Destructor is virtual to enable proper cleanup.
86 virtual ~ACE_Event_Handler () = default;
88 /// Get the I/O handle.
89 virtual ACE_HANDLE
get_handle () const;
91 /// Set the I/O handle.
92 virtual void set_handle (ACE_HANDLE
);
96 /// Get the priority of the Event_Handler.
97 /// @note Priorities run from MIN_PRIORITY (which is the "lowest priority")
98 /// to MAX_PRIORITY (which is the "highest priority").
99 virtual int priority () const;
101 /// Set the priority of the Event_Handler.
102 virtual void priority (int priority
);
104 /// Called when input events occur (e.g., connection or data).
105 virtual int handle_input (ACE_HANDLE fd
= ACE_INVALID_HANDLE
);
107 /// Called when output events are possible (e.g., when flow control
108 /// abates or non-blocking connection completes).
109 virtual int handle_output (ACE_HANDLE fd
= ACE_INVALID_HANDLE
);
111 /// Called when an exceptional events occur (e.g., SIGURG).
112 virtual int handle_exception (ACE_HANDLE fd
= ACE_INVALID_HANDLE
);
115 * Called when timer expires. @a current_time represents the current
116 * time that the Event_Handler was selected for timeout
117 * dispatching and @a act is the asynchronous completion token that
118 * was passed in when <schedule_timer> was invoked.
120 virtual int handle_timeout (const ACE_Time_Value
¤t_time
,
121 const void *act
= 0);
123 /// Called when a process exits.
124 virtual int handle_exit (ACE_Process
*);
126 /// Called when a handle_*() method returns -1 or when the
127 /// remove_handler() method is called on an ACE_Reactor. The
128 /// @a close_mask indicates which event has triggered the
129 /// handle_close() method callback on a particular @a handle.
130 virtual int handle_close (ACE_HANDLE handle
,
131 ACE_Reactor_Mask close_mask
);
133 /// Called when object is signaled by OS (either via UNIX signals or
134 /// when a Win32 object becomes signaled).
135 virtual int handle_signal (int signum
, siginfo_t
* = 0, ucontext_t
* = 0);
139 /// The handler is not resumed at all. Could lead to deadlock..
140 ACE_EVENT_HANDLER_NOT_RESUMED
= -1,
141 /// The reactor takes responsibility of resuming the handler and
143 ACE_REACTOR_RESUMES_HANDLER
= 0,
144 /// The application takes responsibility of resuming the handler
145 ACE_APPLICATION_RESUMES_HANDLER
149 * Called to figure out whether the handler needs to resumed by the
150 * reactor or the application can take care of it. The default
151 * value of 0 would be returned which would allow the reactor to
152 * take care of resumption of the handler. The application can
153 * return a value more than zero and decide to resume the handler
156 * @note This method has an affect only when used with the
157 * ACE_Dev_Poll_Reactor (and then, only on Linux) or the ACE_TP_Reactor.
159 virtual int resume_handler ();
161 virtual int handle_qos (ACE_HANDLE
= ACE_INVALID_HANDLE
);
162 virtual int handle_group_qos (ACE_HANDLE
= ACE_INVALID_HANDLE
);
164 // = Accessors to set/get the various event demultiplexors.
165 /// Set the event demultiplexors.
166 virtual void reactor (ACE_Reactor
*reactor
);
168 /// Get the event demultiplexors.
169 virtual ACE_Reactor
*reactor () const;
171 /// Get only the reactor's timer related interface.
172 virtual ACE_Reactor_Timer_Interface
*reactor_timer_interface () const;
175 * Used to read from non-socket ACE_HANDLEs in our own thread to
176 * work around Win32 limitations that don't allow us to <select> on
177 * non-sockets (such as ACE_STDIN). This is commonly used in
178 * situations where the Reactor is used to demultiplex read events
179 * on ACE_STDIN on UNIX. Note that @a event_handler must be a
180 * subclass of ACE_Event_Handler. If the get_handle() method of
181 * this event handler returns ACE_INVALID_HANDLE we default to
182 * reading from ACE_STDIN.
184 static ACE_THR_FUNC_RETURN
read_adapter (void *event_handler
);
187 * Abstracts away from the differences between Win32 and ACE with
188 * respect to reading from ACE_STDIN, which is non-<select>'able on
191 static int register_stdin_handler (ACE_Event_Handler
*eh
,
192 ACE_Reactor
*reactor
,
193 ACE_Thread_Manager
*thr_mgr
,
194 int flags
= THR_DETACHED
);
196 /// Performs the inverse of the register_stdin_handler() method.
197 static int remove_stdin_handler (ACE_Reactor
*reactor
,
198 ACE_Thread_Manager
*thr_mgr
);
200 /// Reference count type.
201 typedef long Reference_Count
;
203 /// Increment reference count on the handler.
205 * This method is called when the handler is registered with the
206 * Reactor and when the Reactor makes an upcall on the handler.
207 * Reference count is 1 when the handler is created.
209 * @return Current reference count.
211 virtual Reference_Count
add_reference ();
213 /// Decrement reference count on the handler.
215 * This method is called when the handler is removed from the
216 * Reactor and when an upcall made on the handler by the Reactor
217 * completes. Handler is deleted when the reference count reaches
220 * @return Current reference count.
222 virtual Reference_Count
remove_reference ();
227 * @brief Base class for all handler policies.
229 class ACE_Export Policy
232 /// Virtual destructor.
237 * @class Reference_Counting_Policy
240 * This policy dictates the reference counting requirements
243 * This policy allows applications to configure whether it wants the
244 * Reactor to call add_reference() and remove_reference() during
245 * registrations, removals, and upcalls.
247 * <B>Default:</B> DISABLED.
249 class ACE_Export Reference_Counting_Policy
: public Policy
251 /// This policy can only be created by the handler.
252 friend class ACE_Event_Handler
;
257 /// Perform reference counting.
259 /// Don't perform reference counting.
263 /// Current Reference_Counting_Policy.
264 Value
value () const;
266 /// Update Reference_Counting_Policy.
267 void value (Value value
);
270 /// Private constructor.
271 Reference_Counting_Policy (Value value
);
273 /// The value of the policy.
277 /// Current Reference_Counting_Policy.
278 Reference_Counting_Policy
&reference_counting_policy ();
281 /// Force ACE_Event_Handler to be an abstract base class.
282 ACE_Event_Handler (ACE_Reactor
* = nullptr,
283 int priority
= ACE_Event_Handler::LO_PRIORITY
);
285 /// Typedef for implementation of reference counting.
286 typedef std::atomic
<Reference_Count
> Atomic_Reference_Count
;
289 Atomic_Reference_Count reference_count_
;
292 /// Priority of this Event_Handler.
295 /// Pointer to the various event demultiplexors.
296 ACE_Reactor
*reactor_
;
298 /// Reference counting requirements.
299 Reference_Counting_Policy reference_counting_policy_
;
303 * @class ACE_Event_Handler_var
305 * @brief Auto pointer like class for Event Handlers.
307 * Used to manage lifecycle of handlers. This class calls
308 * ACE_Event_Handler::remove_reference() in its destructor.
310 class ACE_Export ACE_Event_Handler_var
313 /// Default constructor.
314 ACE_Event_Handler_var ();
316 /// Construct with a handler.
317 ACE_Event_Handler_var (ACE_Event_Handler
*p
);
319 /// Copy constructor.
320 ACE_Event_Handler_var (const ACE_Event_Handler_var
&b
);
323 ~ACE_Event_Handler_var ();
325 /// Assignment to a handler.
326 ACE_Event_Handler_var
&operator= (ACE_Event_Handler
*p
);
328 /// Assignment to a ACE_Event_Handler_var.
329 ACE_Event_Handler_var
&operator= (const ACE_Event_Handler_var
&b
);
332 ACE_Event_Handler
*operator-> () const;
334 /// Access the handler.
335 ACE_Event_Handler
*handler () const;
337 /// Release the handler.
338 ACE_Event_Handler
*release ();
340 /// Reset the handler.
341 void reset (ACE_Event_Handler
*p
= 0);
343 /// Bool operator to check if the ACE_Event_Handler_var has a value
344 explicit operator bool() const;
345 /// Equality operator to compare with nullptr_t
346 bool operator ==(std::nullptr_t
) const;
347 /// Not equal operator to compare with nullptr_t
348 bool operator !=(std::nullptr_t
) const;
352 ACE_Event_Handler
*ptr_
;
355 /// Define that we can use in user code to check if this
356 /// helper factory method is there
357 #define ACE_HAS_ACE_MAKE_EVENT_HANDLER
361 /// With C++11 it is common to not use C++ new and delete, but
362 /// use std::make_shared and std::make_unique. This will not
363 /// work for ACE event handlers so we introduce a new
364 /// ACE::make_event_handler which can be used in user code to
365 /// allocate a new ACE event handler instance and directly assign
366 /// it to a ACE_Event_Handler_var
367 /// As user this now makes it for example possible to implement
368 /// the following when Simple_Handler is derived from ACE_Event_Handler
369 /// ACE_Event_Handler_var v =
370 /// ACE::make_event_handler<Simple_Handler> (reactor.get());
373 std::enable_if
<std::is_base_of
<ACE_Event_Handler
, T
>::value
>::type
,
374 typename
...Args
> inline
375 ACE_Event_Handler_var
make_event_handler (Args
&& ...args
)
377 return ACE_Event_Handler_var (new T (std::forward
<Args
> (args
)...));
382 * @class ACE_Notification_Buffer
384 * @brief Simple wrapper for passing ACE_Event_Handler *s and
385 * ACE_Reactor_Masks between threads.
387 class ACE_Export ACE_Notification_Buffer
390 ACE_Notification_Buffer ();
392 ACE_Notification_Buffer (ACE_Event_Handler
*eh
, ACE_Reactor_Mask mask
);
394 /// Default destructor.
395 ~ACE_Notification_Buffer () = default;
397 /// Pointer to the Event_Handler that will be dispatched
398 /// by the main event loop.
399 ACE_Event_Handler
*eh_
;
401 /// Mask that indicates which method to call.
402 ACE_Reactor_Mask mask_
;
405 ACE_END_VERSIONED_NAMESPACE_DECL
407 #include /**/ "ace/post.h"
408 #endif /* ACE_EVENT_HANDLER_H */