Skip to content

Comments

start towards supporting an awaiter#34

Merged
dietmarkuehl merged 8 commits intomainfrom
as_awaitable
Jul 6, 2025
Merged

start towards supporting an awaiter#34
dietmarkuehl merged 8 commits intomainfrom
as_awaitable

Conversation

@dietmarkuehl
Copy link
Member

No description provided.

bool& flag;
void set_value() && noexcept { flag = true; }
void set_error(auto&&) && noexcept { unreachable("unexpected call to set_error"); }
void set_stopped() && noexcept { unreachable("unexpected call to set_stopped"); }
Copy link
Member

Choose a reason for hiding this comment

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

should this be qualified std::unreachable?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, there is an "unreachable" function above which deals with some clang-tidy-warnings about assert always failing: that's the point - the function shouldn't be called and if it is I want it to fail.

Copy link
Member

Choose a reason for hiding this comment

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

of course the function was hidden by the review tool -- you've already merged, but I'd suggest a different name when you're in there again. I was expecting a runtime check (since that's what std::unreachable is) - this is more like 'unexpected_call_assert'

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll make this change on the PR for task (I just made the change but there is more to go into the PR).

auto await_suspend(::std::coroutine_handle<PP> parent) noexcept {
this->scheduler.emplace(
this->template from_env<scheduler_type>(::beman::execution::get_env(parent.promise())));
this->parent = ::std::move(parent);
Copy link
Contributor

Choose a reason for hiding this comment

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

[pre-commit] reported by reviewdog 🐶

Suggested change
this->parent = ::std::move(parent);
this->parent = ::std::move(parent);

Comment on lines 40 to 43
return this->no_completion_set()
? this->handle_stopped(this->parent.address())
: ::std::move(this->parent)
;
Copy link
Contributor

Choose a reason for hiding this comment

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

[pre-commit] reported by reviewdog 🐶

Suggested change
return this->no_completion_set()
? this->handle_stopped(this->parent.address())
: ::std::move(this->parent)
;
return this->no_completion_set() ? this->handle_stopped(this->parent.address()) : ::std::move(this->parent);

::std::optional<scheduler_type> scheduler;
::beman::task::detail::handle<Promise> handle;
::std::coroutine_handle<> parent{};
auto (*handle_stopped)(void*)noexcept -> ::std::coroutine_handle<> = nullptr;
Copy link
Contributor

Choose a reason for hiding this comment

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

[pre-commit] reported by reviewdog 🐶

Suggested change
auto (*handle_stopped)(void*)noexcept -> ::std::coroutine_handle<> = nullptr;
auto (*handle_stopped)(void*) noexcept -> ::std::coroutine_handle<> = nullptr;

Comment on lines 70 to 72
std::coroutine_handle<> unhandled_stopped() {
return this->get_state()->complete();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

[pre-commit] reported by reviewdog 🐶

Suggested change
std::coroutine_handle<> unhandled_stopped() {
return this->get_state()->complete();
}
std::coroutine_handle<> unhandled_stopped() { return this->get_state()->complete(); }

Comment on lines 119 to 124
::beman::task::detail::sub_visit<2u>([]<typename E>(E& error) {
if constexpr (::std::same_as<::std::remove_cvref_t<E>, ::std::exception_ptr>)
std::rethrow_exception(::std::move(error));
else
throw ::std::move(error);
}, this->result);
Copy link
Contributor

Choose a reason for hiding this comment

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

[pre-commit] reported by reviewdog 🐶

Suggested change
::beman::task::detail::sub_visit<2u>([]<typename E>(E& error) {
if constexpr (::std::same_as<::std::remove_cvref_t<E>, ::std::exception_ptr>)
std::rethrow_exception(::std::move(error));
else
throw ::std::move(error);
}, this->result);
::beman::task::detail::sub_visit<2u>(
[]<typename E>(E& error) {
if constexpr (::std::same_as<::std::remove_cvref_t<E>, ::std::exception_ptr>)
std::rethrow_exception(::std::move(error));
else
throw ::std::move(error);
},
this->result);

assert(rc);
[[maybe_unused]] auto [value] = rc.value_or(std::tuple{0});
assert(value == 17);

Copy link
Contributor

Choose a reason for hiding this comment

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

[pre-commit] reported by reviewdog 🐶

Suggested change
}

Comment on lines 19 to 30
}

