-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathContinuation.cpp
More file actions
334 lines (297 loc) · 8.29 KB
/
Continuation.cpp
File metadata and controls
334 lines (297 loc) · 8.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#define CONTINUATION_TEST
#include <iostream>
#include <thread>
#include <functional>
#if defined( WIN32 ) || defined( _CONSOLE ) || defined( __GNUC__ )
#else
#define EXPERIMENTAL_COROUTINE
#endif
#ifdef EXPERIMENTAL_COROUTINE
#include <experimental/coroutine>
#else
#include <coroutine>
#endif
namespace
{
template< typename R = nullptr_t > struct Continuation;
#ifdef EXPERIMENTAL_COROUTINE
namespace std_coroutine = std::experimental;
#else
namespace std_coroutine = std;
#endif
void BuildAsyncChain( auto suspendedCoroutine, auto callingCoroutine )
{
suspendedCoroutine.promise().callingCoroutine_ = callingCoroutine;
callingCoroutine.promise().sync_ = false;
}
#ifdef CONTINUATION_TEST
static int ContinuationPromiseCount = 0;
#endif
template< typename R >
struct Continuation
{
struct promise_type
{
promise_type() noexcept
{
#ifdef CONTINUATION_TEST
++ContinuationPromiseCount;
#endif
}
~promise_type() noexcept
{
#ifdef CONTINUATION_TEST
--ContinuationPromiseCount;
#endif
}
Continuation< R > get_return_object()
{
return Continuation< R >{ std_coroutine::coroutine_handle< promise_type >::from_promise( *this ) };
}
struct ResumeContinuation
{
ResumeContinuation() noexcept {}
bool await_ready() const noexcept { return false; }
void await_suspend( std_coroutine::coroutine_handle< promise_type > thisCoroutine ) noexcept
{
auto& promise = thisCoroutine.promise();
if( promise.callingCoroutine_ )
promise.callingCoroutine_.resume();
if( !promise.awaited_ )
thisCoroutine.destroy();
}
void await_resume() noexcept {}
};
auto initial_suspend() noexcept { return std_coroutine::suspend_never{}; }
auto final_suspend() noexcept { return ResumeContinuation{}; }
void return_value( R result ) { result_ = result; }
void unhandled_exception() noexcept
{
exception_ = std::current_exception();
}
R result_ = {};
std_coroutine::coroutine_handle<> callingCoroutine_ = {};
std::exception_ptr exception_ = {};
bool sync_ = true;
bool awaited_ = true;
};
Continuation( const Continuation& ) = delete;
Continuation& operator=( const Continuation& ) = delete;
Continuation& operator=( Continuation&& ) noexcept = delete;
Continuation() noexcept = default;
Continuation( Continuation&& t ) noexcept = default;
explicit Continuation( std_coroutine::coroutine_handle< promise_type > coroutine ) : coroutine_( coroutine ) {}
~Continuation() noexcept
{
if( coroutine_.promise().sync_ )
coroutine_.destroy();
else
coroutine_.promise().awaited_ = false;
}
bool await_ready() const noexcept{ return false; }
void await_suspend( auto callingCoroutine ) noexcept
{
if( coroutine_.promise().sync_ )
callingCoroutine.resume();
else
BuildAsyncChain( this->coroutine_, callingCoroutine );
}
auto await_resume()
{
if( auto exception = coroutine_.promise().exception_ )
std::rethrow_exception( exception );
auto result = coroutine_.promise().result_;
if( !coroutine_.promise().awaited_ )
coroutine_.destroy();
return result;
}
std_coroutine::coroutine_handle< promise_type > coroutine_;
};
template< typename R > using AsyncApiCallback = std::function< void( R ) >;
template< typename R > using AsyncApi = std::function< void( AsyncApiCallback< R > ) >;
template< typename R >
struct AsyncCallbackContinuationAwaiter
{
bool await_ready() { return false; }
void await_suspend( auto callingContinuation )
{
callingContinuation.promise().sync_ = false;
api_( [ this, callingContinuation ]( const R& r ) mutable
{
result_ = r;
callingContinuation.resume();
});
}
R await_resume() { return result_; }
const AsyncApi< R > api_;
R result_ = {};
};
template< typename R >
auto AsyncCallbackContinuation( AsyncApi< R > api )
{
return AsyncCallbackContinuationAwaiter< R >{ api };
}
// TEST
#define BOOST_TEST( x ) \
if( !( x ) ) \
std::cout << "Error: " << #x << std::endl;
#define BOOST_TEST_MESSAGE( x ) \
std::cout << x << std::endl;
namespace Fixture
{
std::thread t;
bool continuationsRun = false;
void apiAsync( const std::function< void( int ) >& callback )
{
t = std::thread( [ = ]
{
std::this_thread::sleep_for( std::chrono::seconds{ 1 } );
callback( 41 );
});
}
int apiSync()
{
return 41;
}
Continuation< int > Test1Async()
{
int initalValue = 1;
BOOST_TEST_MESSAGE( "Start Test1Async" );
int x = co_await AsyncCallbackContinuation< int >( &apiAsync );
x += initalValue;
BOOST_TEST( x == 42 );
BOOST_TEST_MESSAGE( "Test1Async" );
co_return x += 1;
}
Continuation< int > Test1Sync()
{
int initalValue = 1;
BOOST_TEST_MESSAGE( "Start Test1Sync" );
int x = 41;
x += initalValue;
BOOST_TEST( x == 42 );
BOOST_TEST_MESSAGE( "Test1Sync" );
co_return x += 1;
}
Continuation< int > Test1SyncWithException()
{
BOOST_TEST_MESSAGE( "Start Test1SyncWithException" );
throw std::runtime_error( "TestException" );
co_return 42;
}
Continuation< int > Test1AsyncWithException()
{
BOOST_TEST_MESSAGE( "Start Test1AsyncWithException" );
int x = co_await AsyncCallbackContinuation< int >( &apiAsync );
throw std::runtime_error( "TestException" );
co_return 42;
}
Continuation< double > Test2( auto&& test1 )
{
BOOST_TEST_MESSAGE( "Start Test2" );
auto x = co_await test1();
BOOST_TEST( x == 43 );
BOOST_TEST_MESSAGE( "Test2" );
co_return x + 1.0;
}
Continuation< double > Test3( auto&& callbackContinuation )
{
BOOST_TEST_MESSAGE( "Start Test3" );
auto x = co_await Test2( callbackContinuation );
BOOST_TEST( x == 44.0 );
BOOST_TEST_MESSAGE( "Test3" );
co_return x + 1.0;
}
Continuation<> Test4( auto&& callbackContinuation )
{
BOOST_TEST_MESSAGE( "Start Test4" );
auto x = co_await Test3( callbackContinuation );
BOOST_TEST( x == 45.0 );
BOOST_TEST_MESSAGE( "Test4" );
continuationsRun = true;
co_return {};
}
Continuation<> Test5Catched( auto&& throws )
{
BOOST_TEST_MESSAGE( "Start Test5Catched" );
continuationsRun = false;
try
{
co_await Test4( throws );
BOOST_TEST( false );
}
catch( std::runtime_error& e )
{
BOOST_TEST( e.what() == std::string( "TestException" ) );
}
BOOST_TEST( !continuationsRun );
co_return {};
}
}
}
int main()
{
BOOST_TEST_MESSAGE( "main start" );
{
BOOST_TEST_MESSAGE( "Test1Sync start" );
{
auto x = Fixture::Test1Sync();
}
BOOST_TEST_MESSAGE( ContinuationPromiseCount );
BOOST_TEST( ContinuationPromiseCount == 0 );
BOOST_TEST_MESSAGE( "synchron start" );
{
Fixture::continuationsRun = false;
Fixture::Test4( &Fixture::Test1Sync );
BOOST_TEST( Fixture::continuationsRun );
}
BOOST_TEST_MESSAGE( ContinuationPromiseCount );
BOOST_TEST( ContinuationPromiseCount == 0 );
BOOST_TEST_MESSAGE( "synchron start with exception" );
{
Fixture::Test5Catched( &Fixture::Test1SyncWithException );
}
BOOST_TEST_MESSAGE( ContinuationPromiseCount );
BOOST_TEST( ContinuationPromiseCount == 0 );
BOOST_TEST_MESSAGE( "asynchron simple start with exception" );
{
[]()->Continuation<>
{
BOOST_TEST_MESSAGE( "asynchron simple start with exception Catched" );
try
{
co_await Fixture::Test1AsyncWithException();
BOOST_TEST( false );
}
catch( std::runtime_error& e )
{
BOOST_TEST( e.what() == std::string( "TestException" ) );
}
co_return {};
}();
Fixture::t.join();
}
BOOST_TEST_MESSAGE( "asynchron start with exception" );
{
Fixture::Test5Catched( &Fixture::Test1AsyncWithException );
Fixture::t.join();
}
BOOST_TEST_MESSAGE( ContinuationPromiseCount );
BOOST_TEST( ContinuationPromiseCount == 0 );
BOOST_TEST_MESSAGE( "asynchron start" );
{
Fixture::continuationsRun = false;
Fixture::Test4( &Fixture::Test1Async );
BOOST_TEST( !Fixture::continuationsRun );
}
BOOST_TEST( !Fixture::continuationsRun );
BOOST_TEST_MESSAGE( "main after Test4" );
//std::this_thread::sleep_for( std::chrono::seconds{ 1 } );
BOOST_TEST_MESSAGE( "main after sleep" );
Fixture::t.join();
BOOST_TEST( Fixture::continuationsRun );
BOOST_TEST_MESSAGE( "after join" );
BOOST_TEST_MESSAGE( ContinuationPromiseCount );
BOOST_TEST( ContinuationPromiseCount == 0 );
}
}