Skip to content

Commit 08ce0ca

Browse files
authored
Merge pull request #46 from bemanproject/fix-awaiter-environment
Fix awaiter environment
2 parents 51696e6 + 03b6527 commit 08ce0ca

File tree

5 files changed

+33
-25
lines changed

5 files changed

+33
-25
lines changed

include/beman/task/detail/awaiter.hpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,23 @@ struct awaiter_op_t<Awaiter, ParentPromise, false> {
4646
};
4747

4848
template <typename Value, typename Env, typename OwnPromise, typename ParentPromise>
49-
class awaiter : public ::beman::task::detail::state_base<Value, Env>,
50-
::beman::task::detail::state_rep<Env, ::beman::task::detail::handle<OwnPromise>> {
49+
class awaiter : public ::beman::task::detail::state_base<Value, Env> {
5150
public:
5251
using stop_token_type = typename ::beman::task::detail::state_base<Value, Env>::stop_token_type;
5352
using scheduler_type = typename ::beman::task::detail::state_base<Value, Env>::scheduler_type;
5453

55-
explicit awaiter(::beman::task::detail::handle<OwnPromise> h)
56-
: ::beman::task::detail::state_rep<Env, ::beman::task::detail::handle<OwnPromise>>(std::move(h)) {}
54+
explicit awaiter(::beman::task::detail::handle<OwnPromise> h) : handle(std::move(h)) {}
5755
constexpr auto await_ready() const noexcept -> bool { return false; }
58-
auto await_suspend(::std::coroutine_handle<ParentPromise> parent) noexcept {
56+
struct env_receiver {
57+
ParentPromise* parent;
58+
auto get_env() const noexcept { return parent->get_env(); }
59+
};
60+
auto await_suspend(::std::coroutine_handle<ParentPromise> parent) noexcept {
61+
this->state_rep.emplace(env_receiver{&parent.promise()});
5962
this->scheduler.emplace(
6063
this->template from_env<scheduler_type>(::beman::execution::get_env(parent.promise())));
6164
this->parent = ::std::move(parent);
62-
return this->receiver.start(this);
65+
return this->handle.start(this);
6366
}
6467
auto await_resume() { return this->result_resume(); }
6568

@@ -82,20 +85,20 @@ class awaiter : public ::beman::task::detail::state_base<Value, Env>,
8285
return this->actual_complete();
8386
}
8487
auto actual_complete() -> std::coroutine_handle<> {
85-
return this->::beman::task::detail::state_base<Value, Env>::no_completion_set()
86-
? this->parent.promise().unhandled_stopped()
87-
: ::std::move(this->parent);
88+
return this->no_completion_set() ? this->parent.promise().unhandled_stopped() : ::std::move(this->parent);
8889
}
8990
auto do_get_scheduler() -> scheduler_type override { return *this->scheduler; }
9091
auto do_set_scheduler(scheduler_type other) -> scheduler_type override {
9192
return ::std::exchange(*this->scheduler, other);
9293
}
9394
auto do_get_stop_token() -> stop_token_type override { return {}; }
94-
auto do_get_environment() -> Env& override { return this->context; }
95+
auto do_get_environment() -> Env& override { return this->state_rep->context; }
9596

96-
::std::optional<scheduler_type> scheduler;
97-
::std::coroutine_handle<ParentPromise> parent{};
98-
::std::optional<awaiter_op_t<awaiter, ParentPromise>> reschedule{};
97+
::beman::task::detail::handle<OwnPromise> handle;
98+
::std::optional<::beman::task::detail::state_rep<Env, env_receiver>> state_rep;
99+
::std::optional<scheduler_type> scheduler;
100+
::std::coroutine_handle<ParentPromise> parent{};
101+
::std::optional<awaiter_op_t<awaiter, ParentPromise>> reschedule{};
99102
};
100103
} // namespace beman::task::detail
101104

include/beman/task/detail/handle.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ class handle {
3232
auto release() -> ::std::coroutine_handle<P> {
3333
return ::std::coroutine_handle<P>::from_promise(*this->h.release());
3434
}
35-
auto get_env() const noexcept { return ::beman::execution::get_env(*this->h); }
35+
P* get() const noexcept { return this->h.get(); }
36+
auto get_env() const noexcept {
37+
assert(this->h.get());
38+
return ::beman::execution::get_env(*this->h);
39+
}
3640
};
3741

3842
} // namespace beman::task::detail

include/beman/task/detail/promise_type.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,11 @@ class promise_type
109109
auto get_scheduler() const noexcept -> scheduler_type { return this->get_state()->get_scheduler(); }
110110
auto get_allocator() const noexcept -> allocator_type { return this->allocator; }
111111
auto get_stop_token() const noexcept -> stop_token_type { return this->get_state()->get_stop_token(); }
112-
auto get_environment() const noexcept -> const Environment& { return this->get_state()->get_environment(); }
112+
auto get_environment() const noexcept -> const Environment& {
113+
assert(this);
114+
assert(this->get_state());
115+
return this->get_state()->get_environment();
116+
}
113117

114118
private:
115119
using env_t = ::beman::task::detail::promise_env<promise_type>;

include/beman/task/detail/state_base.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ class state_base : public ::beman::task::detail::result_type<::beman::task::deta
2424

2525
auto complete() -> std::coroutine_handle<> { return this->do_complete(); }
2626
auto get_stop_token() -> stop_token_type { return this->do_get_stop_token(); }
27-
auto get_environment() -> Environment& { return this->do_get_environment(); }
27+
auto get_environment() -> Environment& {
28+
assert(this);
29+
return this->do_get_environment();
30+
}
2831
auto get_scheduler() -> scheduler_type { return this->do_get_scheduler(); }
2932
auto set_scheduler(scheduler_type other) -> scheduler_type { return this->do_set_scheduler(other); }
3033

include/beman/task/detail/task.hpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class task {
7272
}
7373
template <typename ParentPromise>
7474
auto as_awaitable(ParentPromise&) -> ::beman::task::detail::awaiter<Value, Env, promise_type, ParentPromise> {
75+
assert(this->handle.get());
7576
return ::beman::task::detail::awaiter<Value, Env, promise_type, ParentPromise>(::std::move(this->handle));
7677
}
7778

@@ -88,20 +89,13 @@ class task {
8889
struct domain {
8990
template <typename DS>
9091
auto transform_sender(DS&&, auto&&...) const noexcept {
91-
std::cout << "task::domain::transform_sender\n";
9292
return dom_sender{};
9393
}
9494
};
9595
struct env {
96-
auto query(const ::beman::execution::get_domain_t&) const noexcept -> domain {
97-
std::cout << "task::env::get_domain\n";
98-
return domain{};
99-
}
96+
auto query(const ::beman::execution::get_domain_t&) const noexcept -> domain { return domain{}; }
10097
};
101-
auto xget_env() const noexcept {
102-
std::cout << "task::get_env\n";
103-
return env{};
104-
}
98+
auto xget_env() const noexcept { return env{}; }
10599

106100
private:
107101
::beman::task::detail::handle<promise_type> handle;

0 commit comments

Comments
 (0)