Merge branch 'develop'

This commit is contained in:
Oliver Kowalke 2015-09-21 17:31:59 +02:00
commit dea936b9fc
7 changed files with 122 additions and 140 deletions

View File

@ -47,7 +47,3 @@ exe parser
exe segmented
: segmented.cpp
;
exe chained
: chained.cpp
;

View File

@ -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 <cstdlib>
#include <iostream>
#include <boost/coroutine2/all.hpp>
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;
}

View File

@ -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;

View File

@ -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);
}

View File

@ -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;

View File

@ -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);
}

View File

@ -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;
}