| 1 |
// { dg-do run } |
| 2 |
// { dg-skip-if "requires hosted libstdc++ for cstdlib abort" { ! hostedlib } } |
| 3 |
|
| 4 |
// Test co_yield in templated code. |
| 5 |
|
| 6 |
#include "../coro.h" |
| 7 |
|
| 8 |
template <typename T> |
| 9 |
struct looper { |
| 10 |
|
| 11 |
struct promise_type { |
| 12 |
T value; |
| 13 |
promise_type() { PRINT ("Created Promise"); } |
| 14 |
~promise_type() { PRINT ("Destroyed Promise"); } |
| 15 |
|
| 16 |
auto get_return_object () { |
| 17 |
PRINT ("get_return_object: handle from promise"); |
| 18 |
return handle_type::from_promise (*this); |
| 19 |
} |
| 20 |
|
| 21 |
auto initial_suspend () { |
| 22 |
PRINT ("get initial_suspend (always)"); |
| 23 |
return suspend_always_prt{}; |
| 24 |
} |
| 25 |
|
| 26 |
auto final_suspend () noexcept { |
| 27 |
PRINT ("get final_suspend (always)"); |
| 28 |
return suspend_always_prt{}; |
| 29 |
} |
| 30 |
|
| 31 |
void return_value (T v) { |
| 32 |
PRINTF ("return_value () %lf\n", (double)v); |
| 33 |
value = v; |
| 34 |
} |
| 35 |
|
| 36 |
auto yield_value (T v) { |
| 37 |
PRINTF ("yield_value () %lf and suspend always\n", (double)v); |
| 38 |
value = v; |
| 39 |
return suspend_always_prt{}; |
| 40 |
} |
| 41 |
|
| 42 |
T get_value (void) { return value; } |
| 43 |
|
| 44 |
void unhandled_exception() { PRINT ("** unhandled exception"); } |
| 45 |
}; |
| 46 |
|
| 47 |
using handle_type = coro::coroutine_handle<looper::promise_type>; |
| 48 |
handle_type handle; |
| 49 |
|
| 50 |
looper () : handle(0) {} |
| 51 |
looper (handle_type _handle) |
| 52 |
: handle(_handle) { |
| 53 |
PRINT("Created coro1 object from handle"); |
| 54 |
} |
| 55 |
looper (const looper &) = delete; // no copying |
| 56 |
looper (looper &&s) : handle(s.handle) { |
| 57 |
s.handle = nullptr; |
| 58 |
PRINT("looper mv ctor "); |
| 59 |
} |
| 60 |
looper &operator = (looper &&s) { |
| 61 |
handle = s.handle; |
| 62 |
s.handle = nullptr; |
| 63 |
PRINT("looper op= "); |
| 64 |
return *this; |
| 65 |
} |
| 66 |
~looper() { |
| 67 |
PRINT("Destroyed coro1"); |
| 68 |
if ( handle ) |
| 69 |
handle.destroy(); |
| 70 |
} |
| 71 |
|
| 72 |
struct suspend_never_prt { |
| 73 |
bool await_ready() const noexcept { return true; } |
| 74 |
void await_suspend(handle_type) const noexcept { PRINT ("susp-never-susp"); } |
| 75 |
void await_resume() const noexcept { PRINT ("susp-never-resume");} |
| 76 |
}; |
| 77 |
|
| 78 |
/* NOTE: this has a DTOR to test that pathway. */ |
| 79 |
struct suspend_always_prt { |
| 80 |
bool await_ready() const noexcept { return false; } |
| 81 |
void await_suspend(handle_type) const noexcept { PRINT ("susp-always-susp"); } |
| 82 |
void await_resume() const noexcept { PRINT ("susp-always-resume"); } |
| 83 |
~suspend_always_prt() { PRINT ("susp-always-DTOR"); } |
| 84 |
}; |
| 85 |
|
| 86 |
}; |
| 87 |
|
| 88 |
// Contrived to avoid non-scalar state across the yield. |
| 89 |
template <typename T> |
| 90 |
looper<T> f () noexcept |
| 91 |
{ |
| 92 |
for (int i = 5; i < 10 ; ++i) |
| 93 |
{ |
| 94 |
PRINTF ("f: about to yield %d\n", i); |
| 95 |
co_yield (T) i; |
| 96 |
} |
| 97 |
|
| 98 |
PRINT ("f: about to return 6174"); |
| 99 |
co_return 6174; |
| 100 |
} |
| 101 |
|
| 102 |
// contrived, only going to work for an int. |
| 103 |
int main () |
| 104 |
{ |
| 105 |
PRINT ("main: create int looper"); |
| 106 |
auto f_coro = f<int> (); |
| 107 |
|
| 108 |
if (f_coro.handle.done()) |
| 109 |
{ |
| 110 |
PRINT ("main: said we were done, but we hadn't started!"); |
| 111 |
abort(); |
| 112 |
} |
| 113 |
|
| 114 |
PRINT ("main: OK -- looping"); |
| 115 |
int y, test = 5; |
| 116 |
do { |
| 117 |
f_coro.handle.resume(); |
| 118 |
if (f_coro.handle.done()) |
| 119 |
break; |
| 120 |
y = f_coro.handle.promise().get_value(); |
| 121 |
if (y != test) |
| 122 |
{ |
| 123 |
PRINTF ("main: failed for test %d, got %d\n", test, y); |
| 124 |
abort(); |
| 125 |
} |
| 126 |
test++; |
| 127 |
} while (test < 20); |
| 128 |
|
| 129 |
y = f_coro.handle.promise().get_value(); |
| 130 |
if ( y != 6174 ) |
| 131 |
abort (); |
| 132 |
|
| 133 |
PRINT ("main: apparently got 6174"); |
| 134 |
if (!f_coro.handle.done()) |
| 135 |
{ |
| 136 |
PRINT ("main: apparently not done..."); |
| 137 |
abort (); |
| 138 |
} |
| 139 |
PRINT ("main: returning"); |
| 140 |
return 0; |
| 141 |
} |