From c0c89f7c4e8dbb1e5d852ac0c41ef20474fac465 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dietmar=20K=C3=BChl?= Date: Fri, 8 Aug 2025 00:02:00 +0100 Subject: [PATCH 1/3] more fixes for the handling of the context --- include/beman/task/detail/awaiter.hpp | 23 +++++++++++++--------- include/beman/task/detail/handle.hpp | 6 +++++- include/beman/task/detail/promise_type.hpp | 6 +++++- include/beman/task/detail/state_base.hpp | 5 ++++- include/beman/task/detail/task.hpp | 4 +--- 5 files changed, 29 insertions(+), 15 deletions(-) diff --git a/include/beman/task/detail/awaiter.hpp b/include/beman/task/detail/awaiter.hpp index ab751b5..90798b4 100644 --- a/include/beman/task/detail/awaiter.hpp +++ b/include/beman/task/detail/awaiter.hpp @@ -46,20 +46,25 @@ struct awaiter_op_t { }; template -class awaiter : public ::beman::task::detail::state_base, - ::beman::task::detail::state_rep> { +class awaiter : public ::beman::task::detail::state_base + { public: using stop_token_type = typename ::beman::task::detail::state_base::stop_token_type; using scheduler_type = typename ::beman::task::detail::state_base::scheduler_type; - explicit awaiter(::beman::task::detail::handle h) - : ::beman::task::detail::state_rep>(std::move(h)) {} + explicit awaiter(::beman::task::detail::handle h) : + handle(std::move(h)) {} constexpr auto await_ready() const noexcept -> bool { return false; } + struct env_receiver { + ParentPromise* parent; + auto get_env() const noexcept { return parent->get_env(); } + }; auto await_suspend(::std::coroutine_handle parent) noexcept { + this->state_rep.emplace(env_receiver{&parent.promise()}); this->scheduler.emplace( this->template from_env(::beman::execution::get_env(parent.promise()))); this->parent = ::std::move(parent); - return this->receiver.start(this); + return this->handle.start(this); } auto await_resume() { return this->result_resume(); } @@ -82,17 +87,17 @@ class awaiter : public ::beman::task::detail::state_base, return this->actual_complete(); } auto actual_complete() -> std::coroutine_handle<> { - return this->::beman::task::detail::state_base::no_completion_set() - ? this->parent.promise().unhandled_stopped() - : ::std::move(this->parent); + return this->no_completion_set() ? this->parent.promise().unhandled_stopped() : ::std::move(this->parent); } auto do_get_scheduler() -> scheduler_type override { return *this->scheduler; } auto do_set_scheduler(scheduler_type other) -> scheduler_type override { return ::std::exchange(*this->scheduler, other); } auto do_get_stop_token() -> stop_token_type override { return {}; } - auto do_get_environment() -> Env& override { return this->context; } + auto do_get_environment() -> Env& override { return this->state_rep->context; } + ::beman::task::detail::handle handle; + ::std::optional<::beman::task::detail::state_rep> state_rep; ::std::optional scheduler; ::std::coroutine_handle parent{}; ::std::optional> reschedule{}; diff --git a/include/beman/task/detail/handle.hpp b/include/beman/task/detail/handle.hpp index 28d9c3d..67a4922 100644 --- a/include/beman/task/detail/handle.hpp +++ b/include/beman/task/detail/handle.hpp @@ -32,7 +32,11 @@ class handle { auto release() -> ::std::coroutine_handle

{ return ::std::coroutine_handle

::from_promise(*this->h.release()); } - auto get_env() const noexcept { return ::beman::execution::get_env(*this->h); } + P* get() const noexcept { return this->h.get(); } + auto get_env() const noexcept { + assert(this->h.get()); + return ::beman::execution::get_env(*this->h); + } }; } // namespace beman::task::detail diff --git a/include/beman/task/detail/promise_type.hpp b/include/beman/task/detail/promise_type.hpp index 309619f..094e4bc 100644 --- a/include/beman/task/detail/promise_type.hpp +++ b/include/beman/task/detail/promise_type.hpp @@ -109,7 +109,11 @@ class promise_type auto get_scheduler() const noexcept -> scheduler_type { return this->get_state()->get_scheduler(); } auto get_allocator() const noexcept -> allocator_type { return this->allocator; } auto get_stop_token() const noexcept -> stop_token_type { return this->get_state()->get_stop_token(); } - auto get_environment() const noexcept -> const Environment& { return this->get_state()->get_environment(); } + auto get_environment() const noexcept -> const Environment& { + assert(this); + assert(this->get_state()); + return this->get_state()->get_environment(); + } private: using env_t = ::beman::task::detail::promise_env; diff --git a/include/beman/task/detail/state_base.hpp b/include/beman/task/detail/state_base.hpp index c1209fb..53c7096 100644 --- a/include/beman/task/detail/state_base.hpp +++ b/include/beman/task/detail/state_base.hpp @@ -24,7 +24,10 @@ class state_base : public ::beman::task::detail::result_type<::beman::task::deta auto complete() -> std::coroutine_handle<> { return this->do_complete(); } auto get_stop_token() -> stop_token_type { return this->do_get_stop_token(); } - auto get_environment() -> Environment& { return this->do_get_environment(); } + auto get_environment() -> Environment& { + assert(this); + return this->do_get_environment(); + } auto get_scheduler() -> scheduler_type { return this->do_get_scheduler(); } auto set_scheduler(scheduler_type other) -> scheduler_type { return this->do_set_scheduler(other); } diff --git a/include/beman/task/detail/task.hpp b/include/beman/task/detail/task.hpp index f9c5121..cc7138d 100644 --- a/include/beman/task/detail/task.hpp +++ b/include/beman/task/detail/task.hpp @@ -72,6 +72,7 @@ class task { } template auto as_awaitable(ParentPromise&) -> ::beman::task::detail::awaiter { + assert(this->handle.get()); return ::beman::task::detail::awaiter(::std::move(this->handle)); } @@ -88,18 +89,15 @@ class task { struct domain { template auto transform_sender(DS&&, auto&&...) const noexcept { - std::cout << "task::domain::transform_sender\n"; return dom_sender{}; } }; struct env { auto query(const ::beman::execution::get_domain_t&) const noexcept -> domain { - std::cout << "task::env::get_domain\n"; return domain{}; } }; auto xget_env() const noexcept { - std::cout << "task::get_env\n"; return env{}; } From 63434b27933695d3ff11beba5e6314f428aa1de3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dietmar=20K=C3=BChl?= Date: Fri, 8 Aug 2025 00:04:37 +0100 Subject: [PATCH 2/3] clang format --- include/beman/task/detail/awaiter.hpp | 10 ++++------ include/beman/task/detail/handle.hpp | 2 +- include/beman/task/detail/task.hpp | 8 ++------ 3 files changed, 7 insertions(+), 13 deletions(-) diff --git a/include/beman/task/detail/awaiter.hpp b/include/beman/task/detail/awaiter.hpp index 90798b4..9caaa98 100644 --- a/include/beman/task/detail/awaiter.hpp +++ b/include/beman/task/detail/awaiter.hpp @@ -46,18 +46,16 @@ struct awaiter_op_t { }; template -class awaiter : public ::beman::task::detail::state_base - { +class awaiter : public ::beman::task::detail::state_base { public: using stop_token_type = typename ::beman::task::detail::state_base::stop_token_type; using scheduler_type = typename ::beman::task::detail::state_base::scheduler_type; - explicit awaiter(::beman::task::detail::handle h) : - handle(std::move(h)) {} + explicit awaiter(::beman::task::detail::handle h) : handle(std::move(h)) {} constexpr auto await_ready() const noexcept -> bool { return false; } struct env_receiver { ParentPromise* parent; - auto get_env() const noexcept { return parent->get_env(); } + auto get_env() const noexcept { return parent->get_env(); } }; auto await_suspend(::std::coroutine_handle parent) noexcept { this->state_rep.emplace(env_receiver{&parent.promise()}); @@ -96,7 +94,7 @@ class awaiter : public ::beman::task::detail::state_base auto do_get_stop_token() -> stop_token_type override { return {}; } auto do_get_environment() -> Env& override { return this->state_rep->context; } - ::beman::task::detail::handle handle; + ::beman::task::detail::handle handle; ::std::optional<::beman::task::detail::state_rep> state_rep; ::std::optional scheduler; ::std::coroutine_handle parent{}; diff --git a/include/beman/task/detail/handle.hpp b/include/beman/task/detail/handle.hpp index 67a4922..276bdfe 100644 --- a/include/beman/task/detail/handle.hpp +++ b/include/beman/task/detail/handle.hpp @@ -32,7 +32,7 @@ class handle { auto release() -> ::std::coroutine_handle

