Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

267 lines
8.9KB

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