2 // impl/system_executor.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2020 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 template <typename Blocking, typename Relationship, typename Allocator>
30 inline system_context&
31 basic_system_executor<Blocking, Relationship, Allocator>::query(
32 execution::context_t) BOOST_ASIO_NOEXCEPT
34 return detail::global<system_context>();
37 template <typename Blocking, typename Relationship, typename Allocator>
39 basic_system_executor<Blocking, Relationship, Allocator>::query(
40 execution::occupancy_t) const BOOST_ASIO_NOEXCEPT
42 return detail::global<system_context>().num_threads_;
45 template <typename Blocking, typename Relationship, typename Allocator>
46 template <typename Function>
48 basic_system_executor<Blocking, Relationship, Allocator>::do_execute(
49 BOOST_ASIO_MOVE_ARG(Function) f, execution::blocking_t::possibly_t) const
51 // Obtain a non-const instance of the function.
52 detail::non_const_lvalue<Function> f2(f);
54 #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
57 #endif// !defined(BOOST_ASIO_NO_EXCEPTIONS)
58 detail::fenced_block b(detail::fenced_block::full);
59 boost_asio_handler_invoke_helpers::invoke(f2.value, f2.value);
60 #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
66 #endif// !defined(BOOST_ASIO_NO_EXCEPTIONS)
69 template <typename Blocking, typename Relationship, typename Allocator>
70 template <typename Function>
72 basic_system_executor<Blocking, Relationship, Allocator>::do_execute(
73 BOOST_ASIO_MOVE_ARG(Function) f, execution::blocking_t::always_t) const
75 // Obtain a non-const instance of the function.
76 detail::non_const_lvalue<Function> f2(f);
78 #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
81 #endif// !defined(BOOST_ASIO_NO_EXCEPTIONS)
82 detail::fenced_block b(detail::fenced_block::full);
83 boost_asio_handler_invoke_helpers::invoke(f2.value, f2.value);
84 #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
90 #endif// !defined(BOOST_ASIO_NO_EXCEPTIONS)
93 template <typename Blocking, typename Relationship, typename Allocator>
94 template <typename Function>
95 void basic_system_executor<Blocking, Relationship, Allocator>::do_execute(
96 BOOST_ASIO_MOVE_ARG(Function) f, execution::blocking_t::never_t) const
98 system_context& ctx = detail::global<system_context>();
100 // Allocate and construct an operation to wrap the function.
101 typedef typename decay<Function>::type function_type;
102 typedef detail::executor_op<function_type, Allocator> op;
103 typename op::ptr p = { detail::addressof(allocator_),
104 op::ptr::allocate(allocator_), 0 };
105 p.p = new (p.v) op(BOOST_ASIO_MOVE_CAST(Function)(f), allocator_);
107 if (is_same<Relationship, execution::relationship_t::continuation_t>::value)
109 BOOST_ASIO_HANDLER_CREATION((ctx, *p.p,
110 "system_executor", &ctx, 0, "execute(blk=never,rel=cont)"));
114 BOOST_ASIO_HANDLER_CREATION((ctx, *p.p,
115 "system_executor", &ctx, 0, "execute(blk=never,rel=fork)"));
118 ctx.scheduler_.post_immediate_completion(p.p,
119 is_same<Relationship, execution::relationship_t::continuation_t>::value);
123 #if !defined(BOOST_ASIO_NO_TS_EXECUTORS)
124 template <typename Blocking, typename Relationship, typename Allocator>
125 inline system_context& basic_system_executor<
126 Blocking, Relationship, Allocator>::context() const BOOST_ASIO_NOEXCEPT
128 return detail::global<system_context>();
131 template <typename Blocking, typename Relationship, typename Allocator>
132 template <typename Function, typename OtherAllocator>
133 void basic_system_executor<Blocking, Relationship, Allocator>::dispatch(
134 BOOST_ASIO_MOVE_ARG(Function) f, const OtherAllocator&) const
136 typename decay<Function>::type tmp(BOOST_ASIO_MOVE_CAST(Function)(f));
137 boost_asio_handler_invoke_helpers::invoke(tmp, tmp);
140 template <typename Blocking, typename Relationship, typename Allocator>
141 template <typename Function, typename OtherAllocator>
142 void basic_system_executor<Blocking, Relationship, Allocator>::post(
143 BOOST_ASIO_MOVE_ARG(Function) f, const OtherAllocator& a) const
145 typedef typename decay<Function>::type function_type;
147 system_context& ctx = detail::global<system_context>();
149 // Allocate and construct an operation to wrap the function.
150 typedef detail::executor_op<function_type, OtherAllocator> op;
151 typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
152 p.p = new (p.v) op(BOOST_ASIO_MOVE_CAST(Function)(f), a);
154 BOOST_ASIO_HANDLER_CREATION((ctx, *p.p,
155 "system_executor", &this->context(), 0, "post"));
157 ctx.scheduler_.post_immediate_completion(p.p, false);
161 template <typename Blocking, typename Relationship, typename Allocator>
162 template <typename Function, typename OtherAllocator>
163 void basic_system_executor<Blocking, Relationship, Allocator>::defer(
164 BOOST_ASIO_MOVE_ARG(Function) f, const OtherAllocator& a) const
166 typedef typename decay<Function>::type function_type;
168 system_context& ctx = detail::global<system_context>();
170 // Allocate and construct an operation to wrap the function.
171 typedef detail::executor_op<function_type, OtherAllocator> op;
172 typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
173 p.p = new (p.v) op(BOOST_ASIO_MOVE_CAST(Function)(f), a);
175 BOOST_ASIO_HANDLER_CREATION((ctx, *p.p,
176 "system_executor", &this->context(), 0, "defer"));
178 ctx.scheduler_.post_immediate_completion(p.p, true);
181 #endif // !defined(BOOST_ASIO_NO_TS_EXECUTORS)
186 #include <boost/asio/detail/pop_options.hpp>
188 #endif // BOOST_ASIO_IMPL_SYSTEM_EXECUTOR_HPP