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.

epoll_reactor.hpp 8.1KB

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