| 1 |
// { dg-do run } |
| 2 |
// https://gcc.gnu.org/PR103953 |
| 3 |
#include <coroutine> |
| 4 |
#include <utility> |
| 5 |
|
| 6 |
static int ctor_dtor_count = 0; |
| 7 |
|
| 8 |
struct task { |
| 9 |
struct promise_type; |
| 10 |
|
| 11 |
using handle_type = std::coroutine_handle<promise_type>; |
| 12 |
|
| 13 |
task(handle_type h) : handle(h) { |
| 14 |
ctor_dtor_count++; |
| 15 |
} |
| 16 |
task(const task & t) : handle(t.handle) { |
| 17 |
ctor_dtor_count++; |
| 18 |
} |
| 19 |
task(task && t) : handle(std::move(t.handle)) { |
| 20 |
ctor_dtor_count++; |
| 21 |
} |
| 22 |
~task() { |
| 23 |
if (--ctor_dtor_count < 0) |
| 24 |
__builtin_abort (); |
| 25 |
} |
| 26 |
|
| 27 |
struct promise_type { |
| 28 |
auto get_return_object() { |
| 29 |
return task{handle_type::from_promise(*this)}; |
| 30 |
} |
| 31 |
|
| 32 |
auto initial_suspend() { |
| 33 |
return std::suspend_always {}; |
| 34 |
} |
| 35 |
|
| 36 |
auto unhandled_exception() {} |
| 37 |
|
| 38 |
auto final_suspend() noexcept { |
| 39 |
return std::suspend_always{}; |
| 40 |
} |
| 41 |
|
| 42 |
void return_void() {} |
| 43 |
}; |
| 44 |
|
| 45 |
handle_type handle; |
| 46 |
|
| 47 |
void await_resume() { |
| 48 |
handle.resume(); |
| 49 |
} |
| 50 |
|
| 51 |
auto await_suspend(handle_type) { |
| 52 |
return handle; |
| 53 |
} |
| 54 |
|
| 55 |
auto await_ready() { |
| 56 |
return false; |
| 57 |
} |
| 58 |
}; |
| 59 |
|
| 60 |
int main() { |
| 61 |
{ |
| 62 |
task coroutine_A = []() ->task { |
| 63 |
co_return; |
| 64 |
}(); |
| 65 |
|
| 66 |
auto t = [&coroutine_A]() ->task { |
| 67 |
co_await coroutine_A; |
| 68 |
}; |
| 69 |
|
| 70 |
task coroutine_B = t(); |
| 71 |
|
| 72 |
coroutine_B.handle.resume(); |
| 73 |
} |
| 74 |
|
| 75 |
if (ctor_dtor_count != 0) |
| 76 |
__builtin_abort (); |
| 77 |
} |