select_reactor.ipp 11 KB

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