]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/context/execution_context_v1.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / context / execution_context_v1.hpp
1
2 // Copyright Oliver Kowalke 2014.
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_EXECUTION_CONTEXT_H
8 #define BOOST_CONTEXT_EXECUTION_CONTEXT_H
9
10 #include <boost/context/detail/config.hpp>
11
12 #include <algorithm>
13 #include <atomic>
14 #include <cstddef>
15 #include <cstdint>
16 #include <cstdlib>
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_CXX17_STD_APPLY)
28 #include <boost/context/detail/apply.hpp>
29 #endif
30 #include <boost/context/detail/disable_overload.hpp>
31 #include <boost/context/detail/fcontext.hpp>
32 #include <boost/context/fixedsize_stack.hpp>
33 #include <boost/context/flags.hpp>
34 #include <boost/context/preallocated.hpp>
35 #include <boost/context/segmented_stack.hpp>
36 #include <boost/context/stack_context.hpp>
37
38 #ifdef BOOST_HAS_ABI_HEADERS
39 # include BOOST_ABI_PREFIX
40 #endif
41
42 #if defined(BOOST_USE_SEGMENTED_STACKS)
43 extern "C" {
44 void __splitstack_getcontext( void * [BOOST_CONTEXT_SEGMENTS]);
45 void __splitstack_setcontext( void * [BOOST_CONTEXT_SEGMENTS]);
46 }
47 #endif
48
49 namespace boost {
50 namespace context {
51 namespace detail {
52
53 template< typename Fn >
54 transfer_t context_ontop( transfer_t);
55
56 struct activation_record;
57
58 struct data_t {
59 activation_record * from;
60 void * data;
61 };
62
63 struct BOOST_CONTEXT_DECL activation_record {
64 typedef boost::intrusive_ptr< activation_record > ptr_t;
65
66 thread_local static ptr_t current_rec;
67
68 std::atomic< std::size_t > use_count{ 0 };
69 fcontext_t fctx{ nullptr };
70 stack_context sctx{};
71 bool main_ctx{ true };
72
73 // used for toplevel-context
74 // (e.g. main context, thread-entry context)
75 activation_record() = default;
76
77 activation_record( fcontext_t fctx_, stack_context sctx_) noexcept :
78 fctx{ fctx_ },
79 sctx( sctx_ ), // sctx{ sctx_ } - clang-3.6: no viable conversion from 'boost::context::stack_context' to 'std::size_t'
80 main_ctx{ false } {
81 }
82
83 virtual ~activation_record() = default;
84
85 bool is_main_context() const noexcept {
86 return main_ctx;
87 }
88
89 void * resume( void * vp) {
90 // store current activation record in local variable
91 auto from = current_rec.get();
92 // store `this` in static, thread local pointer
93 // `this` will become the active (running) context
94 // returned by execution_context::current()
95 current_rec = this;
96 #if defined(BOOST_USE_SEGMENTED_STACKS)
97 // adjust segmented stack properties
98 __splitstack_getcontext( from->sctx.segments_ctx);
99 __splitstack_setcontext( sctx.segments_ctx);
100 #endif
101 data_t d = { from, vp };
102 // context switch from parent context to `this`-context
103 transfer_t t = jump_fcontext( fctx, & d);
104 data_t * dp = reinterpret_cast< data_t * >( t.data);
105 dp->from->fctx = t.fctx;
106 // parent context resumed
107 return dp->data;
108 }
109
110 template< typename Fn >
111 void * resume_ontop( void * data, Fn && fn) {
112 // store current activation record in local variable
113 activation_record * from = current_rec.get();
114 // store `this` in static, thread local pointer
115 // `this` will become the active (running) context
116 // returned by execution_context::current()
117 current_rec = this;
118 #if defined(BOOST_USE_SEGMENTED_STACKS)
119 // adjust segmented stack properties
120 __splitstack_getcontext( from->sctx.segments_ctx);
121 __splitstack_setcontext( sctx.segments_ctx);
122 #endif
123 std::tuple< void *, Fn > p = std::forward_as_tuple( data, fn);
124 data_t d = { from, & p };
125 // context switch from parent context to `this`-context
126 // execute Fn( Tpl) on top of `this`
127 transfer_t t = ontop_fcontext( fctx, & d, context_ontop< Fn >);
128 data_t * dp = reinterpret_cast< data_t * >( t.data);
129 dp->from->fctx = t.fctx;
130 // parent context resumed
131 return dp->data;
132 }
133
134 virtual void deallocate() noexcept {
135 }
136
137 friend void intrusive_ptr_add_ref( activation_record * ar) noexcept {
138 ++ar->use_count;
139 }
140
141 friend void intrusive_ptr_release( activation_record * ar) noexcept {
142 BOOST_ASSERT( nullptr != ar);
143 if ( 0 == --ar->use_count) {
144 ar->deallocate();
145 }
146 }
147 };
148
149 struct BOOST_CONTEXT_DECL activation_record_initializer {
150 activation_record_initializer() noexcept;
151 ~activation_record_initializer();
152 };
153
154 template< typename Fn >
155 transfer_t context_ontop( transfer_t t) {
156 data_t * dp = reinterpret_cast< data_t * >( t.data);
157 dp->from->fctx = t.fctx;
158 auto tpl = reinterpret_cast< std::tuple< void *, Fn > * >( dp->data);
159 BOOST_ASSERT( nullptr != tpl);
160 auto data = std::get< 0 >( * tpl);
161 typename std::decay< Fn >::type fn = std::forward< Fn >( std::get< 1 >( * tpl) );
162 #if defined(BOOST_NO_CXX17_STD_APPLY)
163 dp->data = apply( fn, std::tie( data) );
164 #else
165 dp->data = std::apply( fn, std::tie( data) );
166 #endif
167 return { t.fctx, dp };
168 }
169
170 template< typename StackAlloc, typename Fn, typename ... Args >
171 class capture_record : public activation_record {
172 private:
173 typename std::decay< StackAlloc >::type salloc_;
174 typename std::decay< Fn >::type fn_;
175 std::tuple< typename std::decay< Args >::type ... > args_;
176 activation_record * caller_;
177
178 static void destroy( capture_record * p) noexcept {
179 typename std::decay< StackAlloc >::type salloc = std::move( p->salloc_);
180 stack_context sctx = p->sctx;
181 // deallocate activation record
182 p->~capture_record();
183 // destroy stack with stack allocator
184 salloc.deallocate( sctx);
185 }
186
187 public:
188 capture_record( stack_context sctx, StackAlloc && salloc,
189 fcontext_t fctx,
190 activation_record * caller,
191 Fn && fn, Args && ... args) noexcept :
192 activation_record{ fctx, sctx },
193 salloc_{ std::forward< StackAlloc >( salloc) },
194 fn_( std::forward< Fn >( fn) ),
195 args_( std::forward< Args >( args) ... ),
196 caller_{ caller } {
197 }
198
199 void deallocate() noexcept override final {
200 destroy( this);
201 }
202
203 void run() {
204 auto data = caller_->resume( nullptr);
205 #if defined(BOOST_NO_CXX17_STD_APPLY)
206 apply( fn_, std::tuple_cat( args_, std::tie( data) ) );
207 #else
208 std::apply( fn_, std::tuple_cat( args_, std::tie( data) ) );
209 #endif
210 BOOST_ASSERT_MSG( ! main_ctx, "main-context does not execute activation-record::run()");
211 }
212 };
213
214 }
215
216 class BOOST_CONTEXT_DECL execution_context {
217 private:
218 // tampoline function
219 // entered if the execution context
220 // is resumed for the first time
221 template< typename AR >
222 static void entry_func( detail::transfer_t t) noexcept {
223 detail::data_t * dp = reinterpret_cast< detail::data_t * >( t.data);
224 AR * ar = static_cast< AR * >( dp->data);
225 BOOST_ASSERT( nullptr != ar);
226 dp->from->fctx = t.fctx;
227 // start execution of toplevel context-function
228 ar->run();
229 }
230
231 typedef boost::intrusive_ptr< detail::activation_record > ptr_t;
232
233 ptr_t ptr_;
234
235 template< typename StackAlloc, typename Fn, typename ... Args >
236 static detail::activation_record * create_context( StackAlloc && salloc,
237 Fn && fn, Args && ... args) {
238 typedef detail::capture_record<
239 StackAlloc, Fn, Args ...
240 > capture_t;
241
242 auto sctx = salloc.allocate();
243 // reserve space for control structure
244 #if defined(BOOST_NO_CXX11_CONSTEXPR) || defined(BOOST_NO_CXX11_STD_ALIGN)
245 const std::size_t size = sctx.size - sizeof( capture_t);
246 void * sp = static_cast< char * >( sctx.sp) - sizeof( capture_t);
247 #else
248 constexpr std::size_t func_alignment = 64; // alignof( capture_t);
249 constexpr std::size_t func_size = sizeof( capture_t);
250 // reserve space on stack
251 void * sp = static_cast< char * >( sctx.sp) - func_size - func_alignment;
252 // align sp pointer
253 std::size_t space = func_size + func_alignment;
254 sp = std::align( func_alignment, func_size, sp, space);
255 BOOST_ASSERT( nullptr != sp);
256 // calculate remaining size
257 const std::size_t size = sctx.size - ( static_cast< char * >( sctx.sp) - static_cast< char * >( sp) );
258 #endif
259 // create fast-context
260 const detail::fcontext_t fctx = detail::make_fcontext( sp, size, & execution_context::entry_func< capture_t >);
261 BOOST_ASSERT( nullptr != fctx);
262 // get current activation record
263 auto curr = execution_context::current().ptr_;
264 // placment new for control structure on fast-context stack
265 return ::new ( sp) capture_t{
266 sctx, std::forward< StackAlloc >( salloc), fctx, curr.get(), std::forward< Fn >( fn), std::forward< Args >( args) ... };
267 }
268
269 template< typename StackAlloc, typename Fn, typename ... Args >
270 static detail::activation_record * create_context( preallocated palloc, StackAlloc && salloc,
271 Fn && fn, Args && ... args) {
272 typedef detail::capture_record<
273 StackAlloc, Fn, Args ...
274 > capture_t;
275
276 // reserve space for control structure
277 #if defined(BOOST_NO_CXX11_CONSTEXPR) || defined(BOOST_NO_CXX11_STD_ALIGN)
278 const std::size_t size = palloc.size - sizeof( capture_t);
279 void * sp = static_cast< char * >( palloc.sp) - sizeof( capture_t);
280 #else
281 constexpr std::size_t func_alignment = 64; // alignof( capture_t);
282 constexpr std::size_t func_size = sizeof( capture_t);
283 // reserve space on stack
284 void * sp = static_cast< char * >( palloc.sp) - func_size - func_alignment;
285 // align sp pointer
286 std::size_t space = func_size + func_alignment;
287 sp = std::align( func_alignment, func_size, sp, space);
288 BOOST_ASSERT( nullptr != sp);
289 // calculate remaining size
290 const std::size_t size = palloc.size - ( static_cast< char * >( palloc.sp) - static_cast< char * >( sp) );
291 #endif
292 // create fast-context
293 const detail::fcontext_t fctx = detail::make_fcontext( sp, size, & execution_context::entry_func< capture_t >);
294 BOOST_ASSERT( nullptr != fctx);
295 // get current activation record
296 auto curr = execution_context::current().ptr_;
297 // placment new for control structure on fast-context stack
298 return ::new ( sp) capture_t{
299 palloc.sctx, std::forward< StackAlloc >( salloc), fctx, curr.get(), std::forward< Fn >( fn), std::forward< Args >( args) ... };
300 }
301
302 execution_context() noexcept :
303 // default constructed with current activation_record
304 ptr_{ detail::activation_record::current_rec } {
305 }
306
307 public:
308 static execution_context current() noexcept;
309
310 #if defined(BOOST_USE_SEGMENTED_STACKS)
311 template< typename Fn,
312 typename ... Args,
313 typename = detail::disable_overload< execution_context, Fn >
314 >
315 execution_context( Fn && fn, Args && ... args) :
316 // deferred execution of fn and its arguments
317 // arguments are stored in std::tuple<>
318 // non-type template parameter pack via std::index_sequence_for<>
319 // preserves the number of arguments
320 // used to extract the function arguments from std::tuple<>
321 ptr_{ create_context( segmented_stack(),
322 std::forward< Fn >( fn),
323 std::forward< Args >( args) ...) } {
324 ptr_->resume( ptr_.get() );
325 }
326
327 template< typename Fn,
328 typename ... Args
329 >
330 execution_context( std::allocator_arg_t, segmented_stack salloc, Fn && fn, Args && ... args) :
331 // deferred execution of fn and its arguments
332 // arguments are stored in std::tuple<>
333 // non-type template parameter pack via std::index_sequence_for<>
334 // preserves the number of arguments
335 // used to extract the function arguments from std::tuple<>
336 ptr_{ create_context( std::forward< StackAlloc >( salloc),
337 std::forward< Fn >( fn),
338 std::forward< Args >( args) ...) } {
339 ptr_->resume( ptr_.get() );
340 }
341
342 template< typename Fn,
343 typename ... Args
344 >
345 execution_context( std::allocator_arg_t, preallocated palloc, segmented_stack salloc, Fn && fn, Args && ... args) :
346 // deferred execution of fn and its arguments
347 // arguments are stored in std::tuple<>
348 // non-type template parameter pack via std::index_sequence_for<>
349 // preserves the number of arguments
350 // used to extract the function arguments from std::tuple<>
351 ptr_{ create_context( palloc, std::forward< StackAlloc >( salloc),
352 std::forward< Fn >( fn),
353 std::forward< Args >( args) ...) } {
354 ptr_->resume( ptr_.get() );
355 }
356 #else
357 template< typename Fn,
358 typename ... Args,
359 typename = detail::disable_overload< execution_context, Fn >
360 >
361 execution_context( Fn && fn, Args && ... args) :
362 // deferred execution of fn and its arguments
363 // arguments are stored in std::tuple<>
364 // non-type template parameter pack via std::index_sequence_for<>
365 // preserves the number of arguments
366 // used to extract the function arguments from std::tuple<>
367 ptr_{ create_context( fixedsize_stack(),
368 std::forward< Fn >( fn),
369 std::forward< Args >( args) ...) } {
370 ptr_->resume( ptr_.get() );
371 }
372
373 template< typename StackAlloc,
374 typename Fn,
375 typename ... Args
376 >
377 execution_context( std::allocator_arg_t, StackAlloc && salloc, Fn && fn, Args && ... args) :
378 // deferred execution of fn and its arguments
379 // arguments are stored in std::tuple<>
380 // non-type template parameter pack via std::index_sequence_for<>
381 // preserves the number of arguments
382 // used to extract the function arguments from std::tuple<>
383 ptr_{ create_context( std::forward< StackAlloc >( salloc),
384 std::forward< Fn >( fn),
385 std::forward< Args >( args) ...) } {
386 ptr_->resume( ptr_.get() );
387 }
388
389 template< typename StackAlloc,
390 typename Fn,
391 typename ... Args
392 >
393 execution_context( std::allocator_arg_t, preallocated palloc, StackAlloc && salloc, Fn && fn, Args && ... args) :
394 // deferred execution of fn and its arguments
395 // arguments are stored in std::tuple<>
396 // non-type template parameter pack via std::index_sequence_for<>
397 // preserves the number of arguments
398 // used to extract the function arguments from std::tuple<>
399 ptr_{ create_context( palloc, std::forward< StackAlloc >( salloc),
400 std::forward< Fn >( fn),
401 std::forward< Args >( args) ...) } {
402 ptr_->resume( ptr_.get() );
403 }
404 #endif
405
406 execution_context( execution_context const& other) noexcept :
407 ptr_{ other.ptr_ } {
408 }
409
410 execution_context( execution_context && other) noexcept :
411 ptr_{ other.ptr_ } {
412 other.ptr_.reset();
413 }
414
415 execution_context & operator=( execution_context const& other) noexcept {
416 // intrusive_ptr<> does not test for self-assignment
417 if ( this == & other) return * this;
418 ptr_ = other.ptr_;
419 return * this;
420 }
421
422 execution_context & operator=( execution_context && other) noexcept {
423 if ( this == & other) return * this;
424 execution_context tmp{ std::move( other) };
425 swap( tmp);
426 return * this;
427 }
428
429 void * operator()( void * vp = nullptr) {
430 return ptr_->resume( vp);
431 }
432
433 template< typename Fn >
434 void * operator()( exec_ontop_arg_t, Fn && fn, void * vp = nullptr) {
435 return ptr_->resume_ontop( vp,
436 std::forward< Fn >( fn) );
437 }
438
439 explicit operator bool() const noexcept {
440 return nullptr != ptr_.get();
441 }
442
443 bool operator!() const noexcept {
444 return nullptr == ptr_.get();
445 }
446
447 bool operator==( execution_context const& other) const noexcept {
448 return ptr_ == other.ptr_;
449 }
450
451 bool operator!=( execution_context const& other) const noexcept {
452 return ptr_ != other.ptr_;
453 }
454
455 bool operator<( execution_context const& other) const noexcept {
456 return ptr_ < other.ptr_;
457 }
458
459 bool operator>( execution_context const& other) const noexcept {
460 return other.ptr_ < ptr_;
461 }
462
463 bool operator<=( execution_context const& other) const noexcept {
464 return ! ( * this > other);
465 }
466
467 bool operator>=( execution_context const& other) const noexcept {
468 return ! ( * this < other);
469 }
470
471 template< typename charT, class traitsT >
472 friend std::basic_ostream< charT, traitsT > &
473 operator<<( std::basic_ostream< charT, traitsT > & os, execution_context const& other) {
474 if ( nullptr != other.ptr_) {
475 return os << other.ptr_;
476 } else {
477 return os << "{not-a-context}";
478 }
479 }
480
481 void swap( execution_context & other) noexcept {
482 ptr_.swap( other.ptr_);
483 }
484 };
485
486 inline
487 void swap( execution_context & l, execution_context & r) noexcept {
488 l.swap( r);
489 }
490
491 }}
492
493 #ifdef BOOST_HAS_ABI_HEADERS
494 # include BOOST_ABI_SUFFIX
495 #endif
496
497 #endif // BOOST_CONTEXT_EXECUTION_CONTEXT_H