mirror of
https://github.com/boostorg/context.git
synced 2026-01-19 04:02:17 +00:00
return fiber from resume()/resume_with()
This commit is contained in:
@@ -34,12 +34,12 @@ int main( int argc, char * argv[]) {
|
||||
std::cout << i;
|
||||
echoSSE( i);
|
||||
std::cout << " ";
|
||||
f.resume();
|
||||
f = f.resume();
|
||||
}
|
||||
return std::move( f);
|
||||
}};
|
||||
for (; i < 11; ++i) {
|
||||
f.resume();
|
||||
f = f.resume();
|
||||
}
|
||||
std::cout << "\nmain: done" << std::endl;
|
||||
return EXIT_SUCCESS;
|
||||
|
||||
@@ -14,7 +14,7 @@ namespace ctx = boost::context;
|
||||
ctx::fiber bar( ctx::fiber && f) {
|
||||
do {
|
||||
std::cout << "bar\n";
|
||||
f.resume();
|
||||
f = f.resume();
|
||||
} while ( f);
|
||||
return std::move( f);
|
||||
}
|
||||
@@ -23,7 +23,7 @@ int main() {
|
||||
ctx::fiber f{ bar };
|
||||
do {
|
||||
std::cout << "foo\n";
|
||||
f.resume();
|
||||
f = f.resume();
|
||||
} while ( f);
|
||||
std::cout << "main: done" << std::endl;
|
||||
return EXIT_SUCCESS;
|
||||
|
||||
@@ -19,7 +19,7 @@ int main() {
|
||||
a=0;
|
||||
int b=1;
|
||||
for(;;){
|
||||
f.resume();
|
||||
f = f.resume();
|
||||
int next=a+b;
|
||||
a=b;
|
||||
b=next;
|
||||
@@ -27,7 +27,7 @@ int main() {
|
||||
return std::move( f);
|
||||
}};
|
||||
for ( int j = 0; j < 10; ++j) {
|
||||
f.resume();
|
||||
f = f.resume();
|
||||
std::cout << a << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
@@ -17,14 +17,14 @@ int main() {
|
||||
[&data](ctx::fiber && f){
|
||||
std::cout << "entered first time: " << data << std::endl;
|
||||
data += 2;
|
||||
f.resume();
|
||||
f = f.resume();
|
||||
std::cout << "entered second time: " << data << std::endl;
|
||||
return std::move( f);
|
||||
}};
|
||||
f.resume();
|
||||
f = f.resume();
|
||||
std::cout << "returned first time: " << data << std::endl;
|
||||
data += 2;
|
||||
f.resume();
|
||||
f = f.resume();
|
||||
if ( f) {
|
||||
std::cout << "returned second time: " << data << std::endl;
|
||||
} else {
|
||||
|
||||
@@ -44,15 +44,15 @@ int main() {
|
||||
[&data](ctx::fiber && f){
|
||||
std::cout << "entered first time: " << data.value << std::endl;
|
||||
data = std::move( moveable{ 3 });
|
||||
f.resume();
|
||||
f = f.resume();
|
||||
std::cout << "entered second time: " << data.value << std::endl;
|
||||
data = std::move( moveable{});
|
||||
return std::move( f);
|
||||
}};
|
||||
f.resume();
|
||||
f = f.resume();
|
||||
std::cout << "returned first time: " << data.value << std::endl;
|
||||
data.value = 5;
|
||||
f.resume();
|
||||
f = f.resume();
|
||||
std::cout << "returned second time: " << data.value << std::endl;
|
||||
std::cout << "main: done" << std::endl;
|
||||
return EXIT_SUCCESS;
|
||||
|
||||
@@ -13,16 +13,16 @@ namespace ctx = boost::context;
|
||||
|
||||
ctx::fiber f1( ctx::fiber && f) {
|
||||
std::cout << "f1: entered first time" << std::endl;
|
||||
f.resume();
|
||||
f = f.resume();
|
||||
std::cout << "f1: entered second time" << std::endl;
|
||||
return std::move( f);
|
||||
}
|
||||
|
||||
int main() {
|
||||
ctx::fiber f{ f1 };
|
||||
f.resume();
|
||||
f = f.resume();
|
||||
std::cout << "f1: returned first time" << std::endl;
|
||||
f.resume();
|
||||
f = f.resume();
|
||||
std::cout << "f1: returned second time" << std::endl;
|
||||
std::cout << "main: done" << std::endl;
|
||||
return EXIT_SUCCESS;
|
||||
|
||||
@@ -17,20 +17,20 @@ int main() {
|
||||
ctx::fiber f{ [&data](ctx::fiber && f) {
|
||||
std::cout << "f1: entered first time: " << data << std::endl;
|
||||
data += 1;
|
||||
f.resume();
|
||||
f = f.resume();
|
||||
std::cout << "f1: entered second time: " << data << std::endl;
|
||||
data += 1;
|
||||
f.resume();
|
||||
f = f.resume();
|
||||
std::cout << "f1: entered third time: " << data << std::endl;
|
||||
return std::move( f);
|
||||
}};
|
||||
f.resume();
|
||||
f = f.resume();
|
||||
std::cout << "f1: returned first time: " << data << std::endl;
|
||||
data += 1;
|
||||
f.resume();
|
||||
f = f.resume();
|
||||
std::cout << "f1: returned second time: " << data << std::endl;
|
||||
data += 1;
|
||||
f.resume_with([&data](ctx::fiber && f){
|
||||
f = f.resume_with([&data](ctx::fiber && f){
|
||||
std::cout << "f2: entered: " << data << std::endl;
|
||||
data = -1;
|
||||
return std::move( f);
|
||||
|
||||
@@ -14,9 +14,9 @@ namespace ctx = boost::context;
|
||||
|
||||
ctx::fiber f1( ctx::fiber && f) {
|
||||
std::cout << "f1: entered first time" << std::endl;
|
||||
f.resume();
|
||||
f = f.resume();
|
||||
std::cout << "f1: entered second time" << std::endl;
|
||||
f.resume();
|
||||
f = f.resume();
|
||||
std::cout << "f1: entered third time" << std::endl;
|
||||
return std::move( f);
|
||||
}
|
||||
@@ -28,11 +28,11 @@ ctx::fiber f2( ctx::fiber && f) {
|
||||
|
||||
int main() {
|
||||
ctx::fiber f{ f1 };
|
||||
f.resume();
|
||||
f = f.resume();
|
||||
std::cout << "f1: returned first time" << std::endl;
|
||||
f.resume();
|
||||
f = f.resume();
|
||||
std::cout << "f1: returned second time" << std::endl;
|
||||
f.resume_with( f2);
|
||||
f = f.resume_with( f2);
|
||||
std::cout << "f1: returned third time" << std::endl;
|
||||
|
||||
std::cout << "main: done" << std::endl;
|
||||
|
||||
@@ -105,7 +105,7 @@ int main() {
|
||||
[&sink,&c](char c_){
|
||||
// resume main execution context
|
||||
c = c_;
|
||||
sink.resume();
|
||||
sink = sink.resume();
|
||||
});
|
||||
// start recursive parsing
|
||||
p.run();
|
||||
@@ -114,10 +114,10 @@ int main() {
|
||||
// resume main execution context
|
||||
return std::move(sink);
|
||||
}};
|
||||
source.resume();
|
||||
source = source.resume();
|
||||
while(!done){
|
||||
printf("Parsed: %c\n",c);
|
||||
source.resume();
|
||||
source = source.resume();
|
||||
}
|
||||
std::cout << "main: done" << std::endl;
|
||||
return EXIT_SUCCESS;
|
||||
|
||||
@@ -27,7 +27,7 @@ int main() {
|
||||
for (;;) {
|
||||
try {
|
||||
std::cout << "entered" << std::endl;
|
||||
f.resume();
|
||||
f = f.resume();
|
||||
} catch ( my_exception & ex) {
|
||||
std::cerr << "my_exception: " << ex.what() << std::endl;
|
||||
return std::move( ex.f);
|
||||
@@ -35,8 +35,8 @@ int main() {
|
||||
}
|
||||
return std::move( f);
|
||||
}};
|
||||
f.resume();
|
||||
f.resume_with([](ctx::fiber && f){
|
||||
f = f.resume();
|
||||
f = f.resume_with([](ctx::fiber && f){
|
||||
throw my_exception(std::move( f), "abc");
|
||||
return std::move( f);
|
||||
});
|
||||
|
||||
@@ -275,29 +275,29 @@ public:
|
||||
fiber( fiber const& other) noexcept = delete;
|
||||
fiber & operator=( fiber const& other) noexcept = delete;
|
||||
|
||||
void resume() {
|
||||
fiber resume() {
|
||||
BOOST_ASSERT( nullptr != fctx_);
|
||||
fctx_ = detail::jump_fcontext(
|
||||
return fiber{ detail::jump_fcontext(
|
||||
#if defined(BOOST_NO_CXX14_STD_EXCHANGE)
|
||||
detail::exchange( fctx_, nullptr),
|
||||
#else
|
||||
std::exchange( fctx_, nullptr),
|
||||
#endif
|
||||
nullptr).fctx;
|
||||
nullptr).fctx };
|
||||
}
|
||||
|
||||
template< typename Fn >
|
||||
void resume_with( Fn && fn) {
|
||||
fiber resume_with( Fn && fn) {
|
||||
BOOST_ASSERT( nullptr != fctx_);
|
||||
auto p = std::make_tuple( std::forward< Fn >( fn) );
|
||||
fctx_ = detail::ontop_fcontext(
|
||||
return fiber{ detail::ontop_fcontext(
|
||||
#if defined(BOOST_NO_CXX14_STD_EXCHANGE)
|
||||
detail::exchange( fctx_, nullptr),
|
||||
#else
|
||||
std::exchange( fctx_, nullptr),
|
||||
#endif
|
||||
& p,
|
||||
detail::context_ontop< fiber, Fn >).fctx;
|
||||
detail::context_ontop< fiber, Fn >).fctx };
|
||||
}
|
||||
|
||||
explicit operator bool() const noexcept {
|
||||
|
||||
@@ -432,7 +432,7 @@ public:
|
||||
return * this;
|
||||
}
|
||||
|
||||
void resume() {
|
||||
fiber resume() {
|
||||
BOOST_ASSERT( nullptr != ptr_);
|
||||
#if defined(BOOST_NO_CXX14_STD_EXCHANGE)
|
||||
detail::activation_record * ptr = detail::exchange( ptr_, nullptr)->resume();
|
||||
@@ -445,11 +445,11 @@ public:
|
||||
ptr = detail::activation_record::current()->ontop( ptr);
|
||||
detail::activation_record::current()->ontop = nullptr;
|
||||
}
|
||||
ptr_ = ptr;
|
||||
return fiber{ ptr };
|
||||
}
|
||||
|
||||
template< typename Fn >
|
||||
void resume_with( Fn && fn) {
|
||||
fiber resume_with( Fn && fn) {
|
||||
BOOST_ASSERT( nullptr != ptr_);
|
||||
#if defined(BOOST_NO_CXX14_STD_EXCHANGE)
|
||||
detail::activation_record * ptr =
|
||||
@@ -464,7 +464,7 @@ public:
|
||||
ptr = detail::activation_record::current()->ontop( ptr);
|
||||
detail::activation_record::current()->ontop = nullptr;
|
||||
}
|
||||
ptr_ = ptr;
|
||||
return fiber{ ptr };
|
||||
}
|
||||
|
||||
explicit operator bool() const noexcept {
|
||||
|
||||
@@ -353,7 +353,7 @@ public:
|
||||
return * this;
|
||||
}
|
||||
|
||||
void resume() {
|
||||
fiber resume() {
|
||||
BOOST_ASSERT( nullptr != ptr_);
|
||||
#if defined(BOOST_NO_CXX14_STD_EXCHANGE)
|
||||
detail::activation_record * ptr = detail::exchange( ptr_, nullptr)->resume();
|
||||
@@ -366,11 +366,11 @@ public:
|
||||
ptr = detail::activation_record::current()->ontop( ptr);
|
||||
detail::activation_record::current()->ontop = nullptr;
|
||||
}
|
||||
ptr_ = ptr;
|
||||
return fiber{ ptr };
|
||||
}
|
||||
|
||||
template< typename Fn >
|
||||
void resume_with( Fn && fn) {
|
||||
fiber resume_with( Fn && fn) {
|
||||
BOOST_ASSERT( nullptr != ptr_);
|
||||
#if defined(BOOST_NO_CXX14_STD_EXCHANGE)
|
||||
detail::activation_record * ptr =
|
||||
@@ -385,7 +385,7 @@ public:
|
||||
ptr = detail::activation_record::current()->ontop( ptr);
|
||||
detail::activation_record::current()->ontop = nullptr;
|
||||
}
|
||||
ptr_ = ptr;
|
||||
return fiber{ ptr };
|
||||
}
|
||||
|
||||
explicit operator bool() const noexcept {
|
||||
|
||||
@@ -134,11 +134,11 @@ void test_move() {
|
||||
ctx::fiber f1{
|
||||
[&i](ctx::fiber && f) {
|
||||
value1 = i;
|
||||
f.resume();
|
||||
f = f.resume();
|
||||
value1 = i;
|
||||
return std::move( f);
|
||||
}};
|
||||
f1.resume();
|
||||
f1 = f1.resume();
|
||||
BOOST_CHECK_EQUAL( 1, value1);
|
||||
BOOST_CHECK( f1);
|
||||
ctx::fiber f2;
|
||||
@@ -147,7 +147,7 @@ void test_move() {
|
||||
BOOST_CHECK( ! f1);
|
||||
BOOST_CHECK( f2);
|
||||
i = 3;
|
||||
f2.resume();
|
||||
f2 = f2.resume();
|
||||
BOOST_CHECK_EQUAL( 3, value1);
|
||||
BOOST_CHECK( ! f1);
|
||||
BOOST_CHECK( ! f2);
|
||||
@@ -157,7 +157,7 @@ void test_bind() {
|
||||
value1 = 0;
|
||||
X x;
|
||||
ctx::fiber f{ std::bind( & X::foo, x, std::placeholders::_1, 7) };
|
||||
f.resume();
|
||||
f = f.resume();
|
||||
BOOST_CHECK_EQUAL( 7, value1);
|
||||
}
|
||||
|
||||
@@ -173,7 +173,7 @@ void test_exception() {
|
||||
}
|
||||
return std::move( f);
|
||||
}};
|
||||
f.resume();
|
||||
f = f.resume();
|
||||
BOOST_CHECK_EQUAL( std::string( what), value2);
|
||||
BOOST_CHECK( ! f);
|
||||
}
|
||||
@@ -182,12 +182,12 @@ void test_exception() {
|
||||
bool catched = false;
|
||||
std::thread([&catched](){
|
||||
ctx::fiber f{ [&catched](ctx::fiber && f){
|
||||
f.resume();
|
||||
f = f.resume();
|
||||
seh( catched);
|
||||
return std::move( f);
|
||||
}};
|
||||
BOOST_CHECK( f);
|
||||
f.resume();
|
||||
f = f.resume();
|
||||
}).join();
|
||||
BOOST_CHECK( catched);
|
||||
}
|
||||
@@ -203,7 +203,7 @@ void test_fp() {
|
||||
value3 = d;
|
||||
return std::move( f);
|
||||
}};
|
||||
f.resume();
|
||||
f = f.resume();
|
||||
BOOST_CHECK_EQUAL( 10.58, value3);
|
||||
BOOST_CHECK( ! f);
|
||||
}
|
||||
@@ -218,11 +218,11 @@ void test_stacked() {
|
||||
value1 = 3;
|
||||
return std::move( f);
|
||||
}};
|
||||
f1.resume();
|
||||
f1 = f1.resume();
|
||||
value3 = 3.14;
|
||||
return std::move( f);
|
||||
}};
|
||||
f.resume();
|
||||
f = f.resume();
|
||||
BOOST_CHECK_EQUAL( 3, value1);
|
||||
BOOST_CHECK_EQUAL( 3.14, value3);
|
||||
BOOST_CHECK( ! f);
|
||||
@@ -241,7 +241,7 @@ void test_prealloc() {
|
||||
value1 = i;
|
||||
return std::move( f);
|
||||
}};
|
||||
f.resume();
|
||||
f = f.resume();
|
||||
BOOST_CHECK_EQUAL( 7, value1);
|
||||
BOOST_CHECK( ! f);
|
||||
}
|
||||
@@ -252,12 +252,12 @@ void test_ontop() {
|
||||
ctx::fiber f{ [&i](ctx::fiber && f) {
|
||||
for (;;) {
|
||||
i *= 10;
|
||||
f.resume();
|
||||
f = f.resume();
|
||||
}
|
||||
return std::move( f);
|
||||
}};
|
||||
f.resume();
|
||||
f.resume_with(
|
||||
f = f.resume();
|
||||
f = f.resume_with(
|
||||
[&i](ctx::fiber && f){
|
||||
i -= 10;
|
||||
return std::move( f);
|
||||
@@ -268,12 +268,12 @@ void test_ontop() {
|
||||
{
|
||||
ctx::fiber f1;
|
||||
ctx::fiber f{ [&f1](ctx::fiber && f) {
|
||||
f.resume();
|
||||
f = f.resume();
|
||||
BOOST_CHECK( ! f);
|
||||
return std::move( f1);
|
||||
}};
|
||||
f.resume();
|
||||
f.resume_with(
|
||||
f = f.resume();
|
||||
f = f.resume_with(
|
||||
[&f1](ctx::fiber && f){
|
||||
f1 = std::move( f);
|
||||
return std::move( f);
|
||||
@@ -288,7 +288,7 @@ void test_ontop_exception() {
|
||||
for (;;) {
|
||||
value1 = 3;
|
||||
try {
|
||||
f.resume();
|
||||
f = f.resume();
|
||||
} catch ( my_exception & ex) {
|
||||
value2 = ex.what();
|
||||
return std::move( ex.f);
|
||||
@@ -296,10 +296,10 @@ void test_ontop_exception() {
|
||||
}
|
||||
return std::move( f);
|
||||
}};
|
||||
f.resume();
|
||||
f = f.resume();
|
||||
BOOST_CHECK_EQUAL( 3, value1);
|
||||
const char * what = "hello world";
|
||||
f.resume_with(
|
||||
f = f.resume_with(
|
||||
[what](ctx::fiber && f){
|
||||
throw my_exception( std::move( f), what);
|
||||
return std::move( f);
|
||||
@@ -314,10 +314,10 @@ void test_termination() {
|
||||
ctx::fiber f{
|
||||
[](ctx::fiber && f){
|
||||
Y y;
|
||||
f.resume();
|
||||
f = f.resume();
|
||||
return std::move(f);
|
||||
}};
|
||||
f.resume();
|
||||
f = f.resume();
|
||||
BOOST_CHECK_EQUAL( 3, value1);
|
||||
}
|
||||
BOOST_CHECK_EQUAL( 7, value1);
|
||||
@@ -329,7 +329,7 @@ void test_termination() {
|
||||
value1 = 3;
|
||||
return std::move( f);
|
||||
}};
|
||||
f.resume();
|
||||
f = f.resume();
|
||||
BOOST_CHECK_EQUAL( 3, value1);
|
||||
BOOST_CHECK( ! f);
|
||||
}
|
||||
@@ -340,16 +340,16 @@ void test_termination() {
|
||||
ctx::fiber f{
|
||||
[&i](ctx::fiber && f){
|
||||
value1 = i;
|
||||
f.resume();
|
||||
f = f.resume();
|
||||
value1 = i;
|
||||
return std::move( f);
|
||||
}};
|
||||
f.resume();
|
||||
f = f.resume();
|
||||
BOOST_CHECK( f);
|
||||
BOOST_CHECK_EQUAL( i, value1);
|
||||
BOOST_CHECK( f);
|
||||
i = 7;
|
||||
f.resume();
|
||||
f = f.resume();
|
||||
BOOST_CHECK( ! f);
|
||||
BOOST_CHECK_EQUAL( i, value1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user