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