select_reactor.ipp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. //
  2. // detail/impl/select_reactor.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2025 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_IMPL_SELECT_REACTOR_IPP
  11. #define BOOST_ASIO_DETAIL_IMPL_SELECT_REACTOR_IPP
  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 <boost/asio/detail/fd_set_adapter.hpp>
  22. #include <boost/asio/detail/select_reactor.hpp>
  23. #include <boost/asio/detail/signal_blocker.hpp>
  24. #include <boost/asio/detail/socket_ops.hpp>
  25. #if defined(BOOST_ASIO_HAS_IOCP)
  26. # include <boost/asio/detail/win_iocp_io_context.hpp>
  27. #else // defined(BOOST_ASIO_HAS_IOCP)
  28. # include <boost/asio/detail/scheduler.hpp>
  29. #endif // defined(BOOST_ASIO_HAS_IOCP)
  30. #include <boost/asio/detail/push_options.hpp>
  31. namespace boost {
  32. namespace asio {
  33. namespace detail {
  34. #if defined(BOOST_ASIO_HAS_IOCP)
  35. class select_reactor::thread_function
  36. {
  37. public:
  38. explicit thread_function(select_reactor* r)
  39. : this_(r)
  40. {
  41. }
  42. void operator()()
  43. {
  44. this_->run_thread();
  45. }
  46. private:
  47. select_reactor* this_;
  48. };
  49. #endif // defined(BOOST_ASIO_HAS_IOCP)
  50. select_reactor::select_reactor(boost::asio::execution_context& ctx)
  51. : execution_context_service_base<select_reactor>(ctx),
  52. scheduler_(use_service<scheduler_type>(ctx)),
  53. mutex_(),
  54. interrupter_(),
  55. #if defined(BOOST_ASIO_HAS_IOCP)
  56. stop_thread_(false),
  57. thread_(),
  58. restart_reactor_(this),
  59. #endif // defined(BOOST_ASIO_HAS_IOCP)
  60. shutdown_(false)
  61. {
  62. #if defined(BOOST_ASIO_HAS_IOCP)
  63. boost::asio::detail::signal_blocker sb;
  64. thread_ = thread(thread_function(this));
  65. #endif // defined(BOOST_ASIO_HAS_IOCP)
  66. }
  67. select_reactor::~select_reactor()
  68. {
  69. shutdown();
  70. }
  71. void select_reactor::shutdown()
  72. {
  73. boost::asio::detail::mutex::scoped_lock lock(mutex_);
  74. shutdown_ = true;
  75. #if defined(BOOST_ASIO_HAS_IOCP)
  76. stop_thread_ = true;
  77. if (thread_.joinable())
  78. interrupter_.interrupt();
  79. #endif // defined(BOOST_ASIO_HAS_IOCP)
  80. lock.unlock();
  81. #if defined(BOOST_ASIO_HAS_IOCP)
  82. thread_.join();
  83. #endif // defined(BOOST_ASIO_HAS_IOCP)
  84. op_queue<operation> ops;
  85. for (int i = 0; i < max_ops; ++i)
  86. op_queue_[i].get_all_operations(ops);
  87. timer_queues_.get_all_timers(ops);
  88. scheduler_.abandon_operations(ops);
  89. }
  90. void select_reactor::notify_fork(
  91. boost::asio::execution_context::fork_event fork_ev)
  92. {
  93. #if defined(BOOST_ASIO_HAS_IOCP)
  94. (void)fork_ev;
  95. #else // defined(BOOST_ASIO_HAS_IOCP)
  96. if (fork_ev == boost::asio::execution_context::fork_child)
  97. interrupter_.recreate();
  98. #endif // defined(BOOST_ASIO_HAS_IOCP)
  99. }
  100. void select_reactor::init_task()
  101. {
  102. scheduler_.init_task();
  103. }
  104. int select_reactor::register_descriptor(socket_type,
  105. select_reactor::per_descriptor_data&)
  106. {
  107. return 0;
  108. }
  109. int select_reactor::register_internal_descriptor(
  110. int op_type, socket_type descriptor,
  111. select_reactor::per_descriptor_data&, reactor_op* op)
  112. {
  113. boost::asio::detail::mutex::scoped_lock lock(mutex_);
  114. op_queue_[op_type].enqueue_operation(descriptor, op);
  115. interrupter_.interrupt();
  116. return 0;
  117. }
  118. void select_reactor::move_descriptor(socket_type,
  119. select_reactor::per_descriptor_data&,
  120. select_reactor::per_descriptor_data&)
  121. {
  122. }
  123. void select_reactor::call_post_immediate_completion(
  124. operation* op, bool is_continuation, const void* self)
  125. {
  126. static_cast<const select_reactor*>(self)->post_immediate_completion(
  127. op, is_continuation);
  128. }
  129. void select_reactor::start_op(int op_type, socket_type descriptor,
  130. select_reactor::per_descriptor_data&, reactor_op* op, bool is_continuation,
  131. bool, void (*on_immediate)(operation*, bool, const void*),
  132. const void* immediate_arg)
  133. {
  134. boost::asio::detail::mutex::scoped_lock lock(mutex_);
  135. if (shutdown_)
  136. {
  137. on_immediate(op, is_continuation, immediate_arg);
  138. return;
  139. }
  140. bool first = op_queue_[op_type].enqueue_operation(descriptor, op);
  141. scheduler_.work_started();
  142. if (first)
  143. interrupter_.interrupt();
  144. }
  145. void select_reactor::cancel_ops(socket_type descriptor,
  146. select_reactor::per_descriptor_data&)
  147. {
  148. boost::asio::detail::mutex::scoped_lock lock(mutex_);
  149. cancel_ops_unlocked(descriptor, boost::asio::error::operation_aborted);
  150. }
  151. void select_reactor::cancel_ops_by_key(socket_type descriptor,
  152. select_reactor::per_descriptor_data&,
  153. int op_type, void* cancellation_key)
  154. {
  155. boost::asio::detail::mutex::scoped_lock lock(mutex_);
  156. op_queue<operation> ops;
  157. bool need_interrupt = op_queue_[op_type].cancel_operations_by_key(
  158. descriptor, ops, cancellation_key, boost::asio::error::operation_aborted);
  159. scheduler_.post_deferred_completions(ops);
  160. if (need_interrupt)
  161. interrupter_.interrupt();
  162. }
  163. void select_reactor::deregister_descriptor(socket_type descriptor,
  164. select_reactor::per_descriptor_data&, bool)
  165. {
  166. boost::asio::detail::mutex::scoped_lock lock(mutex_);
  167. cancel_ops_unlocked(descriptor, boost::asio::error::operation_aborted);
  168. }
  169. void select_reactor::deregister_internal_descriptor(
  170. socket_type descriptor, select_reactor::per_descriptor_data&)
  171. {
  172. boost::asio::detail::mutex::scoped_lock lock(mutex_);
  173. op_queue<operation> ops;
  174. for (int i = 0; i < max_ops; ++i)
  175. op_queue_[i].cancel_operations(descriptor, ops);
  176. }
  177. void select_reactor::cleanup_descriptor_data(
  178. select_reactor::per_descriptor_data&)
  179. {
  180. }
  181. void select_reactor::run(long usec, op_queue<operation>& ops)
  182. {
  183. boost::asio::detail::mutex::scoped_lock lock(mutex_);
  184. #if defined(BOOST_ASIO_HAS_IOCP)
  185. // Check if the thread is supposed to stop.
  186. if (stop_thread_)
  187. return;
  188. #endif // defined(BOOST_ASIO_HAS_IOCP)
  189. // Set up the descriptor sets.
  190. for (int i = 0; i < max_select_ops; ++i)
  191. fd_sets_[i].reset();
  192. fd_sets_[read_op].set(interrupter_.read_descriptor());
  193. socket_type max_fd = 0;
  194. bool have_work_to_do = !timer_queues_.all_empty();
  195. for (int i = 0; i < max_select_ops; ++i)
  196. {
  197. have_work_to_do = have_work_to_do || !op_queue_[i].empty();
  198. fd_sets_[i].set(op_queue_[i], ops);
  199. if (fd_sets_[i].max_descriptor() > max_fd)
  200. max_fd = fd_sets_[i].max_descriptor();
  201. }
  202. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  203. // Connection operations on Windows use both except and write fd_sets.
  204. have_work_to_do = have_work_to_do || !op_queue_[connect_op].empty();
  205. fd_sets_[write_op].set(op_queue_[connect_op], ops);
  206. if (fd_sets_[write_op].max_descriptor() > max_fd)
  207. max_fd = fd_sets_[write_op].max_descriptor();
  208. fd_sets_[except_op].set(op_queue_[connect_op], ops);
  209. if (fd_sets_[except_op].max_descriptor() > max_fd)
  210. max_fd = fd_sets_[except_op].max_descriptor();
  211. #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  212. // We can return immediately if there's no work to do and the reactor is
  213. // not supposed to block.
  214. if (!usec && !have_work_to_do)
  215. return;
  216. // Determine how long to block while waiting for events.
  217. timeval tv_buf = { 0, 0 };
  218. timeval* tv = usec ? get_timeout(usec, tv_buf) : &tv_buf;
  219. lock.unlock();
  220. // Block on the select call until descriptors become ready.
  221. boost::system::error_code ec;
  222. int retval = socket_ops::select(static_cast<int>(max_fd + 1),
  223. fd_sets_[read_op], fd_sets_[write_op], fd_sets_[except_op], tv, ec);
  224. // Reset the interrupter.
  225. if (retval > 0 && fd_sets_[read_op].is_set(interrupter_.read_descriptor()))
  226. {
  227. if (!interrupter_.reset())
  228. {
  229. lock.lock();
  230. #if defined(BOOST_ASIO_HAS_IOCP)
  231. stop_thread_ = true;
  232. scheduler_.post_immediate_completion(&restart_reactor_, false);
  233. #else // defined(BOOST_ASIO_HAS_IOCP)
  234. interrupter_.recreate();
  235. #endif // defined(BOOST_ASIO_HAS_IOCP)
  236. }
  237. --retval;
  238. }
  239. lock.lock();
  240. // Dispatch all ready operations.
  241. if (retval > 0)
  242. {
  243. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  244. // Connection operations on Windows use both except and write fd_sets.
  245. fd_sets_[except_op].perform(op_queue_[connect_op], ops);
  246. fd_sets_[write_op].perform(op_queue_[connect_op], ops);
  247. #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  248. // Exception operations must be processed first to ensure that any
  249. // out-of-band data is read before normal data.
  250. for (int i = max_select_ops - 1; i >= 0; --i)
  251. fd_sets_[i].perform(op_queue_[i], ops);
  252. }
  253. timer_queues_.get_ready_timers(ops);
  254. }
  255. void select_reactor::interrupt()
  256. {
  257. interrupter_.interrupt();
  258. }
  259. #if defined(BOOST_ASIO_HAS_IOCP)
  260. void select_reactor::run_thread()
  261. {
  262. boost::asio::detail::mutex::scoped_lock lock(mutex_);
  263. while (!stop_thread_)
  264. {
  265. lock.unlock();
  266. op_queue<operation> ops;
  267. run(-1, ops);
  268. scheduler_.post_deferred_completions(ops);
  269. lock.lock();
  270. }
  271. }
  272. void select_reactor::restart_reactor::do_complete(void* owner, operation* base,
  273. const boost::system::error_code& /*ec*/, std::size_t /*bytes_transferred*/)
  274. {
  275. if (owner)
  276. {
  277. select_reactor* reactor = static_cast<restart_reactor*>(base)->reactor_;
  278. reactor->thread_.join();
  279. boost::asio::detail::mutex::scoped_lock lock(reactor->mutex_);
  280. reactor->interrupter_.recreate();
  281. reactor->stop_thread_ = false;
  282. lock.unlock();
  283. boost::asio::detail::signal_blocker sb;
  284. reactor->thread_ = thread(thread_function(reactor));
  285. }
  286. }
  287. #endif // defined(BOOST_ASIO_HAS_IOCP)
  288. void select_reactor::do_add_timer_queue(timer_queue_base& queue)
  289. {
  290. mutex::scoped_lock lock(mutex_);
  291. timer_queues_.insert(&queue);
  292. }
  293. void select_reactor::do_remove_timer_queue(timer_queue_base& queue)
  294. {
  295. mutex::scoped_lock lock(mutex_);
  296. timer_queues_.erase(&queue);
  297. }
  298. timeval* select_reactor::get_timeout(long usec, timeval& tv)
  299. {
  300. // By default we will wait no longer than 5 minutes. This will ensure that
  301. // any changes to the system clock are detected after no longer than this.
  302. const long max_usec = 5 * 60 * 1000 * 1000;
  303. usec = timer_queues_.wait_duration_usec(
  304. (usec < 0 || max_usec < usec) ? max_usec : usec);
  305. tv.tv_sec = usec / 1000000;
  306. tv.tv_usec = usec % 1000000;
  307. return &tv;
  308. }
  309. void select_reactor::cancel_ops_unlocked(socket_type descriptor,
  310. const boost::system::error_code& ec)
  311. {
  312. bool need_interrupt = false;
  313. op_queue<operation> ops;
  314. for (int i = 0; i < max_ops; ++i)
  315. need_interrupt = op_queue_[i].cancel_operations(
  316. descriptor, ops, ec) || need_interrupt;
  317. scheduler_.post_deferred_completions(ops);
  318. if (need_interrupt)
  319. interrupter_.interrupt();
  320. }
  321. } // namespace detail
  322. } // namespace asio
  323. } // namespace boost
  324. #include <boost/asio/detail/pop_options.hpp>
  325. #endif // defined(BOOST_ASIO_HAS_IOCP)
  326. // || (!defined(BOOST_ASIO_HAS_DEV_POLL)
  327. // && !defined(BOOST_ASIO_HAS_EPOLL)
  328. // && !defined(BOOST_ASIO_HAS_KQUEUE))
  329. // && !defined(BOOST_ASIO_WINDOWS_RUNTIME))
  330. #endif // BOOST_ASIO_DETAIL_IMPL_SELECT_REACTOR_IPP