select_reactor.ipp 8.9 KB

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