Chris@16
|
1 //
|
Chris@16
|
2 // detail/impl/strand_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_IMPL_STRAND_SERVICE_HPP
|
Chris@16
|
12 #define BOOST_ASIO_DETAIL_IMPL_STRAND_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/addressof.hpp>
|
Chris@16
|
19 #include <boost/asio/detail/call_stack.hpp>
|
Chris@16
|
20 #include <boost/asio/detail/completion_handler.hpp>
|
Chris@16
|
21 #include <boost/asio/detail/fenced_block.hpp>
|
Chris@16
|
22 #include <boost/asio/detail/handler_alloc_helpers.hpp>
|
Chris@16
|
23 #include <boost/asio/detail/handler_invoke_helpers.hpp>
|
Chris@16
|
24
|
Chris@16
|
25 #include <boost/asio/detail/push_options.hpp>
|
Chris@16
|
26
|
Chris@16
|
27 namespace boost {
|
Chris@16
|
28 namespace asio {
|
Chris@16
|
29 namespace detail {
|
Chris@16
|
30
|
Chris@16
|
31 inline strand_service::strand_impl::strand_impl()
|
Chris@16
|
32 : operation(&strand_service::do_complete),
|
Chris@16
|
33 locked_(false)
|
Chris@16
|
34 {
|
Chris@16
|
35 }
|
Chris@16
|
36
|
Chris@16
|
37 struct strand_service::on_dispatch_exit
|
Chris@16
|
38 {
|
Chris@16
|
39 io_service_impl* io_service_;
|
Chris@16
|
40 strand_impl* impl_;
|
Chris@16
|
41
|
Chris@16
|
42 ~on_dispatch_exit()
|
Chris@16
|
43 {
|
Chris@16
|
44 impl_->mutex_.lock();
|
Chris@16
|
45 impl_->ready_queue_.push(impl_->waiting_queue_);
|
Chris@16
|
46 bool more_handlers = impl_->locked_ = !impl_->ready_queue_.empty();
|
Chris@16
|
47 impl_->mutex_.unlock();
|
Chris@16
|
48
|
Chris@16
|
49 if (more_handlers)
|
Chris@16
|
50 io_service_->post_immediate_completion(impl_, false);
|
Chris@16
|
51 }
|
Chris@16
|
52 };
|
Chris@16
|
53
|
Chris@16
|
54 template <typename Handler>
|
Chris@16
|
55 void strand_service::dispatch(strand_service::implementation_type& impl,
|
Chris@16
|
56 Handler& handler)
|
Chris@16
|
57 {
|
Chris@16
|
58 // If we are already in the strand then the handler can run immediately.
|
Chris@16
|
59 if (call_stack<strand_impl>::contains(impl))
|
Chris@16
|
60 {
|
Chris@16
|
61 fenced_block b(fenced_block::full);
|
Chris@16
|
62 boost_asio_handler_invoke_helpers::invoke(handler, handler);
|
Chris@16
|
63 return;
|
Chris@16
|
64 }
|
Chris@16
|
65
|
Chris@16
|
66 // Allocate and construct an operation to wrap the handler.
|
Chris@16
|
67 typedef completion_handler<Handler> op;
|
Chris@16
|
68 typename op::ptr p = { boost::asio::detail::addressof(handler),
|
Chris@16
|
69 boost_asio_handler_alloc_helpers::allocate(
|
Chris@16
|
70 sizeof(op), handler), 0 };
|
Chris@16
|
71 p.p = new (p.v) op(handler);
|
Chris@16
|
72
|
Chris@16
|
73 BOOST_ASIO_HANDLER_CREATION((p.p, "strand", impl, "dispatch"));
|
Chris@16
|
74
|
Chris@16
|
75 bool dispatch_immediately = do_dispatch(impl, p.p);
|
Chris@16
|
76 operation* o = p.p;
|
Chris@16
|
77 p.v = p.p = 0;
|
Chris@16
|
78
|
Chris@16
|
79 if (dispatch_immediately)
|
Chris@16
|
80 {
|
Chris@16
|
81 // Indicate that this strand is executing on the current thread.
|
Chris@16
|
82 call_stack<strand_impl>::context ctx(impl);
|
Chris@16
|
83
|
Chris@16
|
84 // Ensure the next handler, if any, is scheduled on block exit.
|
Chris@16
|
85 on_dispatch_exit on_exit = { &io_service_, impl };
|
Chris@16
|
86 (void)on_exit;
|
Chris@16
|
87
|
Chris@16
|
88 completion_handler<Handler>::do_complete(
|
Chris@16
|
89 &io_service_, o, boost::system::error_code(), 0);
|
Chris@16
|
90 }
|
Chris@16
|
91 }
|
Chris@16
|
92
|
Chris@16
|
93 // Request the io_service to invoke the given handler and return immediately.
|
Chris@16
|
94 template <typename Handler>
|
Chris@16
|
95 void strand_service::post(strand_service::implementation_type& impl,
|
Chris@16
|
96 Handler& handler)
|
Chris@16
|
97 {
|
Chris@16
|
98 bool is_continuation =
|
Chris@16
|
99 boost_asio_handler_cont_helpers::is_continuation(handler);
|
Chris@16
|
100
|
Chris@16
|
101 // Allocate and construct an operation to wrap the handler.
|
Chris@16
|
102 typedef completion_handler<Handler> op;
|
Chris@16
|
103 typename op::ptr p = { boost::asio::detail::addressof(handler),
|
Chris@16
|
104 boost_asio_handler_alloc_helpers::allocate(
|
Chris@16
|
105 sizeof(op), handler), 0 };
|
Chris@16
|
106 p.p = new (p.v) op(handler);
|
Chris@16
|
107
|
Chris@16
|
108 BOOST_ASIO_HANDLER_CREATION((p.p, "strand", impl, "post"));
|
Chris@16
|
109
|
Chris@16
|
110 do_post(impl, p.p, is_continuation);
|
Chris@16
|
111 p.v = p.p = 0;
|
Chris@16
|
112 }
|
Chris@16
|
113
|
Chris@16
|
114 } // namespace detail
|
Chris@16
|
115 } // namespace asio
|
Chris@16
|
116 } // namespace boost
|
Chris@16
|
117
|
Chris@16
|
118 #include <boost/asio/detail/pop_options.hpp>
|
Chris@16
|
119
|
Chris@16
|
120 #endif // BOOST_ASIO_DETAIL_IMPL_STRAND_SERVICE_HPP
|