2 // impl/system_executor.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 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_IMPL_SYSTEM_EXECUTOR_HPP
12 #define BOOST_ASIO_IMPL_SYSTEM_EXECUTOR_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
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>
24 #include <boost/asio/detail/push_options.hpp>
29 inline system_context& system_executor::context() const BOOST_ASIO_NOEXCEPT
31 return detail::global<system_context>();
34 template <typename Function, typename Allocator>
35 void system_executor::dispatch(
36 BOOST_ASIO_MOVE_ARG(Function) f, const Allocator&) const
38 typename decay<Function>::type tmp(BOOST_ASIO_MOVE_CAST(Function)(f));
39 boost_asio_handler_invoke_helpers::invoke(tmp, tmp);
42 template <typename Function, typename Allocator>
43 void system_executor::post(
44 BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const
46 typedef typename decay<Function>::type function_type;
48 system_context& ctx = detail::global<system_context>();
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);
55 BOOST_ASIO_HANDLER_CREATION((ctx, *p.p,
56 "system_executor", &this->context(), 0, "post"));
58 ctx.scheduler_.post_immediate_completion(p.p, false);
62 template <typename Function, typename Allocator>
63 void system_executor::defer(
64 BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const
66 typedef typename decay<Function>::type function_type;
68 system_context& ctx = detail::global<system_context>();
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);
75 BOOST_ASIO_HANDLER_CREATION((ctx, *p.p,
76 "system_executor", &this->context(), 0, "defer"));
78 ctx.scheduler_.post_immediate_completion(p.p, true);
85 #include <boost/asio/detail/pop_options.hpp>
87 #endif // BOOST_ASIO_IMPL_SYSTEM_EXECUTOR_HPP