-
Notifications
You must be signed in to change notification settings - Fork 137
/
mingw.future.h
1133 lines (994 loc) · 31.5 KB
/
mingw.future.h
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
/// \file mingw.future.h
/// \brief Standard-compliant C++ futures for MinGW
///
/// (c) 2018 by Nathaniel J. McClatchey, San Jose, California
/// \author Nathaniel J. McClatchey, PhD
///
/// \copyright Simplified (2-clause) BSD License.
///
/// \note This file may become part of the mingw-w64 runtime package. If/when
/// this happens, the appropriate license will be added, i.e. this code will
/// become dual-licensed, and the current BSD 2-clause license will stay.
/// \note Target Windows version is determined by WINVER, which is determined in
/// <windows.h> from _WIN32_WINNT, which can itself be set by the user.
#ifndef MINGW_FUTURE_H_
#define MINGW_FUTURE_H_
#if !defined(__cplusplus) || (__cplusplus < 201103L)
#error The MinGW STD Threads library requires a compiler supporting C++11.
#endif
#include <future>
#include <cassert>
#include <vector>
#include <utility> // For std::pair
#include <type_traits>
#include <memory>
#include <functional> // For std::hash
#include "mingw.thread.h" // Start new threads, and use invoke.
// Mutexes and condition variables are used explicitly.
#include "mingw.mutex.h"
#include "mingw.condition_variable.h"
#if (defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR))
#pragma message "The Windows API that MinGW-w32 provides is not fully compatible\
with Microsoft's API. We'll try to work around this, but we can make no\
guarantees. This problem does not exist in MinGW-w64."
#include <windows.h> // No further granularity can be expected.
#else
#include <synchapi.h>
#include <handleapi.h>
#include <processthreadsapi.h>
#endif
// Note:
// std::shared_ptr is the natural choice for this. However, a custom
// implementation removes the need to keep a control block separate from the
// class itself (no need to support weak pointers).
namespace mingw_stdthread
{
using std::future_errc;
using std::future_error;
using std::future_status;
using std::launch;
using std::future_category;
namespace detail
{
struct Empty { };
// Use a class template to allow instantiation of statics in a header-only
// library. Note: Template will only be instantiated once to avoid bloat.
template<bool>
struct FutureStatic
{
enum Type : uint_fast8_t
{
kUndecided = 0x00,
kValueFlag = 0x01,
kExceptionFlag = 0x02,
kDeferredFlag = 0x04, // Needs special handling. Must not wait.
kReadyFlag = 0x10, // Results are ready for consumption
kTypeMask = 0x07,
kNoWaitMask = 0x14 // Indicates that waits should immediately exit.
};
static std::vector<std::pair<mutex, condition_variable> > sync_pool;
static mutex & get_mutex (void const * ptr)
{
std::hash<void const *> hash_func;
return sync_pool[hash_func(ptr) % sync_pool.size()].first;
}
static condition_variable & get_condition_variable (void const * ptr)
{
std::hash<void const *> hash_func;
return sync_pool[hash_func(ptr) % sync_pool.size()].second;
}
};
template<bool b>
std::vector<std::pair<mutex, condition_variable> > FutureStatic<b>::sync_pool (thread::hardware_concurrency() * 2 + 1);
struct FutureStateBase
{
inline mutex & get_mutex (void) const
{
return FutureStatic<true>::get_mutex(this);
}
inline condition_variable & get_condition_variable (void) const
{
return FutureStatic<true>::get_condition_variable(this);
}
typedef typename FutureStatic<true>::Type Type;
// Destroys this object. Used for allocator-awareness.
virtual void deallocate_this (void) noexcept = 0;
virtual ~FutureStateBase (void) = default;
FutureStateBase (FutureStateBase const &) = delete;
FutureStateBase & operator= (FutureStateBase const &) = delete;
FutureStateBase(Type t) noexcept
: mReferences(0), mType(t)
{
}
void increment_references (void) noexcept
{
mReferences.fetch_add(1, std::memory_order_relaxed);
}
void decrement_references (void) noexcept
{
if (mReferences.fetch_sub(1, std::memory_order_acquire) == 0)
deallocate_this();
}
std::atomic<size_t> mReferences;
std::atomic<uint_fast8_t> mType;
};
// Reduce compilation time and improve code re-use.
struct FutureBase : public FutureStatic<true>
{
typedef FutureStatic<true> Base;
FutureStateBase * mState;
mutex & get_mutex (void) const
{
return FutureStatic<true>::get_mutex(mState);
}
condition_variable & get_condition_variable (void) const
{
return FutureStatic<true>::get_condition_variable(mState);
}
FutureBase (FutureStateBase * ptr) noexcept
: mState(ptr)
{
}
FutureBase (FutureBase && source) noexcept
: mState(source.mState)
{
source.mState = nullptr;
}
~FutureBase (void)
{
release();
}
FutureBase (FutureBase const &) = delete;
FutureBase & operator= (FutureBase const &) = delete;
bool valid (void) const noexcept
{
return mState != nullptr;
}
// Releases this object's hold on its state. Requires a specification of
// which state is being used.
inline void release (void) noexcept
{
if (valid())
mState->decrement_references();
mState = nullptr;
}
void wait (std::unique_lock<mutex> & lock) const
{
#if !defined(NDEBUG)
if (!valid())
throw future_error(future_errc::no_state);
#endif
// If there's already a value or exception, don't do any extraneous
// synchronization. The `get()` method will do that for us.
if (mState->mType.load(std::memory_order_relaxed) & Type::kNoWaitMask)
return;
get_condition_variable().wait(lock, [this](void)->bool {
return mState->mType.load(std::memory_order_relaxed) & Type::kNoWaitMask;
});
}
template<class Rep, class Period>
future_status wait_for (std::chrono::duration<Rep,Period> const & dur) const
{
#if !defined(NDEBUG)
if (!valid())
throw future_error(future_errc::no_state);
#endif
auto current_state = mState->mType.load(std::memory_order_relaxed);
if (current_state & Type::kNoWaitMask)
return (current_state & Type::kDeferredFlag) ? future_status::deferred : future_status::ready;
std::unique_lock<mutex> lock { get_mutex() };
if (get_condition_variable().wait_for(lock, dur,
[this](void)->bool {
return mState->mType.load(std::memory_order_relaxed) & Type::kNoWaitMask;
}))
return future_status::ready;
else
return future_status::timeout;
}
template<class Clock, class Duration>
future_status wait_until(const std::chrono::time_point<Clock,Duration>& time) const
{
return wait_for(time - Clock::now());
}
};
template<class T>
struct FutureState : public FutureStateBase
{
// The state never needs more than one of these at any one time, so don't
// waste space or allocation time.
union {
struct {} mUndecided; // Included to make the active member unambiguous.
T mObject;
std::exception_ptr mException;
std::function<void(void)> mFunction;
};
FutureState (void) noexcept
: FutureStateBase(Type::kUndecided), mUndecided()
{
}
FutureState (std::function<void(void)> && deferred_function)
: FutureStateBase(Type::kDeferredFlag), mFunction(std::move(deferred_function))
{
}
void deallocate_this (void) noexcept override
{
delete this;
}
template<class Arg>
void set_value (Arg && arg)
{
assert(!(mType.load(std::memory_order_relaxed) & Type::kReadyFlag));
new(&mObject) T (std::forward<Arg>(arg));
mType.store(Type::kValueFlag | Type::kReadyFlag, std::memory_order_release);
}
template<class Arg>
void set_exception (Arg && arg)
{
assert(!(mType.load(std::memory_order_relaxed) & Type::kReadyFlag));
new(&mException) std::exception_ptr (std::forward<Arg>(arg));
mType.store(Type::kExceptionFlag | Type::kReadyFlag, std::memory_order_release);
}
// These overloads set value/exception, but don't make it ready.
template<class Arg>
void set_value (Arg && arg, bool)
{
assert(!(mType.load(std::memory_order_relaxed) & Type::kReadyFlag));
new(&mObject) T (std::forward<Arg>(arg));
mType.store(Type::kValueFlag, std::memory_order_release);
}
template<class Arg>
void set_exception (Arg && arg, bool)
{
assert(!(mType.load(std::memory_order_relaxed) & Type::kReadyFlag));
new(&mException) std::exception_ptr (std::forward<Arg>(arg));
mType.store(Type::kExceptionFlag, std::memory_order_release);
}
//private:
~FutureState (void)
{
auto type = mType.load(std::memory_order_acquire);
switch (type & Type::kTypeMask)
{
case Type::kDeferredFlag:
mFunction.~function();
break;
case Type::kValueFlag:
mObject.~T();
break;
case Type::kExceptionFlag:
mException.~exception_ptr();
break;
default:
assert(type == Type::kUndecided);
}
}
};
template<class T, class Alloc>
struct FutureStateAllocated : public FutureState<T>
{
typedef typename std::allocator_traits<Alloc>::void_pointer void_pointer;
void_pointer mThis;
Alloc mAllocator;
FutureStateAllocated (Alloc const & alloc, void_pointer const & vptr) noexcept
: FutureState<T>(), mThis(vptr), mAllocator(alloc)
{
}
FutureStateAllocated (FutureStateAllocated<T,Alloc> const &) = delete;
FutureStateAllocated<T,Alloc> & operator= (FutureStateAllocated<T,Alloc> const &) = delete;
void deallocate_this (void) noexcept override
{
typedef typename std::allocator_traits<Alloc>::template rebind_traits<FutureStateAllocated<T, Alloc> > allocator_traits;
typename allocator_traits::allocator_type alloc(std::move(mAllocator));
typedef typename allocator_traits::pointer pointer;
pointer ptr(static_cast<pointer>(mThis));
allocator_traits::destroy(alloc, this);
allocator_traits::deallocate(alloc, ptr, 1);
}
};
} // Namespace "detail"
#if (defined(__MINGW32__ ) && !defined(_GLIBCXX_HAS_GTHREADS))
}
namespace std {
#else
template<class T>
class future;
template<class T>
class shared_future;
template<class T>
class promise;
#endif
template<class T>
class future : mingw_stdthread::detail::FutureBase
{
typedef mingw_stdthread::detail::FutureState<T> state_type;
future (state_type * ptr) noexcept
: FutureBase(ptr)
{
}
friend class shared_future<T>;
friend class promise<T>;
template<class U>
friend class future;
template<class _Fn, class ... _Args>
friend future<__async_result_of<_Fn, _Args...>> async (std::launch, _Fn &&, _Args&&...);
public:
using FutureBase::valid;
using FutureBase::wait_for;
using FutureBase::wait_until;
future (void) noexcept
: FutureBase(nullptr)
{
}
future<T> & operator= (future<T> && source) noexcept
{
// Check for this atypical behavior rather than creating a nonsensical state.
if (this != &source)
{
release();
mState = source.mState;
source.mState = nullptr;
}
return *this;
}
future (future<T> && source) noexcept
: FutureBase(std::move(source))
{
}
~future (void) = default;
future (future<T> const &) = delete;
future<T> & operator= (future<T> const &) = delete;
T const & get (void) const
{
wait();
auto type = mState->mType.load(std::memory_order_acquire);
if (type == (Type::kValueFlag | Type::kReadyFlag))
return static_cast<state_type *>(mState)->mObject;
else
{
assert(type == (Type::kExceptionFlag | Type::kReadyFlag));
std::rethrow_exception(static_cast<state_type *>(mState)->mException);
}
}
shared_future<T> share (void) noexcept;
void wait (void) const
{
auto state = mState->mType.load(std::memory_order_relaxed);
if (state & Type::kReadyFlag)
return;
std::unique_lock<mingw_stdthread::mutex> lock { get_mutex() };
FutureBase::wait(lock);
if (mState->mType.load(std::memory_order_acquire) & Type::kDeferredFlag)
{
state_type * ptr = static_cast<state_type *>(mState);
decltype(ptr->mFunction) func = std::move(ptr->mFunction);
ptr->mFunction.~function();
func();
lock.unlock();
ptr->get_condition_variable().notify_all();
}
}
};
template<class T>
class shared_future : future<T>
{
typedef typename future<T>::state_type state_type;
public:
using future<T>::get;
using future<T>::wait;
using future<T>::wait_for;
using future<T>::wait_until;
using future<T>::valid;
shared_future (void) noexcept : future<T>()
{
}
shared_future (shared_future<T> && source) noexcept
: future<T>(std::move(source))
{
}
shared_future<T> & operator= (shared_future<T> && source) noexcept
{
return future<T>::operator=(std::move(source));
}
shared_future (shared_future<T> const & source) noexcept(__cplusplus >= 201703L)
: future<T>(static_cast<state_type *>(source.mState))
{
future<T>::mState->increment_references();
}
shared_future<T> & operator= (shared_future<T> const & source) noexcept(__cplusplus >= 201703L)
{
if (future<T>::mState == source.mState)
return *this;
future<T>::release();
future<T>::mState = source.mState;
future<T>::mState->increment_references();
return *this;
}
shared_future (future<T> && source) noexcept
: future<T>(std::move(source))
{
}
shared_future<T> & operator= (future<T> && source) noexcept
{
future<T>::operator=(std::move(source));
return *this;
}
~shared_future (void) = default;
};
template<class T>
class promise : mingw_stdthread::detail::FutureBase
{
bool mRetrieved;
typedef mingw_stdthread::detail::FutureState<T> state_type;
void check_before_set (void) const
{
if (!valid())
throw future_error(future_errc::no_state);
if (mState->mType.load(std::memory_order_relaxed) & Type::kReadyFlag)
throw future_error(future_errc::promise_already_satisfied);
}
void check_abandon (void)
{
if (valid() && !(mState->mType.load(std::memory_order_relaxed) & Type::kReadyFlag))
{
set_exception(std::make_exception_ptr(future_error(future_errc::broken_promise)));
}
}
/// \bug Might throw more exceptions than specified by the standard...
// Need OS support for this...
void make_ready_at_thread_exit (void)
{
static constexpr DWORD kInfinite = 0xffffffffl;
// Need to turn the pseudohandle from GetCurrentThread() into a true handle...
HANDLE thread_handle;
BOOL success = DuplicateHandle(GetCurrentProcess(),
GetCurrentThread(),
GetCurrentProcess(),
&thread_handle,
0, // Access doesn't matter. Will be duplicated.
FALSE, // No need for this to be inherited.
DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE);
if (!success)
throw std::runtime_error("MinGW STD Threads library failed to make a promise ready after thread exit.");
mState->increment_references();
bool handle_handled = false;
try {
state_type * ptr = static_cast<state_type *>(mState);
mingw_stdthread::thread watcher_thread ([ptr, thread_handle, &handle_handled](void)
{
{
std::lock_guard<mingw_stdthread::mutex> guard (ptr->get_mutex());
handle_handled = true;
}
ptr->get_condition_variable().notify_all();
// Wait for the original thread to die.
WaitForSingleObject(thread_handle, kInfinite);
CloseHandle(thread_handle);
{
std::lock_guard<mingw_stdthread::mutex> guard (ptr->get_mutex());
ptr->mType.fetch_or(Type::kReadyFlag, std::memory_order_relaxed);
}
ptr->get_condition_variable().notify_all();
ptr->decrement_references();
});
{
std::unique_lock<mingw_stdthread::mutex> guard (ptr->get_mutex());
ptr->get_condition_variable().wait(guard, [&handle_handled](void)->bool
{
return handle_handled;
});
}
watcher_thread.detach();
}
catch (...)
{
// Because the original promise is still alive, this can't be the decrement
// destroys it.
mState->decrement_references();
if (!handle_handled)
CloseHandle(thread_handle);
}
}
template<class U>
future<U> make_future (void)
{
if (!valid())
throw future_error(future_errc::no_state);
if (mRetrieved)
throw future_error(future_errc::future_already_retrieved);
mState->increment_references();
mRetrieved = true;
return future<U>(static_cast<state_type *>(mState));
}
template<class U>
friend class promise;
public:
// Create a promise with an empty state, with the reference counter set to
// indicate that the state is only held by this promise (i.e. not by any
// futures).
promise (void)
: FutureBase(new state_type ()), mRetrieved(false)
{
}
template<typename Alloc>
promise (std::allocator_arg_t, Alloc const & alloc)
: FutureBase(nullptr), mRetrieved(false)
{
typedef mingw_stdthread::detail::FutureStateAllocated<T,Alloc> State;
typedef typename std::allocator_traits<Alloc>::template rebind_traits<State> Traits;
typename Traits::allocator_type rebound_alloc(alloc);
typename Traits::pointer ptr = Traits::allocate(rebound_alloc, 1);
typename Traits::void_pointer vptr = ptr;
State * sptr = std::addressof(*ptr);
Traits::construct(rebound_alloc, sptr, std::move(rebound_alloc), vptr);
mState = static_cast<state_type *>(sptr);
}
promise (promise<T> && source) noexcept
: FutureBase(std::move(source)), mRetrieved(source.mRetrieved)
{
}
~promise (void)
{
check_abandon();
}
promise<T> & operator= (promise<T> && source) noexcept
{
if (this == &source)
return *this;
check_abandon();
release();
mState = source.mState;
mRetrieved = source.mRetrieved;
source.mState = nullptr;
return *this;
}
void swap (promise<T> & other) noexcept
{
std::swap(mState, other.mState);
std::swap(mRetrieved, other.mRetrieved);
}
promise (promise<T> const &) = delete;
promise<T> & operator= (promise<T> const &) = delete;
future<T> get_future (void)
{
return make_future<T>();
}
void set_value (T const & value)
{
{
std::lock_guard<mingw_stdthread::mutex> lock { get_mutex() };
check_before_set();
static_cast<state_type *>(mState)->set_value(value);
}
get_condition_variable().notify_all();
}
void set_value (T && value)
{
{
std::lock_guard<mingw_stdthread::mutex> lock { get_mutex() };
check_before_set();
static_cast<state_type *>(mState)->set_value(std::move(value));
}
get_condition_variable().notify_all();
}
void set_value_at_thread_exit (T const & value)
{
{
std::lock_guard<mingw_stdthread::mutex> lock { get_mutex() };
check_before_set();
static_cast<state_type *>(mState)->set_value(value, false);
}
make_ready_at_thread_exit();
}
void set_value_at_thread_exit (T && value)
{
{
std::lock_guard<mingw_stdthread::mutex> lock { get_mutex() };
check_before_set();
static_cast<state_type *>(mState)->set_value(std::move(value), false);
}
make_ready_at_thread_exit();
}
void set_exception (std::exception_ptr eptr)
{
{
std::lock_guard<mingw_stdthread::mutex> lock { get_mutex() };
check_before_set();
static_cast<state_type *>(mState)->set_exception(eptr);
}
get_condition_variable().notify_all();
}
void set_exception_at_thread_exit (std::exception_ptr eptr)
{
{
std::lock_guard<mingw_stdthread::mutex> lock { get_mutex() };
check_before_set();
static_cast<state_type *>(mState)->set_exception(eptr, false);
}
make_ready_at_thread_exit();
}
};
////////////////////////////////////////////////////////////////////////////////
// Reference Specialization //
////////////////////////////////////////////////////////////////////////////////
template<class T>
class future<T&> : future<void *>
{
typedef future<void *> Base;
template<class U>
friend class shared_future;
template<class U>
friend class promise;
future (typename Base::state_type * state)
: Base(state)
{
}
template<class _Fn, class ... _Args>
friend future<__async_result_of<_Fn, _Args...>> async (std::launch, _Fn &&, _Args&&...);
public:
using Base::valid;
using Base::wait_for;
using Base::wait_until;
using Base::wait;
future (void) noexcept = default;
inline T& get (void) const
{
return *static_cast<T *>(Base::get());
}
shared_future<T&> share (void) noexcept;
};
template<class T>
class shared_future<T&> : shared_future<void *>
{
typedef shared_future<void *> Base;
public:
using Base::wait;
using Base::wait_for;
using Base::wait_until;
using Base::valid;
inline T& get (void) const
{
return *static_cast<T *>(Base::get());
}
shared_future (future<T&> && source) noexcept
: Base(std::move(source))
{
}
shared_future<T&> & operator= (future<T&> && source) noexcept
{
Base::operator=(std::move(source));
return *this;
}
~shared_future (void) = default;
};
template<class T>
class promise<T&> : private promise<void *>
{
typedef promise<void *> Base;
public:
using Base::set_exception;
using Base::set_exception_at_thread_exit;
promise (void) = default;
template<typename Alloc>
promise (std::allocator_arg_t arg, Alloc const & alloc)
: Base(arg, alloc)
{
}
inline void set_value (T & value)
{
typedef typename std::remove_cv<T>::type T_non_cv;
Base::set_value(const_cast<T_non_cv *>(std::addressof(value)));
}
inline void set_value_at_thread_exit (T & value)
{
typedef typename std::remove_cv<T>::type T_non_cv;
Base::set_value_at_thread_exit(const_cast<T_non_cv *>(std::addressof(value)));
}
inline future<T&> get_future (void)
{
return Base::template make_future<T&>();
}
void swap (promise<T&> & other) noexcept
{
Base::swap(other);
}
};
////////////////////////////////////////////////////////////////////////////////
// Void Specialization //
////////////////////////////////////////////////////////////////////////////////
template<>
class future<void> : future<mingw_stdthread::detail::Empty>
{
typedef mingw_stdthread::detail::Empty Empty;
template<class U>
friend class shared_future;
template<class U>
friend class promise;
future(future<Empty>::state_type * state)
: future<Empty>(state)
{
}
template<class _Fn, class ... _Args>
friend future<__async_result_of<_Fn, _Args...>> async (std::launch, _Fn &&, _Args&&...);
public:
using future<Empty>::valid;
using future<Empty>::wait_for;
using future<Empty>::wait_until;
using future<Empty>::wait;
future (void) noexcept = default;
void get (void) const
{
future<Empty>::get();
}
shared_future<void> share (void) noexcept;
};
template<>
class shared_future<void> : shared_future<mingw_stdthread::detail::Empty>
{
typedef mingw_stdthread::detail::Empty Empty;
public:
using shared_future<Empty>::wait;
using shared_future<Empty>::wait_for;
using shared_future<Empty>::wait_until;
using shared_future<Empty>::valid;
void get (void) const
{
shared_future<Empty>::get();
}
shared_future (void) noexcept = default;
shared_future (shared_future<void> && source) noexcept = default;
shared_future<void> & operator= (shared_future<void> && source) noexcept = default;
shared_future (shared_future<void> const & source) noexcept(__cplusplus >= 201703L) = default;
shared_future<void> & operator= (shared_future<void> const & source) noexcept(__cplusplus >= 201703L) = default;
shared_future (future<void> && source) noexcept
: shared_future<Empty>(std::move(source))
{
}
shared_future<void> & operator= (future<void> && source) noexcept
{
shared_future<Empty>::operator=(std::move(source));
return *this;
}
~shared_future (void) = default;
};
inline shared_future<void> future<void>::share (void) noexcept
{
return shared_future<void>(std::move(*this));
}
template<class T>
shared_future<T> future<T>::share (void) noexcept
{
return shared_future<T>(std::move(*this));
}
template<class T>
shared_future<T&> future<T&>::share (void) noexcept
{
return shared_future<T&>(std::move(*this));
}
template<>
class promise<void> : private promise<mingw_stdthread::detail::Empty>
{
typedef mingw_stdthread::detail::Empty Empty;
public:
using promise<Empty>::set_exception;
using promise<Empty>::set_exception_at_thread_exit;
promise (void) = default;
template<typename Alloc>
promise (std::allocator_arg_t arg, Alloc const & alloc)
: promise<Empty>(arg, alloc)
{
}
inline void set_value (void)
{
promise<Empty>::set_value(Empty());
}
inline void set_value_at_thread_exit (void)
{
promise<Empty>::set_value_at_thread_exit(Empty());
}
inline future<void> get_future (void)
{
return promise<Empty>::template make_future<void>();
}
void swap (promise<void> & other) noexcept
{
promise<Empty>::swap(other);
}
};
template<class T>
void swap(promise<T> & lhs, promise<T> & rhs) noexcept
{
lhs.swap(rhs);
}
} // Namespace "std"
namespace mingw_stdthread
{
namespace detail
{
template<class Ret>
struct StorageHelper
{
template<class Func, class ... Args>
static void store_deferred (FutureState<Ret> * state_ptr, Func && func, Args&&... args)
{
try {
state_ptr->set_value(invoke(std::forward<Func>(func), std::forward<Args>(args)...));
} catch (...) {
state_ptr->set_exception(std::current_exception());
}
}
template<class Func, class ... Args>
static void store (FutureState<Ret> * state_ptr, Func && func, Args&&... args)
{
try {
auto result = invoke(std::forward<Func>(func), std::forward<Args>(args)...);
std::lock_guard<mingw_stdthread::mutex> lock { state_ptr->get_mutex() }; // Reveal the results of the above action to
state_ptr->set_value(std::move(result));
} catch (...) {
std::lock_guard<mingw_stdthread::mutex> lock { state_ptr->get_mutex() }; // Reveal the results of the above action to
state_ptr->set_exception(std::current_exception());
}
state_ptr->get_condition_variable().notify_all();
}
};
template<class Ref>
struct StorageHelper<Ref&>
{
template<class Func, class ... Args>
static void store_deferred (FutureState<void*> * state_ptr, Func && func, Args&&... args)
{
try {
typedef typename std::remove_cv<Ref>::type Ref_non_cv;
Ref & rf = invoke(std::forward<Func>(func), std::forward<Args>(args)...);
state_ptr->set_value(const_cast<Ref_non_cv *>(std::addressof(rf)));
} catch (...) {
state_ptr->set_exception(std::current_exception());
}
}
template<class Func, class ... Args>
static void store (FutureState<void*> * state_ptr, Func && func, Args&&... args)
{
try {
typedef typename std::remove_cv<Ref>::type Ref_non_cv;
Ref & rf = invoke(std::forward<Func>(func), std::forward<Args>(args)...);
std::lock_guard<mingw_stdthread::mutex> lock { state_ptr->get_mutex() };
state_ptr->set_value(const_cast<Ref_non_cv *>(std::addressof(rf)));
} catch (...) {
std::lock_guard<mingw_stdthread::mutex> lock { state_ptr->get_mutex() };
state_ptr->set_exception(std::current_exception());
}
state_ptr->get_condition_variable().notify_all();
}
};
template<>
struct StorageHelper<void>
{
template<class Func, class ... Args>
static void store_deferred (FutureState<Empty> * state_ptr, Func && func, Args&&... args)
{
try {
invoke(std::forward<Func>(func), std::forward<Args>(args)...);
state_ptr->set_value(Empty{});