Realistic 3D camera system
3D camera system components
win_iocp_io_service.hpp
Go to the documentation of this file.
1 //
2 // detail/win_iocp_io_service.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 
11 #ifndef ASIO_DETAIL_WIN_IOCP_IO_SERVICE_HPP
12 #define 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 "asio/detail/config.hpp"
19 
20 #if defined(ASIO_HAS_IOCP)
21 
22 #include "asio/io_service.hpp"
24 #include "asio/detail/limits.hpp"
25 #include "asio/detail/mutex.hpp"
26 #include "asio/detail/op_queue.hpp"
29 #include "asio/detail/thread.hpp"
32 #include "asio/detail/wait_op.hpp"
35 
37 
38 namespace asio {
39 namespace detail {
40 
41 class wait_op;
42 
43 class win_iocp_io_service
44  : public asio::detail::service_base<win_iocp_io_service>
45 {
46 public:
47 
48  // Constructor. Specifies a concurrency hint that is passed through to the
49  // underlying I/O completion port.
50  ASIO_DECL win_iocp_io_service(asio::io_service& io_service,
51  size_t concurrency_hint = 0);
52 
53  // Destroy all user-defined handler objects owned by the service.
54  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  ASIO_DECL asio::error_code register_handle(
63  HANDLE handle, asio::error_code& ec);
64 
65  // Run the event loop until stopped or no more work.
66  ASIO_DECL size_t run(asio::error_code& ec);
67 
68  // Run until stopped or one operation is performed.
69  ASIO_DECL size_t run_one(asio::error_code& ec);
70 
71  // Poll for operations without blocking.
72  ASIO_DECL size_t poll(asio::error_code& ec);
73 
74  // Poll for one operation without blocking.
75  ASIO_DECL size_t poll_one(asio::error_code& ec);
76 
77  // Stop the event processing loop.
78  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  // Return whether a handler can be dispatched immediately.
106  bool can_dispatch()
107  {
108  return thread_call_stack::contains(this) != 0;
109  }
110 
111  // Request invocation of the given handler.
112  template <typename Handler>
113  void dispatch(Handler& handler);
114 
115  // Request invocation of the given handler and return immediately.
116  template <typename Handler>
117  void post(Handler& handler);
118 
119  // Request invocation of the given operation and return immediately. Assumes
120  // that work_started() has not yet been called for the operation.
121  void post_immediate_completion(win_iocp_operation* op, bool)
122  {
123  work_started();
124  post_deferred_completion(op);
125  }
126 
127  // Request invocation of the given operation and return immediately. Assumes
128  // that work_started() was previously called for the operation.
129  ASIO_DECL void post_deferred_completion(win_iocp_operation* op);
130 
131  // Request invocation of the given operation and return immediately. Assumes
132  // that work_started() was previously called for the operations.
133  ASIO_DECL void post_deferred_completions(
134  op_queue<win_iocp_operation>& ops);
135 
136  // Request invocation of the given operation using the thread-private queue
137  // and return immediately. Assumes that work_started() has not yet been
138  // called for the operation.
139  void post_private_immediate_completion(win_iocp_operation* op)
140  {
141  post_immediate_completion(op, false);
142  }
143 
144  // Request invocation of the given operation using the thread-private queue
145  // and return immediately. Assumes that work_started() was previously called
146  // for the operation.
147  void post_private_deferred_completion(win_iocp_operation* op)
148  {
149  post_deferred_completion(op);
150  }
151 
152  // Process unfinished operations as part of a shutdown_service operation.
153  // Assumes that work_started() was previously called for the operations.
154  ASIO_DECL void abandon_operations(op_queue<operation>& ops);
155 
156  // Called after starting an overlapped I/O operation that did not complete
157  // immediately. The caller must have already called work_started() prior to
158  // starting the operation.
159  ASIO_DECL void on_pending(win_iocp_operation* op);
160 
161  // Called after starting an overlapped I/O operation that completed
162  // immediately. The caller must have already called work_started() prior to
163  // starting the operation.
164  ASIO_DECL void on_completion(win_iocp_operation* op,
165  DWORD last_error = 0, DWORD bytes_transferred = 0);
166 
167  // Called after starting an overlapped I/O operation that completed
168  // immediately. The caller must have already called work_started() prior to
169  // starting the operation.
170  ASIO_DECL void on_completion(win_iocp_operation* op,
171  const asio::error_code& ec, DWORD bytes_transferred = 0);
172 
173  // Add a new timer queue to the service.
174  template <typename Time_Traits>
175  void add_timer_queue(timer_queue<Time_Traits>& timer_queue);
176 
177  // Remove a timer queue from the service.
178  template <typename Time_Traits>
179  void remove_timer_queue(timer_queue<Time_Traits>& timer_queue);
180 
181  // Schedule a new operation in the given timer queue to expire at the
182  // specified absolute time.
183  template <typename Time_Traits>
184  void schedule_timer(timer_queue<Time_Traits>& queue,
185  const typename Time_Traits::time_type& time,
186  typename timer_queue<Time_Traits>::per_timer_data& timer, wait_op* op);
187 
188  // Cancel the timer associated with the given token. Returns the number of
189  // handlers that have been posted or dispatched.
190  template <typename Time_Traits>
191  std::size_t cancel_timer(timer_queue<Time_Traits>& queue,
192  typename timer_queue<Time_Traits>::per_timer_data& timer,
193  std::size_t max_cancelled = (std::numeric_limits<std::size_t>::max)());
194 
195 private:
196 #if defined(WINVER) && (WINVER < 0x0500)
197  typedef DWORD dword_ptr_t;
198  typedef ULONG ulong_ptr_t;
199 #else // defined(WINVER) && (WINVER < 0x0500)
200  typedef DWORD_PTR dword_ptr_t;
201  typedef ULONG_PTR ulong_ptr_t;
202 #endif // defined(WINVER) && (WINVER < 0x0500)
203 
204  // Dequeues at most one operation from the I/O completion port, and then
205  // executes it. Returns the number of operations that were dequeued (i.e.
206  // either 0 or 1).
207  ASIO_DECL size_t do_one(bool block, asio::error_code& ec);
208 
209  // Helper to calculate the GetQueuedCompletionStatus timeout.
210  ASIO_DECL static DWORD get_gqcs_timeout();
211 
212  // Helper function to add a new timer queue.
213  ASIO_DECL void do_add_timer_queue(timer_queue_base& queue);
214 
215  // Helper function to remove a timer queue.
216  ASIO_DECL void do_remove_timer_queue(timer_queue_base& queue);
217 
218  // Called to recalculate and update the timeout.
219  ASIO_DECL void update_timeout();
220 
221  // Helper class to call work_finished() on block exit.
222  struct work_finished_on_block_exit;
223 
224  // Helper class for managing a HANDLE.
225  struct auto_handle
226  {
227  HANDLE handle;
228  auto_handle() : handle(0) {}
229  ~auto_handle() { if (handle) ::CloseHandle(handle); }
230  };
231 
232  // The IO completion port used for queueing operations.
233  auto_handle iocp_;
234 
235  // The count of unfinished work.
236  long outstanding_work_;
237 
238  // Flag to indicate whether the event loop has been stopped.
239  mutable long stopped_;
240 
241  // Flag to indicate whether there is an in-flight stop event. Every event
242  // posted using PostQueuedCompletionStatus consumes non-paged pool, so to
243  // avoid exhausting this resouce we limit the number of outstanding events.
244  long stop_event_posted_;
245 
246  // Flag to indicate whether the service has been shut down.
247  long shutdown_;
248 
249  enum
250  {
251  // Timeout to use with GetQueuedCompletionStatus on older versions of
252  // Windows. Some versions of windows have a "bug" where a call to
253  // GetQueuedCompletionStatus can appear stuck even though there are events
254  // waiting on the queue. Using a timeout helps to work around the issue.
255  default_gqcs_timeout = 500,
256 
257  // Maximum waitable timer timeout, in milliseconds.
258  max_timeout_msec = 5 * 60 * 1000,
259 
260  // Maximum waitable timer timeout, in microseconds.
261  max_timeout_usec = max_timeout_msec * 1000,
262 
263  // Completion key value used to wake up a thread to dispatch timers or
264  // completed operations.
265  wake_for_dispatch = 1,
266 
267  // Completion key value to indicate that an operation has posted with the
268  // original last_error and bytes_transferred values stored in the fields of
269  // the OVERLAPPED structure.
270  overlapped_contains_result = 2
271  };
272 
273  // Timeout to use with GetQueuedCompletionStatus.
274  const DWORD gqcs_timeout_;
275 
276  // Function object for processing timeouts in a background thread.
277  struct timer_thread_function;
278  friend struct timer_thread_function;
279 
280  // Background thread used for processing timeouts.
281  scoped_ptr<thread> timer_thread_;
282 
283  // A waitable timer object used for waiting for timeouts.
284  auto_handle waitable_timer_;
285 
286  // Non-zero if timers or completed operations need to be dispatched.
287  long dispatch_required_;
288 
289  // Mutex for protecting access to the timer queues and completed operations.
290  mutex dispatch_mutex_;
291 
292  // The timer queues.
293  timer_queue_set timer_queues_;
294 
295  // The operations that are ready to dispatch.
296  op_queue<win_iocp_operation> completed_ops_;
297 
298  // Per-thread call stack to track the state of each thread in the io_service.
299  typedef call_stack<win_iocp_io_service,
300  win_iocp_thread_info> thread_call_stack;
301 };
302 
303 } // namespace detail
304 } // namespace asio
305 
307 
309 #if defined(ASIO_HEADER_ONLY)
311 #endif // defined(ASIO_HEADER_ONLY)
312 
313 #endif // defined(ASIO_HAS_IOCP)
314 
315 #endif // ASIO_DETAIL_WIN_IOCP_IO_SERVICE_HPP
Provides core I/O functionality.
Definition: io_service.hpp:184
null_mutex mutex
Definition: mutex.hpp:36
Class to represent an error code value.
Definition: error_code.hpp:80
#define ASIO_DECL
Definition: config.hpp:43