2 // detail/impl/win_iocp_io_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2016 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_DETAIL_IMPL_WIN_IOCP_IO_SERVICE_HPP
12 #define BOOST_ASIO_DETAIL_IMPL_WIN_IOCP_IO_SERVICE_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #if defined(BOOST_ASIO_HAS_IOCP)
22 #include <boost/asio/detail/addressof.hpp>
23 #include <boost/asio/detail/completion_handler.hpp>
24 #include <boost/asio/detail/fenced_block.hpp>
25 #include <boost/asio/detail/handler_alloc_helpers.hpp>
26 #include <boost/asio/detail/handler_invoke_helpers.hpp>
28 #include <boost/asio/detail/push_options.hpp>
34 template <typename Handler>
35 void win_iocp_io_service::dispatch(Handler& handler)
37 if (thread_call_stack::contains(this))
39 fenced_block b(fenced_block::full);
40 boost_asio_handler_invoke_helpers::invoke(handler, handler);
44 // Allocate and construct an operation to wrap the handler.
45 typedef completion_handler<Handler> op;
46 typename op::ptr p = { boost::asio::detail::addressof(handler),
47 boost_asio_handler_alloc_helpers::allocate(
48 sizeof(op), handler), 0 };
49 p.p = new (p.v) op(handler);
51 BOOST_ASIO_HANDLER_CREATION((p.p, "io_service", this, "dispatch"));
53 post_immediate_completion(p.p, false);
58 template <typename Handler>
59 void win_iocp_io_service::post(Handler& handler)
61 // Allocate and construct an operation to wrap the handler.
62 typedef completion_handler<Handler> op;
63 typename op::ptr p = { boost::asio::detail::addressof(handler),
64 boost_asio_handler_alloc_helpers::allocate(
65 sizeof(op), handler), 0 };
66 p.p = new (p.v) op(handler);
68 BOOST_ASIO_HANDLER_CREATION((p.p, "io_service", this, "post"));
70 post_immediate_completion(p.p, false);
74 template <typename Time_Traits>
75 void win_iocp_io_service::add_timer_queue(
76 timer_queue<Time_Traits>& queue)
78 do_add_timer_queue(queue);
81 template <typename Time_Traits>
82 void win_iocp_io_service::remove_timer_queue(
83 timer_queue<Time_Traits>& queue)
85 do_remove_timer_queue(queue);
88 template <typename Time_Traits>
89 void win_iocp_io_service::schedule_timer(timer_queue<Time_Traits>& queue,
90 const typename Time_Traits::time_type& time,
91 typename timer_queue<Time_Traits>::per_timer_data& timer, wait_op* op)
93 // If the service has been shut down we silently discard the timer.
94 if (::InterlockedExchangeAdd(&shutdown_, 0) != 0)
96 post_immediate_completion(op, false);
100 mutex::scoped_lock lock(dispatch_mutex_);
102 bool earliest = queue.enqueue_timer(time, timer, op);
108 template <typename Time_Traits>
109 std::size_t win_iocp_io_service::cancel_timer(timer_queue<Time_Traits>& queue,
110 typename timer_queue<Time_Traits>::per_timer_data& timer,
111 std::size_t max_cancelled)
113 // If the service has been shut down we silently ignore the cancellation.
114 if (::InterlockedExchangeAdd(&shutdown_, 0) != 0)
117 mutex::scoped_lock lock(dispatch_mutex_);
118 op_queue<win_iocp_operation> ops;
119 std::size_t n = queue.cancel_timer(timer, ops, max_cancelled);
120 post_deferred_completions(ops);
124 } // namespace detail
128 #include <boost/asio/detail/pop_options.hpp>
130 #endif // defined(BOOST_ASIO_HAS_IOCP)
132 #endif // BOOST_ASIO_DETAIL_IMPL_WIN_IOCP_IO_SERVICE_HPP