select_reactor.ipp 9.1 KB

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