PageRenderTime 35ms CodeModel.GetById 1ms app.highlight 25ms RepoModel.GetById 5ms app.codeStats 0ms

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

http://hadesmem.googlecode.com/
C++ Header | 279 lines | 139 code | 63 blank | 77 comment | 6 complexity | d2b2d6bb89218ca6e807f6001d75780e MD5 | raw file
  1//
  2// detail/win_iocp_io_service.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_WIN_IOCP_IO_SERVICE_HPP
 12#define BOOST_ASIO_DETAIL_WIN_IOCP_IO_SERVICE_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_IOCP)
 21
 22#include <boost/limits.hpp>
 23#include <boost/asio/io_service.hpp>
 24#include <boost/asio/detail/mutex.hpp>
 25#include <boost/asio/detail/op_queue.hpp>
 26#include <boost/asio/detail/scoped_ptr.hpp>
 27#include <boost/asio/detail/socket_types.hpp>
 28#include <boost/asio/detail/timer_op.hpp>
 29#include <boost/asio/detail/timer_queue_base.hpp>
 30#include <boost/asio/detail/timer_queue_fwd.hpp>
 31#include <boost/asio/detail/timer_queue_set.hpp>
 32#include <boost/asio/detail/win_iocp_io_service_fwd.hpp>
 33#include <boost/asio/detail/win_iocp_operation.hpp>
 34#include <boost/asio/detail/thread.hpp>
 35
 36#include <boost/asio/detail/push_options.hpp>
 37
 38namespace boost {
 39namespace asio {
 40namespace detail {
 41
 42class timer_op;
 43
 44class win_iocp_io_service
 45  : public boost::asio::detail::service_base<win_iocp_io_service>
 46{
 47public:
 48  // Constructor.
 49  BOOST_ASIO_DECL win_iocp_io_service(boost::asio::io_service& io_service);
 50
 51  BOOST_ASIO_DECL void init(size_t concurrency_hint);
 52
 53  // Destroy all user-defined handler objects owned by the service.
 54  BOOST_ASIO_DECL void shutdown_service();
 55
 56  // Initialise the task. Nothing to do here.
 57  void init_task()
 58  {
 59  }
 60
 61  // Register a handle with the IO completion port.
 62  BOOST_ASIO_DECL boost::system::error_code register_handle(
 63      HANDLE handle, boost::system::error_code& ec);
 64
 65  // Run the event loop until stopped or no more work.
 66  BOOST_ASIO_DECL size_t run(boost::system::error_code& ec);
 67
 68  // Run until stopped or one operation is performed.
 69  BOOST_ASIO_DECL size_t run_one(boost::system::error_code& ec);
 70
 71  // Poll for operations without blocking.
 72  BOOST_ASIO_DECL size_t poll(boost::system::error_code& ec);
 73
 74  // Poll for one operation without blocking.
 75  BOOST_ASIO_DECL size_t poll_one(boost::system::error_code& ec);
 76
 77  // Stop the event processing loop.
 78  BOOST_ASIO_DECL void stop();
 79
 80  // Determine whether the io_service is stopped.
 81  bool stopped() const
 82  {
 83    return ::InterlockedExchangeAdd(&stopped_, 0) != 0;
 84  }
 85
 86  // Reset in preparation for a subsequent run invocation.
 87  void reset()
 88  {
 89    ::InterlockedExchange(&stopped_, 0);
 90  }
 91
 92  // Notify that some work has started.
 93  void work_started()
 94  {
 95    ::InterlockedIncrement(&outstanding_work_);
 96  }
 97
 98  // Notify that some work has finished.
 99  void work_finished()
100  {
101    if (::InterlockedDecrement(&outstanding_work_) == 0)
102      stop();
103  }
104
105  // Request invocation of the given handler.
106  template <typename Handler>
107  void dispatch(Handler handler);
108
109  // Request invocation of the given handler and return immediately.
110  template <typename Handler>
111  void post(Handler handler);
112
113  // Request invocation of the given operation and return immediately. Assumes
114  // that work_started() has not yet been called for the operation.
115  void post_immediate_completion(win_iocp_operation* op)
116  {
117    work_started();
118    post_deferred_completion(op);
119  }
120
121  // Request invocation of the given operation and return immediately. Assumes
122  // that work_started() was previously called for the operation.
123  BOOST_ASIO_DECL void post_deferred_completion(win_iocp_operation* op);
124
125  // Request invocation of the given operation and return immediately. Assumes
126  // that work_started() was previously called for the operations.
127  BOOST_ASIO_DECL void post_deferred_completions(
128      op_queue<win_iocp_operation>& ops);
129
130  // Process unfinished operations as part of a shutdown_service operation.
131  // Assumes that work_started() was previously called for the operations.
132  BOOST_ASIO_DECL void abandon_operations(op_queue<operation>& ops);
133
134  // Called after starting an overlapped I/O operation that did not complete
135  // immediately. The caller must have already called work_started() prior to
136  // starting the operation.
137  BOOST_ASIO_DECL void on_pending(win_iocp_operation* op);
138
139  // Called after starting an overlapped I/O operation that completed
140  // immediately. The caller must have already called work_started() prior to
141  // starting the operation.
142  BOOST_ASIO_DECL void on_completion(win_iocp_operation* op,
143      DWORD last_error = 0, DWORD bytes_transferred = 0);
144
145  // Called after starting an overlapped I/O operation that completed
146  // immediately. The caller must have already called work_started() prior to
147  // starting the operation.
148  BOOST_ASIO_DECL void on_completion(win_iocp_operation* op,
149      const boost::system::error_code& ec, DWORD bytes_transferred = 0);
150
151  // Add a new timer queue to the service.
152  template <typename Time_Traits>
153  void add_timer_queue(timer_queue<Time_Traits>& timer_queue);
154
155  // Remove a timer queue from the service.
156  template <typename Time_Traits>
157  void remove_timer_queue(timer_queue<Time_Traits>& timer_queue);
158
159  // Schedule a new operation in the given timer queue to expire at the
160  // specified absolute time.
161  template <typename Time_Traits>
162  void schedule_timer(timer_queue<Time_Traits>& queue,
163      const typename Time_Traits::time_type& time,
164      typename timer_queue<Time_Traits>::per_timer_data& timer, timer_op* op);
165
166  // Cancel the timer associated with the given token. Returns the number of
167  // handlers that have been posted or dispatched.
168  template <typename Time_Traits>
169  std::size_t cancel_timer(timer_queue<Time_Traits>& queue,
170      typename timer_queue<Time_Traits>::per_timer_data& timer,
171      std::size_t max_cancelled = (std::numeric_limits<std::size_t>::max)());
172
173private:
174#if defined(WINVER) && (WINVER < 0x0500)
175  typedef DWORD dword_ptr_t;
176  typedef ULONG ulong_ptr_t;
177#else // defined(WINVER) && (WINVER < 0x0500)
178  typedef DWORD_PTR dword_ptr_t;
179  typedef ULONG_PTR ulong_ptr_t;
180#endif // defined(WINVER) && (WINVER < 0x0500)
181
182  // Dequeues at most one operation from the I/O completion port, and then
183  // executes it. Returns the number of operations that were dequeued (i.e.
184  // either 0 or 1).
185  BOOST_ASIO_DECL size_t do_one(bool block, boost::system::error_code& ec);
186
187  // Helper function to add a new timer queue.
188  BOOST_ASIO_DECL void do_add_timer_queue(timer_queue_base& queue);
189
190  // Helper function to remove a timer queue.
191  BOOST_ASIO_DECL void do_remove_timer_queue(timer_queue_base& queue);
192
193  // Called to recalculate and update the timeout.
194  BOOST_ASIO_DECL void update_timeout();
195
196  // Helper class to call work_finished() on block exit.
197  struct work_finished_on_block_exit;
198
199  // Helper class for managing a HANDLE.
200  struct auto_handle
201  {
202    HANDLE handle;
203    auto_handle() : handle(0) {}
204    ~auto_handle() { if (handle) ::CloseHandle(handle); }
205  };
206
207  // The IO completion port used for queueing operations.
208  auto_handle iocp_;
209
210  // The count of unfinished work.
211  long outstanding_work_;
212
213  // Flag to indicate whether the event loop has been stopped.
214  mutable long stopped_;
215
216  // Flag to indicate whether the service has been shut down.
217  long shutdown_;
218
219  enum
220  {
221    // Timeout to use with GetQueuedCompletionStatus. Some versions of windows
222    // have a "bug" where a call to GetQueuedCompletionStatus can appear stuck
223    // even though there are events waiting on the queue. Using a timeout helps
224    // to work around the issue.
225    gqcs_timeout = 500,
226
227    // Maximum waitable timer timeout, in milliseconds.
228    max_timeout_msec = 5 * 60 * 1000,
229
230    // Maximum waitable timer timeout, in microseconds.
231    max_timeout_usec = max_timeout_msec * 1000,
232
233    // Completion key value used to wake up a thread to dispatch timers or
234    // completed operations.
235    wake_for_dispatch = 1,
236
237    // Completion key value to indicate that an operation has posted with the
238    // original last_error and bytes_transferred values stored in the fields of
239    // the OVERLAPPED structure.
240    overlapped_contains_result = 2
241  };
242
243  // Function object for processing timeouts in a background thread.
244  struct timer_thread_function;
245  friend struct timer_thread_function;
246
247  // Background thread used for processing timeouts.
248  scoped_ptr<thread> timer_thread_;
249
250  // A waitable timer object used for waiting for timeouts.
251  auto_handle waitable_timer_;
252
253  // Non-zero if timers or completed operations need to be dispatched.
254  long dispatch_required_;
255
256  // Mutex for protecting access to the timer queues and completed operations.
257  mutex dispatch_mutex_;
258
259  // The timer queues.
260  timer_queue_set timer_queues_;
261
262  // The operations that are ready to dispatch.
263  op_queue<win_iocp_operation> completed_ops_;
264};
265
266} // namespace detail
267} // namespace asio
268} // namespace boost
269
270#include <boost/asio/detail/pop_options.hpp>
271
272#include <boost/asio/detail/impl/win_iocp_io_service.hpp>
273#if defined(BOOST_ASIO_HEADER_ONLY)
274# include <boost/asio/detail/impl/win_iocp_io_service.ipp>
275#endif // defined(BOOST_ASIO_HEADER_ONLY)
276
277#endif // defined(BOOST_ASIO_HAS_IOCP)
278
279#endif // BOOST_ASIO_DETAIL_WIN_IOCP_IO_SERVICE_HPP