dev_poll_reactor.hpp 6.9 KB

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