From 3219de5e0f2981d61dc08e17fe8ff81498a6fbcc Mon Sep 17 00:00:00 2001 From: Oliver Kowalke Date: Mon, 21 Sep 2015 17:30:46 +0200 Subject: [PATCH 1/3] remove example chained.cpp --- example/Jamfile.v2 | 4 --- example/chained.cpp | 59 --------------------------------------------- 2 files changed, 63 deletions(-) delete mode 100644 example/chained.cpp diff --git a/example/Jamfile.v2 b/example/Jamfile.v2 index 1389d61..5c5599d 100644 --- a/example/Jamfile.v2 +++ b/example/Jamfile.v2 @@ -47,7 +47,3 @@ exe parser exe segmented : segmented.cpp ; - -exe chained - : chained.cpp - ; diff --git a/example/chained.cpp b/example/chained.cpp deleted file mode 100644 index 14e3f0f..0000000 --- a/example/chained.cpp +++ /dev/null @@ -1,59 +0,0 @@ - -// Copyright Oliver Kowalke 2014. -// 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) - -#include -#include - -#include - -template< typename coroutine > -struct test { - using pull_type = typename coroutine::pull_type; - using push_type = typename coroutine::push_type; - - pull_type * child = nullptr; - - void start_child_coroutine() { - child = new pull_type([](push_type & yield) { - std::cout << "2"; - yield(); - std::cout << "2"; - yield(); - std::cout << "2"; - yield(); - std::cout << "2"; - yield(); - std::cout << "2"; - yield(); - std::cout << "2"; - }); - } - - pull_type start_parent_coroutine() { - return pull_type([=](push_type & yield) { - std::cout << "1"; - start_child_coroutine(); - yield(); - std::cout << "1"; - }); - } - - test() { - auto parent = start_parent_coroutine(); - while ( * child) { - ( * child)(); - } - std::cout << std::endl; - } -}; - - -int main() { - test< boost::coroutines2::coroutine< void > > t; - std::cout << "Done" << std::endl; - - return EXIT_SUCCESS; -} From 105ad9c10d415e838fa2f92fe93f31c9a1009f11 Mon Sep 17 00:00:00 2001 From: Oliver Kowalke Date: Mon, 21 Sep 2015 17:31:10 +0200 Subject: [PATCH 2/3] enhance tests --- test/test_coroutine.cpp | 45 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/test/test_coroutine.cpp b/test/test_coroutine.cpp index 7a13a81..bc25135 100644 --- a/test/test_coroutine.cpp +++ b/test/test_coroutine.cpp @@ -570,6 +570,50 @@ void test_no_result() BOOST_CHECK( ! coro); } +std::vector< int > vec; +coro::coroutine< void >::pull_type * child = nullptr; + +void start_child_coroutine() { + child = new coro::coroutine< void >::pull_type([](coro::coroutine< void >::push_type & yield) { + vec.push_back( 2); + yield(); + vec.push_back( 2); + yield(); + vec.push_back( 2); + yield(); + vec.push_back( 2); + yield(); + vec.push_back( 2); + yield(); + vec.push_back( 2); + }); +} + +coro::coroutine< void >::pull_type start_parent_coroutine() { + return coro::coroutine< void >::pull_type([=](coro::coroutine< void >::push_type & yield) { + vec.push_back( 1); + start_child_coroutine(); + yield(); + vec.push_back( 1); + }); +} + +void test_chaining() +{ + auto parent = start_parent_coroutine(); + while ( * child) { + ( * child)(); + } + BOOST_CHECK_EQUAL( 7, vec.size() ); + BOOST_CHECK_EQUAL( 1, vec[0]); + BOOST_CHECK_EQUAL( 2, vec[1]); + BOOST_CHECK_EQUAL( 2, vec[2]); + BOOST_CHECK_EQUAL( 2, vec[3]); + BOOST_CHECK_EQUAL( 2, vec[4]); + BOOST_CHECK_EQUAL( 2, vec[5]); + BOOST_CHECK_EQUAL( 2, vec[6]); +} + boost::unit_test::test_suite * init_unit_test_suite( int, char* []) { boost::unit_test::test_suite * test = @@ -593,6 +637,7 @@ boost::unit_test::test_suite * init_unit_test_suite( int, char* []) test->add( BOOST_TEST_CASE( & test_exceptions) ); test->add( BOOST_TEST_CASE( & test_input_iterator) ); test->add( BOOST_TEST_CASE( & test_output_iterator) ); + test->add( BOOST_TEST_CASE( & test_chaining) ); return test; } From b5e475ff36c62ad06fb09b8638df08f1f6fcf978 Mon Sep 17 00:00:00 2001 From: Oliver Kowalke Date: Mon, 21 Sep 2015 17:31:24 +0200 Subject: [PATCH 3/3] rename callee and caller --- .../coroutine2/detail/pull_control_block.hpp | 6 +- .../coroutine2/detail/pull_control_block.ipp | 66 ++++++++-------- .../coroutine2/detail/push_control_block.hpp | 6 +- .../coroutine2/detail/push_control_block.ipp | 76 +++++++++---------- 4 files changed, 77 insertions(+), 77 deletions(-) diff --git a/include/boost/coroutine2/detail/pull_control_block.hpp b/include/boost/coroutine2/detail/pull_control_block.hpp index ee2644c..58cf10b 100644 --- a/include/boost/coroutine2/detail/pull_control_block.hpp +++ b/include/boost/coroutine2/detail/pull_control_block.hpp @@ -23,7 +23,7 @@ namespace detail { template< typename T > struct pull_coroutine< T >::control_block { typename push_coroutine< T >::control_block * other; - boost::context::execution_context callee; + boost::context::execution_context ctx; bool preserve_fpu; int state; std::exception_ptr except; @@ -46,7 +46,7 @@ struct pull_coroutine< T >::control_block { template< typename T > struct pull_coroutine< T & >::control_block { typename push_coroutine< T & >::control_block * other; - boost::context::execution_context callee; + boost::context::execution_context ctx; bool preserve_fpu; int state; std::exception_ptr except; @@ -68,7 +68,7 @@ struct pull_coroutine< T & >::control_block { struct pull_coroutine< void >::control_block { push_coroutine< void >::control_block * other; - boost::context::execution_context callee; + boost::context::execution_context ctx; bool preserve_fpu; int state; std::exception_ptr except; diff --git a/include/boost/coroutine2/detail/pull_control_block.ipp b/include/boost/coroutine2/detail/pull_control_block.ipp index ecef2aa..ac4cdbc 100644 --- a/include/boost/coroutine2/detail/pull_control_block.ipp +++ b/include/boost/coroutine2/detail/pull_control_block.ipp @@ -33,10 +33,10 @@ template< typename StackAllocator, typename Fn > pull_coroutine< T >::control_block::control_block( context::preallocated palloc, StackAllocator salloc, Fn && fn_, bool preserve_fpu_) : other( nullptr), - callee( palloc, salloc, - [=,fn=std::forward< Fn >( fn_),caller=boost::context::execution_context::current()] () mutable -> void { + ctx( palloc, salloc, + [=,fn=std::forward< Fn >( fn_),ctx=boost::context::execution_context::current()] () mutable -> void { // create synthesized push_coroutine< T > - typename push_coroutine< T >::control_block synthesized_cb( this, caller); + typename push_coroutine< T >::control_block synthesized_cb( this, ctx); push_coroutine< T > synthesized( & synthesized_cb); other = & synthesized_cb; try { @@ -50,22 +50,22 @@ pull_coroutine< T >::control_block::control_block( context::preallocated palloc, } // set termination flags state |= static_cast< int >( state_t::complete); - // jump back to caller - other->callee( preserve_fpu); + // jump back to ctx + other->ctx( preserve_fpu); BOOST_ASSERT_MSG( false, "pull_coroutine is complete"); }), preserve_fpu( preserve_fpu_), state( static_cast< int >( state_t::unwind) ), except() { // enter coroutine-fn in order to have first value available after ctor returns - callee( preserve_fpu); + ctx( preserve_fpu); } template< typename T > pull_coroutine< T >::control_block::control_block( typename push_coroutine< T >::control_block * cb, - boost::context::execution_context const& caller) : + boost::context::execution_context const& ctx_) : other( cb), - callee( caller), + ctx( ctx_), preserve_fpu( other->preserve_fpu), state( 0), except() { @@ -77,15 +77,15 @@ pull_coroutine< T >::control_block::~control_block() { 0 != ( state & static_cast< int >( state_t::unwind) ) ) { // set early-exit flag state |= static_cast< int >( state_t::early_exit); - callee( preserve_fpu); + ctx( preserve_fpu); } } template< typename T > void pull_coroutine< T >::control_block::resume() { - other->callee = boost::context::execution_context::current(); - callee( preserve_fpu); + other->ctx = boost::context::execution_context::current(); + ctx( preserve_fpu); if ( except) { std::rethrow_exception( except); } @@ -109,10 +109,10 @@ template< typename StackAllocator, typename Fn > pull_coroutine< T & >::control_block::control_block( context::preallocated palloc, StackAllocator salloc, Fn && fn_, bool preserve_fpu_) : other( nullptr), - callee( palloc, salloc, - [=,fn=std::forward< Fn >( fn_),caller=boost::context::execution_context::current()] () mutable -> void { + ctx( palloc, salloc, + [=,fn=std::forward< Fn >( fn_),ctx=boost::context::execution_context::current()] () mutable -> void { // create synthesized push_coroutine< T > - typename push_coroutine< T & >::control_block synthesized_cb( this, caller); + typename push_coroutine< T & >::control_block synthesized_cb( this, ctx); push_coroutine< T & > synthesized( & synthesized_cb); other = & synthesized_cb; try { @@ -126,22 +126,22 @@ pull_coroutine< T & >::control_block::control_block( context::preallocated pallo } // set termination flags state |= static_cast< int >( state_t::complete); - // jump back to caller - other->callee( preserve_fpu); + // jump back to ctx + other->ctx( preserve_fpu); BOOST_ASSERT_MSG( false, "pull_coroutine is complete"); }), preserve_fpu( preserve_fpu_), state( static_cast< int >( state_t::unwind) ), except() { // enter coroutine-fn in order to have first value available after ctor returns - callee( preserve_fpu); + ctx( preserve_fpu); } template< typename T > pull_coroutine< T & >::control_block::control_block( typename push_coroutine< T & >::control_block * cb, - boost::context::execution_context const& caller) : + boost::context::execution_context const& ctx_) : other( cb), - callee( caller), + ctx( ctx_), preserve_fpu( other->preserve_fpu), state( 0), except() { @@ -153,15 +153,15 @@ pull_coroutine< T & >::control_block::~control_block() { 0 != ( state & static_cast< int >( state_t::unwind) ) ) { // set early-exit flag state |= static_cast< int >( state_t::early_exit); - callee( preserve_fpu); + ctx( preserve_fpu); } } template< typename T > void pull_coroutine< T & >::control_block::resume() { - other->callee = boost::context::execution_context::current(); - callee( preserve_fpu); + other->ctx = boost::context::execution_context::current(); + ctx( preserve_fpu); if ( except) { std::rethrow_exception( except); } @@ -184,10 +184,10 @@ template< typename StackAllocator, typename Fn > pull_coroutine< void >::control_block::control_block( context::preallocated palloc, StackAllocator salloc, Fn && fn_, bool preserve_fpu_) : other( nullptr), - callee( palloc, salloc, - [=,fn=std::forward< Fn >( fn_),caller=boost::context::execution_context::current()] () mutable -> void { + ctx( palloc, salloc, + [=,fn=std::forward< Fn >( fn_),ctx=boost::context::execution_context::current()] () mutable -> void { // create synthesized push_coroutine< T > - typename push_coroutine< void >::control_block synthesized_cb( this, caller); + typename push_coroutine< void >::control_block synthesized_cb( this, ctx); push_coroutine< void > synthesized( & synthesized_cb); other = & synthesized_cb; try { @@ -201,22 +201,22 @@ pull_coroutine< void >::control_block::control_block( context::preallocated pall } // set termination flags state |= static_cast< int >( state_t::complete); - // jump back to caller - other->callee( preserve_fpu); + // jump back to ctx + other->ctx( preserve_fpu); BOOST_ASSERT_MSG( false, "pull_coroutine is complete"); }), preserve_fpu( preserve_fpu_), state( static_cast< int >( state_t::unwind) ), except() { // enter coroutine-fn in order to have first value available after ctor returns - callee( preserve_fpu); + ctx( preserve_fpu); } inline pull_coroutine< void >::control_block::control_block( push_coroutine< void >::control_block * cb, - boost::context::execution_context const& caller) : + boost::context::execution_context const& ctx_) : other( cb), - callee( caller), + ctx( ctx_), preserve_fpu( other->preserve_fpu), state( 0), except() { @@ -228,15 +228,15 @@ pull_coroutine< void >::control_block::~control_block() { 0 != ( state & static_cast< int >( state_t::unwind) ) ) { // set early-exit flag state |= static_cast< int >( state_t::early_exit); - callee( preserve_fpu); + ctx( preserve_fpu); } } inline void pull_coroutine< void >::control_block::resume() { - other->callee = boost::context::execution_context::current(); - callee( preserve_fpu); + other->ctx = boost::context::execution_context::current(); + ctx( preserve_fpu); if ( except) { std::rethrow_exception( except); } diff --git a/include/boost/coroutine2/detail/push_control_block.hpp b/include/boost/coroutine2/detail/push_control_block.hpp index b4cf4e4..631b5e7 100644 --- a/include/boost/coroutine2/detail/push_control_block.hpp +++ b/include/boost/coroutine2/detail/push_control_block.hpp @@ -23,7 +23,7 @@ namespace detail { template< typename T > struct push_coroutine< T >::control_block { typename pull_coroutine< T >::control_block * other; - boost::context::execution_context callee; + boost::context::execution_context ctx; bool preserve_fpu; int state; std::exception_ptr except; @@ -49,7 +49,7 @@ struct push_coroutine< T >::control_block { template< typename T > struct push_coroutine< T & >::control_block { typename pull_coroutine< T & >::control_block * other; - boost::context::execution_context callee; + boost::context::execution_context ctx; bool preserve_fpu; int state; std::exception_ptr except; @@ -72,7 +72,7 @@ struct push_coroutine< T & >::control_block { struct push_coroutine< void >::control_block { pull_coroutine< void >::control_block * other; - boost::context::execution_context callee; + boost::context::execution_context ctx; bool preserve_fpu; int state; std::exception_ptr except; diff --git a/include/boost/coroutine2/detail/push_control_block.ipp b/include/boost/coroutine2/detail/push_control_block.ipp index 0f0bee4..ded634b 100644 --- a/include/boost/coroutine2/detail/push_control_block.ipp +++ b/include/boost/coroutine2/detail/push_control_block.ipp @@ -34,14 +34,14 @@ template< typename StackAllocator, typename Fn > push_coroutine< T >::control_block::control_block( context::preallocated palloc, StackAllocator salloc, Fn && fn_, bool preserve_fpu_) : other( nullptr), - callee( palloc, salloc, - [=,fn=std::forward< Fn >( fn_),caller=boost::context::execution_context::current()] () mutable -> void { + ctx( palloc, salloc, + [=,fn=std::forward< Fn >( fn_),ctx=boost::context::execution_context::current()] () mutable -> void { // create synthesized pull_coroutine< T > - typename pull_coroutine< T >::control_block synthesized_cb( this, caller); + typename pull_coroutine< T >::control_block synthesized_cb( this, ctx); pull_coroutine< T > synthesized( & synthesized_cb); other = & synthesized_cb; // jump back to ctor - caller( preserve_fpu); + ctx( preserve_fpu); try { // call coroutine-fn with synthesized pull_coroutine as argument fn( synthesized); @@ -53,8 +53,8 @@ push_coroutine< T >::control_block::control_block( context::preallocated palloc, } // set termination flags state |= static_cast< int >( state_t::complete); - // jump back to caller - other->callee( preserve_fpu); + // jump back to ctx + other->ctx( preserve_fpu); BOOST_ASSERT_MSG( false, "push_coroutine is complete"); }), preserve_fpu( preserve_fpu_), @@ -62,14 +62,14 @@ push_coroutine< T >::control_block::control_block( context::preallocated palloc, except(), t( nullptr) { // enter coroutine-fn in order to get other set - callee( preserve_fpu); + ctx( preserve_fpu); } template< typename T > push_coroutine< T >::control_block::control_block( typename pull_coroutine< T >::control_block * cb, - boost::context::execution_context const& caller) : + boost::context::execution_context const& ctx_) : other( cb), - callee( caller), + ctx( ctx_), preserve_fpu( other->preserve_fpu), state( 0), except(), @@ -82,7 +82,7 @@ push_coroutine< T >::control_block::~control_block() { 0 != ( state & static_cast< int >( state_t::unwind) ) ) { // set early-exit flag state |= static_cast< int >( state_t::early_exit); - callee( preserve_fpu); + ctx( preserve_fpu); } } @@ -93,8 +93,8 @@ push_coroutine< T >::control_block::resume( T const& t_) { // pass an pointer (address of tmp) to other context T tmp( t_); t = & tmp; - other->callee = boost::context::execution_context::current(); - callee( preserve_fpu); + other->ctx = boost::context::execution_context::current(); + ctx( preserve_fpu); t = nullptr; if ( except) { std::rethrow_exception( except); @@ -112,8 +112,8 @@ push_coroutine< T >::control_block::resume( T && t_) { // pass an pointer (address of tmp) to other context T tmp( std::move( t_) ); t = & tmp; - other->callee = boost::context::execution_context::current(); - callee( preserve_fpu); + other->ctx = boost::context::execution_context::current(); + ctx( preserve_fpu); t = nullptr; if ( except) { std::rethrow_exception( except); @@ -138,14 +138,14 @@ template< typename StackAllocator, typename Fn > push_coroutine< T & >::control_block::control_block( context::preallocated palloc, StackAllocator salloc, Fn && fn_, bool preserve_fpu_) : other( nullptr), - callee( palloc, salloc, - [=,fn=std::forward< Fn >( fn_),caller=boost::context::execution_context::current()] () mutable -> void { + ctx( palloc, salloc, + [=,fn=std::forward< Fn >( fn_),ctx=boost::context::execution_context::current()] () mutable -> void { // create synthesized pull_coroutine< T > - typename pull_coroutine< T & >::control_block synthesized_cb( this, caller); + typename pull_coroutine< T & >::control_block synthesized_cb( this, ctx); pull_coroutine< T & > synthesized( & synthesized_cb); other = & synthesized_cb; // jump back to ctor - caller( preserve_fpu); + ctx( preserve_fpu); try { // call coroutine-fn with synthesized pull_coroutine as argument fn( synthesized); @@ -157,8 +157,8 @@ push_coroutine< T & >::control_block::control_block( context::preallocated pallo } // set termination flags state |= static_cast< int >( state_t::complete); - // jump back to caller - other->callee( preserve_fpu); + // jump back to ctx + other->ctx( preserve_fpu); BOOST_ASSERT_MSG( false, "push_coroutine is complete"); }), preserve_fpu( preserve_fpu_), @@ -166,14 +166,14 @@ push_coroutine< T & >::control_block::control_block( context::preallocated pallo except(), t( nullptr) { // enter coroutine-fn in order to get other set - callee( preserve_fpu); + ctx( preserve_fpu); } template< typename T > push_coroutine< T & >::control_block::control_block( typename pull_coroutine< T & >::control_block * cb, - boost::context::execution_context const& caller) : + boost::context::execution_context const& ctx_) : other( cb), - callee( caller), + ctx( ctx_), preserve_fpu( other->preserve_fpu), state( 0), except(), @@ -186,7 +186,7 @@ push_coroutine< T & >::control_block::~control_block() { 0 != ( state & static_cast< int >( state_t::unwind) ) ) { // set early-exit flag state |= static_cast< int >( state_t::early_exit); - callee( preserve_fpu); + ctx( preserve_fpu); } } @@ -194,8 +194,8 @@ template< typename T > void push_coroutine< T & >::control_block::resume( T & t_) { t = & t_; - other->callee = boost::context::execution_context::current(); - callee( preserve_fpu); + other->ctx = boost::context::execution_context::current(); + ctx( preserve_fpu); t = nullptr; if ( except) { std::rethrow_exception( except); @@ -218,14 +218,14 @@ push_coroutine< T & >::control_block::valid() const noexcept { template< typename StackAllocator, typename Fn > push_coroutine< void >::control_block::control_block( context::preallocated palloc, StackAllocator salloc, Fn && fn_, bool preserve_fpu_) : other( nullptr), - callee( palloc, salloc, - [=,fn=std::forward< Fn >( fn_),caller=boost::context::execution_context::current()] () mutable -> void { + ctx( palloc, salloc, + [=,fn=std::forward< Fn >( fn_),ctx=boost::context::execution_context::current()] () mutable -> void { // create synthesized pull_coroutine< T > - typename pull_coroutine< void >::control_block synthesized_cb( this, caller); + typename pull_coroutine< void >::control_block synthesized_cb( this, ctx); pull_coroutine< void > synthesized( & synthesized_cb); other = & synthesized_cb; // jump back to ctor - caller( preserve_fpu); + ctx( preserve_fpu); try { // call coroutine-fn with synthesized pull_coroutine as argument fn( synthesized); @@ -237,22 +237,22 @@ push_coroutine< void >::control_block::control_block( context::preallocated pall } // set termination flags state |= static_cast< int >( state_t::complete); - // jump back to caller - other->callee( preserve_fpu); + // jump back to ctx + other->ctx( preserve_fpu); BOOST_ASSERT_MSG( false, "push_coroutine is complete"); }), preserve_fpu( preserve_fpu_), state( static_cast< int >( state_t::unwind) ), except() { // enter coroutine-fn in order to get other set - callee( preserve_fpu); + ctx( preserve_fpu); } inline push_coroutine< void >::control_block::control_block( pull_coroutine< void >::control_block * cb, - boost::context::execution_context const& caller) : + boost::context::execution_context const& ctx_) : other( cb), - callee( caller), + ctx( ctx_), preserve_fpu( other->preserve_fpu), state( 0), except() { @@ -264,15 +264,15 @@ push_coroutine< void >::control_block::~control_block() { 0 != ( state & static_cast< int >( state_t::unwind) ) ) { // set early-exit flag state |= static_cast< int >( state_t::early_exit); - callee( preserve_fpu); + ctx( preserve_fpu); } } inline void push_coroutine< void >::control_block::resume() { - other->callee = boost::context::execution_context::current(); - callee( preserve_fpu); + other->ctx = boost::context::execution_context::current(); + ctx( preserve_fpu); if ( except) { std::rethrow_exception( except); }