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)
7 #ifndef BOOST_CONTEXT_CONTINUATION_H
8 #define BOOST_CONTEXT_CONTINUATION_H
10 #include <boost/predef.h>
12 #define _XOPEN_SOURCE 600
19 #include <boost/context/detail/config.hpp>
29 #include <system_error>
33 #include <boost/assert.hpp>
34 #include <boost/config.hpp>
36 #include <boost/context/detail/disable_overload.hpp>
37 #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
38 #include <boost/context/detail/exchange.hpp>
40 #include <boost/context/detail/externc.hpp>
41 #if defined(BOOST_NO_CXX17_STD_INVOKE)
42 #include <boost/context/detail/invoke.hpp>
44 #include <boost/context/fixedsize_stack.hpp>
45 #include <boost/context/flags.hpp>
46 #include <boost/context/preallocated.hpp>
47 #if defined(BOOST_USE_SEGMENTED_STACKS)
48 #include <boost/context/segmented_stack.hpp>
50 #include <boost/context/stack_context.hpp>
52 #ifdef BOOST_HAS_ABI_HEADERS
53 # include BOOST_ABI_PREFIX
61 // entered if the execution context
62 // is resumed for the first time
63 template< typename Record >
64 static void entry_func( void * data) noexcept {
65 Record * record = static_cast< Record * >( data);
66 BOOST_ASSERT( nullptr != record);
67 // start execution of toplevel context-function
71 struct BOOST_CONTEXT_DECL activation_record {
74 bool main_ctx{ true };
75 activation_record * from{ nullptr };
76 std::function< activation_record*(activation_record*&) > ontop{};
77 bool terminated{ false };
78 bool force_unwind{ false };
79 #if defined(BOOST_USE_ASAN)
80 void * fake_stack{ nullptr };
81 void * stack_bottom{ nullptr };
82 std::size_t stack_size{ 0 };
85 static activation_record *& current() noexcept;
87 // used for toplevel-context
88 // (e.g. main context, thread-entry context)
90 if ( BOOST_UNLIKELY( 0 != ::getcontext( & uctx) ) ) {
91 throw std::system_error(
92 std::error_code( errno, std::system_category() ),
93 "getcontext() failed");
97 activation_record( stack_context sctx_) noexcept :
102 virtual ~activation_record() {
105 activation_record( activation_record const&) = delete;
106 activation_record & operator=( activation_record const&) = delete;
108 bool is_main_context() const noexcept {
112 activation_record * resume() {
114 // store `this` in static, thread local pointer
115 // `this` will become the active (running) context
117 #if defined(BOOST_USE_SEGMENTED_STACKS)
118 // adjust segmented stack properties
119 __splitstack_getcontext( from->sctx.segments_ctx);
120 __splitstack_setcontext( sctx.segments_ctx);
122 #if defined(BOOST_USE_ASAN)
124 __sanitizer_start_switch_fiber( nullptr, stack_bottom, stack_size);
126 __sanitizer_start_switch_fiber( & from->fake_stack, stack_bottom, stack_size);
129 // context switch from parent context to `this`-context
130 ::swapcontext( & from->uctx, & uctx);
131 #if defined(BOOST_USE_ASAN)
132 __sanitizer_finish_switch_fiber( current()->fake_stack,
133 (const void **) & current()->from->stack_bottom,
134 & current()->from->stack_size);
136 #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
137 return exchange( current()->from, nullptr);
139 return std::exchange( current()->from, nullptr);
143 template< typename Ctx, typename Fn >
144 activation_record * resume_with( Fn && fn) {
146 // store `this` in static, thread local pointer
147 // `this` will become the active (running) context
148 // returned by continuation::current()
150 #if defined(BOOST_NO_CXX14_GENERIC_LAMBDAS)
151 current()->ontop = std::bind(
152 [](typename std::decay< Fn >::type & fn, activation_record *& ptr){
154 c = fn( std::move( c) );
158 #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
159 return exchange( c.ptr_, nullptr);
161 return std::exchange( c.ptr_, nullptr);
164 std::forward< Fn >( fn),
165 std::placeholders::_1);
167 current()->ontop = [fn=std::forward<Fn>(fn)](activation_record *& ptr){
169 c = fn( std::move( c) );
173 #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
174 return exchange( c.ptr_, nullptr);
176 return std::exchange( c.ptr_, nullptr);
180 #if defined(BOOST_USE_SEGMENTED_STACKS)
181 // adjust segmented stack properties
182 __splitstack_getcontext( from->sctx.segments_ctx);
183 __splitstack_setcontext( sctx.segments_ctx);
185 #if defined(BOOST_USE_ASAN)
186 __sanitizer_start_switch_fiber( & from->fake_stack, stack_bottom, stack_size);
188 // context switch from parent context to `this`-context
189 ::swapcontext( & from->uctx, & uctx);
190 #if defined(BOOST_USE_ASAN)
191 __sanitizer_finish_switch_fiber( current()->fake_stack,
192 (const void **) & current()->from->stack_bottom,
193 & current()->from->stack_size);
195 #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
196 return exchange( current()->from, nullptr);
198 return std::exchange( current()->from, nullptr);
202 virtual void deallocate() noexcept {
206 struct BOOST_CONTEXT_DECL activation_record_initializer {
207 activation_record_initializer() noexcept;
208 ~activation_record_initializer();
211 struct forced_unwind {
212 activation_record * from{ nullptr };
214 forced_unwind( activation_record * from_) noexcept :
219 template< typename Ctx, typename StackAlloc, typename Fn >
220 class capture_record : public activation_record {
222 typename std::decay< StackAlloc >::type salloc_;
223 typename std::decay< Fn >::type fn_;
225 static void destroy( capture_record * p) noexcept {
226 typename std::decay< StackAlloc >::type salloc = std::move( p->salloc_);
227 stack_context sctx = p->sctx;
228 // deallocate activation record
229 p->~capture_record();
230 // destroy stack with stack allocator
231 salloc.deallocate( sctx);
235 capture_record( stack_context sctx, StackAlloc && salloc, Fn && fn) noexcept :
236 activation_record{ sctx },
237 salloc_{ std::forward< StackAlloc >( salloc) },
238 fn_( std::forward< Fn >( fn) ) {
241 void deallocate() noexcept override final {
242 BOOST_ASSERT( main_ctx || ( ! main_ctx && terminated) );
247 #if defined(BOOST_USE_ASAN)
248 __sanitizer_finish_switch_fiber( fake_stack,
249 (const void **) & from->stack_bottom,
254 // invoke context-function
255 #if defined(BOOST_NO_CXX17_STD_INVOKE)
256 c = boost::context::detail::invoke( fn_, std::move( c) );
258 c = std::invoke( fn_, std::move( c) );
260 } catch ( forced_unwind const& ex) {
263 // this context has finished its task
267 force_unwind = false;
269 BOOST_ASSERT_MSG( false, "continuation already terminated");
273 template< typename Ctx, typename StackAlloc, typename Fn >
274 static activation_record * create_context1( StackAlloc && salloc, Fn && fn) {
275 typedef capture_record< Ctx, StackAlloc, Fn > capture_t;
277 auto sctx = salloc.allocate();
278 // reserve space for control structure
279 void * storage = reinterpret_cast< void * >(
280 ( reinterpret_cast< uintptr_t >( sctx.sp) - static_cast< uintptr_t >( sizeof( capture_t) ) )
281 & ~ static_cast< uintptr_t >( 0xff) );
282 // placment new for control structure on context stack
283 capture_t * record = new ( storage) capture_t{
284 sctx, std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) };
286 void * stack_bottom = reinterpret_cast< void * >(
287 reinterpret_cast< uintptr_t >( sctx.sp) - static_cast< uintptr_t >( sctx.size) );
288 // create user-context
289 if ( BOOST_UNLIKELY( 0 != ::getcontext( & record->uctx) ) ) {
290 throw std::system_error(
291 std::error_code( errno, std::system_category() ),
292 "getcontext() failed");
294 record->uctx.uc_stack.ss_sp = stack_bottom;
295 // 64byte gap between control structure and stack top
296 record->uctx.uc_stack.ss_size = reinterpret_cast< uintptr_t >( storage) -
297 reinterpret_cast< uintptr_t >( stack_bottom) - static_cast< uintptr_t >( 64);
298 record->uctx.uc_link = nullptr;
299 ::makecontext( & record->uctx, ( void (*)() ) & entry_func< capture_t >, 1, record);
300 #if defined(BOOST_USE_ASAN)
301 record->stack_bottom = record->uctx.uc_stack.ss_sp;
302 record->stack_size = record->uctx.uc_stack.ss_size;
307 template< typename Ctx, typename StackAlloc, typename Fn >
308 static activation_record * create_context2( preallocated palloc, StackAlloc && salloc, Fn && fn) {
309 typedef capture_record< Ctx, StackAlloc, Fn > capture_t;
311 // reserve space for control structure
312 void * storage = reinterpret_cast< void * >(
313 ( reinterpret_cast< uintptr_t >( palloc.sp) - static_cast< uintptr_t >( sizeof( capture_t) ) )
314 & ~ static_cast< uintptr_t >( 0xff) );
315 // placment new for control structure on context stack
316 capture_t * record = new ( storage) capture_t{
317 palloc.sctx, std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) };
319 void * stack_bottom = reinterpret_cast< void * >(
320 reinterpret_cast< uintptr_t >( palloc.sctx.sp) - static_cast< uintptr_t >( palloc.sctx.size) );
321 // create user-context
322 if ( BOOST_UNLIKELY( 0 != ::getcontext( & record->uctx) ) ) {
323 throw std::system_error(
324 std::error_code( errno, std::system_category() ),
325 "getcontext() failed");
327 record->uctx.uc_stack.ss_sp = stack_bottom;
328 // 64byte gap between control structure and stack top
329 record->uctx.uc_stack.ss_size = reinterpret_cast< uintptr_t >( storage) -
330 reinterpret_cast< uintptr_t >( stack_bottom) - static_cast< uintptr_t >( 64);
331 record->uctx.uc_link = nullptr;
332 ::makecontext( & record->uctx, ( void (*)() ) & entry_func< capture_t >, 1, record);
333 #if defined(BOOST_USE_ASAN)
334 record->stack_bottom = record->uctx.uc_stack.ss_sp;
335 record->stack_size = record->uctx.uc_stack.ss_size;
342 class BOOST_CONTEXT_DECL continuation {
344 friend struct detail::activation_record;
346 template< typename Ctx, typename StackAlloc, typename Fn >
347 friend class detail::capture_record;
349 template< typename Ctx, typename StackAlloc, typename Fn >
350 friend detail::activation_record * detail::create_context1( StackAlloc &&, Fn &&);
352 template< typename Ctx, typename StackAlloc, typename Fn >
353 friend detail::activation_record * detail::create_context2( preallocated, StackAlloc &&, Fn &&);
355 template< typename StackAlloc, typename Fn >
357 callcc( std::allocator_arg_t, StackAlloc &&, Fn &&);
359 template< typename StackAlloc, typename Fn >
361 callcc( std::allocator_arg_t, preallocated, StackAlloc &&, Fn &&);
363 detail::activation_record * ptr_{ nullptr };
365 continuation( detail::activation_record * ptr) noexcept :
370 continuation() = default;
373 if ( BOOST_UNLIKELY( nullptr != ptr_) && ! ptr_->main_ctx) {
374 if ( BOOST_LIKELY( ! ptr_->terminated) ) {
375 ptr_->force_unwind = true;
377 BOOST_ASSERT( ptr_->terminated);
383 continuation( continuation const&) = delete;
384 continuation & operator=( continuation const&) = delete;
386 continuation( continuation && other) noexcept {
390 continuation & operator=( continuation && other) noexcept {
391 if ( BOOST_LIKELY( this != & other) ) {
392 continuation tmp = std::move( other);
398 continuation resume() & {
399 return std::move( * this).resume();
402 continuation resume() && {
403 #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
404 detail::activation_record * ptr = detail::exchange( ptr_, nullptr)->resume();
406 detail::activation_record * ptr = std::exchange( ptr_, nullptr)->resume();
408 if ( BOOST_UNLIKELY( detail::activation_record::current()->force_unwind) ) {
409 throw detail::forced_unwind{ ptr};
410 } else if ( BOOST_UNLIKELY( nullptr != detail::activation_record::current()->ontop) ) {
411 ptr = detail::activation_record::current()->ontop( ptr);
412 detail::activation_record::current()->ontop = nullptr;
417 template< typename Fn >
418 continuation resume_with( Fn && fn) & {
419 return std::move( * this).resume_with( std::forward< Fn >( fn) );
422 template< typename Fn >
423 continuation resume_with( Fn && fn) && {
424 #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
425 detail::activation_record * ptr =
426 detail::exchange( ptr_, nullptr)->resume_with< continuation >( std::forward< Fn >( fn) );
428 detail::activation_record * ptr =
429 std::exchange( ptr_, nullptr)->resume_with< continuation >( std::forward< Fn >( fn) );
431 if ( BOOST_UNLIKELY( detail::activation_record::current()->force_unwind) ) {
432 throw detail::forced_unwind{ ptr};
433 } else if ( BOOST_UNLIKELY( nullptr != detail::activation_record::current()->ontop) ) {
434 ptr = detail::activation_record::current()->ontop( ptr);
435 detail::activation_record::current()->ontop = nullptr;
440 explicit operator bool() const noexcept {
441 return nullptr != ptr_ && ! ptr_->terminated;
444 bool operator!() const noexcept {
445 return nullptr == ptr_ || ptr_->terminated;
448 bool operator==( continuation const& other) const noexcept {
449 return ptr_ == other.ptr_;
452 bool operator!=( continuation const& other) const noexcept {
453 return ptr_ != other.ptr_;
456 bool operator<( continuation const& other) const noexcept {
457 return ptr_ < other.ptr_;
460 bool operator>( continuation const& other) const noexcept {
461 return other.ptr_ < ptr_;
464 bool operator<=( continuation const& other) const noexcept {
465 return ! ( * this > other);
468 bool operator>=( continuation const& other) const noexcept {
469 return ! ( * this < other);
472 template< typename charT, class traitsT >
473 friend std::basic_ostream< charT, traitsT > &
474 operator<<( std::basic_ostream< charT, traitsT > & os, continuation const& other) {
475 if ( nullptr != other.ptr_) {
476 return os << other.ptr_;
478 return os << "{not-a-context}";
482 void swap( continuation & other) noexcept {
483 std::swap( ptr_, other.ptr_);
489 typename = detail::disable_overload< continuation, Fn >
495 #if defined(BOOST_USE_SEGMENTED_STACKS)
500 std::forward< Fn >( fn) );
503 template< typename StackAlloc, typename Fn >
505 callcc( std::allocator_arg_t, StackAlloc && salloc, Fn && fn) {
507 detail::create_context1< continuation >(
508 std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) ) }.resume();
511 template< typename StackAlloc, typename Fn >
513 callcc( std::allocator_arg_t, preallocated palloc, StackAlloc && salloc, Fn && fn) {
515 detail::create_context2< continuation >(
516 palloc, std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) ) }.resume();
520 void swap( continuation & l, continuation & r) noexcept {
526 #ifdef BOOST_HAS_ABI_HEADERS
527 # include BOOST_ABI_SUFFIX
530 #endif // BOOST_CONTEXT_CONTINUATION_H