proxygen
|
#include <AsyncSignalHandler.h>
Public Member Functions | |
AsyncSignalHandler (EventBase *eventBase) | |
virtual | ~AsyncSignalHandler () |
void | attachEventBase (EventBase *eventBase) |
void | detachEventBase () |
EventBase * | getEventBase () const |
void | registerSignalHandler (int signum) |
void | unregisterSignalHandler (int signum) |
virtual void | signalReceived (int signum) noexcept=0 |
Private Types | |
typedef std::map< int, struct event > | SignalEventMap |
Private Member Functions | |
AsyncSignalHandler (AsyncSignalHandler const &) | |
AsyncSignalHandler & | operator= (AsyncSignalHandler const &) |
Static Private Member Functions | |
static void | libeventCallback (libevent_fd_t signum, short events, void *arg) |
Private Attributes | |
EventBase * | eventBase_ {nullptr} |
SignalEventMap | signalEvents_ |
A handler to receive notification about POSIX signals.
AsyncSignalHandler allows code to process signals from within a EventBase loop.
Standard signal handlers interrupt execution of the main thread, and are run while the main thread is paused. As a result, great care must be taken to avoid race conditions if the signal handler has to access or modify any data used by the main thread.
AsyncSignalHandler solves this problem by running the AsyncSignalHandler callback in normal thread of execution, as a EventBase callback.
AsyncSignalHandler may only be used in a single thread. It will only process signals received by the thread where the AsyncSignalHandler is registered. It is the user's responsibility to ensure that signals are delivered to the desired thread in multi-threaded programs.
Definition at line 43 of file AsyncSignalHandler.h.
|
private |
Definition at line 105 of file AsyncSignalHandler.h.
|
explicit |
|
virtual |
Definition at line 31 of file AsyncSignalHandler.cpp.
References signalEvents_.
|
private |
void folly::AsyncSignalHandler::attachEventBase | ( | EventBase * | eventBase | ) |
Attach this AsyncSignalHandler to an EventBase.
This should only be called if the AsyncSignalHandler is not currently registered for any signals and is not currently attached to an existing EventBase.
Definition at line 40 of file AsyncSignalHandler.cpp.
References eventBase_, and signalEvents_.
void folly::AsyncSignalHandler::detachEventBase | ( | ) |
Detach this AsyncSignalHandler from its EventBase.
This should only be called if the AsyncSignalHandler is not currently registered for any signals.
Definition at line 46 of file AsyncSignalHandler.cpp.
References eventBase_, and signalEvents_.
|
inline |
Get the EventBase used by this AsyncSignalHandler.
Definition at line 71 of file AsyncSignalHandler.h.
References eventBase_, folly::pushmi::__adl::noexcept(), registerSignalHandler(), signalReceived(), and unregisterSignalHandler().
|
staticprivate |
Definition at line 92 of file AsyncSignalHandler.cpp.
References handler(), and signalReceived().
Referenced by registerSignalHandler().
|
private |
void folly::AsyncSignalHandler::registerSignalHandler | ( | int | signum | ) |
Register to receive callbacks about the specified signal.
Once the handler has been registered for a particular signal, signalReceived() will be called each time this thread receives this signal.
Throws if an error occurs or if this handler is already registered for this signal.
Definition at line 52 of file AsyncSignalHandler.cpp.
References eventBase_, folly::EventBase::getLibeventBase(), libeventCallback(), and signalEvents_.
Referenced by getEventBase(), and proxygen::SignalHandler::install().
|
pure virtualnoexcept |
signalReceived() will called to indicate that the specified signal has been received.
signalReceived() will always be invoked from the EventBase loop (i.e., after the main POSIX signal handler has returned control to the EventBase thread).
Implemented in proxygen::SignalHandler.
Referenced by getEventBase(), and libeventCallback().
void folly::AsyncSignalHandler::unregisterSignalHandler | ( | int | signum | ) |
Unregister for callbacks about the specified signal.
Throws if an error occurs, or if this signal was not registered.
Definition at line 79 of file AsyncSignalHandler.cpp.
References signalEvents_.
Referenced by getEventBase().
Definition at line 113 of file AsyncSignalHandler.h.
Referenced by attachEventBase(), detachEventBase(), getEventBase(), and registerSignalHandler().
|
private |
Definition at line 114 of file AsyncSignalHandler.h.
Referenced by attachEventBase(), detachEventBase(), registerSignalHandler(), unregisterSignalHandler(), and ~AsyncSignalHandler().