123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449 |
- // Copyright Oliver Kowalke 2009.
- // 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_COROUTINES_DETAIL_SYMMETRIC_COROUTINE_IMPL_H
- #define BOOST_COROUTINES_DETAIL_SYMMETRIC_COROUTINE_IMPL_H
- #include <boost/assert.hpp>
- #include <boost/config.hpp>
- #include <boost/cstdint.hpp>
- #include <boost/utility.hpp>
- #include <boost/coroutine/detail/config.hpp>
- #include <boost/coroutine/detail/coroutine_context.hpp>
- #include <boost/coroutine/detail/flags.hpp>
- #include <boost/coroutine/detail/parameters.hpp>
- #include <boost/coroutine/detail/preallocated.hpp>
- #include <boost/coroutine/detail/trampoline.hpp>
- #include <boost/coroutine/exceptions.hpp>
- #include <boost/coroutine/stack_context.hpp>
- #ifdef BOOST_HAS_ABI_HEADERS
- # include BOOST_ABI_PREFIX
- #endif
- namespace boost {
- namespace coroutines {
- namespace detail {
- template< typename R >
- class symmetric_coroutine_impl : private noncopyable
- {
- public:
- typedef parameters< R > param_type;
- symmetric_coroutine_impl( preallocated const& palloc,
- bool unwind) BOOST_NOEXCEPT :
- flags_( 0),
- caller_(),
- callee_( trampoline< symmetric_coroutine_impl< R > >, palloc)
- {
- if ( unwind) flags_ |= flag_force_unwind;
- }
- virtual ~symmetric_coroutine_impl() {}
- bool force_unwind() const BOOST_NOEXCEPT
- { return 0 != ( flags_ & flag_force_unwind); }
- bool unwind_requested() const BOOST_NOEXCEPT
- { return 0 != ( flags_ & flag_unwind_stack); }
- bool is_started() const BOOST_NOEXCEPT
- { return 0 != ( flags_ & flag_started); }
- bool is_running() const BOOST_NOEXCEPT
- { return 0 != ( flags_ & flag_running); }
- bool is_complete() const BOOST_NOEXCEPT
- { return 0 != ( flags_ & flag_complete); }
- void unwind_stack() BOOST_NOEXCEPT
- {
- if ( is_started() && ! is_complete() && force_unwind() )
- {
- flags_ |= flag_unwind_stack;
- flags_ |= flag_running;
- param_type to( unwind_t::force_unwind);
- caller_.jump(
- callee_,
- & to);
- flags_ &= ~flag_running;
- flags_ &= ~flag_unwind_stack;
- BOOST_ASSERT( is_complete() );
- }
- }
- void resume( R r) BOOST_NOEXCEPT
- {
- param_type to( const_cast< R * >( & r), this);
- resume_( & to);
- }
- R * yield()
- {
- BOOST_ASSERT( is_running() );
- BOOST_ASSERT( ! is_complete() );
- flags_ &= ~flag_running;
- param_type to;
- param_type * from(
- static_cast< param_type * >(
- callee_.jump(
- caller_,
- & to) ) );
- flags_ |= flag_running;
- if ( from->do_unwind) throw forced_unwind();
- BOOST_ASSERT( from->data);
- return from->data;
- }
- template< typename X >
- R * yield_to( symmetric_coroutine_impl< X > * other, X x)
- {
- typename symmetric_coroutine_impl< X >::param_type to( & x, other);
- return yield_to_( other, & to);
- }
- template< typename X >
- R * yield_to( symmetric_coroutine_impl< X & > * other, X & x)
- {
- typename symmetric_coroutine_impl< X & >::param_type to( & x, other);
- return yield_to_( other, & to);
- }
- template< typename X >
- R * yield_to( symmetric_coroutine_impl< X > * other)
- {
- typename symmetric_coroutine_impl< X >::param_type to( other);
- return yield_to_( other, & to);
- }
- virtual void run( R *) BOOST_NOEXCEPT = 0;
- virtual void destroy() = 0;
- protected:
- template< typename X >
- friend class symmetric_coroutine_impl;
- int flags_;
- coroutine_context caller_;
- coroutine_context callee_;
- void resume_( param_type * to) BOOST_NOEXCEPT
- {
- BOOST_ASSERT( ! is_running() );
- BOOST_ASSERT( ! is_complete() );
- flags_ |= flag_running;
- caller_.jump(
- callee_,
- to);
- flags_ &= ~flag_running;
- }
- template< typename Other >
- R * yield_to_( Other * other, typename Other::param_type * to)
- {
- BOOST_ASSERT( is_running() );
- BOOST_ASSERT( ! is_complete() );
- BOOST_ASSERT( ! other->is_running() );
- BOOST_ASSERT( ! other->is_complete() );
- other->caller_ = caller_;
- flags_ &= ~flag_running;
- param_type * from(
- static_cast< param_type * >(
- callee_.jump(
- other->callee_,
- to) ) );
- flags_ |= flag_running;
- if ( from->do_unwind) throw forced_unwind();
- BOOST_ASSERT( from->data);
- return from->data;
- }
- };
- template< typename R >
- class symmetric_coroutine_impl< R & > : private noncopyable
- {
- public:
- typedef parameters< R & > param_type;
- symmetric_coroutine_impl( preallocated const& palloc,
- bool unwind) BOOST_NOEXCEPT :
- flags_( 0),
- caller_(),
- callee_( trampoline< symmetric_coroutine_impl< R > >, palloc)
- {
- if ( unwind) flags_ |= flag_force_unwind;
- }
- virtual ~symmetric_coroutine_impl() {}
- bool force_unwind() const BOOST_NOEXCEPT
- { return 0 != ( flags_ & flag_force_unwind); }
- bool unwind_requested() const BOOST_NOEXCEPT
- { return 0 != ( flags_ & flag_unwind_stack); }
- bool is_started() const BOOST_NOEXCEPT
- { return 0 != ( flags_ & flag_started); }
- bool is_running() const BOOST_NOEXCEPT
- { return 0 != ( flags_ & flag_running); }
- bool is_complete() const BOOST_NOEXCEPT
- { return 0 != ( flags_ & flag_complete); }
- void unwind_stack() BOOST_NOEXCEPT
- {
- if ( is_started() && ! is_complete() && force_unwind() )
- {
- flags_ |= flag_unwind_stack;
- flags_ |= flag_running;
- param_type to( unwind_t::force_unwind);
- caller_.jump(
- callee_,
- & to);
- flags_ &= ~flag_running;
- flags_ &= ~flag_unwind_stack;
- BOOST_ASSERT( is_complete() );
- }
- }
- void resume( R & arg) BOOST_NOEXCEPT
- {
- param_type to( & arg, this);
- resume_( & to);
- }
- R * yield()
- {
- BOOST_ASSERT( is_running() );
- BOOST_ASSERT( ! is_complete() );
- flags_ &= ~flag_running;
- param_type to;
- param_type * from(
- static_cast< param_type * >(
- callee_.jump(
- caller_,
- & to) ) );
- flags_ |= flag_running;
- if ( from->do_unwind) throw forced_unwind();
- BOOST_ASSERT( from->data);
- return from->data;
- }
- template< typename X >
- R * yield_to( symmetric_coroutine_impl< X > * other, X x)
- {
- typename symmetric_coroutine_impl< X >::param_type to( & x, other);
- return yield_to_( other, & to);
- }
- template< typename X >
- R * yield_to( symmetric_coroutine_impl< X & > * other, X & x)
- {
- typename symmetric_coroutine_impl< X & >::param_type to( & x, other);
- return yield_to_( other, & to);
- }
- template< typename X >
- R * yield_to( symmetric_coroutine_impl< X > * other)
- {
- typename symmetric_coroutine_impl< X >::param_type to( other);
- return yield_to_( other, & to);
- }
- virtual void run( R *) BOOST_NOEXCEPT = 0;
- virtual void destroy() = 0;
- protected:
- template< typename X >
- friend class symmetric_coroutine_impl;
- int flags_;
- coroutine_context caller_;
- coroutine_context callee_;
- void resume_( param_type * to) BOOST_NOEXCEPT
- {
- BOOST_ASSERT( ! is_running() );
- BOOST_ASSERT( ! is_complete() );
- flags_ |= flag_running;
- caller_.jump(
- callee_,
- to);
- flags_ &= ~flag_running;
- }
- template< typename Other >
- R * yield_to_( Other * other, typename Other::param_type * to)
- {
- BOOST_ASSERT( is_running() );
- BOOST_ASSERT( ! is_complete() );
- BOOST_ASSERT( ! other->is_running() );
- BOOST_ASSERT( ! other->is_complete() );
- other->caller_ = caller_;
- flags_ &= ~flag_running;
- param_type * from(
- static_cast< param_type * >(
- callee_.jump(
- other->callee_,
- to) ) );
- flags_ |= flag_running;
- if ( from->do_unwind) throw forced_unwind();
- BOOST_ASSERT( from->data);
- return from->data;
- }
- };
- template<>
- class symmetric_coroutine_impl< void > : private noncopyable
- {
- public:
- typedef parameters< void > param_type;
- symmetric_coroutine_impl( preallocated const& palloc,
- bool unwind) BOOST_NOEXCEPT :
- flags_( 0),
- caller_(),
- callee_( trampoline_void< symmetric_coroutine_impl< void > >, palloc)
- {
- if ( unwind) flags_ |= flag_force_unwind;
- }
- virtual ~symmetric_coroutine_impl() {}
- inline bool force_unwind() const BOOST_NOEXCEPT
- { return 0 != ( flags_ & flag_force_unwind); }
- inline bool unwind_requested() const BOOST_NOEXCEPT
- { return 0 != ( flags_ & flag_unwind_stack); }
- inline bool is_started() const BOOST_NOEXCEPT
- { return 0 != ( flags_ & flag_started); }
- inline bool is_running() const BOOST_NOEXCEPT
- { return 0 != ( flags_ & flag_running); }
- inline bool is_complete() const BOOST_NOEXCEPT
- { return 0 != ( flags_ & flag_complete); }
- inline void unwind_stack() BOOST_NOEXCEPT
- {
- if ( is_started() && ! is_complete() && force_unwind() )
- {
- flags_ |= flag_unwind_stack;
- flags_ |= flag_running;
- param_type to( unwind_t::force_unwind);
- caller_.jump(
- callee_,
- & to);
- flags_ &= ~flag_running;
- flags_ &= ~flag_unwind_stack;
- BOOST_ASSERT( is_complete() );
- }
- }
- inline void resume() BOOST_NOEXCEPT
- {
- BOOST_ASSERT( ! is_running() );
- BOOST_ASSERT( ! is_complete() );
- param_type to( this);
- flags_ |= flag_running;
- caller_.jump(
- callee_,
- & to);
- flags_ &= ~flag_running;
- }
- inline void yield()
- {
- BOOST_ASSERT( is_running() );
- BOOST_ASSERT( ! is_complete() );
- flags_ &= ~flag_running;
- param_type to;
- param_type * from(
- static_cast< param_type * >(
- callee_.jump(
- caller_,
- & to) ) );
- flags_ |= flag_running;
- if ( from->do_unwind) throw forced_unwind();
- }
- template< typename X >
- void yield_to( symmetric_coroutine_impl< X > * other, X x)
- {
- typename symmetric_coroutine_impl< X >::param_type to( & x, other);
- yield_to_( other, & to);
- }
- template< typename X >
- void yield_to( symmetric_coroutine_impl< X & > * other, X & x)
- {
- typename symmetric_coroutine_impl< X & >::param_type to( & x, other);
- yield_to_( other, & to);
- }
- template< typename X >
- void yield_to( symmetric_coroutine_impl< X > * other)
- {
- typename symmetric_coroutine_impl< X >::param_type to( other);
- yield_to_( other, & to);
- }
- virtual void run() BOOST_NOEXCEPT = 0;
- virtual void destroy() = 0;
- protected:
- template< typename X >
- friend class symmetric_coroutine_impl;
- int flags_;
- coroutine_context caller_;
- coroutine_context callee_;
- template< typename Other >
- void yield_to_( Other * other, typename Other::param_type * to)
- {
- BOOST_ASSERT( is_running() );
- BOOST_ASSERT( ! is_complete() );
- BOOST_ASSERT( ! other->is_running() );
- BOOST_ASSERT( ! other->is_complete() );
- other->caller_ = caller_;
- flags_ &= ~flag_running;
- param_type * from(
- static_cast< param_type * >(
- callee_.jump(
- other->callee_,
- to) ) );
- flags_ |= flag_running;
- if ( from->do_unwind) throw forced_unwind();
- }
- };
- }}}
- #ifdef BOOST_HAS_ABI_HEADERS
- # include BOOST_ABI_SUFFIX
- #endif
- #endif // BOOST_COROUTINES_DETAIL_SYMMETRIC_COROUTINE_IMPL_H
|