dev_poll_reactor.hpp 9.2 KB

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