X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2Fio%2Fasync%2FAsyncSignalHandler.h;h=aee742274eb4e38100c9c326b01b672794c24a59;hb=cd1bdc912603c0358ba733d379a74ae90ab3a437;hp=ca35bc17025fdb2502af1c2d712b96f9e5ac383f;hpb=d2711b8a7416ea6d4fb17ce47e5a74f6ab78e0df;p=folly.git diff --git a/folly/io/async/AsyncSignalHandler.h b/folly/io/async/AsyncSignalHandler.h index ca35bc17..aee74227 100644 --- a/folly/io/async/AsyncSignalHandler.h +++ b/folly/io/async/AsyncSignalHandler.h @@ -1,5 +1,5 @@ /* - * Copyright 2015 Facebook, Inc. + * Copyright 2011-present Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,7 +16,7 @@ #pragma once #include -#include +#include #include namespace folly { @@ -24,17 +24,19 @@ namespace folly { /** * A handler to receive notification about POSIX signals. * - * TAsyncSignalHandler allows code to process signals from within a EventBase - * loop. Standard signal handlers interrupt execution of the main thread, and + * 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. * - * TAsyncSignalHandler solves this problem by running the TAsyncSignalHandler + * AsyncSignalHandler solves this problem by running the AsyncSignalHandler * callback in normal thread of execution, as a EventBase callback. * - * TAsyncSignalHandler may only be used in a single thread. It will only - * process signals received by the thread where the TAsyncSignalHandler is + * 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. */ @@ -46,6 +48,30 @@ class AsyncSignalHandler { explicit AsyncSignalHandler(EventBase* eventBase); virtual ~AsyncSignalHandler(); + /** + * 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. + */ + void attachEventBase(EventBase* eventBase); + + /** + * Detach this AsyncSignalHandler from its EventBase. + * + * This should only be called if the AsyncSignalHandler is not currently + * registered for any signals. + */ + void detachEventBase(); + + /** + * Get the EventBase used by this AsyncSignalHandler. + */ + EventBase* getEventBase() const { + return eventBase_; + } + /** * Register to receive callbacks about the specified signal. * @@ -53,7 +79,7 @@ class AsyncSignalHandler { * signalReceived() will be called each time this thread receives this * signal. * - * Throws a TException if an error occurs, or if this handler is already + * Throws if an error occurs or if this handler is already * registered for this signal. */ void registerSignalHandler(int signum); @@ -61,8 +87,7 @@ class AsyncSignalHandler { /** * Unregister for callbacks about the specified signal. * - * Throws a TException if an error occurs, or if this signal was not - * registered. + * Throws if an error occurs, or if this signal was not registered. */ void unregisterSignalHandler(int signum); @@ -83,10 +108,10 @@ class AsyncSignalHandler { AsyncSignalHandler(AsyncSignalHandler const &); AsyncSignalHandler& operator=(AsyncSignalHandler const &); - static void libeventCallback(int signum, short events, void* arg); + static void libeventCallback(libevent_fd_t signum, short events, void* arg); - EventBase* eventBase_; + EventBase* eventBase_{nullptr}; SignalEventMap signalEvents_; }; -} // folly +} // namespace folly