]>
Commit | Line | Data |
---|---|---|
b32b8144 FG |
1 | // |
2 | // impl/system_executor.hpp | |
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_IMPL_SYSTEM_EXECUTOR_HPP | |
12 | #define BOOST_ASIO_IMPL_SYSTEM_EXECUTOR_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/executor_op.hpp> | |
19 | #include <boost/asio/detail/global.hpp> | |
20 | #include <boost/asio/detail/recycling_allocator.hpp> | |
21 | #include <boost/asio/detail/type_traits.hpp> | |
22 | #include <boost/asio/system_context.hpp> | |
23 | ||
24 | #include <boost/asio/detail/push_options.hpp> | |
25 | ||
26 | namespace boost { | |
27 | namespace asio { | |
28 | ||
29 | inline system_context& system_executor::context() const BOOST_ASIO_NOEXCEPT | |
30 | { | |
31 | return detail::global<system_context>(); | |
32 | } | |
33 | ||
34 | template <typename Function, typename Allocator> | |
35 | void system_executor::dispatch( | |
36 | BOOST_ASIO_MOVE_ARG(Function) f, const Allocator&) const | |
37 | { | |
38 | typename decay<Function>::type tmp(BOOST_ASIO_MOVE_CAST(Function)(f)); | |
39 | boost_asio_handler_invoke_helpers::invoke(tmp, tmp); | |
40 | } | |
41 | ||
42 | template <typename Function, typename Allocator> | |
43 | void system_executor::post( | |
44 | BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const | |
45 | { | |
46 | typedef typename decay<Function>::type function_type; | |
47 | ||
48 | system_context& ctx = detail::global<system_context>(); | |
49 | ||
50 | // Allocate and construct an operation to wrap the function. | |
51 | typedef detail::executor_op<function_type, Allocator> op; | |
52 | typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 }; | |
53 | p.p = new (p.v) op(BOOST_ASIO_MOVE_CAST(Function)(f), a); | |
54 | ||
55 | BOOST_ASIO_HANDLER_CREATION((ctx, *p.p, | |
56 | "system_executor", &this->context(), 0, "post")); | |
57 | ||
58 | ctx.scheduler_.post_immediate_completion(p.p, false); | |
59 | p.v = p.p = 0; | |
60 | } | |
61 | ||
62 | template <typename Function, typename Allocator> | |
63 | void system_executor::defer( | |
64 | BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const | |
65 | { | |
66 | typedef typename decay<Function>::type function_type; | |
67 | ||
68 | system_context& ctx = detail::global<system_context>(); | |
69 | ||
70 | // Allocate and construct an operation to wrap the function. | |
71 | typedef detail::executor_op<function_type, Allocator> op; | |
72 | typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 }; | |
73 | p.p = new (p.v) op(BOOST_ASIO_MOVE_CAST(Function)(f), a); | |
74 | ||
75 | BOOST_ASIO_HANDLER_CREATION((ctx, *p.p, | |
76 | "system_executor", &this->context(), 0, "defer")); | |
77 | ||
78 | ctx.scheduler_.post_immediate_completion(p.p, true); | |
79 | p.v = p.p = 0; | |
80 | } | |
81 | ||
82 | } // namespace asio | |
83 | } // namespace boost | |
84 | ||
85 | #include <boost/asio/detail/pop_options.hpp> | |
86 | ||
87 | #endif // BOOST_ASIO_IMPL_SYSTEM_EXECUTOR_HPP |