select_reactor.ipp 9.4 KB

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