diff --git a/include/boost/fiber/context.hpp b/include/boost/fiber/context.hpp index 134bf84a..5e8ba290 100644 --- a/include/boost/fiber/context.hpp +++ b/include/boost/fiber/context.hpp @@ -166,7 +166,7 @@ private: boost::context::execution_context ctx_; public: - detail::worker_hook worker_hook_; + detail::worker_hook worker_hook_; detail::terminated_hook terminated_hook_; detail::ready_hook ready_hook_; detail::remote_ready_hook remote_ready_hook_; @@ -265,10 +265,16 @@ public: ctx_( std::allocator_arg, palloc, salloc, // mutable: generated operator() is not const -> enables std::move( fn) // std::make_tuple: stores decayed copies of its args, implicitly unwraps std::reference_wrapper - [=,fn=std::forward< Fn >( fn),tpl=std::make_tuple( std::forward< Args >( args) ...)] () mutable -> void { + [=,fn_=std::forward< Fn >( fn),tpl=std::make_tuple( std::forward< Args >( args) ...), + ctx=boost::context::execution_context::current()] () mutable -> void { try { - // invoke fiber function - boost::context::detail::invoke_helper( std::move( fn), std::move( tpl) ); + typename std::decay< Fn >::type fn( std::move( fn_) ); + // jump back after initialization + ctx(); + // check for unwinding + if ( ! unwinding_requested() ) { + boost::context::detail::invoke_helper( fn, std::move( tpl) ); + } } catch ( fiber_interrupted const&) { } catch ( forced_unwind const&) { } catch ( ... ) { @@ -292,6 +298,8 @@ public: fss_data_(), wait_queue_(), splk_() { + // switch for initialization + ctx_(); } virtual ~context();