select_reactor.ipp 7.4 KB

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