]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/context/continuation_fcontext.hpp
update sources to v12.2.3
[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& e) {
82 t = { e.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 = 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 std::swap( fctx_, other.fctx_);
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 BOOST_ASSERT( nullptr != fctx_);
257 return detail::jump_fcontext(
258 #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
259 detail::exchange( fctx_, nullptr),
260 #else
261 std::exchange( fctx_, nullptr),
262 #endif
263 nullptr).fctx;
264 }
265
266 template< typename Fn >
267 continuation resume_with( Fn && fn) {
268 BOOST_ASSERT( nullptr != fctx_);
269 auto p = std::make_tuple( std::forward< Fn >( fn) );
270 return detail::ontop_fcontext(
271 #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
272 detail::exchange( fctx_, nullptr),
273 #else
274 std::exchange( fctx_, nullptr),
275 #endif
276 & p,
277 detail::context_ontop< continuation, Fn >).fctx;
278 }
279
280 explicit operator bool() const noexcept {
281 return nullptr != fctx_;
282 }
283
284 bool operator!() const noexcept {
285 return nullptr == fctx_;
286 }
287
288 bool operator==( continuation const& other) const noexcept {
289 return fctx_ == other.fctx_;
290 }
291
292 bool operator!=( continuation const& other) const noexcept {
293 return fctx_ != other.fctx_;
294 }
295
296 bool operator<( continuation const& other) const noexcept {
297 return fctx_ < other.fctx_;
298 }
299
300 bool operator>( continuation const& other) const noexcept {
301 return other.fctx_ < fctx_;
302 }
303
304 bool operator<=( continuation const& other) const noexcept {
305 return ! ( * this > other);
306 }
307
308 bool operator>=( continuation const& other) const noexcept {
309 return ! ( * this < other);
310 }
311
312 template< typename charT, class traitsT >
313 friend std::basic_ostream< charT, traitsT > &
314 operator<<( std::basic_ostream< charT, traitsT > & os, continuation const& other) {
315 if ( nullptr != other.fctx_) {
316 return os << other.fctx_;
317 } else {
318 return os << "{not-a-context}";
319 }
320 }
321
322 void swap( continuation & other) noexcept {
323 std::swap( fctx_, other.fctx_);
324 }
325 };
326
327 template<
328 typename Fn,
329 typename = detail::disable_overload< continuation, Fn >
330 >
331 continuation
332 callcc( Fn && fn) {
333 return callcc(
334 std::allocator_arg, fixedsize_stack(),
335 std::forward< Fn >( fn) );
336 }
337
338 template< typename StackAlloc, typename Fn >
339 continuation
340 callcc( std::allocator_arg_t, StackAlloc && salloc, Fn && fn) {
341 using Record = detail::record< continuation, StackAlloc, Fn >;
342 return continuation{
343 detail::create_context1< Record >(
344 std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) ) }.resume();
345 }
346
347 template< typename StackAlloc, typename Fn >
348 continuation
349 callcc( std::allocator_arg_t, preallocated palloc, StackAlloc && salloc, Fn && fn) {
350 using Record = detail::record< continuation, StackAlloc, Fn >;
351 return continuation{
352 detail::create_context2< Record >(
353 palloc, std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) ) }.resume();
354 }
355
356 #if defined(BOOST_USE_SEGMENTED_STACKS)
357 template< typename Fn >
358 continuation
359 callcc( std::allocator_arg_t, segmented_stack, Fn &&);
360
361 template< typename StackAlloc, typename Fn >
362 continuation
363 callcc( std::allocator_arg_t, preallocated, segmented_stack, Fn &&);
364 #endif
365
366 inline
367 void swap( continuation & l, continuation & r) noexcept {
368 l.swap( r);
369 }
370
371 }}
372
373 #if defined(BOOST_MSVC)
374 # pragma warning(pop)
375 #endif
376
377 #ifdef BOOST_HAS_ABI_HEADERS
378 # include BOOST_ABI_SUFFIX
379 #endif
380
381 #endif // BOOST_CONTEXT_CONTINUATION_H