AsyncSignalHandler.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright 2011-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #pragma once
  17. #include <folly/io/async/EventBase.h>
  18. #include <folly/portability/Event.h>
  19. #include <map>
  20. namespace folly {
  21. /**
  22. * A handler to receive notification about POSIX signals.
  23. *
  24. * AsyncSignalHandler allows code to process signals from within a EventBase
  25. * loop.
  26. *
  27. * Standard signal handlers interrupt execution of the main thread, and
  28. * are run while the main thread is paused. As a result, great care must be
  29. * taken to avoid race conditions if the signal handler has to access or modify
  30. * any data used by the main thread.
  31. *
  32. * AsyncSignalHandler solves this problem by running the AsyncSignalHandler
  33. * callback in normal thread of execution, as a EventBase callback.
  34. *
  35. * AsyncSignalHandler may only be used in a single thread. It will only
  36. * process signals received by the thread where the AsyncSignalHandler is
  37. * registered. It is the user's responsibility to ensure that signals are
  38. * delivered to the desired thread in multi-threaded programs.
  39. */
  40. class AsyncSignalHandler {
  41. public:
  42. /**
  43. * Create a new AsyncSignalHandler.
  44. */
  45. explicit AsyncSignalHandler(EventBase* eventBase);
  46. virtual ~AsyncSignalHandler();
  47. /**
  48. * Attach this AsyncSignalHandler to an EventBase.
  49. *
  50. * This should only be called if the AsyncSignalHandler is not currently
  51. * registered for any signals and is not currently attached to an existing
  52. * EventBase.
  53. */
  54. void attachEventBase(EventBase* eventBase);
  55. /**
  56. * Detach this AsyncSignalHandler from its EventBase.
  57. *
  58. * This should only be called if the AsyncSignalHandler is not currently
  59. * registered for any signals.
  60. */
  61. void detachEventBase();
  62. /**
  63. * Get the EventBase used by this AsyncSignalHandler.
  64. */
  65. EventBase* getEventBase() const {
  66. return eventBase_;
  67. }
  68. /**
  69. * Register to receive callbacks about the specified signal.
  70. *
  71. * Once the handler has been registered for a particular signal,
  72. * signalReceived() will be called each time this thread receives this
  73. * signal.
  74. *
  75. * Throws if an error occurs or if this handler is already
  76. * registered for this signal.
  77. */
  78. void registerSignalHandler(int signum);
  79. /**
  80. * Unregister for callbacks about the specified signal.
  81. *
  82. * Throws if an error occurs, or if this signal was not registered.
  83. */
  84. void unregisterSignalHandler(int signum);
  85. /**
  86. * signalReceived() will called to indicate that the specified signal has
  87. * been received.
  88. *
  89. * signalReceived() will always be invoked from the EventBase loop (i.e.,
  90. * after the main POSIX signal handler has returned control to the EventBase
  91. * thread).
  92. */
  93. virtual void signalReceived(int signum) noexcept = 0;
  94. private:
  95. typedef std::map<int, struct event> SignalEventMap;
  96. // Forbidden copy constructor and assignment operator
  97. AsyncSignalHandler(AsyncSignalHandler const&);
  98. AsyncSignalHandler& operator=(AsyncSignalHandler const&);
  99. static void libeventCallback(libevent_fd_t signum, short events, void* arg);
  100. EventBase* eventBase_{nullptr};
  101. SignalEventMap signalEvents_;
  102. };
  103. } // namespace folly