-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathfuture
1565 lines (1230 loc) · 51.5 KB
/
future
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
// future standard header
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifndef _FUTURE_
#define _FUTURE_
#include <yvals_core.h>
#if _STL_COMPILER_PREPROCESSOR
#ifdef _M_CEE_PURE
#error <future> is not supported when compiling with /clr:pure.
#endif // _M_CEE_PURE
#ifdef _RESUMABLE_FUNCTIONS_SUPPORTED
#include <experimental/resumable>
#endif // _RESUMABLE_FUNCTIONS_SUPPORTED
#include <__msvc_chrono.hpp>
#include <condition_variable>
#include <functional>
#include <memory>
#include <mutex>
#include <ppltasks.h>
#include <system_error>
#include <thread>
#include <utility>
#pragma pack(push, _CRT_PACKING)
#pragma warning(push, _STL_WARNING_LEVEL)
#pragma warning(disable : _STL_DISABLED_WARNINGS)
_STL_DISABLE_CLANG_WARNINGS
#pragma push_macro("new")
#undef new
_STD_BEGIN
template <class _Alloc>
struct _Allocator_deleter {
_Alloc _Al;
using pointer = typename allocator_traits<_Alloc>::pointer;
void operator()(pointer _Ptr) noexcept { // delete the pointer
_Destroy_in_place(*_Ptr);
_Al.deallocate(_Ptr, 1);
}
};
template <class _Alloc>
using _Unique_ptr_alloc = unique_ptr<typename _Alloc::value_type, _Allocator_deleter<_Alloc>>;
template <class _Alloc, class... _Args>
_NODISCARD _Unique_ptr_alloc<_Alloc> _Make_unique_alloc(_Alloc& _Al, _Args&&... _Vals) {
// construct an object with an allocator and return it owned by a unique_ptr
_Alloc_construct_ptr<_Alloc> _Constructor{_Al};
_Constructor._Allocate();
_Construct_in_place(*_Constructor._Ptr, _STD forward<_Args>(_Vals)...);
return _Unique_ptr_alloc<_Alloc>(_Constructor._Release(), _Allocator_deleter<_Alloc>{_Al});
}
struct _From_raw_state_tag { // internal tag type for constructing a future from a raw state
explicit _From_raw_state_tag() = default;
};
_EXPORT_STD enum class future_errc { // names for futures errors
broken_promise = 1,
future_already_retrieved,
promise_already_satisfied,
no_state
};
_EXPORT_STD enum class launch { // names for launch options passed to async
async = 0x1,
deferred = 0x2
};
_BITMASK_OPS(_EXPORT_STD, launch)
_EXPORT_STD enum class future_status { // names for timed wait function returns
ready,
timeout,
deferred
};
template <>
struct is_error_code_enum<future_errc> : true_type {};
_EXPORT_STD _NODISCARD const error_category& future_category() noexcept;
_EXPORT_STD _NODISCARD inline error_code make_error_code(future_errc _Ec) noexcept {
return error_code(static_cast<int>(_Ec), _STD future_category());
}
_EXPORT_STD _NODISCARD inline error_condition make_error_condition(future_errc _Ec) noexcept {
return error_condition(static_cast<int>(_Ec), _STD future_category());
}
_NODISCARD inline const char* _Future_error_map(int _Errcode) noexcept { // convert to name of future error
switch (static_cast<future_errc>(_Errcode)) { // switch on error code value
case future_errc::broken_promise:
return "broken promise";
case future_errc::future_already_retrieved:
return "future already retrieved";
case future_errc::promise_already_satisfied:
return "promise already satisfied";
case future_errc::no_state:
return "no state";
default:
return nullptr;
}
}
_EXPORT_STD class future_error : public logic_error { // future exception
public:
explicit future_error(error_code _Errcode) // internal, TRANSITION, will be removed
: logic_error(""), _Mycode(_Errcode) {}
explicit future_error(future_errc _Ec) : logic_error(""), _Mycode(_STD make_error_code(_Ec)) {}
_NODISCARD const error_code& code() const noexcept {
return _Mycode;
}
_NODISCARD const char* __CLR_OR_THIS_CALL what() const noexcept override { // get message string
return _Future_error_map(_Mycode.value());
}
#if !_HAS_EXCEPTIONS
protected:
void _Doraise() const override { // perform class-specific exception handling
_RAISE(*this);
}
#endif // !_HAS_EXCEPTIONS
private:
error_code _Mycode; // the stored error code
};
// TRANSITION, ABI: remove `2` suffix
[[noreturn]] inline void _Throw_future_error2(const future_errc _Ec) {
_THROW(future_error{_Ec});
}
class _Future_error_category2 : public error_category { // categorize a future error
public:
constexpr _Future_error_category2() noexcept : error_category(_Future_addr) {}
const char* name() const noexcept override {
return "future";
}
string message(int _Errcode) const override {
const char* _Name = _Future_error_map(_Errcode);
if (_Name) {
return _Name;
}
return _Syserror_map(_Errcode);
}
};
_EXPORT_STD _NODISCARD inline const error_category& future_category() noexcept {
return _Immortalize_memcpy_image<_Future_error_category2>();
}
template <class _Ty>
class _Associated_state;
template <class _Ty>
struct __declspec(novtable) _Deleter_base { // abstract base class for managing deletion of state objects
virtual void _Delete(_Associated_state<_Ty>*) noexcept = 0;
virtual ~_Deleter_base() = default; // TRANSITION, vNext (make non-virtual, never called polymorphically)
};
template <class _Ty, class _Derived, class _Alloc>
struct _State_deleter : _Deleter_base<_Ty> { // manage allocator and deletion state objects
_State_deleter(const _Alloc& _Al) : _My_alloc(_Al) {}
_State_deleter(const _State_deleter&) = delete;
_State_deleter& operator=(const _State_deleter&) = delete;
void _Delete(_Associated_state<_Ty>* _State) noexcept override;
_Alloc _My_alloc;
};
template <class _Ty>
union _Result_holder {
_Result_holder() noexcept {}
~_Result_holder() noexcept {}
_Ty _Held_value;
};
template <class _Ty>
class _Associated_state { // class for managing associated synchronous state
public:
using _State_type = _Ty;
using _Mydel = _Deleter_base<_Ty>;
// TRANSITION, incorrectly default constructs _Result when _Ty is default constructible
_Associated_state(_Mydel* _Dp = nullptr)
: _Refs(1), // non-atomic initialization
_Result(), _Exception(), _Retrieved(false), _Ready(false), _Ready_at_thread_exit(false),
_Has_stored_result(false), _Running(false), _Deleter(_Dp) {}
virtual ~_Associated_state() noexcept {
if (_Already_has_stored_result() && !_Ready) { // registered for release at thread exit
_Cond._Unregister(_Mtx);
}
if constexpr (!is_default_constructible_v<_Ty>) {
if (_Has_stored_result) {
_Result._Held_value.~_Ty();
}
}
}
void _Retain() noexcept { // increment reference count
_MT_INCR(_Refs);
}
void _Release() noexcept { // decrement reference count and destroy when zero
if (_MT_DECR(_Refs) == 0) {
_Delete_this();
}
}
private:
_Atomic_counter_t _Refs;
public:
virtual void _Wait() { // wait for signal
unique_lock<mutex> _Lock(_Mtx);
_Maybe_run_deferred_function(_Lock);
while (!_Ready) {
_Cond.wait(_Lock);
}
}
struct _Test_ready { // wraps _Associated_state
_Test_ready(const _Associated_state* _St) : _State(_St) {}
bool operator()() const { // test state
return _State->_Ready != 0;
}
const _Associated_state* _State;
};
template <class _Rep, class _Per>
future_status _Wait_for(const chrono::duration<_Rep, _Per>& _Rel_time) { // wait for duration
unique_lock<mutex> _Lock(_Mtx);
if (_Has_deferred_function()) {
return future_status::deferred;
}
if (_Cond.wait_for(_Lock, _Rel_time, _Test_ready(this))) {
return future_status::ready;
}
return future_status::timeout;
}
template <class _Clock, class _Dur>
future_status _Wait_until(const chrono::time_point<_Clock, _Dur>& _Abs_time) { // wait until time point
unique_lock<mutex> _Lock(_Mtx);
if (_Has_deferred_function()) {
return future_status::deferred;
}
if (_Cond.wait_until(_Lock, _Abs_time, _Test_ready(this))) {
return future_status::ready;
}
return future_status::timeout;
}
virtual _Ty& _Get_value(bool _Get_only_once) {
unique_lock<mutex> _Lock(_Mtx);
if (_Get_only_once && _Retrieved) {
_Throw_future_error2(future_errc::future_already_retrieved);
}
if (_Exception) {
_STD rethrow_exception(_Exception);
}
// TRANSITION: `_Retrieved` should be assigned before `_Exception` is thrown so that a `future::get`
// that throws a stored exception invalidates the future (N4950 [futures.unique.future]/17)
_Retrieved = true;
_Maybe_run_deferred_function(_Lock);
while (!_Ready) {
_Cond.wait(_Lock);
}
if (_Exception) {
_STD rethrow_exception(_Exception);
}
if constexpr (is_default_constructible_v<_Ty>) {
return _Result;
} else {
return _Result._Held_value;
}
}
template <class _Ty2>
void _Emplace_result(_Ty2&& _Val) {
// TRANSITION, incorrectly assigns _Result when _Ty is default constructible
if constexpr (is_default_constructible_v<_Ty>) {
_Result = _STD forward<_Ty2>(_Val);
} else {
::new (static_cast<void*>(_STD addressof(_Result._Held_value))) _Ty(_STD forward<_Ty2>(_Val));
_Has_stored_result = true;
}
}
void _Set_value(const _Ty& _Val, bool _At_thread_exit) { // store a result
unique_lock<mutex> _Lock(_Mtx);
_Set_value_raw(_Val, &_Lock, _At_thread_exit);
}
void _Set_value_raw(const _Ty& _Val, unique_lock<mutex>* _Lock, bool _At_thread_exit) {
// store a result while inside a locked block
if (_Already_has_stored_result()) {
_Throw_future_error2(future_errc::promise_already_satisfied);
}
_Emplace_result(_Val);
_Do_notify(_Lock, _At_thread_exit);
}
void _Set_value(_Ty&& _Val, bool _At_thread_exit) { // store a result
unique_lock<mutex> _Lock(_Mtx);
_Set_value_raw(_STD forward<_Ty>(_Val), &_Lock, _At_thread_exit);
}
void _Set_value_raw(_Ty&& _Val, unique_lock<mutex>* _Lock, bool _At_thread_exit) {
// store a result while inside a locked block
if (_Already_has_stored_result()) {
_Throw_future_error2(future_errc::promise_already_satisfied);
}
_Emplace_result(_STD forward<_Ty>(_Val));
_Do_notify(_Lock, _At_thread_exit);
}
void _Set_exception(exception_ptr _Exc, bool _At_thread_exit) { // store a result
unique_lock<mutex> _Lock(_Mtx);
_Set_exception_raw(_Exc, &_Lock, _At_thread_exit);
}
void _Set_exception_raw(exception_ptr _Exc, unique_lock<mutex>* _Lock, bool _At_thread_exit) {
// store a result while inside a locked block
if (_Already_has_stored_result()) {
_Throw_future_error2(future_errc::promise_already_satisfied);
}
_STL_ASSERT(_Exc != nullptr, "promise<T>::set_exception/set_exception_at_thread_exit called with a null "
"std::exception_ptr, which is invalid per N4950 [futures.promise]/20");
_Exception = _Exc;
_Do_notify(_Lock, _At_thread_exit);
}
bool _Is_ready() const noexcept {
return _Ready != 0;
}
bool _Already_has_stored_result() const noexcept { // Has a result or an exception
if constexpr (is_default_constructible_v<_Ty>) {
return _Has_stored_result;
} else {
return _Has_stored_result || _Exception;
}
}
bool _Already_retrieved() const noexcept {
return _Retrieved;
}
void _Abandon() { // abandon shared state
unique_lock<mutex> _Lock(_Mtx);
if (!_Already_has_stored_result()) { // queue exception
future_error _Fut(_STD make_error_code(future_errc::broken_promise));
_Set_exception_raw(_STD make_exception_ptr(_Fut), &_Lock, false);
}
}
protected:
void _Maybe_run_deferred_function(unique_lock<mutex>& _Lock) { // run a deferred function if not already done
if (!_Running) { // run the function
_Running = true;
_Run_deferred_function(_Lock);
}
}
public:
conditional_t<is_default_constructible_v<_Ty>, _Ty, _Result_holder<_Ty>> _Result;
exception_ptr _Exception;
mutex _Mtx;
condition_variable _Cond;
bool _Retrieved;
int _Ready;
bool _Ready_at_thread_exit; // TRANSITION, ABI
bool _Has_stored_result;
bool _Running;
private:
virtual bool _Has_deferred_function() const noexcept { // overridden by _Deferred_async_state
return false;
}
virtual void _Run_deferred_function(unique_lock<mutex>&) {} // do nothing
virtual void _Do_notify(unique_lock<mutex>* _Lock, bool _At_thread_exit) { // notify waiting threads
// TRANSITION, ABI: This is virtual, but never overridden.
if constexpr (is_default_constructible_v<_Ty>) {
_Has_stored_result = true;
}
if (_At_thread_exit) { // notify at thread exit
_Cond._Register(*_Lock, &_Ready);
} else { // notify immediately
_Ready = true;
_Cond.notify_all();
}
}
void _Delete_this() noexcept { // delete this object
if (_Deleter) {
_Deleter->_Delete(this);
} else {
delete this;
}
}
_Mydel* _Deleter;
public:
_Associated_state(const _Associated_state&) = delete;
_Associated_state& operator=(const _Associated_state&) = delete;
};
template <class _Ty, class _Derived, class _Alloc>
void _State_deleter<_Ty, _Derived, _Alloc>::_Delete(_Associated_state<_Ty>* _State) noexcept {
// delete _State and this using stored allocator
using _State_allocator = _Rebind_alloc_t<_Alloc, _Derived>;
_State_allocator _St_alloc(_My_alloc);
using _Deleter_allocator = _Rebind_alloc_t<_Alloc, _State_deleter>;
_Deleter_allocator _Del_alloc(_My_alloc);
_Derived* _Ptr = static_cast<_Derived*>(_State);
_Delete_plain_internal(_St_alloc, _Ptr);
_Delete_plain_internal(_Del_alloc, this);
}
template <class>
class _Packaged_state;
template <class _Ret, class... _ArgTypes>
class _Packaged_state<_Ret(_ArgTypes...)>
: public _Associated_state<_Ret> { // class for managing associated asynchronous state for packaged_task
public:
using _Mybase = _Associated_state<_Ret>;
using _Mydel = typename _Mybase::_Mydel;
template <class _Fty2>
_Packaged_state(const _Fty2& _Fnarg) : _Fn(_Fnarg) {}
#if _HAS_FUNCTION_ALLOCATOR_SUPPORT
template <class _Fty2, class _Alloc>
_Packaged_state(const _Fty2& _Fnarg, const _Alloc& _Al, _Mydel* _Dp)
: _Mybase(_Dp), _Fn(allocator_arg, _Al, _Fnarg) {}
#endif // _HAS_FUNCTION_ALLOCATOR_SUPPORT
template <class _Fty2>
_Packaged_state(_Fty2&& _Fnarg) : _Fn(_STD forward<_Fty2>(_Fnarg)) {}
#if _HAS_FUNCTION_ALLOCATOR_SUPPORT
template <class _Fty2, class _Alloc>
_Packaged_state(_Fty2&& _Fnarg, const _Alloc& _Al, _Mydel* _Dp)
: _Mybase(_Dp), _Fn(allocator_arg, _Al, _STD forward<_Fty2>(_Fnarg)) {}
#endif // _HAS_FUNCTION_ALLOCATOR_SUPPORT
void _Call_deferred(_ArgTypes... _Args) { // set deferred call
_TRY_BEGIN
// call function object and catch exceptions
this->_Set_value(_Fn(_STD forward<_ArgTypes>(_Args)...), true);
_CATCH_ALL
// function object threw exception; record result
this->_Set_exception(_STD current_exception(), true);
_CATCH_END
}
void _Call_immediate(_ArgTypes... _Args) { // call function object
_TRY_BEGIN
// call function object and catch exceptions
this->_Set_value(_Fn(_STD forward<_ArgTypes>(_Args)...), false);
_CATCH_ALL
// function object threw exception; record result
this->_Set_exception(_STD current_exception(), false);
_CATCH_END
}
const function<_Ret(_ArgTypes...)>& _Get_fn() {
return _Fn;
}
private:
function<_Ret(_ArgTypes...)> _Fn;
};
template <class _Ret, class... _ArgTypes>
class _Packaged_state<_Ret&(_ArgTypes...)>
: public _Associated_state<_Ret*> { // class for managing associated asynchronous state for packaged_task
public:
using _Mybase = _Associated_state<_Ret*>;
using _Mydel = typename _Mybase::_Mydel;
template <class _Fty2>
_Packaged_state(const _Fty2& _Fnarg) : _Fn(_Fnarg) {}
#if _HAS_FUNCTION_ALLOCATOR_SUPPORT
template <class _Fty2, class _Alloc>
_Packaged_state(const _Fty2& _Fnarg, const _Alloc& _Al, _Mydel* _Dp)
: _Mybase(_Dp), _Fn(allocator_arg, _Al, _Fnarg) {}
#endif // _HAS_FUNCTION_ALLOCATOR_SUPPORT
template <class _Fty2>
_Packaged_state(_Fty2&& _Fnarg) : _Fn(_STD forward<_Fty2>(_Fnarg)) {}
#if _HAS_FUNCTION_ALLOCATOR_SUPPORT
template <class _Fty2, class _Alloc>
_Packaged_state(_Fty2&& _Fnarg, const _Alloc& _Al, _Mydel* _Dp)
: _Mybase(_Dp), _Fn(allocator_arg, _Al, _STD forward<_Fty2>(_Fnarg)) {}
#endif // _HAS_FUNCTION_ALLOCATOR_SUPPORT
void _Call_deferred(_ArgTypes... _Args) { // set deferred call
_TRY_BEGIN
// call function object and catch exceptions
this->_Set_value(_STD addressof(_Fn(_STD forward<_ArgTypes>(_Args)...)), true);
_CATCH_ALL
// function object threw exception; record result
this->_Set_exception(_STD current_exception(), true);
_CATCH_END
}
void _Call_immediate(_ArgTypes... _Args) { // call function object
_TRY_BEGIN
// call function object and catch exceptions
this->_Set_value(_STD addressof(_Fn(_STD forward<_ArgTypes>(_Args)...)), false);
_CATCH_ALL
// function object threw exception; record result
this->_Set_exception(_STD current_exception(), false);
_CATCH_END
}
const function<_Ret&(_ArgTypes...)>& _Get_fn() {
return _Fn;
}
private:
function<_Ret&(_ArgTypes...)> _Fn;
};
template <class... _ArgTypes>
class _Packaged_state<void(_ArgTypes...)>
: public _Associated_state<int> { // class for managing associated asynchronous state for packaged_task
public:
using _Mybase = _Associated_state<int>;
using _Mydel = typename _Mybase::_Mydel;
template <class _Fty2>
_Packaged_state(const _Fty2& _Fnarg) : _Fn(_Fnarg) {}
#if _HAS_FUNCTION_ALLOCATOR_SUPPORT
template <class _Fty2, class _Alloc>
_Packaged_state(const _Fty2& _Fnarg, const _Alloc& _Al, _Mydel* _Dp)
: _Mybase(_Dp), _Fn(allocator_arg, _Al, _Fnarg) {}
#endif // _HAS_FUNCTION_ALLOCATOR_SUPPORT
template <class _Fty2>
_Packaged_state(_Fty2&& _Fnarg) : _Fn(_STD forward<_Fty2>(_Fnarg)) {}
#if _HAS_FUNCTION_ALLOCATOR_SUPPORT
template <class _Fty2, class _Alloc>
_Packaged_state(_Fty2&& _Fnarg, const _Alloc& _Al, _Mydel* _Dp)
: _Mybase(_Dp), _Fn(allocator_arg, _Al, _STD forward<_Fty2>(_Fnarg)) {}
#endif // _HAS_FUNCTION_ALLOCATOR_SUPPORT
void _Call_deferred(_ArgTypes... _Args) { // set deferred call
_TRY_BEGIN
// call function object and catch exceptions
_Fn(_STD forward<_ArgTypes>(_Args)...);
this->_Set_value(1, true);
_CATCH_ALL
// function object threw exception; record result
this->_Set_exception(_STD current_exception(), true);
_CATCH_END
}
void _Call_immediate(_ArgTypes... _Args) { // call function object
_TRY_BEGIN
// call function object and catch exceptions
_Fn(_STD forward<_ArgTypes>(_Args)...);
this->_Set_value(1, false);
_CATCH_ALL
// function object threw exception; record result
this->_Set_exception(_STD current_exception(), false);
_CATCH_END
}
const function<void(_ArgTypes...)>& _Get_fn() {
return _Fn;
}
private:
function<void(_ArgTypes...)> _Fn;
};
template <class _Ty, class _Alloc>
_Associated_state<_Ty>* _Make_associated_state(const _Alloc& _Al) {
// construct an _Associated_state object with an allocator
using _Delty = _State_deleter<_Ty, _Associated_state<_Ty>, _Alloc>;
using _Aldelty = _Rebind_alloc_t<_Alloc, _Delty>;
using _Alstate = _Rebind_alloc_t<_Alloc, _Associated_state<_Ty>>;
_Aldelty _Del_alloc(_Al);
_Alstate _State_alloc(_Al);
auto _Del = _Make_unique_alloc(_Del_alloc, _Al);
auto _Res = _Make_unique_alloc(_State_alloc, _Unfancy(_Del.get()));
(void) _Del.release(); // ownership of _Del.get() now transferred to _Res
return _Unfancy(_Res.release()); // ownership transferred to caller
}
#if _HAS_FUNCTION_ALLOCATOR_SUPPORT
template <class _Pack_state, class _Fty2, class _Alloc>
_Pack_state* _Make_packaged_state(_Fty2&& _Fnarg, const _Alloc& _Al) {
// construct a _Packaged_state object with an allocator from an rvalue function object
using _Delty = _State_deleter<typename _Pack_state::_Mybase::_State_type, _Pack_state, _Alloc>;
using _Aldelty = _Rebind_alloc_t<_Alloc, _Delty>;
using _Alstate = _Rebind_alloc_t<_Alloc, _Pack_state>;
_Aldelty _Del_alloc(_Al);
_Alstate _State_alloc(_Al);
auto _Del = _Make_unique_alloc(_Del_alloc, _Al);
auto _Res = _Make_unique_alloc(_State_alloc, _STD forward<_Fty2>(_Fnarg), _Al, _Unfancy(_Del.get()));
(void) _Del.release(); // ownership of _Del.get() now transferred to _Res
return _Unfancy(_Res.release()); // ownership transferred to caller
}
#endif // _HAS_FUNCTION_ALLOCATOR_SUPPORT
template <class _Rx>
class _Deferred_async_state : public _Packaged_state<_Rx()> {
// class for managing associated synchronous state for deferred execution from async
public:
template <class _Fty2>
_Deferred_async_state(const _Fty2& _Fnarg) : _Packaged_state<_Rx()>(_Fnarg) {}
template <class _Fty2>
_Deferred_async_state(_Fty2&& _Fnarg) : _Packaged_state<_Rx()>(_STD forward<_Fty2>(_Fnarg)) {}
private:
bool _Has_deferred_function() const noexcept override {
// this function is considered to be deferred until it's invoked
return !this->_Running;
}
void _Run_deferred_function(unique_lock<mutex>& _Lock) override { // run the deferred function
_Lock.unlock();
_Packaged_state<_Rx()>::_Call_immediate();
_Lock.lock();
}
};
template <class _Rx>
class _Task_async_state : public _Packaged_state<_Rx()> {
// class for managing associated synchronous state for asynchronous execution from async
public:
using _Mybase = _Packaged_state<_Rx()>;
using _State_type = typename _Mybase::_State_type;
template <class _Fty2>
_Task_async_state(_Fty2&& _Fnarg) : _Mybase(_STD forward<_Fty2>(_Fnarg)) {
_Task = ::Concurrency::create_task([this]() { // do it now
this->_Call_immediate();
});
this->_Running = true;
}
~_Task_async_state() noexcept override {
_Wait();
}
void _Wait() override { // wait for completion
_Task.wait();
}
_State_type& _Get_value(bool _Get_only_once) override {
// return the stored result or throw stored exception
_Task.wait();
return _Mybase::_Get_value(_Get_only_once);
}
private:
::Concurrency::task<void> _Task;
};
template <class _Ty>
class _State_manager {
// class for managing possibly non-existent associated asynchronous state object
public:
_State_manager() = default; // construct with no associated asynchronous state object
_State_manager(_Associated_state<_Ty>* _New_state, bool _Get_once) noexcept
: _Assoc_state(_New_state), _Get_only_once(_Get_once) {} // construct with _New_state
_State_manager(const _State_manager& _Other, bool _Get_once = false) noexcept
: _Assoc_state(_Other._Assoc_state), _Get_only_once(_Get_once) {
if (_Assoc_state) { // do the copy
_Assoc_state->_Retain();
}
}
_State_manager(_State_manager&& _Other, bool _Get_once = false) noexcept
: _Assoc_state(_STD exchange(_Other._Assoc_state, nullptr)), _Get_only_once(_Get_once) {}
~_State_manager() noexcept {
if (_Assoc_state) {
_Assoc_state->_Release();
}
}
_State_manager& operator=(const _State_manager& _Other) noexcept {
// copy stored associated asynchronous state object from _Other
if (_Other._Assoc_state) {
_Other._Assoc_state->_Retain();
}
if (_Assoc_state) {
_Assoc_state->_Release();
}
_Assoc_state = _Other._Assoc_state;
_Get_only_once = _Other._Get_only_once;
return *this;
}
_State_manager& operator=(_State_manager&& _Other) noexcept {
// move stored associated asynchronous state object from _Other
if (this != _STD addressof(_Other)) {
if (_Assoc_state) {
_Assoc_state->_Release();
}
_Assoc_state = _STD exchange(_Other._Assoc_state, nullptr);
_Get_only_once = _Other._Get_only_once;
}
return *this;
}
_NODISCARD bool valid() const noexcept {
return _Assoc_state && !(_Get_only_once && _Assoc_state->_Already_retrieved());
}
void wait() const { // wait for signal
if (!valid()) {
_Throw_future_error2(future_errc::no_state);
}
_Assoc_state->_Wait();
}
template <class _Rep, class _Per>
future_status wait_for(const chrono::duration<_Rep, _Per>& _Rel_time) const { // wait for duration
if (!valid()) {
_Throw_future_error2(future_errc::no_state);
}
return _Assoc_state->_Wait_for(_Rel_time);
}
template <class _Clock, class _Dur>
future_status wait_until(const chrono::time_point<_Clock, _Dur>& _Abs_time) const { // wait until time point
#if _HAS_CXX20
static_assert(chrono::is_clock_v<_Clock>, "Clock type required");
#endif // _HAS_CXX20
if (!valid()) {
_Throw_future_error2(future_errc::no_state);
}
return _Assoc_state->_Wait_until(_Abs_time);
}
_Ty& _Get_value() const {
if (!valid()) {
_Throw_future_error2(future_errc::no_state);
}
return _Assoc_state->_Get_value(_Get_only_once);
}
void _Set_value(const _Ty& _Val, bool _Defer) { // store a result
if (!valid()) {
_Throw_future_error2(future_errc::no_state);
}
_Assoc_state->_Set_value(_Val, _Defer);
}
void _Set_value(_Ty&& _Val, bool _Defer) { // store a result
if (!valid()) {
_Throw_future_error2(future_errc::no_state);
}
_Assoc_state->_Set_value(_STD forward<_Ty>(_Val), _Defer);
}
void _Abandon() { // abandon shared state
if (_Assoc_state) {
_Assoc_state->_Abandon();
}
}
void _Set_exception(exception_ptr _Exc, bool _Defer) { // store a result
if (!valid()) {
_Throw_future_error2(future_errc::no_state);
}
_Assoc_state->_Set_exception(_Exc, _Defer);
}
void _Swap(_State_manager& _Other) noexcept { // exchange with _Other
_STD swap(_Assoc_state, _Other._Assoc_state);
}
_Associated_state<_Ty>* _Ptr() const noexcept {
return _Assoc_state;
}
bool _Is_ready() const noexcept {
return _Assoc_state && _Assoc_state->_Is_ready();
}
private:
_Associated_state<_Ty>* _Assoc_state = nullptr;
bool _Get_only_once = false;
};
_EXPORT_STD template <class _Ty>
class shared_future;
_EXPORT_STD template <class _Ty>
class future : public _State_manager<_Ty> {
// class that defines a non-copyable asynchronous return object that holds a value
private:
using _Mybase = _State_manager<_Ty>;
public:
static_assert(!is_array_v<_Ty> && is_object_v<_Ty> && is_destructible_v<_Ty>,
"T in future<T> must meet the Cpp17Destructible requirements (N4950 [futures.unique.future]/4).");
future() = default;
future(future&& _Other) noexcept : _Mybase(_STD move(_Other), true) {}
future& operator=(future&&) = default;
future(_From_raw_state_tag, const _Mybase& _State) noexcept : _Mybase(_State, true) {}
_Ty get() {
// block until ready then return the stored result or throw the stored exception
future _Local{_STD move(*this)};
return _STD move(_Local._Get_value());
}
_NODISCARD shared_future<_Ty> share() noexcept {
return shared_future<_Ty>(_STD move(*this));
}
future(const future&) = delete;
future& operator=(const future&) = delete;
};
template <class _Ty>
class future<_Ty&> : public _State_manager<_Ty*> {
// class that defines a non-copyable asynchronous return object that holds a reference
private:
using _Mybase = _State_manager<_Ty*>;
public:
future() = default;
future(future&& _Other) noexcept : _Mybase(_STD move(_Other), true) {}
future& operator=(future&&) = default;
future(_From_raw_state_tag, const _Mybase& _State) noexcept : _Mybase(_State, true) {}
_Ty& get() {
// block until ready then return the stored result or throw the stored exception
future _Local{_STD move(*this)};
return *_Local._Get_value();
}
_NODISCARD shared_future<_Ty&> share() noexcept {
return shared_future<_Ty&>(_STD move(*this));
}
future(const future&) = delete;
future& operator=(const future&) = delete;
};
template <>
class future<void> : public _State_manager<int> {
// class that defines a non-copyable asynchronous return object that does not hold a value
private:
using _Mybase = _State_manager<int>;
public:
future() = default;
future(future&& _Other) noexcept : _Mybase(_STD move(_Other), true) {}
future& operator=(future&&) = default;
future(_From_raw_state_tag, const _Mybase& _State) noexcept : _Mybase(_State, true) {}
void get() {
// block until ready then return or throw the stored exception
future _Local{_STD move(*this)};
_Local._Get_value();
}
_NODISCARD shared_future<void> share() noexcept;
future(const future&) = delete;
future& operator=(const future&) = delete;
};
_EXPORT_STD template <class _Ty>
class shared_future : public _State_manager<_Ty> {
// class that defines a copyable asynchronous return object that holds a value
private:
using _Mybase = _State_manager<_Ty>;
public:
static_assert(!is_array_v<_Ty> && is_object_v<_Ty> && is_destructible_v<_Ty>,
"T in shared_future<T> must meet the Cpp17Destructible requirements (N4950 [futures.shared.future]/4).");
shared_future() = default;
shared_future(const shared_future& _Other) noexcept : _Mybase(_Other) {}
shared_future& operator=(const shared_future&) = default;
shared_future(future<_Ty>&& _Other) noexcept : _Mybase(_STD forward<_Mybase>(_Other)) {}
shared_future(shared_future&& _Other) noexcept : _Mybase(_STD move(_Other)) {}
shared_future& operator=(shared_future&&) = default;
const _Ty& get() const {
// block until ready then return the stored result or throw the stored exception
return this->_Get_value();
}
};
template <class _Ty>
class shared_future<_Ty&> : public _State_manager<_Ty*> {
// class that defines a copyable asynchronous return object that holds a reference
private:
using _Mybase = _State_manager<_Ty*>;
public:
shared_future() = default;
shared_future(const shared_future& _Other) noexcept : _Mybase(_Other) {}
shared_future& operator=(const shared_future&) = default;
shared_future(future<_Ty&>&& _Other) noexcept : _Mybase(_STD forward<_Mybase>(_Other)) {}
shared_future(shared_future&& _Other) noexcept : _Mybase(_STD move(_Other)) {}
shared_future& operator=(shared_future&&) = default;
_Ty& get() const {
// block until ready then return the stored result or throw the stored exception
return *this->_Get_value();
}
};
template <>