dev_poll_reactor.hpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. //
  2. // detail/dev_poll_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_DEV_POLL_REACTOR_HPP
  11. #define ASIO_DETAIL_DEV_POLL_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_DEV_POLL)
  17. #include <cstddef>
  18. #include <vector>
  19. #include <sys/devpoll.h>
  20. #include "asio/detail/hash_map.hpp"
  21. #include "asio/detail/limits.hpp"
  22. #include "asio/detail/mutex.hpp"
  23. #include "asio/detail/op_queue.hpp"
  24. #include "asio/detail/reactor_op.hpp"
  25. #include "asio/detail/reactor_op_queue.hpp"
  26. #include "asio/detail/scheduler_task.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/execution_context.hpp"
  33. #include "asio/detail/push_options.hpp"
  34. namespace asio {
  35. namespace detail {
  36. class dev_poll_reactor
  37. : public execution_context_service_base<dev_poll_reactor>,
  38. public scheduler_task
  39. {
  40. public:
  41. enum op_types { read_op = 0, write_op = 1,
  42. connect_op = 1, except_op = 2, max_ops = 3 };
  43. // Per-descriptor data.
  44. struct per_descriptor_data
  45. {
  46. };
  47. // Constructor.
  48. ASIO_DECL dev_poll_reactor(asio::execution_context& ctx);
  49. // Destructor.
  50. ASIO_DECL ~dev_poll_reactor();
  51. // Destroy all user-defined handler objects owned by the service.
  52. ASIO_DECL void shutdown();
  53. // Recreate internal descriptors following a fork.
  54. ASIO_DECL void notify_fork(
  55. asio::execution_context::fork_event fork_ev);
  56. // Initialise the task.
  57. ASIO_DECL void init_task();
  58. // Register a socket with the reactor. Returns 0 on success, system error
  59. // code on failure.
  60. ASIO_DECL int register_descriptor(socket_type, per_descriptor_data&);
  61. // Register a descriptor with an associated single operation. Returns 0 on
  62. // success, system error code on failure.
  63. ASIO_DECL int register_internal_descriptor(
  64. int op_type, socket_type descriptor,
  65. per_descriptor_data& descriptor_data, reactor_op* op);
  66. // Move descriptor registration from one descriptor_data object to another.
  67. ASIO_DECL void move_descriptor(socket_type descriptor,
  68. per_descriptor_data& target_descriptor_data,
  69. per_descriptor_data& source_descriptor_data);
  70. // Post a reactor operation for immediate completion.
  71. void post_immediate_completion(operation* op, bool is_continuation) const;
  72. // Post a reactor operation for immediate completion.
  73. ASIO_DECL static void call_post_immediate_completion(
  74. operation* op, bool is_continuation, const void* self);
  75. // Start a new operation. The reactor operation will be performed when the
  76. // given descriptor is flagged as ready, or an error has occurred.
  77. ASIO_DECL void start_op(int op_type, socket_type descriptor,
  78. per_descriptor_data&, reactor_op* op,
  79. bool is_continuation, bool allow_speculative,
  80. void (*on_immediate)(operation*, bool, const void*),
  81. const void* immediate_arg);
  82. // Start a new operation. The reactor operation will be performed when the
  83. // given descriptor is flagged as ready, or an error has occurred.
  84. void start_op(int op_type, socket_type descriptor,
  85. per_descriptor_data& descriptor_data, reactor_op* op,
  86. bool is_continuation, bool allow_speculative)
  87. {
  88. start_op(op_type, descriptor, descriptor_data,
  89. op, is_continuation, allow_speculative,
  90. &dev_poll_reactor::call_post_immediate_completion, this);
  91. }
  92. // Cancel all operations associated with the given descriptor. The
  93. // handlers associated with the descriptor will be invoked with the
  94. // operation_aborted error.
  95. ASIO_DECL void cancel_ops(socket_type descriptor, per_descriptor_data&);
  96. // Cancel all operations associated with the given descriptor and key. The
  97. // handlers associated with the descriptor will be invoked with the
  98. // operation_aborted error.
  99. ASIO_DECL void cancel_ops_by_key(socket_type descriptor,
  100. per_descriptor_data& descriptor_data,
  101. int op_type, void* cancellation_key);
  102. // Cancel any operations that are running against the descriptor and remove
  103. // its registration from the reactor. The reactor resources associated with
  104. // the descriptor must be released by calling cleanup_descriptor_data.
  105. ASIO_DECL void deregister_descriptor(socket_type descriptor,
  106. per_descriptor_data&, bool closing);
  107. // Remove the descriptor's registration from the reactor. The reactor
  108. // resources associated with the descriptor must be released by calling
  109. // cleanup_descriptor_data.
  110. ASIO_DECL void deregister_internal_descriptor(
  111. socket_type descriptor, per_descriptor_data&);
  112. // Perform any post-deregistration cleanup tasks associated with the
  113. // descriptor data.
  114. ASIO_DECL void cleanup_descriptor_data(per_descriptor_data&);
  115. // Add a new timer queue to the reactor.
  116. template <typename Time_Traits>
  117. void add_timer_queue(timer_queue<Time_Traits>& queue);
  118. // Remove a timer queue from the reactor.
  119. template <typename Time_Traits>
  120. void remove_timer_queue(timer_queue<Time_Traits>& queue);
  121. // Schedule a new operation in the given timer queue to expire at the
  122. // specified absolute time.
  123. template <typename Time_Traits>
  124. void schedule_timer(timer_queue<Time_Traits>& queue,
  125. const typename Time_Traits::time_type& time,
  126. typename timer_queue<Time_Traits>::per_timer_data& timer, wait_op* op);
  127. // Cancel the timer operations associated with the given token. Returns the
  128. // number of operations that have been posted or dispatched.
  129. template <typename Time_Traits>
  130. std::size_t cancel_timer(timer_queue<Time_Traits>& queue,
  131. typename timer_queue<Time_Traits>::per_timer_data& timer,
  132. std::size_t max_cancelled = (std::numeric_limits<std::size_t>::max)());
  133. // Cancel the timer operations associated with the given key.
  134. template <typename Time_Traits>
  135. void cancel_timer_by_key(timer_queue<Time_Traits>& queue,
  136. typename timer_queue<Time_Traits>::per_timer_data* timer,
  137. void* cancellation_key);
  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 /dev/poll once until interrupted or events are ready to be dispatched.
  144. ASIO_DECL void run(long usec, op_queue<operation>& ops);
  145. // Interrupt the select loop.
  146. ASIO_DECL void interrupt();
  147. private:
  148. // Create the /dev/poll file descriptor. Throws an exception if the descriptor
  149. // cannot be created.
  150. ASIO_DECL static int do_dev_poll_create();
  151. // Helper function to add a new timer queue.
  152. ASIO_DECL void do_add_timer_queue(timer_queue_base& queue);
  153. // Helper function to remove a timer queue.
  154. ASIO_DECL void do_remove_timer_queue(timer_queue_base& queue);
  155. // Get the timeout value for the /dev/poll DP_POLL operation. The timeout
  156. // value is returned as a number of milliseconds. A return value of -1
  157. // indicates that the poll should block indefinitely.
  158. ASIO_DECL int get_timeout(int msec);
  159. // Cancel all operations associated with the given descriptor. The do_cancel
  160. // function of the handler objects will be invoked. This function does not
  161. // acquire the dev_poll_reactor's mutex.
  162. ASIO_DECL void cancel_ops_unlocked(socket_type descriptor,
  163. const asio::error_code& ec);
  164. // Add a pending event entry for the given descriptor.
  165. ASIO_DECL ::pollfd& add_pending_event_change(int descriptor);
  166. // The scheduler implementation used to post completions.
  167. scheduler& scheduler_;
  168. // Mutex to protect access to internal data.
  169. asio::detail::mutex mutex_;
  170. // The /dev/poll file descriptor.
  171. int dev_poll_fd_;
  172. // Vector of /dev/poll events waiting to be written to the descriptor.
  173. std::vector< ::pollfd> pending_event_changes_;
  174. // Hash map to associate a descriptor with a pending event change index.
  175. hash_map<int, std::size_t> pending_event_change_index_;
  176. // The interrupter is used to break a blocking DP_POLL operation.
  177. select_interrupter interrupter_;
  178. // The queues of read, write and except operations.
  179. reactor_op_queue<socket_type> op_queue_[max_ops];
  180. // The timer queues.
  181. timer_queue_set timer_queues_;
  182. // Whether the service has been shut down.
  183. bool shutdown_;
  184. };
  185. } // namespace detail
  186. } // namespace asio
  187. #include "asio/detail/pop_options.hpp"
  188. #include "asio/detail/impl/dev_poll_reactor.hpp"
  189. #if defined(ASIO_HEADER_ONLY)
  190. # include "asio/detail/impl/dev_poll_reactor.ipp"
  191. #endif // defined(ASIO_HEADER_ONLY)
  192. #endif // defined(ASIO_HAS_DEV_POLL)
  193. #endif // ASIO_DETAIL_DEV_POLL_REACTOR_HPP