select_reactor.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. //
  2. // detail/select_reactor.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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 BOOST_ASIO_DETAIL_SELECT_REACTOR_HPP
  11. #define BOOST_ASIO_DETAIL_SELECT_REACTOR_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #if defined(BOOST_ASIO_HAS_IOCP) \
  17. || (!defined(BOOST_ASIO_HAS_DEV_POLL) \
  18. && !defined(BOOST_ASIO_HAS_EPOLL) \
  19. && !defined(BOOST_ASIO_HAS_KQUEUE) \
  20. && !defined(BOOST_ASIO_WINDOWS_RUNTIME))
  21. #include <cstddef>
  22. #include <boost/asio/detail/fd_set_adapter.hpp>
  23. #include <boost/asio/detail/limits.hpp>
  24. #include <boost/asio/detail/mutex.hpp>
  25. #include <boost/asio/detail/op_queue.hpp>
  26. #include <boost/asio/detail/reactor_op.hpp>
  27. #include <boost/asio/detail/reactor_op_queue.hpp>
  28. #include <boost/asio/detail/scheduler_task.hpp>
  29. #include <boost/asio/detail/select_interrupter.hpp>
  30. #include <boost/asio/detail/socket_types.hpp>
  31. #include <boost/asio/detail/timer_queue_base.hpp>
  32. #include <boost/asio/detail/timer_queue_set.hpp>
  33. #include <boost/asio/detail/wait_op.hpp>
  34. #include <boost/asio/execution_context.hpp>
  35. #if defined(BOOST_ASIO_HAS_IOCP)
  36. # include <boost/asio/detail/thread.hpp>
  37. #endif // defined(BOOST_ASIO_HAS_IOCP)
  38. #include <boost/asio/detail/push_options.hpp>
  39. namespace boost {
  40. namespace asio {
  41. namespace detail {
  42. class select_reactor
  43. : public execution_context_service_base<select_reactor>
  44. #if !defined(BOOST_ASIO_HAS_IOCP)
  45. , public scheduler_task
  46. #endif // !defined(BOOST_ASIO_HAS_IOCP)
  47. {
  48. public:
  49. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  50. enum op_types { read_op = 0, write_op = 1, except_op = 2,
  51. max_select_ops = 3, connect_op = 3, max_ops = 4 };
  52. #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  53. enum op_types { read_op = 0, write_op = 1, except_op = 2,
  54. max_select_ops = 3, connect_op = 1, max_ops = 3 };
  55. #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  56. // Per-descriptor data.
  57. struct per_descriptor_data
  58. {
  59. };
  60. // Constructor.
  61. BOOST_ASIO_DECL select_reactor(boost::asio::execution_context& ctx);
  62. // Destructor.
  63. BOOST_ASIO_DECL ~select_reactor();
  64. // Destroy all user-defined handler objects owned by the service.
  65. BOOST_ASIO_DECL void shutdown();
  66. // Recreate internal descriptors following a fork.
  67. BOOST_ASIO_DECL void notify_fork(
  68. boost::asio::execution_context::fork_event fork_ev);
  69. // Initialise the task, but only if the reactor is not in its own thread.
  70. BOOST_ASIO_DECL void init_task();
  71. // Register a socket with the reactor. Returns 0 on success, system error
  72. // code on failure.
  73. BOOST_ASIO_DECL int register_descriptor(socket_type, per_descriptor_data&);
  74. // Register a descriptor with an associated single operation. Returns 0 on
  75. // success, system error code on failure.
  76. BOOST_ASIO_DECL int register_internal_descriptor(
  77. int op_type, socket_type descriptor,
  78. per_descriptor_data& descriptor_data, reactor_op* op);
  79. // Post a reactor operation for immediate completion.
  80. void post_immediate_completion(operation* op, bool is_continuation) const;
  81. // Post a reactor operation for immediate completion.
  82. BOOST_ASIO_DECL static void call_post_immediate_completion(
  83. operation* op, bool is_continuation, const void* self);
  84. // Start a new operation. The reactor operation will be performed when the
  85. // given descriptor is flagged as ready, or an error has occurred.
  86. BOOST_ASIO_DECL void start_op(int op_type, socket_type descriptor,
  87. per_descriptor_data&, reactor_op* op, bool is_continuation, bool,
  88. void (*on_immediate)(operation*, bool, const void*),
  89. const void* immediate_arg);
  90. // Start a new operation. The reactor operation will be performed when the
  91. // given descriptor is flagged as ready, or an error has occurred.
  92. void start_op(int op_type, socket_type descriptor,
  93. per_descriptor_data& descriptor_data, reactor_op* op,
  94. bool is_continuation, bool allow_speculative)
  95. {
  96. start_op(op_type, descriptor, descriptor_data,
  97. op, is_continuation, allow_speculative,
  98. &select_reactor::call_post_immediate_completion, this);
  99. }
  100. // Cancel all operations associated with the given descriptor. The
  101. // handlers associated with the descriptor will be invoked with the
  102. // operation_aborted error.
  103. BOOST_ASIO_DECL void cancel_ops(socket_type descriptor, per_descriptor_data&);
  104. // Cancel all operations associated with the given descriptor and key. The
  105. // handlers associated with the descriptor will be invoked with the
  106. // operation_aborted error.
  107. BOOST_ASIO_DECL void cancel_ops_by_key(socket_type descriptor,
  108. per_descriptor_data& descriptor_data,
  109. int op_type, void* cancellation_key);
  110. // Cancel any operations that are running against the descriptor and remove
  111. // its registration from the reactor. The reactor resources associated with
  112. // the descriptor must be released by calling cleanup_descriptor_data.
  113. BOOST_ASIO_DECL void deregister_descriptor(socket_type descriptor,
  114. per_descriptor_data&, bool closing);
  115. // Remove the descriptor's registration from the reactor. The reactor
  116. // resources associated with the descriptor must be released by calling
  117. // cleanup_descriptor_data.
  118. BOOST_ASIO_DECL void deregister_internal_descriptor(
  119. socket_type descriptor, per_descriptor_data&);
  120. // Perform any post-deregistration cleanup tasks associated with the
  121. // descriptor data.
  122. BOOST_ASIO_DECL void cleanup_descriptor_data(per_descriptor_data&);
  123. // Move descriptor registration from one descriptor_data object to another.
  124. BOOST_ASIO_DECL void move_descriptor(socket_type descriptor,
  125. per_descriptor_data& target_descriptor_data,
  126. per_descriptor_data& source_descriptor_data);
  127. // Add a new timer queue to the reactor.
  128. template <typename Time_Traits>
  129. void add_timer_queue(timer_queue<Time_Traits>& queue);
  130. // Remove a timer queue from the reactor.
  131. template <typename Time_Traits>
  132. void remove_timer_queue(timer_queue<Time_Traits>& queue);
  133. // Schedule a new operation in the given timer queue to expire at the
  134. // specified absolute time.
  135. template <typename Time_Traits>
  136. void schedule_timer(timer_queue<Time_Traits>& queue,
  137. const typename Time_Traits::time_type& time,
  138. typename timer_queue<Time_Traits>::per_timer_data& timer, wait_op* op);
  139. // Cancel the timer operations associated with the given token. Returns the
  140. // number of operations that have been posted or dispatched.
  141. template <typename Time_Traits>
  142. std::size_t cancel_timer(timer_queue<Time_Traits>& queue,
  143. typename timer_queue<Time_Traits>::per_timer_data& timer,
  144. std::size_t max_cancelled = (std::numeric_limits<std::size_t>::max)());
  145. // Cancel the timer operations associated with the given key.
  146. template <typename Time_Traits>
  147. void cancel_timer_by_key(timer_queue<Time_Traits>& queue,
  148. typename timer_queue<Time_Traits>::per_timer_data* timer,
  149. void* cancellation_key);
  150. // Move the timer operations associated with the given timer.
  151. template <typename Time_Traits>
  152. void move_timer(timer_queue<Time_Traits>& queue,
  153. typename timer_queue<Time_Traits>::per_timer_data& target,
  154. typename timer_queue<Time_Traits>::per_timer_data& source);
  155. // Run select once until interrupted or events are ready to be dispatched.
  156. BOOST_ASIO_DECL void run(long usec, op_queue<operation>& ops);
  157. // Interrupt the select loop.
  158. BOOST_ASIO_DECL void interrupt();
  159. private:
  160. #if defined(BOOST_ASIO_HAS_IOCP)
  161. // Run the select loop in the thread.
  162. BOOST_ASIO_DECL void run_thread();
  163. #endif // defined(BOOST_ASIO_HAS_IOCP)
  164. // Helper function to add a new timer queue.
  165. BOOST_ASIO_DECL void do_add_timer_queue(timer_queue_base& queue);
  166. // Helper function to remove a timer queue.
  167. BOOST_ASIO_DECL void do_remove_timer_queue(timer_queue_base& queue);
  168. // Get the timeout value for the select call.
  169. BOOST_ASIO_DECL timeval* get_timeout(long usec, timeval& tv);
  170. // Cancel all operations associated with the given descriptor. This function
  171. // does not acquire the select_reactor's mutex.
  172. BOOST_ASIO_DECL void cancel_ops_unlocked(socket_type descriptor,
  173. const boost::system::error_code& ec);
  174. // The scheduler implementation used to post completions.
  175. # if defined(BOOST_ASIO_HAS_IOCP)
  176. typedef class win_iocp_io_context scheduler_type;
  177. # else // defined(BOOST_ASIO_HAS_IOCP)
  178. typedef class scheduler scheduler_type;
  179. # endif // defined(BOOST_ASIO_HAS_IOCP)
  180. scheduler_type& scheduler_;
  181. // Mutex to protect access to internal data.
  182. boost::asio::detail::mutex mutex_;
  183. // The interrupter is used to break a blocking select call.
  184. select_interrupter interrupter_;
  185. // The queues of read, write and except operations.
  186. reactor_op_queue<socket_type> op_queue_[max_ops];
  187. // The file descriptor sets to be passed to the select system call.
  188. fd_set_adapter fd_sets_[max_select_ops];
  189. // The timer queues.
  190. timer_queue_set timer_queues_;
  191. #if defined(BOOST_ASIO_HAS_IOCP)
  192. // Helper class to run the reactor loop in a thread.
  193. class thread_function;
  194. friend class thread_function;
  195. // Does the reactor loop thread need to stop.
  196. bool stop_thread_;
  197. // The thread that is running the reactor loop.
  198. boost::asio::detail::thread* thread_;
  199. // Helper class to join and restart the reactor thread.
  200. class restart_reactor : public operation
  201. {
  202. public:
  203. restart_reactor(select_reactor* r)
  204. : operation(&restart_reactor::do_complete),
  205. reactor_(r)
  206. {
  207. }
  208. BOOST_ASIO_DECL static void do_complete(void* owner, operation* base,
  209. const boost::system::error_code& ec, std::size_t bytes_transferred);
  210. private:
  211. select_reactor* reactor_;
  212. };
  213. friend class restart_reactor;
  214. // Operation used to join and restart the reactor thread.
  215. restart_reactor restart_reactor_;
  216. #endif // defined(BOOST_ASIO_HAS_IOCP)
  217. // Whether the service has been shut down.
  218. bool shutdown_;
  219. };
  220. } // namespace detail
  221. } // namespace asio
  222. } // namespace boost
  223. #include <boost/asio/detail/pop_options.hpp>
  224. #include <boost/asio/detail/impl/select_reactor.hpp>
  225. #if defined(BOOST_ASIO_HEADER_ONLY)
  226. # include <boost/asio/detail/impl/select_reactor.ipp>
  227. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  228. #endif // defined(BOOST_ASIO_HAS_IOCP)
  229. // || (!defined(BOOST_ASIO_HAS_DEV_POLL)
  230. // && !defined(BOOST_ASIO_HAS_EPOLL)
  231. // && !defined(BOOST_ASIO_HAS_KQUEUE)
  232. // && !defined(BOOST_ASIO_WINDOWS_RUNTIME))
  233. #endif // BOOST_ASIO_DETAIL_SELECT_REACTOR_HPP