123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373 |
- // Copyright Oliver Kowalke 2017.
- // 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_CONTEXT_FIBER_H
- #define BOOST_CONTEXT_FIBER_H
- #include <boost/context/detail/config.hpp>
- #include <algorithm>
- #include <cstddef>
- #include <cstdint>
- #include <cstdlib>
- #include <exception>
- #include <functional>
- #include <memory>
- #include <ostream>
- #include <tuple>
- #include <utility>
- #include <boost/assert.hpp>
- #include <boost/config.hpp>
- #include <boost/intrusive_ptr.hpp>
- #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
- #include <boost/context/detail/exchange.hpp>
- #endif
- #if defined(BOOST_NO_CXX17_STD_INVOKE)
- #include <boost/context/detail/invoke.hpp>
- #endif
- #include <boost/context/detail/disable_overload.hpp>
- #include <boost/context/detail/exception.hpp>
- #include <boost/context/detail/fcontext.hpp>
- #include <boost/context/detail/tuple.hpp>
- #include <boost/context/fixedsize_stack.hpp>
- #include <boost/context/flags.hpp>
- #include <boost/context/preallocated.hpp>
- #include <boost/context/segmented_stack.hpp>
- #include <boost/context/stack_context.hpp>
- #ifdef BOOST_HAS_ABI_HEADERS
- # include BOOST_ABI_PREFIX
- #endif
- #if defined(BOOST_MSVC)
- # pragma warning(push)
- # pragma warning(disable: 4702)
- #endif
- namespace boost {
- namespace context {
- namespace detail {
- inline
- transfer_t fiber_unwind( transfer_t t) {
- throw forced_unwind( t.fctx);
- return { nullptr, nullptr };
- }
- template< typename Rec >
- transfer_t fiber_exit( transfer_t t) noexcept {
- Rec * rec = static_cast< Rec * >( t.data);
- // destroy context stack
- rec->deallocate();
- return { nullptr, nullptr };
- }
- template< typename Rec >
- void fiber_entry( transfer_t t) noexcept {
- // transfer control structure to the context-stack
- Rec * rec = static_cast< Rec * >( t.data);
- BOOST_ASSERT( nullptr != t.fctx);
- BOOST_ASSERT( nullptr != rec);
- try {
- // jump back to `create_context()`
- t = jump_fcontext( t.fctx, nullptr);
- // start executing
- t.fctx = rec->run( t.fctx);
- } catch ( forced_unwind const& ex) {
- t = { ex.fctx, nullptr };
- #ifndef BOOST_ASSERT_IS_VOID
- const_cast< forced_unwind & >( ex).caught = true;
- #endif
- }
- BOOST_ASSERT( nullptr != t.fctx);
- // destroy context-stack of `this`context on next context
- ontop_fcontext( t.fctx, rec, fiber_exit< Rec >);
- BOOST_ASSERT_MSG( false, "context already terminated");
- }
- template< typename Ctx, typename Fn >
- transfer_t fiber_ontop( transfer_t t) {
- BOOST_ASSERT( nullptr != t.data);
- auto p = *static_cast< Fn * >( t.data);
- t.data = nullptr;
- // execute function, pass fiber via reference
- Ctx c = p( Ctx{ t.fctx } );
- #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
- return { exchange( c.fctx_, nullptr), nullptr };
- #else
- return { std::exchange( c.fctx_, nullptr), nullptr };
- #endif
- }
- template< typename Ctx, typename StackAlloc, typename Fn >
- class fiber_record {
- private:
- stack_context sctx_;
- typename std::decay< StackAlloc >::type salloc_;
- typename std::decay< Fn >::type fn_;
- static void destroy( fiber_record * p) noexcept {
- typename std::decay< StackAlloc >::type salloc = std::move( p->salloc_);
- stack_context sctx = p->sctx_;
- // deallocate fiber_record
- p->~fiber_record();
- // destroy stack with stack allocator
- salloc.deallocate( sctx);
- }
- public:
- fiber_record( stack_context sctx, StackAlloc && salloc,
- Fn && fn) noexcept :
- sctx_( sctx),
- salloc_( std::forward< StackAlloc >( salloc)),
- fn_( std::forward< Fn >( fn) ) {
- }
- fiber_record( fiber_record const&) = delete;
- fiber_record & operator=( fiber_record const&) = delete;
- void deallocate() noexcept {
- destroy( this);
- }
- fcontext_t run( fcontext_t fctx) {
- // invoke context-function
- #if defined(BOOST_NO_CXX17_STD_INVOKE)
- Ctx c = boost::context::detail::invoke( fn_, Ctx{ fctx } );
- #else
- Ctx c = std::invoke( fn_, Ctx{ fctx } );
- #endif
- #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
- return exchange( c.fctx_, nullptr);
- #else
- return std::exchange( c.fctx_, nullptr);
- #endif
- }
- };
- template< typename Record, typename StackAlloc, typename Fn >
- fcontext_t create_fiber1( StackAlloc && salloc, Fn && fn) {
- auto sctx = salloc.allocate();
- // reserve space for control structure
- void * storage = reinterpret_cast< void * >(
- ( reinterpret_cast< uintptr_t >( sctx.sp) - static_cast< uintptr_t >( sizeof( Record) ) )
- & ~static_cast< uintptr_t >( 0xff) );
- // placment new for control structure on context stack
- Record * record = new ( storage) Record{
- sctx, std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) };
- // 64byte gab between control structure and stack top
- // should be 16byte aligned
- void * stack_top = reinterpret_cast< void * >(
- reinterpret_cast< uintptr_t >( storage) - static_cast< uintptr_t >( 64) );
- void * stack_bottom = reinterpret_cast< void * >(
- reinterpret_cast< uintptr_t >( sctx.sp) - static_cast< uintptr_t >( sctx.size) );
- // create fast-context
- const std::size_t size = reinterpret_cast< uintptr_t >( stack_top) - reinterpret_cast< uintptr_t >( stack_bottom);
- const fcontext_t fctx = make_fcontext( stack_top, size, & fiber_entry< Record >);
- BOOST_ASSERT( nullptr != fctx);
- // transfer control structure to context-stack
- return jump_fcontext( fctx, record).fctx;
- }
- template< typename Record, typename StackAlloc, typename Fn >
- fcontext_t create_fiber2( preallocated palloc, StackAlloc && salloc, Fn && fn) {
- // reserve space for control structure
- void * storage = reinterpret_cast< void * >(
- ( reinterpret_cast< uintptr_t >( palloc.sp) - static_cast< uintptr_t >( sizeof( Record) ) )
- & ~ static_cast< uintptr_t >( 0xff) );
- // placment new for control structure on context-stack
- Record * record = new ( storage) Record{
- palloc.sctx, std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) };
- // 64byte gab between control structure and stack top
- void * stack_top = reinterpret_cast< void * >(
- reinterpret_cast< uintptr_t >( storage) - static_cast< uintptr_t >( 64) );
- void * stack_bottom = reinterpret_cast< void * >(
- reinterpret_cast< uintptr_t >( palloc.sctx.sp) - static_cast< uintptr_t >( palloc.sctx.size) );
- // create fast-context
- const std::size_t size = reinterpret_cast< uintptr_t >( stack_top) - reinterpret_cast< uintptr_t >( stack_bottom);
- const fcontext_t fctx = make_fcontext( stack_top, size, & fiber_entry< Record >);
- BOOST_ASSERT( nullptr != fctx);
- // transfer control structure to context-stack
- return jump_fcontext( fctx, record).fctx;
- }
- }
- class fiber {
- private:
- template< typename Ctx, typename StackAlloc, typename Fn >
- friend class detail::fiber_record;
- template< typename Ctx, typename Fn >
- friend detail::transfer_t
- detail::fiber_ontop( detail::transfer_t);
- template< typename StackAlloc, typename Fn >
- friend fiber
- callcc( std::allocator_arg_t, StackAlloc &&, Fn &&);
- template< typename StackAlloc, typename Fn >
- friend fiber
- callcc( std::allocator_arg_t, preallocated, StackAlloc &&, Fn &&);
- detail::fcontext_t fctx_{ nullptr };
- fiber( detail::fcontext_t fctx) noexcept :
- fctx_{ fctx } {
- }
- public:
- fiber() noexcept = default;
- template< typename Fn, typename = detail::disable_overload< fiber, Fn > >
- fiber( Fn && fn) :
- fiber{ std::allocator_arg, fixedsize_stack(), std::forward< Fn >( fn) } {
- }
- template< typename StackAlloc, typename Fn >
- fiber( std::allocator_arg_t, StackAlloc && salloc, Fn && fn) :
- fctx_{ detail::create_fiber1< detail::fiber_record< fiber, StackAlloc, Fn > >(
- std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) ) } {
- }
- template< typename StackAlloc, typename Fn >
- fiber( std::allocator_arg_t, preallocated palloc, StackAlloc && salloc, Fn && fn) :
- fctx_{ detail::create_fiber2< detail::fiber_record< fiber, StackAlloc, Fn > >(
- palloc, std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) ) } {
- }
- #if defined(BOOST_USE_SEGMENTED_STACKS)
- template< typename Fn >
- fiber( std::allocator_arg_t, segmented_stack, Fn &&);
- template< typename StackAlloc, typename Fn >
- fiber( std::allocator_arg_t, preallocated, segmented_stack, Fn &&);
- #endif
- ~fiber() {
- if ( BOOST_UNLIKELY( nullptr != fctx_) ) {
- detail::ontop_fcontext(
- #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
- detail::exchange( fctx_, nullptr),
- #else
- std::exchange( fctx_, nullptr),
- #endif
- nullptr,
- detail::fiber_unwind);
- }
- }
- fiber( fiber && other) noexcept {
- swap( other);
- }
- fiber & operator=( fiber && other) noexcept {
- if ( BOOST_LIKELY( this != & other) ) {
- fiber tmp = std::move( other);
- swap( tmp);
- }
- return * this;
- }
- fiber( fiber const& other) noexcept = delete;
- fiber & operator=( fiber const& other) noexcept = delete;
- fiber resume() && {
- BOOST_ASSERT( nullptr != fctx_);
- return { detail::jump_fcontext(
- #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
- detail::exchange( fctx_, nullptr),
- #else
- std::exchange( fctx_, nullptr),
- #endif
- nullptr).fctx };
- }
- template< typename Fn >
- fiber resume_with( Fn && fn) && {
- BOOST_ASSERT( nullptr != fctx_);
- auto p = std::forward< Fn >( fn);
- return { detail::ontop_fcontext(
- #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
- detail::exchange( fctx_, nullptr),
- #else
- std::exchange( fctx_, nullptr),
- #endif
- & p,
- detail::fiber_ontop< fiber, decltype(p) >).fctx };
- }
- explicit operator bool() const noexcept {
- return nullptr != fctx_;
- }
- bool operator!() const noexcept {
- return nullptr == fctx_;
- }
- bool operator<( fiber const& other) const noexcept {
- return fctx_ < other.fctx_;
- }
- #if !defined(BOOST_EMBTC)
-
- template< typename charT, class traitsT >
- friend std::basic_ostream< charT, traitsT > &
- operator<<( std::basic_ostream< charT, traitsT > & os, fiber const& other) {
- if ( nullptr != other.fctx_) {
- return os << other.fctx_;
- } else {
- return os << "{not-a-context}";
- }
- }
- #else
-
- template< typename charT, class traitsT >
- friend std::basic_ostream< charT, traitsT > &
- operator<<( std::basic_ostream< charT, traitsT > & os, fiber const& other);
- #endif
- void swap( fiber & other) noexcept {
- std::swap( fctx_, other.fctx_);
- }
- };
- #if defined(BOOST_EMBTC)
- template< typename charT, class traitsT >
- inline std::basic_ostream< charT, traitsT > &
- operator<<( std::basic_ostream< charT, traitsT > & os, fiber const& other) {
- if ( nullptr != other.fctx_) {
- return os << other.fctx_;
- } else {
- return os << "{not-a-context}";
- }
- }
- #endif
-
- inline
- void swap( fiber & l, fiber & r) noexcept {
- l.swap( r);
- }
- typedef fiber fiber_context;
- }}
- #if defined(BOOST_MSVC)
- # pragma warning(pop)
- #endif
- #ifdef BOOST_HAS_ABI_HEADERS
- # include BOOST_ABI_SUFFIX
- #endif
- #endif // BOOST_CONTEXT_FIBER_H
|