Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

217 lines
6.0KB

  1. //
  2. // detail/signal_set_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef ASIO_DETAIL_SIGNAL_SET_SERVICE_HPP
  11. #define ASIO_DETAIL_SIGNAL_SET_SERVICE_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #include <cstddef>
  17. #include <signal.h>
  18. #include "asio/error.hpp"
  19. #include "asio/io_context.hpp"
  20. #include "asio/detail/handler_alloc_helpers.hpp"
  21. #include "asio/detail/memory.hpp"
  22. #include "asio/detail/op_queue.hpp"
  23. #include "asio/detail/signal_handler.hpp"
  24. #include "asio/detail/signal_op.hpp"
  25. #include "asio/detail/socket_types.hpp"
  26. #if !defined(ASIO_WINDOWS) && !defined(__CYGWIN__)
  27. # include "asio/detail/reactor.hpp"
  28. #endif // !defined(ASIO_WINDOWS) && !defined(__CYGWIN__)
  29. #include "asio/detail/push_options.hpp"
  30. namespace asio {
  31. namespace detail {
  32. #if defined(NSIG) && (NSIG > 0)
  33. enum { max_signal_number = NSIG };
  34. #else
  35. enum { max_signal_number = 128 };
  36. #endif
  37. extern ASIO_DECL struct signal_state* get_signal_state();
  38. extern "C" ASIO_DECL void asio_signal_handler(int signal_number);
  39. class signal_set_service
  40. {
  41. public:
  42. // Type used for tracking an individual signal registration.
  43. class registration
  44. {
  45. public:
  46. // Default constructor.
  47. registration()
  48. : signal_number_(0),
  49. queue_(0),
  50. undelivered_(0),
  51. next_in_table_(0),
  52. prev_in_table_(0),
  53. next_in_set_(0)
  54. {
  55. }
  56. private:
  57. // Only this service will have access to the internal values.
  58. friend class signal_set_service;
  59. // The signal number that is registered.
  60. int signal_number_;
  61. // The waiting signal handlers.
  62. op_queue<signal_op>* queue_;
  63. // The number of undelivered signals.
  64. std::size_t undelivered_;
  65. // Pointers to adjacent registrations in the registrations_ table.
  66. registration* next_in_table_;
  67. registration* prev_in_table_;
  68. // Link to next registration in the signal set.
  69. registration* next_in_set_;
  70. };
  71. // The implementation type of the signal_set.
  72. class implementation_type
  73. {
  74. public:
  75. // Default constructor.
  76. implementation_type()
  77. : signals_(0)
  78. {
  79. }
  80. private:
  81. // Only this service will have access to the internal values.
  82. friend class signal_set_service;
  83. // The pending signal handlers.
  84. op_queue<signal_op> queue_;
  85. // Linked list of registered signals.
  86. registration* signals_;
  87. };
  88. // Constructor.
  89. ASIO_DECL signal_set_service(asio::io_context& io_context);
  90. // Destructor.
  91. ASIO_DECL ~signal_set_service();
  92. // Destroy all user-defined handler objects owned by the service.
  93. ASIO_DECL void shutdown();
  94. // Perform fork-related housekeeping.
  95. ASIO_DECL void notify_fork(
  96. asio::io_context::fork_event fork_ev);
  97. // Construct a new signal_set implementation.
  98. ASIO_DECL void construct(implementation_type& impl);
  99. // Destroy a signal_set implementation.
  100. ASIO_DECL void destroy(implementation_type& impl);
  101. // Add a signal to a signal_set.
  102. ASIO_DECL asio::error_code add(implementation_type& impl,
  103. int signal_number, asio::error_code& ec);
  104. // Remove a signal to a signal_set.
  105. ASIO_DECL asio::error_code remove(implementation_type& impl,
  106. int signal_number, asio::error_code& ec);
  107. // Remove all signals from a signal_set.
  108. ASIO_DECL asio::error_code clear(implementation_type& impl,
  109. asio::error_code& ec);
  110. // Cancel all operations associated with the signal set.
  111. ASIO_DECL asio::error_code cancel(implementation_type& impl,
  112. asio::error_code& ec);
  113. // Start an asynchronous operation to wait for a signal to be delivered.
  114. template <typename Handler>
  115. void async_wait(implementation_type& impl, Handler& handler)
  116. {
  117. // Allocate and construct an operation to wrap the handler.
  118. typedef signal_handler<Handler> op;
  119. typename op::ptr p = { asio::detail::addressof(handler),
  120. op::ptr::allocate(handler), 0 };
  121. p.p = new (p.v) op(handler);
  122. ASIO_HANDLER_CREATION((io_context_.context(),
  123. *p.p, "signal_set", &impl, 0, "async_wait"));
  124. start_wait_op(impl, p.p);
  125. p.v = p.p = 0;
  126. }
  127. // Deliver notification that a particular signal occurred.
  128. ASIO_DECL static void deliver_signal(int signal_number);
  129. private:
  130. // Helper function to add a service to the global signal state.
  131. ASIO_DECL static void add_service(signal_set_service* service);
  132. // Helper function to remove a service from the global signal state.
  133. ASIO_DECL static void remove_service(signal_set_service* service);
  134. // Helper function to create the pipe descriptors.
  135. ASIO_DECL static void open_descriptors();
  136. // Helper function to close the pipe descriptors.
  137. ASIO_DECL static void close_descriptors();
  138. // Helper function to start a wait operation.
  139. ASIO_DECL void start_wait_op(implementation_type& impl, signal_op* op);
  140. // The io_context instance used for dispatching handlers.
  141. io_context_impl& io_context_;
  142. #if !defined(ASIO_WINDOWS) \
  143. && !defined(ASIO_WINDOWS_RUNTIME) \
  144. && !defined(__CYGWIN__)
  145. // The type used for registering for pipe reactor notifications.
  146. class pipe_read_op;
  147. // The reactor used for waiting for pipe readiness.
  148. reactor& reactor_;
  149. // The per-descriptor reactor data used for the pipe.
  150. reactor::per_descriptor_data reactor_data_;
  151. #endif // !defined(ASIO_WINDOWS)
  152. // && !defined(ASIO_WINDOWS_RUNTIME)
  153. // && !defined(__CYGWIN__)
  154. // A mapping from signal number to the registered signal sets.
  155. registration* registrations_[max_signal_number];
  156. // Pointers to adjacent services in linked list.
  157. signal_set_service* next_;
  158. signal_set_service* prev_;
  159. };
  160. } // namespace detail
  161. } // namespace asio
  162. #include "asio/detail/pop_options.hpp"
  163. #if defined(ASIO_HEADER_ONLY)
  164. # include "asio/detail/impl/signal_set_service.ipp"
  165. #endif // defined(ASIO_HEADER_ONLY)
  166. #endif // ASIO_DETAIL_SIGNAL_SET_SERVICE_HPP