]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/asio/detail/impl/win_iocp_socket_service_base.ipp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / asio / detail / impl / win_iocp_socket_service_base.ipp
1 //
2 // detail/impl/win_iocp_socket_service_base.ipp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2017 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_IMPL_WIN_IOCP_SOCKET_SERVICE_BASE_IPP
12 #define BOOST_ASIO_DETAIL_IMPL_WIN_IOCP_SOCKET_SERVICE_BASE_IPP
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/asio/detail/win_iocp_socket_service_base.hpp>
23
24 #include <boost/asio/detail/push_options.hpp>
25
26 namespace boost {
27 namespace asio {
28 namespace detail {
29
30 win_iocp_socket_service_base::win_iocp_socket_service_base(
31 boost::asio::io_context& io_context)
32 : io_context_(io_context),
33 iocp_service_(use_service<win_iocp_io_context>(io_context)),
34 reactor_(0),
35 connect_ex_(0),
36 nt_set_info_(0),
37 mutex_(),
38 impl_list_(0)
39 {
40 }
41
42 void win_iocp_socket_service_base::base_shutdown()
43 {
44 // Close all implementations, causing all operations to complete.
45 boost::asio::detail::mutex::scoped_lock lock(mutex_);
46 base_implementation_type* impl = impl_list_;
47 while (impl)
48 {
49 close_for_destruction(*impl);
50 impl = impl->next_;
51 }
52 }
53
54 void win_iocp_socket_service_base::construct(
55 win_iocp_socket_service_base::base_implementation_type& impl)
56 {
57 impl.socket_ = invalid_socket;
58 impl.state_ = 0;
59 impl.cancel_token_.reset();
60 #if defined(BOOST_ASIO_ENABLE_CANCELIO)
61 impl.safe_cancellation_thread_id_ = 0;
62 #endif // defined(BOOST_ASIO_ENABLE_CANCELIO)
63
64 // Insert implementation into linked list of all implementations.
65 boost::asio::detail::mutex::scoped_lock lock(mutex_);
66 impl.next_ = impl_list_;
67 impl.prev_ = 0;
68 if (impl_list_)
69 impl_list_->prev_ = &impl;
70 impl_list_ = &impl;
71 }
72
73 void win_iocp_socket_service_base::base_move_construct(
74 win_iocp_socket_service_base::base_implementation_type& impl,
75 win_iocp_socket_service_base::base_implementation_type& other_impl)
76 {
77 impl.socket_ = other_impl.socket_;
78 other_impl.socket_ = invalid_socket;
79
80 impl.state_ = other_impl.state_;
81 other_impl.state_ = 0;
82
83 impl.cancel_token_ = other_impl.cancel_token_;
84 other_impl.cancel_token_.reset();
85
86 #if defined(BOOST_ASIO_ENABLE_CANCELIO)
87 impl.safe_cancellation_thread_id_ = other_impl.safe_cancellation_thread_id_;
88 other_impl.safe_cancellation_thread_id_ = 0;
89 #endif // defined(BOOST_ASIO_ENABLE_CANCELIO)
90
91 // Insert implementation into linked list of all implementations.
92 boost::asio::detail::mutex::scoped_lock lock(mutex_);
93 impl.next_ = impl_list_;
94 impl.prev_ = 0;
95 if (impl_list_)
96 impl_list_->prev_ = &impl;
97 impl_list_ = &impl;
98 }
99
100 void win_iocp_socket_service_base::base_move_assign(
101 win_iocp_socket_service_base::base_implementation_type& impl,
102 win_iocp_socket_service_base& other_service,
103 win_iocp_socket_service_base::base_implementation_type& other_impl)
104 {
105 close_for_destruction(impl);
106
107 if (this != &other_service)
108 {
109 // Remove implementation from linked list of all implementations.
110 boost::asio::detail::mutex::scoped_lock lock(mutex_);
111 if (impl_list_ == &impl)
112 impl_list_ = impl.next_;
113 if (impl.prev_)
114 impl.prev_->next_ = impl.next_;
115 if (impl.next_)
116 impl.next_->prev_= impl.prev_;
117 impl.next_ = 0;
118 impl.prev_ = 0;
119 }
120
121 impl.socket_ = other_impl.socket_;
122 other_impl.socket_ = invalid_socket;
123
124 impl.state_ = other_impl.state_;
125 other_impl.state_ = 0;
126
127 impl.cancel_token_ = other_impl.cancel_token_;
128 other_impl.cancel_token_.reset();
129
130 #if defined(BOOST_ASIO_ENABLE_CANCELIO)
131 impl.safe_cancellation_thread_id_ = other_impl.safe_cancellation_thread_id_;
132 other_impl.safe_cancellation_thread_id_ = 0;
133 #endif // defined(BOOST_ASIO_ENABLE_CANCELIO)
134
135 if (this != &other_service)
136 {
137 // Insert implementation into linked list of all implementations.
138 boost::asio::detail::mutex::scoped_lock lock(other_service.mutex_);
139 impl.next_ = other_service.impl_list_;
140 impl.prev_ = 0;
141 if (other_service.impl_list_)
142 other_service.impl_list_->prev_ = &impl;
143 other_service.impl_list_ = &impl;
144 }
145 }
146
147 void win_iocp_socket_service_base::destroy(
148 win_iocp_socket_service_base::base_implementation_type& impl)
149 {
150 close_for_destruction(impl);
151
152 // Remove implementation from linked list of all implementations.
153 boost::asio::detail::mutex::scoped_lock lock(mutex_);
154 if (impl_list_ == &impl)
155 impl_list_ = impl.next_;
156 if (impl.prev_)
157 impl.prev_->next_ = impl.next_;
158 if (impl.next_)
159 impl.next_->prev_= impl.prev_;
160 impl.next_ = 0;
161 impl.prev_ = 0;
162 }
163
164 boost::system::error_code win_iocp_socket_service_base::close(
165 win_iocp_socket_service_base::base_implementation_type& impl,
166 boost::system::error_code& ec)
167 {
168 if (is_open(impl))
169 {
170 BOOST_ASIO_HANDLER_OPERATION((iocp_service_.context(),
171 "socket", &impl, impl.socket_, "close"));
172
173 // Check if the reactor was created, in which case we need to close the
174 // socket on the reactor as well to cancel any operations that might be
175 // running there.
176 select_reactor* r = static_cast<select_reactor*>(
177 interlocked_compare_exchange_pointer(
178 reinterpret_cast<void**>(&reactor_), 0, 0));
179 if (r)
180 r->deregister_descriptor(impl.socket_, impl.reactor_data_, true);
181
182 socket_ops::close(impl.socket_, impl.state_, false, ec);
183
184 if (r)
185 r->cleanup_descriptor_data(impl.reactor_data_);
186 }
187 else
188 {
189 ec = boost::system::error_code();
190 }
191
192 impl.socket_ = invalid_socket;
193 impl.state_ = 0;
194 impl.cancel_token_.reset();
195 #if defined(BOOST_ASIO_ENABLE_CANCELIO)
196 impl.safe_cancellation_thread_id_ = 0;
197 #endif // defined(BOOST_ASIO_ENABLE_CANCELIO)
198
199 return ec;
200 }
201
202 socket_type win_iocp_socket_service_base::release(
203 win_iocp_socket_service_base::base_implementation_type& impl,
204 boost::system::error_code& ec)
205 {
206 if (!is_open(impl))
207 return invalid_socket;
208
209 cancel(impl, ec);
210 if (ec)
211 return invalid_socket;
212
213 nt_set_info_fn fn = get_nt_set_info();
214 if (fn == 0)
215 {
216 ec = boost::asio::error::operation_not_supported;
217 return invalid_socket;
218 }
219
220 HANDLE sock_as_handle = reinterpret_cast<HANDLE>(impl.socket_);
221 ULONG_PTR iosb[2] = { 0, 0 };
222 void* info[2] = { 0, 0 };
223 if (fn(sock_as_handle, iosb, &info, sizeof(info),
224 61 /* FileReplaceCompletionInformation */))
225 {
226 ec = boost::asio::error::operation_not_supported;
227 return invalid_socket;
228 }
229
230 socket_type tmp = impl.socket_;
231 impl.socket_ = invalid_socket;
232 return tmp;
233 }
234
235 boost::system::error_code win_iocp_socket_service_base::cancel(
236 win_iocp_socket_service_base::base_implementation_type& impl,
237 boost::system::error_code& ec)
238 {
239 if (!is_open(impl))
240 {
241 ec = boost::asio::error::bad_descriptor;
242 return ec;
243 }
244
245 BOOST_ASIO_HANDLER_OPERATION((iocp_service_.context(),
246 "socket", &impl, impl.socket_, "cancel"));
247
248 if (FARPROC cancel_io_ex_ptr = ::GetProcAddress(
249 ::GetModuleHandleA("KERNEL32"), "CancelIoEx"))
250 {
251 // The version of Windows supports cancellation from any thread.
252 typedef BOOL (WINAPI* cancel_io_ex_t)(HANDLE, LPOVERLAPPED);
253 cancel_io_ex_t cancel_io_ex = (cancel_io_ex_t)cancel_io_ex_ptr;
254 socket_type sock = impl.socket_;
255 HANDLE sock_as_handle = reinterpret_cast<HANDLE>(sock);
256 if (!cancel_io_ex(sock_as_handle, 0))
257 {
258 DWORD last_error = ::GetLastError();
259 if (last_error == ERROR_NOT_FOUND)
260 {
261 // ERROR_NOT_FOUND means that there were no operations to be
262 // cancelled. We swallow this error to match the behaviour on other
263 // platforms.
264 ec = boost::system::error_code();
265 }
266 else
267 {
268 ec = boost::system::error_code(last_error,
269 boost::asio::error::get_system_category());
270 }
271 }
272 else
273 {
274 ec = boost::system::error_code();
275 }
276 }
277 #if defined(BOOST_ASIO_ENABLE_CANCELIO)
278 else if (impl.safe_cancellation_thread_id_ == 0)
279 {
280 // No operations have been started, so there's nothing to cancel.
281 ec = boost::system::error_code();
282 }
283 else if (impl.safe_cancellation_thread_id_ == ::GetCurrentThreadId())
284 {
285 // Asynchronous operations have been started from the current thread only,
286 // so it is safe to try to cancel them using CancelIo.
287 socket_type sock = impl.socket_;
288 HANDLE sock_as_handle = reinterpret_cast<HANDLE>(sock);
289 if (!::CancelIo(sock_as_handle))
290 {
291 DWORD last_error = ::GetLastError();
292 ec = boost::system::error_code(last_error,
293 boost::asio::error::get_system_category());
294 }
295 else
296 {
297 ec = boost::system::error_code();
298 }
299 }
300 else
301 {
302 // Asynchronous operations have been started from more than one thread,
303 // so cancellation is not safe.
304 ec = boost::asio::error::operation_not_supported;
305 }
306 #else // defined(BOOST_ASIO_ENABLE_CANCELIO)
307 else
308 {
309 // Cancellation is not supported as CancelIo may not be used.
310 ec = boost::asio::error::operation_not_supported;
311 }
312 #endif // defined(BOOST_ASIO_ENABLE_CANCELIO)
313
314 // Cancel any operations started via the reactor.
315 if (!ec)
316 {
317 select_reactor* r = static_cast<select_reactor*>(
318 interlocked_compare_exchange_pointer(
319 reinterpret_cast<void**>(&reactor_), 0, 0));
320 if (r)
321 r->cancel_ops(impl.socket_, impl.reactor_data_);
322 }
323
324 return ec;
325 }
326
327 boost::system::error_code win_iocp_socket_service_base::do_open(
328 win_iocp_socket_service_base::base_implementation_type& impl,
329 int family, int type, int protocol, boost::system::error_code& ec)
330 {
331 if (is_open(impl))
332 {
333 ec = boost::asio::error::already_open;
334 return ec;
335 }
336
337 socket_holder sock(socket_ops::socket(family, type, protocol, ec));
338 if (sock.get() == invalid_socket)
339 return ec;
340
341 HANDLE sock_as_handle = reinterpret_cast<HANDLE>(sock.get());
342 if (iocp_service_.register_handle(sock_as_handle, ec))
343 return ec;
344
345 impl.socket_ = sock.release();
346 switch (type)
347 {
348 case SOCK_STREAM: impl.state_ = socket_ops::stream_oriented; break;
349 case SOCK_DGRAM: impl.state_ = socket_ops::datagram_oriented; break;
350 default: impl.state_ = 0; break;
351 }
352 impl.cancel_token_.reset(static_cast<void*>(0), socket_ops::noop_deleter());
353 ec = boost::system::error_code();
354 return ec;
355 }
356
357 boost::system::error_code win_iocp_socket_service_base::do_assign(
358 win_iocp_socket_service_base::base_implementation_type& impl,
359 int type, socket_type native_socket, boost::system::error_code& ec)
360 {
361 if (is_open(impl))
362 {
363 ec = boost::asio::error::already_open;
364 return ec;
365 }
366
367 HANDLE sock_as_handle = reinterpret_cast<HANDLE>(native_socket);
368 if (iocp_service_.register_handle(sock_as_handle, ec))
369 return ec;
370
371 impl.socket_ = native_socket;
372 switch (type)
373 {
374 case SOCK_STREAM: impl.state_ = socket_ops::stream_oriented; break;
375 case SOCK_DGRAM: impl.state_ = socket_ops::datagram_oriented; break;
376 default: impl.state_ = 0; break;
377 }
378 impl.cancel_token_.reset(static_cast<void*>(0), socket_ops::noop_deleter());
379 ec = boost::system::error_code();
380 return ec;
381 }
382
383 void win_iocp_socket_service_base::start_send_op(
384 win_iocp_socket_service_base::base_implementation_type& impl,
385 WSABUF* buffers, std::size_t buffer_count,
386 socket_base::message_flags flags, bool noop, operation* op)
387 {
388 update_cancellation_thread_id(impl);
389 iocp_service_.work_started();
390
391 if (noop)
392 iocp_service_.on_completion(op);
393 else if (!is_open(impl))
394 iocp_service_.on_completion(op, boost::asio::error::bad_descriptor);
395 else
396 {
397 DWORD bytes_transferred = 0;
398 int result = ::WSASend(impl.socket_, buffers,
399 static_cast<DWORD>(buffer_count), &bytes_transferred, flags, op, 0);
400 DWORD last_error = ::WSAGetLastError();
401 if (last_error == ERROR_PORT_UNREACHABLE)
402 last_error = WSAECONNREFUSED;
403 if (result != 0 && last_error != WSA_IO_PENDING)
404 iocp_service_.on_completion(op, last_error, bytes_transferred);
405 else
406 iocp_service_.on_pending(op);
407 }
408 }
409
410 void win_iocp_socket_service_base::start_send_to_op(
411 win_iocp_socket_service_base::base_implementation_type& impl,
412 WSABUF* buffers, std::size_t buffer_count,
413 const socket_addr_type* addr, int addrlen,
414 socket_base::message_flags flags, operation* op)
415 {
416 update_cancellation_thread_id(impl);
417 iocp_service_.work_started();
418
419 if (!is_open(impl))
420 iocp_service_.on_completion(op, boost::asio::error::bad_descriptor);
421 else
422 {
423 DWORD bytes_transferred = 0;
424 int result = ::WSASendTo(impl.socket_, buffers,
425 static_cast<DWORD>(buffer_count),
426 &bytes_transferred, flags, addr, addrlen, op, 0);
427 DWORD last_error = ::WSAGetLastError();
428 if (last_error == ERROR_PORT_UNREACHABLE)
429 last_error = WSAECONNREFUSED;
430 if (result != 0 && last_error != WSA_IO_PENDING)
431 iocp_service_.on_completion(op, last_error, bytes_transferred);
432 else
433 iocp_service_.on_pending(op);
434 }
435 }
436
437 void win_iocp_socket_service_base::start_receive_op(
438 win_iocp_socket_service_base::base_implementation_type& impl,
439 WSABUF* buffers, std::size_t buffer_count,
440 socket_base::message_flags flags, bool noop, operation* op)
441 {
442 update_cancellation_thread_id(impl);
443 iocp_service_.work_started();
444
445 if (noop)
446 iocp_service_.on_completion(op);
447 else if (!is_open(impl))
448 iocp_service_.on_completion(op, boost::asio::error::bad_descriptor);
449 else
450 {
451 DWORD bytes_transferred = 0;
452 DWORD recv_flags = flags;
453 int result = ::WSARecv(impl.socket_, buffers,
454 static_cast<DWORD>(buffer_count),
455 &bytes_transferred, &recv_flags, op, 0);
456 DWORD last_error = ::WSAGetLastError();
457 if (last_error == ERROR_NETNAME_DELETED)
458 last_error = WSAECONNRESET;
459 else if (last_error == ERROR_PORT_UNREACHABLE)
460 last_error = WSAECONNREFUSED;
461 if (result != 0 && last_error != WSA_IO_PENDING)
462 iocp_service_.on_completion(op, last_error, bytes_transferred);
463 else
464 iocp_service_.on_pending(op);
465 }
466 }
467
468 void win_iocp_socket_service_base::start_null_buffers_receive_op(
469 win_iocp_socket_service_base::base_implementation_type& impl,
470 socket_base::message_flags flags, reactor_op* op)
471 {
472 if ((impl.state_ & socket_ops::stream_oriented) != 0)
473 {
474 // For stream sockets on Windows, we may issue a 0-byte overlapped
475 // WSARecv to wait until there is data available on the socket.
476 ::WSABUF buf = { 0, 0 };
477 start_receive_op(impl, &buf, 1, flags, false, op);
478 }
479 else
480 {
481 start_reactor_op(impl,
482 (flags & socket_base::message_out_of_band)
483 ? select_reactor::except_op : select_reactor::read_op,
484 op);
485 }
486 }
487
488 void win_iocp_socket_service_base::start_receive_from_op(
489 win_iocp_socket_service_base::base_implementation_type& impl,
490 WSABUF* buffers, std::size_t buffer_count, socket_addr_type* addr,
491 socket_base::message_flags flags, int* addrlen, operation* op)
492 {
493 update_cancellation_thread_id(impl);
494 iocp_service_.work_started();
495
496 if (!is_open(impl))
497 iocp_service_.on_completion(op, boost::asio::error::bad_descriptor);
498 else
499 {
500 DWORD bytes_transferred = 0;
501 DWORD recv_flags = flags;
502 int result = ::WSARecvFrom(impl.socket_, buffers,
503 static_cast<DWORD>(buffer_count),
504 &bytes_transferred, &recv_flags, addr, addrlen, op, 0);
505 DWORD last_error = ::WSAGetLastError();
506 if (last_error == ERROR_PORT_UNREACHABLE)
507 last_error = WSAECONNREFUSED;
508 if (result != 0 && last_error != WSA_IO_PENDING)
509 iocp_service_.on_completion(op, last_error, bytes_transferred);
510 else
511 iocp_service_.on_pending(op);
512 }
513 }
514
515 void win_iocp_socket_service_base::start_accept_op(
516 win_iocp_socket_service_base::base_implementation_type& impl,
517 bool peer_is_open, socket_holder& new_socket, int family, int type,
518 int protocol, void* output_buffer, DWORD address_length, operation* op)
519 {
520 update_cancellation_thread_id(impl);
521 iocp_service_.work_started();
522
523 if (!is_open(impl))
524 iocp_service_.on_completion(op, boost::asio::error::bad_descriptor);
525 else if (peer_is_open)
526 iocp_service_.on_completion(op, boost::asio::error::already_open);
527 else
528 {
529 boost::system::error_code ec;
530 new_socket.reset(socket_ops::socket(family, type, protocol, ec));
531 if (new_socket.get() == invalid_socket)
532 iocp_service_.on_completion(op, ec);
533 else
534 {
535 DWORD bytes_read = 0;
536 BOOL result = ::AcceptEx(impl.socket_, new_socket.get(), output_buffer,
537 0, address_length, address_length, &bytes_read, op);
538 DWORD last_error = ::WSAGetLastError();
539 if (!result && last_error != WSA_IO_PENDING)
540 iocp_service_.on_completion(op, last_error);
541 else
542 iocp_service_.on_pending(op);
543 }
544 }
545 }
546
547 void win_iocp_socket_service_base::restart_accept_op(
548 socket_type s, socket_holder& new_socket, int family, int type,
549 int protocol, void* output_buffer, DWORD address_length, operation* op)
550 {
551 new_socket.reset();
552 iocp_service_.work_started();
553
554 boost::system::error_code ec;
555 new_socket.reset(socket_ops::socket(family, type, protocol, ec));
556 if (new_socket.get() == invalid_socket)
557 iocp_service_.on_completion(op, ec);
558 else
559 {
560 DWORD bytes_read = 0;
561 BOOL result = ::AcceptEx(s, new_socket.get(), output_buffer,
562 0, address_length, address_length, &bytes_read, op);
563 DWORD last_error = ::WSAGetLastError();
564 if (!result && last_error != WSA_IO_PENDING)
565 iocp_service_.on_completion(op, last_error);
566 else
567 iocp_service_.on_pending(op);
568 }
569 }
570
571 void win_iocp_socket_service_base::start_reactor_op(
572 win_iocp_socket_service_base::base_implementation_type& impl,
573 int op_type, reactor_op* op)
574 {
575 select_reactor& r = get_reactor();
576 update_cancellation_thread_id(impl);
577
578 if (is_open(impl))
579 {
580 r.start_op(op_type, impl.socket_, impl.reactor_data_, op, false, false);
581 return;
582 }
583 else
584 op->ec_ = boost::asio::error::bad_descriptor;
585
586 iocp_service_.post_immediate_completion(op, false);
587 }
588
589 void win_iocp_socket_service_base::start_connect_op(
590 win_iocp_socket_service_base::base_implementation_type& impl,
591 int family, int type, const socket_addr_type* addr,
592 std::size_t addrlen, win_iocp_socket_connect_op_base* op)
593 {
594 // If ConnectEx is available, use that.
595 if (family == BOOST_ASIO_OS_DEF(AF_INET)
596 || family == BOOST_ASIO_OS_DEF(AF_INET6))
597 {
598 if (connect_ex_fn connect_ex = get_connect_ex(impl, type))
599 {
600 union address_union
601 {
602 socket_addr_type base;
603 sockaddr_in4_type v4;
604 sockaddr_in6_type v6;
605 } a;
606
607 using namespace std; // For memset.
608 memset(&a, 0, sizeof(a));
609 a.base.sa_family = family;
610
611 socket_ops::bind(impl.socket_, &a.base,
612 family == BOOST_ASIO_OS_DEF(AF_INET)
613 ? sizeof(a.v4) : sizeof(a.v6), op->ec_);
614 if (op->ec_ && op->ec_ != boost::asio::error::invalid_argument)
615 {
616 iocp_service_.post_immediate_completion(op, false);
617 return;
618 }
619
620 op->connect_ex_ = true;
621 update_cancellation_thread_id(impl);
622 iocp_service_.work_started();
623
624 BOOL result = connect_ex(impl.socket_,
625 addr, static_cast<int>(addrlen), 0, 0, 0, op);
626 DWORD last_error = ::WSAGetLastError();
627 if (!result && last_error != WSA_IO_PENDING)
628 iocp_service_.on_completion(op, last_error);
629 else
630 iocp_service_.on_pending(op);
631 return;
632 }
633 }
634
635 // Otherwise, fall back to a reactor-based implementation.
636 select_reactor& r = get_reactor();
637 update_cancellation_thread_id(impl);
638
639 if ((impl.state_ & socket_ops::non_blocking) != 0
640 || socket_ops::set_internal_non_blocking(
641 impl.socket_, impl.state_, true, op->ec_))
642 {
643 if (socket_ops::connect(impl.socket_, addr, addrlen, op->ec_) != 0)
644 {
645 if (op->ec_ == boost::asio::error::in_progress
646 || op->ec_ == boost::asio::error::would_block)
647 {
648 op->ec_ = boost::system::error_code();
649 r.start_op(select_reactor::connect_op, impl.socket_,
650 impl.reactor_data_, op, false, false);
651 return;
652 }
653 }
654 }
655
656 r.post_immediate_completion(op, false);
657 }
658
659 void win_iocp_socket_service_base::close_for_destruction(
660 win_iocp_socket_service_base::base_implementation_type& impl)
661 {
662 if (is_open(impl))
663 {
664 BOOST_ASIO_HANDLER_OPERATION((iocp_service_.context(),
665 "socket", &impl, impl.socket_, "close"));
666
667 // Check if the reactor was created, in which case we need to close the
668 // socket on the reactor as well to cancel any operations that might be
669 // running there.
670 select_reactor* r = static_cast<select_reactor*>(
671 interlocked_compare_exchange_pointer(
672 reinterpret_cast<void**>(&reactor_), 0, 0));
673 if (r)
674 r->deregister_descriptor(impl.socket_, impl.reactor_data_, true);
675
676 boost::system::error_code ignored_ec;
677 socket_ops::close(impl.socket_, impl.state_, true, ignored_ec);
678
679 if (r)
680 r->cleanup_descriptor_data(impl.reactor_data_);
681 }
682
683 impl.socket_ = invalid_socket;
684 impl.state_ = 0;
685 impl.cancel_token_.reset();
686 #if defined(BOOST_ASIO_ENABLE_CANCELIO)
687 impl.safe_cancellation_thread_id_ = 0;
688 #endif // defined(BOOST_ASIO_ENABLE_CANCELIO)
689 }
690
691 void win_iocp_socket_service_base::update_cancellation_thread_id(
692 win_iocp_socket_service_base::base_implementation_type& impl)
693 {
694 #if defined(BOOST_ASIO_ENABLE_CANCELIO)
695 if (impl.safe_cancellation_thread_id_ == 0)
696 impl.safe_cancellation_thread_id_ = ::GetCurrentThreadId();
697 else if (impl.safe_cancellation_thread_id_ != ::GetCurrentThreadId())
698 impl.safe_cancellation_thread_id_ = ~DWORD(0);
699 #else // defined(BOOST_ASIO_ENABLE_CANCELIO)
700 (void)impl;
701 #endif // defined(BOOST_ASIO_ENABLE_CANCELIO)
702 }
703
704 select_reactor& win_iocp_socket_service_base::get_reactor()
705 {
706 select_reactor* r = static_cast<select_reactor*>(
707 interlocked_compare_exchange_pointer(
708 reinterpret_cast<void**>(&reactor_), 0, 0));
709 if (!r)
710 {
711 r = &(use_service<select_reactor>(io_context_));
712 interlocked_exchange_pointer(reinterpret_cast<void**>(&reactor_), r);
713 }
714 return *r;
715 }
716
717 win_iocp_socket_service_base::connect_ex_fn
718 win_iocp_socket_service_base::get_connect_ex(
719 win_iocp_socket_service_base::base_implementation_type& impl, int type)
720 {
721 #if defined(BOOST_ASIO_DISABLE_CONNECTEX)
722 (void)impl;
723 (void)type;
724 return 0;
725 #else // defined(BOOST_ASIO_DISABLE_CONNECTEX)
726 if (type != BOOST_ASIO_OS_DEF(SOCK_STREAM)
727 && type != BOOST_ASIO_OS_DEF(SOCK_SEQPACKET))
728 return 0;
729
730 void* ptr = interlocked_compare_exchange_pointer(&connect_ex_, 0, 0);
731 if (!ptr)
732 {
733 GUID guid = { 0x25a207b9, 0xddf3, 0x4660,
734 { 0x8e, 0xe9, 0x76, 0xe5, 0x8c, 0x74, 0x06, 0x3e } };
735
736 DWORD bytes = 0;
737 if (::WSAIoctl(impl.socket_, SIO_GET_EXTENSION_FUNCTION_POINTER,
738 &guid, sizeof(guid), &ptr, sizeof(ptr), &bytes, 0, 0) != 0)
739 {
740 // Set connect_ex_ to a special value to indicate that ConnectEx is
741 // unavailable. That way we won't bother trying to look it up again.
742 ptr = this;
743 }
744
745 interlocked_exchange_pointer(&connect_ex_, ptr);
746 }
747
748 return reinterpret_cast<connect_ex_fn>(ptr == this ? 0 : ptr);
749 #endif // defined(BOOST_ASIO_DISABLE_CONNECTEX)
750 }
751
752 win_iocp_socket_service_base::nt_set_info_fn
753 win_iocp_socket_service_base::get_nt_set_info()
754 {
755 void* ptr = interlocked_compare_exchange_pointer(&nt_set_info_, 0, 0);
756 if (!ptr)
757 {
758 if (HMODULE h = ::GetModuleHandleA("NTDLL.DLL"))
759 ptr = reinterpret_cast<void*>(GetProcAddress(h, "NtSetInformationFile"));
760
761 // On failure, set nt_set_info_ to a special value to indicate that the
762 // NtSetInformationFile function is unavailable. That way we won't bother
763 // trying to look it up again.
764 interlocked_exchange_pointer(&nt_set_info_, ptr ? ptr : this);
765 }
766
767 return reinterpret_cast<nt_set_info_fn>(ptr == this ? 0 : ptr);
768 }
769
770 void* win_iocp_socket_service_base::interlocked_compare_exchange_pointer(
771 void** dest, void* exch, void* cmp)
772 {
773 #if defined(_M_IX86)
774 return reinterpret_cast<void*>(InterlockedCompareExchange(
775 reinterpret_cast<PLONG>(dest), reinterpret_cast<LONG>(exch),
776 reinterpret_cast<LONG>(cmp)));
777 #else
778 return InterlockedCompareExchangePointer(dest, exch, cmp);
779 #endif
780 }
781
782 void* win_iocp_socket_service_base::interlocked_exchange_pointer(
783 void** dest, void* val)
784 {
785 #if defined(_M_IX86)
786 return reinterpret_cast<void*>(InterlockedExchange(
787 reinterpret_cast<PLONG>(dest), reinterpret_cast<LONG>(val)));
788 #else
789 return InterlockedExchangePointer(dest, val);
790 #endif
791 }
792
793 } // namespace detail
794 } // namespace asio
795 } // namespace boost
796
797 #include <boost/asio/detail/pop_options.hpp>
798
799 #endif // defined(BOOST_ASIO_HAS_IOCP)
800
801 #endif // BOOST_ASIO_DETAIL_IMPL_WIN_IOCP_SOCKET_SERVICE_BASE_IPP