-
Notifications
You must be signed in to change notification settings - Fork 143
/
execution.hpp
1949 lines (1715 loc) · 70.6 KB
/
execution.hpp
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) NVIDIA
*
* Licensed under the Apache License Version 2.0 with LLVM Exceptions
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://llvm.org/LICENSE.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <cassert>
#include <condition_variable>
#include <stdexcept>
#include <mutex>
#include <optional>
#include <tuple>
#include <type_traits>
#include <variant>
#include <any>
#include <__utility.hpp>
#include <functional.hpp>
#include <concepts.hpp>
#include <coroutine.hpp>
#include <stop_token.hpp>
namespace std::execution {
template<template<template<class...> class, template<class...> class> class>
struct __test_has_values;
template<template<template<class...> class> class>
struct __test_has_errors;
template<class T>
concept __has_sender_types = requires {
typename __test_has_values<T::template value_types>;
typename __test_has_errors<T::template error_types>;
typename bool_constant<T::sends_done>;
};
/////////////////////////////////////////////////////////////////////////////
// [execution.senders.traits]
using sender_base = struct __sender_base {};
struct __no_sender_traits {
using __unspecialized = void;
};
template <bool SendsDone>
struct __void_sender {
template<template<class...> class Tuple, template<class...> class Variant>
using value_types = Variant<Tuple<>>;
template<template<class...> class Variant>
using error_types = Variant<std::exception_ptr>;
static constexpr bool sends_done = SendsDone;
};
template <bool SendsDone, class... Ts>
struct __sender_of {
template<template<class...> class Tuple, template<class...> class Variant>
using value_types = Variant<Tuple<Ts...>>;
template<template<class...> class Variant>
using error_types = Variant<std::exception_ptr>;
static constexpr bool sends_done = SendsDone;
};
template<class S>
struct __typed_sender {
template<template<class...> class Tuple, template<class...> class Variant>
using value_types = typename S::template value_types<Tuple, Variant>;
template<template<class...> class Variant>
using error_types = typename S::template error_types<Variant>;
static constexpr bool sends_done = S::sends_done;
};
template<class S>
auto __sender_traits_base_fn() {
if constexpr (__has_sender_types<S>) {
return __typed_sender<S>{};
} else if constexpr (derived_from<S, sender_base>) {
return sender_base{};
} else if constexpr (__awaitable<S>) { // NOT TO SPEC
if constexpr (is_void_v<__await_result_t<S>>) {
return __void_sender<false>{};
} else {
return __sender_of<false, __await_result_t<S>>{};
}
} else {
return __no_sender_traits{};
}
}
/////////////////////////////////////////////////////////////////////////////
// [execution.senders.traits]
template<class S>
struct sender_traits
: decltype(__sender_traits_base_fn<S>()) {};
/////////////////////////////////////////////////////////////////////////////
// [execution.receivers]
inline namespace __receiver_cpo {
inline constexpr struct set_value_t {
template<class R, class... As>
requires tag_invocable<set_value_t, R, As...>
void operator()(R&& r, As&&... as) const
noexcept(nothrow_tag_invocable<set_value_t, R, As...>) {
(void) tag_invoke(set_value_t{}, (R&&) r, (As&&) as...);
}
} set_value{};
inline constexpr struct set_error_t {
template<class R, class E>
requires tag_invocable<set_error_t, R, E>
void operator()(R&& r, E&& e) const
noexcept(nothrow_tag_invocable<set_error_t, R, E>) {
(void) tag_invoke(set_error_t{}, (R&&) r, (E&&) e);
}
} set_error {};
inline constexpr struct set_done_t {
template<class R>
requires tag_invocable<set_done_t, R>
void operator()(R&& r) const
noexcept(nothrow_tag_invocable<set_done_t, R>) {
(void) tag_invoke(set_done_t{}, (R&&) r);
}
} set_done{};
}
/////////////////////////////////////////////////////////////////////////////
// [execution.receivers]
template<class R, class E = exception_ptr>
concept receiver =
move_constructible<remove_cvref_t<R>> &&
constructible_from<remove_cvref_t<R>, R> &&
requires(remove_cvref_t<R>&& r, E&& e) {
{ set_done(std::move(r)) } noexcept;
{ set_error(std::move(r), (E&&) e) } noexcept;
};
template<class R, class... An>
concept receiver_of =
receiver<R> &&
requires(remove_cvref_t<R>&& r, An&&... an) {
set_value((remove_cvref_t<R>&&) r, (An&&) an...);
};
// NOT TO SPEC
template<class R, class...As>
inline constexpr bool nothrow_receiver_of =
receiver_of<R, As...> &&
nothrow_tag_invocable<set_value_t, R, As...>;
/////////////////////////////////////////////////////////////////////////////
// [execution.senders]
template<class S>
concept sender =
move_constructible<remove_cvref_t<S>> &&
!requires {
typename sender_traits<remove_cvref_t<S>>::__unspecialized;
};
template<class S>
concept typed_sender =
sender<S> &&
__has_sender_types<sender_traits<remove_cvref_t<S>>>;
template <class... As>
requires (sizeof...(As) != 0)
struct __front;
template <class A, class... As>
struct __front<A, As...> {
using type = A;
};
template <class... As>
requires (sizeof...(As) == 1)
using __single_t = __t<__front<As...>>;
template <class... As>
requires (sizeof...(As) <= 1)
using __single_or_void_t = __t<__front<As..., void>>;
template<class S>
using __single_sender_value_t =
typename sender_traits<remove_cvref_t<S>>
::template value_types<__single_or_void_t, __single_or_void_t>;
template<class S>
concept __single_typed_sender =
typed_sender<S> &&
requires { typename __single_sender_value_t<S>; };
/////////////////////////////////////////////////////////////////////////////
// [execution.senders.schedule]
inline namespace __schedule {
inline constexpr struct schedule_t {
template<class S>
requires tag_invocable<schedule_t, S> &&
sender<tag_invoke_result_t<schedule_t, S>>
auto operator()(S&& s) const
noexcept(nothrow_tag_invocable<schedule_t, S>) {
return tag_invoke(schedule_t{}, (S&&) s);
}
} schedule {};
}
/////////////////////////////////////////////////////////////////////////////
// [execution.schedulers]
template<class S>
concept scheduler =
copy_constructible<remove_cvref_t<S>> &&
equality_comparable<remove_cvref_t<S>> &&
requires(S&& s) {
schedule((S&&) s);
};
// NOT TO SPEC
template <scheduler S>
using schedule_result_t = decltype(schedule(std::declval<S>()));
// [execution.receivers.queries], receiver queries
inline namespace __receiver_queries {
namespace __impl {
template <class T>
using __cref_t = const remove_reference_t<T>&;
// TODO: implement allocator concept
template <class A>
concept __allocator = true;
struct get_scheduler_t {
template <receiver R>
requires nothrow_tag_invocable<get_scheduler_t, __cref_t<R>> &&
scheduler<tag_invoke_result_t<get_scheduler_t, __cref_t<R>>>
tag_invoke_result_t<get_scheduler_t, __cref_t<R>> operator()(R&& r) const
noexcept(nothrow_tag_invocable<get_scheduler_t, __cref_t<R>>) {
return tag_invoke(get_scheduler_t{}, std::as_const(r));
}
};
struct get_allocator_t {
template <receiver R>
requires nothrow_tag_invocable<get_allocator_t, __cref_t<R>> &&
__allocator<tag_invoke_result_t<get_allocator_t, __cref_t<R>>>
tag_invoke_result_t<get_allocator_t, __cref_t<R>> operator()(R&& r) const
noexcept(nothrow_tag_invocable<get_allocator_t, __cref_t<R>>) {
return tag_invoke(get_allocator_t{}, std::as_const(r));
}
};
struct get_stop_token_t {
template <receiver R>
requires tag_invocable<get_stop_token_t, __cref_t<R>> &&
stoppable_token<tag_invoke_result_t<get_stop_token_t, __cref_t<R>>>
tag_invoke_result_t<get_stop_token_t, __cref_t<R>> operator()(R&& r) const
noexcept(nothrow_tag_invocable<get_stop_token_t, __cref_t<R>>) {
return tag_invoke(get_stop_token_t{}, std::as_const(r));
}
never_stop_token operator()(receiver auto&&) const noexcept {
return {};
}
};
}
using __impl::get_allocator_t;
using __impl::get_scheduler_t;
using __impl::get_stop_token_t;
inline constexpr get_scheduler_t get_scheduler{};
inline constexpr get_allocator_t get_allocator{};
inline constexpr get_stop_token_t get_stop_token{};
}
// NOT TO SPEC
template <class R>
using stop_token_type_t = remove_cvref_t<decltype(get_stop_token(std::declval<R>()))>;
/////////////////////////////////////////////////////////////////////////////
// [execution.op_state]
inline namespace __start {
inline constexpr struct start_t {
template<class O>
requires tag_invocable<start_t, O&>
void operator()(O& o) const noexcept(nothrow_tag_invocable<start_t, O&>) {
(void) tag_invoke(start_t{}, o);
}
} start {};
}
/////////////////////////////////////////////////////////////////////////////
// [execution.op_state]
template<class O>
concept operation_state =
destructible<O> &&
is_object_v<O> &&
requires (O& o) {
{start(o)} noexcept;
};
/////////////////////////////////////////////////////////////////////////////
// NOT TO SPEC: __connect_awaitable_
inline namespace __connect_awaitable_ {
namespace __impl {
struct __op_base {
struct __promise_base {
coro::suspend_always initial_suspend() noexcept {
return {};
}
[[noreturn]] coro::suspend_always final_suspend() noexcept {
terminate();
}
[[noreturn]] void unhandled_exception() noexcept {
terminate();
}
[[noreturn]] void return_void() noexcept {
terminate();
}
template <class Func>
auto yield_value(Func&& func) noexcept {
struct awaiter {
Func&& func_;
bool await_ready() noexcept {
return false;
}
void await_suspend(coro::coroutine_handle<>)
noexcept(is_nothrow_invocable_v<Func>) {
// If this throws, the runtime catches the exception,
// resumes the __connect_awaitable coroutine, and immediately
// rethrows the exception. The end result is that an
// exception_ptr to the exception gets passed to set_error.
((Func &&) func_)();
}
[[noreturn]] void await_resume() noexcept {
terminate();
}
};
return awaiter{(Func &&) func};
}
};
coro::coroutine_handle<> coro_;
explicit __op_base(coro::coroutine_handle<> coro) noexcept
: coro_(coro) {}
__op_base(__op_base&& other) noexcept
: coro_(exchange(other.coro_, {})) {}
~__op_base() {
if (coro_)
coro_.destroy();
}
friend void tag_invoke(start_t, __op_base& self) noexcept {
self.coro_.resume();
}
};
template<class R_>
class __op : public __op_base {
using R = __t<R_>;
public:
struct promise_type : __promise_base {
template <class A>
explicit promise_type(A&, R& r) noexcept
: r_(r)
{}
coro::coroutine_handle<> unhandled_done() noexcept {
set_done(std::move(r_));
// Returning noop_coroutine here causes the __connect_awaitable
// coroutine to never resume past the point where it co_await's
// the awaitable.
return coro::noop_coroutine();
}
__op get_return_object() noexcept {
return __op{
coro::coroutine_handle<promise_type>::from_promise(*this)};
}
// Pass through receiver queries
template<class... As, invocable<R&, As...> CPO>
requires (!__one_of<CPO, set_value_t, set_error_t, set_done_t>)
friend auto tag_invoke(CPO cpo, const promise_type& self, As&&... as)
noexcept(is_nothrow_invocable_v<CPO, R&, As...>)
-> invoke_result_t<CPO, R&, As...> {
return ((CPO&&) cpo)(self.r_, (As&&) as...);
}
R& r_;
};
using __op_base::__op_base;
};
}
inline constexpr struct __fn {
private:
template <class R, class... Args>
using __nothrow_ = bool_constant<nothrow_receiver_of<R, Args...>>;
template <class A, class R>
static __impl::__op<__id_t<remove_cvref_t<R>>> __impl(A&& a, R&& r) {
exception_ptr ex;
try {
// This is a bit mind bending control-flow wise.
// We are first evaluating the co_await expression.
// Then the result of that is passed into a lambda
// that curries a reference to the result into another
// lambda which is then returned to 'co_yield'.
// The 'co_yield' expression then invokes this lambda
// after the coroutine is suspended so that it is safe
// for the receiver to destroy the coroutine.
auto fn = [&]<bool Nothrow>(bool_constant<Nothrow>, auto&&... as) noexcept {
return [&]() noexcept(Nothrow) -> void {
set_value((R&&) r, (add_rvalue_reference_t<__await_result_t<A>>) as...);
};
};
if constexpr (is_void_v<__await_result_t<A>>)
co_yield (co_await (A &&) a, fn(__nothrow_<R>{}));
else
co_yield fn(__nothrow_<R, __await_result_t<A>>{}, co_await (A &&) a);
} catch (...) {
ex = current_exception();
}
co_yield [&]() noexcept -> void {
set_error((R&&) r, (exception_ptr&&) ex);
};
}
public:
template <__awaitable A, receiver R>
requires receiver_of<R, __await_result_t<A>>
__impl::__op<__id_t<remove_cvref_t<R>>> operator()(A&& a, R&& r) const {
return __impl((A&&) a, (R&&) r);
}
template <__awaitable A, receiver R>
requires same_as<void, __await_result_t<A>> && receiver_of<R>
__impl::__op<__id_t<remove_cvref_t<R>>> operator()(A&& a, R&& r) const {
return __impl((A&&) a, (R&&) r);
}
} __connect_awaitable{};
}
/////////////////////////////////////////////////////////////////////////////
// [execution.senders.connect]
inline namespace __connect {
inline constexpr struct connect_t {
template<sender S, receiver R>
requires tag_invocable<connect_t, S, R> &&
operation_state<tag_invoke_result_t<connect_t, S, R>>
auto operator()(S&& s, R&& r) const
noexcept(nothrow_tag_invocable<connect_t, S, R>) {
return tag_invoke(connect_t{}, (S&&) s, (R&&) r);
}
// NOT TO SPEC:
template<__awaitable A, receiver R>
requires (!tag_invocable<connect_t, A, R>)
auto operator()(A&& a, R&& r) const {
return __connect_awaitable((A&&) a, (R&&) r);
}
} connect {};
}
/////////////////////////////////////////////////////////////////////////////
// [execution.senders]
template<class S, class R>
concept sender_to =
sender<S> &&
receiver<R> &&
requires (S&& s, R&& r) {
connect((S&&) s, (R&&) r);
};
template<class S, class R>
using connect_result_t = tag_invoke_result_t<connect_t, S, R>;
/////////////////////////////////////////////////////////////////////////////
// [execution.senders.queries], sender queries
template<__one_of<set_value_t, set_error_t, set_done_t> CPO>
struct get_completion_scheduler_t {
template <sender S>
requires tag_invocable<get_completion_scheduler_t, const S&> &&
scheduler<tag_invoke_result_t<get_completion_scheduler_t, const S&>>
auto operator()(const S& s) const noexcept
-> tag_invoke_result_t<get_completion_scheduler_t, const S&> {
// NOT TO SPEC:
static_assert(
nothrow_tag_invocable<get_completion_scheduler_t, const S&>,
"get_completion_scheduler<CPO> should be noexcept");
return tag_invoke(*this, s);
}
};
template<__one_of<set_value_t, set_error_t, set_done_t> CPO>
inline constexpr get_completion_scheduler_t<CPO> get_completion_scheduler{};
template <class S, class CPO>
concept __has_completion_scheduler =
invocable<get_completion_scheduler_t<CPO>, S>;
template <class S, class CPO>
using __completion_scheduler_for =
invoke_result_t<get_completion_scheduler_t<CPO>, S>;
template <class Fn, class CPO, class S, class... As>
concept __tag_invocable_with_completion_scheduler =
__has_completion_scheduler<S, CPO> &&
tag_invocable<Fn, __completion_scheduler_for<S, CPO>, S, As...>;
/////////////////////////////////////////////////////////////////////////////
inline namespace __with_awaitable_senders {
namespace __impl {
struct __void {};
template <class Value>
using __value_or_void_t =
conditional_t<is_void_v<Value>, __void, Value>;
template <class Value>
using __expected_t =
variant<monostate, __value_or_void_t<Value>, std::exception_ptr>;
template <class Value>
struct __rec_base {
template <class... Us>
requires constructible_from<Value, Us...> ||
(is_void_v<Value> && sizeof...(Us) == 0)
friend void tag_invoke(set_value_t, __rec_base&& self, Us&&... us)
noexcept(is_nothrow_constructible_v<Value, Us...> ||
is_void_v<Value>) {
self.result_->template emplace<1>((Us&&) us...);
self.continuation_.resume();
}
friend void tag_invoke(set_error_t, __rec_base&& self, exception_ptr eptr) noexcept {
self.result_->template emplace<2>((exception_ptr&&) eptr);
self.continuation_.resume();
}
__expected_t<Value>* result_;
coro::coroutine_handle<> continuation_;
};
template <typename P_, typename Value>
struct __awaitable_base {
using Promise = __t<P_>;
struct __rec : __rec_base<Value> {
friend void tag_invoke(set_done_t, __rec&& self) noexcept {
auto continuation = coro::coroutine_handle<Promise>::from_address(
self.continuation_.address());
continuation.promise().unhandled_done().resume();
}
// Forward other tag_invoke overloads to the promise
template <class... As, invocable<Promise&, As...> CPO>
friend auto tag_invoke(CPO cpo, const __rec& self, As&&... as)
noexcept(is_nothrow_invocable_v<CPO, Promise&, As...>)
-> invoke_result_t<CPO, Promise&, As...> {
auto continuation = coro::coroutine_handle<Promise>::from_address(
self.continuation_.address());
return ((CPO&&) cpo)(continuation.promise(), (As&&) as...);
}
};
bool await_ready() const noexcept {
return false;
}
Value await_resume() {
switch (result_.index()) {
case 0: // receiver contract not satisfied
assert(!"Should never get here");
break;
case 1: // set_value
if constexpr (!is_void_v<Value>)
return (Value&&) std::get<1>(result_);
else
return;
case 2: // set_error
std::rethrow_exception(std::get<2>(result_));
}
terminate();
}
protected:
__expected_t<Value> result_;
};
template <typename P_, typename S_>
struct __awaitable
: __awaitable_base<P_, __single_sender_value_t<__t<S_>>> {
private:
using Promise = __t<P_>;
using Sender = __t<S_>;
using Base = __awaitable_base<P_, __single_sender_value_t<Sender>>;
using __rec = typename Base::__rec;
connect_result_t<Sender, __rec> op_;
public:
__awaitable(Sender&& sender, coro::coroutine_handle<Promise> h)
noexcept(/* TODO: is_nothrow_connectable_v<Sender, __rec>*/ false)
: op_(connect((Sender&&)sender, __rec{{&this->result_, h}}))
{}
void await_suspend(coro::coroutine_handle<Promise>) noexcept {
start(op_);
}
};
}
inline constexpr struct as_awaitable_t {
template <__single_typed_sender S, class Promise>
auto operator()(S&& s, Promise& promise) const
noexcept(/*TODO*/ false)
-> __impl::__awaitable<__id_t<Promise>, __id_t<remove_cvref_t<S>>> {
auto h = coro::coroutine_handle<Promise>::from_promise(promise);
return {(S&&) s, h};
}
} as_awaitable{};
template <class Promise>
struct with_awaitable_senders;
struct with_awaitable_senders_base {
template <class OtherPromise>
void set_continuation(coro::coroutine_handle<OtherPromise> h) noexcept {
static_assert(!is_void_v<OtherPromise>);
continuation_ = h;
if constexpr (requires(OtherPromise& other) { other.unhandled_done(); }) {
done_callback_ = [](void* address) noexcept -> coro::coroutine_handle<> {
// This causes the rest of the coroutine (the part after the co_await
// of the sender) to be skipped and invokes the calling coroutine's
// done handler.
return coro::coroutine_handle<OtherPromise>::from_address(address)
.promise().unhandled_done();
};
}
// If OtherPromise doesn't implement unhandled_done(), then if a "done" unwind
// reaches this point, it's considered an unhandled exception and terminate()
// is called.
}
coro::coroutine_handle<> continuation() const noexcept {
return continuation_;
}
coro::coroutine_handle<> unhandled_done() noexcept {
return (*done_callback_)(continuation_.address());
}
private:
coro::coroutine_handle<> continuation_{};
coro::coroutine_handle<> (*done_callback_)(void*) noexcept =
[](void*) noexcept -> coro::coroutine_handle<> {
std::terminate();
};
};
template <class Promise>
struct with_awaitable_senders : with_awaitable_senders_base {
template <class Value>
decltype(auto) await_transform(Value&& value) {
static_assert(derived_from<Promise, with_awaitable_senders>);
if constexpr (__awaitable<Value>)
return (Value&&) value;
else if constexpr (sender<Value>)
return as_awaitable((Value&&) value, static_cast<Promise&>(*this));
else
return (Value&&) value;
}
};
}
/////////////////////////////////////////////////////////////////////////////
// [execution.senders.consumer.start_detached]
// TODO: turn this into start_detached
inline namespace __submit {
namespace __impl {
template<class S, class R_>
struct __rec {
using R = remove_cvref_t<R_>;
struct __wrap {
__rec* __this;
// Forward all tag_invoke calls, including the receiver ops.
template<__decays_to<__wrap> Self, class... As, invocable<__member_t<Self, R>, As...> Tag>
friend auto tag_invoke(Tag tag, Self&& self, As&&... as)
noexcept(is_nothrow_invocable_v<Tag, __member_t<Self, R>, As...>)
-> invoke_result_t<Tag, __member_t<Self, R>, As...> {
// If we are about to complete the receiver contract, delete the state as cleanup:
struct _g_t {
__rec* r_;
~_g_t() { delete r_; }
} _g{__one_of<Tag, set_value_t, set_error_t, set_done_t> ? self.__this : nullptr};
return ((Tag&&) tag)((__member_t<Self, R>&&) self.__this->__r, (As&&) as...);
}
};
R __r;
connect_result_t<S, __wrap> __state;
__rec(S&& s, R_&& r)
: __r((R_&&) r)
, __state(connect((S&&) s, __wrap{this}))
{}
};
}
inline constexpr struct submit_t {
template<receiver R, sender_to<R> S>
void operator()(S&& s, R&& r) const noexcept(false) {
start((new __impl::__rec<S, R>{(S&&) s, (R&&) r})->__state);
}
template<receiver R, sender_to<R> S>
requires tag_invocable<submit_t, S, R>
void operator()(S&& s, R&& r) const
noexcept(nothrow_tag_invocable<submit_t, S, R>) {
(void) tag_invoke(submit_t{}, (S&&) s, (R&&) r);
}
} submit {};
}
/////////////////////////////////////////////////////////////////////////////
// [execution.senders.factories]
inline namespace __just {
namespace __impl {
template <class CPO, class... Ts>
struct __traits {
template<template<class...> class Tuple,
template<class...> class Variant>
using value_types = Variant<Tuple<Ts...>>;
template<template<class...> class Variant>
using error_types = Variant<exception_ptr>;
static const constexpr auto sends_done = false;
};
template<class Error>
struct __traits<set_error_t, Error> {
template<template<class...> class,
template<class...> class Variant>
using value_types = Variant<>;
template<template<class...> class Variant>
using error_types = Variant<Error>;
static const constexpr auto sends_done = false;
};
template<>
struct __traits<set_done_t> {
template<template<class...> class,
template<class...> class Variant>
using value_types = Variant<>;
template<template<class...> class Variant>
using error_types = Variant<>;
static const constexpr auto sends_done = true;
};
template <class CPO, class... Ts>
struct __sender : __traits<CPO, Ts...> {
tuple<Ts...> vs_;
template<class R_>
struct __op {
using R = __t<R_>;
std::tuple<Ts...> vs_;
R r_;
friend void tag_invoke(start_t, __op& op) noexcept try {
std::apply([&op](Ts&... ts) {
CPO{}((R&&) op.r_, (Ts&&) ts...);
}, op.vs_);
} catch(...) {
set_error((R&&) op.r_, current_exception());
}
};
template<receiver_of<Ts...> R>
requires (copy_constructible<Ts> &&...)
friend auto tag_invoke(connect_t, const __sender& s, R&& r)
noexcept((is_nothrow_copy_constructible_v<Ts> &&...))
-> __op<__id_t<remove_cvref_t<R>>> {
return {s.vs_, (R&&) r};
}
template<receiver_of<Ts...> R>
friend auto tag_invoke(connect_t, __sender&& s, R&& r)
noexcept((is_nothrow_move_constructible_v<Ts> &&...))
-> __op<__id_t<remove_cvref_t<R>>> {
return {((__sender&&) s).vs_, (R&&) r};
}
};
}
inline constexpr struct __just_t {
template <class... Ts>
requires (constructible_from<decay_t<Ts>, Ts> &&...)
__impl::__sender<set_value_t, decay_t<Ts>...> operator()(Ts&&... ts) const
noexcept((is_nothrow_constructible_v<decay_t<Ts>, Ts> &&...)) {
return {{}, {(Ts&&) ts...}};
}
} just {};
inline constexpr struct __just_error_t {
template <class Error>
requires constructible_from<decay_t<Error>, Error>
__impl::__sender<set_error_t, Error> operator()(Error&& err) const
noexcept(is_nothrow_constructible_v<decay_t<Error>, Error>) {
return {{}, {(Error&&) err}};
}
} just_error {};
inline constexpr struct __just_done_t {
__impl::__sender<set_done_t> operator()() const noexcept {
return {{}, {}};
}
} just_done {};
}
/////////////////////////////////////////////////////////////////////////////
// [execution.execute]
inline namespace __execute {
namespace __impl {
template<class F>
struct __as_receiver {
F f_;
friend void tag_invoke(set_value_t, __as_receiver&& r) noexcept(is_nothrow_invocable_v<F&>) {
r.f_();
}
[[noreturn]]
friend void tag_invoke(set_error_t, __as_receiver&&, std::exception_ptr) noexcept {
terminate();
}
friend void tag_invoke(set_done_t, __as_receiver&&) noexcept {}
};
}
inline constexpr struct execute_t {
template<scheduler Sch, class F>
requires invocable<F&> && move_constructible<F>
void operator()(Sch&& sch, F f) const
noexcept(noexcept(
submit(schedule((Sch&&) sch), __impl::__as_receiver<F>{(F&&) f}))) {
(void) submit(schedule((Sch&&) sch), __impl::__as_receiver<F>{(F&&) f});
}
template<scheduler Sch, class F>
requires invocable<F&> &&
move_constructible<F> &&
tag_invocable<execute_t, Sch, F>
void operator()(Sch&& sch, F f) const
noexcept(nothrow_tag_invocable<execute_t, Sch, F>) {
(void) tag_invoke(execute_t{}, (Sch&&) sch, (F&&) f);
}
} execute {};
}
// NOT TO SPEC:
namespace __closure {
template <__class D>
struct sender_adaptor_closure;
}
using __closure::sender_adaptor_closure;
template <class T>
concept __sender_adaptor_closure =
derived_from<remove_cvref_t<T>, sender_adaptor_closure<remove_cvref_t<T>>> &&
move_constructible<remove_cvref_t<T>> &&
constructible_from<remove_cvref_t<T>, T>;
namespace __closure {
template <class A, class B>
struct __compose : sender_adaptor_closure<__compose<A, B>> {
[[no_unique_address]] A a_;
[[no_unique_address]] B b_;
template <sender S>
requires invocable<A, S> && invocable<B, invoke_result_t<A, S>>
invoke_result_t<B, invoke_result_t<A, S>> operator()(S&& s) && {
return ((B&&) b_)(((A&&) a_)((S&&) s));
}
template <sender S>
requires invocable<const A&, S> && invocable<const B&, invoke_result_t<const A&, S>>
invoke_result_t<B, invoke_result_t<A, S>> operator()(S&& s) const & {
return b_(a_((S&&) s));
}
};
template <__class D>
struct sender_adaptor_closure
{};
template <__sender_adaptor_closure A, __sender_adaptor_closure B>
__compose<remove_cvref_t<A>, remove_cvref_t<B>> operator|(A&& a, B&& b) {
return {(A&&) a, (B&&) b};
}
template <sender S, __sender_adaptor_closure C>
requires invocable<C, S>
invoke_result_t<C, S> operator|(S&& s, C&& c) {
return ((C&&) c)((S&&) s);
}
template <class Fn, class... As>
struct __binder_back : sender_adaptor_closure<__binder_back<Fn, As...>> {
[[no_unique_address]] Fn fn;
tuple<As...> as;
template <sender S>
requires invocable<Fn, S, As...>
invoke_result_t<Fn, S, As...> operator()(S&& s) &&
noexcept(is_nothrow_invocable_v<Fn, S, As...>) {
return std::apply([&s, this](As&... as) {
return ((Fn&&) fn)((S&&) s, (As&&) as...);
}, as);
}
template <sender S>
requires invocable<Fn, S, As...>
invoke_result_t<const Fn&, S, const As&...> operator()(S&& s) const &
noexcept(is_nothrow_invocable_v<const Fn&, S, const As&...>) {
return std::apply([&s, this](const As&... as) {
return fn((S&&) s, as...);
}, as);
}
};
inline constexpr struct __bind_back_fn {
template <class Fn, class... As>
__binder_back<Fn, decay_t<As>...> operator()(Fn fn, As&&... as) const
noexcept(noexcept(__binder_back<Fn, decay_t<As>...>{
{}, (Fn&&) fn, tuple<decay_t<As>...>{(As&&) as...}})) {
return {{}, (Fn&&) fn, tuple<decay_t<As>...>{(As&&) as...}};
}
} __bind_back {};
} // __closure
using __closure::__binder_back;
using __closure::__bind_back;
// NOT TO SPEC
namespace __tag_invoke_adaptors {
// A derived-to-base cast that works even when the base is not
// accessible from derived.
template <class T, class U>
__member_t<U, T> __c_cast(U&& u) noexcept requires __decays_to<T, T> {
static_assert(is_reference_v<__member_t<U, T>>);
static_assert(is_base_of_v<T, remove_reference_t<U>>);
return (__member_t<U, T>) (U&&) u;
}
namespace __no {
struct __nope {};
struct __receiver : __nope {};
void tag_invoke(set_error_t, __receiver, std::exception_ptr) noexcept;
void tag_invoke(set_done_t, __receiver) noexcept;
}
using __not_a_receiver = __no::__receiver;
template <class Base>
struct __adaptor {
struct __t {
template <class B>
requires constructible_from<Base, B>
explicit __t(B&& base) : base_((B&&) base) {}
private:
[[no_unique_address]] Base base_;
protected:
Base& base() & noexcept { return base_; }
const Base& base() const & noexcept { return base_; }
Base&& base() && noexcept { return (Base&&) base_; }
};
};
template <derived_from<__no::__nope> Base>
struct __adaptor<Base> {
struct __t : __no::__nope { };
};
template <class Base>
using __adaptor_base = typename __adaptor<Base>::__t;
template <class Sender, class Receiver>
concept __has_connect =
requires(Sender&& s, Receiver&& r) {
((Sender&&) s).connect((Receiver&&) r);
};
template <__class Derived, sender Base>
struct __sender_adaptor {
class __t : __adaptor_base<Base> {
template <__decays_to<Derived> Self, receiver R>
requires __has_connect<Self, R>
friend auto tag_invoke(connect_t, Self&& self, R&& r)
noexcept(noexcept(((Self&&) self).connect((R&&) r)))
-> decltype(((Self&&) self).connect((R&&) r)) {
return ((Self&&) self).connect((R&&) r);
}
protected:
using __adaptor_base<Base>::base;
public: