123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442 |
- // Copyright Oliver Kowalke 2016.
- // Distributed under the Boost Software License, Version 1.0.
- // (See accompanying file LICENSE_1_0.txt or copy at
- // http://www.boost.org/LICENSE_1_0.txt)
- #ifndef BOOST_COROUTINES2_DETAIL_PULL_CONTROL_BLOCK_IPP
- #define BOOST_COROUTINES2_DETAIL_PULL_CONTROL_BLOCK_IPP
- #include <algorithm>
- #include <exception>
- #include <memory>
- #include <tuple>
- #include <boost/assert.hpp>
- #include <boost/config.hpp>
- #include <boost/context/detail/config.hpp>
- #include <boost/context/fiber.hpp>
- #include <boost/coroutine2/detail/config.hpp>
- #include <boost/coroutine2/detail/forced_unwind.hpp>
- #include <boost/coroutine2/detail/wrap.hpp>
- #ifdef BOOST_HAS_ABI_HEADERS
- # include BOOST_ABI_PREFIX
- #endif
- namespace boost {
- namespace coroutines2 {
- namespace detail {
- // pull_coroutine< T >
- template< typename T >
- void
- pull_coroutine< T >::control_block::destroy( control_block * cb) noexcept {
- boost::context::fiber c = std::move( cb->c);
- // destroy control structure
- cb->~control_block();
- // destroy coroutine's stack
- cb->state |= state_t::destroy;
- }
- template< typename T >
- template< typename StackAllocator, typename Fn >
- pull_coroutine< T >::control_block::control_block( context::preallocated palloc, StackAllocator && salloc,
- Fn && fn) :
- #if defined(BOOST_NO_CXX14_GENERIC_LAMBDAS)
- 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 };
- 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;
- #if defined( BOOST_CONTEXT_HAS_CXXABI_H )
- } catch ( abi::__forced_unwind const&) {
- throw;
- #endif
- } catch (...) {
- // store other exceptions in exception-pointer
- except = std::current_exception();
- }
- }
- // set termination flags
- state |= state_t::complete;
- // jump back
- return std::move( 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;
- #if defined( BOOST_CONTEXT_HAS_CXXABI_H )
- } catch ( abi::__forced_unwind const&) {
- throw;
- #endif
- } catch (...) {
- // store other exceptions in exception-pointer
- except = std::current_exception();
- }
- }
- // set termination flags
- state |= state_t::complete;
- // jump back
- return std::move( other->c).resume();
- } },
- #endif
- other{ nullptr },
- state{ state_t::unwind },
- except{},
- bvalid{ false },
- storage{} {
- c = std::move( 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::fiber & c_) noexcept :
- c{ std::move( c_) },
- other{ cb },
- state{ state_t::none },
- except{},
- bvalid{ false },
- storage{} {
- }
- template< typename T >
- pull_coroutine< T >::control_block::~control_block() {
- // destroy data if set
- if ( bvalid) {
- reinterpret_cast< T * >( std::addressof( storage) )->~T();
- }
- }
- template< typename T >
- void
- pull_coroutine< T >::control_block::deallocate() noexcept {
- if ( state_t::none != ( state & state_t::unwind) ) {
- destroy( this);
- }
- }
- template< typename T >
- void
- pull_coroutine< T >::control_block::resume() {
- c = std::move( c).resume();
- if ( except) {
- std::rethrow_exception( except);
- }
- }
- template< typename T >
- void
- pull_coroutine< T >::control_block::set( T const& t) {
- // destroy data if set
- if ( bvalid) {
- reinterpret_cast< T * >( std::addressof( storage) )->~T();
- }
- ::new ( static_cast< void * >( std::addressof( storage) ) ) T( t);
- bvalid = true;
- }
- template< typename T >
- void
- pull_coroutine< T >::control_block::set( T && t) {
- // destroy data if set
- if ( bvalid) {
- reinterpret_cast< T * >( std::addressof( storage) )->~T();
- }
- ::new ( static_cast< void * >( std::addressof( storage) ) ) T( std::move( t) );
- bvalid = true;
- }
- template< typename T >
- T &
- pull_coroutine< T >::control_block::get() noexcept {
- return * reinterpret_cast< T * >( std::addressof( storage) );
- }
- template< typename T >
- bool
- pull_coroutine< T >::control_block::valid() const noexcept {
- return nullptr != other && state_t::none == ( state & state_t::complete) && bvalid;
- }
- // pull_coroutine< T & >
- template< typename T >
- void
- pull_coroutine< T & >::control_block::destroy( control_block * cb) noexcept {
- boost::context::fiber c = std::move( cb->c);
- // destroy control structure
- cb->~control_block();
- // destroy coroutine's stack
- cb->state |= state_t::destroy;
- }
- template< typename T >
- template< typename StackAllocator, typename Fn >
- pull_coroutine< T & >::control_block::control_block( context::preallocated palloc, StackAllocator && salloc,
- Fn && fn) :
- #if defined(BOOST_NO_CXX14_GENERIC_LAMBDAS)
- 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 };
- 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;
- #if defined( BOOST_CONTEXT_HAS_CXXABI_H )
- } catch ( abi::__forced_unwind const&) {
- throw;
- #endif
- } catch (...) {
- // store other exceptions in exception-pointer
- except = std::current_exception();
- }
- }
- // set termination flags
- state |= state_t::complete;
- // jump back
- return std::move( 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;
- #if defined( BOOST_CONTEXT_HAS_CXXABI_H )
- } catch ( abi::__forced_unwind const&) {
- throw;
- #endif
- } catch (...) {
- // store other exceptions in exception-pointer
- except = std::current_exception();
- }
- }
- // set termination flags
- state |= state_t::complete;
- // jump back
- return std::move( other->c).resume();
- } },
- #endif
- other{ nullptr },
- state{ state_t::unwind },
- except{},
- bvalid{ false },
- storage{} {
- c = std::move( 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::fiber & c_) noexcept :
- c{ std::move( c_) },
- other{ cb },
- state{ state_t::none },
- except{},
- bvalid{ false },
- storage{} {
- }
- template< typename T >
- void
- pull_coroutine< T & >::control_block::deallocate() noexcept {
- if ( state_t::none != ( state & state_t::unwind) ) {
- destroy( this);
- }
- }
- template< typename T >
- void
- pull_coroutine< T & >::control_block::resume() {
- c = std::move( c).resume();
- if ( except) {
- std::rethrow_exception( except);
- }
- }
- template< typename T >
- void
- pull_coroutine< T & >::control_block::set( T & t) {
- ::new ( static_cast< void * >( std::addressof( storage) ) ) holder{ t };
- bvalid = true;
- }
- template< typename T >
- T &
- pull_coroutine< T & >::control_block::get() noexcept {
- return reinterpret_cast< holder * >( std::addressof( storage) )->t;
- }
- template< typename T >
- bool
- pull_coroutine< T & >::control_block::valid() const noexcept {
- return nullptr != other && state_t::none == ( state & state_t::complete) && bvalid;
- }
- // pull_coroutine< void >
- inline
- void
- pull_coroutine< void >::control_block::destroy( control_block * cb) noexcept {
- boost::context::fiber c = std::move( cb->c);
- // destroy control structure
- cb->~control_block();
- // destroy coroutine's stack
- cb->state |= state_t::destroy;
- }
- template< typename StackAllocator, typename Fn >
- pull_coroutine< void >::control_block::control_block( context::preallocated palloc, StackAllocator && salloc,
- Fn && fn) :
- #if defined(BOOST_NO_CXX14_GENERIC_LAMBDAS)
- 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 };
- 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;
- #if defined( BOOST_CONTEXT_HAS_CXXABI_H )
- } catch ( abi::__forced_unwind const&) {
- throw;
- #endif
- } catch (...) {
- // store other exceptions in exception-pointer
- except = std::current_exception();
- }
- }
- // set termination flags
- state |= state_t::complete;
- // jump back
- return std::move( 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;
- #if defined( BOOST_CONTEXT_HAS_CXXABI_H )
- } catch ( abi::__forced_unwind const&) {
- throw;
- #endif
- } catch (...) {
- // store other exceptions in exception-pointer
- except = std::current_exception();
- }
- }
- // set termination flags
- state |= state_t::complete;
- // jump back to ctx
- return std::move( other->c).resume();
- } },
- #endif
- other{ nullptr },
- state{ state_t::unwind },
- except{} {
- c = std::move( c).resume();
- if ( except) {
- std::rethrow_exception( except);
- }
- }
- inline
- pull_coroutine< void >::control_block::control_block( push_coroutine< void >::control_block * cb,
- boost::context::fiber & c_) noexcept :
- c{ std::move( c_) },
- other{ cb },
- state{ state_t::none },
- except{} {
- }
- inline
- void
- pull_coroutine< void >::control_block::deallocate() noexcept {
- if ( state_t::none != ( state & state_t::unwind) ) {
- destroy( this);
- }
- }
- inline
- void
- pull_coroutine< void >::control_block::resume() {
- c = std::move( c).resume();
- if ( except) {
- std::rethrow_exception( except);
- }
- }
- inline
- bool
- pull_coroutine< void >::control_block::valid() const noexcept {
- return nullptr != other && state_t::none == ( state & state_t::complete);
- }
- }}}
- #ifdef BOOST_HAS_ABI_HEADERS
- # include BOOST_ABI_SUFFIX
- #endif
- #endif // BOOST_COROUTINES2_DETAIL_PULL_CONTROL_BLOCK_IPP
|