From d5e8413b5d5a9a564a9fb8207cf42e4f21a26d28 Mon Sep 17 00:00:00 2001 From: Oliver Kowalke Date: Tue, 28 Jan 2014 20:46:45 +0100 Subject: [PATCH] remove pull/push_coroutine_object< void > --- example/cpp03/simple.cpp | 17 +- include/boost/coroutine/coroutine.hpp | 233 +++++++++--------- .../coroutine/detail/pull_coroutine_base.hpp | 8 +- .../detail/pull_coroutine_object.hpp | 71 ------ .../coroutine/detail/push_coroutine_base.hpp | 5 +- .../detail/push_coroutine_object.hpp | 71 ------ include/boost/coroutine/detail/trampoline.hpp | 55 ++++- 7 files changed, 183 insertions(+), 277 deletions(-) diff --git a/example/cpp03/simple.cpp b/example/cpp03/simple.cpp index b0c39fe..612dc96 100644 --- a/example/cpp03/simple.cpp +++ b/example/cpp03/simple.cpp @@ -19,23 +19,21 @@ struct X {} }; -typedef boost::coroutines::coroutine< X* >::pull_type pull_coro_t; -typedef boost::coroutines::coroutine< X* >::push_type push_coro_t; +typedef boost::coroutines::coroutine< void >::pull_type pull_coro_t; +typedef boost::coroutines::coroutine< void >::push_type push_coro_t; void fn1( push_coro_t & sink) { for ( int i = 0; i < 10; ++i) { - X x( i); - sink( & x); + sink(); } } void fn2( pull_coro_t & source) { while ( source) { - X * x = source.get(); - std::cout << "i = " << x->i << std::endl; + std::cout << "i = " << std::endl; source(); } } @@ -45,8 +43,8 @@ int main( int argc, char * argv[]) { pull_coro_t source( fn1); while ( source) { - X * x = source.get(); - std::cout << "i = " << x->i << std::endl; +// X * x = source.get(); + std::cout << "i = " << std::endl; source(); } } @@ -54,8 +52,7 @@ int main( int argc, char * argv[]) push_coro_t sink( fn2); for ( int i = 0; i < 10; ++i) { - X x( i); - sink( & x); + sink(); } } std::cout << "Done" << std::endl; diff --git a/include/boost/coroutine/coroutine.hpp b/include/boost/coroutine/coroutine.hpp index 09a58c2..24498a6 100644 --- a/include/boost/coroutine/coroutine.hpp +++ b/include/boost/coroutine/coroutine.hpp @@ -422,6 +422,10 @@ private: typename X, typename Y, typename Z > friend class detail::pull_coroutine_object; + template< + typename X, typename Y, typename Z + > + friend void detail::trampoline_void( intptr_t); typedef detail::push_coroutine_base< void > base_t; @@ -1429,6 +1433,10 @@ private: typename X, typename Y, typename Z > friend class detail::push_coroutine_object; + template< + typename X, typename Y, typename Z + > + friend void detail::trampoline_void( intptr_t); typedef detail::pull_coroutine_base< void > base_t; @@ -1471,15 +1479,14 @@ public: caller_(), callee_() { - typedef detail::pull_coroutine_object< - void, coroutine_fn, push_coroutine< void, StackAllocator > - > object_t; - stack_alloc_.allocate( stack_ctx_, attr.size); - callee_ = detail::coroutine_context( detail::trampoline< coroutine_fn, object_t >, - & stack_ctx_); - detail::setup< coroutine_fn, object_t > to( forward< coroutine_fn >( fn), - & caller_, & callee_, attr); + callee_ = detail::coroutine_context( + detail::trampoline< + coroutine_fn, base_t, push_corotuine< void, StackAllocator > + >, + & stack_ctx_); + detail::setup< coroutine_fn, base_t > to( forward< coroutine_fn >( fn), + & caller_, & callee_, attr); impl_ = reinterpret_cast< base_t * >( caller_.jump( callee_, @@ -1498,15 +1505,14 @@ public: caller_(), callee_() { - typedef detail::pull_coroutine_object< - void, coroutine_fn, push_coroutine< void, StackAllocator > - > object_t; - stack_alloc_.allocate( stack_ctx_, attr.size); - callee_ = detail::coroutine_context( detail::trampoline< coroutine_fn, object_t >, - & stack_ctx_); - detail::setup< coroutine_fn, object_t > to( forward< coroutine_fn >( fn), - & caller_, & callee_, attr); + callee_ = detail::coroutine_context( + detail::trampoline< + coroutine_fn, base_t, push_coroutine< void, StackAllocator > + >, + & stack_ctx_); + detail::setup< coroutine_fn, base_t > to( forward< coroutine_fn >( fn), + & caller_, & callee_, attr); impl_ = reinterpret_cast< base_t * >( caller_.jump( callee_, @@ -1525,15 +1531,14 @@ public: caller_(), callee_() { - typedef detail::pull_coroutine_object< - void, Fn, push_coroutine< void, StackAllocator > - > object_t; - stack_alloc_.allocate( stack_ctx_, attr.size); - callee_ = detail::coroutine_context( detail::trampoline< Fn, object_t >, - & stack_ctx_); - detail::setup< Fn, object_t > to( forward< Fn >( fn), - & caller_, & callee_, attr); + callee_ = detail::coroutine_context( + detail::trampoline< + Fn, base_t, push_coroutine< void, StackAllocator > + >, + & stack_ctx_); + detail::setup< Fn, base_t > to( forward< Fn >( fn), + & caller_, & callee_, attr); impl_ = reinterpret_cast< base_t * >( caller_.jump( callee_, @@ -1553,15 +1558,14 @@ public: caller_(), callee_() { - typedef detail::pull_coroutine_object< - void, Fn, push_coroutine< void, StackAllocator > - > object_t; - stack_alloc_.allocate( stack_ctx_, attr.size); - callee_ = detail::coroutine_context( detail::trampoline< Fn, object_t >, - & stack_ctx_); - detail::setup< Fn, object_t > to( forward< Fn >( fn), - & caller_, & callee_, attr); + callee_ = detail::coroutine_context( + detail::trampoline< + Fn, base_t, push_coroutine< void, StackAllocator > + >, + & stack_ctx_); + detail::setup< Fn, base_t > to( forward< Fn >( fn), + & caller_, & callee_, attr); impl_ = reinterpret_cast< base_t * >( caller_.jump( callee_, @@ -1583,13 +1587,13 @@ public: caller_(), callee_() { - typedef detail::pull_coroutine_object< - void, Fn, push_coroutine< void, StackAllocator > - > object_t; - stack_alloc_.allocate( stack_ctx_, attr.size); - callee_ = detail::coroutine_context( detail::trampoline< Fn, object_t >, & stack_ctx_); - detail::setup< Fn, object_t > to( fn, & caller_, & callee_, attr); + callee_ = detail::coroutine_context( + detail::trampoline_void< + Fn, base_t, push_coroutine< void, StackAllocator > + >, + & stack_ctx_); + detail::setup< Fn, base_t > to( fn, & caller_, & callee_, attr); impl_ = reinterpret_cast< base_t * >( caller_.jump( callee_, @@ -1612,14 +1616,13 @@ public: caller_(), callee_() { - typedef detail::pull_coroutine_object< - void, Fn, push_coroutine< void, StackAllocator > - > object_t; - stack_alloc_.allocate( stack_ctx_, attr.size); - callee_ = detail::coroutine_context( detail::trampoline< Fn, object_t >, - & stack_ctx_); - detail::setup< Fn, object_t > to( fn, & caller_, & callee_, attr); + callee_ = detail::coroutine_context( + detail::trampoline_void< + Fn, base_t, push_coroutine< void, StackAllocator > + >, + & stack_ctx_); + detail::setup< Fn, base_t > to( fn, & caller_, & callee_, attr); impl_ = reinterpret_cast< base_t * >( caller_.jump( callee_, @@ -1642,15 +1645,13 @@ public: caller_(), callee_() { - typedef detail::pull_coroutine_object< - void, Fn, push_coroutine< void, StackAllocator > - > object_t; - stack_alloc_.allocate( stack_ctx_, attr.size); - callee_ = detail::coroutine_context( detail::trampoline< Fn, object_t >, - & stack_ctx_); - detail::setup< Fn, object_t > to( forward< Fn >( fn), - & caller_, & callee_, attr); + callee_ = detail::coroutine_context( + detail::trampoline< + Fn, base_t, push_coroutine< void, StackAllocator > + >, + & stack_ctx_); + detail::setup< Fn, base_t > to( fn, & caller_, & callee_, attr); impl_ = reinterpret_cast< base_t * >( caller_.jump( callee_, @@ -1674,15 +1675,13 @@ public: caller_(), callee_() { - typedef detail::pull_coroutine_object< - void, Fn, push_coroutine< void, StackAllocator > - > object_t; - stack_alloc_.allocate( stack_ctx_, attr.size); - callee_ = detail::coroutine_context( detail::trampoline< Fn, object_t >, - & stack_ctx_); - detail::setup< Fn, object_t > to( forward< Fn >( fn), - & caller_, & callee_, attr); + callee_ = detail::coroutine_context( + detail::trampoline< + Fn, base_t, push_coroutine< void, StackAllocator > + >, + & stack_ctx_); + detail::setup< Fn, base_t > to( fn, & caller_, & callee_, attr); impl_ = reinterpret_cast< base_t * >( caller_.jump( callee_, @@ -1865,15 +1864,14 @@ push_coroutine< void, StackAllocator >::push_coroutine( coroutine_fn fn, caller_(), callee_() { - typedef detail::push_coroutine_object< - void, coroutine_fn, pull_coroutine< void, StackAllocator > - > object_t; - stack_alloc_.allocate( stack_ctx_, attr.size); - callee_ = detail::coroutine_context( detail::trampoline< coroutine_fn, object_t >, - & stack_ctx_); - detail::setup< coroutine_fn, object_t > to( forward< coroutine_fn >( fn), - & caller_, & callee_, attr); + callee_ = detail::coroutine_context( + detail::trampoline< + coroutine_fn, base_t, pull_corotuine< void, StackAllocator > + >, + & stack_ctx_); + detail::setup< coroutine_fn, base_t > to( forward< coroutine_fn >( fn), + & caller_, & callee_, attr); impl_ = reinterpret_cast< base_t * >( caller_.jump( callee_, @@ -1892,15 +1890,14 @@ push_coroutine< void, StackAllocator >::push_coroutine( coroutine_fn fn, caller_(), callee_() { - typedef detail::push_coroutine_object< - void, coroutine_fn, pull_coroutine< void, StackAllocator > - > object_t; - stack_alloc_.allocate( stack_ctx_, attr.size); - callee_ = detail::coroutine_context( detail::trampoline< coroutine_fn, object_t >, - & stack_ctx_); - detail::setup< coroutine_fn, object_t > to( forward< coroutine_fn >( fn), - & caller_, & callee_, attr); + callee_ = detail::coroutine_context( + detail::trampoline< + coroutine_fn, base_t, pull_corotuine< void, StackAllocator > + >, + & stack_ctx_); + detail::setup< coroutine_fn, base_t > to( forward< coroutine_fn >( fn), + & caller_, & callee_, attr); impl_ = reinterpret_cast< base_t * >( caller_.jump( callee_, @@ -2029,15 +2026,14 @@ push_coroutine< void, StackAllocator >::push_coroutine( BOOST_RV_REF( Fn) fn, caller_(), callee_() { - typedef detail::push_coroutine_object< - void, Fn, pull_coroutine< void, StackAllocator > - > object_t; - stack_alloc_.allocate( stack_ctx_, attr.size); - callee_ = detail::coroutine_context( detail::trampoline< Fn, object_t >, - & stack_ctx_); - detail::setup< Fn, object_t > to( forward< Fn >( fn), - & caller_, & callee_, attr); + callee_ = detail::coroutine_context( + detail::trampoline< + Fn, base_t, pull_coroutine< void, StackAllocator > + >, + & stack_ctx_); + detail::setup< Fn, base_t > to( forward< Fn >( fn), + & caller_, & callee_, attr); impl_ = reinterpret_cast< base_t * >( caller_.jump( callee_, @@ -2057,15 +2053,14 @@ push_coroutine< void, StackAllocator >::push_coroutine( BOOST_RV_REF( Fn) fn, caller_(), callee_() { - typedef detail::push_coroutine_object< - void, Fn, pull_coroutine< void, StackAllocator > - > object_t; - stack_alloc_.allocate( stack_ctx_, attr.size); - callee_ = detail::coroutine_context( detail::trampoline< Fn, object_t >, - & stack_ctx_); - detail::setup< Fn, object_t > to( forward< Fn >( fn), - & caller_, & callee_, attr); + callee_ = detail::coroutine_context( + detail::trampoline< + Fn, base_t, pull_coroutine< void, StackAllocator > + >, + & stack_ctx_); + detail::setup< Fn, base_t > to( forward< Fn >( fn), + & caller_, & callee_, attr); impl_ = reinterpret_cast< base_t * >( caller_.jump( callee_, @@ -2210,14 +2205,13 @@ push_coroutine< void, StackAllocator >::push_coroutine( Fn fn, caller_(), callee_() { - typedef detail::push_coroutine_object< - void, Fn, pull_coroutine< void, StackAllocator > - > object_t; - stack_alloc_.allocate( stack_ctx_, attr.size); - callee_ = detail::coroutine_context( detail::trampoline< Fn, object_t >, - & stack_ctx_); - detail::setup< Fn, object_t > to( fn, & caller_, & callee_, attr); + callee_ = detail::coroutine_context( + detail::trampoline_void< + Fn, base_t, pull_coroutine< void, StackAllocator > + >, + & stack_ctx_); + detail::setup< Fn, base_t > to( fn, & caller_, & callee_, attr); impl_ = reinterpret_cast< base_t * >( caller_.jump( callee_, @@ -2241,14 +2235,13 @@ push_coroutine< void, StackAllocator >::push_coroutine( Fn fn, caller_(), callee_() { - typedef detail::push_coroutine_object< - void, Fn, pull_coroutine< void, StackAllocator > - > object_t; - stack_alloc_.allocate( stack_ctx_, attr.size); - callee_ = detail::coroutine_context( detail::trampoline< Fn, object_t >, - & stack_ctx_); - detail::setup< Fn, object_t > to( fn, & caller_, & callee_, attr); + callee_ = detail::coroutine_context( + detail::trampoline_void< + Fn, base_t, pull_coroutine< void, StackAllocator > + >, + & stack_ctx_); + detail::setup< Fn, base_t > to( fn, & caller_, & callee_, attr); impl_ = reinterpret_cast< base_t * >( caller_.jump( callee_, @@ -2393,14 +2386,13 @@ push_coroutine< void, StackAllocator >::push_coroutine( BOOST_RV_REF( Fn) fn, caller_(), callee_() { - typedef detail::push_coroutine_object< - void, Fn, pull_coroutine< void, StackAllocator > - > object_t; - stack_alloc_.allocate( stack_ctx_, attr.size); - callee_ = detail::coroutine_context( detail::trampoline< Fn, object_t >, - & stack_ctx_); - detail::setup< Fn, object_t > to( fn, & caller_, & callee_, attr); + callee_ = detail::coroutine_context( + detail::trampoline< + Fn, base_t, pull_coroutine< void, StackAllocator > + >, + & stack_ctx_); + detail::setup< Fn, base_t > to( fn, & caller_, & callee_, attr); impl_ = reinterpret_cast< base_t * >( caller_.jump( callee_, @@ -2424,14 +2416,13 @@ push_coroutine< void, StackAllocator >::push_coroutine( BOOST_RV_REF( Fn) fn, caller_(), callee_() { - typedef detail::push_coroutine_object< - void, Fn, pull_coroutine< void, StackAllocator > - > object_t; - stack_alloc_.allocate( stack_ctx_, attr.size); - callee_ = detail::coroutine_context( detail::trampoline< Fn, object_t >, - & stack_ctx_); - detail::setup< Fn, object_t > to( fn, & caller_, & callee_, attr); + callee_ = detail::coroutine_context( + detail::trampoline< + Fn, base_t, pull_coroutine< void, StackAllocator > + >, + & stack_ctx_); + detail::setup< Fn, base_t > to( fn, & caller_, & callee_, attr); impl_ = reinterpret_cast< base_t * >( caller_.jump( callee_, diff --git a/include/boost/coroutine/detail/pull_coroutine_base.hpp b/include/boost/coroutine/detail/pull_coroutine_base.hpp index cbfbc1f..0d96c2d 100644 --- a/include/boost/coroutine/detail/pull_coroutine_base.hpp +++ b/include/boost/coroutine/detail/pull_coroutine_base.hpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #ifdef BOOST_HAS_ABI_HEADERS @@ -262,10 +263,13 @@ class pull_coroutine_base< void > : private noncopyable private: template< typename X, typename Y, typename Z > friend class push_coroutine_object; + template< + typename X, typename Y, typename Z + > + friend void trampoline_void( intptr_t); - typedef parameters< void > param_type; + typedef parameters< void > param_type; -protected: int flags_; exception_ptr except_; coroutine_context * caller_; diff --git a/include/boost/coroutine/detail/pull_coroutine_object.hpp b/include/boost/coroutine/detail/pull_coroutine_object.hpp index 6d4c16d..b1245ef 100644 --- a/include/boost/coroutine/detail/pull_coroutine_object.hpp +++ b/include/boost/coroutine/detail/pull_coroutine_object.hpp @@ -178,77 +178,6 @@ public: } }; -template< typename Fn, typename Caller > -class pull_coroutine_object< void, Fn, Caller > : public pull_coroutine_base< void > -{ -private: - typedef pull_coroutine_base< void > base_type; - typedef parameters< void > param_type; - - Fn fn_; - - pull_coroutine_object( pull_coroutine_object &); - pull_coroutine_object & operator=( pull_coroutine_object const&); - -public: -#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES - pull_coroutine_object( BOOST_RV_REF( Fn) fn, attributes const& attr, - coroutine_context * caller, - coroutine_context * callee) : - base_type( caller, callee, - stack_unwind == attr.do_unwind, - fpu_preserved == attr.preserve_fpu), - fn_( forward< Fn >( fn) ) - {} -#else - pull_coroutine_object( Fn fn, attributes const& attr, - coroutine_context * caller, - coroutine_context * callee) : - base_type( caller, callee, - stack_unwind == attr.do_unwind, - fpu_preserved == attr.preserve_fpu), - fn_( fn) - {} - - pull_coroutine_object( BOOST_RV_REF( Fn) fn, attributes const& attr, - coroutine_context * caller, - coroutine_context * callee) : - base_type( caller, callee, - stack_unwind == attr.do_unwind, - fpu_preserved == attr.preserve_fpu), - fn_( fn) - {} -#endif - - void run() - { - { - this->callee_->jump( - * this->caller_, - reinterpret_cast< intptr_t >( this), - this->preserve_fpu() ); - - // create push_coroutine - typename Caller::base_t b( this->callee_, this->caller_, false, this->preserve_fpu() ); - Caller c( & b); - try - { fn_( c); } - catch ( forced_unwind const&) - {} - catch (...) - { this->except_ = current_exception(); } - } - - this->flags_ |= flag_complete; - param_type to; - this->callee_->jump( - * this->caller_, - reinterpret_cast< intptr_t >( & to), - this->preserve_fpu() ); - BOOST_ASSERT_MSG( false, "pull_coroutine is complete"); - } -}; - }}} #ifdef BOOST_HAS_ABI_HEADERS diff --git a/include/boost/coroutine/detail/push_coroutine_base.hpp b/include/boost/coroutine/detail/push_coroutine_base.hpp index e7574a9..767dba0 100644 --- a/include/boost/coroutine/detail/push_coroutine_base.hpp +++ b/include/boost/coroutine/detail/push_coroutine_base.hpp @@ -207,10 +207,13 @@ private: typename X, typename Y, typename Z > friend class pull_coroutine_object; + template< + typename X, typename Y, typename Z + > + friend void trampoline_void( intptr_t); typedef parameters< void > param_type; -protected: int flags_; exception_ptr except_; coroutine_context * caller_; diff --git a/include/boost/coroutine/detail/push_coroutine_object.hpp b/include/boost/coroutine/detail/push_coroutine_object.hpp index a296eb8..76d6199 100644 --- a/include/boost/coroutine/detail/push_coroutine_object.hpp +++ b/include/boost/coroutine/detail/push_coroutine_object.hpp @@ -179,77 +179,6 @@ public: } }; -template< typename Fn, typename Caller > -class push_coroutine_object< void, Fn, Caller > : public push_coroutine_base< void > -{ -private: - typedef push_coroutine_base< void > base_type; - typedef parameters< void > param_type; - - Fn fn_; - - push_coroutine_object( push_coroutine_object &); - push_coroutine_object & operator=( push_coroutine_object const&); - -public: -#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES - push_coroutine_object( BOOST_RV_REF( Fn) fn, attributes const& attr, - coroutine_context * caller, - coroutine_context * callee) : - base_type( caller, callee, - stack_unwind == attr.do_unwind, - fpu_preserved == attr.preserve_fpu), - fn_( forward< Fn >( fn) ) - {} -#else - push_coroutine_object( Fn fn, attributes const& attr, - coroutine_context * caller, - coroutine_context * callee) : - base_type( caller, callee, - stack_unwind == attr.do_unwind, - fpu_preserved == attr.preserve_fpu), - fn_( fn) - {} - - push_coroutine_object( BOOST_RV_REF( Fn) fn, attributes const& attr, - coroutine_context * caller, - coroutine_context * callee) : - base_type( caller, callee, - stack_unwind == attr.do_unwind, - fpu_preserved == attr.preserve_fpu), - fn_( fn) - {} -#endif - - void run() - { - { - this->callee_->jump( - * this->caller_, - reinterpret_cast< intptr_t >( this), - this->preserve_fpu() ); - - // create pull_coroutine - typename Caller::base_t b( this->callee_, this->caller_, false, this->preserve_fpu() ); - Caller c( & b); - try - { fn_( c); } - catch ( forced_unwind const&) - {} - catch (...) - { this->except_ = current_exception(); } - } - - this->flags_ |= flag_complete; - param_type to; - this->callee_->jump( - * this->caller_, - reinterpret_cast< intptr_t >( & to), - this->preserve_fpu() ); - BOOST_ASSERT_MSG( false, "push_coroutine is complete"); - } -}; - }}} #ifdef BOOST_HAS_ABI_HEADERS diff --git a/include/boost/coroutine/detail/trampoline.hpp b/include/boost/coroutine/detail/trampoline.hpp index c0c97d1..b0b72ee 100644 --- a/include/boost/coroutine/detail/trampoline.hpp +++ b/include/boost/coroutine/detail/trampoline.hpp @@ -12,10 +12,16 @@ #include #include #include +#include #include -#include +#include +#include +#include #include +#include +#include +#include #ifdef BOOST_HAS_ABI_HEADERS # include BOOST_ABI_PREFIX @@ -38,9 +44,56 @@ void trampoline( intptr_t vp) #else Coro c( move( from->fn), from->attr, from->caller, from->callee); #endif + from = 0; c.run(); } +template< typename Fn, typename Coro, typename Self > +void trampoline_void( intptr_t vp) +{ + typedef parameters< void > param_type; + + BOOST_ASSERT( vp); + + setup< Fn, Coro > * from( + reinterpret_cast< setup< Fn, Coro > * >( vp) ); + +#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES + Fn fn( forward< Fn >( from->fn) ); +#else + Fn fn( move( from->fn) ); +#endif + Coro c( from->caller, from->callee, + stack_unwind == from->attr.do_unwind, + fpu_preserved == from->attr.preserve_fpu); + from = 0; + + { + c.callee_->jump( + * c.caller_, + reinterpret_cast< intptr_t >( & c), + c.preserve_fpu() ); + + // create push_coroutine + typename Self::base_t b( c.callee_, c.caller_, false, c.preserve_fpu() ); + Self self( & b); + try + { fn( self); } + catch ( forced_unwind const&) + {} + catch (...) + { c.except_ = current_exception(); } + } + + c.flags_ |= flag_complete; + param_type to; + c.callee_->jump( + * c.caller_, + reinterpret_cast< intptr_t >( & to), + c.preserve_fpu() ); + BOOST_ASSERT_MSG( false, "pull_coroutine is complete"); +} + }}} #ifdef BOOST_HAS_ABI_HEADERS