]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/asio/impl/system_executor.hpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / boost / asio / impl / system_executor.hpp
1 //
2 // impl/system_executor.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2020 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 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
33 {
34 return detail::global<system_context>();
35 }
36
37 template <typename Blocking, typename Relationship, typename Allocator>
38 inline std::size_t
39 basic_system_executor<Blocking, Relationship, Allocator>::query(
40 execution::occupancy_t) const BOOST_ASIO_NOEXCEPT
41 {
42 return detail::global<system_context>().num_threads_;
43 }
44
45 template <typename Blocking, typename Relationship, typename Allocator>
46 template <typename Function>
47 inline void
48 basic_system_executor<Blocking, Relationship, Allocator>::do_execute(
49 BOOST_ASIO_MOVE_ARG(Function) f, execution::blocking_t::possibly_t) const
50 {
51 // Obtain a non-const instance of the function.
52 detail::non_const_lvalue<Function> f2(f);
53
54 #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
55 try
56 {
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)
61 }
62 catch (...)
63 {
64 std::terminate();
65 }
66 #endif// !defined(BOOST_ASIO_NO_EXCEPTIONS)
67 }
68
69 template <typename Blocking, typename Relationship, typename Allocator>
70 template <typename Function>
71 inline void
72 basic_system_executor<Blocking, Relationship, Allocator>::do_execute(
73 BOOST_ASIO_MOVE_ARG(Function) f, execution::blocking_t::always_t) const
74 {
75 // Obtain a non-const instance of the function.
76 detail::non_const_lvalue<Function> f2(f);
77
78 #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
79 try
80 {
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)
85 }
86 catch (...)
87 {
88 std::terminate();
89 }
90 #endif// !defined(BOOST_ASIO_NO_EXCEPTIONS)
91 }
92
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
97 {
98 system_context& ctx = detail::global<system_context>();
99
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_);
106
107 if (is_same<Relationship, execution::relationship_t::continuation_t>::value)
108 {
109 BOOST_ASIO_HANDLER_CREATION((ctx, *p.p,
110 "system_executor", &ctx, 0, "execute(blk=never,rel=cont)"));
111 }
112 else
113 {
114 BOOST_ASIO_HANDLER_CREATION((ctx, *p.p,
115 "system_executor", &ctx, 0, "execute(blk=never,rel=fork)"));
116 }
117
118 ctx.scheduler_.post_immediate_completion(p.p,
119 is_same<Relationship, execution::relationship_t::continuation_t>::value);
120 p.v = p.p = 0;
121 }
122
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
127 {
128 return detail::global<system_context>();
129 }
130
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
135 {
136 typename decay<Function>::type tmp(BOOST_ASIO_MOVE_CAST(Function)(f));
137 boost_asio_handler_invoke_helpers::invoke(tmp, tmp);
138 }
139
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
144 {
145 typedef typename decay<Function>::type function_type;
146
147 system_context& ctx = detail::global<system_context>();
148
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);
153
154 BOOST_ASIO_HANDLER_CREATION((ctx, *p.p,
155 "system_executor", &this->context(), 0, "post"));
156
157 ctx.scheduler_.post_immediate_completion(p.p, false);
158 p.v = p.p = 0;
159 }
160
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
165 {
166 typedef typename decay<Function>::type function_type;
167
168 system_context& ctx = detail::global<system_context>();
169
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);
174
175 BOOST_ASIO_HANDLER_CREATION((ctx, *p.p,
176 "system_executor", &this->context(), 0, "defer"));
177
178 ctx.scheduler_.post_immediate_completion(p.p, true);
179 p.v = p.p = 0;
180 }
181 #endif // !defined(BOOST_ASIO_NO_TS_EXECUTORS)
182
183 } // namespace asio
184 } // namespace boost
185
186 #include <boost/asio/detail/pop_options.hpp>
187
188 #endif // BOOST_ASIO_IMPL_SYSTEM_EXECUTOR_HPP