Skip to content

Commit

Permalink
fix unreachable attr (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
kelbon authored Sep 29, 2024
1 parent 12be8c3 commit be345e8
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 28 deletions.
2 changes: 1 addition & 1 deletion include/kelcoro/algorithm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ template <size_t I, typename T, typename Ctx, typename... Ts>
job job_for_when_any(task<T, Ctx> child, std::weak_ptr<when_any_state<Ts...>> state) {
// stop at entry and give when_any do its preparations
co_await std::suspend_always{};
if (!state.lock()) {
if (state.expired()) {
// someone sets result while we was starting without awaiting
co_return;
}
Expand Down
12 changes: 4 additions & 8 deletions include/kelcoro/noexport/macro.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@

#define KELCORO_CO_AWAIT_REQUIRED [[nodiscard("forget co_await?")]]

#ifdef NDEBUG
#if defined(__GNUC__) || defined(__clang__)
#define KELCORO_UNREACHABLE __builtin_unreachable()
#elif defined(_MSC_VER)
#define KELCORO_UNREACHABLE __assume(false)
#else
#define KELCORO_UNREACHABLE assert(false)
#endif
#if defined(__GNUC__) || defined(__clang__)
#define KELCORO_UNREACHABLE __builtin_unreachable()
#elif defined(_MSC_VER)
#define KELCORO_UNREACHABLE __assume(false)
#else
#define KELCORO_UNREACHABLE assert(false)
#endif
Expand Down
13 changes: 3 additions & 10 deletions include/kelcoro/task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,9 @@ struct null_context {
template <typename TaskPromise>
static void on_start(std::coroutine_handle<TaskPromise>) noexcept {
}
// invoked when task successfully ended
// invoked when task ended with/without exception
template <typename TaskPromise>
static void on_end_success(std::coroutine_handle<TaskPromise>) noexcept {
}
// invoked when task ended with exception
// Note: by default, exception when no one waits is ignored, but this function may check it and
// std::terminate or smth like this (check .who_waits == nullptr)
template <typename TaskPromise>
static void on_end_failure(std::coroutine_handle<TaskPromise>, const std::exception_ptr&) noexcept {
static void on_end(std::coroutine_handle<TaskPromise>) noexcept {
}
};

Expand Down Expand Up @@ -69,10 +63,9 @@ struct task_promise : return_block<Result> {
}
void unhandled_exception() noexcept {
exception = std::current_exception();
ctx.on_end_failure(self_handle(), exception);
}
auto final_suspend() noexcept {
ctx.on_end_success(self_handle());
ctx.on_end(self_handle());
return transfer_control_to{who_waits};
}
};
Expand Down
10 changes: 1 addition & 9 deletions tests/test_coroutines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -584,15 +584,7 @@ struct ctx {
s->push_back(name);
}
template <typename TaskPromise>
void on_end_success(std::coroutine_handle<TaskPromise>) noexcept {
if (s && !s->empty())
s->pop_back();
}
// invoked when task ended with exception
// Note: by default, exception when no one waits is ignored, but this function may check it and
// std::terminate or smth like this (check .who_waits == nullptr)
template <typename TaskPromise>
void on_end_failure(std::coroutine_handle<TaskPromise>, const std::exception_ptr&) noexcept {
void on_end(std::coroutine_handle<TaskPromise>) noexcept {
if (s && !s->empty())
s->pop_back();
}
Expand Down

0 comments on commit be345e8

Please sign in to comment.