select_reactor.ipp 9.1 KB

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