epoll_reactor.hpp 9.2 KB

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