proxygen
folly::AsyncSignalHandler Class Referenceabstract

#include <AsyncSignalHandler.h>

Inheritance diagram for folly::AsyncSignalHandler:
proxygen::SignalHandler

Public Member Functions

 AsyncSignalHandler (EventBase *eventBase)
 
virtual ~AsyncSignalHandler ()
 
void attachEventBase (EventBase *eventBase)
 
void detachEventBase ()
 
EventBasegetEventBase () 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 &)
 
AsyncSignalHandleroperator= (AsyncSignalHandler const &)
 

Static Private Member Functions

static void libeventCallback (libevent_fd_t signum, short events, void *arg)
 

Private Attributes

EventBaseeventBase_ {nullptr}
 
SignalEventMap signalEvents_
 

Detailed Description

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.

Member Typedef Documentation

typedef std::map<int, struct event> folly::AsyncSignalHandler::SignalEventMap
private

Definition at line 105 of file AsyncSignalHandler.h.

Constructor & Destructor Documentation

folly::AsyncSignalHandler::AsyncSignalHandler ( EventBase eventBase)
explicit

Create a new AsyncSignalHandler.

Definition at line 28 of file AsyncSignalHandler.cpp.

29  : eventBase_(eventBase) {}
folly::AsyncSignalHandler::~AsyncSignalHandler ( )
virtual

Definition at line 31 of file AsyncSignalHandler.cpp.

References signalEvents_.

31  {
32  // Unregister any outstanding events
33  for (SignalEventMap::iterator it = signalEvents_.begin();
34  it != signalEvents_.end();
35  ++it) {
36  event_del(&it->second);
37  }
38 }
folly::AsyncSignalHandler::AsyncSignalHandler ( AsyncSignalHandler const &  )
private

Member Function Documentation

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_.

40  {
41  assert(eventBase_ == nullptr);
42  assert(signalEvents_.empty());
43  eventBase_ = eventBase;
44 }
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_.

46  {
47  assert(eventBase_ != nullptr);
48  assert(signalEvents_.empty());
49  eventBase_ = nullptr;
50 }
EventBase* folly::AsyncSignalHandler::getEventBase ( ) const
inline
void folly::AsyncSignalHandler::libeventCallback ( libevent_fd_t  signum,
short  events,
void *  arg 
)
staticprivate

Definition at line 92 of file AsyncSignalHandler.cpp.

References handler(), and signalReceived().

Referenced by registerSignalHandler().

95  {
96  AsyncSignalHandler* handler = static_cast<AsyncSignalHandler*>(arg);
97  handler->signalReceived(int(signum));
98 }
void handler(int, siginfo_t *, void *)
AsyncSignalHandler(EventBase *eventBase)
AsyncSignalHandler& folly::AsyncSignalHandler::operator= ( AsyncSignalHandler const &  )
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().

52  {
54  signalEvents_.insert(make_pair(signum, event()));
55  if (!ret.second) {
56  // This signal has already been registered
57  throw std::runtime_error(
58  folly::to<string>("handler already registered for signal ", signum));
59  }
60 
61  struct event* ev = &(ret.first->second);
62  try {
63  signal_set(ev, signum, libeventCallback, this);
64  if (event_base_set(eventBase_->getLibeventBase(), ev) != 0) {
65  throw std::runtime_error(folly::to<string>(
66  "error initializing event handler for signal ", signum));
67  }
68 
69  if (event_add(ev, nullptr) != 0) {
70  throw std::runtime_error(
71  folly::to<string>("error adding event handler for signal ", signum));
72  }
73  } catch (...) {
74  signalEvents_.erase(ret.first);
75  throw;
76  }
77 }
static void libeventCallback(libevent_fd_t signum, short events, void *arg)
event_base * getLibeventBase() const
Definition: EventBase.h:537
Definition: Traits.h:577
virtual void folly::AsyncSignalHandler::signalReceived ( int  signum)
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().

79  {
80  SignalEventMap::iterator it = signalEvents_.find(signum);
81  if (it == signalEvents_.end()) {
82  throw std::runtime_error(folly::to<string>(
83  "unable to unregister handler for signal ",
84  signum,
85  ": signal not registered"));
86  }
87 
88  event_del(&it->second);
89  signalEvents_.erase(it);
90 }

Member Data Documentation

EventBase* folly::AsyncSignalHandler::eventBase_ {nullptr}
private
SignalEventMap folly::AsyncSignalHandler::signalEvents_
private

The documentation for this class was generated from the following files: