diff --git a/examples/Jamfile.v2 b/examples/Jamfile.v2 index b6f504be..44ef72fe 100644 --- a/examples/Jamfile.v2 +++ b/examples/Jamfile.v2 @@ -30,7 +30,6 @@ exe adapt_method_calls : adapt_method_calls.cpp ; exe adapt_nonblocking : adapt_nonblocking.cpp ; exe barrier : barrier.cpp ; exe future : future.cpp ; -exe interrupt : interrupt.cpp ; exe join : join.cpp ; exe ping_pong : ping_pong.cpp ; exe priority : priority.cpp ; diff --git a/examples/interrupt.cpp b/examples/interrupt.cpp deleted file mode 100644 index 01464874..00000000 --- a/examples/interrupt.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#include -#include -#include - -#include - -struct condition_test_data -{ - condition_test_data() : notified(0), awoken(0) { } - - boost::fibers::mutex mutex; - boost::fibers::condition condition; - int notified; - int awoken; -}; - -void condition_test_fiber(condition_test_data* data) -{ - std::unique_lock lock(data->mutex); - while (!(data->notified > 0)) - data->condition.wait(lock); - data->awoken++; -} - -int main() -{ - condition_test_data data; - boost::fibers::fiber f( & condition_test_fiber, & data); - - f.interrupt(); - try - { - f.join(); - } - catch ( boost::fibers::fiber_interrupted const&) - { std::cerr << "interrupted" << std::endl; } - catch ( std::exception const& e) - { std::cerr << "exception: " << e.what() << std::endl; } - catch (...) - { std::cerr << "unhandled exception" << std::endl; } - - std::cout << "done." << std::endl; - - return EXIT_SUCCESS; -} diff --git a/include/boost/fiber/context.hpp b/include/boost/fiber/context.hpp index 5cd6790a..461c5a79 100644 --- a/include/boost/fiber/context.hpp +++ b/include/boost/fiber/context.hpp @@ -252,8 +252,6 @@ public: static context * active() noexcept; - static void active( context * active) noexcept; - static void reset_active() noexcept; // main fiber context @@ -320,7 +318,7 @@ public: id get_id() const noexcept; - std::function< void() > * resume( std::function< void() > *); + void resume( std::function< void() > *); void suspend( std::function< void() > * = nullptr) noexcept; diff --git a/src/context.cpp b/src/context.cpp index c5613c5b..1f91bebb 100644 --- a/src/context.cpp +++ b/src/context.cpp @@ -141,12 +141,6 @@ context::active() noexcept { return active_; } -void -context::active( context * active) noexcept { - BOOST_ASSERT( nullptr != active); - active_ = active; -} - void context::reset_active() noexcept { active_ = nullptr; @@ -220,9 +214,16 @@ context::get_id() const noexcept { return id( const_cast< context * >( this) ); } -std::function< void() > * +void context::resume( std::function< void() > * func) { - return static_cast< std::function< void() > * >( ctx_( func) ); + context * prev = this; + // active_ will point to `this` + // prev will point to previous active context + std::swap( active_, prev); + func = static_cast< std::function< void() > * >( ctx_( func) ); + if ( nullptr != func) { + ( * func)(); + } } void diff --git a/src/scheduler.cpp b/src/scheduler.cpp index 5dd1e1a6..15e91b63 100644 --- a/src/scheduler.cpp +++ b/src/scheduler.cpp @@ -31,14 +31,9 @@ scheduler::resume_( context * active_ctx, context * ctx, std::function< void() > BOOST_ASSERT( this == ctx->get_scheduler() ); BOOST_ASSERT( active_ctx->get_scheduler() == ctx->get_scheduler() ); BOOST_ASSERT( active_ctx != ctx); - // assign new fiber to active-fiber - context::active( ctx); // resume active-fiber == ctx - func = ctx->resume( func); + ctx->resume( func); BOOST_ASSERT( context::active() == active_ctx); - if ( nullptr != func) { - ( * func)(); - } if ( active_ctx->unwinding_requested() ) { throw forced_unwind(); }