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