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.

kqueue_reactor.hpp 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. //
  2. // detail/kqueue_reactor.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. // Copyright (c) 2005 Stefan Arentz (stefan at soze dot com)
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. #ifndef ASIO_DETAIL_KQUEUE_REACTOR_HPP
  12. #define ASIO_DETAIL_KQUEUE_REACTOR_HPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include "asio/detail/config.hpp"
  17. #if defined(ASIO_HAS_KQUEUE)
  18. #include <cstddef>
  19. #include <sys/types.h>
  20. #include <sys/event.h>
  21. #include <sys/time.h>
  22. #include "asio/detail/limits.hpp"
  23. #include "asio/detail/mutex.hpp"
  24. #include "asio/detail/object_pool.hpp"
  25. #include "asio/detail/op_queue.hpp"
  26. #include "asio/detail/reactor_op.hpp"
  27. #include "asio/detail/select_interrupter.hpp"
  28. #include "asio/detail/socket_types.hpp"
  29. #include "asio/detail/timer_queue_base.hpp"
  30. #include "asio/detail/timer_queue_set.hpp"
  31. #include "asio/detail/wait_op.hpp"
  32. #include "asio/error.hpp"
  33. #include "asio/execution_context.hpp"
  34. // Older versions of Mac OS X may not define EV_OOBAND.
  35. #if !defined(EV_OOBAND)
  36. # define EV_OOBAND EV_FLAG1
  37. #endif // !defined(EV_OOBAND)
  38. #include "asio/detail/push_options.hpp"
  39. namespace asio {
  40. namespace detail {
  41. class scheduler;
  42. class kqueue_reactor
  43. : public execution_context_service_base<kqueue_reactor>
  44. {
  45. private:
  46. // The mutex type used by this reactor.
  47. typedef conditionally_enabled_mutex mutex;
  48. public:
  49. enum op_types { read_op = 0, write_op = 1,
  50. connect_op = 1, except_op = 2, max_ops = 3 };
  51. // Per-descriptor queues.
  52. struct descriptor_state
  53. {
  54. descriptor_state(bool locking) : mutex_(locking) {}
  55. friend class kqueue_reactor;
  56. friend class object_pool_access;
  57. descriptor_state* next_;
  58. descriptor_state* prev_;
  59. mutex mutex_;
  60. int descriptor_;
  61. int num_kevents_; // 1 == read only, 2 == read and write
  62. op_queue<reactor_op> op_queue_[max_ops];
  63. bool shutdown_;
  64. };
  65. // Per-descriptor data.
  66. typedef descriptor_state* per_descriptor_data;
  67. // Constructor.
  68. ASIO_DECL kqueue_reactor(asio::execution_context& ctx);
  69. // Destructor.
  70. ASIO_DECL ~kqueue_reactor();
  71. // Destroy all user-defined handler objects owned by the service.
  72. ASIO_DECL void shutdown();
  73. // Recreate internal descriptors following a fork.
  74. ASIO_DECL void notify_fork(
  75. asio::execution_context::fork_event fork_ev);
  76. // Initialise the task.
  77. ASIO_DECL void init_task();
  78. // Register a socket with the reactor. Returns 0 on success, system error
  79. // code on failure.
  80. ASIO_DECL int register_descriptor(socket_type descriptor,
  81. per_descriptor_data& descriptor_data);
  82. // Register a descriptor with an associated single operation. Returns 0 on
  83. // success, system error code on failure.
  84. ASIO_DECL int register_internal_descriptor(
  85. int op_type, socket_type descriptor,
  86. per_descriptor_data& descriptor_data, reactor_op* op);
  87. // Move descriptor registration from one descriptor_data object to another.
  88. ASIO_DECL void move_descriptor(socket_type descriptor,
  89. per_descriptor_data& target_descriptor_data,
  90. per_descriptor_data& source_descriptor_data);
  91. // Post a reactor operation for immediate completion.
  92. void post_immediate_completion(reactor_op* op, bool is_continuation)
  93. {
  94. scheduler_.post_immediate_completion(op, is_continuation);
  95. }
  96. // Start a new operation. The reactor operation will be performed when the
  97. // given descriptor is flagged as ready, or an error has occurred.
  98. ASIO_DECL void start_op(int op_type, socket_type descriptor,
  99. per_descriptor_data& descriptor_data, reactor_op* op,
  100. bool is_continuation, bool allow_speculative);
  101. // Cancel all operations associated with the given descriptor. The
  102. // handlers associated with the descriptor will be invoked with the
  103. // operation_aborted error.
  104. ASIO_DECL void cancel_ops(socket_type descriptor,
  105. per_descriptor_data& descriptor_data);
  106. // Cancel any operations that are running against the descriptor and remove
  107. // its registration from the reactor. The reactor resources associated with
  108. // the descriptor must be released by calling cleanup_descriptor_data.
  109. ASIO_DECL void deregister_descriptor(socket_type descriptor,
  110. per_descriptor_data& descriptor_data, bool closing);
  111. // Remove the descriptor's registration from the reactor. The reactor
  112. // resources associated with the descriptor must be released by calling
  113. // cleanup_descriptor_data.
  114. ASIO_DECL void deregister_internal_descriptor(
  115. socket_type descriptor, per_descriptor_data& descriptor_data);
  116. // Perform any post-deregistration cleanup tasks associated with the
  117. // descriptor data.
  118. ASIO_DECL void cleanup_descriptor_data(
  119. per_descriptor_data& descriptor_data);
  120. // Add a new timer queue to the reactor.
  121. template <typename Time_Traits>
  122. void add_timer_queue(timer_queue<Time_Traits>& queue);
  123. // Remove a timer queue from the reactor.
  124. template <typename Time_Traits>
  125. void remove_timer_queue(timer_queue<Time_Traits>& queue);
  126. // Schedule a new operation in the given timer queue to expire at the
  127. // specified absolute time.
  128. template <typename Time_Traits>
  129. void schedule_timer(timer_queue<Time_Traits>& queue,
  130. const typename Time_Traits::time_type& time,
  131. typename timer_queue<Time_Traits>::per_timer_data& timer, wait_op* op);
  132. // Cancel the timer operations associated with the given token. Returns the
  133. // number of operations that have been posted or dispatched.
  134. template <typename Time_Traits>
  135. std::size_t cancel_timer(timer_queue<Time_Traits>& queue,
  136. typename timer_queue<Time_Traits>::per_timer_data& timer,
  137. std::size_t max_cancelled = (std::numeric_limits<std::size_t>::max)());
  138. // Move the timer operations associated with the given timer.
  139. template <typename Time_Traits>
  140. void move_timer(timer_queue<Time_Traits>& queue,
  141. typename timer_queue<Time_Traits>::per_timer_data& target,
  142. typename timer_queue<Time_Traits>::per_timer_data& source);
  143. // Run the kqueue loop.
  144. ASIO_DECL void run(long usec, op_queue<operation>& ops);
  145. // Interrupt the kqueue loop.
  146. ASIO_DECL void interrupt();
  147. private:
  148. // Create the kqueue file descriptor. Throws an exception if the descriptor
  149. // cannot be created.
  150. ASIO_DECL static int do_kqueue_create();
  151. // Allocate a new descriptor state object.
  152. ASIO_DECL descriptor_state* allocate_descriptor_state();
  153. // Free an existing descriptor state object.
  154. ASIO_DECL void free_descriptor_state(descriptor_state* s);
  155. // Helper function to add a new timer queue.
  156. ASIO_DECL void do_add_timer_queue(timer_queue_base& queue);
  157. // Helper function to remove a timer queue.
  158. ASIO_DECL void do_remove_timer_queue(timer_queue_base& queue);
  159. // Get the timeout value for the kevent call.
  160. ASIO_DECL timespec* get_timeout(long usec, timespec& ts);
  161. // The scheduler used to post completions.
  162. scheduler& scheduler_;
  163. // Mutex to protect access to internal data.
  164. mutex mutex_;
  165. // The kqueue file descriptor.
  166. int kqueue_fd_;
  167. // The interrupter is used to break a blocking kevent call.
  168. select_interrupter interrupter_;
  169. // The timer queues.
  170. timer_queue_set timer_queues_;
  171. // Whether the service has been shut down.
  172. bool shutdown_;
  173. // Mutex to protect access to the registered descriptors.
  174. mutex registered_descriptors_mutex_;
  175. // Keep track of all registered descriptors.
  176. object_pool<descriptor_state> registered_descriptors_;
  177. };
  178. } // namespace detail
  179. } // namespace asio
  180. #include "asio/detail/pop_options.hpp"
  181. #include "asio/detail/impl/kqueue_reactor.hpp"
  182. #if defined(ASIO_HEADER_ONLY)
  183. # include "asio/detail/impl/kqueue_reactor.ipp"
  184. #endif // defined(ASIO_HEADER_ONLY)
  185. #endif // defined(ASIO_HAS_KQUEUE)
  186. #endif // ASIO_DETAIL_KQUEUE_REACTOR_HPP