dev_poll_reactor.hpp 7.9 KB

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