/Src/Dependencies/Boost/boost/asio/detail/dev_poll_reactor.hpp

http://hadesmem.googlecode.com/ · C++ Header · 213 lines · 105 code · 49 blank · 59 comment · 1 complexity · aaf020bb75163ea673823d071eae87ab MD5 · raw file

  1. //
  2. // detail/dev_poll_reactor.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2011 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 <boost/limits.hpp>
  18. #include <cstddef>
  19. #include <vector>
  20. #include <sys/devpoll.h>
  21. #include <boost/asio/detail/dev_poll_reactor_fwd.hpp>
  22. #include <boost/asio/detail/hash_map.hpp>
  23. #include <boost/asio/detail/mutex.hpp>
  24. #include <boost/asio/detail/op_queue.hpp>
  25. #include <boost/asio/detail/reactor_op.hpp>
  26. #include <boost/asio/detail/reactor_op_queue.hpp>
  27. #include <boost/asio/detail/select_interrupter.hpp>
  28. #include <boost/asio/detail/socket_types.hpp>
  29. #include <boost/asio/detail/timer_op.hpp>
  30. #include <boost/asio/detail/timer_queue_base.hpp>
  31. #include <boost/asio/detail/timer_queue_fwd.hpp>
  32. #include <boost/asio/detail/timer_queue_set.hpp>
  33. #include <boost/asio/io_service.hpp>
  34. #include <boost/asio/detail/push_options.hpp>
  35. namespace boost {
  36. namespace asio {
  37. namespace detail {
  38. class dev_poll_reactor
  39. : public boost::asio::detail::service_base<dev_poll_reactor>
  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::io_service& io_service);
  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_service();
  54. // Recreate internal descriptors following a fork.
  55. BOOST_ASIO_DECL void fork_service(
  56. boost::asio::io_service::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(reactor_op* op)
  73. {
  74. io_service_.post_immediate_completion(op);
  75. }
  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, bool allow_speculative);
  80. // Cancel all operations associated with the given descriptor. The
  81. // handlers associated with the descriptor will be invoked with the
  82. // operation_aborted error.
  83. BOOST_ASIO_DECL void cancel_ops(socket_type descriptor, per_descriptor_data&);
  84. // Cancel any operations that are running against the descriptor and remove
  85. // its registration from the reactor.
  86. BOOST_ASIO_DECL void deregister_descriptor(socket_type descriptor,
  87. per_descriptor_data&, bool closing);
  88. // Cancel any operations that are running against the descriptor and remove
  89. // its registration from the reactor.
  90. BOOST_ASIO_DECL void deregister_internal_descriptor(
  91. socket_type descriptor, per_descriptor_data&);
  92. // Add a new timer queue to the reactor.
  93. template <typename Time_Traits>
  94. void add_timer_queue(timer_queue<Time_Traits>& queue);
  95. // Remove a timer queue from the reactor.
  96. template <typename Time_Traits>
  97. void remove_timer_queue(timer_queue<Time_Traits>& queue);
  98. // Schedule a new operation in the given timer queue to expire at the
  99. // specified absolute time.
  100. template <typename Time_Traits>
  101. void schedule_timer(timer_queue<Time_Traits>& queue,
  102. const typename Time_Traits::time_type& time,
  103. typename timer_queue<Time_Traits>::per_timer_data& timer, timer_op* op);
  104. // Cancel the timer operations associated with the given token. Returns the
  105. // number of operations that have been posted or dispatched.
  106. template <typename Time_Traits>
  107. std::size_t cancel_timer(timer_queue<Time_Traits>& queue,
  108. typename timer_queue<Time_Traits>::per_timer_data& timer,
  109. std::size_t max_cancelled = (std::numeric_limits<std::size_t>::max)());
  110. // Run /dev/poll once until interrupted or events are ready to be dispatched.
  111. BOOST_ASIO_DECL void run(bool block, op_queue<operation>& ops);
  112. // Interrupt the select loop.
  113. BOOST_ASIO_DECL void interrupt();
  114. private:
  115. // Create the /dev/poll file descriptor. Throws an exception if the descriptor
  116. // cannot be created.
  117. BOOST_ASIO_DECL static int do_dev_poll_create();
  118. // Helper function to add a new timer queue.
  119. BOOST_ASIO_DECL void do_add_timer_queue(timer_queue_base& queue);
  120. // Helper function to remove a timer queue.
  121. BOOST_ASIO_DECL void do_remove_timer_queue(timer_queue_base& queue);
  122. // Get the timeout value for the /dev/poll DP_POLL operation. The timeout
  123. // value is returned as a number of milliseconds. A return value of -1
  124. // indicates that the poll should block indefinitely.
  125. BOOST_ASIO_DECL int get_timeout();
  126. // Cancel all operations associated with the given descriptor. The do_cancel
  127. // function of the handler objects will be invoked. This function does not
  128. // acquire the dev_poll_reactor's mutex.
  129. BOOST_ASIO_DECL void cancel_ops_unlocked(socket_type descriptor,
  130. const boost::system::error_code& ec);
  131. // Helper class used to reregister descriptors after a fork.
  132. class fork_helper;
  133. friend class fork_helper;
  134. // Add a pending event entry for the given descriptor.
  135. BOOST_ASIO_DECL ::pollfd& add_pending_event_change(int descriptor);
  136. // The io_service implementation used to post completions.
  137. io_service_impl& io_service_;
  138. // Mutex to protect access to internal data.
  139. boost::asio::detail::mutex mutex_;
  140. // The /dev/poll file descriptor.
  141. int dev_poll_fd_;
  142. // Vector of /dev/poll events waiting to be written to the descriptor.
  143. std::vector< ::pollfd> pending_event_changes_;
  144. // Hash map to associate a descriptor with a pending event change index.
  145. hash_map<int, std::size_t> pending_event_change_index_;
  146. // The interrupter is used to break a blocking DP_POLL operation.
  147. select_interrupter interrupter_;
  148. // The queues of read, write and except operations.
  149. reactor_op_queue<socket_type> op_queue_[max_ops];
  150. // The timer queues.
  151. timer_queue_set timer_queues_;
  152. // Whether the service has been shut down.
  153. bool shutdown_;
  154. };
  155. } // namespace detail
  156. } // namespace asio
  157. } // namespace boost
  158. #include <boost/asio/detail/pop_options.hpp>
  159. #include <boost/asio/detail/impl/dev_poll_reactor.hpp>
  160. #if defined(BOOST_ASIO_HEADER_ONLY)
  161. # include <boost/asio/detail/impl/dev_poll_reactor.ipp>
  162. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  163. #endif // defined(BOOST_ASIO_HAS_DEV_POLL)
  164. #endif // BOOST_ASIO_DETAIL_DEV_POLL_REACTOR_HPP