forked from lewissbaker/cppcoro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.hpp
More file actions
481 lines (388 loc) · 11.3 KB
/
task.hpp
File metadata and controls
481 lines (388 loc) · 11.3 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Lewis Baker
// Licenced under MIT license. See LICENSE.txt for details.
///////////////////////////////////////////////////////////////////////////////
#ifndef CPPCORO_TASK_HPP_INCLUDED
#define CPPCORO_TASK_HPP_INCLUDED
#include <cppcoro/config.hpp>
#include <cppcoro/awaitable_traits.hpp>
#include <cppcoro/broken_promise.hpp>
#include <cppcoro/detail/remove_rvalue_reference.hpp>
#include <atomic>
#include <exception>
#include <utility>
#include <type_traits>
#include <cstdint>
#include <cassert>
#include <experimental/coroutine>
namespace cppcoro
{
template<typename T> class task;
namespace detail
{
class task_promise_base
{
friend struct final_awaitable;
struct final_awaitable
{
bool await_ready() const noexcept { return false; }
#if CPPCORO_COMPILER_SUPPORTS_SYMMETRIC_TRANSFER
template<typename PROMISE>
std::experimental::coroutine_handle<> await_suspend(
std::experimental::coroutine_handle<PROMISE> coro) noexcept
{
return coro.promise().m_continuation;
}
#else
// HACK: Need to add CPPCORO_NOINLINE to await_suspend() method
// to avoid MSVC 2017.8 from spilling some local variables in
// await_suspend() onto the coroutine frame in some cases.
// Without this, some tests in async_auto_reset_event_tests.cpp
// were crashing under x86 optimised builds.
template<typename PROMISE>
CPPCORO_NOINLINE
void await_suspend(std::experimental::coroutine_handle<PROMISE> coroutine)
{
task_promise_base& promise = coroutine.promise();
// Use 'release' memory semantics in case we finish before the
// awaiter can suspend so that the awaiting thread sees our
// writes to the resulting value.
// Use 'acquire' memory semantics in case the caller registered
// the continuation before we finished. Ensure we see their write
// to m_continuation.
if (promise.m_state.exchange(true, std::memory_order_acq_rel))
{
promise.m_continuation.resume();
}
}
#endif
void await_resume() noexcept {}
};
public:
task_promise_base() noexcept
#if !CPPCORO_COMPILER_SUPPORTS_SYMMETRIC_TRANSFER
: m_state(false)
#endif
{}
auto initial_suspend() noexcept
{
return std::experimental::suspend_always{};
}
auto final_suspend() noexcept
{
return final_awaitable{};
}
#if CPPCORO_COMPILER_SUPPORTS_SYMMETRIC_TRANSFER
void set_continuation(std::experimental::coroutine_handle<> continuation) noexcept
{
m_continuation = continuation;
}
#else
bool try_set_continuation(std::experimental::coroutine_handle<> continuation)
{
m_continuation = continuation;
return !m_state.exchange(true, std::memory_order_acq_rel);
}
#endif
private:
std::experimental::coroutine_handle<> m_continuation;
#if !CPPCORO_COMPILER_SUPPORTS_SYMMETRIC_TRANSFER
// Initially false. Set to true when either a continuation is registered
// or when the coroutine has run to completion. Whichever operation
// successfully transitions from false->true got there first.
std::atomic<bool> m_state;
#endif
};
template<typename T>
class task_promise final : public task_promise_base
{
public:
task_promise() noexcept {}
~task_promise()
{
switch (m_resultType)
{
case result_type::value:
m_value.~T();
break;
case result_type::exception:
m_exception.~exception_ptr();
break;
default:
break;
}
}
task<T> get_return_object() noexcept;
void unhandled_exception() noexcept
{
::new (static_cast<void*>(std::addressof(m_exception))) std::exception_ptr(
std::current_exception());
m_resultType = result_type::exception;
}
template<
typename VALUE,
typename = std::enable_if_t<std::is_convertible_v<VALUE&&, T>>>
void return_value(VALUE&& value)
noexcept(std::is_nothrow_constructible_v<T, VALUE&&>)
{
::new (static_cast<void*>(std::addressof(m_value))) T(std::forward<VALUE>(value));
m_resultType = result_type::value;
}
T& result() &
{
if (m_resultType == result_type::exception)
{
std::rethrow_exception(m_exception);
}
assert(m_resultType == result_type::value);
return m_value;
}
// HACK: Need to have co_await of task<int> return prvalue rather than
// rvalue-reference to work around an issue with MSVC where returning
// rvalue reference of a fundamental type from await_resume() will
// cause the value to be copied to a temporary. This breaks the
// sync_wait() implementation.
// See https://github.com/lewissbaker/cppcoro/issues/40#issuecomment-326864107
using rvalue_type = std::conditional_t<
std::is_arithmetic_v<T> || std::is_pointer_v<T>,
T,
T&&>;
rvalue_type result() &&
{
if (m_resultType == result_type::exception)
{
std::rethrow_exception(m_exception);
}
assert(m_resultType == result_type::value);
return std::move(m_value);
}
private:
enum class result_type { empty, value, exception };
result_type m_resultType = result_type::empty;
union
{
T m_value;
std::exception_ptr m_exception;
};
};
template<>
class task_promise<void> : public task_promise_base
{
public:
task_promise() noexcept = default;
task<void> get_return_object() noexcept;
void return_void() noexcept
{}
void unhandled_exception() noexcept
{
m_exception = std::current_exception();
}
void result()
{
if (m_exception)
{
std::rethrow_exception(m_exception);
}
}
private:
std::exception_ptr m_exception;
};
template<typename T>
class task_promise<T&> : public task_promise_base
{
public:
task_promise() noexcept = default;
task<T&> get_return_object() noexcept;
void unhandled_exception() noexcept
{
m_exception = std::current_exception();
}
void return_value(T& value) noexcept
{
m_value = std::addressof(value);
}
T& result()
{
if (m_exception)
{
std::rethrow_exception(m_exception);
}
return *m_value;
}
private:
T* m_value = nullptr;
std::exception_ptr m_exception;
};
}
/// \brief
/// A task represents an operation that produces a result both lazily
/// and asynchronously.
///
/// When you call a coroutine that returns a task, the coroutine
/// simply captures any passed parameters and returns exeuction to the
/// caller. Execution of the coroutine body does not start until the
/// coroutine is first co_await'ed.
template<typename T = void>
class [[nodiscard]] task
{
public:
using promise_type = detail::task_promise<T>;
using value_type = T;
private:
struct awaitable_base
{
std::experimental::coroutine_handle<promise_type> m_coroutine;
awaitable_base(std::experimental::coroutine_handle<promise_type> coroutine) noexcept
: m_coroutine(coroutine)
{}
bool await_ready() const noexcept
{
return !m_coroutine || m_coroutine.done();
}
#if CPPCORO_COMPILER_SUPPORTS_SYMMETRIC_TRANSFER
std::experimental::coroutine_handle<> await_suspend(
std::experimental::coroutine_handle<> awaitingCoroutine) noexcept
{
m_coroutine.promise().set_continuation(awaitingCoroutine);
return m_coroutine;
}
#else
bool await_suspend(std::experimental::coroutine_handle<> awaitingCoroutine) noexcept
{
// NOTE: We are using the bool-returning version of await_suspend() here
// to work around a potential stack-overflow issue if a coroutine
// awaits many synchronously-completing tasks in a loop.
//
// We first start the task by calling resume() and then conditionally
// attach the continuation if it has not already completed. This allows us
// to immediately resume the awaiting coroutine without increasing
// the stack depth, avoiding the stack-overflow problem. However, it has
// the down-side of requiring a std::atomic to arbitrate the race between
// the coroutine potentially completing on another thread concurrently
// with registering the continuation on this thread.
//
// We can eliminate the use of the std::atomic once we have access to
// coroutine_handle-returning await_suspend() on both MSVC and Clang
// as this will provide ability to suspend the awaiting coroutine and
// resume another coroutine with a guaranteed tail-call to resume().
m_coroutine.resume();
return m_coroutine.promise().try_set_continuation(awaitingCoroutine);
}
#endif
};
public:
task() noexcept
: m_coroutine(nullptr)
{}
explicit task(std::experimental::coroutine_handle<promise_type> coroutine)
: m_coroutine(coroutine)
{}
task(task&& t) noexcept
: m_coroutine(t.m_coroutine)
{
t.m_coroutine = nullptr;
}
/// Disable copy construction/assignment.
task(const task&) = delete;
task& operator=(const task&) = delete;
/// Frees resources used by this task.
~task()
{
if (m_coroutine)
{
m_coroutine.destroy();
}
}
task& operator=(task&& other) noexcept
{
if (std::addressof(other) != this)
{
if (m_coroutine)
{
m_coroutine.destroy();
}
m_coroutine = other.m_coroutine;
other.m_coroutine = nullptr;
}
return *this;
}
/// \brief
/// Query if the task result is complete.
///
/// Awaiting a task that is ready is guaranteed not to block/suspend.
bool is_ready() const noexcept
{
return !m_coroutine || m_coroutine.done();
}
auto operator co_await() const & noexcept
{
struct awaitable : awaitable_base
{
using awaitable_base::awaitable_base;
decltype(auto) await_resume()
{
if (!this->m_coroutine)
{
throw broken_promise{};
}
return this->m_coroutine.promise().result();
}
};
return awaitable{ m_coroutine };
}
auto operator co_await() const && noexcept
{
struct awaitable : awaitable_base
{
using awaitable_base::awaitable_base;
decltype(auto) await_resume()
{
if (!this->m_coroutine)
{
throw broken_promise{};
}
return std::move(this->m_coroutine.promise()).result();
}
};
return awaitable{ m_coroutine };
}
/// \brief
/// Returns an awaitable that will await completion of the task without
/// attempting to retrieve the result.
auto when_ready() const noexcept
{
struct awaitable : awaitable_base
{
using awaitable_base::awaitable_base;
void await_resume() const noexcept {}
};
return awaitable{ m_coroutine };
}
private:
std::experimental::coroutine_handle<promise_type> m_coroutine;
};
namespace detail
{
template<typename T>
task<T> task_promise<T>::get_return_object() noexcept
{
return task<T>{ std::experimental::coroutine_handle<task_promise>::from_promise(*this) };
}
inline task<void> task_promise<void>::get_return_object() noexcept
{
return task<void>{ std::experimental::coroutine_handle<task_promise>::from_promise(*this) };
}
template<typename T>
task<T&> task_promise<T&>::get_return_object() noexcept
{
return task<T&>{ std::experimental::coroutine_handle<task_promise>::from_promise(*this) };
}
}
template<typename AWAITABLE>
auto make_task(AWAITABLE awaitable)
-> task<detail::remove_rvalue_reference_t<typename awaitable_traits<AWAITABLE>::await_result_t>>
{
co_return co_await static_cast<AWAITABLE&&>(awaitable);
}
}
#endif