Commit 5b4eed64 authored by Dan Melnic's avatar Dan Melnic Committed by Facebook GitHub Bot

Add support for non libevent based backends in the AsyncSignalHandler

Summary: Add support for non libevent based backends in the AsyncSignalHandler

Reviewed By: kevin-vigor

Differential Revision: D20524116

fbshipit-source-id: fdd49cf75bd51599055239270dd704c496d567e3
parent 69296166
......@@ -32,7 +32,7 @@ AsyncSignalHandler::AsyncSignalHandler(EventBase* eventBase)
AsyncSignalHandler::~AsyncSignalHandler() {
// Unregister any outstanding events
for (auto& signalEvent : signalEvents_) {
event_del(&signalEvent.second);
signalEvent.second->eb_event_del();
}
}
......@@ -49,23 +49,23 @@ void AsyncSignalHandler::detachEventBase() {
}
void AsyncSignalHandler::registerSignalHandler(int signum) {
pair<SignalEventMap::iterator, bool> ret =
signalEvents_.insert(make_pair(signum, event()));
pair<SignalEventMap::iterator, bool> ret = signalEvents_.insert(
make_pair(signum, std::make_unique<EventBaseEvent>()));
if (!ret.second) {
// This signal has already been registered
throw std::runtime_error(
folly::to<string>("handler already registered for signal ", signum));
}
struct event* ev = &(ret.first->second);
EventBaseEvent* ev = ret.first->second.get();
try {
signal_set(ev, signum, libeventCallback, this);
if (event_base_set(eventBase_->getLibeventBase(), ev) != 0) {
ev->eb_signal_set(signum, libeventCallback, this);
if (ev->eb_event_base_set(eventBase_) != 0) {
throw std::runtime_error(folly::to<string>(
"error initializing event handler for signal ", signum));
}
if (event_add(ev, nullptr) != 0) {
if (ev->eb_event_add(nullptr) != 0) {
throw std::runtime_error(
folly::to<string>("error adding event handler for signal ", signum));
}
......@@ -84,7 +84,7 @@ void AsyncSignalHandler::unregisterSignalHandler(int signum) {
": signal not registered"));
}
event_del(&it->second);
it->second->eb_event_del();
signalEvents_.erase(it);
}
......
......@@ -103,7 +103,10 @@ class AsyncSignalHandler {
virtual void signalReceived(int signum) noexcept = 0;
private:
typedef std::map<int, struct event> SignalEventMap;
// we cannot copy the EventBaseEvent instances
// so we need to store ptrs to them
// Also some backends store ptrs to the EventBaseEvent instances
using SignalEventMap = std::map<int, std::unique_ptr<EventBaseEvent>>;
// Forbidden copy constructor and assignment operator
AsyncSignalHandler(AsyncSignalHandler const&);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment