PageRenderTime 52ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/package/boost_1_58_0/boost/asio/detail/impl/select_reactor.ipp

https://gitlab.com/cdeclare/intcrypt
C++ Header | 315 lines | 237 code | 51 blank | 27 comment | 32 complexity | 7983ce48d81a35689d45da786237cdce MD5 | raw file
  1. //
  2. // detail/impl/select_reactor.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 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. fd_sets_[i].set(op_queue_[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. fd_sets_[write_op].set(op_queue_[connect_op], ops);
  162. if (fd_sets_[write_op].max_descriptor() > max_fd)
  163. max_fd = fd_sets_[write_op].max_descriptor();
  164. fd_sets_[except_op].set(op_queue_[connect_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. fd_sets_[except_op].perform(op_queue_[connect_op], ops);
  193. fd_sets_[write_op].perform(op_queue_[connect_op], ops);
  194. #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  195. // Exception operations must be processed first to ensure that any
  196. // out-of-band data is read before normal data.
  197. for (int i = max_select_ops - 1; i >= 0; --i)
  198. fd_sets_[i].perform(op_queue_[i], ops);
  199. }
  200. timer_queues_.get_ready_timers(ops);
  201. }
  202. void select_reactor::interrupt()
  203. {
  204. interrupter_.interrupt();
  205. }
  206. #if defined(BOOST_ASIO_HAS_IOCP)
  207. void select_reactor::run_thread()
  208. {
  209. boost::asio::detail::mutex::scoped_lock lock(mutex_);
  210. while (!stop_thread_)
  211. {
  212. lock.unlock();
  213. op_queue<operation> ops;
  214. run(true, ops);
  215. io_service_.post_deferred_completions(ops);
  216. lock.lock();
  217. }
  218. }
  219. void select_reactor::call_run_thread(select_reactor* reactor)
  220. {
  221. reactor->run_thread();
  222. }
  223. #endif // defined(BOOST_ASIO_HAS_IOCP)
  224. void select_reactor::do_add_timer_queue(timer_queue_base& queue)
  225. {
  226. mutex::scoped_lock lock(mutex_);
  227. timer_queues_.insert(&queue);
  228. }
  229. void select_reactor::do_remove_timer_queue(timer_queue_base& queue)
  230. {
  231. mutex::scoped_lock lock(mutex_);
  232. timer_queues_.erase(&queue);
  233. }
  234. timeval* select_reactor::get_timeout(timeval& tv)
  235. {
  236. // By default we will wait no longer than 5 minutes. This will ensure that
  237. // any changes to the system clock are detected after no longer than this.
  238. long usec = timer_queues_.wait_duration_usec(5 * 60 * 1000 * 1000);
  239. tv.tv_sec = usec / 1000000;
  240. tv.tv_usec = usec % 1000000;
  241. return &tv;
  242. }
  243. void select_reactor::cancel_ops_unlocked(socket_type descriptor,
  244. const boost::system::error_code& ec)
  245. {
  246. bool need_interrupt = false;
  247. op_queue<operation> ops;
  248. for (int i = 0; i < max_ops; ++i)
  249. need_interrupt = op_queue_[i].cancel_operations(
  250. descriptor, ops, ec) || need_interrupt;
  251. io_service_.post_deferred_completions(ops);
  252. if (need_interrupt)
  253. interrupter_.interrupt();
  254. }
  255. } // namespace detail
  256. } // namespace asio
  257. } // namespace boost
  258. #include <boost/asio/detail/pop_options.hpp>
  259. #endif // defined(BOOST_ASIO_HAS_IOCP)
  260. // || (!defined(BOOST_ASIO_HAS_DEV_POLL)
  261. // && !defined(BOOST_ASIO_HAS_EPOLL)
  262. // && !defined(BOOST_ASIO_HAS_KQUEUE))
  263. // && !defined(BOOST_ASIO_WINDOWS_RUNTIME))
  264. #endif // BOOST_ASIO_DETAIL_IMPL_SELECT_REACTOR_IPP