epoll_reactor.hpp 10 KB

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