auto test_cancel() {
auto rc = ex::sync_wait([]() -> ex::task<> {
bool stopped{};
co_await (
[]()->ex::task<void> { co_await ex::just_stopped(); }()
| ex::upon_stopped([&stopped]() { stopped = true; })
);
assert(stopped);
}());
}
Copy link
Contributor

Choose a reason for hiding this comment

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

[pre-commit] reported by reviewdog 🐶

Suggested change
}
auto test_cancel() {
auto rc = ex::sync_wait([]() -> ex::task<> {
bool stopped{};
co_await (
[]()->ex::task<void> { co_await ex::just_stopped(); }()
| ex::upon_stopped([&stopped]() { stopped = true; })
);
assert(stopped);
}());
}
auto test_cancel() {
auto rc = ex::sync_wait([]() -> ex::task<> {
bool stopped{};
co_await ([]() -> ex::task<void> { co_await ex::just_stopped(); }() |
ex::upon_stopped([&stopped]() { stopped = true; }));
assert(stopped);
}());
}

Comment on lines 32 to 53
auto test_indirect_cancel() {
// This approach uses symmetric transfer
auto rc = ex::sync_wait([]() -> ex::task<> {
std::cout << "indirect cancel\n" << std::unitbuf;
bool stopped{};
std::cout << "outer co_await\n";
co_await (
[]()->ex::task<void> {
std::cout << "middle co_await\n";
co_await []()->ex::task<void> {
std::cout << "inner stopping\n";
co_await ex::just_stopped();
std::cout << "inner after stopped\n";
}();
std::cout << "middle after co_await\n";
}()
| ex::upon_stopped([&stopped]() { stopped = true; })
);
std::cout << "outer after co_await\n";
assert(stopped);
}());
}
Copy link
Contributor

Choose a reason for hiding this comment

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

[pre-commit] reported by reviewdog 🐶

Suggested change
auto test_indirect_cancel() {
// This approach uses symmetric transfer
auto rc = ex::sync_wait([]() -> ex::task<> {
std::cout << "indirect cancel\n" << std::unitbuf;
bool stopped{};
std::cout << "outer co_await\n";
co_await (
[]()->ex::task<void> {
std::cout << "middle co_await\n";
co_await []()->ex::task<void> {
std::cout << "inner stopping\n";
co_await ex::just_stopped();
std::cout << "inner after stopped\n";
}();
std::cout << "middle after co_await\n";
}()
| ex::upon_stopped([&stopped]() { stopped = true; })
);
std::cout << "outer after co_await\n";
assert(stopped);
}());
}
auto test_indirect_cancel() {
// This approach uses symmetric transfer
auto rc = ex::sync_wait([]() -> ex::task<> {
std::cout << "indirect cancel\n" << std::unitbuf;
bool stopped{};
std::cout << "outer co_await\n";
co_await ([]() -> ex::task<void> {
std::cout << "middle co_await\n";
co_await []() -> ex::task<void> {
std::cout << "inner stopping\n";
co_await ex::just_stopped();
std::cout << "inner after stopped\n";
}();
std::cout << "middle after co_await\n";
}() | ex::upon_stopped([&stopped]() { stopped = true; }));
std::cout << "outer after co_await\n";
assert(stopped);
}());

}());
}
}

Copy link
Contributor

Choose a reason for hiding this comment

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

[pre-commit] reported by reviewdog 🐶

Suggested change
} // namespace

}
}

auto main() -> int{
Copy link
Contributor

Choose a reason for hiding this comment

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

[pre-commit] reported by reviewdog 🐶

Suggested change
auto main() -> int{
auto main() -> int {

@dietmarkuehl dietmarkuehl marked this pull request as ready for review July 6, 2025 23:19
@dietmarkuehl dietmarkuehl merged commit 4cc77d5 into main Jul 6, 2025
48 checks passed
@dietmarkuehl dietmarkuehl deleted the as_awaitable branch July 6, 2025 23:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants