Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions include/boost/context/fiber_fcontext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ namespace context {
namespace detail {

// manage_exception_state is a dummy struct unless we have specific support
struct manage_exception_state {};
struct manage_exception_state {
void from_forced_unwind() noexcept {}
};

} // namespace detail
} // namespace context
Expand All @@ -90,6 +92,11 @@ class manage_exception_state {
BOOST_NOINLINE manage_exception_state() {
exception_state_ = *__cxa_get_globals();
}
// Hack to account for the forced_unwind exception thrown in fiber_unwind
// that's run ontop before the destructor.
void from_forced_unwind() noexcept {
exception_state_.uncaughtExceptions += 1;
}
BOOST_NOINLINE ~manage_exception_state() {
*__cxa_get_globals() = exception_state_;
}
Expand Down Expand Up @@ -376,13 +383,18 @@ class fiber {
BOOST_ASSERT( nullptr != fctx_);
detail::manage_exception_state exstate;
boost::ignore_unused(exstate);
return { detail::jump_fcontext(
try {
return { detail::jump_fcontext(
#if defined(BOOST_NO_CXX14_STD_EXCHANGE)
detail::exchange( fctx_, nullptr),
detail::exchange( fctx_, nullptr),
#else
std::exchange( fctx_, nullptr),
std::exchange( fctx_, nullptr),
#endif
nullptr).fctx };
nullptr).fctx };
} catch ( detail::forced_unwind const& ) {
exstate.from_forced_unwind();
throw;
}
}

template< typename Fn >
Expand All @@ -391,14 +403,19 @@ class fiber {
detail::manage_exception_state exstate;
boost::ignore_unused(exstate);
auto p = std::forward< Fn >( fn);
return { detail::ontop_fcontext(
try {
return { detail::ontop_fcontext(
#if defined(BOOST_NO_CXX14_STD_EXCHANGE)
detail::exchange( fctx_, nullptr),
detail::exchange( fctx_, nullptr),
#else
std::exchange( fctx_, nullptr),
std::exchange( fctx_, nullptr),
#endif
& p,
detail::fiber_ontop< fiber, decltype(p) >).fctx };
& p,
detail::fiber_ontop< fiber, decltype(p) >).fctx };
} catch ( detail::forced_unwind const& ) {
exstate.from_forced_unwind();
Comment on lines +407 to +416

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess if an exception is thrown on the switched-to context it would also need to be handled here - and that's probably in-scope. Happy to tackle this (maybe in a follow-up), but I've started by putting out the immediate fire under our feet in nix.

throw;
}
}

explicit operator bool() const noexcept {
Expand Down
24 changes: 24 additions & 0 deletions test/test_fiber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,29 @@ void test_badcatch() {
#endif
}

void test_uncaught_exceptions() {
int i = 42;
{
ctx::fiber f{
[&i](ctx::fiber && f ) {
struct scope_test {
int &i_;
scope_test( int &i) :
i_(i) {
}
~scope_test() {
i_ = std::uncaught_exceptions();
}
};
scope_test scope( i);
return std::move( f).resume();
i = 84;
}};
f = std::move(f).resume();
}
BOOST_CHECK_EQUAL( i, 1);
}

int main()
{
test_move();
Expand All @@ -529,6 +552,7 @@ int main()
#endif
test_goodcatch();
test_badcatch();
test_uncaught_exceptions();

return boost::report_errors();
}
Expand Down
Loading