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.

230 lines
6.4KB

  1. //
  2. // detail/signal_set_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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/execution_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_HAS_IOCP)
  27. # include "asio/detail/win_iocp_io_context.hpp"
  28. #else // defined(ASIO_HAS_IOCP)
  29. # include "asio/detail/scheduler.hpp"
  30. #endif // defined(ASIO_HAS_IOCP)
  31. #if !defined(ASIO_WINDOWS) && !defined(__CYGWIN__)
  32. # include "asio/detail/reactor.hpp"
  33. #endif // !defined(ASIO_WINDOWS) && !defined(__CYGWIN__)
  34. #include "asio/detail/push_options.hpp"
  35. namespace asio {
  36. namespace detail {
  37. #if defined(NSIG) && (NSIG > 0)
  38. enum { max_signal_number = NSIG };
  39. #else
  40. enum { max_signal_number = 128 };
  41. #endif
  42. extern ASIO_DECL struct signal_state* get_signal_state();
  43. extern "C" ASIO_DECL void asio_signal_handler(int signal_number);
  44. class signal_set_service :
  45. public execution_context_service_base<signal_set_service>
  46. {
  47. public:
  48. // Type used for tracking an individual signal registration.
  49. class registration
  50. {
  51. public:
  52. // Default constructor.
  53. registration()
  54. : signal_number_(0),
  55. queue_(0),
  56. undelivered_(0),
  57. next_in_table_(0),
  58. prev_in_table_(0),
  59. next_in_set_(0)
  60. {
  61. }
  62. private:
  63. // Only this service will have access to the internal values.
  64. friend class signal_set_service;
  65. // The signal number that is registered.
  66. int signal_number_;
  67. // The waiting signal handlers.
  68. op_queue<signal_op>* queue_;
  69. // The number of undelivered signals.
  70. std::size_t undelivered_;
  71. // Pointers to adjacent registrations in the registrations_ table.
  72. registration* next_in_table_;
  73. registration* prev_in_table_;
  74. // Link to next registration in the signal set.
  75. registration* next_in_set_;
  76. };
  77. // The implementation type of the signal_set.
  78. class implementation_type
  79. {
  80. public:
  81. // Default constructor.
  82. implementation_type()
  83. : signals_(0)
  84. {
  85. }
  86. private:
  87. // Only this service will have access to the internal values.
  88. friend class signal_set_service;
  89. // The pending signal handlers.
  90. op_queue<signal_op> queue_;
  91. // Linked list of registered signals.
  92. registration* signals_;
  93. };
  94. // Constructor.
  95. ASIO_DECL signal_set_service(execution_context& context);
  96. // Destructor.
  97. ASIO_DECL ~signal_set_service();
  98. // Destroy all user-defined handler objects owned by the service.
  99. ASIO_DECL void shutdown();
  100. // Perform fork-related housekeeping.
  101. ASIO_DECL void notify_fork(
  102. asio::execution_context::fork_event fork_ev);
  103. // Construct a new signal_set implementation.
  104. ASIO_DECL void construct(implementation_type& impl);
  105. // Destroy a signal_set implementation.
  106. ASIO_DECL void destroy(implementation_type& impl);
  107. // Add a signal to a signal_set.
  108. ASIO_DECL asio::error_code add(implementation_type& impl,
  109. int signal_number, asio::error_code& ec);
  110. // Remove a signal to a signal_set.
  111. ASIO_DECL asio::error_code remove(implementation_type& impl,
  112. int signal_number, asio::error_code& ec);
  113. // Remove all signals from a signal_set.
  114. ASIO_DECL asio::error_code clear(implementation_type& impl,
  115. asio::error_code& ec);
  116. // Cancel all operations associated with the signal set.
  117. ASIO_DECL asio::error_code cancel(implementation_type& impl,
  118. asio::error_code& ec);
  119. // Start an asynchronous operation to wait for a signal to be delivered.
  120. template <typename Handler, typename IoExecutor>
  121. void async_wait(implementation_type& impl,
  122. Handler& handler, const IoExecutor& io_ex)
  123. {
  124. // Allocate and construct an operation to wrap the handler.
  125. typedef signal_handler<Handler, IoExecutor> op;
  126. typename op::ptr p = { asio::detail::addressof(handler),
  127. op::ptr::allocate(handler), 0 };
  128. p.p = new (p.v) op(handler, io_ex);
  129. ASIO_HANDLER_CREATION((scheduler_.context(),
  130. *p.p, "signal_set", &impl, 0, "async_wait"));
  131. start_wait_op(impl, p.p);
  132. p.v = p.p = 0;
  133. }
  134. // Deliver notification that a particular signal occurred.
  135. ASIO_DECL static void deliver_signal(int signal_number);
  136. private:
  137. // Helper function to add a service to the global signal state.
  138. ASIO_DECL static void add_service(signal_set_service* service);
  139. // Helper function to remove a service from the global signal state.
  140. ASIO_DECL static void remove_service(signal_set_service* service);
  141. // Helper function to create the pipe descriptors.
  142. ASIO_DECL static void open_descriptors();
  143. // Helper function to close the pipe descriptors.
  144. ASIO_DECL static void close_descriptors();
  145. // Helper function to start a wait operation.
  146. ASIO_DECL void start_wait_op(implementation_type& impl, signal_op* op);
  147. // The scheduler used for dispatching handlers.
  148. #if defined(ASIO_HAS_IOCP)
  149. typedef class win_iocp_io_context scheduler_impl;
  150. #else
  151. typedef class scheduler scheduler_impl;
  152. #endif
  153. scheduler_impl& scheduler_;
  154. #if !defined(ASIO_WINDOWS) \
  155. && !defined(ASIO_WINDOWS_RUNTIME) \
  156. && !defined(__CYGWIN__)
  157. // The type used for registering for pipe reactor notifications.
  158. class pipe_read_op;
  159. // The reactor used for waiting for pipe readiness.
  160. reactor& reactor_;
  161. // The per-descriptor reactor data used for the pipe.
  162. reactor::per_descriptor_data reactor_data_;
  163. #endif // !defined(ASIO_WINDOWS)
  164. // && !defined(ASIO_WINDOWS_RUNTIME)
  165. // && !defined(__CYGWIN__)
  166. // A mapping from signal number to the registered signal sets.
  167. registration* registrations_[max_signal_number];
  168. // Pointers to adjacent services in linked list.
  169. signal_set_service* next_;
  170. signal_set_service* prev_;
  171. };
  172. } // namespace detail
  173. } // namespace asio
  174. #include "asio/detail/pop_options.hpp"
  175. #if defined(ASIO_HEADER_ONLY)
  176. # include "asio/detail/impl/signal_set_service.ipp"
  177. #endif // defined(ASIO_HEADER_ONLY)
  178. #endif // ASIO_DETAIL_SIGNAL_SET_SERVICE_HPP