-
Notifications
You must be signed in to change notification settings - Fork 505
Expand file tree
/
Copy pathshared_task.hpp
More file actions
511 lines (412 loc) · 12.2 KB
/
shared_task.hpp
File metadata and controls
511 lines (412 loc) · 12.2 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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Lewis Baker
// Licenced under MIT license. See LICENSE.txt for details.
///////////////////////////////////////////////////////////////////////////////
#ifndef CPPCORO_SHARED_LAZY_TASK_HPP_INCLUDED
#define CPPCORO_SHARED_LAZY_TASK_HPP_INCLUDED
#include <cppcoro/config.hpp>
#include <cppcoro/awaitable_traits.hpp>
#include <cppcoro/broken_promise.hpp>
#include <cppcoro/task.hpp>
#include <cppcoro/detail/remove_rvalue_reference.hpp>
#include <atomic>
#include <exception>
#include <utility>
#include <type_traits>
#include <experimental/coroutine>
namespace cppcoro
{
template<typename T>
class shared_task;
namespace detail
{
struct shared_task_waiter
{
std::experimental::coroutine_handle<> m_continuation;
shared_task_waiter* m_next;
};
class shared_task_promise_base
{
friend struct final_awaiter;
struct final_awaiter
{
bool await_ready() const noexcept { return false; }
template<typename PROMISE>
void await_suspend(std::experimental::coroutine_handle<PROMISE> h) noexcept
{
shared_task_promise_base& promise = h.promise();
// Exchange operation needs to be 'release' so that subsequent awaiters have
// visibility of the result. Also needs to be 'acquire' so we have visibility
// of writes to the waiters list.
void* const valueReadyValue = &promise;
void* waiters = promise.m_waiters.exchange(valueReadyValue, std::memory_order_acq_rel);
if (waiters != nullptr)
{
shared_task_waiter* waiter = static_cast<shared_task_waiter*>(waiters);
while (waiter->m_next != nullptr)
{
// Read the m_next pointer before resuming the coroutine
// since resuming the coroutine may destroy the shared_task_waiter value.
auto* next = waiter->m_next;
waiter->m_continuation.resume();
waiter = next;
}
// Resume last waiter in tail position to allow it to potentially
// be compiled as a tail-call.
waiter->m_continuation.resume();
}
}
void await_resume() noexcept {}
};
public:
shared_task_promise_base() noexcept
: m_refCount(1)
, m_waiters(&this->m_waiters)
, m_exception(nullptr)
{}
std::experimental::suspend_always initial_suspend() noexcept { return {}; }
final_awaiter final_suspend() noexcept { return {}; }
void unhandled_exception() noexcept
{
m_exception = std::current_exception();
}
bool is_ready() const noexcept
{
const void* const valueReadyValue = this;
return m_waiters.load(std::memory_order_acquire) == valueReadyValue;
}
void add_ref() noexcept
{
m_refCount.fetch_add(1, std::memory_order_relaxed);
}
/// Decrement the reference count.
///
/// \return
/// true if successfully detached, false if this was the last
/// reference to the coroutine, in which case the caller must
/// call destroy() on the coroutine handle.
bool try_detach() noexcept
{
return m_refCount.fetch_sub(1, std::memory_order_acq_rel) != 1;
}
/// Try to enqueue a waiter to the list of waiters.
///
/// \param waiter
/// Pointer to the state from the waiter object.
/// Must have waiter->m_coroutine member populated with the coroutine
/// handle of the awaiting coroutine.
///
/// \param coroutine
/// Coroutine handle for this promise object.
///
/// \return
/// true if the waiter was successfully queued, in which case
/// waiter->m_coroutine will be resumed when the task completes.
/// false if the coroutine was already completed and the awaiting
/// coroutine can continue without suspending.
bool try_await(shared_task_waiter* waiter, std::experimental::coroutine_handle<> coroutine)
{
void* const valueReadyValue = this;
void* const notStartedValue = &this->m_waiters;
constexpr void* startedNoWaitersValue = static_cast<shared_task_waiter*>(nullptr);
// NOTE: If the coroutine is not yet started then the first waiter
// will start the coroutine before enqueuing itself up to the list
// of suspended waiters waiting for completion. We split this into
// two steps to allow the first awaiter to return without suspending.
// This avoids recursively resuming the first waiter inside the call to
// coroutine.resume() in the case that the coroutine completes
// synchronously, which could otherwise lead to stack-overflow if
// the awaiting coroutine awaited many synchronously-completing
// tasks in a row.
// Start the coroutine if not already started.
void* oldWaiters = m_waiters.load(std::memory_order_acquire);
if (oldWaiters == notStartedValue &&
m_waiters.compare_exchange_strong(
oldWaiters,
startedNoWaitersValue,
std::memory_order_relaxed))
{
// Start the task executing.
coroutine.resume();
oldWaiters = m_waiters.load(std::memory_order_acquire);
}
// Enqueue the waiter into the list of waiting coroutines.
do
{
if (oldWaiters == valueReadyValue)
{
// Coroutine already completed, don't suspend.
return false;
}
waiter->m_next = static_cast<shared_task_waiter*>(oldWaiters);
} while (!m_waiters.compare_exchange_weak(
oldWaiters,
static_cast<void*>(waiter),
std::memory_order_release,
std::memory_order_acquire));
return true;
}
protected:
bool completed_with_unhandled_exception()
{
return m_exception != nullptr;
}
void rethrow_if_unhandled_exception()
{
if (m_exception != nullptr)
{
std::rethrow_exception(m_exception);
}
}
private:
std::atomic<std::uint32_t> m_refCount;
// Value is either
// - nullptr - indicates started, no waiters
// - this - indicates value is ready
// - &this->m_waiters - indicates coroutine not started
// - other - pointer to head item in linked-list of waiters.
// values are of type 'cppcoro::shared_task_waiter'.
// indicates that the coroutine has been started.
std::atomic<void*> m_waiters;
std::exception_ptr m_exception;
};
template<typename T>
class shared_task_promise : public shared_task_promise_base
{
public:
shared_task_promise() noexcept = default;
~shared_task_promise()
{
if (this->is_ready() && !this->completed_with_unhandled_exception())
{
reinterpret_cast<T*>(&m_valueStorage)->~T();
}
}
shared_task<T> get_return_object() noexcept;
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 (&m_valueStorage) T(std::forward<VALUE>(value));
}
T& result()
{
this->rethrow_if_unhandled_exception();
return *reinterpret_cast<T*>(&m_valueStorage);
}
private:
// Not using std::aligned_storage here due to bug in MSVC 2015 Update 2
// that means it doesn't work for types with alignof(T) > 8.
// See MS-Connect bug #2658635.
alignas(T) char m_valueStorage[sizeof(T)];
};
template<>
class shared_task_promise<void> : public shared_task_promise_base
{
public:
shared_task_promise() noexcept = default;
shared_task<void> get_return_object() noexcept;
void return_void() noexcept
{}
void result()
{
this->rethrow_if_unhandled_exception();
}
};
template<typename T>
class shared_task_promise<T&> : public shared_task_promise_base
{
public:
shared_task_promise() noexcept = default;
shared_task<T&> get_return_object() noexcept;
void return_value(T& value) noexcept
{
m_value = std::addressof(value);
}
T& result()
{
this->rethrow_if_unhandled_exception();
return *m_value;
}
private:
T* m_value;
};
}
template<typename T = void>
class [[nodiscard]] shared_task
{
public:
using promise_type = detail::shared_task_promise<T>;
using value_type = T;
private:
struct awaitable_base
{
std::experimental::coroutine_handle<promise_type> m_coroutine;
detail::shared_task_waiter m_waiter;
awaitable_base(std::experimental::coroutine_handle<promise_type> coroutine) noexcept
: m_coroutine(coroutine)
{}
bool await_ready() const noexcept
{
return !m_coroutine || m_coroutine.promise().is_ready();
}
bool await_suspend(std::experimental::coroutine_handle<> awaiter) noexcept
{
m_waiter.m_continuation = awaiter;
return m_coroutine.promise().try_await(&m_waiter, m_coroutine);
}
};
public:
shared_task() noexcept
: m_coroutine(nullptr)
{}
explicit shared_task(std::experimental::coroutine_handle<promise_type> coroutine)
: m_coroutine(coroutine)
{
// Don't increment the ref-count here since it has already been
// initialised to 2 (one for shared_task and one for coroutine)
// in the shared_task_promise constructor.
}
shared_task(shared_task&& other) noexcept
: m_coroutine(other.m_coroutine)
{
other.m_coroutine = nullptr;
}
shared_task(const shared_task& other) noexcept
: m_coroutine(other.m_coroutine)
{
if (m_coroutine)
{
m_coroutine.promise().add_ref();
}
}
~shared_task()
{
destroy();
}
shared_task& operator=(shared_task&& other) noexcept
{
if (&other != this)
{
destroy();
m_coroutine = other.m_coroutine;
other.m_coroutine = nullptr;
}
return *this;
}
shared_task& operator=(const shared_task& other) noexcept
{
if (m_coroutine != other.m_coroutine)
{
destroy();
m_coroutine = other.m_coroutine;
if (m_coroutine)
{
m_coroutine.promise().add_ref();
}
}
return *this;
}
void swap(shared_task& other) noexcept
{
std::swap(m_coroutine, other.m_coroutine);
}
/// \brief
/// Query if the task result is complete.
///
/// Awaiting a task that is ready will not block.
bool is_ready() const noexcept
{
return !m_coroutine || m_coroutine.promise().is_ready();
}
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 };
}
/// \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:
template<typename U>
friend bool operator==(const shared_task<U>&, const shared_task<U>&) noexcept;
void destroy() noexcept
{
if (m_coroutine)
{
if (!m_coroutine.promise().try_detach())
{
m_coroutine.destroy();
}
}
}
std::experimental::coroutine_handle<promise_type> m_coroutine;
};
template<typename T>
bool operator==(const shared_task<T>& lhs, const shared_task<T>& rhs) noexcept
{
return lhs.m_coroutine == rhs.m_coroutine;
}
template<typename T>
bool operator!=(const shared_task<T>& lhs, const shared_task<T>& rhs) noexcept
{
return !(lhs == rhs);
}
template<typename T>
void swap(shared_task<T>& a, shared_task<T>& b) noexcept
{
a.swap(b);
}
namespace detail
{
template<typename T>
shared_task<T> shared_task_promise<T>::get_return_object() noexcept
{
return shared_task<T>{
std::experimental::coroutine_handle<shared_task_promise>::from_promise(*this)
};
}
template<typename T>
shared_task<T&> shared_task_promise<T&>::get_return_object() noexcept
{
return shared_task<T&>{
std::experimental::coroutine_handle<shared_task_promise>::from_promise(*this)
};
}
inline shared_task<void> shared_task_promise<void>::get_return_object() noexcept
{
return shared_task<void>{
std::experimental::coroutine_handle<shared_task_promise>::from_promise(*this)
};
}
}
template<typename AWAITABLE>
auto make_shared_task(AWAITABLE awaitable)
-> shared_task<detail::remove_rvalue_reference_t<typename awaitable_traits<AWAITABLE>::await_result_t>>
{
co_return co_await static_cast<AWAITABLE&&>(awaitable);
}
}
#endif