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.

select_reactor.hpp 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. //
  2. // detail/select_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_SELECT_REACTOR_HPP
  11. #define ASIO_DETAIL_SELECT_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_IOCP) \
  17. || (!defined(ASIO_HAS_DEV_POLL) \
  18. && !defined(ASIO_HAS_EPOLL) \
  19. && !defined(ASIO_HAS_KQUEUE) \
  20. && !defined(ASIO_WINDOWS_RUNTIME))
  21. #include <cstddef>
  22. #include "asio/detail/fd_set_adapter.hpp"
  23. #include "asio/detail/limits.hpp"
  24. #include "asio/detail/mutex.hpp"
  25. #include "asio/detail/op_queue.hpp"
  26. #include "asio/detail/reactor_op.hpp"
  27. #include "asio/detail/reactor_op_queue.hpp"
  28. #include "asio/detail/select_interrupter.hpp"
  29. #include "asio/detail/socket_types.hpp"
  30. #include "asio/detail/timer_queue_base.hpp"
  31. #include "asio/detail/timer_queue_set.hpp"
  32. #include "asio/detail/wait_op.hpp"
  33. #include "asio/execution_context.hpp"
  34. #if defined(ASIO_HAS_IOCP)
  35. # include "asio/detail/thread.hpp"
  36. #endif // defined(ASIO_HAS_IOCP)
  37. #include "asio/detail/push_options.hpp"
  38. namespace asio {
  39. namespace detail {
  40. class select_reactor
  41. : public execution_context_service_base<select_reactor>
  42. {
  43. public:
  44. #if defined(ASIO_WINDOWS) || defined(__CYGWIN__)
  45. enum op_types { read_op = 0, write_op = 1, except_op = 2,
  46. max_select_ops = 3, connect_op = 3, max_ops = 4 };
  47. #else // defined(ASIO_WINDOWS) || defined(__CYGWIN__)
  48. enum op_types { read_op = 0, write_op = 1, except_op = 2,
  49. max_select_ops = 3, connect_op = 1, max_ops = 3 };
  50. #endif // defined(ASIO_WINDOWS) || defined(__CYGWIN__)
  51. // Per-descriptor data.
  52. struct per_descriptor_data
  53. {
  54. };
  55. // Constructor.
  56. ASIO_DECL select_reactor(asio::execution_context& ctx);
  57. // Destructor.
  58. ASIO_DECL ~select_reactor();
  59. // Destroy all user-defined handler objects owned by the service.
  60. ASIO_DECL void shutdown();
  61. // Recreate internal descriptors following a fork.
  62. ASIO_DECL void notify_fork(
  63. asio::execution_context::fork_event fork_ev);
  64. // Initialise the task, but only if the reactor is not in its own thread.
  65. ASIO_DECL void init_task();
  66. // Register a socket with the reactor. Returns 0 on success, system error
  67. // code on failure.
  68. ASIO_DECL int register_descriptor(socket_type, per_descriptor_data&);
  69. // Register a descriptor with an associated single operation. Returns 0 on
  70. // success, system error code on failure.
  71. ASIO_DECL int register_internal_descriptor(
  72. int op_type, socket_type descriptor,
  73. per_descriptor_data& descriptor_data, reactor_op* op);
  74. // Post a reactor operation for immediate completion.
  75. void post_immediate_completion(reactor_op* op, bool is_continuation)
  76. {
  77. scheduler_.post_immediate_completion(op, is_continuation);
  78. }
  79. // Start a new operation. The reactor operation will be performed when the
  80. // given descriptor is flagged as ready, or an error has occurred.
  81. ASIO_DECL void start_op(int op_type, socket_type descriptor,
  82. per_descriptor_data&, reactor_op* op, bool is_continuation, bool);
  83. // Cancel all operations associated with the given descriptor. The
  84. // handlers associated with the descriptor will be invoked with the
  85. // operation_aborted error.
  86. ASIO_DECL void cancel_ops(socket_type descriptor, per_descriptor_data&);
  87. // Cancel any operations that are running against the descriptor and remove
  88. // its registration from the reactor.
  89. ASIO_DECL void deregister_descriptor(socket_type descriptor,
  90. per_descriptor_data&, bool closing);
  91. // Remote the descriptor's registration from the reactor.
  92. ASIO_DECL void deregister_internal_descriptor(
  93. socket_type descriptor, per_descriptor_data& descriptor_data);
  94. // Move descriptor registration from one descriptor_data object to another.
  95. ASIO_DECL void move_descriptor(socket_type descriptor,
  96. per_descriptor_data& target_descriptor_data,
  97. per_descriptor_data& source_descriptor_data);
  98. // Add a new timer queue to the reactor.
  99. template <typename Time_Traits>
  100. void add_timer_queue(timer_queue<Time_Traits>& queue);
  101. // Remove a timer queue from the reactor.
  102. template <typename Time_Traits>
  103. void remove_timer_queue(timer_queue<Time_Traits>& queue);
  104. // Schedule a new operation in the given timer queue to expire at the
  105. // specified absolute time.
  106. template <typename Time_Traits>
  107. void schedule_timer(timer_queue<Time_Traits>& queue,
  108. const typename Time_Traits::time_type& time,
  109. typename timer_queue<Time_Traits>::per_timer_data& timer, wait_op* op);
  110. // Cancel the timer operations associated with the given token. Returns the
  111. // number of operations that have been posted or dispatched.
  112. template <typename Time_Traits>
  113. std::size_t cancel_timer(timer_queue<Time_Traits>& queue,
  114. typename timer_queue<Time_Traits>::per_timer_data& timer,
  115. std::size_t max_cancelled = (std::numeric_limits<std::size_t>::max)());
  116. // Move the timer operations associated with the given timer.
  117. template <typename Time_Traits>
  118. void move_timer(timer_queue<Time_Traits>& queue,
  119. typename timer_queue<Time_Traits>::per_timer_data& target,
  120. typename timer_queue<Time_Traits>::per_timer_data& source);
  121. // Run select once until interrupted or events are ready to be dispatched.
  122. ASIO_DECL void run(bool block, op_queue<operation>& ops);
  123. // Interrupt the select loop.
  124. ASIO_DECL void interrupt();
  125. private:
  126. #if defined(ASIO_HAS_IOCP)
  127. // Run the select loop in the thread.
  128. ASIO_DECL void run_thread();
  129. #endif // defined(ASIO_HAS_IOCP)
  130. // Helper function to add a new timer queue.
  131. ASIO_DECL void do_add_timer_queue(timer_queue_base& queue);
  132. // Helper function to remove a timer queue.
  133. ASIO_DECL void do_remove_timer_queue(timer_queue_base& queue);
  134. // Get the timeout value for the select call.
  135. ASIO_DECL timeval* get_timeout(timeval& tv);
  136. // Cancel all operations associated with the given descriptor. This function
  137. // does not acquire the select_reactor's mutex.
  138. ASIO_DECL void cancel_ops_unlocked(socket_type descriptor,
  139. const asio::error_code& ec);
  140. // The scheduler implementation used to post completions.
  141. # if defined(ASIO_HAS_IOCP)
  142. typedef class win_iocp_io_context scheduler_type;
  143. # else // defined(ASIO_HAS_IOCP)
  144. typedef class scheduler scheduler_type;
  145. # endif // defined(ASIO_HAS_IOCP)
  146. scheduler_type& scheduler_;
  147. // Mutex to protect access to internal data.
  148. asio::detail::mutex mutex_;
  149. // The interrupter is used to break a blocking select call.
  150. select_interrupter interrupter_;
  151. // The queues of read, write and except operations.
  152. reactor_op_queue<socket_type> op_queue_[max_ops];
  153. // The file descriptor sets to be passed to the select system call.
  154. fd_set_adapter fd_sets_[max_select_ops];
  155. // The timer queues.
  156. timer_queue_set timer_queues_;
  157. #if defined(ASIO_HAS_IOCP)
  158. // Helper class to run the reactor loop in a thread.
  159. class thread_function;
  160. friend class thread_function;
  161. // Does the reactor loop thread need to stop.
  162. bool stop_thread_;
  163. // The thread that is running the reactor loop.
  164. asio::detail::thread* thread_;
  165. #endif // defined(ASIO_HAS_IOCP)
  166. // Whether the service has been shut down.
  167. bool shutdown_;
  168. };
  169. } // namespace detail
  170. } // namespace asio
  171. #include "asio/detail/pop_options.hpp"
  172. #include "asio/detail/impl/select_reactor.hpp"
  173. #if defined(ASIO_HEADER_ONLY)
  174. # include "asio/detail/impl/select_reactor.ipp"
  175. #endif // defined(ASIO_HEADER_ONLY)
  176. #endif // defined(ASIO_HAS_IOCP)
  177. // || (!defined(ASIO_HAS_DEV_POLL)
  178. // && !defined(ASIO_HAS_EPOLL)
  179. // && !defined(ASIO_HAS_KQUEUE)
  180. // && !defined(ASIO_WINDOWS_RUNTIME))
  181. #endif // ASIO_DETAIL_SELECT_REACTOR_HPP