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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
|
// <future> -*- C++ -*-
// Copyright (C) 2009-2015 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>.
/** @file include/future
* This is a Standard C++ Library header.
*/
#ifndef _GLIBCXX_FUTURE
#define _GLIBCXX_FUTURE 1
#pragma GCC system_header
#if __cplusplus < 201103L
# include <bits/c++0x_warning.h>
#else
#include <functional>
#include <mutex>
#include <thread>
#include <condition_variable>
#include <system_error>
#include <atomic>
#include <bits/atomic_futex.h>
#include <bits/functexcept.h>
#include <bits/unique_ptr.h>
#include <bits/shared_ptr.h>
#include <bits/uses_allocator.h>
#include <bits/allocated_ptr.h>
#include <ext/aligned_buffer.h>
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
/**
* @defgroup futures Futures
* @ingroup concurrency
*
* Classes for futures support.
* @{
*/
/// Error code for futures
enum class future_errc
{
future_already_retrieved = 1,
promise_already_satisfied,
no_state,
broken_promise
};
/// Specialization.
template<>
struct is_error_code_enum<future_errc> : public true_type { };
/// Points to a statically-allocated object derived from error_category.
const error_category&
future_category() noexcept;
/// Overload for make_error_code.
inline error_code
make_error_code(future_errc __errc) noexcept
{ return error_code(static_cast<int>(__errc), future_category()); }
/// Overload for make_error_condition.
inline error_condition
make_error_condition(future_errc __errc) noexcept
{ return error_condition(static_cast<int>(__errc), future_category()); }
/**
* @brief Exception type thrown by futures.
* @ingroup exceptions
*/
class future_error : public logic_error
{
error_code _M_code;
public:
explicit future_error(error_code __ec)
: logic_error("std::future_error: " + __ec.message()), _M_code(__ec)
{ }
virtual ~future_error() noexcept;
virtual const char*
what() const noexcept;
const error_code&
code() const noexcept { return _M_code; }
};
// Forward declarations.
template<typename _Res>
class future;
template<typename _Res>
class shared_future;
template<typename _Signature>
class packaged_task;
template<typename _Res>
class promise;
/// Launch code for futures
enum class launch
{
async = 1,
deferred = 2
};
constexpr launch operator&(launch __x, launch __y)
{
return static_cast<launch>(
static_cast<int>(__x) & static_cast<int>(__y));
}
constexpr launch operator|(launch __x, launch __y)
{
return static_cast<launch>(
static_cast<int>(__x) | static_cast<int>(__y));
}
constexpr launch operator^(launch __x, launch __y)
{
return static_cast<launch>(
static_cast<int>(__x) ^ static_cast<int>(__y));
}
constexpr launch operator~(launch __x)
{ return static_cast<launch>(~static_cast<int>(__x)); }
inline launch& operator&=(launch& __x, launch __y)
{ return __x = __x & __y; }
inline launch& operator|=(launch& __x, launch __y)
{ return __x = __x | __y; }
inline launch& operator^=(launch& __x, launch __y)
{ return __x = __x ^ __y; }
/// Status code for futures
enum class future_status
{
ready,
timeout,
deferred
};
template<typename _Fn, typename... _Args>
future<typename result_of<_Fn(_Args...)>::type>
async(launch __policy, _Fn&& __fn, _Args&&... __args);
template<typename _Fn, typename... _Args>
future<typename result_of<_Fn(_Args...)>::type>
async(_Fn&& __fn, _Args&&... __args);
#if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1) \
&& (ATOMIC_INT_LOCK_FREE > 1)
/// Base class and enclosing scope.
struct __future_base
{
/// Base class for results.
struct _Result_base
{
exception_ptr _M_error;
_Result_base(const _Result_base&) = delete;
_Result_base& operator=(const _Result_base&) = delete;
// _M_destroy() allows derived classes to control deallocation
virtual void _M_destroy() = 0;
struct _Deleter
{
void operator()(_Result_base* __fr) const { __fr->_M_destroy(); }
};
protected:
_Result_base();
virtual ~_Result_base();
};
/// A unique_ptr for result objects.
template<typename _Res>
using _Ptr = unique_ptr<_Res, _Result_base::_Deleter>;
/// A result object that has storage for an object of type _Res.
template<typename _Res>
struct _Result : _Result_base
{
private:
__gnu_cxx::__aligned_buffer<_Res> _M_storage;
bool _M_initialized;
public:
typedef _Res result_type;
_Result() noexcept : _M_initialized() { }
~_Result()
{
if (_M_initialized)
_M_value().~_Res();
}
// Return lvalue, future will add const or rvalue-reference
_Res&
_M_value() noexcept { return *_M_storage._M_ptr(); }
void
_M_set(const _Res& __res)
{
::new (_M_storage._M_addr()) _Res(__res);
_M_initialized = true;
}
void
_M_set(_Res&& __res)
{
::new (_M_storage._M_addr()) _Res(std::move(__res));
_M_initialized = true;
}
private:
void _M_destroy() { delete this; }
};
/// A result object that uses an allocator.
template<typename _Res, typename _Alloc>
struct _Result_alloc final : _Result<_Res>, _Alloc
{
using __allocator_type = __alloc_rebind<_Alloc, _Result_alloc>;
explicit
_Result_alloc(const _Alloc& __a) : _Result<_Res>(), _Alloc(__a)
{ }
private:
void _M_destroy()
{
__allocator_type __a(*this);
__allocated_ptr<__allocator_type> __guard_ptr{ __a, this };
this->~_Result_alloc();
}
};
// Create a result object that uses an allocator.
template<typename _Res, typename _Allocator>
static _Ptr<_Result_alloc<_Res, _Allocator>>
_S_allocate_result(const _Allocator& __a)
{
using __result_type = _Result_alloc<_Res, _Allocator>;
typename __result_type::__allocator_type __a2(__a);
auto __guard = std::__allocate_guarded(__a2);
__result_type* __p = ::new((void*)__guard.get()) __result_type{__a};
__guard = nullptr;
return _Ptr<__result_type>(__p);
}
// Keep it simple for std::allocator.
template<typename _Res, typename _Tp>
static _Ptr<_Result<_Res>>
_S_allocate_result(const std::allocator<_Tp>& __a)
{
return _Ptr<_Result<_Res>>(new _Result<_Res>);
}
// Base class for various types of shared state created by an
// asynchronous provider (such as a std::promise) and shared with one
// or more associated futures.
class _State_baseV2
{
typedef _Ptr<_Result_base> _Ptr_type;
enum _Status : unsigned {
__not_ready,
__ready
};
_Ptr_type _M_result;
__atomic_futex_unsigned<> _M_status;
atomic_flag _M_retrieved = ATOMIC_FLAG_INIT;
once_flag _M_once;
public:
_State_baseV2() noexcept : _M_result(), _M_status(_Status::__not_ready)
{ }
_State_baseV2(const _State_baseV2&) = delete;
_State_baseV2& operator=(const _State_baseV2&) = delete;
virtual ~_State_baseV2() = default;
_Result_base&
wait()
{
// Run any deferred function or join any asynchronous thread:
_M_complete_async();
// Acquire MO makes sure this synchronizes with the thread that made
// the future ready.
_M_status._M_load_when_equal(_Status::__ready, memory_order_acquire);
return *_M_result;
}
template<typename _Rep, typename _Period>
future_status
wait_for(const chrono::duration<_Rep, _Period>& __rel)
{
// First, check if the future has been made ready. Use acquire MO
// to synchronize with the thread that made it ready.
if (_M_status._M_load(memory_order_acquire) == _Status::__ready)
return future_status::ready;
if (_M_is_deferred_future())
return future_status::deferred;
if (_M_status._M_load_when_equal_for(_Status::__ready,
memory_order_acquire, __rel))
{
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 2100. timed waiting functions must also join
// This call is a no-op by default except on an async future,
// in which case the async thread is joined. It's also not a
// no-op for a deferred future, but such a future will never
// reach this point because it returns future_status::deferred
// instead of waiting for the future to become ready (see
// above). Async futures synchronize in this call, so we need
// no further synchronization here.
_M_complete_async();
return future_status::ready;
}
return future_status::timeout;
}
template<typename _Clock, typename _Duration>
future_status
wait_until(const chrono::time_point<_Clock, _Duration>& __abs)
{
// First, check if the future has been made ready. Use acquire MO
// to synchronize with the thread that made it ready.
if (_M_status._M_load(memory_order_acquire) == _Status::__ready)
return future_status::ready;
if (_M_is_deferred_future())
return future_status::deferred;
if (_M_status._M_load_when_equal_until(_Status::__ready,
memory_order_acquire, __abs))
{
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 2100. timed waiting functions must also join
// See wait_for(...) above.
_M_complete_async();
return future_status::ready;
}
return future_status::timeout;
}
// Provide a result to the shared state and make it ready.
// Calls at most once: _M_result = __res();
void
_M_set_result(function<_Ptr_type()> __res, bool __ignore_failure = false)
{
bool __did_set = false;
// all calls to this function are serialized,
// side-effects of invoking __res only happen once
call_once(_M_once, &_State_baseV2::_M_do_set, this,
std::__addressof(__res), std::__addressof(__did_set));
if (__did_set)
// Use release MO to synchronize with observers of the ready state.
_M_status._M_store_notify_all(_Status::__ready,
memory_order_release);
else if (!__ignore_failure)
__throw_future_error(int(future_errc::promise_already_satisfied));
}
// Provide a result to the shared state but delay making it ready
// until the calling thread exits.
// Calls at most once: _M_result = __res();
void
_M_set_delayed_result(function<_Ptr_type()> __res,
weak_ptr<_State_baseV2> __self)
{
bool __did_set = false;
unique_ptr<_Make_ready> __mr{new _Make_ready};
// all calls to this function are serialized,
// side-effects of invoking __res only happen once
call_once(_M_once, &_State_baseV2::_M_do_set, this,
std::__addressof(__res), std::__addressof(__did_set));
if (!__did_set)
__throw_future_error(int(future_errc::promise_already_satisfied));
__mr->_M_shared_state = std::move(__self);
__mr->_M_set();
__mr.release();
}
// Abandon this shared state.
void
_M_break_promise(_Ptr_type __res)
{
if (static_cast<bool>(__res))
{
error_code __ec(make_error_code(future_errc::broken_promise));
__res->_M_error = make_exception_ptr(future_error(__ec));
// This function is only called when the last asynchronous result
// provider is abandoning this shared state, so noone can be
// trying to make the shared state ready at the same time, and
// we can access _M_result directly instead of through call_once.
_M_result.swap(__res);
// Use release MO to synchronize with observers of the ready state.
_M_status._M_store_notify_all(_Status::__ready,
memory_order_release);
}
}
// Called when this object is first passed to a future.
void
_M_set_retrieved_flag()
{
if (_M_retrieved.test_and_set())
__throw_future_error(int(future_errc::future_already_retrieved));
}
template<typename _Res, typename _Arg>
struct _Setter;
// set lvalues
template<typename _Res, typename _Arg>
struct _Setter<_Res, _Arg&>
{
// check this is only used by promise<R>::set_value(const R&)
// or promise<R&>::set_value(R&)
static_assert(is_same<_Res, _Arg&>::value // promise<R&>
|| is_same<const _Res, _Arg>::value, // promise<R>
"Invalid specialisation");
// Used by std::promise to copy construct the result.
typename promise<_Res>::_Ptr_type operator()() const
{
_State_baseV2::_S_check(_M_promise->_M_future);
_M_promise->_M_storage->_M_set(*_M_arg);
return std::move(_M_promise->_M_storage);
}
promise<_Res>* _M_promise;
_Arg* _M_arg;
};
// set rvalues
template<typename _Res>
struct _Setter<_Res, _Res&&>
{
// Used by std::promise to move construct the result.
typename promise<_Res>::_Ptr_type operator()() const
{
_State_baseV2::_S_check(_M_promise->_M_future);
_M_promise->_M_storage->_M_set(std::move(*_M_arg));
return std::move(_M_promise->_M_storage);
}
promise<_Res>* _M_promise;
_Res* _M_arg;
};
struct __exception_ptr_tag { };
// set exceptions
template<typename _Res>
struct _Setter<_Res, __exception_ptr_tag>
{
// Used by std::promise to store an exception as the result.
typename promise<_Res>::_Ptr_type operator()() const
{
_State_baseV2::_S_check(_M_promise->_M_future);
_M_promise->_M_storage->_M_error = *_M_ex;
return std::move(_M_promise->_M_storage);
}
promise<_Res>* _M_promise;
exception_ptr* _M_ex;
};
template<typename _Res, typename _Arg>
static _Setter<_Res, _Arg&&>
__setter(promise<_Res>* __prom, _Arg&& __arg)
{
return _Setter<_Res, _Arg&&>{ __prom, &__arg };
}
template<typename _Res>
static _Setter<_Res, __exception_ptr_tag>
__setter(exception_ptr& __ex, promise<_Res>* __prom)
{
return _Setter<_Res, __exception_ptr_tag>{ __prom, &__ex };
}
template<typename _Tp>
static void
_S_check(const shared_ptr<_Tp>& __p)
{
if (!static_cast<bool>(__p))
__throw_future_error((int)future_errc::no_state);
}
private:
// The function invoked with std::call_once(_M_once, ...).
void
_M_do_set(function<_Ptr_type()>* __f, bool* __did_set)
{
_Ptr_type __res = (*__f)();
// Notify the caller that we did try to set; if we do not throw an
// exception, the caller will be aware that it did set (e.g., see
// _M_set_result).
*__did_set = true;
_M_result.swap(__res); // nothrow
}
// Wait for completion of async function.
virtual void _M_complete_async() { }
// Return true if state corresponds to a deferred function.
virtual bool _M_is_deferred_future() const { return false; }
struct _Make_ready final : __at_thread_exit_elt
{
weak_ptr<_State_baseV2> _M_shared_state;
static void _S_run(void*);
void _M_set();
};
};
#ifdef _GLIBCXX_ASYNC_ABI_COMPAT
class _State_base;
class _Async_state_common;
#else
using _State_base = _State_baseV2;
class _Async_state_commonV2;
#endif
template<typename _BoundFn, typename = typename _BoundFn::result_type>
class _Deferred_state;
template<typename _BoundFn, typename = typename _BoundFn::result_type>
class _Async_state_impl;
template<typename _Signature>
class _Task_state_base;
template<typename _Fn, typename _Alloc, typename _Signature>
class _Task_state;
template<typename _BoundFn>
static std::shared_ptr<_State_base>
_S_make_deferred_state(_BoundFn&& __fn);
template<typename _BoundFn>
static std::shared_ptr<_State_base>
_S_make_async_state(_BoundFn&& __fn);
template<typename _Res_ptr, typename _Fn,
typename _Res = typename _Res_ptr::element_type::result_type>
struct _Task_setter;
template<typename _Res_ptr, typename _BoundFn>
static _Task_setter<_Res_ptr, _BoundFn>
_S_task_setter(_Res_ptr& __ptr, _BoundFn& __call)
{
return { std::__addressof(__ptr), std::__addressof(__call) };
}
};
/// Partial specialization for reference types.
template<typename _Res>
struct __future_base::_Result<_Res&> : __future_base::_Result_base
{
typedef _Res& result_type;
_Result() noexcept : _M_value_ptr() { }
void
_M_set(_Res& __res) noexcept
{ _M_value_ptr = std::addressof(__res); }
_Res& _M_get() noexcept { return *_M_value_ptr; }
private:
_Res* _M_value_ptr;
void _M_destroy() { delete this; }
};
/// Explicit specialization for void.
template<>
struct __future_base::_Result<void> : __future_base::_Result_base
{
typedef void result_type;
private:
void _M_destroy() { delete this; }
};
#ifndef _GLIBCXX_ASYNC_ABI_COMPAT
// Allow _Setter objects to be stored locally in std::function
template<typename _Res, typename _Arg>
struct __is_location_invariant
<__future_base::_State_base::_Setter<_Res, _Arg>>
: true_type { };
// Allow _Task_setter objects to be stored locally in std::function
template<typename _Res_ptr, typename _Fn, typename _Res>
struct __is_location_invariant
<__future_base::_Task_setter<_Res_ptr, _Fn, _Res>>
: true_type { };
/// Common implementation for future and shared_future.
template<typename _Res>
class __basic_future : public __future_base
{
protected:
typedef shared_ptr<_State_base> __state_type;
typedef __future_base::_Result<_Res>& __result_type;
private:
__state_type _M_state;
public:
// Disable copying.
__basic_future(const __basic_future&) = delete;
__basic_future& operator=(const __basic_future&) = delete;
bool
valid() const noexcept { return static_cast<bool>(_M_state); }
void
wait() const
{
_State_base::_S_check(_M_state);
_M_state->wait();
}
template<typename _Rep, typename _Period>
future_status
wait_for(const chrono::duration<_Rep, _Period>& __rel) const
{
_State_base::_S_check(_M_state);
return _M_state->wait_for(__rel);
}
template<typename _Clock, typename _Duration>
future_status
wait_until(const chrono::time_point<_Clock, _Duration>& __abs) const
{
_State_base::_S_check(_M_state);
return _M_state->wait_until(__abs);
}
protected:
/// Wait for the state to be ready and rethrow any stored exception
__result_type
_M_get_result() const
{
_State_base::_S_check(_M_state);
_Result_base& __res = _M_state->wait();
if (!(__res._M_error == 0))
rethrow_exception(__res._M_error);
return static_cast<__result_type>(__res);
}
void _M_swap(__basic_future& __that) noexcept
{
_M_state.swap(__that._M_state);
}
// Construction of a future by promise::get_future()
explicit
__basic_future(const __state_type& __state) : _M_state(__state)
{
_State_base::_S_check(_M_state);
_M_state->_M_set_retrieved_flag();
}
// Copy construction from a shared_future
explicit
__basic_future(const shared_future<_Res>&) noexcept;
// Move construction from a shared_future
explicit
__basic_future(shared_future<_Res>&&) noexcept;
// Move construction from a future
explicit
__basic_future(future<_Res>&&) noexcept;
constexpr __basic_future() noexcept : _M_state() { }
struct _Reset
{
explicit _Reset(__basic_future& __fut) noexcept : _M_fut(__fut) { }
~_Reset() { _M_fut._M_state.reset(); }
__basic_future& _M_fut;
};
};
/// Primary template for future.
template<typename _Res>
class future : public __basic_future<_Res>
{
friend class promise<_Res>;
template<typename> friend class packaged_task;
template<typename _Fn, typename... _Args>
friend future<typename result_of<_Fn(_Args...)>::type>
async(launch, _Fn&&, _Args&&...);
typedef __basic_future<_Res> _Base_type;
typedef typename _Base_type::__state_type __state_type;
explicit
future(const __state_type& __state) : _Base_type(__state) { }
public:
constexpr future() noexcept : _Base_type() { }
/// Move constructor
future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
// Disable copying
future(const future&) = delete;
future& operator=(const future&) = delete;
future& operator=(future&& __fut) noexcept
{
future(std::move(__fut))._M_swap(*this);
return *this;
}
/// Retrieving the value
_Res
get()
{
typename _Base_type::_Reset __reset(*this);
return std::move(this->_M_get_result()._M_value());
}
shared_future<_Res> share();
};
/// Partial specialization for future<R&>
template<typename _Res>
class future<_Res&> : public __basic_future<_Res&>
{
friend class promise<_Res&>;
template<typename> friend class packaged_task;
template<typename _Fn, typename... _Args>
friend future<typename result_of<_Fn(_Args...)>::type>
async(launch, _Fn&&, _Args&&...);
typedef __basic_future<_Res&> _Base_type;
typedef typename _Base_type::__state_type __state_type;
explicit
future(const __state_type& __state) : _Base_type(__state) { }
public:
constexpr future() noexcept : _Base_type() { }
/// Move constructor
future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
// Disable copying
future(const future&) = delete;
future& operator=(const future&) = delete;
future& operator=(future&& __fut) noexcept
{
future(std::move(__fut))._M_swap(*this);
return *this;
}
/// Retrieving the value
_Res&
get()
{
typename _Base_type::_Reset __reset(*this);
return this->_M_get_result()._M_get();
}
shared_future<_Res&> share();
};
/// Explicit specialization for future<void>
template<>
class future<void> : public __basic_future<void>
{
friend class promise<void>;
template<typename> friend class packaged_task;
template<typename _Fn, typename... _Args>
friend future<typename result_of<_Fn(_Args...)>::type>
async(launch, _Fn&&, _Args&&...);
typedef __basic_future<void> _Base_type;
typedef typename _Base_type::__state_type __state_type;
explicit
future(const __state_type& __state) : _Base_type(__state) { }
public:
constexpr future() noexcept : _Base_type() { }
/// Move constructor
future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
// Disable copying
future(const future&) = delete;
future& operator=(const future&) = delete;
future& operator=(future&& __fut) noexcept
{
future(std::move(__fut))._M_swap(*this);
return *this;
}
/// Retrieving the value
void
get()
{
typename _Base_type::_Reset __reset(*this);
this->_M_get_result();
}
shared_future<void> share();
};
/// Primary template for shared_future.
template<typename _Res>
class shared_future : public __basic_future<_Res>
{
typedef __basic_future<_Res> _Base_type;
public:
constexpr shared_future() noexcept : _Base_type() { }
/// Copy constructor
shared_future(const shared_future& __sf) : _Base_type(__sf) { }
/// Construct from a future rvalue
shared_future(future<_Res>&& __uf) noexcept
: _Base_type(std::move(__uf))
{ }
/// Construct from a shared_future rvalue
shared_future(shared_future&& __sf) noexcept
: _Base_type(std::move(__sf))
{ }
shared_future& operator=(const shared_future& __sf)
{
shared_future(__sf)._M_swap(*this);
return *this;
}
shared_future& operator=(shared_future&& __sf) noexcept
{
shared_future(std::move(__sf))._M_swap(*this);
return *this;
}
/// Retrieving the value
const _Res&
get() const { return this->_M_get_result()._M_value(); }
};
/// Partial specialization for shared_future<R&>
template<typename _Res>
class shared_future<_Res&> : public __basic_future<_Res&>
{
typedef __basic_future<_Res&> _Base_type;
public:
constexpr shared_future() noexcept : _Base_type() { }
/// Copy constructor
shared_future(const shared_future& __sf) : _Base_type(__sf) { }
/// Construct from a future rvalue
shared_future(future<_Res&>&& __uf) noexcept
: _Base_type(std::move(__uf))
{ }
/// Construct from a shared_future rvalue
shared_future(shared_future&& __sf) noexcept
: _Base_type(std::move(__sf))
{ }
shared_future& operator=(const shared_future& __sf)
{
shared_future(__sf)._M_swap(*this);
return *this;
}
shared_future& operator=(shared_future&& __sf) noexcept
{
shared_future(std::move(__sf))._M_swap(*this);
return *this;
}
/// Retrieving the value
_Res&
get() const { return this->_M_get_result()._M_get(); }
};
/// Explicit specialization for shared_future<void>
template<>
class shared_future<void> : public __basic_future<void>
{
typedef __basic_future<void> _Base_type;
public:
constexpr shared_future() noexcept : _Base_type() { }
/// Copy constructor
shared_future(const shared_future& __sf) : _Base_type(__sf) { }
/// Construct from a future rvalue
shared_future(future<void>&& __uf) noexcept
: _Base_type(std::move(__uf))
{ }
/// Construct from a shared_future rvalue
shared_future(shared_future&& __sf) noexcept
: _Base_type(std::move(__sf))
{ }
shared_future& operator=(const shared_future& __sf)
{
shared_future(__sf)._M_swap(*this);
return *this;
}
shared_future& operator=(shared_future&& __sf) noexcept
{
shared_future(std::move(__sf))._M_swap(*this);
return *this;
}
// Retrieving the value
void
get() const { this->_M_get_result(); }
};
// Now we can define the protected __basic_future constructors.
template<typename _Res>
inline __basic_future<_Res>::
__basic_future(const shared_future<_Res>& __sf) noexcept
: _M_state(__sf._M_state)
{ }
template<typename _Res>
inline __basic_future<_Res>::
__basic_future(shared_future<_Res>&& __sf) noexcept
: _M_state(std::move(__sf._M_state))
{ }
template<typename _Res>
inline __basic_future<_Res>::
__basic_future(future<_Res>&& __uf) noexcept
: _M_state(std::move(__uf._M_state))
{ }
template<typename _Res>
inline shared_future<_Res>
future<_Res>::share()
{ return shared_future<_Res>(std::move(*this)); }
template<typename _Res>
inline shared_future<_Res&>
future<_Res&>::share()
{ return shared_future<_Res&>(std::move(*this)); }
inline shared_future<void>
future<void>::share()
{ return shared_future<void>(std::move(*this)); }
/// Primary template for promise
template<typename _Res>
class promise
{
typedef __future_base::_State_base _State;
typedef __future_base::_Result<_Res> _Res_type;
typedef __future_base::_Ptr<_Res_type> _Ptr_type;
template<typename, typename> friend class _State::_Setter;
shared_ptr<_State> _M_future;
_Ptr_type _M_storage;
public:
promise()
: _M_future(std::make_shared<_State>()),
_M_storage(new _Res_type())
{ }
promise(promise&& __rhs) noexcept
: _M_future(std::move(__rhs._M_future)),
_M_storage(std::move(__rhs._M_storage))
{ }
template<typename _Allocator>
promise(allocator_arg_t, const _Allocator& __a)
: _M_future(std::allocate_shared<_State>(__a)),
_M_storage(__future_base::_S_allocate_result<_Res>(__a))
{ }
template<typename _Allocator>
promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
: _M_future(std::move(__rhs._M_future)),
_M_storage(std::move(__rhs._M_storage))
{ }
promise(const promise&) = delete;
~promise()
{
if (static_cast<bool>(_M_future) && !_M_future.unique())
_M_future->_M_break_promise(std::move(_M_storage));
}
// Assignment
promise&
operator=(promise&& __rhs) noexcept
{
promise(std::move(__rhs)).swap(*this);
return *this;
}
promise& operator=(const promise&) = delete;
void
swap(promise& __rhs) noexcept
{
_M_future.swap(__rhs._M_future);
_M_storage.swap(__rhs._M_storage);
}
// Retrieving the result
future<_Res>
get_future()
{ return future<_Res>(_M_future); }
// Setting the result
void
set_value(const _Res& __r)
{ _M_future->_M_set_result(_State::__setter(this, __r)); }
void
set_value(_Res&& __r)
{ _M_future->_M_set_result(_State::__setter(this, std::move(__r))); }
void
set_exception(exception_ptr __p)
{ _M_future->_M_set_result(_State::__setter(__p, this)); }
void
set_value_at_thread_exit(const _Res& __r)
{
_M_future->_M_set_delayed_result(_State::__setter(this, __r),
_M_future);
}
void
set_value_at_thread_exit(_Res&& __r)
{
_M_future->_M_set_delayed_result(
_State::__setter(this, std::move(__r)), _M_future);
}
void
set_exception_at_thread_exit(exception_ptr __p)
{
_M_future->_M_set_delayed_result(_State::__setter(__p, this),
_M_future);
}
};
template<typename _Res>
inline void
swap(promise<_Res>& __x, promise<_Res>& __y) noexcept
{ __x.swap(__y); }
template<typename _Res, typename _Alloc>
struct uses_allocator<promise<_Res>, _Alloc>
: public true_type { };
/// Partial specialization for promise<R&>
template<typename _Res>
class promise<_Res&>
{
typedef __future_base::_State_base _State;
typedef __future_base::_Result<_Res&> _Res_type;
typedef __future_base::_Ptr<_Res_type> _Ptr_type;
template<typename, typename> friend class _State::_Setter;
shared_ptr<_State> _M_future;
_Ptr_type _M_storage;
public:
promise()
: _M_future(std::make_shared<_State>()),
_M_storage(new _Res_type())
{ }
promise(promise&& __rhs) noexcept
: _M_future(std::move(__rhs._M_future)),
_M_storage(std::move(__rhs._M_storage))
{ }
template<typename _Allocator>
promise(allocator_arg_t, const _Allocator& __a)
: _M_future(std::allocate_shared<_State>(__a)),
_M_storage(__future_base::_S_allocate_result<_Res&>(__a))
{ }
template<typename _Allocator>
promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
: _M_future(std::move(__rhs._M_future)),
_M_storage(std::move(__rhs._M_storage))
{ }
promise(const promise&) = delete;
~promise()
{
if (static_cast<bool>(_M_future) && !_M_future.unique())
_M_future->_M_break_promise(std::move(_M_storage));
}
// Assignment
promise&
operator=(promise&& __rhs) noexcept
{
promise(std::move(__rhs)).swap(*this);
return *this;
}
promise& operator=(const promise&) = delete;
void
swap(promise& __rhs) noexcept
{
_M_future.swap(__rhs._M_future);
_M_storage.swap(__rhs._M_storage);
}
// Retrieving the result
future<_Res&>
get_future()
{ return future<_Res&>(_M_future); }
// Setting the result
void
set_value(_Res& __r)
{ _M_future->_M_set_result(_State::__setter(this, __r)); }
void
set_exception(exception_ptr __p)
{ _M_future->_M_set_result(_State::__setter(__p, this)); }
void
set_value_at_thread_exit(_Res& __r)
{
_M_future->_M_set_delayed_result(_State::__setter(this, __r),
_M_future);
}
void
set_exception_at_thread_exit(exception_ptr __p)
{
_M_future->_M_set_delayed_result(_State::__setter(__p, this),
_M_future);
}
};
/// Explicit specialization for promise<void>
template<>
class promise<void>
{
typedef __future_base::_State_base _State;
typedef __future_base::_Result<void> _Res_type;
typedef __future_base::_Ptr<_Res_type> _Ptr_type;
template<typename, typename> friend class _State::_Setter;
shared_ptr<_State> _M_future;
_Ptr_type _M_storage;
public:
promise()
: _M_future(std::make_shared<_State>()),
_M_storage(new _Res_type())
{ }
promise(promise&& __rhs) noexcept
: _M_future(std::move(__rhs._M_future)),
_M_storage(std::move(__rhs._M_storage))
{ }
template<typename _Allocator>
promise(allocator_arg_t, const _Allocator& __a)
: _M_future(std::allocate_shared<_State>(__a)),
_M_storage(__future_base::_S_allocate_result<void>(__a))
{ }
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 2095. missing constructors needed for uses-allocator construction
template<typename _Allocator>
promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
: _M_future(std::move(__rhs._M_future)),
_M_storage(std::move(__rhs._M_storage))
{ }
promise(const promise&) = delete;
~promise()
{
if (static_cast<bool>(_M_future) && !_M_future.unique())
_M_future->_M_break_promise(std::move(_M_storage));
}
// Assignment
promise&
operator=(promise&& __rhs) noexcept
{
promise(std::move(__rhs)).swap(*this);
return *this;
}
promise& operator=(const promise&) = delete;
void
swap(promise& __rhs) noexcept
{
_M_future.swap(__rhs._M_future);
_M_storage.swap(__rhs._M_storage);
}
// Retrieving the result
future<void>
get_future()
{ return future<void>(_M_future); }
// Setting the result
void set_value();
void
set_exception(exception_ptr __p)
{ _M_future->_M_set_result(_State::__setter(__p, this)); }
void
set_value_at_thread_exit();
void
set_exception_at_thread_exit(exception_ptr __p)
{
_M_future->_M_set_delayed_result(_State::__setter(__p, this),
_M_future);
}
};
// set void
template<>
struct __future_base::_State_base::_Setter<void, void>
{
promise<void>::_Ptr_type operator()() const
{
_State_base::_S_check(_M_promise->_M_future);
return std::move(_M_promise->_M_storage);
}
promise<void>* _M_promise;
};
inline void
promise<void>::set_value()
{ _M_future->_M_set_result(_State::_Setter<void, void>{ this }); }
inline void
promise<void>::set_value_at_thread_exit()
{
_M_future->_M_set_delayed_result(_State::_Setter<void, void>{this},
_M_future);
}
template<typename _Ptr_type, typename _Fn, typename _Res>
struct __future_base::_Task_setter
{
// Invoke the function and provide the result to the caller.
_Ptr_type operator()() const
{
__try
{
(*_M_result)->_M_set((*_M_fn)());
}
__catch(const __cxxabiv1::__forced_unwind&)
{
__throw_exception_again; // will cause broken_promise
}
__catch(...)
{
(*_M_result)->_M_error = current_exception();
}
return std::move(*_M_result);
}
_Ptr_type* _M_result;
_Fn* _M_fn;
};
template<typename _Ptr_type, typename _Fn>
struct __future_base::_Task_setter<_Ptr_type, _Fn, void>
{
_Ptr_type operator()() const
{
__try
{
(*_M_fn)();
}
__catch(const __cxxabiv1::__forced_unwind&)
{
__throw_exception_again; // will cause broken_promise
}
__catch(...)
{
(*_M_result)->_M_error = current_exception();
}
return std::move(*_M_result);
}
_Ptr_type* _M_result;
_Fn* _M_fn;
};
// Holds storage for a packaged_task's result.
template<typename _Res, typename... _Args>
struct __future_base::_Task_state_base<_Res(_Args...)>
: __future_base::_State_base
{
typedef _Res _Res_type;
template<typename _Alloc>
_Task_state_base(const _Alloc& __a)
: _M_result(_S_allocate_result<_Res>(__a))
{ }
// Invoke the stored task and make the state ready.
virtual void
_M_run(_Args&&... __args) = 0;
// Invoke the stored task and make the state ready at thread exit.
virtual void
_M_run_delayed(_Args&&... __args, weak_ptr<_State_base>) = 0;
virtual shared_ptr<_Task_state_base>
_M_reset() = 0;
typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
_Ptr_type _M_result;
};
// Holds a packaged_task's stored task.
template<typename _Fn, typename _Alloc, typename _Res, typename... _Args>
struct __future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)> final
: __future_base::_Task_state_base<_Res(_Args...)>
{
template<typename _Fn2>
_Task_state(_Fn2&& __fn, const _Alloc& __a)
: _Task_state_base<_Res(_Args...)>(__a),
_M_impl(std::forward<_Fn2>(__fn), __a)
{ }
private:
virtual void
_M_run(_Args&&... __args)
{
// bound arguments decay so wrap lvalue references
auto __boundfn = std::__bind_simple(std::ref(_M_impl._M_fn),
_S_maybe_wrap_ref(std::forward<_Args>(__args))...);
this->_M_set_result(_S_task_setter(this->_M_result, __boundfn));
}
virtual void
_M_run_delayed(_Args&&... __args, weak_ptr<_State_base> __self)
{
// bound arguments decay so wrap lvalue references
auto __boundfn = std::__bind_simple(std::ref(_M_impl._M_fn),
_S_maybe_wrap_ref(std::forward<_Args>(__args))...);
this->_M_set_delayed_result(_S_task_setter(this->_M_result, __boundfn),
std::move(__self));
}
virtual shared_ptr<_Task_state_base<_Res(_Args...)>>
_M_reset();
template<typename _Tp>
static reference_wrapper<_Tp>
_S_maybe_wrap_ref(_Tp& __t)
{ return std::ref(__t); }
template<typename _Tp>
static
typename enable_if<!is_lvalue_reference<_Tp>::value, _Tp>::type&&
_S_maybe_wrap_ref(_Tp&& __t)
{ return std::forward<_Tp>(__t); }
struct _Impl : _Alloc
{
template<typename _Fn2>
_Impl(_Fn2&& __fn, const _Alloc& __a)
: _Alloc(__a), _M_fn(std::forward<_Fn2>(__fn)) { }
_Fn _M_fn;
} _M_impl;
};
template<typename _Signature, typename _Fn, typename _Alloc>
static shared_ptr<__future_base::_Task_state_base<_Signature>>
__create_task_state(_Fn&& __fn, const _Alloc& __a)
{
typedef typename decay<_Fn>::type _Fn2;
typedef __future_base::_Task_state<_Fn2, _Alloc, _Signature> _State;
return std::allocate_shared<_State>(__a, std::forward<_Fn>(__fn), __a);
}
template<typename _Fn, typename _Alloc, typename _Res, typename... _Args>
shared_ptr<__future_base::_Task_state_base<_Res(_Args...)>>
__future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)>::_M_reset()
{
return __create_task_state<_Res(_Args...)>(std::move(_M_impl._M_fn),
static_cast<_Alloc&>(_M_impl));
}
template<typename _Task, typename _Fn, bool
= is_same<_Task, typename decay<_Fn>::type>::value>
struct __constrain_pkgdtask
{ typedef void __type; };
template<typename _Task, typename _Fn>
struct __constrain_pkgdtask<_Task, _Fn, true>
{ };
/// packaged_task
template<typename _Res, typename... _ArgTypes>
class packaged_task<_Res(_ArgTypes...)>
{
typedef __future_base::_Task_state_base<_Res(_ArgTypes...)> _State_type;
shared_ptr<_State_type> _M_state;
public:
// Construction and destruction
packaged_task() noexcept { }
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 2095. missing constructors needed for uses-allocator construction
template<typename _Allocator>
packaged_task(allocator_arg_t, const _Allocator& __a) noexcept
{ }
template<typename _Fn, typename = typename
__constrain_pkgdtask<packaged_task, _Fn>::__type>
explicit
packaged_task(_Fn&& __fn)
: packaged_task(allocator_arg, std::allocator<int>(),
std::forward<_Fn>(__fn))
{ }
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 2097. packaged_task constructors should be constrained
// 2407. [this constructor should not be] explicit
template<typename _Fn, typename _Alloc, typename = typename
__constrain_pkgdtask<packaged_task, _Fn>::__type>
packaged_task(allocator_arg_t, const _Alloc& __a, _Fn&& __fn)
: _M_state(__create_task_state<_Res(_ArgTypes...)>(
std::forward<_Fn>(__fn), __a))
{ }
~packaged_task()
{
if (static_cast<bool>(_M_state) && !_M_state.unique())
_M_state->_M_break_promise(std::move(_M_state->_M_result));
}
// No copy
packaged_task(const packaged_task&) = delete;
packaged_task& operator=(const packaged_task&) = delete;
template<typename _Allocator>
packaged_task(allocator_arg_t, const _Allocator&,
const packaged_task&) = delete;
// Move support
packaged_task(packaged_task&& __other) noexcept
{ this->swap(__other); }
template<typename _Allocator>
packaged_task(allocator_arg_t, const _Allocator&,
packaged_task&& __other) noexcept
{ this->swap(__other); }
packaged_task& operator=(packaged_task&& __other) noexcept
{
packaged_task(std::move(__other)).swap(*this);
return *this;
}
void
swap(packaged_task& __other) noexcept
{ _M_state.swap(__other._M_state); }
bool
valid() const noexcept
{ return static_cast<bool>(_M_state); }
// Result retrieval
future<_Res>
get_future()
{ return future<_Res>(_M_state); }
// Execution
void
operator()(_ArgTypes... __args)
{
__future_base::_State_base::_S_check(_M_state);
_M_state->_M_run(std::forward<_ArgTypes>(__args)...);
}
void
make_ready_at_thread_exit(_ArgTypes... __args)
{
__future_base::_State_base::_S_check(_M_state);
_M_state->_M_run_delayed(std::forward<_ArgTypes>(__args)..., _M_state);
}
void
reset()
{
__future_base::_State_base::_S_check(_M_state);
packaged_task __tmp;
__tmp._M_state = _M_state;
_M_state = _M_state->_M_reset();
}
};
/// swap
template<typename _Res, typename... _ArgTypes>
inline void
swap(packaged_task<_Res(_ArgTypes...)>& __x,
packaged_task<_Res(_ArgTypes...)>& __y) noexcept
{ __x.swap(__y); }
template<typename _Res, typename _Alloc>
struct uses_allocator<packaged_task<_Res>, _Alloc>
: public true_type { };
// Shared state created by std::async().
// Holds a deferred function and storage for its result.
template<typename _BoundFn, typename _Res>
class __future_base::_Deferred_state final
: public __future_base::_State_base
{
public:
explicit
_Deferred_state(_BoundFn&& __fn)
: _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
{ }
private:
typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
_Ptr_type _M_result;
_BoundFn _M_fn;
// Run the deferred function.
virtual void
_M_complete_async()
{
// Multiple threads can call a waiting function on the future and
// reach this point at the same time. The call_once in _M_set_result
// ensures only the first one run the deferred function, stores the
// result in _M_result, swaps that with the base _M_result and makes
// the state ready. Tell _M_set_result to ignore failure so all later
// calls do nothing.
_M_set_result(_S_task_setter(_M_result, _M_fn), true);
}
// Caller should check whether the state is ready first, because this
// function will return true even after the deferred function has run.
virtual bool _M_is_deferred_future() const { return true; }
};
// Common functionality hoisted out of the _Async_state_impl template.
class __future_base::_Async_state_commonV2
: public __future_base::_State_base
{
protected:
~_Async_state_commonV2() = default;
// Make waiting functions block until the thread completes, as if joined.
//
// This function is used by wait() to satisfy the first requirement below
// and by wait_for() / wait_until() to satisfy the second.
//
// [futures.async]:
//
// — a call to a waiting function on an asynchronous return object that
// shares the shared state created by this async call shall block until
// the associated thread has completed, as if joined, or else time out.
//
// — the associated thread completion synchronizes with the return from
// the first function that successfully detects the ready status of the
// shared state or with the return from the last function that releases
// the shared state, whichever happens first.
virtual void _M_complete_async() { _M_join(); }
void _M_join() { std::call_once(_M_once, &thread::join, ref(_M_thread)); }
thread _M_thread;
once_flag _M_once;
};
// Shared state created by std::async().
// Starts a new thread that runs a function and makes the shared state ready.
template<typename _BoundFn, typename _Res>
class __future_base::_Async_state_impl final
: public __future_base::_Async_state_commonV2
{
public:
explicit
_Async_state_impl(_BoundFn&& __fn)
: _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
{
_M_thread = std::thread{ [this] {
__try
{
_M_set_result(_S_task_setter(_M_result, _M_fn));
}
__catch (const __cxxabiv1::__forced_unwind&)
{
// make the shared state ready on thread cancellation
if (static_cast<bool>(_M_result))
this->_M_break_promise(std::move(_M_result));
__throw_exception_again;
}
} };
}
// Must not destroy _M_result and _M_fn until the thread finishes.
// Call join() directly rather than through _M_join() because no other
// thread can be referring to this state if it is being destroyed.
~_Async_state_impl() { if (_M_thread.joinable()) _M_thread.join(); }
private:
typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
_Ptr_type _M_result;
_BoundFn _M_fn;
};
template<typename _BoundFn>
inline std::shared_ptr<__future_base::_State_base>
__future_base::_S_make_deferred_state(_BoundFn&& __fn)
{
typedef typename remove_reference<_BoundFn>::type __fn_type;
typedef _Deferred_state<__fn_type> __state_type;
return std::make_shared<__state_type>(std::move(__fn));
}
template<typename _BoundFn>
inline std::shared_ptr<__future_base::_State_base>
__future_base::_S_make_async_state(_BoundFn&& __fn)
{
typedef typename remove_reference<_BoundFn>::type __fn_type;
typedef _Async_state_impl<__fn_type> __state_type;
return std::make_shared<__state_type>(std::move(__fn));
}
/// async
template<typename _Fn, typename... _Args>
future<typename result_of<_Fn(_Args...)>::type>
async(launch __policy, _Fn&& __fn, _Args&&... __args)
{
typedef typename result_of<_Fn(_Args...)>::type result_type;
std::shared_ptr<__future_base::_State_base> __state;
if ((__policy & launch::async) == launch::async)
{
__state = __future_base::_S_make_async_state(std::__bind_simple(
std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
}
else
{
__state = __future_base::_S_make_deferred_state(std::__bind_simple(
std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
}
return future<result_type>(__state);
}
/// async, potential overload
template<typename _Fn, typename... _Args>
inline future<typename result_of<_Fn(_Args...)>::type>
async(_Fn&& __fn, _Args&&... __args)
{
return async(launch::async|launch::deferred, std::forward<_Fn>(__fn),
std::forward<_Args>(__args)...);
}
#endif // _GLIBCXX_ASYNC_ABI_COMPAT
#endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
// && ATOMIC_INT_LOCK_FREE
// @} group futures
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace
#endif // C++11
#endif // _GLIBCXX_FUTURE
|