Manages reactor (polling thread) state including poll interest, request cache, and timers. More...
#include <Reactor.h>
Public Types | |
enum | Priority { Priority::HIGH = 0, Priority::NORMAL } |
Enumeration for reactor priority. More... | |
Public Member Functions | |
Reactor () | |
Constructor. More... | |
~Reactor () | |
Destructor. More... | |
void | add_request (uint32_t id, IOHandler *handler, DispatchHandler *dh, ClockT::time_point expire) |
Adds a request to request cache and adjusts poll timeout if necessary. More... | |
bool | remove_request (uint32_t id, DispatchHandler *&handler) |
Removes request associated with id More... | |
void | cancel_requests (IOHandler *handler, int32_t error=Error::COMM_BROKEN_CONNECTION) |
Cancels outstanding requests associated with handler More... | |
void | add_timer (ExpireTimer &timer) |
Adds a timer. More... | |
void | cancel_timer (const DispatchHandlerPtr &handler) |
Cancels timers associated with handler . More... | |
void | schedule_removal (IOHandler *handler) |
Schedules handler for removal. More... | |
void | get_removed_handlers (std::set< IOHandler * > &dst) |
Returns set of I/O handlers scheduled for removal. More... | |
void | handle_timeouts (PollTimeout &next_timeout) |
Processes request timeouts and timers. More... | |
int | add_poll_interest (int sd, short events, IOHandler *handler) |
Add poll interest for socket (POSIX poll only). More... | |
int | remove_poll_interest (int sd) |
Remove poll interest for socket (POSIX poll only). More... | |
int | modify_poll_interest (int sd, short events) |
Modify poll interest for socket (POSIX poll only). More... | |
void | fetch_poll_array (std::vector< struct pollfd > &fdarray, std::vector< IOHandler * > &handlers) |
Fetches poll state vectors (POSIX poll only). More... | |
int | poll_loop_interrupt () |
Forces polling interface wait call to return. More... | |
int | poll_loop_continue () |
Reset state after call to poll_loop_interrupt. More... | |
int | interrupt_sd () |
Returns interrupt socket. More... | |
Protected Types | |
typedef std::priority_queue < ExpireTimer, std::vector < ExpireTimer >, LtTimerHeap > | TimerHeap |
Priority queue for timers. More... | |
Protected Attributes | |
std::mutex | m_mutex |
Mutex to protect members. More... | |
std::mutex | m_polldata_mutex |
Mutex to protect m_polldata member. More... | |
RequestCache | m_request_cache |
Request cache. More... | |
TimerHeap | m_timer_heap |
ExpireTimer heap. More... | |
int | m_interrupt_sd |
Interrupt socket. More... | |
bool | m_interrupt_in_progress |
Set to true if poll loop interrupt in progress. More... | |
std::vector< PollDescriptorT > | m_polldata |
Vector of poll descriptor state structures for use with POSIX poll() . More... | |
ClockT::time_point | m_next_wakeup |
Next polling interface wait timeout (absolute) More... | |
std::set< IOHandler * > | m_removed_handlers |
Set of IOHandler objects scheduled for removal. More... | |
Friends | |
class | ReactorFactory |
Manages reactor (polling thread) state including poll interest, request cache, and timers.
|
protected |
Reactor::Reactor | ( | ) |
Constructor.
Initializes polling interface and creates interrupt socket. If ReactorFactory::use_poll is set to true, then the reactor will use the POSIX poll()
interface, otherwise epoll
is used on Linux, kqueue
on OSX and FreeBSD, and port_associate
on Solaris. For polling mechanisms that do not provide an interface for breaking out of the poll wait, a UDP socket m_interrupt_sd is created (and connected to itself) and added to the poll set.
The following logic creates a UDP socket that is used to interrupt epoll_wait so that it can reset its timeout value
Definition at line 63 of file Reactor.cc.
int Reactor::add_poll_interest | ( | int | sd, |
short | events, | ||
IOHandler * | handler | ||
) |
Add poll interest for socket (POSIX poll
only).
This method is only called when ReactorFactory::use_poll
is set to true. It modifies m_polldata accordingly and then calls poll_loop_interrupt. If an error is encountered, then the poll interest is removed by clearing the m_polldata entry for sd
.
sd | Socket descriptor for which to add poll interest |
events | Bitmask of poll events (see POSIX poll() ) |
handler | Pointer to I/O handler associated with socket |
Definition at line 383 of file Reactor.cc.
|
inline |
|
inline |
Adds a timer.
Pushes timer onto m_timer_heap and interrupts the polling loop so that the poll timeout can be adjusted if necessary.
timer | Reference to ExpireTimer object |
|
inline |
|
inline |
void Reactor::fetch_poll_array | ( | std::vector< struct pollfd > & | fdarray, |
std::vector< IOHandler * > & | handlers | ||
) |
Fetches poll state vectors (POSIX poll
only).
fdarray | Vector of pollfd structures to be passed into poll() |
handlers | Vector of corresponding I/O handlers |
Definition at line 444 of file Reactor.cc.
|
inline |
Returns set of I/O handlers scheduled for removal.
This is a one shot method adds the handlers that have been added to m_removed_handlers and then clears the m_removed_handlers set.
dst | reference to set filled in with removed handlers |
void Reactor::handle_timeouts | ( | PollTimeout & | next_timeout | ) |
Processes request timeouts and timers.
This method removes timed out requests from the request cache, delivering ERROR events (with error == Error::REQUEST_TIMEOUT) via each request's dispatch handler. It also processes expired timers by removing them from m_timer_heap and delivering a TIMEOUT event via the timer handler if it exsists.
next_timeout | Set to next earliest timeout of active requests and timers |
Deliver timer events
Definition at line 170 of file Reactor.cc.
|
inline |
int Reactor::modify_poll_interest | ( | int | sd, |
short | events | ||
) |
Modify poll interest for socket (POSIX poll
only).
This method is only called when ReactorFactory::use_poll
is set to true. It modifies m_polldata accordingly and then calls poll_loop_interrupt.
sd | Socket descriptor for which to add poll interest |
events | Bitmask of poll events (see POSIX poll() ) |
Definition at line 433 of file Reactor.cc.
int Reactor::poll_loop_continue | ( | ) |
Reset state after call to poll_loop_interrupt.
After calling poll_loop_interrupt and handling the interrupt, this method should be called to reset back to normal polling state.
Definition at line 331 of file Reactor.cc.
int Reactor::poll_loop_interrupt | ( | ) |
Forces polling interface wait call to return.
Definition at line 255 of file Reactor.cc.
int Reactor::remove_poll_interest | ( | int | sd | ) |
Remove poll interest for socket (POSIX poll
only).
This method is only called when ReactorFactory::use_poll
is set to true. It modifies m_polldata accordingly and then calls poll_loop_interrupt.
sd | Socket descriptor for which to modify poll interest |
Definition at line 412 of file Reactor.cc.
|
inline |
|
inline |
Schedules handler
for removal.
This method schedules an I/O handler for removal. It should be called only when the handler has been decomissioned in the HandlerMap and when there are no outstanding references to the handler. The handler is added to the m_removed_handlers set and a timer is set for 200 milliseconds in the future so that the ReactorRunner will wake up and complete the removal by removing it completely from the HandlerMap and delivering a DISCONNECT event if it is for a TCP socket.
handler | I/O handler to schedule for removal |
|
friend |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
Mutex to protect m_polldata member.
|
protected |
|
protected |
|
protected |
ExpireTimer heap.