select_reactor.ipp 11 KB

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