]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/context/continuation_fcontext.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / boost / context / continuation_fcontext.hpp
1
2 // Copyright Oliver Kowalke 2017.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6
7 #ifndef BOOST_CONTEXT_CONTINUATION_H
8 #define BOOST_CONTEXT_CONTINUATION_H
9
10 #include <boost/context/detail/config.hpp>
11
12 #include <algorithm>
13 #include <cstddef>
14 #include <cstdint>
15 #include <cstdlib>
16 #include <exception>
17 #include <functional>
18 #include <memory>
19 #include <ostream>
20 #include <tuple>
21 #include <utility>
22
23 #include <boost/assert.hpp>
24 #include <boost/config.hpp>
25 #include <boost/intrusive_ptr.hpp>
26
27 #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
28 #include <boost/context/detail/exchange.hpp>
29 #endif
30 #if defined(BOOST_NO_CXX17_STD_INVOKE)
31 #include <boost/context/detail/invoke.hpp>
32 #endif
33 #include <boost/context/detail/disable_overload.hpp>
34 #include <boost/context/detail/exception.hpp>
35 #include <boost/context/detail/fcontext.hpp>
36 #include <boost/context/detail/tuple.hpp>
37 #include <boost/context/fixedsize_stack.hpp>
38 #include <boost/context/flags.hpp>
39 #include <boost/context/preallocated.hpp>
40 #include <boost/context/segmented_stack.hpp>
41 #include <boost/context/stack_context.hpp>
42
43 #ifdef BOOST_HAS_ABI_HEADERS
44 # include BOOST_ABI_PREFIX
45 #endif
46
47 #if defined(BOOST_MSVC)
48 # pragma warning(push)
49 # pragma warning(disable: 4702)
50 #endif
51
52 namespace boost {
53 namespace context {
54 namespace detail {
55
56 inline
57 transfer_t context_unwind( transfer_t t) {
58 throw forced_unwind( t.fctx);
59 return { nullptr, nullptr };
60 }
61
62 template< typename Rec >
63 transfer_t context_exit( transfer_t t) noexcept {
64 Rec * rec = static_cast< Rec * >( t.data);
65 // destroy context stack
66 rec->deallocate();
67 return { nullptr, nullptr };
68 }
69
70 template< typename Rec >
71 void context_entry( transfer_t t) noexcept {
72 // transfer control structure to the context-stack
73 Rec * rec = static_cast< Rec * >( t.data);
74 BOOST_ASSERT( nullptr != t.fctx);
75 BOOST_ASSERT( nullptr != rec);
76 try {
77 // jump back to `create_context()`
78 t = jump_fcontext( t.fctx, nullptr);
79 // start executing
80 t.fctx = rec->run( t.fctx);
81 } catch ( forced_unwind const& ex) {
82 t = { ex.fctx, nullptr };
83 }
84 BOOST_ASSERT( nullptr != t.fctx);
85 // destroy context-stack of `this`context on next context
86 ontop_fcontext( t.fctx, rec, context_exit< Rec >);
87 BOOST_ASSERT_MSG( false, "context already terminated");
88 }
89
90 template< typename Ctx, typename Fn >
91 transfer_t context_ontop( transfer_t t) {
92 auto p = static_cast< std::tuple< Fn > * >( t.data);
93 BOOST_ASSERT( nullptr != p);
94 typename std::decay< Fn >::type fn = std::get< 0 >( * p);
95 t.data = nullptr;
96 Ctx c{ t.fctx };
97 // execute function, pass continuation via reference
98 c = fn( std::move( c) );
99 #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
100 return { exchange( c.fctx_, nullptr), nullptr };
101 #else
102 return { std::exchange( c.fctx_, nullptr), nullptr };
103 #endif
104 }
105
106 template< typename Ctx, typename StackAlloc, typename Fn >
107 class record {
108 private:
109 stack_context sctx_;
110 typename std::decay< StackAlloc >::type salloc_;
111 typename std::decay< Fn >::type fn_;
112
113 static void destroy( record * p) noexcept {
114 typename std::decay< StackAlloc >::type salloc = std::move( p->salloc_);
115 stack_context sctx = p->sctx_;
116 // deallocate record
117 p->~record();
118 // destroy stack with stack allocator
119 salloc.deallocate( sctx);
120 }
121
122 public:
123 record( stack_context sctx, StackAlloc && salloc,
124 Fn && fn) noexcept :
125 sctx_( sctx),
126 salloc_( std::forward< StackAlloc >( salloc)),
127 fn_( std::forward< Fn >( fn) ) {
128 }
129
130 record( record const&) = delete;
131 record & operator=( record const&) = delete;
132
133 void deallocate() noexcept {
134 destroy( this);
135 }
136
137 fcontext_t run( fcontext_t fctx) {
138 Ctx c{ fctx };
139 // invoke context-function
140 #if defined(BOOST_NO_CXX17_STD_INVOKE)
141 c = boost::context::detail::invoke( fn_, std::move( c) );
142 #else
143 c = std::invoke( fn_, std::move( c) );
144 #endif
145 #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
146 return exchange( c.fctx_, nullptr);
147 #else
148 return std::exchange( c.fctx_, nullptr);
149 #endif
150 }
151 };
152
153 template< typename Record, typename StackAlloc, typename Fn >
154 fcontext_t create_context1( StackAlloc && salloc, Fn && fn) {
155 auto sctx = salloc.allocate();
156 // reserve space for control structure
157 void * storage = reinterpret_cast< void * >(
158 ( reinterpret_cast< uintptr_t >( sctx.sp) - static_cast< uintptr_t >( sizeof( Record) ) )
159 & ~static_cast< uintptr_t >( 0xff) );
160 // placment new for control structure on context stack
161 Record * record = new ( storage) Record{
162 sctx, std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) };
163 // 64byte gab between control structure and stack top
164 // should be 16byte aligned
165 void * stack_top = reinterpret_cast< void * >(
166 reinterpret_cast< uintptr_t >( storage) - static_cast< uintptr_t >( 64) );
167 void * stack_bottom = reinterpret_cast< void * >(
168 reinterpret_cast< uintptr_t >( sctx.sp) - static_cast< uintptr_t >( sctx.size) );
169 // create fast-context
170 const std::size_t size = reinterpret_cast< uintptr_t >( stack_top) - reinterpret_cast< uintptr_t >( stack_bottom);
171 const fcontext_t fctx = make_fcontext( stack_top, size, & context_entry< Record >);
172 BOOST_ASSERT( nullptr != fctx);
173 // transfer control structure to context-stack
174 return jump_fcontext( fctx, record).fctx;
175 }
176
177 template< typename Record, typename StackAlloc, typename Fn >
178 fcontext_t create_context2( preallocated palloc, StackAlloc && salloc, Fn && fn) {
179 // reserve space for control structure
180 void * storage = reinterpret_cast< void * >(
181 ( reinterpret_cast< uintptr_t >( palloc.sp) - static_cast< uintptr_t >( sizeof( Record) ) )
182 & ~ static_cast< uintptr_t >( 0xff) );
183 // placment new for control structure on context-stack
184 Record * record = new ( storage) Record{
185 palloc.sctx, std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) };
186 // 64byte gab between control structure and stack top
187 void * stack_top = reinterpret_cast< void * >(
188 reinterpret_cast< uintptr_t >( storage) - static_cast< uintptr_t >( 64) );
189 void * stack_bottom = reinterpret_cast< void * >(
190 reinterpret_cast< uintptr_t >( palloc.sctx.sp) - static_cast< uintptr_t >( palloc.sctx.size) );
191 // create fast-context
192 const std::size_t size = reinterpret_cast< uintptr_t >( stack_top) - reinterpret_cast< uintptr_t >( stack_bottom);
193 const fcontext_t fctx = make_fcontext( stack_top, size, & context_entry< Record >);
194 BOOST_ASSERT( nullptr != fctx);
195 // transfer control structure to context-stack
196 return jump_fcontext( fctx, record).fctx;
197 }
198
199 }
200
201 class continuation {
202 private:
203 template< typename Ctx, typename StackAlloc, typename Fn >
204 friend class detail::record;
205
206 template< typename Ctx, typename Fn >
207 friend detail::transfer_t
208 detail::context_ontop( detail::transfer_t);
209
210 template< typename StackAlloc, typename Fn >
211 friend continuation
212 callcc( std::allocator_arg_t, StackAlloc &&, Fn &&);
213
214 template< typename StackAlloc, typename Fn >
215 friend continuation
216 callcc( std::allocator_arg_t, preallocated, StackAlloc &&, Fn &&);
217
218 detail::fcontext_t fctx_{ nullptr };
219
220 continuation( detail::fcontext_t fctx) noexcept :
221 fctx_{ fctx } {
222 }
223
224 public:
225 continuation() noexcept = default;
226
227 ~continuation() {
228 if ( BOOST_UNLIKELY( nullptr != fctx_) ) {
229 detail::ontop_fcontext(
230 #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
231 detail::exchange( fctx_, nullptr),
232 #else
233 std::exchange( fctx_, nullptr),
234 #endif
235 nullptr,
236 detail::context_unwind);
237 }
238 }
239
240 continuation( continuation && other) noexcept {
241 swap( other);
242 }
243
244 continuation & operator=( continuation && other) noexcept {
245 if ( BOOST_LIKELY( this != & other) ) {
246 continuation tmp = std::move( other);
247 swap( tmp);
248 }
249 return * this;
250 }
251
252 continuation( continuation const& other) noexcept = delete;
253 continuation & operator=( continuation const& other) noexcept = delete;
254
255 continuation resume() & {
256 return std::move( * this).resume();
257 }
258
259 continuation resume() && {
260 BOOST_ASSERT( nullptr != fctx_);
261 return { detail::jump_fcontext(
262 #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
263 detail::exchange( fctx_, nullptr),
264 #else
265 std::exchange( fctx_, nullptr),
266 #endif
267 nullptr).fctx };
268 }
269
270 template< typename Fn >
271 continuation resume_with( Fn && fn) & {
272 return std::move( * this).resume_with( std::forward< Fn >( fn) );
273 }
274
275 template< typename Fn >
276 continuation resume_with( Fn && fn) && {
277 BOOST_ASSERT( nullptr != fctx_);
278 auto p = std::make_tuple( std::forward< Fn >( fn) );
279 return { detail::ontop_fcontext(
280 #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
281 detail::exchange( fctx_, nullptr),
282 #else
283 std::exchange( fctx_, nullptr),
284 #endif
285 & p,
286 detail::context_ontop< continuation, Fn >).fctx };
287 }
288
289 explicit operator bool() const noexcept {
290 return nullptr != fctx_;
291 }
292
293 bool operator!() const noexcept {
294 return nullptr == fctx_;
295 }
296
297 bool operator<( continuation const& other) const noexcept {
298 return fctx_ < other.fctx_;
299 }
300
301 template< typename charT, class traitsT >
302 friend std::basic_ostream< charT, traitsT > &
303 operator<<( std::basic_ostream< charT, traitsT > & os, continuation const& other) {
304 if ( nullptr != other.fctx_) {
305 return os << other.fctx_;
306 } else {
307 return os << "{not-a-context}";
308 }
309 }
310
311 void swap( continuation & other) noexcept {
312 std::swap( fctx_, other.fctx_);
313 }
314 };
315
316 template<
317 typename Fn,
318 typename = detail::disable_overload< continuation, Fn >
319 >
320 continuation
321 callcc( Fn && fn) {
322 return callcc(
323 std::allocator_arg, fixedsize_stack(),
324 std::forward< Fn >( fn) );
325 }
326
327 template< typename StackAlloc, typename Fn >
328 continuation
329 callcc( std::allocator_arg_t, StackAlloc && salloc, Fn && fn) {
330 using Record = detail::record< continuation, StackAlloc, Fn >;
331 return continuation{
332 detail::create_context1< Record >(
333 std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) ) }.resume();
334 }
335
336 template< typename StackAlloc, typename Fn >
337 continuation
338 callcc( std::allocator_arg_t, preallocated palloc, StackAlloc && salloc, Fn && fn) {
339 using Record = detail::record< continuation, StackAlloc, Fn >;
340 return continuation{
341 detail::create_context2< Record >(
342 palloc, std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) ) }.resume();
343 }
344
345 #if defined(BOOST_USE_SEGMENTED_STACKS)
346 template< typename Fn >
347 continuation
348 callcc( std::allocator_arg_t, segmented_stack, Fn &&);
349
350 template< typename StackAlloc, typename Fn >
351 continuation
352 callcc( std::allocator_arg_t, preallocated, segmented_stack, Fn &&);
353 #endif
354
355 inline
356 void swap( continuation & l, continuation & r) noexcept {
357 l.swap( r);
358 }
359
360 }}
361
362 #if defined(BOOST_MSVC)
363 # pragma warning(pop)
364 #endif
365
366 #ifdef BOOST_HAS_ABI_HEADERS
367 # include BOOST_ABI_SUFFIX
368 #endif
369
370 #endif // BOOST_CONTEXT_CONTINUATION_H