PageRenderTime 26ms CodeModel.GetById 12ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 11#ifndef BOOST_ASIO_DETAIL_DEV_POLL_REACTOR_HPP
 12#define BOOST_ASIO_DETAIL_DEV_POLL_REACTOR_HPP
 13
 14#if defined(_MSC_VER) && (_MSC_VER >= 1200)
 15# pragma once
 16#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
 17
 18#include <boost/asio/detail/config.hpp>
 19
 20#if defined(BOOST_ASIO_HAS_DEV_POLL)
 21
 22#include <boost/limits.hpp>
 23#include <cstddef>
 24#include <vector>
 25#include <sys/devpoll.h>
 26#include <boost/asio/detail/dev_poll_reactor_fwd.hpp>
 27#include <boost/asio/detail/hash_map.hpp>
 28#include <boost/asio/detail/mutex.hpp>
 29#include <boost/asio/detail/op_queue.hpp>
 30#include <boost/asio/detail/reactor_op.hpp>
 31#include <boost/asio/detail/reactor_op_queue.hpp>
 32#include <boost/asio/detail/select_interrupter.hpp>
 33#include <boost/asio/detail/socket_types.hpp>
 34#include <boost/asio/detail/timer_op.hpp>
 35#include <boost/asio/detail/timer_queue_base.hpp>
 36#include <boost/asio/detail/timer_queue_fwd.hpp>
 37#include <boost/asio/detail/timer_queue_set.hpp>
 38#include <boost/asio/io_service.hpp>
 39
 40#include <boost/asio/detail/push_options.hpp>
 41
 42namespace boost {
 43namespace asio {
 44namespace detail {
 45
 46class dev_poll_reactor
 47  : public boost::asio::detail::service_base<dev_poll_reactor>
 48{
 49public:
 50  enum op_types { read_op = 0, write_op = 1,
 51    connect_op = 1, except_op = 2, max_ops = 3 };
 52
 53  // Per-descriptor data.
 54  struct per_descriptor_data
 55  {
 56  };
 57
 58  // Constructor.
 59  BOOST_ASIO_DECL dev_poll_reactor(boost::asio::io_service& io_service);
 60
 61  // Destructor.
 62  BOOST_ASIO_DECL ~dev_poll_reactor();
 63
 64  // Destroy all user-defined handler objects owned by the service.
 65  BOOST_ASIO_DECL void shutdown_service();
 66
 67  // Recreate internal descriptors following a fork.
 68  BOOST_ASIO_DECL void fork_service(
 69      boost::asio::io_service::fork_event fork_ev);
 70
 71  // Initialise the task.
 72  BOOST_ASIO_DECL void init_task();
 73
 74  // Register a socket with the reactor. Returns 0 on success, system error
 75  // code on failure.
 76  BOOST_ASIO_DECL int register_descriptor(socket_type, per_descriptor_data&);
 77
 78  // Register a descriptor with an associated single operation. Returns 0 on
 79  // success, system error code on failure.
 80  BOOST_ASIO_DECL int register_internal_descriptor(
 81      int op_type, socket_type descriptor,
 82      per_descriptor_data& descriptor_data, reactor_op* op);
 83
 84  // Move descriptor registration from one descriptor_data object to another.
 85  BOOST_ASIO_DECL void move_descriptor(socket_type descriptor,
 86      per_descriptor_data& target_descriptor_data,
 87      per_descriptor_data& source_descriptor_data);
 88
 89  // Post a reactor operation for immediate completion.
 90  void post_immediate_completion(reactor_op* op)
 91  {
 92    io_service_.post_immediate_completion(op);
 93  }
 94
 95  // Start a new operation. The reactor operation will be performed when the
 96  // given descriptor is flagged as ready, or an error has occurred.
 97  BOOST_ASIO_DECL void start_op(int op_type, socket_type descriptor,
 98      per_descriptor_data&, reactor_op* op, bool allow_speculative);
 99
100  // Cancel all operations associated with the given descriptor. The
101  // handlers associated with the descriptor will be invoked with the
102  // operation_aborted error.
103  BOOST_ASIO_DECL void cancel_ops(socket_type descriptor, per_descriptor_data&);
104
105  // Cancel any operations that are running against the descriptor and remove
106  // its registration from the reactor.
107  BOOST_ASIO_DECL void deregister_descriptor(socket_type descriptor,
108      per_descriptor_data&, bool closing);
109
110  // Cancel any operations that are running against the descriptor and remove
111  // its registration from the reactor.
112  BOOST_ASIO_DECL void deregister_internal_descriptor(
113      socket_type descriptor, per_descriptor_data&);
114
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
119  // Remove a timer queue from the reactor.
120  template <typename Time_Traits>
121  void remove_timer_queue(timer_queue<Time_Traits>& queue);
122
123  // Schedule a new operation in the given timer queue to expire at the
124  // specified absolute time.
125  template <typename Time_Traits>
126  void schedule_timer(timer_queue<Time_Traits>& queue,
127      const typename Time_Traits::time_type& time,
128      typename timer_queue<Time_Traits>::per_timer_data& timer, timer_op* op);
129
130  // Cancel the timer operations associated with the given token. Returns the
131  // number of operations that have been posted or dispatched.
132  template <typename Time_Traits>
133  std::size_t cancel_timer(timer_queue<Time_Traits>& queue,
134      typename timer_queue<Time_Traits>::per_timer_data& timer,
135      std::size_t max_cancelled = (std::numeric_limits<std::size_t>::max)());
136
137  // Run /dev/poll once until interrupted or events are ready to be dispatched.
138  BOOST_ASIO_DECL void run(bool block, op_queue<operation>& ops);
139
140  // Interrupt the select loop.
141  BOOST_ASIO_DECL void interrupt();
142
143private:
144  // Create the /dev/poll file descriptor. Throws an exception if the descriptor
145  // cannot be created.
146  BOOST_ASIO_DECL static int do_dev_poll_create();
147
148  // Helper function to add a new timer queue.
149  BOOST_ASIO_DECL void do_add_timer_queue(timer_queue_base& queue);
150
151  // Helper function to remove a timer queue.
152  BOOST_ASIO_DECL void do_remove_timer_queue(timer_queue_base& queue);
153
154  // Get the timeout value for the /dev/poll DP_POLL operation. The timeout
155  // value is returned as a number of milliseconds. A return value of -1
156  // indicates that the poll should block indefinitely.
157  BOOST_ASIO_DECL int get_timeout();
158
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  BOOST_ASIO_DECL void cancel_ops_unlocked(socket_type descriptor,
163      const boost::system::error_code& ec);
164
165  // Helper class used to reregister descriptors after a fork.
166  class fork_helper;
167  friend class fork_helper;
168
169  // Add a pending event entry for the given descriptor.
170  BOOST_ASIO_DECL ::pollfd& add_pending_event_change(int descriptor);
171
172  // The io_service implementation used to post completions.
173  io_service_impl& io_service_;
174
175  // Mutex to protect access to internal data.
176  boost::asio::detail::mutex mutex_;
177
178  // The /dev/poll file descriptor.
179  int dev_poll_fd_;
180
181  // Vector of /dev/poll events waiting to be written to the descriptor.
182  std::vector< ::pollfd> pending_event_changes_;
183
184  // Hash map to associate a descriptor with a pending event change index.
185  hash_map<int, std::size_t> pending_event_change_index_;
186
187  // The interrupter is used to break a blocking DP_POLL operation.
188  select_interrupter interrupter_;
189
190  // The queues of read, write and except operations.
191  reactor_op_queue<socket_type> op_queue_[max_ops];
192
193  // The timer queues.
194  timer_queue_set timer_queues_;
195
196  // Whether the service has been shut down.
197  bool shutdown_;
198};
199
200} // namespace detail
201} // namespace asio
202} // namespace boost
203
204#include <boost/asio/detail/pop_options.hpp>
205
206#include <boost/asio/detail/impl/dev_poll_reactor.hpp>
207#if defined(BOOST_ASIO_HEADER_ONLY)
208# include <boost/asio/detail/impl/dev_poll_reactor.ipp>
209#endif // defined(BOOST_ASIO_HEADER_ONLY)
210
211#endif // defined(BOOST_ASIO_HAS_DEV_POLL)
212
213#endif // BOOST_ASIO_DETAIL_DEV_POLL_REACTOR_HPP