From 49716a54d836d13c1f3c8bb95f523cf44a1d98bb Mon Sep 17 00:00:00 2001 From: Oliver Kowalke Date: Thu, 18 Jan 2018 21:21:28 +0100 Subject: [PATCH] support for context::fiber --- .../detail/pull_control_block_cc.hpp | 14 +- .../detail/pull_control_block_cc.ipp | 240 +++++++++--------- .../detail/push_control_block_cc.hpp | 14 +- .../detail/push_control_block_cc.ipp | 227 ++++++++--------- include/boost/coroutine2/detail/wrap.hpp | 8 +- 5 files changed, 247 insertions(+), 256 deletions(-) diff --git a/include/boost/coroutine2/detail/pull_control_block_cc.hpp b/include/boost/coroutine2/detail/pull_control_block_cc.hpp index b9ac614..da60cc4 100644 --- a/include/boost/coroutine2/detail/pull_control_block_cc.hpp +++ b/include/boost/coroutine2/detail/pull_control_block_cc.hpp @@ -11,7 +11,7 @@ #include #include -#include +#include #include @@ -25,7 +25,7 @@ namespace detail { template< typename T > struct pull_coroutine< T >::control_block { - boost::context::continuation c; + boost::context::fiber c; typename push_coroutine< T >::control_block * other; state_t state; std::exception_ptr except; @@ -37,7 +37,7 @@ struct pull_coroutine< T >::control_block { template< typename StackAllocator, typename Fn > control_block( context::preallocated, StackAllocator &&, Fn &&); - control_block( typename push_coroutine< T >::control_block *, boost::context::continuation &) noexcept; + control_block( typename push_coroutine< T >::control_block *, boost::context::fiber &) noexcept; ~control_block(); @@ -66,7 +66,7 @@ struct pull_coroutine< T & >::control_block { } }; - boost::context::continuation c; + boost::context::fiber c; typename push_coroutine< T & >::control_block * other; state_t state; std::exception_ptr except; @@ -78,7 +78,7 @@ struct pull_coroutine< T & >::control_block { template< typename StackAllocator, typename Fn > control_block( context::preallocated, StackAllocator &&, Fn &&); - control_block( typename push_coroutine< T & >::control_block *, boost::context::continuation &) noexcept; + control_block( typename push_coroutine< T & >::control_block *, boost::context::fiber &) noexcept; control_block( control_block &) = delete; control_block & operator=( control_block &) = delete; @@ -95,7 +95,7 @@ struct pull_coroutine< T & >::control_block { }; struct pull_coroutine< void >::control_block { - boost::context::continuation c; + boost::context::fiber c; push_coroutine< void >::control_block * other; state_t state; std::exception_ptr except; @@ -105,7 +105,7 @@ struct pull_coroutine< void >::control_block { template< typename StackAllocator, typename Fn > control_block( context::preallocated, StackAllocator &&, Fn &&); - control_block( push_coroutine< void >::control_block *, boost::context::continuation &) noexcept; + control_block( push_coroutine< void >::control_block *, boost::context::fiber &) noexcept; control_block( control_block &) = delete; control_block & operator=( control_block &) = delete; diff --git a/include/boost/coroutine2/detail/pull_control_block_cc.ipp b/include/boost/coroutine2/detail/pull_control_block_cc.ipp index 43fdb76..217d381 100644 --- a/include/boost/coroutine2/detail/pull_control_block_cc.ipp +++ b/include/boost/coroutine2/detail/pull_control_block_cc.ipp @@ -15,7 +15,7 @@ #include #include -#include +#include #include #include @@ -34,7 +34,7 @@ namespace detail { template< typename T > void pull_coroutine< T >::control_block::destroy( control_block * cb) noexcept { - boost::context::continuation c = std::move( cb->c); + boost::context::fiber c = std::move( cb->c); // destroy control structure cb->~control_block(); // destroy coroutine's stack @@ -45,42 +45,9 @@ template< typename T > template< typename StackAllocator, typename Fn > pull_coroutine< T >::control_block::control_block( context::preallocated palloc, StackAllocator && salloc, Fn && fn) : - c{}, - other{ nullptr }, - state{ state_t::unwind }, - except{}, - bvalid{ false }, - storage{} { #if defined(BOOST_NO_CXX14_GENERIC_LAMBDAS) - c = boost::context::callcc( - std::allocator_arg, palloc, std::forward< StackAllocator >( salloc), - wrap( [this](typename std::decay< Fn >::type & fn_,boost::context::continuation && c) mutable { - // create synthesized push_coroutine< T > - typename push_coroutine< T >::control_block synthesized_cb{ this, c }; - push_coroutine< T > synthesized{ & synthesized_cb }; - other = & synthesized_cb; - if ( state_t::none == ( state & state_t::destroy) ) { - try { - auto fn = std::move( fn_); - // call coroutine-fn with synthesized push_coroutine as argument - fn( synthesized); - } catch ( boost::context::detail::forced_unwind const&) { - throw; - } catch (...) { - // store other exceptions in exception-pointer - except = std::current_exception(); - } - } - // set termination flags - state |= state_t::complete; - // jump back - return other->c.resume(); - }, - std::forward< Fn >( fn) ) ); -#else - c = boost::context::callcc( - std::allocator_arg, palloc, std::forward< StackAllocator >( salloc), - [this,fn_=std::forward< Fn >( fn)](boost::context::continuation && c) mutable { + c{ std::allocator_arg, palloc, std::forward< StackAllocator >( salloc), + wrap( [this](typename std::decay< Fn >::type & fn_,boost::context::fiber && c) mutable { // create synthesized push_coroutine< T > typename push_coroutine< T >::control_block synthesized_cb{ this, c }; push_coroutine< T > synthesized{ & synthesized_cb }; @@ -101,16 +68,47 @@ pull_coroutine< T >::control_block::control_block( context::preallocated palloc, state |= state_t::complete; // jump back return other->c.resume(); - }); + }, + std::forward< Fn >( fn) ) }, +#else + c{ std::allocator_arg, palloc, std::forward< StackAllocator >( salloc), + [this,fn_=std::forward< Fn >( fn)](boost::context::fiber && c) mutable { + // create synthesized push_coroutine< T > + typename push_coroutine< T >::control_block synthesized_cb{ this, c }; + push_coroutine< T > synthesized{ & synthesized_cb }; + other = & synthesized_cb; + if ( state_t::none == ( state & state_t::destroy) ) { + try { + auto fn = std::move( fn_); + // call coroutine-fn with synthesized push_coroutine as argument + fn( synthesized); + } catch ( boost::context::detail::forced_unwind const&) { + throw; + } catch (...) { + // store other exceptions in exception-pointer + except = std::current_exception(); + } + } + // set termination flags + state |= state_t::complete; + // jump back + return other->c.resume(); + } }, #endif - if ( except) { - std::rethrow_exception( except); - } + other{ nullptr }, + state{ state_t::unwind }, + except{}, + bvalid{ false }, + storage{} { + c = c.resume(); + if ( except) { + std::rethrow_exception( except); + } } template< typename T > pull_coroutine< T >::control_block::control_block( typename push_coroutine< T >::control_block * cb, - boost::context::continuation & c_) noexcept : + boost::context::fiber & c_) noexcept : c{ std::move( c_) }, other{ cb }, state{ state_t::none }, @@ -184,7 +182,7 @@ pull_coroutine< T >::control_block::valid() const noexcept { template< typename T > void pull_coroutine< T & >::control_block::destroy( control_block * cb) noexcept { - boost::context::continuation c = std::move( cb->c); + boost::context::fiber c = std::move( cb->c); // destroy control structure cb->~control_block(); // destroy coroutine's stack @@ -195,42 +193,9 @@ template< typename T > template< typename StackAllocator, typename Fn > pull_coroutine< T & >::control_block::control_block( context::preallocated palloc, StackAllocator && salloc, Fn && fn) : - c{}, - other{ nullptr }, - state{ state_t::unwind }, - except{}, - bvalid{ false }, - storage{} { #if defined(BOOST_NO_CXX14_GENERIC_LAMBDAS) - c = boost::context::callcc( - std::allocator_arg, palloc, std::forward< StackAllocator >( salloc), - wrap( [this](typename std::decay< Fn >::type & fn_,boost::context::continuation && c) mutable { - // create synthesized push_coroutine< T & > - typename push_coroutine< T & >::control_block synthesized_cb{ this, c }; - push_coroutine< T & > synthesized{ & synthesized_cb }; - other = & synthesized_cb; - if ( state_t::none == ( state & state_t::destroy) ) { - try { - auto fn = std::move( fn_); - // call coroutine-fn with synthesized push_coroutine as argument - fn( synthesized); - } catch ( boost::context::detail::forced_unwind const&) { - throw; - } catch (...) { - // store other exceptions in exception-pointer - except = std::current_exception(); - } - } - // set termination flags - state |= state_t::complete; - // jump back - return other->c.resume(); - }, - std::forward< Fn >( fn) ) ); -#else - c = boost::context::callcc( - std::allocator_arg, palloc, std::forward< StackAllocator >( salloc), - [this,fn_=std::forward< Fn >( fn)](boost::context::continuation && c) mutable { + c{ std::allocator_arg, palloc, std::forward< StackAllocator >( salloc), + wrap( [this](typename std::decay< Fn >::type & fn_,boost::context::fiber && c) mutable { // create synthesized push_coroutine< T & > typename push_coroutine< T & >::control_block synthesized_cb{ this, c }; push_coroutine< T & > synthesized{ & synthesized_cb }; @@ -251,16 +216,47 @@ pull_coroutine< T & >::control_block::control_block( context::preallocated pallo state |= state_t::complete; // jump back return other->c.resume(); - }); + }, + std::forward< Fn >( fn) ) }, +#else + c{ std::allocator_arg, palloc, std::forward< StackAllocator >( salloc), + [this,fn_=std::forward< Fn >( fn)](boost::context::fiber && c) mutable { + // create synthesized push_coroutine< T & > + typename push_coroutine< T & >::control_block synthesized_cb{ this, c }; + push_coroutine< T & > synthesized{ & synthesized_cb }; + other = & synthesized_cb; + if ( state_t::none == ( state & state_t::destroy) ) { + try { + auto fn = std::move( fn_); + // call coroutine-fn with synthesized push_coroutine as argument + fn( synthesized); + } catch ( boost::context::detail::forced_unwind const&) { + throw; + } catch (...) { + // store other exceptions in exception-pointer + except = std::current_exception(); + } + } + // set termination flags + state |= state_t::complete; + // jump back + return other->c.resume(); + } }, #endif - if ( except) { - std::rethrow_exception( except); - } + other{ nullptr }, + state{ state_t::unwind }, + except{}, + bvalid{ false }, + storage{} { + c = c.resume(); + if ( except) { + std::rethrow_exception( except); + } } template< typename T > pull_coroutine< T & >::control_block::control_block( typename push_coroutine< T & >::control_block * cb, - boost::context::continuation & c_) noexcept : + boost::context::fiber & c_) noexcept : c{ std::move( c_) }, other{ cb }, state{ state_t::none }, @@ -311,7 +307,7 @@ pull_coroutine< T & >::control_block::valid() const noexcept { inline void pull_coroutine< void >::control_block::destroy( control_block * cb) noexcept { - boost::context::continuation c = std::move( cb->c); + boost::context::fiber c = std::move( cb->c); // destroy control structure cb->~control_block(); // destroy coroutine's stack @@ -321,40 +317,9 @@ pull_coroutine< void >::control_block::destroy( control_block * cb) noexcept { template< typename StackAllocator, typename Fn > pull_coroutine< void >::control_block::control_block( context::preallocated palloc, StackAllocator && salloc, Fn && fn) : - c{}, - other{ nullptr }, - state{ state_t::unwind }, - except{} { #if defined(BOOST_NO_CXX14_GENERIC_LAMBDAS) - c = boost::context::callcc( - std::allocator_arg, palloc, std::forward< StackAllocator >( salloc), - wrap( [this](typename std::decay< Fn >::type & fn_,boost::context::continuation && c) mutable { - // create synthesized push_coroutine< void > - typename push_coroutine< void >::control_block synthesized_cb{ this, c }; - push_coroutine< void > synthesized{ & synthesized_cb }; - other = & synthesized_cb; - if ( state_t::none == ( state & state_t::destroy) ) { - try { - auto fn = std::move( fn_); - // call coroutine-fn with synthesized push_coroutine as argument - fn( synthesized); - } catch ( boost::context::detail::forced_unwind const&) { - throw; - } catch (...) { - // store other exceptions in exception-pointer - except = std::current_exception(); - } - } - // set termination flags - state |= state_t::complete; - // jump back - return other->c.resume(); - }, - std::forward< Fn >( fn) ) ); -#else - c = boost::context::callcc( - std::allocator_arg, palloc, std::forward< StackAllocator >( salloc), - [this,fn_=std::forward< Fn >( fn)]( boost::context::continuation && c) mutable { + c{ std::allocator_arg, palloc, std::forward< StackAllocator >( salloc), + wrap( [this](typename std::decay< Fn >::type & fn_,boost::context::fiber && c) mutable { // create synthesized push_coroutine< void > typename push_coroutine< void >::control_block synthesized_cb{ this, c }; push_coroutine< void > synthesized{ & synthesized_cb }; @@ -373,18 +338,47 @@ pull_coroutine< void >::control_block::control_block( context::preallocated pall } // set termination flags state |= state_t::complete; - // jump back to ctx + // jump back return other->c.resume(); - }); + }, + std::forward< Fn >( fn) ) }, +#else + c{ std::allocator_arg, palloc, std::forward< StackAllocator >( salloc), + [this,fn_=std::forward< Fn >( fn)]( boost::context::fiber && c) mutable { + // create synthesized push_coroutine< void > + typename push_coroutine< void >::control_block synthesized_cb{ this, c }; + push_coroutine< void > synthesized{ & synthesized_cb }; + other = & synthesized_cb; + if ( state_t::none == ( state & state_t::destroy) ) { + try { + auto fn = std::move( fn_); + // call coroutine-fn with synthesized push_coroutine as argument + fn( synthesized); + } catch ( boost::context::detail::forced_unwind const&) { + throw; + } catch (...) { + // store other exceptions in exception-pointer + except = std::current_exception(); + } + } + // set termination flags + state |= state_t::complete; + // jump back to ctx + return other->c.resume(); + } }, #endif - if ( except) { - std::rethrow_exception( except); - } + other{ nullptr }, + state{ state_t::unwind }, + except{} { + c = c.resume(); + if ( except) { + std::rethrow_exception( except); + } } inline pull_coroutine< void >::control_block::control_block( push_coroutine< void >::control_block * cb, - boost::context::continuation & c_) noexcept : + boost::context::fiber & c_) noexcept : c{ std::move( c_) }, other{ cb }, state{ state_t::none }, diff --git a/include/boost/coroutine2/detail/push_control_block_cc.hpp b/include/boost/coroutine2/detail/push_control_block_cc.hpp index 8dda44f..1c32a0e 100644 --- a/include/boost/coroutine2/detail/push_control_block_cc.hpp +++ b/include/boost/coroutine2/detail/push_control_block_cc.hpp @@ -10,7 +10,7 @@ #include #include -#include +#include #include @@ -24,7 +24,7 @@ namespace detail { template< typename T > struct push_coroutine< T >::control_block { - boost::context::continuation c; + boost::context::fiber c; typename pull_coroutine< T >::control_block * other; state_t state; std::exception_ptr except; @@ -34,7 +34,7 @@ struct push_coroutine< T >::control_block { template< typename StackAllocator, typename Fn > control_block( context::preallocated, StackAllocator &&, Fn &&); - control_block( typename pull_coroutine< T >::control_block *, boost::context::continuation &) noexcept; + control_block( typename pull_coroutine< T >::control_block *, boost::context::fiber &) noexcept; control_block( control_block &) = delete; control_block & operator=( control_block &) = delete; @@ -50,7 +50,7 @@ struct push_coroutine< T >::control_block { template< typename T > struct push_coroutine< T & >::control_block { - boost::context::continuation c; + boost::context::fiber c; typename pull_coroutine< T & >::control_block * other; state_t state; std::exception_ptr except; @@ -60,7 +60,7 @@ struct push_coroutine< T & >::control_block { template< typename StackAllocator, typename Fn > control_block( context::preallocated, StackAllocator &&, Fn &&); - control_block( typename pull_coroutine< T & >::control_block *, boost::context::continuation &) noexcept; + control_block( typename pull_coroutine< T & >::control_block *, boost::context::fiber &) noexcept; control_block( control_block &) = delete; control_block & operator=( control_block &) = delete; @@ -73,7 +73,7 @@ struct push_coroutine< T & >::control_block { }; struct push_coroutine< void >::control_block { - boost::context::continuation c; + boost::context::fiber c; pull_coroutine< void >::control_block * other; state_t state; std::exception_ptr except; @@ -83,7 +83,7 @@ struct push_coroutine< void >::control_block { template< typename StackAllocator, typename Fn > control_block( context::preallocated, StackAllocator &&, Fn &&); - control_block( pull_coroutine< void >::control_block *, boost::context::continuation &) noexcept; + control_block( pull_coroutine< void >::control_block *, boost::context::fiber &) noexcept; control_block( control_block &) = delete; control_block & operator=( control_block &) = delete; diff --git a/include/boost/coroutine2/detail/push_control_block_cc.ipp b/include/boost/coroutine2/detail/push_control_block_cc.ipp index ae7dee1..fbecea1 100644 --- a/include/boost/coroutine2/detail/push_control_block_cc.ipp +++ b/include/boost/coroutine2/detail/push_control_block_cc.ipp @@ -14,7 +14,7 @@ #include #include -#include +#include #include #include @@ -33,7 +33,7 @@ namespace detail { template< typename T > void push_coroutine< T >::control_block::destroy( control_block * cb) noexcept { - boost::context::continuation c = std::move( cb->c); + boost::context::fiber c = std::move( cb->c); // destroy control structure cb->~control_block(); // destroy coroutine's stack @@ -44,41 +44,9 @@ template< typename T > template< typename StackAllocator, typename Fn > push_coroutine< T >::control_block::control_block( context::preallocated palloc, StackAllocator && salloc, Fn && fn) : - c{}, - other{ nullptr }, - state{ state_t::unwind }, - except{} { #if defined(BOOST_NO_CXX14_GENERIC_LAMBDAS) - c = boost::context::callcc( - std::allocator_arg, palloc, std::forward< StackAllocator >( salloc), - wrap( [this](typename std::decay< Fn >::type & fn_,boost::context::continuation && c) mutable { - // create synthesized pull_coroutine< T > - typename pull_coroutine< T >::control_block synthesized_cb{ this, c }; - pull_coroutine< T > synthesized{ & synthesized_cb }; - other = & synthesized_cb; - other->c = other->c.resume(); - if ( state_t::none == ( state & state_t::destroy) ) { - try { - auto fn = std::move( fn_); - // call coroutine-fn with synthesized pull_coroutine as argument - fn( synthesized); - } catch ( boost::context::detail::forced_unwind const&) { - throw; - } catch (...) { - // store other exceptions in exception-pointer - except = std::current_exception(); - } - } - // set termination flags - state |= state_t::complete; - // jump back - return other->c.resume(); - }, - std::forward< Fn >( fn) ) ); -#else - c = boost::context::callcc( - std::allocator_arg, palloc, std::forward< StackAllocator >( salloc), - [this,fn_=std::forward< Fn >( fn)](boost::context::continuation && c) mutable { + c{ std::allocator_arg, palloc, std::forward< StackAllocator >( salloc), + wrap( [this](typename std::decay< Fn >::type & fn_,boost::context::fiber && c) mutable { // create synthesized pull_coroutine< T > typename pull_coroutine< T >::control_block synthesized_cb{ this, c }; pull_coroutine< T > synthesized{ & synthesized_cb }; @@ -100,13 +68,43 @@ push_coroutine< T >::control_block::control_block( context::preallocated palloc, state |= state_t::complete; // jump back return other->c.resume(); - }); + }, + std::forward< Fn >( fn) ) }, +#else + c{ std::allocator_arg, palloc, std::forward< StackAllocator >( salloc), + [this,fn_=std::forward< Fn >( fn)](boost::context::fiber && c) mutable { + // create synthesized pull_coroutine< T > + typename pull_coroutine< T >::control_block synthesized_cb{ this, c }; + pull_coroutine< T > synthesized{ & synthesized_cb }; + other = & synthesized_cb; + other->c = other->c.resume(); + if ( state_t::none == ( state & state_t::destroy) ) { + try { + auto fn = std::move( fn_); + // call coroutine-fn with synthesized pull_coroutine as argument + fn( synthesized); + } catch ( boost::context::detail::forced_unwind const&) { + throw; + } catch (...) { + // store other exceptions in exception-pointer + except = std::current_exception(); + } + } + // set termination flags + state |= state_t::complete; + // jump back + return other->c.resume(); + } }, #endif + other{ nullptr }, + state{ state_t::unwind }, + except{} { + c = c.resume(); } template< typename T > push_coroutine< T >::control_block::control_block( typename pull_coroutine< T >::control_block * cb, - boost::context::continuation & c_) noexcept : + boost::context::fiber & c_) noexcept : c{ std::move( c_) }, other{ cb }, state{ state_t::none }, @@ -157,7 +155,7 @@ push_coroutine< T >::control_block::valid() const noexcept { template< typename T > void push_coroutine< T & >::control_block::destroy( control_block * cb) noexcept { - boost::context::continuation c = std::move( cb->c); + boost::context::fiber c = std::move( cb->c); // destroy control structure cb->~control_block(); // destroy coroutine's stack @@ -168,41 +166,9 @@ template< typename T > template< typename StackAllocator, typename Fn > push_coroutine< T & >::control_block::control_block( context::preallocated palloc, StackAllocator && salloc, Fn && fn) : - c{}, - other{ nullptr }, - state{ state_t::unwind }, - except{} { #if defined(BOOST_NO_CXX14_GENERIC_LAMBDAS) - c = boost::context::callcc( - std::allocator_arg, palloc, std::forward< StackAllocator >( salloc), - wrap( [this](typename std::decay< Fn >::type & fn_,boost::context::continuation && c) mutable { - // create synthesized pull_coroutine< T & > - typename pull_coroutine< T & >::control_block synthesized_cb{ this, c }; - pull_coroutine< T & > synthesized{ & synthesized_cb }; - other = & synthesized_cb; - other->c = other->c.resume(); - if ( state_t::none == ( state & state_t::destroy) ) { - try { - auto fn = std::move( fn_); - // call coroutine-fn with synthesized pull_coroutine as argument - fn( synthesized); - } catch ( boost::context::detail::forced_unwind const&) { - throw; - } catch (...) { - // store other exceptions in exception-pointer - except = std::current_exception(); - } - } - // set termination flags - state |= state_t::complete; - // jump back - return other->c.resume(); - }, - std::forward< Fn >( fn) ) ); -#else - c = boost::context::callcc( - std::allocator_arg, palloc, std::forward< StackAllocator >( salloc), - [this,fn_=std::forward< Fn >( fn)](boost::context::continuation && c) mutable { + c{ std::allocator_arg, palloc, std::forward< StackAllocator >( salloc), + wrap( [this](typename std::decay< Fn >::type & fn_,boost::context::fiber && c) mutable { // create synthesized pull_coroutine< T & > typename pull_coroutine< T & >::control_block synthesized_cb{ this, c }; pull_coroutine< T & > synthesized{ & synthesized_cb }; @@ -223,14 +189,45 @@ push_coroutine< T & >::control_block::control_block( context::preallocated pallo // set termination flags state |= state_t::complete; // jump back - return other->c.resume(); - }); + other->c = other->c.resume(); + }, + std::forward< Fn >( fn) ) }, +#else + c{ std::allocator_arg, palloc, std::forward< StackAllocator >( salloc), + [this,fn_=std::forward< Fn >( fn)](boost::context::fiber && c) mutable { + // create synthesized pull_coroutine< T & > + typename pull_coroutine< T & >::control_block synthesized_cb{ this, c }; + pull_coroutine< T & > synthesized{ & synthesized_cb }; + other = & synthesized_cb; + other->c = other->c.resume(); + if ( state_t::none == ( state & state_t::destroy) ) { + try { + auto fn = std::move( fn_); + // call coroutine-fn with synthesized pull_coroutine as argument + fn( synthesized); + } catch ( boost::context::detail::forced_unwind const&) { + throw; + } catch (...) { + // store other exceptions in exception-pointer + except = std::current_exception(); + } + } + // set termination flags + state |= state_t::complete; + // jump back + other->c = other->c.resume(); + return std::move( other->c); + } }, #endif + other{ nullptr }, + state{ state_t::unwind }, + except{} { + c = c.resume(); } template< typename T > push_coroutine< T & >::control_block::control_block( typename pull_coroutine< T & >::control_block * cb, - boost::context::continuation & c_) noexcept : + boost::context::fiber & c_) noexcept : c{ std::move( c_) }, other{ cb }, state{ state_t::none }, @@ -269,7 +266,7 @@ push_coroutine< T & >::control_block::valid() const noexcept { inline void push_coroutine< void >::control_block::destroy( control_block * cb) noexcept { - boost::context::continuation c = std::move( cb->c); + boost::context::fiber c = std::move( cb->c); // destroy control structure cb->~control_block(); // destroy coroutine's stack @@ -278,43 +275,11 @@ push_coroutine< void >::control_block::destroy( control_block * cb) noexcept { template< typename StackAllocator, typename Fn > push_coroutine< void >::control_block::control_block( context::preallocated palloc, StackAllocator && salloc, Fn && fn) : - c{}, - other{ nullptr }, - state{ state_t::unwind }, - except{} { #if defined(BOOST_NO_CXX14_GENERIC_LAMBDAS) - c = boost::context::callcc( - std::allocator_arg, palloc, std::forward< StackAllocator >( salloc), - wrap( [this](typename std::decay< Fn >::type & fn_,boost::context::continuation && c) mutable { - // create synthesized pull_coroutine< void > - typename pull_coroutine< void >::control_block synthesized_cb{ this, c }; - pull_coroutine< void > synthesized{ & synthesized_cb }; - other = & synthesized_cb; - other->c = other->c.resume(); - if ( state_t::none == ( state & state_t::destroy) ) { - try { - auto fn = std::move( fn_); - // call coroutine-fn with synthesized pull_coroutine as argument - fn( synthesized); - } catch ( boost::context::detail::forced_unwind const&) { - throw; - } catch (...) { - // store other exceptions in exception-pointer - except = std::current_exception(); - } - } - // set termination flags - state |= state_t::complete; - // jump back - return other->c.resume(); - }, - std::forward< Fn >( fn) ) ); -#else - c = boost::context::callcc( - std::allocator_arg, palloc, std::forward< StackAllocator >( salloc), - [this,fn_=std::forward< Fn >( fn)](boost::context::continuation && c) mutable { + c{ std::allocator_arg, palloc, std::forward< StackAllocator >( salloc), + wrap( [this](typename std::decay< Fn >::type & fn_,boost::context::fiber && c) mutable { // create synthesized pull_coroutine< void > - typename pull_coroutine< void >::control_block synthesized_cb{ this, c}; + typename pull_coroutine< void >::control_block synthesized_cb{ this, c }; pull_coroutine< void > synthesized{ & synthesized_cb }; other = & synthesized_cb; other->c = other->c.resume(); @@ -333,14 +298,46 @@ push_coroutine< void >::control_block::control_block( context::preallocated pall // set termination flags state |= state_t::complete; // jump back - return other->c.resume(); - }); + other->c = other->c.resume(); + return std::move( other->c); + }, + std::forward< Fn >( fn) ) }, +#else + c{ std::allocator_arg, palloc, std::forward< StackAllocator >( salloc), + [this,fn_=std::forward< Fn >( fn)](boost::context::fiber && c) mutable { + // create synthesized pull_coroutine< void > + typename pull_coroutine< void >::control_block synthesized_cb{ this, c}; + pull_coroutine< void > synthesized{ & synthesized_cb }; + other = & synthesized_cb; + other->c = other->c.resume(); + if ( state_t::none == ( state & state_t::destroy) ) { + try { + auto fn = std::move( fn_); + // call coroutine-fn with synthesized pull_coroutine as argument + fn( synthesized); + } catch ( boost::context::detail::forced_unwind const&) { + throw; + } catch (...) { + // store other exceptions in exception-pointer + except = std::current_exception(); + } + } + // set termination flags + state |= state_t::complete; + // jump back + other->c = other->c.resume(); + return std::move( other->c); + } }, #endif + other{ nullptr }, + state{ state_t::unwind }, + except{} { + c = c.resume(); } inline push_coroutine< void >::control_block::control_block( pull_coroutine< void >::control_block * cb, - boost::context::continuation & c_) noexcept : + boost::context::fiber & c_) noexcept : c{ std::move( c_) }, other{ cb }, state{ state_t::none }, diff --git a/include/boost/coroutine2/detail/wrap.hpp b/include/boost/coroutine2/detail/wrap.hpp index cd56338..87809f0 100644 --- a/include/boost/coroutine2/detail/wrap.hpp +++ b/include/boost/coroutine2/detail/wrap.hpp @@ -11,7 +11,7 @@ #include #include -#include +#include #include @@ -41,12 +41,12 @@ public: wrapper( wrapper && other) = default; wrapper & operator=( wrapper && other) = default; - boost::context::continuation - operator()( boost::context::continuation && c) { + boost::context::fiber + operator()( boost::context::fiber && c) { return boost::context::detail::invoke( std::move( fn1_), fn2_, - std::forward< boost::context::continuation >( c) ); + std::forward< boost::context::fiber >( c) ); } };