| 1 |
// { dg-do run } |
| 2 |
#include <coroutine> |
| 3 |
|
| 4 |
#ifndef OUTPUT |
| 5 |
# define PRINT(X) |
| 6 |
# define PRINTF(...) |
| 7 |
#else |
| 8 |
#include <stdio.h> |
| 9 |
# define PRINT(X) puts(X) |
| 10 |
# define PRINTF(...) printf(__VA_ARGS__) |
| 11 |
#endif |
| 12 |
|
| 13 |
// coroutine management object with simple interlocks for the underlying |
| 14 |
// coroutine. |
| 15 |
|
| 16 |
struct coro1 { |
| 17 |
struct promise_type; |
| 18 |
using handle_type = std::coroutine_handle<coro1::promise_type>; |
| 19 |
|
| 20 |
handle_type handle = nullptr; |
| 21 |
|
| 22 |
coro1 () : handle(0) {} |
| 23 |
coro1 (handle_type _handle) |
| 24 |
: handle(_handle) |
| 25 |
{ |
| 26 |
PRINT("Created coro1 object from handle"); |
| 27 |
// We're alive - let the promise object know so that it can notify us |
| 28 |
// if the coroutine terminates before this object is destroyed. |
| 29 |
handle.promise().set_owner (this); |
| 30 |
} |
| 31 |
|
| 32 |
coro1 (const coro1 &) = delete; // no copying |
| 33 |
|
| 34 |
coro1 (coro1 &&s) : handle (s.handle) { |
| 35 |
s.handle = nullptr; |
| 36 |
PRINT("coro1 mv ctor "); |
| 37 |
handle.promise().set_owner (this); |
| 38 |
} |
| 39 |
|
| 40 |
coro1 &operator = (coro1 &&s) { |
| 41 |
handle = s.handle; |
| 42 |
s.handle = nullptr; |
| 43 |
PRINT("coro1 op= "); |
| 44 |
handle.promise().set_owner (this); |
| 45 |
return *this; |
| 46 |
} |
| 47 |
|
| 48 |
~coro1() { |
| 49 |
PRINT("Destroyed coro1"); |
| 50 |
// We might come here before the coroutine has finished so... |
| 51 |
if ( handle ) |
| 52 |
{ |
| 53 |
handle.promise().set_owner (nullptr); |
| 54 |
handle.destroy(); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
// Special awaiters. |
| 59 |
struct suspend_always_prt { |
| 60 |
bool await_ready() const noexcept { PRINT ("susp-always-is-not-ready") ; return false; } |
| 61 |
void await_suspend(handle_type) const noexcept { PRINT ("susp-always-susp");} |
| 62 |
void await_resume() const noexcept { PRINT ("susp-always-resume");} |
| 63 |
~suspend_always_prt() { PRINT ("susp-always-dtor"); } |
| 64 |
}; |
| 65 |
|
| 66 |
struct suspend_never_prt { |
| 67 |
bool await_ready () const noexcept { PRINT ("susp-never-is-ready") ; return true; } |
| 68 |
void await_suspend (handle_type) const noexcept { PRINT ("susp-never-susp");} |
| 69 |
void await_resume () const noexcept { PRINT ("susp-never-resume");} |
| 70 |
~suspend_never_prt () { PRINT ("susp-never-dtor"); } |
| 71 |
}; |
| 72 |
|
| 73 |
struct promise_type { |
| 74 |
|
| 75 |
promise_type () : vv(-1) { PRINT ("Created Promise"); } |
| 76 |
|
| 77 |
promise_type (int __x) : vv(__x) { PRINTF ("Created Promise with %d\n",__x); } |
| 78 |
|
| 79 |
~promise_type () { |
| 80 |
PRINT ("Destroyed Promise"); |
| 81 |
// The coroutine is about to become invalid - so remove the handle from the |
| 82 |
// owner. |
| 83 |
if (owner) |
| 84 |
owner->handle = nullptr; |
| 85 |
} |
| 86 |
|
| 87 |
// The coro1 ramp return object will be constructed from this. |
| 88 |
auto get_return_object () { |
| 89 |
PRINT ("get_return_object: handle from promise"); |
| 90 |
return coro1 (handle_type::from_promise (*this)); |
| 91 |
} |
| 92 |
|
| 93 |
auto initial_suspend () { return suspend_never_prt{}; } |
| 94 |
auto final_suspend () noexcept { return suspend_never_prt{}; } |
| 95 |
|
| 96 |
void return_value (int v) { |
| 97 |
PRINTF ("return_value (%d)\n", v); |
| 98 |
vv = v; |
| 99 |
} |
| 100 |
|
| 101 |
void unhandled_exception() { PRINT ("** unhandled exception"); } |
| 102 |
|
| 103 |
int get_value () { return vv; } |
| 104 |
void set_owner (coro1 *new_owner) { owner = new_owner; } |
| 105 |
|
| 106 |
private: |
| 107 |
coro1 *owner = nullptr; |
| 108 |
int vv; |
| 109 |
}; |
| 110 |
}; |
| 111 |
|
| 112 |
struct coro1 |
| 113 |
finishes_synchronously (const int v) |
| 114 |
{ |
| 115 |
co_return v; |
| 116 |
} |
| 117 |
|
| 118 |
int main () |
| 119 |
{ |
| 120 |
struct coro1 x = finishes_synchronously (42); |
| 121 |
// the underlying coroutine is done and destroyed... and we should have |
| 122 |
// signalled that by clearing coro1's handle. |
| 123 |
if (x.handle) |
| 124 |
__builtin_abort (); |
| 125 |
// ... so we just clean up the return object here. |
| 126 |
return 0; |
| 127 |
} |