comparison gcc/testsuite/g++.dg/coroutines/torture/mid-suspend-destruction-0.C @ 145:1830386684a0

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