| 1 |
// See PR94359, we will need either a general solution to this, or at least |
| 2 |
// some hook for targets to opt in, for now the test will work on targets that |
| 3 |
// can do the tailcall (which would normally be available for O2+) |
| 4 |
|
| 5 |
// { dg-do run { target { i?86-*-linux-gnu x86_64-*-linux-gnu *-*-darwin* } } } |
| 6 |
// { dg-additional-options "-O2" } |
| 7 |
// { dg-skip-if "requires hosted libstdc++ for chrono" { ! hostedlib } } |
| 8 |
|
| 9 |
#if __has_include(<coroutine>) |
| 10 |
|
| 11 |
#include <coroutine> |
| 12 |
namespace coro = std; |
| 13 |
|
| 14 |
#elif __has_include(<experimental/coroutine>) |
| 15 |
|
| 16 |
#include <experimental/coroutine> |
| 17 |
namespace coro = std::experimental; |
| 18 |
|
| 19 |
#endif |
| 20 |
|
| 21 |
#if __clang__ |
| 22 |
# include <utility> |
| 23 |
#endif |
| 24 |
|
| 25 |
#include <chrono> |
| 26 |
#include <thread> |
| 27 |
#include <cstdio> |
| 28 |
|
| 29 |
template <typename T> |
| 30 |
struct Loopy { |
| 31 |
struct promise_type; |
| 32 |
using handle_type = coro::coroutine_handle<Loopy::promise_type>; |
| 33 |
handle_type handle; |
| 34 |
|
| 35 |
struct promise_type { |
| 36 |
// Cause the Loopy object to be created from the handle. |
| 37 |
auto get_return_object () { |
| 38 |
return handle_type::from_promise (*this); |
| 39 |
} |
| 40 |
|
| 41 |
coro::suspend_never yield_value(T value) { |
| 42 |
//fprintf (stderr, "%s yields %d\n", me, value); |
| 43 |
current_value = value; |
| 44 |
return coro::suspend_never{}; |
| 45 |
} |
| 46 |
|
| 47 |
coro::suspend_always initial_suspend() { return {}; } |
| 48 |
coro::suspend_always final_suspend() noexcept { return {}; } |
| 49 |
|
| 50 |
void unhandled_exception() { /*std::terminate();*/ } |
| 51 |
void return_void() {} |
| 52 |
|
| 53 |
void set_peer (handle_type _p) { peer = _p; } |
| 54 |
void set_me (const char *m) { me = m; } |
| 55 |
|
| 56 |
T get_value () {return current_value;} |
| 57 |
T current_value; |
| 58 |
handle_type peer; |
| 59 |
const char *me; |
| 60 |
}; |
| 61 |
|
| 62 |
Loopy () : handle(0) {} |
| 63 |
Loopy (handle_type _handle) : handle(_handle) {} |
| 64 |
Loopy (const Loopy &) = delete; |
| 65 |
Loopy (Loopy &&s) : handle(s.handle) { s.handle = nullptr; } |
| 66 |
~Loopy() { if ( handle ) handle.destroy(); } |
| 67 |
|
| 68 |
struct loopy_awaiter { |
| 69 |
handle_type p; |
| 70 |
bool await_ready () { return false; } |
| 71 |
/* continue the peer. */ |
| 72 |
handle_type await_suspend (handle_type h) { |
| 73 |
p = h.promise().peer; |
| 74 |
return p; |
| 75 |
} |
| 76 |
T await_resume () { return p.promise().get_value (); } |
| 77 |
}; |
| 78 |
}; |
| 79 |
|
| 80 |
Loopy<int> |
| 81 |
pingpong (const char *id) |
| 82 |
{ |
| 83 |
int v = 0; |
| 84 |
Loopy<int>::loopy_awaiter aw{}; |
| 85 |
for (;;) |
| 86 |
{ |
| 87 |
co_yield (v+1); |
| 88 |
//std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
| 89 |
if (v > 10'000'000) |
| 90 |
break; |
| 91 |
v = co_await aw; |
| 92 |
//fprintf (stderr, "%s = %d\n", id, v); |
| 93 |
} |
| 94 |
//fprintf (stderr, "%s = %d\n", id, v); |
| 95 |
} |
| 96 |
|
| 97 |
int main () |
| 98 |
{ |
| 99 |
// identify for fun.. |
| 100 |
const char *ping_id = "ping"; |
| 101 |
const char *pong_id = "pong"; |
| 102 |
auto ping = pingpong (ping_id); // created suspended. |
| 103 |
auto pong = pingpong (pong_id); |
| 104 |
|
| 105 |
// point them at each other... |
| 106 |
ping.handle.promise().set_peer (pong.handle); |
| 107 |
pong.handle.promise().set_peer (ping.handle); |
| 108 |
|
| 109 |
ping.handle.promise().set_me (ping_id); |
| 110 |
pong.handle.promise().set_me (pong_id); |
| 111 |
|
| 112 |
// and start them ... |
| 113 |
ping.handle.resume (); |
| 114 |
pong.handle.resume (); |
| 115 |
|
| 116 |
} |
| 117 |
|