mirror of
https://github.com/boostorg/coroutine2.git
synced 2025-05-09 23:24:01 +00:00
Merge branch 'develop'
This commit is contained in:
commit
bf4014a0c4
@ -47,3 +47,7 @@ exe parser
|
||||
exe segmented
|
||||
: segmented.cpp
|
||||
;
|
||||
|
||||
exe chained
|
||||
: chained.cpp
|
||||
;
|
||||
|
70
example/chained.cpp
Normal file
70
example/chained.cpp
Normal file
@ -0,0 +1,70 @@
|
||||
|
||||
// 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
|
||||
(
|
||||
[this](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>> t2;
|
||||
std::cout << "Done" << std::endl;
|
||||
}
|
@ -52,12 +52,13 @@ pull_coroutine< T >::control_block::control_block( context::preallocated palloc,
|
||||
// set termination flags
|
||||
state |= static_cast< int >( state_t::complete);
|
||||
// jump back to caller
|
||||
caller( preserve_fpu);
|
||||
other->callee( 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);
|
||||
}
|
||||
|
||||
@ -84,7 +85,7 @@ pull_coroutine< T >::control_block::~control_block() {
|
||||
template< typename T >
|
||||
void
|
||||
pull_coroutine< T >::control_block::resume() {
|
||||
caller = boost::context::execution_context::current();
|
||||
other->callee = boost::context::execution_context::current();
|
||||
callee( preserve_fpu);
|
||||
if ( except) {
|
||||
std::rethrow_exception( except);
|
||||
@ -128,12 +129,13 @@ pull_coroutine< T & >::control_block::control_block( context::preallocated pallo
|
||||
// set termination flags
|
||||
state |= static_cast< int >( state_t::complete);
|
||||
// jump back to caller
|
||||
caller( preserve_fpu);
|
||||
other->callee( 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);
|
||||
}
|
||||
|
||||
@ -160,7 +162,7 @@ pull_coroutine< T & >::control_block::~control_block() {
|
||||
template< typename T >
|
||||
void
|
||||
pull_coroutine< T & >::control_block::resume() {
|
||||
caller = boost::context::execution_context::current();
|
||||
other->callee = boost::context::execution_context::current();
|
||||
callee( preserve_fpu);
|
||||
if ( except) {
|
||||
std::rethrow_exception( except);
|
||||
@ -203,12 +205,13 @@ pull_coroutine< void >::control_block::control_block( context::preallocated pall
|
||||
// set termination flags
|
||||
state |= static_cast< int >( state_t::complete);
|
||||
// jump back to caller
|
||||
caller( preserve_fpu);
|
||||
other->callee( 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);
|
||||
}
|
||||
|
||||
@ -235,7 +238,7 @@ pull_coroutine< void >::control_block::~control_block() {
|
||||
inline
|
||||
void
|
||||
pull_coroutine< void >::control_block::resume() {
|
||||
caller = boost::context::execution_context::current();
|
||||
other->callee = boost::context::execution_context::current();
|
||||
callee( preserve_fpu);
|
||||
if ( except) {
|
||||
std::rethrow_exception( except);
|
||||
|
@ -41,6 +41,8 @@ push_coroutine< T >::control_block::control_block( context::preallocated palloc,
|
||||
typename pull_coroutine< T >::control_block synthesized_cb( this);
|
||||
pull_coroutine< T > synthesized( & synthesized_cb);
|
||||
other = & synthesized_cb;
|
||||
// jump back to ctor
|
||||
caller( preserve_fpu);
|
||||
try {
|
||||
// call coroutine-fn with synthesized pull_coroutine as argument
|
||||
fn( synthesized);
|
||||
@ -52,13 +54,16 @@ push_coroutine< T >::control_block::control_block( context::preallocated palloc,
|
||||
}
|
||||
// set termination flags
|
||||
state |= static_cast< int >( state_t::complete);
|
||||
caller( preserve_fpu);
|
||||
// jump back to caller
|
||||
other->callee( preserve_fpu);
|
||||
BOOST_ASSERT_MSG( false, "push_coroutine is complete");
|
||||
}),
|
||||
preserve_fpu( preserve_fpu_),
|
||||
state( static_cast< int >( state_t::unwind) ),
|
||||
except(),
|
||||
t( nullptr) {
|
||||
// enter coroutine-fn in order to get other set
|
||||
callee( preserve_fpu);
|
||||
}
|
||||
|
||||
template< typename T >
|
||||
@ -89,7 +94,7 @@ push_coroutine< T >::control_block::resume( T const& t_) {
|
||||
// pass an pointer (address of tmp) to other context
|
||||
T tmp( t_);
|
||||
t = & tmp;
|
||||
caller = boost::context::execution_context::current();
|
||||
other->callee = boost::context::execution_context::current();
|
||||
callee( preserve_fpu);
|
||||
t = nullptr;
|
||||
if ( except) {
|
||||
@ -108,7 +113,7 @@ push_coroutine< T >::control_block::resume( T && t_) {
|
||||
// pass an pointer (address of tmp) to other context
|
||||
T tmp( std::move( t_) );
|
||||
t = & tmp;
|
||||
caller = boost::context::execution_context::current();
|
||||
other->callee = boost::context::execution_context::current();
|
||||
callee( preserve_fpu);
|
||||
t = nullptr;
|
||||
if ( except) {
|
||||
@ -141,6 +146,8 @@ push_coroutine< T & >::control_block::control_block( context::preallocated pallo
|
||||
typename pull_coroutine< T & >::control_block synthesized_cb( this);
|
||||
pull_coroutine< T & > synthesized( & synthesized_cb);
|
||||
other = & synthesized_cb;
|
||||
// jump back to ctor
|
||||
caller( preserve_fpu);
|
||||
try {
|
||||
// call coroutine-fn with synthesized pull_coroutine as argument
|
||||
fn( synthesized);
|
||||
@ -152,13 +159,16 @@ push_coroutine< T & >::control_block::control_block( context::preallocated pallo
|
||||
}
|
||||
// set termination flags
|
||||
state |= static_cast< int >( state_t::complete);
|
||||
caller( preserve_fpu);
|
||||
// jump back to caller
|
||||
other->callee( preserve_fpu);
|
||||
BOOST_ASSERT_MSG( false, "push_coroutine is complete");
|
||||
}),
|
||||
preserve_fpu( preserve_fpu_),
|
||||
state( static_cast< int >( state_t::unwind) ),
|
||||
except(),
|
||||
t( nullptr) {
|
||||
// enter coroutine-fn in order to get other set
|
||||
callee( preserve_fpu);
|
||||
}
|
||||
|
||||
template< typename T >
|
||||
@ -186,7 +196,7 @@ template< typename T >
|
||||
void
|
||||
push_coroutine< T & >::control_block::resume( T & t_) {
|
||||
t = & t_;
|
||||
caller = boost::context::execution_context::current();
|
||||
other->callee = boost::context::execution_context::current();
|
||||
callee( preserve_fpu);
|
||||
t = nullptr;
|
||||
if ( except) {
|
||||
@ -217,6 +227,8 @@ push_coroutine< void >::control_block::control_block( context::preallocated pall
|
||||
typename pull_coroutine< void >::control_block synthesized_cb( this);
|
||||
pull_coroutine< void > synthesized( & synthesized_cb);
|
||||
other = & synthesized_cb;
|
||||
// jump back to ctor
|
||||
caller( preserve_fpu);
|
||||
try {
|
||||
// call coroutine-fn with synthesized pull_coroutine as argument
|
||||
fn( synthesized);
|
||||
@ -228,12 +240,15 @@ push_coroutine< void >::control_block::control_block( context::preallocated pall
|
||||
}
|
||||
// set termination flags
|
||||
state |= static_cast< int >( state_t::complete);
|
||||
caller( preserve_fpu);
|
||||
// jump back to caller
|
||||
other->callee( 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);
|
||||
}
|
||||
|
||||
inline
|
||||
@ -259,7 +274,7 @@ push_coroutine< void >::control_block::~control_block() {
|
||||
inline
|
||||
void
|
||||
push_coroutine< void >::control_block::resume() {
|
||||
caller = boost::context::execution_context::current();
|
||||
other->callee = boost::context::execution_context::current();
|
||||
callee( preserve_fpu);
|
||||
if ( except) {
|
||||
std::rethrow_exception( except);
|
||||
|
Loading…
x
Reference in New Issue
Block a user