Skip to content

Commit 6a85f24

Browse files
committed
fix: refine coro
1 parent 3dc4fd4 commit 6a85f24

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

include/coro/coro.hpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <cassert>
34
#include <coroutine>
45
#include <functional>
56
#include <memory>
@@ -146,7 +147,7 @@ struct final_awaitable {
146147
if (h.promise().parent_handle_) {
147148
return h.promise().parent_handle_;
148149
} else {
149-
if (h.done() && !h.promise().awaitable_) {
150+
if (!h.promise().awaitable_) {
150151
h.destroy();
151152
}
152153
return std::noop_coroutine();
@@ -207,13 +208,19 @@ struct awaitable {
207208
/// enable move
208209
awaitable(awaitable&& other) noexcept : current_coro_handle_(other.current_coro_handle_) {
209210
CORO_DEBUG_LIFECYCLE("awaitable: move(c): %p to %p, h: %p", &other, this, current_coro_handle_.address());
211+
if (current_coro_handle_) {
212+
current_coro_handle_.promise().awaitable_ = this;
213+
}
210214
other.current_coro_handle_ = nullptr;
211215
}
212216
awaitable& operator=(awaitable&& other) noexcept {
213217
CORO_DEBUG_LIFECYCLE("awaitable: move(=): %p to %p, h: %p", &other, this, current_coro_handle_.address());
214218
if (this != &other) {
215219
if (current_coro_handle_) current_coro_handle_.destroy();
216220
current_coro_handle_ = other.current_coro_handle_;
221+
if (current_coro_handle_) {
222+
current_coro_handle_.promise().awaitable_ = this;
223+
}
217224
other.current_coro_handle_ = nullptr;
218225
}
219226
return *this;
@@ -339,6 +346,7 @@ struct callback_awaiter : detail::callback_awaiter_base<T> {
339346
template <typename Promise>
340347
void await_suspend(std::coroutine_handle<Promise> handle) {
341348
auto executor = handle.promise().executor_;
349+
assert(executor && "executor must be set before using callback_awaiter");
342350
switch (callback_function_.index()) {
343351
case 0: {
344352
auto& func = std::get<0>(callback_function_);
@@ -402,7 +410,7 @@ struct current_executor_awaiter {
402410

403411
} // namespace detail
404412

405-
[[nodiscard]] detail::current_executor_awaiter current_executor() {
413+
[[nodiscard]] inline detail::current_executor_awaiter current_executor() {
406414
return detail::current_executor_awaiter{};
407415
}
408416

0 commit comments

Comments
 (0)