{ return ::std::coroutine_handle

::from_promise(*this->h.release()); } - P* get() const noexcept { return this->h.get(); } + P* get() const noexcept { return this->h.get(); } auto get_env() const noexcept { assert(this->h.get()); return ::beman::execution::get_env(*this->h); diff --git a/include/beman/task/detail/task.hpp b/include/beman/task/detail/task.hpp index cc7138d..1f88343 100644 --- a/include/beman/task/detail/task.hpp +++ b/include/beman/task/detail/task.hpp @@ -93,13 +93,9 @@ class task { } }; struct env { - auto query(const ::beman::execution::get_domain_t&) const noexcept -> domain { - return domain{}; - } + auto query(const ::beman::execution::get_domain_t&) const noexcept -> domain { return domain{}; } }; - auto xget_env() const noexcept { - return env{}; - } + auto xget_env() const noexcept { return env{}; } private: ::beman::task::detail::handle handle; From 03b65274b8678c66dadba82f261cdb2404543230 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dietmar=20K=C3=BChl?= Date: Fri, 8 Aug 2025 00:39:32 +0100 Subject: [PATCH 3/3] clang-forma --- include/beman/task/detail/awaiter.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/beman/task/detail/awaiter.hpp b/include/beman/task/detail/awaiter.hpp index 9caaa98..4eff758 100644 --- a/include/beman/task/detail/awaiter.hpp +++ b/include/beman/task/detail/awaiter.hpp @@ -57,7 +57,7 @@ class awaiter : public ::beman::task::detail::state_base { ParentPromise* parent; auto get_env() const noexcept { return parent->get_env(); } }; - auto await_suspend(::std::coroutine_handle parent) noexcept { + auto await_suspend(::std::coroutine_handle parent) noexcept { this->state_rep.emplace(env_receiver{&parent.promise()}); this->scheduler.emplace( this->template from_env(::beman::execution::get_env(parent.promise()))); @@ -96,9 +96,9 @@ class awaiter : public ::beman::task::detail::state_base { ::beman::task::detail::handle handle; ::std::optional<::beman::task::detail::state_rep> state_rep; - ::std::optional scheduler; - ::std::coroutine_handle parent{}; - ::std::optional> reschedule{}; + ::std::optional scheduler; + ::std::coroutine_handle parent{}; + ::std::optional> reschedule{}; }; } // namespace beman::task::detail