select_reactor.hpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. //
  2. // detail/select_reactor.hpp
  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_SELECT_REACTOR_HPP
  11. #define BOOST_ASIO_DETAIL_SELECT_REACTOR_HPP
  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 <cstddef>
  22. #include <boost/asio/detail/fd_set_adapter.hpp>
  23. #include <boost/asio/detail/limits.hpp>
  24. #include <boost/asio/detail/mutex.hpp>
  25. #include <boost/asio/detail/op_queue.hpp>
  26. #include <boost/asio/detail/reactor_op.hpp>
  27. #include <boost/asio/detail/reactor_op_queue.hpp>
  28. #include <boost/asio/detail/select_interrupter.hpp>
  29. #include <boost/asio/detail/socket_types.hpp>
  30. #include <boost/asio/detail/timer_queue_base.hpp>
  31. #include <boost/asio/detail/timer_queue_set.hpp>
  32. #include <boost/asio/detail/wait_op.hpp>
  33. #include <boost/asio/io_service.hpp>
  34. #if defined(BOOST_ASIO_HAS_IOCP)
  35. # include <boost/asio/detail/thread.hpp>
  36. #endif // defined(BOOST_ASIO_HAS_IOCP)
  37. #include <boost/asio/detail/push_options.hpp>
  38. namespace boost {
  39. namespace asio {
  40. namespace detail {
  41. class select_reactor
  42. : public boost::asio::detail::service_base<select_reactor>
  43. {
  44. public:
  45. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  46. enum op_types { read_op = 0, write_op = 1, except_op = 2,
  47. max_select_ops = 3, connect_op = 3, max_ops = 4 };
  48. #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  49. enum op_types { read_op = 0, write_op = 1, except_op = 2,
  50. max_select_ops = 3, connect_op = 1, max_ops = 3 };
  51. #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  52. // Per-descriptor data.
  53. struct per_descriptor_data
  54. {
  55. };
  56. // Constructor.
  57. BOOST_ASIO_DECL select_reactor(boost::asio::io_service& io_service);
  58. // Destructor.
  59. BOOST_ASIO_DECL ~select_reactor();
  60. // Destroy all user-defined handler objects owned by the service.
  61. BOOST_ASIO_DECL void shutdown_service();
  62. // Recreate internal descriptors following a fork.
  63. BOOST_ASIO_DECL void fork_service(
  64. boost::asio::io_service::fork_event fork_ev);
  65. // Initialise the task, but only if the reactor is not in its own thread.
  66. BOOST_ASIO_DECL void init_task();
  67. // Register a socket with the reactor. Returns 0 on success, system error
  68. // code on failure.
  69. BOOST_ASIO_DECL int register_descriptor(socket_type, per_descriptor_data&);
  70. // Register a descriptor with an associated single operation. Returns 0 on
  71. // success, system error code on failure.
  72. BOOST_ASIO_DECL int register_internal_descriptor(
  73. int op_type, socket_type descriptor,
  74. per_descriptor_data& descriptor_data, reactor_op* op);
  75. // Post a reactor operation for immediate completion.
  76. void post_immediate_completion(reactor_op* op, bool is_continuation)
  77. {
  78. io_service_.post_immediate_completion(op, is_continuation);
  79. }
  80. // Start a new operation. The reactor operation will be performed when the
  81. // given descriptor is flagged as ready, or an error has occurred.
  82. BOOST_ASIO_DECL void start_op(int op_type, socket_type descriptor,
  83. per_descriptor_data&, reactor_op* op, bool is_continuation, bool);
  84. // Cancel all operations associated with the given descriptor. The
  85. // handlers associated with the descriptor will be invoked with the
  86. // operation_aborted error.
  87. BOOST_ASIO_DECL void cancel_ops(socket_type descriptor, per_descriptor_data&);
  88. // Cancel any operations that are running against the descriptor and remove
  89. // its registration from the reactor.
  90. BOOST_ASIO_DECL void deregister_descriptor(socket_type descriptor,
  91. per_descriptor_data&, bool closing);
  92. // Remote the descriptor's registration from the reactor.
  93. BOOST_ASIO_DECL void deregister_internal_descriptor(
  94. socket_type descriptor, per_descriptor_data& descriptor_data);
  95. // Move descriptor registration from one descriptor_data object to another.
  96. BOOST_ASIO_DECL void move_descriptor(socket_type descriptor,
  97. per_descriptor_data& target_descriptor_data,
  98. per_descriptor_data& source_descriptor_data);
  99. // Add a new timer queue to the reactor.
  100. template <typename Time_Traits>
  101. void add_timer_queue(timer_queue<Time_Traits>& queue);
  102. // Remove a timer queue from the reactor.
  103. template <typename Time_Traits>
  104. void remove_timer_queue(timer_queue<Time_Traits>& queue);
  105. // Schedule a new operation in the given timer queue to expire at the
  106. // specified absolute time.
  107. template <typename Time_Traits>
  108. void schedule_timer(timer_queue<Time_Traits>& queue,
  109. const typename Time_Traits::time_type& time,
  110. typename timer_queue<Time_Traits>::per_timer_data& timer, wait_op* op);
  111. // Cancel the timer operations associated with the given token. Returns the
  112. // number of operations that have been posted or dispatched.
  113. template <typename Time_Traits>
  114. std::size_t cancel_timer(timer_queue<Time_Traits>& queue,
  115. typename timer_queue<Time_Traits>::per_timer_data& timer,
  116. std::size_t max_cancelled = (std::numeric_limits<std::size_t>::max)());
  117. // Run select once until interrupted or events are ready to be dispatched.
  118. BOOST_ASIO_DECL void run(bool block, op_queue<operation>& ops);
  119. // Interrupt the select loop.
  120. BOOST_ASIO_DECL void interrupt();
  121. private:
  122. #if defined(BOOST_ASIO_HAS_IOCP)
  123. // Run the select loop in the thread.
  124. BOOST_ASIO_DECL void run_thread();
  125. // Entry point for the select loop thread.
  126. BOOST_ASIO_DECL static void call_run_thread(select_reactor* reactor);
  127. #endif // defined(BOOST_ASIO_HAS_IOCP)
  128. // Helper function to add a new timer queue.
  129. BOOST_ASIO_DECL void do_add_timer_queue(timer_queue_base& queue);
  130. // Helper function to remove a timer queue.
  131. BOOST_ASIO_DECL void do_remove_timer_queue(timer_queue_base& queue);
  132. // Get the timeout value for the select call.
  133. BOOST_ASIO_DECL timeval* get_timeout(timeval& tv);
  134. // Cancel all operations associated with the given descriptor. This function
  135. // does not acquire the select_reactor's mutex.
  136. BOOST_ASIO_DECL void cancel_ops_unlocked(socket_type descriptor,
  137. const boost::system::error_code& ec);
  138. // The io_service implementation used to post completions.
  139. io_service_impl& io_service_;
  140. // Mutex to protect access to internal data.
  141. boost::asio::detail::mutex mutex_;
  142. // The interrupter is used to break a blocking select call.
  143. select_interrupter interrupter_;
  144. // The queues of read, write and except operations.
  145. reactor_op_queue<socket_type> op_queue_[max_ops];
  146. // The file descriptor sets to be passed to the select system call.
  147. fd_set_adapter fd_sets_[max_select_ops];
  148. // The timer queues.
  149. timer_queue_set timer_queues_;
  150. #if defined(BOOST_ASIO_HAS_IOCP)
  151. // Does the reactor loop thread need to stop.
  152. bool stop_thread_;
  153. // The thread that is running the reactor loop.
  154. boost::asio::detail::thread* thread_;
  155. #endif // defined(BOOST_ASIO_HAS_IOCP)
  156. // Whether the service has been shut down.
  157. bool shutdown_;
  158. };
  159. } // namespace detail
  160. } // namespace asio
  161. } // namespace boost
  162. #include <boost/asio/detail/pop_options.hpp>
  163. #include <boost/asio/detail/impl/select_reactor.hpp>
  164. #if defined(BOOST_ASIO_HEADER_ONLY)
  165. # include <boost/asio/detail/impl/select_reactor.ipp>
  166. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  167. #endif // defined(BOOST_ASIO_HAS_IOCP)
  168. // || (!defined(BOOST_ASIO_HAS_DEV_POLL)
  169. // && !defined(BOOST_ASIO_HAS_EPOLL)
  170. // && !defined(BOOST_ASIO_HAS_KQUEUE)
  171. // && !defined(BOOST_ASIO_WINDOWS_RUNTIME))
  172. #endif // BOOST_ASIO_DETAIL_SELECT_REACTOR_HPP