kqueue_reactor.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. //
  2. // detail/kqueue_reactor.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2025 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. // Copyright (c) 2005 Stefan Arentz (stefan at soze dot com)
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. #ifndef BOOST_ASIO_DETAIL_KQUEUE_REACTOR_HPP
  12. #define BOOST_ASIO_DETAIL_KQUEUE_REACTOR_HPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include <boost/asio/detail/config.hpp>
  17. #if defined(BOOST_ASIO_HAS_KQUEUE)
  18. #include <cstddef>
  19. #include <sys/types.h>
  20. #include <sys/event.h>
  21. #include <sys/time.h>
  22. #include <boost/asio/detail/conditionally_enabled_mutex.hpp>
  23. #include <boost/asio/detail/limits.hpp>
  24. #include <boost/asio/detail/object_pool.hpp>
  25. #include <boost/asio/detail/op_queue.hpp>
  26. #include <boost/asio/detail/reactor_op.hpp>
  27. #include <boost/asio/detail/scheduler_task.hpp>
  28. #include <boost/asio/detail/select_interrupter.hpp>
  29. #include <boost/asio/detail/socket_types.hpp>
  30. #include <boost/asio/detail/timer_queue_base.hpp>
  31. #include <boost/asio/detail/timer_queue_set.hpp>
  32. #include <boost/asio/detail/wait_op.hpp>
  33. #include <boost/asio/error.hpp>
  34. #include <boost/asio/execution_context.hpp>
  35. // Older versions of Mac OS X may not define EV_OOBAND.
  36. #if !defined(EV_OOBAND)
  37. # define EV_OOBAND EV_FLAG1
  38. #endif // !defined(EV_OOBAND)
  39. #include <boost/asio/detail/push_options.hpp>
  40. namespace boost {
  41. namespace asio {
  42. namespace detail {
  43. class scheduler;
  44. class kqueue_reactor
  45. : public execution_context_service_base<kqueue_reactor>,
  46. public scheduler_task
  47. {
  48. private:
  49. // The mutex type used by this reactor.
  50. typedef conditionally_enabled_mutex mutex;
  51. public:
  52. enum op_types { read_op = 0, write_op = 1,
  53. connect_op = 1, except_op = 2, max_ops = 3 };
  54. // Per-descriptor queues.
  55. struct descriptor_state
  56. {
  57. descriptor_state(bool locking, int spin_count)
  58. : mutex_(locking, spin_count) {}
  59. descriptor_state* next_;
  60. descriptor_state* prev_;
  61. mutex mutex_;
  62. int descriptor_;
  63. int num_kevents_; // 1 == read only, 2 == read and write
  64. op_queue<reactor_op> op_queue_[max_ops];
  65. bool shutdown_;
  66. };
  67. // Per-descriptor data.
  68. typedef descriptor_state* per_descriptor_data;
  69. // Constructor.
  70. BOOST_ASIO_DECL kqueue_reactor(boost::asio::execution_context& ctx);
  71. // Destructor.
  72. BOOST_ASIO_DECL ~kqueue_reactor();
  73. // Destroy all user-defined handler objects owned by the service.
  74. BOOST_ASIO_DECL void shutdown();
  75. // Recreate internal descriptors following a fork.
  76. BOOST_ASIO_DECL void notify_fork(
  77. boost::asio::execution_context::fork_event fork_ev);
  78. // Initialise the task.
  79. BOOST_ASIO_DECL void init_task();
  80. // Register a socket with the reactor. Returns 0 on success, system error
  81. // code on failure.
  82. BOOST_ASIO_DECL int register_descriptor(socket_type descriptor,
  83. per_descriptor_data& descriptor_data);
  84. // Register a descriptor with an associated single operation. Returns 0 on
  85. // success, system error code on failure.
  86. BOOST_ASIO_DECL int register_internal_descriptor(
  87. int op_type, socket_type descriptor,
  88. per_descriptor_data& descriptor_data, reactor_op* op);
  89. // Move descriptor registration from one descriptor_data object to another.
  90. BOOST_ASIO_DECL void move_descriptor(socket_type descriptor,
  91. per_descriptor_data& target_descriptor_data,
  92. per_descriptor_data& source_descriptor_data);
  93. // Post a reactor operation for immediate completion.
  94. void post_immediate_completion(operation* op, bool is_continuation) const;
  95. // Post a reactor operation for immediate completion.
  96. BOOST_ASIO_DECL static void call_post_immediate_completion(
  97. operation* op, bool is_continuation, const void* self);
  98. // Start a new operation. The reactor operation will be performed when the
  99. // given descriptor is flagged as ready, or an error has occurred.
  100. BOOST_ASIO_DECL void start_op(int op_type, socket_type descriptor,
  101. per_descriptor_data& descriptor_data, reactor_op* op,
  102. bool is_continuation, bool allow_speculative,
  103. void (*on_immediate)(operation*, bool, const void*),
  104. const void* immediate_arg);
  105. // Start a new operation. The reactor operation will be performed when the
  106. // given descriptor is flagged as ready, or an error has occurred.
  107. void start_op(int op_type, socket_type descriptor,
  108. per_descriptor_data& descriptor_data, reactor_op* op,
  109. bool is_continuation, bool allow_speculative)
  110. {
  111. start_op(op_type, descriptor, descriptor_data,
  112. op, is_continuation, allow_speculative,
  113. &kqueue_reactor::call_post_immediate_completion, this);
  114. }
  115. // Cancel all operations associated with the given descriptor. The
  116. // handlers associated with the descriptor will be invoked with the
  117. // operation_aborted error.
  118. BOOST_ASIO_DECL void cancel_ops(socket_type descriptor,
  119. per_descriptor_data& descriptor_data);
  120. // Cancel all operations associated with the given descriptor and key. The
  121. // handlers associated with the descriptor will be invoked with the
  122. // operation_aborted error.
  123. BOOST_ASIO_DECL void cancel_ops_by_key(socket_type descriptor,
  124. per_descriptor_data& descriptor_data,
  125. int op_type, void* cancellation_key);
  126. // Cancel any operations that are running against the descriptor and remove
  127. // its registration from the reactor. The reactor resources associated with
  128. // the descriptor must be released by calling cleanup_descriptor_data.
  129. BOOST_ASIO_DECL void deregister_descriptor(socket_type descriptor,
  130. per_descriptor_data& descriptor_data, bool closing);
  131. // Remove the descriptor's registration from the reactor. The reactor
  132. // resources associated with the descriptor must be released by calling
  133. // cleanup_descriptor_data.
  134. BOOST_ASIO_DECL void deregister_internal_descriptor(
  135. socket_type descriptor, per_descriptor_data& descriptor_data);
  136. // Perform any post-deregistration cleanup tasks associated with the
  137. // descriptor data.
  138. BOOST_ASIO_DECL void cleanup_descriptor_data(
  139. per_descriptor_data& descriptor_data);
  140. // Add a new timer queue to the reactor.
  141. template <typename TimeTraits, typename Allocator>
  142. void add_timer_queue(timer_queue<TimeTraits, Allocator>& timer_queue);
  143. // Remove a timer queue from the reactor.
  144. template <typename TimeTraits, typename Allocator>
  145. void remove_timer_queue(timer_queue<TimeTraits, Allocator>& timer_queue);
  146. // Schedule a new operation in the given timer queue to expire at the
  147. // specified absolute time.
  148. template <typename TimeTraits, typename Allocator>
  149. void schedule_timer(timer_queue<TimeTraits, Allocator>& queue,
  150. const typename TimeTraits::time_type& time,
  151. typename timer_queue<TimeTraits, Allocator>::per_timer_data& timer,
  152. wait_op* op);
  153. // Cancel the timer operations associated with the given token. Returns the
  154. // number of operations that have been posted or dispatched.
  155. template <typename TimeTraits, typename Allocator>
  156. std::size_t cancel_timer(timer_queue<TimeTraits, Allocator>& queue,
  157. typename timer_queue<TimeTraits, Allocator>::per_timer_data& timer,
  158. std::size_t max_cancelled = (std::numeric_limits<std::size_t>::max)());
  159. // Cancel the timer operations associated with the given key.
  160. template <typename TimeTraits, typename Allocator>
  161. void cancel_timer_by_key(timer_queue<TimeTraits, Allocator>& queue,
  162. typename timer_queue<TimeTraits, Allocator>::per_timer_data* timer,
  163. void* cancellation_key);
  164. // Move the timer operations associated with the given timer.
  165. template <typename TimeTraits, typename Allocator>
  166. void move_timer(timer_queue<TimeTraits, Allocator>& queue,
  167. typename timer_queue<TimeTraits, Allocator>::per_timer_data& target,
  168. typename timer_queue<TimeTraits, Allocator>::per_timer_data& source);
  169. // Run the kqueue loop.
  170. BOOST_ASIO_DECL void run(long usec, op_queue<operation>& ops);
  171. // Interrupt the kqueue loop.
  172. BOOST_ASIO_DECL void interrupt();
  173. private:
  174. // Create the kqueue file descriptor. Throws an exception if the descriptor
  175. // cannot be created.
  176. BOOST_ASIO_DECL static int do_kqueue_create();
  177. // Allocate a new descriptor state object.
  178. BOOST_ASIO_DECL descriptor_state* allocate_descriptor_state();
  179. // Free an existing descriptor state object.
  180. BOOST_ASIO_DECL void free_descriptor_state(descriptor_state* s);
  181. // Helper function to add a new timer queue.
  182. BOOST_ASIO_DECL void do_add_timer_queue(timer_queue_base& queue);
  183. // Helper function to remove a timer queue.
  184. BOOST_ASIO_DECL void do_remove_timer_queue(timer_queue_base& queue);
  185. // Get the timeout value for the kevent call.
  186. BOOST_ASIO_DECL timespec* get_timeout(long usec, timespec& ts);
  187. // The scheduler used to post completions.
  188. scheduler& scheduler_;
  189. // Mutex to protect access to internal data.
  190. mutex mutex_;
  191. // The kqueue file descriptor.
  192. int kqueue_fd_;
  193. // The interrupter is used to break a blocking kevent call.
  194. select_interrupter interrupter_;
  195. // The timer queues.
  196. timer_queue_set timer_queues_;
  197. // Whether the service has been shut down.
  198. bool shutdown_;
  199. // Whether I/O locking is enabled.
  200. const bool io_locking_;
  201. // How any times to spin waiting for the I/O mutex.
  202. const int io_locking_spin_count_;
  203. // Mutex to protect access to the registered descriptors.
  204. mutex registered_descriptors_mutex_;
  205. // Keep track of all registered descriptors.
  206. object_pool<descriptor_state, execution_context::allocator<void>>
  207. registered_descriptors_;
  208. // Atomic used to create a happens-before relationship between allocation of
  209. // a new descriptor state and that state being received via a kqueue event.
  210. atomic_count allocation_counter_;
  211. };
  212. } // namespace detail
  213. } // namespace asio
  214. } // namespace boost
  215. #include <boost/asio/detail/pop_options.hpp>
  216. #include <boost/asio/detail/impl/kqueue_reactor.hpp>
  217. #if defined(BOOST_ASIO_HEADER_ONLY)
  218. # include <boost/asio/detail/impl/kqueue_reactor.ipp>
  219. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  220. #endif // defined(BOOST_ASIO_HAS_KQUEUE)
  221. #endif // BOOST_ASIO_DETAIL_KQUEUE_REACTOR_HPP