epoll_reactor.hpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. //
  2. // detail/epoll_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_EPOLL_REACTOR_HPP
  11. #define BOOST_ASIO_DETAIL_EPOLL_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_EPOLL)
  17. #include <boost/asio/io_service.hpp>
  18. #include <boost/asio/detail/atomic_count.hpp>
  19. #include <boost/asio/detail/limits.hpp>
  20. #include <boost/asio/detail/mutex.hpp>
  21. #include <boost/asio/detail/object_pool.hpp>
  22. #include <boost/asio/detail/op_queue.hpp>
  23. #include <boost/asio/detail/reactor_op.hpp>
  24. #include <boost/asio/detail/select_interrupter.hpp>
  25. #include <boost/asio/detail/socket_types.hpp>
  26. #include <boost/asio/detail/timer_queue_base.hpp>
  27. #include <boost/asio/detail/timer_queue_set.hpp>
  28. #include <boost/asio/detail/wait_op.hpp>
  29. #include <boost/asio/detail/push_options.hpp>
  30. namespace boost {
  31. namespace asio {
  32. namespace detail {
  33. class epoll_reactor
  34. : public boost::asio::detail::service_base<epoll_reactor>
  35. {
  36. public:
  37. enum op_types { read_op = 0, write_op = 1,
  38. connect_op = 1, except_op = 2, max_ops = 3 };
  39. // Per-descriptor queues.
  40. class descriptor_state : operation
  41. {
  42. friend class epoll_reactor;
  43. friend class object_pool_access;
  44. descriptor_state* next_;
  45. descriptor_state* prev_;
  46. mutex mutex_;
  47. epoll_reactor* reactor_;
  48. int descriptor_;
  49. uint32_t registered_events_;
  50. op_queue<reactor_op> op_queue_[max_ops];
  51. bool shutdown_;
  52. BOOST_ASIO_DECL descriptor_state();
  53. void set_ready_events(uint32_t events) { task_result_ = events; }
  54. BOOST_ASIO_DECL operation* perform_io(uint32_t events);
  55. BOOST_ASIO_DECL static void do_complete(
  56. io_service_impl* owner, operation* base,
  57. const boost::system::error_code& ec, std::size_t bytes_transferred);
  58. };
  59. // Per-descriptor data.
  60. typedef descriptor_state* per_descriptor_data;
  61. // Constructor.
  62. BOOST_ASIO_DECL epoll_reactor(boost::asio::io_service& io_service);
  63. // Destructor.
  64. BOOST_ASIO_DECL ~epoll_reactor();
  65. // Destroy all user-defined handler objects owned by the service.
  66. BOOST_ASIO_DECL void shutdown_service();
  67. // Recreate internal descriptors following a fork.
  68. BOOST_ASIO_DECL void fork_service(
  69. boost::asio::io_service::fork_event fork_ev);
  70. // Initialise the task.
  71. BOOST_ASIO_DECL void init_task();
  72. // Register a socket with the reactor. Returns 0 on success, system error
  73. // code on failure.
  74. BOOST_ASIO_DECL int register_descriptor(socket_type descriptor,
  75. per_descriptor_data& descriptor_data);
  76. // Register a descriptor with an associated single operation. Returns 0 on
  77. // success, system error code on failure.
  78. BOOST_ASIO_DECL int register_internal_descriptor(
  79. int op_type, socket_type descriptor,
  80. per_descriptor_data& descriptor_data, reactor_op* op);
  81. // Move descriptor registration from one descriptor_data object to another.
  82. BOOST_ASIO_DECL void move_descriptor(socket_type descriptor,
  83. per_descriptor_data& target_descriptor_data,
  84. per_descriptor_data& source_descriptor_data);
  85. // Post a reactor operation for immediate completion.
  86. void post_immediate_completion(reactor_op* op, bool is_continuation)
  87. {
  88. io_service_.post_immediate_completion(op, is_continuation);
  89. }
  90. // Start a new operation. The reactor operation will be performed when the
  91. // given descriptor is flagged as ready, or an error has occurred.
  92. BOOST_ASIO_DECL void start_op(int op_type, socket_type descriptor,
  93. per_descriptor_data& descriptor_data, reactor_op* op,
  94. bool is_continuation, bool allow_speculative);
  95. // Cancel all operations associated with the given descriptor. The
  96. // handlers associated with the descriptor will be invoked with the
  97. // operation_aborted error.
  98. BOOST_ASIO_DECL void cancel_ops(socket_type descriptor,
  99. per_descriptor_data& descriptor_data);
  100. // Cancel any operations that are running against the descriptor and remove
  101. // its registration from the reactor.
  102. BOOST_ASIO_DECL void deregister_descriptor(socket_type descriptor,
  103. per_descriptor_data& descriptor_data, bool closing);
  104. // Remote the descriptor's registration from the reactor.
  105. BOOST_ASIO_DECL void deregister_internal_descriptor(
  106. socket_type descriptor, per_descriptor_data& descriptor_data);
  107. // Add a new timer queue to the reactor.
  108. template <typename Time_Traits>
  109. void add_timer_queue(timer_queue<Time_Traits>& timer_queue);
  110. // Remove a timer queue from the reactor.
  111. template <typename Time_Traits>
  112. void remove_timer_queue(timer_queue<Time_Traits>& timer_queue);
  113. // Schedule a new operation in the given timer queue to expire at the
  114. // specified absolute time.
  115. template <typename Time_Traits>
  116. void schedule_timer(timer_queue<Time_Traits>& queue,
  117. const typename Time_Traits::time_type& time,
  118. typename timer_queue<Time_Traits>::per_timer_data& timer, wait_op* op);
  119. // Cancel the timer operations associated with the given token. Returns the
  120. // number of operations that have been posted or dispatched.
  121. template <typename Time_Traits>
  122. std::size_t cancel_timer(timer_queue<Time_Traits>& queue,
  123. typename timer_queue<Time_Traits>::per_timer_data& timer,
  124. std::size_t max_cancelled = (std::numeric_limits<std::size_t>::max)());
  125. // Run epoll once until interrupted or events are ready to be dispatched.
  126. BOOST_ASIO_DECL void run(bool block, op_queue<operation>& ops);
  127. // Interrupt the select loop.
  128. BOOST_ASIO_DECL void interrupt();
  129. private:
  130. // The hint to pass to epoll_create to size its data structures.
  131. enum { epoll_size = 20000 };
  132. // Create the epoll file descriptor. Throws an exception if the descriptor
  133. // cannot be created.
  134. BOOST_ASIO_DECL static int do_epoll_create();
  135. // Create the timerfd file descriptor. Does not throw.
  136. BOOST_ASIO_DECL static int do_timerfd_create();
  137. // Allocate a new descriptor state object.
  138. BOOST_ASIO_DECL descriptor_state* allocate_descriptor_state();
  139. // Free an existing descriptor state object.
  140. BOOST_ASIO_DECL void free_descriptor_state(descriptor_state* s);
  141. // Helper function to add a new timer queue.
  142. BOOST_ASIO_DECL void do_add_timer_queue(timer_queue_base& queue);
  143. // Helper function to remove a timer queue.
  144. BOOST_ASIO_DECL void do_remove_timer_queue(timer_queue_base& queue);
  145. // Called to recalculate and update the timeout.
  146. BOOST_ASIO_DECL void update_timeout();
  147. // Get the timeout value for the epoll_wait call. The timeout value is
  148. // returned as a number of milliseconds. A return value of -1 indicates
  149. // that epoll_wait should block indefinitely.
  150. BOOST_ASIO_DECL int get_timeout();
  151. #if defined(BOOST_ASIO_HAS_TIMERFD)
  152. // Get the timeout value for the timer descriptor. The return value is the
  153. // flag argument to be used when calling timerfd_settime.
  154. BOOST_ASIO_DECL int get_timeout(itimerspec& ts);
  155. #endif // defined(BOOST_ASIO_HAS_TIMERFD)
  156. // The io_service implementation used to post completions.
  157. io_service_impl& io_service_;
  158. // Mutex to protect access to internal data.
  159. mutex mutex_;
  160. // The interrupter is used to break a blocking epoll_wait call.
  161. select_interrupter interrupter_;
  162. // The epoll file descriptor.
  163. int epoll_fd_;
  164. // The timer file descriptor.
  165. int timer_fd_;
  166. // The timer queues.
  167. timer_queue_set timer_queues_;
  168. // Whether the service has been shut down.
  169. bool shutdown_;
  170. // Mutex to protect access to the registered descriptors.
  171. mutex registered_descriptors_mutex_;
  172. // Keep track of all registered descriptors.
  173. object_pool<descriptor_state> registered_descriptors_;
  174. // Helper class to do post-perform_io cleanup.
  175. struct perform_io_cleanup_on_block_exit;
  176. friend struct perform_io_cleanup_on_block_exit;
  177. };
  178. } // namespace detail
  179. } // namespace asio
  180. } // namespace boost
  181. #include <boost/asio/detail/pop_options.hpp>
  182. #include <boost/asio/detail/impl/epoll_reactor.hpp>
  183. #if defined(BOOST_ASIO_HEADER_ONLY)
  184. # include <boost/asio/detail/impl/epoll_reactor.ipp>
  185. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  186. #endif // defined(BOOST_ASIO_HAS_EPOLL)
  187. #endif // BOOST_ASIO_DETAIL_EPOLL_REACTOR_HPP