Debian Codegraph @

gcc/testsuite/g++.dg/coroutines/torture/mid-suspend-destruction-0.C

raw
1 // { dg-do run }
2 // { dg-output "main: returning(\n|\r\n|\r)" }
3 // { dg-output "Destroyed coro1(\n|\r\n|\r)" }
4 // { dg-output "Destroyed suspend_always_prt(\n|\r\n|\r)" }
5 // { dg-output "Destroyed Promise(\n|\r\n|\r)" }
6 // { dg-skip-if "requires hosted libstdc++ for cstdlib abort" { ! hostedlib } }
7
8 // Check that we still get the right DTORs run when we let a suspended coro
9 // go out of scope.
10
11 #include "../coro.h"
12 #include <stdio.h>
13
14 struct coro1 {
15 struct promise_type;
16 using handle_type = coro::coroutine_handle<coro1::promise_type>;
17 handle_type handle;
18 coro1 () : handle(0) {}
19 coro1 (handle_type _handle)
20 : handle(_handle) {
21 PRINT("Created coro1 object from handle");
22 }
23 coro1 (const coro1 &) = delete; // no copying
24 coro1 (coro1 &&s) : handle(s.handle) {
25 s.handle = nullptr;
26 PRINT("coro1 mv ctor ");
27 }
28 coro1 &operator = (coro1 &&s) {
29 handle = s.handle;
30 s.handle = nullptr;
31 PRINT("coro1 op= ");
32 return *this;
33 }
34 ~coro1() {
35 printf ("Destroyed coro1\n");
36 if ( handle )
37 handle.destroy();
38 }
39
40 struct suspend_never_prt {
41 bool await_ready() const noexcept { return true; }
42 void await_suspend(handle_type) const noexcept { PRINT ("susp-never-susp");}
43 void await_resume() const noexcept { PRINT ("susp-never-resume");}
44 ~suspend_never_prt() {};
45 };
46
47 struct suspend_always_prt {
48 bool await_ready() const noexcept { return false; }
49 void await_suspend(handle_type) const noexcept { PRINT ("susp-always-susp");}
50 void await_resume() const noexcept { PRINT ("susp-always-resume");}
51 ~suspend_always_prt() { printf ("Destroyed suspend_always_prt\n"); }
52 };
53
54 struct promise_type {
55 promise_type() { PRINT ("Created Promise"); }
56 ~promise_type() { printf ("Destroyed Promise\n"); }
57
58 auto get_return_object () {
59 PRINT ("get_return_object: handle from promise");
60 return handle_type::from_promise (*this);
61 }
62 auto initial_suspend () {
63 PRINT ("get initial_suspend (always)");
64 return suspend_always_prt{};
65 }
66 auto final_suspend () noexcept {
67 PRINT ("get final_suspend (always)");
68 return suspend_always_prt{};
69 }
70 void return_void () {
71 PRINT ("return_void ()");
72 }
73
74 void unhandled_exception() { PRINT ("** unhandled exception"); }
75 };
76 };
77
78 struct coro1
79 f () noexcept
80 {
81 PRINT ("coro1: about to return");
82 co_return;
83 }
84
85 int main ()
86 {
87 PRINT ("main: create coro1");
88 struct coro1 x = f ();
89 PRINT ("main: got coro1 - resuming");
90 if (x.handle.done())
91 {
92 PRINT ("main: f() should be suspended, says it's done");
93 abort();
94 }
95
96 #if __has_builtin (__builtin_coro_suspended)
97 if (! __builtin_coro_suspended(handle))
98 {
99 PRINT ("main: f() should be suspended, but says it isn't");
100 abort();
101 }
102 #endif
103
104 /* We are suspended... so let everything out of scope and therefore
105 destroy it. */
106
107 puts ("main: returning");
108 return 0;
109 }