| 1 |
// { dg-do run } |
| 2 |
// { dg-skip-if "requires hosted libstdc++ for stdexcept" { ! hostedlib } } |
| 3 |
#include "../coro.h" |
| 4 |
|
| 5 |
#include <stdexcept> |
| 6 |
|
| 7 |
int frame_live = 0; |
| 8 |
int promise_live = 0; |
| 9 |
int task_live = 0; |
| 10 |
|
| 11 |
struct Task |
| 12 |
{ |
| 13 |
struct promise_type; |
| 14 |
using handle = std::coroutine_handle<promise_type>; |
| 15 |
|
| 16 |
struct promise_type |
| 17 |
{ |
| 18 |
promise_type () { promise_live++; PRINT ("promise_type ()"); } |
| 19 |
~promise_type () { promise_live--; PRINT ("~promise_type ()"); } |
| 20 |
void* operator new(size_t sz) { |
| 21 |
PRINT("operator new()"); |
| 22 |
frame_live++; |
| 23 |
return ::operator new(sz); |
| 24 |
} |
| 25 |
void operator delete(void* p, size_t sz) { |
| 26 |
PRINT("operator delete"); |
| 27 |
frame_live--; |
| 28 |
return ::operator delete(p, sz); |
| 29 |
} |
| 30 |
|
| 31 |
Task get_return_object() { return handle::from_promise(*this); } |
| 32 |
auto initial_suspend() noexcept { return std::suspend_always{}; } |
| 33 |
auto final_suspend() noexcept { return std::suspend_always{}; } |
| 34 |
void return_void() noexcept {} |
| 35 |
|
| 36 |
auto yield_value(int x) noexcept |
| 37 |
{ |
| 38 |
PRINTF ("yield_value(%d)\n", x); |
| 39 |
return std::suspend_always{}; |
| 40 |
} |
| 41 |
|
| 42 |
void unhandled_exception() |
| 43 |
{ |
| 44 |
PRINT ("unhandled_exception()"); |
| 45 |
throw; |
| 46 |
} |
| 47 |
}; |
| 48 |
|
| 49 |
Task(handle h) : coro(h) { task_live++; PRINT ("Task(handle h)"); } |
| 50 |
~Task() { task_live--; PRINT ("~Task()"); if (coro) coro.destroy(); } |
| 51 |
|
| 52 |
handle coro; |
| 53 |
}; |
| 54 |
|
| 55 |
Task myco() |
| 56 |
{ |
| 57 |
co_yield 42; |
| 58 |
throw std::out_of_range("TEST EXCEPTION"); |
| 59 |
} |
| 60 |
|
| 61 |
int main() |
| 62 |
{ |
| 63 |
{ |
| 64 |
Task task = myco(); |
| 65 |
PRINT ("START"); |
| 66 |
try { |
| 67 |
PRINTF ("done #0 = %d\n", task.coro.done()); |
| 68 |
if (task.coro.done()) |
| 69 |
abort(); |
| 70 |
task.coro.resume(); // will yield 42 |
| 71 |
PRINTF ("done #1 = %d\n", task.coro.done()); |
| 72 |
if (task.coro.done()) |
| 73 |
abort(); |
| 74 |
task.coro.resume(); // will throw exception |
| 75 |
PRINT ("should not be reached"); |
| 76 |
abort (); |
| 77 |
} |
| 78 |
catch (const std::exception&) { |
| 79 |
PRINTF ("done exc = %d\n", task.coro.done()); |
| 80 |
if (!task.coro.done()) |
| 81 |
abort(); |
| 82 |
} |
| 83 |
if (!task.coro.done()) |
| 84 |
abort(); |
| 85 |
} // should cause cause the destroy () to run. |
| 86 |
if (task_live || promise_live || frame_live) |
| 87 |
{ |
| 88 |
PRINTF ("task_live = %d, promise_live = %d, frame_live = %d\n", |
| 89 |
task_live, promise_live, frame_live); |
| 90 |
abort (); |
| 91 |
} |
| 92 |
} |