]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // detail/impl/win_iocp_io_service.hpp | |
3 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
4 | // | |
5 | // Copyright (c) 2003-2016 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_IO_SERVICE_HPP | |
12 | #define BOOST_ASIO_DETAIL_IMPL_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/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> | |
27 | ||
28 | #include <boost/asio/detail/push_options.hpp> | |
29 | ||
30 | namespace boost { | |
31 | namespace asio { | |
32 | namespace detail { | |
33 | ||
34 | template <typename Handler> | |
35 | void win_iocp_io_service::dispatch(Handler& handler) | |
36 | { | |
37 | if (thread_call_stack::contains(this)) | |
38 | { | |
39 | fenced_block b(fenced_block::full); | |
40 | boost_asio_handler_invoke_helpers::invoke(handler, handler); | |
41 | } | |
42 | else | |
43 | { | |
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); | |
50 | ||
51 | BOOST_ASIO_HANDLER_CREATION((p.p, "io_service", this, "dispatch")); | |
52 | ||
53 | post_immediate_completion(p.p, false); | |
54 | p.v = p.p = 0; | |
55 | } | |
56 | } | |
57 | ||
58 | template <typename Handler> | |
59 | void win_iocp_io_service::post(Handler& handler) | |
60 | { | |
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); | |
67 | ||
68 | BOOST_ASIO_HANDLER_CREATION((p.p, "io_service", this, "post")); | |
69 | ||
70 | post_immediate_completion(p.p, false); | |
71 | p.v = p.p = 0; | |
72 | } | |
73 | ||
74 | template <typename Time_Traits> | |
75 | void win_iocp_io_service::add_timer_queue( | |
76 | timer_queue<Time_Traits>& queue) | |
77 | { | |
78 | do_add_timer_queue(queue); | |
79 | } | |
80 | ||
81 | template <typename Time_Traits> | |
82 | void win_iocp_io_service::remove_timer_queue( | |
83 | timer_queue<Time_Traits>& queue) | |
84 | { | |
85 | do_remove_timer_queue(queue); | |
86 | } | |
87 | ||
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) | |
92 | { | |
93 | // If the service has been shut down we silently discard the timer. | |
94 | if (::InterlockedExchangeAdd(&shutdown_, 0) != 0) | |
95 | { | |
96 | post_immediate_completion(op, false); | |
97 | return; | |
98 | } | |
99 | ||
100 | mutex::scoped_lock lock(dispatch_mutex_); | |
101 | ||
102 | bool earliest = queue.enqueue_timer(time, timer, op); | |
103 | work_started(); | |
104 | if (earliest) | |
105 | update_timeout(); | |
106 | } | |
107 | ||
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) | |
112 | { | |
113 | // If the service has been shut down we silently ignore the cancellation. | |
114 | if (::InterlockedExchangeAdd(&shutdown_, 0) != 0) | |
115 | return 0; | |
116 | ||
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); | |
121 | return n; | |
122 | } | |
123 | ||
124 | } // namespace detail | |
125 | } // namespace asio | |
126 | } // namespace boost | |
127 | ||
128 | #include <boost/asio/detail/pop_options.hpp> | |
129 | ||
130 | #endif // defined(BOOST_ASIO_HAS_IOCP) | |
131 | ||
132 | #endif // BOOST_ASIO_DETAIL_IMPL_WIN_IOCP_IO_SERVICE_HPP |