Coverage Report

Created: 2024-09-08 06:18

/src/llvm-project-16.0.6.build/include/c++/v1/future
Line
Count
Source (jump to first uncovered line)
1
// -*- C++ -*-
2
//===----------------------------------------------------------------------===//
3
//
4
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5
// See https://llvm.org/LICENSE.txt for license information.
6
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7
//
8
//===----------------------------------------------------------------------===//
9
10
#ifndef _LIBCPP_FUTURE
11
#define _LIBCPP_FUTURE
12
13
/*
14
    future synopsis
15
16
namespace std
17
{
18
19
enum class future_errc
20
{
21
    future_already_retrieved = 1,
22
    promise_already_satisfied,
23
    no_state,
24
    broken_promise
25
};
26
27
enum class launch
28
{
29
    async = 1,
30
    deferred = 2,
31
    any = async | deferred
32
};
33
34
enum class future_status
35
{
36
    ready,
37
    timeout,
38
    deferred
39
};
40
41
template <> struct is_error_code_enum<future_errc> : public true_type { };
42
error_code make_error_code(future_errc e) noexcept;
43
error_condition make_error_condition(future_errc e) noexcept;
44
45
const error_category& future_category() noexcept;
46
47
class future_error
48
    : public logic_error
49
{
50
public:
51
    future_error(error_code ec);  // exposition only
52
    explicit future_error(future_errc); // C++17
53
    const error_code& code() const noexcept;
54
    const char*       what() const noexcept;
55
};
56
57
template <class R>
58
class promise
59
{
60
public:
61
    promise();
62
    template <class Allocator>
63
        promise(allocator_arg_t, const Allocator& a);
64
    promise(promise&& rhs) noexcept;
65
    promise(const promise& rhs) = delete;
66
    ~promise();
67
68
    // assignment
69
    promise& operator=(promise&& rhs) noexcept;
70
    promise& operator=(const promise& rhs) = delete;
71
    void swap(promise& other) noexcept;
72
73
    // retrieving the result
74
    future<R> get_future();
75
76
    // setting the result
77
    void set_value(const R& r);
78
    void set_value(R&& r);
79
    void set_exception(exception_ptr p);
80
81
    // setting the result with deferred notification
82
    void set_value_at_thread_exit(const R& r);
83
    void set_value_at_thread_exit(R&& r);
84
    void set_exception_at_thread_exit(exception_ptr p);
85
};
86
87
template <class R>
88
class promise<R&>
89
{
90
public:
91
    promise();
92
    template <class Allocator>
93
        promise(allocator_arg_t, const Allocator& a);
94
    promise(promise&& rhs) noexcept;
95
    promise(const promise& rhs) = delete;
96
    ~promise();
97
98
    // assignment
99
    promise& operator=(promise&& rhs) noexcept;
100
    promise& operator=(const promise& rhs) = delete;
101
    void swap(promise& other) noexcept;
102
103
    // retrieving the result
104
    future<R&> get_future();
105
106
    // setting the result
107
    void set_value(R& r);
108
    void set_exception(exception_ptr p);
109
110
    // setting the result with deferred notification
111
    void set_value_at_thread_exit(R&);
112
    void set_exception_at_thread_exit(exception_ptr p);
113
};
114
115
template <>
116
class promise<void>
117
{
118
public:
119
    promise();
120
    template <class Allocator>
121
        promise(allocator_arg_t, const Allocator& a);
122
    promise(promise&& rhs) noexcept;
123
    promise(const promise& rhs) = delete;
124
    ~promise();
125
126
    // assignment
127
    promise& operator=(promise&& rhs) noexcept;
128
    promise& operator=(const promise& rhs) = delete;
129
    void swap(promise& other) noexcept;
130
131
    // retrieving the result
132
    future<void> get_future();
133
134
    // setting the result
135
    void set_value();
136
    void set_exception(exception_ptr p);
137
138
    // setting the result with deferred notification
139
    void set_value_at_thread_exit();
140
    void set_exception_at_thread_exit(exception_ptr p);
141
};
142
143
template <class R> void swap(promise<R>& x, promise<R>& y) noexcept;
144
145
template <class R, class Alloc>
146
    struct uses_allocator<promise<R>, Alloc> : public true_type {};
147
148
template <class R>
149
class future
150
{
151
public:
152
    future() noexcept;
153
    future(future&&) noexcept;
154
    future(const future& rhs) = delete;
155
    ~future();
156
    future& operator=(const future& rhs) = delete;
157
    future& operator=(future&&) noexcept;
158
    shared_future<R> share() noexcept;
159
160
    // retrieving the value
161
    R get();
162
163
    // functions to check state
164
    bool valid() const noexcept;
165
166
    void wait() const;
167
    template <class Rep, class Period>
168
        future_status
169
        wait_for(const chrono::duration<Rep, Period>& rel_time) const;
170
    template <class Clock, class Duration>
171
        future_status
172
        wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
173
};
174
175
template <class R>
176
class future<R&>
177
{
178
public:
179
    future() noexcept;
180
    future(future&&) noexcept;
181
    future(const future& rhs) = delete;
182
    ~future();
183
    future& operator=(const future& rhs) = delete;
184
    future& operator=(future&&) noexcept;
185
    shared_future<R&> share() noexcept;
186
187
    // retrieving the value
188
    R& get();
189
190
    // functions to check state
191
    bool valid() const noexcept;
192
193
    void wait() const;
194
    template <class Rep, class Period>
195
        future_status
196
        wait_for(const chrono::duration<Rep, Period>& rel_time) const;
197
    template <class Clock, class Duration>
198
        future_status
199
        wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
200
};
201
202
template <>
203
class future<void>
204
{
205
public:
206
    future() noexcept;
207
    future(future&&) noexcept;
208
    future(const future& rhs) = delete;
209
    ~future();
210
    future& operator=(const future& rhs) = delete;
211
    future& operator=(future&&) noexcept;
212
    shared_future<void> share() noexcept;
213
214
    // retrieving the value
215
    void get();
216
217
    // functions to check state
218
    bool valid() const noexcept;
219
220
    void wait() const;
221
    template <class Rep, class Period>
222
        future_status
223
        wait_for(const chrono::duration<Rep, Period>& rel_time) const;
224
    template <class Clock, class Duration>
225
        future_status
226
        wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
227
};
228
229
template <class R>
230
class shared_future
231
{
232
public:
233
    shared_future() noexcept;
234
    shared_future(const shared_future& rhs);
235
    shared_future(future<R>&&) noexcept;
236
    shared_future(shared_future&& rhs) noexcept;
237
    ~shared_future();
238
    shared_future& operator=(const shared_future& rhs);
239
    shared_future& operator=(shared_future&& rhs) noexcept;
240
241
    // retrieving the value
242
    const R& get() const;
243
244
    // functions to check state
245
    bool valid() const noexcept;
246
247
    void wait() const;
248
    template <class Rep, class Period>
249
        future_status
250
        wait_for(const chrono::duration<Rep, Period>& rel_time) const;
251
    template <class Clock, class Duration>
252
        future_status
253
        wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
254
};
255
256
template <class R>
257
class shared_future<R&>
258
{
259
public:
260
    shared_future() noexcept;
261
    shared_future(const shared_future& rhs);
262
    shared_future(future<R&>&&) noexcept;
263
    shared_future(shared_future&& rhs) noexcept;
264
    ~shared_future();
265
    shared_future& operator=(const shared_future& rhs);
266
    shared_future& operator=(shared_future&& rhs) noexcept;
267
268
    // retrieving the value
269
    R& get() const;
270
271
    // functions to check state
272
    bool valid() const noexcept;
273
274
    void wait() const;
275
    template <class Rep, class Period>
276
        future_status
277
        wait_for(const chrono::duration<Rep, Period>& rel_time) const;
278
    template <class Clock, class Duration>
279
        future_status
280
        wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
281
};
282
283
template <>
284
class shared_future<void>
285
{
286
public:
287
    shared_future() noexcept;
288
    shared_future(const shared_future& rhs);
289
    shared_future(future<void>&&) noexcept;
290
    shared_future(shared_future&& rhs) noexcept;
291
    ~shared_future();
292
    shared_future& operator=(const shared_future& rhs);
293
    shared_future& operator=(shared_future&& rhs) noexcept;
294
295
    // retrieving the value
296
    void get() const;
297
298
    // functions to check state
299
    bool valid() const noexcept;
300
301
    void wait() const;
302
    template <class Rep, class Period>
303
        future_status
304
        wait_for(const chrono::duration<Rep, Period>& rel_time) const;
305
    template <class Clock, class Duration>
306
        future_status
307
        wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
308
};
309
310
template <class F, class... Args>
311
  future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
312
  async(F&& f, Args&&... args);
313
314
template <class F, class... Args>
315
  future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
316
  async(launch policy, F&& f, Args&&... args);
317
318
template <class> class packaged_task; // undefined
319
320
template <class R, class... ArgTypes>
321
class packaged_task<R(ArgTypes...)>
322
{
323
public:
324
    typedef R result_type; // extension
325
326
    // construction and destruction
327
    packaged_task() noexcept;
328
    template <class F>
329
        explicit packaged_task(F&& f);
330
    template <class F, class Allocator>
331
        packaged_task(allocator_arg_t, const Allocator& a, F&& f);
332
    ~packaged_task();
333
334
    // no copy
335
    packaged_task(const packaged_task&) = delete;
336
    packaged_task& operator=(const packaged_task&) = delete;
337
338
    // move support
339
    packaged_task(packaged_task&& other) noexcept;
340
    packaged_task& operator=(packaged_task&& other) noexcept;
341
    void swap(packaged_task& other) noexcept;
342
343
    bool valid() const noexcept;
344
345
    // result retrieval
346
    future<R> get_future();
347
348
    // execution
349
    void operator()(ArgTypes... );
350
    void make_ready_at_thread_exit(ArgTypes...);
351
352
    void reset();
353
};
354
355
template <class R>
356
  void swap(packaged_task<R(ArgTypes...)&, packaged_task<R(ArgTypes...)>&) noexcept;
357
358
template <class R, class Alloc> struct uses_allocator<packaged_task<R>, Alloc>;
359
360
}  // std
361
362
*/
363
364
#include <__assert> // all public C++ headers provide the assertion handler
365
#include <__availability>
366
#include <__chrono/duration.h>
367
#include <__chrono/time_point.h>
368
#include <__config>
369
#include <__memory/allocator_arg_t.h>
370
#include <__memory/allocator_destructor.h>
371
#include <__memory/uses_allocator.h>
372
#include <__type_traits/strip_signature.h>
373
#include <__utility/auto_cast.h>
374
#include <__utility/forward.h>
375
#include <__utility/move.h>
376
#include <exception>
377
#include <mutex>
378
#include <new>
379
#include <system_error>
380
#include <thread>
381
#include <version>
382
383
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
384
#  pragma GCC system_header
385
#endif
386
387
#ifdef _LIBCPP_HAS_NO_THREADS
388
# error "<future> is not supported since libc++ has been configured without support for threads."
389
#endif
390
391
_LIBCPP_BEGIN_NAMESPACE_STD
392
393
//enum class future_errc
394
_LIBCPP_DECLARE_STRONG_ENUM(future_errc)
395
{
396
    future_already_retrieved = 1,
397
    promise_already_satisfied,
398
    no_state,
399
    broken_promise
400
};
401
_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_errc)
402
403
template <>
404
struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<future_errc> : public true_type {};
405
406
#ifdef _LIBCPP_CXX03_LANG
407
template <>
408
struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<future_errc::__lx> : public true_type { };
409
#endif
410
411
//enum class launch
412
_LIBCPP_DECLARE_STRONG_ENUM(launch)
413
{
414
    async = 1,
415
    deferred = 2,
416
    any = async | deferred
417
};
418
_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(launch)
419
420
#ifndef _LIBCPP_CXX03_LANG
421
422
typedef underlying_type<launch>::type __launch_underlying_type;
423
424
inline _LIBCPP_INLINE_VISIBILITY
425
_LIBCPP_CONSTEXPR
426
launch
427
operator&(launch __x, launch __y)
428
0
{
429
0
    return static_cast<launch>(static_cast<__launch_underlying_type>(__x) &
430
0
                               static_cast<__launch_underlying_type>(__y));
431
0
}
432
433
inline _LIBCPP_INLINE_VISIBILITY
434
_LIBCPP_CONSTEXPR
435
launch
436
operator|(launch __x, launch __y)
437
0
{
438
0
    return static_cast<launch>(static_cast<__launch_underlying_type>(__x) |
439
0
                               static_cast<__launch_underlying_type>(__y));
440
0
}
441
442
inline _LIBCPP_INLINE_VISIBILITY
443
_LIBCPP_CONSTEXPR
444
launch
445
operator^(launch __x, launch __y)
446
0
{
447
0
    return static_cast<launch>(static_cast<__launch_underlying_type>(__x) ^
448
0
                               static_cast<__launch_underlying_type>(__y));
449
0
}
450
451
inline _LIBCPP_INLINE_VISIBILITY
452
_LIBCPP_CONSTEXPR
453
launch
454
operator~(launch __x)
455
0
{
456
0
    return static_cast<launch>(~static_cast<__launch_underlying_type>(__x) & 3);
457
0
}
458
459
inline _LIBCPP_INLINE_VISIBILITY
460
launch&
461
operator&=(launch& __x, launch __y)
462
0
{
463
0
    __x = __x & __y; return __x;
464
0
}
465
466
inline _LIBCPP_INLINE_VISIBILITY
467
launch&
468
operator|=(launch& __x, launch __y)
469
0
{
470
0
    __x = __x | __y; return __x;
471
0
}
472
473
inline _LIBCPP_INLINE_VISIBILITY
474
launch&
475
operator^=(launch& __x, launch __y)
476
0
{
477
0
    __x = __x ^ __y; return __x;
478
0
}
479
480
#endif // !_LIBCPP_CXX03_LANG
481
482
//enum class future_status
483
_LIBCPP_DECLARE_STRONG_ENUM(future_status)
484
{
485
    ready,
486
    timeout,
487
    deferred
488
};
489
_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_status)
490
491
_LIBCPP_FUNC_VIS
492
const error_category& future_category() _NOEXCEPT;
493
494
inline _LIBCPP_INLINE_VISIBILITY
495
error_code
496
make_error_code(future_errc __e) _NOEXCEPT
497
0
{
498
0
    return error_code(static_cast<int>(__e), future_category());
499
0
}
500
501
inline _LIBCPP_INLINE_VISIBILITY
502
error_condition
503
make_error_condition(future_errc __e) _NOEXCEPT
504
0
{
505
0
    return error_condition(static_cast<int>(__e), future_category());
506
0
}
507
508
class _LIBCPP_EXCEPTION_ABI _LIBCPP_AVAILABILITY_FUTURE_ERROR future_error
509
    : public logic_error
510
{
511
    error_code __ec_;
512
public:
513
    future_error(error_code __ec);
514
515
    _LIBCPP_INLINE_VISIBILITY
516
0
    const error_code& code() const _NOEXCEPT {return __ec_;}
517
518
0
    future_error(const future_error&) _NOEXCEPT = default;
519
    ~future_error() _NOEXCEPT override;
520
};
521
522
_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
523
#ifndef _LIBCPP_NO_EXCEPTIONS
524
_LIBCPP_AVAILABILITY_FUTURE_ERROR
525
#endif
526
void __throw_future_error(future_errc __ev)
527
0
{
528
0
#ifndef _LIBCPP_NO_EXCEPTIONS
529
0
    throw future_error(make_error_code(__ev));
530
#else
531
    ((void)__ev);
532
    _VSTD::abort();
533
#endif
534
0
}
535
536
class _LIBCPP_TYPE_VIS _LIBCPP_AVAILABILITY_FUTURE __assoc_sub_state
537
    : public __shared_count
538
{
539
protected:
540
    exception_ptr __exception_;
541
    mutable mutex __mut_;
542
    mutable condition_variable __cv_;
543
    unsigned __state_;
544
545
    void __on_zero_shared() _NOEXCEPT override;
546
    void __sub_wait(unique_lock<mutex>& __lk);
547
public:
548
    enum
549
    {
550
        __constructed = 1,
551
        __future_attached = 2,
552
        ready = 4,
553
        deferred = 8
554
    };
555
556
    _LIBCPP_INLINE_VISIBILITY
557
0
    __assoc_sub_state() : __state_(0) {}
558
559
    _LIBCPP_INLINE_VISIBILITY
560
    bool __has_value() const
561
0
        {return (__state_ & __constructed) || (__exception_ != nullptr);}
562
563
    _LIBCPP_INLINE_VISIBILITY
564
0
    void __attach_future() {
565
0
        lock_guard<mutex> __lk(__mut_);
566
0
        bool __has_future_attached = (__state_ & __future_attached) != 0;
567
0
        if (__has_future_attached)
568
0
            __throw_future_error(future_errc::future_already_retrieved);
569
0
        this->__add_shared();
570
0
        __state_ |= __future_attached;
571
0
    }
572
573
    _LIBCPP_INLINE_VISIBILITY
574
0
    void __set_deferred() {__state_ |= deferred;}
575
576
    void __make_ready();
577
    _LIBCPP_INLINE_VISIBILITY
578
0
    bool __is_ready() const {return (__state_ & ready) != 0;}
579
580
    void set_value();
581
    void set_value_at_thread_exit();
582
583
    void set_exception(exception_ptr __p);
584
    void set_exception_at_thread_exit(exception_ptr __p);
585
586
    void copy();
587
588
    void wait();
589
    template <class _Rep, class _Period>
590
        future_status
591
        _LIBCPP_INLINE_VISIBILITY
592
        wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const;
593
    template <class _Clock, class _Duration>
594
        _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
595
        future_status
596
        wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const;
597
598
    virtual void __execute();
599
};
600
601
template <class _Clock, class _Duration>
602
future_status
603
__assoc_sub_state::wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
604
{
605
    unique_lock<mutex> __lk(__mut_);
606
    if (__state_ & deferred)
607
        return future_status::deferred;
608
    while (!(__state_ & ready) && _Clock::now() < __abs_time)
609
        __cv_.wait_until(__lk, __abs_time);
610
    if (__state_ & ready)
611
        return future_status::ready;
612
    return future_status::timeout;
613
}
614
615
template <class _Rep, class _Period>
616
inline
617
future_status
618
__assoc_sub_state::wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
619
{
620
    return wait_until(chrono::steady_clock::now() + __rel_time);
621
}
622
623
template <class _Rp>
624
class _LIBCPP_AVAILABILITY_FUTURE _LIBCPP_HIDDEN __assoc_state
625
    : public __assoc_sub_state
626
{
627
    typedef __assoc_sub_state base;
628
_LIBCPP_SUPPRESS_DEPRECATED_PUSH
629
    typedef typename aligned_storage<sizeof(_Rp), alignment_of<_Rp>::value>::type _Up;
630
_LIBCPP_SUPPRESS_DEPRECATED_POP
631
protected:
632
    _Up __value_;
633
634
    void __on_zero_shared() _NOEXCEPT override;
635
public:
636
637
    template <class _Arg>
638
    void set_value(_Arg&& __arg);
639
640
    template <class _Arg>
641
    void set_value_at_thread_exit(_Arg&& __arg);
642
643
    _Rp move();
644
    __add_lvalue_reference_t<_Rp> copy();
645
};
646
647
template <class _Rp>
648
void
649
__assoc_state<_Rp>::__on_zero_shared() _NOEXCEPT
650
{
651
    if (this->__state_ & base::__constructed)
652
        reinterpret_cast<_Rp*>(&__value_)->~_Rp();
653
    delete this;
654
}
655
656
template <class _Rp>
657
template <class _Arg>
658
_LIBCPP_AVAILABILITY_FUTURE
659
void
660
__assoc_state<_Rp>::set_value(_Arg&& __arg)
661
{
662
    unique_lock<mutex> __lk(this->__mut_);
663
    if (this->__has_value())
664
        __throw_future_error(future_errc::promise_already_satisfied);
665
    ::new ((void*)&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
666
    this->__state_ |= base::__constructed | base::ready;
667
    __cv_.notify_all();
668
}
669
670
template <class _Rp>
671
template <class _Arg>
672
void
673
__assoc_state<_Rp>::set_value_at_thread_exit(_Arg&& __arg)
674
{
675
    unique_lock<mutex> __lk(this->__mut_);
676
    if (this->__has_value())
677
        __throw_future_error(future_errc::promise_already_satisfied);
678
    ::new ((void*)&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
679
    this->__state_ |= base::__constructed;
680
    __thread_local_data()->__make_ready_at_thread_exit(this);
681
}
682
683
template <class _Rp>
684
_Rp
685
__assoc_state<_Rp>::move()
686
{
687
    unique_lock<mutex> __lk(this->__mut_);
688
    this->__sub_wait(__lk);
689
    if (this->__exception_ != nullptr)
690
        std::rethrow_exception(this->__exception_);
691
    return _VSTD::move(*reinterpret_cast<_Rp*>(&__value_));
692
}
693
694
template <class _Rp>
695
__add_lvalue_reference_t<_Rp>
696
__assoc_state<_Rp>::copy()
697
{
698
    unique_lock<mutex> __lk(this->__mut_);
699
    this->__sub_wait(__lk);
700
    if (this->__exception_ != nullptr)
701
        std::rethrow_exception(this->__exception_);
702
    return *reinterpret_cast<_Rp*>(&__value_);
703
}
704
705
template <class _Rp>
706
class _LIBCPP_AVAILABILITY_FUTURE __assoc_state<_Rp&>
707
    : public __assoc_sub_state
708
{
709
    typedef __assoc_sub_state base;
710
    typedef _Rp* _Up;
711
protected:
712
    _Up __value_;
713
714
    void __on_zero_shared() _NOEXCEPT override;
715
public:
716
717
    void set_value(_Rp& __arg);
718
    void set_value_at_thread_exit(_Rp& __arg);
719
720
    _Rp& copy();
721
};
722
723
template <class _Rp>
724
void
725
__assoc_state<_Rp&>::__on_zero_shared() _NOEXCEPT
726
{
727
    delete this;
728
}
729
730
template <class _Rp>
731
void
732
__assoc_state<_Rp&>::set_value(_Rp& __arg)
733
{
734
    unique_lock<mutex> __lk(this->__mut_);
735
    if (this->__has_value())
736
        __throw_future_error(future_errc::promise_already_satisfied);
737
    __value_ = _VSTD::addressof(__arg);
738
    this->__state_ |= base::__constructed | base::ready;
739
    __cv_.notify_all();
740
}
741
742
template <class _Rp>
743
void
744
__assoc_state<_Rp&>::set_value_at_thread_exit(_Rp& __arg)
745
{
746
    unique_lock<mutex> __lk(this->__mut_);
747
    if (this->__has_value())
748
        __throw_future_error(future_errc::promise_already_satisfied);
749
    __value_ = _VSTD::addressof(__arg);
750
    this->__state_ |= base::__constructed;
751
    __thread_local_data()->__make_ready_at_thread_exit(this);
752
}
753
754
template <class _Rp>
755
_Rp&
756
__assoc_state<_Rp&>::copy()
757
{
758
    unique_lock<mutex> __lk(this->__mut_);
759
    this->__sub_wait(__lk);
760
    if (this->__exception_ != nullptr)
761
        std::rethrow_exception(this->__exception_);
762
    return *__value_;
763
}
764
765
template <class _Rp, class _Alloc>
766
class _LIBCPP_AVAILABILITY_FUTURE __assoc_state_alloc
767
    : public __assoc_state<_Rp>
768
{
769
    typedef __assoc_state<_Rp> base;
770
    _Alloc __alloc_;
771
772
    virtual void __on_zero_shared() _NOEXCEPT;
773
public:
774
    _LIBCPP_INLINE_VISIBILITY
775
    explicit __assoc_state_alloc(const _Alloc& __a)
776
        : __alloc_(__a) {}
777
};
778
779
template <class _Rp, class _Alloc>
780
void
781
__assoc_state_alloc<_Rp, _Alloc>::__on_zero_shared() _NOEXCEPT
782
{
783
    if (this->__state_ & base::__constructed)
784
        reinterpret_cast<_Rp*>(_VSTD::addressof(this->__value_))->~_Rp();
785
    typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
786
    typedef allocator_traits<_Al> _ATraits;
787
    typedef pointer_traits<typename _ATraits::pointer> _PTraits;
788
    _Al __a(__alloc_);
789
    this->~__assoc_state_alloc();
790
    __a.deallocate(_PTraits::pointer_to(*this), 1);
791
}
792
793
template <class _Rp, class _Alloc>
794
class _LIBCPP_AVAILABILITY_FUTURE __assoc_state_alloc<_Rp&, _Alloc>
795
    : public __assoc_state<_Rp&>
796
{
797
    typedef __assoc_state<_Rp&> base;
798
    _Alloc __alloc_;
799
800
    virtual void __on_zero_shared() _NOEXCEPT;
801
public:
802
    _LIBCPP_INLINE_VISIBILITY
803
    explicit __assoc_state_alloc(const _Alloc& __a)
804
        : __alloc_(__a) {}
805
};
806
807
template <class _Rp, class _Alloc>
808
void
809
__assoc_state_alloc<_Rp&, _Alloc>::__on_zero_shared() _NOEXCEPT
810
{
811
    typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
812
    typedef allocator_traits<_Al> _ATraits;
813
    typedef pointer_traits<typename _ATraits::pointer> _PTraits;
814
    _Al __a(__alloc_);
815
    this->~__assoc_state_alloc();
816
    __a.deallocate(_PTraits::pointer_to(*this), 1);
817
}
818
819
template <class _Alloc>
820
class _LIBCPP_AVAILABILITY_FUTURE __assoc_sub_state_alloc
821
    : public __assoc_sub_state
822
{
823
    typedef __assoc_sub_state base;
824
    _Alloc __alloc_;
825
826
    void __on_zero_shared() _NOEXCEPT override;
827
public:
828
    _LIBCPP_INLINE_VISIBILITY
829
    explicit __assoc_sub_state_alloc(const _Alloc& __a)
830
        : __alloc_(__a) {}
831
};
832
833
template <class _Alloc>
834
void
835
__assoc_sub_state_alloc<_Alloc>::__on_zero_shared() _NOEXCEPT
836
{
837
    typedef typename __allocator_traits_rebind<_Alloc, __assoc_sub_state_alloc>::type _Al;
838
    typedef allocator_traits<_Al> _ATraits;
839
    typedef pointer_traits<typename _ATraits::pointer> _PTraits;
840
    _Al __a(__alloc_);
841
    this->~__assoc_sub_state_alloc();
842
    __a.deallocate(_PTraits::pointer_to(*this), 1);
843
}
844
845
template <class _Rp, class _Fp>
846
class _LIBCPP_AVAILABILITY_FUTURE __deferred_assoc_state
847
    : public __assoc_state<_Rp>
848
{
849
    typedef __assoc_state<_Rp> base;
850
851
    _Fp __func_;
852
853
public:
854
    _LIBCPP_INLINE_VISIBILITY
855
    explicit __deferred_assoc_state(_Fp&& __f);
856
857
    virtual void __execute();
858
};
859
860
template <class _Rp, class _Fp>
861
inline
862
__deferred_assoc_state<_Rp, _Fp>::__deferred_assoc_state(_Fp&& __f)
863
    : __func_(_VSTD::forward<_Fp>(__f))
864
{
865
    this->__set_deferred();
866
}
867
868
template <class _Rp, class _Fp>
869
void
870
__deferred_assoc_state<_Rp, _Fp>::__execute()
871
{
872
#ifndef _LIBCPP_NO_EXCEPTIONS
873
    try
874
    {
875
#endif // _LIBCPP_NO_EXCEPTIONS
876
        this->set_value(__func_());
877
#ifndef _LIBCPP_NO_EXCEPTIONS
878
    }
879
    catch (...)
880
    {
881
        this->set_exception(current_exception());
882
    }
883
#endif // _LIBCPP_NO_EXCEPTIONS
884
}
885
886
template <class _Fp>
887
class _LIBCPP_AVAILABILITY_FUTURE __deferred_assoc_state<void, _Fp>
888
    : public __assoc_sub_state
889
{
890
    typedef __assoc_sub_state base;
891
892
    _Fp __func_;
893
894
public:
895
    _LIBCPP_INLINE_VISIBILITY
896
    explicit __deferred_assoc_state(_Fp&& __f);
897
898
    void __execute() override;
899
};
900
901
template <class _Fp>
902
inline
903
__deferred_assoc_state<void, _Fp>::__deferred_assoc_state(_Fp&& __f)
904
    : __func_(_VSTD::forward<_Fp>(__f))
905
{
906
    this->__set_deferred();
907
}
908
909
template <class _Fp>
910
void
911
__deferred_assoc_state<void, _Fp>::__execute()
912
{
913
#ifndef _LIBCPP_NO_EXCEPTIONS
914
    try
915
    {
916
#endif // _LIBCPP_NO_EXCEPTIONS
917
        __func_();
918
        this->set_value();
919
#ifndef _LIBCPP_NO_EXCEPTIONS
920
    }
921
    catch (...)
922
    {
923
        this->set_exception(current_exception());
924
    }
925
#endif // _LIBCPP_NO_EXCEPTIONS
926
}
927
928
template <class _Rp, class _Fp>
929
class _LIBCPP_AVAILABILITY_FUTURE __async_assoc_state
930
    : public __assoc_state<_Rp>
931
{
932
    typedef __assoc_state<_Rp> base;
933
934
    _Fp __func_;
935
936
    virtual void __on_zero_shared() _NOEXCEPT;
937
public:
938
    _LIBCPP_INLINE_VISIBILITY
939
    explicit __async_assoc_state(_Fp&& __f);
940
941
    virtual void __execute();
942
};
943
944
template <class _Rp, class _Fp>
945
inline
946
__async_assoc_state<_Rp, _Fp>::__async_assoc_state(_Fp&& __f)
947
    : __func_(_VSTD::forward<_Fp>(__f))
948
{
949
}
950
951
template <class _Rp, class _Fp>
952
void
953
__async_assoc_state<_Rp, _Fp>::__execute()
954
{
955
#ifndef _LIBCPP_NO_EXCEPTIONS
956
    try
957
    {
958
#endif // _LIBCPP_NO_EXCEPTIONS
959
        this->set_value(__func_());
960
#ifndef _LIBCPP_NO_EXCEPTIONS
961
    }
962
    catch (...)
963
    {
964
        this->set_exception(current_exception());
965
    }
966
#endif // _LIBCPP_NO_EXCEPTIONS
967
}
968
969
template <class _Rp, class _Fp>
970
void
971
__async_assoc_state<_Rp, _Fp>::__on_zero_shared() _NOEXCEPT
972
{
973
    this->wait();
974
    base::__on_zero_shared();
975
}
976
977
template <class _Fp>
978
class _LIBCPP_AVAILABILITY_FUTURE __async_assoc_state<void, _Fp>
979
    : public __assoc_sub_state
980
{
981
    typedef __assoc_sub_state base;
982
983
    _Fp __func_;
984
985
    void __on_zero_shared() _NOEXCEPT override;
986
public:
987
    _LIBCPP_INLINE_VISIBILITY
988
    explicit __async_assoc_state(_Fp&& __f);
989
990
    void __execute() override;
991
};
992
993
template <class _Fp>
994
inline
995
__async_assoc_state<void, _Fp>::__async_assoc_state(_Fp&& __f)
996
    : __func_(_VSTD::forward<_Fp>(__f))
997
{
998
}
999
1000
template <class _Fp>
1001
void
1002
__async_assoc_state<void, _Fp>::__execute()
1003
{
1004
#ifndef _LIBCPP_NO_EXCEPTIONS
1005
    try
1006
    {
1007
#endif // _LIBCPP_NO_EXCEPTIONS
1008
        __func_();
1009
        this->set_value();
1010
#ifndef _LIBCPP_NO_EXCEPTIONS
1011
    }
1012
    catch (...)
1013
    {
1014
        this->set_exception(current_exception());
1015
    }
1016
#endif // _LIBCPP_NO_EXCEPTIONS
1017
}
1018
1019
template <class _Fp>
1020
void
1021
__async_assoc_state<void, _Fp>::__on_zero_shared() _NOEXCEPT
1022
{
1023
    this->wait();
1024
    base::__on_zero_shared();
1025
}
1026
1027
template <class _Rp> class _LIBCPP_TEMPLATE_VIS promise;
1028
template <class _Rp> class _LIBCPP_TEMPLATE_VIS shared_future;
1029
1030
// future
1031
1032
template <class _Rp> class _LIBCPP_TEMPLATE_VIS future;
1033
1034
template <class _Rp, class _Fp>
1035
_LIBCPP_INLINE_VISIBILITY future<_Rp>
1036
__make_deferred_assoc_state(_Fp&& __f);
1037
1038
template <class _Rp, class _Fp>
1039
_LIBCPP_INLINE_VISIBILITY future<_Rp>
1040
__make_async_assoc_state(_Fp&& __f);
1041
1042
template <class _Rp>
1043
class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE future
1044
{
1045
    __assoc_state<_Rp>* __state_;
1046
1047
    explicit future(__assoc_state<_Rp>* __state);
1048
1049
    template <class> friend class promise;
1050
    template <class> friend class shared_future;
1051
1052
    template <class _R1, class _Fp>
1053
        friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1054
    template <class _R1, class _Fp>
1055
        friend future<_R1> __make_async_assoc_state(_Fp&& __f);
1056
1057
public:
1058
    _LIBCPP_INLINE_VISIBILITY
1059
    future() _NOEXCEPT : __state_(nullptr) {}
1060
    _LIBCPP_INLINE_VISIBILITY
1061
    future(future&& __rhs) _NOEXCEPT
1062
        : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1063
    future(const future&) = delete;
1064
    future& operator=(const future&) = delete;
1065
    _LIBCPP_INLINE_VISIBILITY
1066
    future& operator=(future&& __rhs) _NOEXCEPT
1067
        {
1068
            future(_VSTD::move(__rhs)).swap(*this);
1069
            return *this;
1070
        }
1071
1072
    ~future();
1073
    _LIBCPP_INLINE_VISIBILITY
1074
    shared_future<_Rp> share() _NOEXCEPT;
1075
1076
    // retrieving the value
1077
    _Rp get();
1078
1079
    _LIBCPP_INLINE_VISIBILITY
1080
    void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1081
1082
    // functions to check state
1083
    _LIBCPP_INLINE_VISIBILITY
1084
    bool valid() const _NOEXCEPT {return __state_ != nullptr;}
1085
1086
    _LIBCPP_INLINE_VISIBILITY
1087
    void wait() const {__state_->wait();}
1088
    template <class _Rep, class _Period>
1089
        _LIBCPP_INLINE_VISIBILITY
1090
        future_status
1091
        wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1092
            {return __state_->wait_for(__rel_time);}
1093
    template <class _Clock, class _Duration>
1094
        _LIBCPP_INLINE_VISIBILITY
1095
        future_status
1096
        wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1097
            {return __state_->wait_until(__abs_time);}
1098
};
1099
1100
template <class _Rp>
1101
future<_Rp>::future(__assoc_state<_Rp>* __state)
1102
    : __state_(__state)
1103
{
1104
    __state_->__attach_future();
1105
}
1106
1107
struct __release_shared_count
1108
{
1109
0
    void operator()(__shared_count* __p) {__p->__release_shared();}
1110
};
1111
1112
template <class _Rp>
1113
future<_Rp>::~future()
1114
{
1115
    if (__state_)
1116
        __state_->__release_shared();
1117
}
1118
1119
template <class _Rp>
1120
_Rp
1121
future<_Rp>::get()
1122
{
1123
    unique_ptr<__shared_count, __release_shared_count> __(__state_);
1124
    __assoc_state<_Rp>* __s = __state_;
1125
    __state_ = nullptr;
1126
    return __s->move();
1127
}
1128
1129
template <class _Rp>
1130
class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE future<_Rp&>
1131
{
1132
    __assoc_state<_Rp&>* __state_;
1133
1134
    explicit future(__assoc_state<_Rp&>* __state);
1135
1136
    template <class> friend class promise;
1137
    template <class> friend class shared_future;
1138
1139
    template <class _R1, class _Fp>
1140
        friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1141
    template <class _R1, class _Fp>
1142
        friend future<_R1> __make_async_assoc_state(_Fp&& __f);
1143
1144
public:
1145
    _LIBCPP_INLINE_VISIBILITY
1146
    future() _NOEXCEPT : __state_(nullptr) {}
1147
    _LIBCPP_INLINE_VISIBILITY
1148
    future(future&& __rhs) _NOEXCEPT
1149
        : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1150
    future(const future&) = delete;
1151
    future& operator=(const future&) = delete;
1152
    _LIBCPP_INLINE_VISIBILITY
1153
    future& operator=(future&& __rhs) _NOEXCEPT
1154
        {
1155
            future(_VSTD::move(__rhs)).swap(*this);
1156
            return *this;
1157
        }
1158
1159
    ~future();
1160
    _LIBCPP_INLINE_VISIBILITY
1161
    shared_future<_Rp&> share() _NOEXCEPT;
1162
1163
    // retrieving the value
1164
    _Rp& get();
1165
1166
    _LIBCPP_INLINE_VISIBILITY
1167
    void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1168
1169
    // functions to check state
1170
    _LIBCPP_INLINE_VISIBILITY
1171
    bool valid() const _NOEXCEPT {return __state_ != nullptr;}
1172
1173
    _LIBCPP_INLINE_VISIBILITY
1174
    void wait() const {__state_->wait();}
1175
    template <class _Rep, class _Period>
1176
        _LIBCPP_INLINE_VISIBILITY
1177
        future_status
1178
        wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1179
            {return __state_->wait_for(__rel_time);}
1180
    template <class _Clock, class _Duration>
1181
        _LIBCPP_INLINE_VISIBILITY
1182
        future_status
1183
        wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1184
            {return __state_->wait_until(__abs_time);}
1185
};
1186
1187
template <class _Rp>
1188
future<_Rp&>::future(__assoc_state<_Rp&>* __state)
1189
    : __state_(__state)
1190
{
1191
    __state_->__attach_future();
1192
}
1193
1194
template <class _Rp>
1195
future<_Rp&>::~future()
1196
{
1197
    if (__state_)
1198
        __state_->__release_shared();
1199
}
1200
1201
template <class _Rp>
1202
_Rp&
1203
future<_Rp&>::get()
1204
{
1205
    unique_ptr<__shared_count, __release_shared_count> __(__state_);
1206
    __assoc_state<_Rp&>* __s = __state_;
1207
    __state_ = nullptr;
1208
    return __s->copy();
1209
}
1210
1211
template <>
1212
class _LIBCPP_TYPE_VIS _LIBCPP_AVAILABILITY_FUTURE future<void>
1213
{
1214
    __assoc_sub_state* __state_;
1215
1216
    explicit future(__assoc_sub_state* __state);
1217
1218
    template <class> friend class promise;
1219
    template <class> friend class shared_future;
1220
1221
    template <class _R1, class _Fp>
1222
        friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1223
    template <class _R1, class _Fp>
1224
        friend future<_R1> __make_async_assoc_state(_Fp&& __f);
1225
1226
public:
1227
    _LIBCPP_INLINE_VISIBILITY
1228
0
    future() _NOEXCEPT : __state_(nullptr) {}
1229
    _LIBCPP_INLINE_VISIBILITY
1230
    future(future&& __rhs) _NOEXCEPT
1231
0
        : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1232
    future(const future&) = delete;
1233
    future& operator=(const future&) = delete;
1234
    _LIBCPP_INLINE_VISIBILITY
1235
    future& operator=(future&& __rhs) _NOEXCEPT
1236
0
        {
1237
0
            future(_VSTD::move(__rhs)).swap(*this);
1238
0
            return *this;
1239
0
        }
1240
1241
    ~future();
1242
    _LIBCPP_INLINE_VISIBILITY
1243
    shared_future<void> share() _NOEXCEPT;
1244
1245
    // retrieving the value
1246
    void get();
1247
1248
    _LIBCPP_INLINE_VISIBILITY
1249
0
    void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1250
1251
    // functions to check state
1252
    _LIBCPP_INLINE_VISIBILITY
1253
0
    bool valid() const _NOEXCEPT {return __state_ != nullptr;}
1254
1255
    _LIBCPP_INLINE_VISIBILITY
1256
0
    void wait() const {__state_->wait();}
1257
    template <class _Rep, class _Period>
1258
        _LIBCPP_INLINE_VISIBILITY
1259
        future_status
1260
        wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1261
            {return __state_->wait_for(__rel_time);}
1262
    template <class _Clock, class _Duration>
1263
        _LIBCPP_INLINE_VISIBILITY
1264
        future_status
1265
        wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1266
            {return __state_->wait_until(__abs_time);}
1267
};
1268
1269
template <class _Rp>
1270
inline _LIBCPP_INLINE_VISIBILITY
1271
void
1272
swap(future<_Rp>& __x, future<_Rp>& __y) _NOEXCEPT
1273
{
1274
    __x.swap(__y);
1275
}
1276
1277
// promise<R>
1278
1279
template <class _Callable> class packaged_task;
1280
1281
template <class _Rp>
1282
class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE promise
1283
{
1284
    __assoc_state<_Rp>* __state_;
1285
1286
    _LIBCPP_INLINE_VISIBILITY
1287
    explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1288
1289
    template <class> friend class packaged_task;
1290
public:
1291
    promise();
1292
    template <class _Alloc>
1293
        promise(allocator_arg_t, const _Alloc& __a);
1294
    _LIBCPP_INLINE_VISIBILITY
1295
    promise(promise&& __rhs) _NOEXCEPT
1296
        : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1297
    promise(const promise& __rhs) = delete;
1298
    ~promise();
1299
1300
    // assignment
1301
    _LIBCPP_INLINE_VISIBILITY
1302
    promise& operator=(promise&& __rhs) _NOEXCEPT
1303
        {
1304
            promise(_VSTD::move(__rhs)).swap(*this);
1305
            return *this;
1306
        }
1307
    promise& operator=(const promise& __rhs) = delete;
1308
1309
    _LIBCPP_INLINE_VISIBILITY
1310
    void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1311
1312
    // retrieving the result
1313
    future<_Rp> get_future();
1314
1315
    // setting the result
1316
    void set_value(const _Rp& __r);
1317
    void set_value(_Rp&& __r);
1318
    void set_exception(exception_ptr __p);
1319
1320
    // setting the result with deferred notification
1321
    void set_value_at_thread_exit(const _Rp& __r);
1322
    void set_value_at_thread_exit(_Rp&& __r);
1323
    void set_exception_at_thread_exit(exception_ptr __p);
1324
};
1325
1326
template <class _Rp>
1327
promise<_Rp>::promise()
1328
    : __state_(new __assoc_state<_Rp>)
1329
{
1330
}
1331
1332
template <class _Rp>
1333
template <class _Alloc>
1334
promise<_Rp>::promise(allocator_arg_t, const _Alloc& __a0)
1335
{
1336
    typedef __assoc_state_alloc<_Rp, _Alloc> _State;
1337
    typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1338
    typedef __allocator_destructor<_A2> _D2;
1339
    _A2 __a(__a0);
1340
    unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1341
    ::new ((void*)_VSTD::addressof(*__hold.get())) _State(__a0);
1342
    __state_ = _VSTD::addressof(*__hold.release());
1343
}
1344
1345
template <class _Rp>
1346
promise<_Rp>::~promise()
1347
{
1348
    if (__state_)
1349
    {
1350
        if (!__state_->__has_value() && __state_->use_count() > 1)
1351
            __state_->set_exception(make_exception_ptr(
1352
                      future_error(make_error_code(future_errc::broken_promise))
1353
                                                      ));
1354
        __state_->__release_shared();
1355
    }
1356
}
1357
1358
template <class _Rp>
1359
future<_Rp>
1360
promise<_Rp>::get_future()
1361
{
1362
    if (__state_ == nullptr)
1363
        __throw_future_error(future_errc::no_state);
1364
    return future<_Rp>(__state_);
1365
}
1366
1367
template <class _Rp>
1368
void
1369
promise<_Rp>::set_value(const _Rp& __r)
1370
{
1371
    if (__state_ == nullptr)
1372
        __throw_future_error(future_errc::no_state);
1373
    __state_->set_value(__r);
1374
}
1375
1376
template <class _Rp>
1377
void
1378
promise<_Rp>::set_value(_Rp&& __r)
1379
{
1380
    if (__state_ == nullptr)
1381
        __throw_future_error(future_errc::no_state);
1382
    __state_->set_value(_VSTD::move(__r));
1383
}
1384
1385
template <class _Rp>
1386
void
1387
promise<_Rp>::set_exception(exception_ptr __p)
1388
{
1389
    _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception: received nullptr" );
1390
    if (__state_ == nullptr)
1391
        __throw_future_error(future_errc::no_state);
1392
    __state_->set_exception(__p);
1393
}
1394
1395
template <class _Rp>
1396
void
1397
promise<_Rp>::set_value_at_thread_exit(const _Rp& __r)
1398
{
1399
    if (__state_ == nullptr)
1400
        __throw_future_error(future_errc::no_state);
1401
    __state_->set_value_at_thread_exit(__r);
1402
}
1403
1404
template <class _Rp>
1405
void
1406
promise<_Rp>::set_value_at_thread_exit(_Rp&& __r)
1407
{
1408
    if (__state_ == nullptr)
1409
        __throw_future_error(future_errc::no_state);
1410
    __state_->set_value_at_thread_exit(_VSTD::move(__r));
1411
}
1412
1413
template <class _Rp>
1414
void
1415
promise<_Rp>::set_exception_at_thread_exit(exception_ptr __p)
1416
{
1417
    _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception_at_thread_exit: received nullptr" );
1418
    if (__state_ == nullptr)
1419
        __throw_future_error(future_errc::no_state);
1420
    __state_->set_exception_at_thread_exit(__p);
1421
}
1422
1423
// promise<R&>
1424
1425
template <class _Rp>
1426
class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE promise<_Rp&>
1427
{
1428
    __assoc_state<_Rp&>* __state_;
1429
1430
    _LIBCPP_INLINE_VISIBILITY
1431
    explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1432
1433
    template <class> friend class packaged_task;
1434
1435
public:
1436
    promise();
1437
    template <class _Allocator>
1438
        promise(allocator_arg_t, const _Allocator& __a);
1439
    _LIBCPP_INLINE_VISIBILITY
1440
    promise(promise&& __rhs) _NOEXCEPT
1441
        : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1442
    promise(const promise& __rhs) = delete;
1443
    ~promise();
1444
1445
    // assignment
1446
    _LIBCPP_INLINE_VISIBILITY
1447
    promise& operator=(promise&& __rhs) _NOEXCEPT
1448
        {
1449
            promise(_VSTD::move(__rhs)).swap(*this);
1450
            return *this;
1451
        }
1452
    promise& operator=(const promise& __rhs) = delete;
1453
1454
    _LIBCPP_INLINE_VISIBILITY
1455
    void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1456
1457
    // retrieving the result
1458
    future<_Rp&> get_future();
1459
1460
    // setting the result
1461
    void set_value(_Rp& __r);
1462
    void set_exception(exception_ptr __p);
1463
1464
    // setting the result with deferred notification
1465
    void set_value_at_thread_exit(_Rp&);
1466
    void set_exception_at_thread_exit(exception_ptr __p);
1467
};
1468
1469
template <class _Rp>
1470
promise<_Rp&>::promise()
1471
    : __state_(new __assoc_state<_Rp&>)
1472
{
1473
}
1474
1475
template <class _Rp>
1476
template <class _Alloc>
1477
promise<_Rp&>::promise(allocator_arg_t, const _Alloc& __a0)
1478
{
1479
    typedef __assoc_state_alloc<_Rp&, _Alloc> _State;
1480
    typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1481
    typedef __allocator_destructor<_A2> _D2;
1482
    _A2 __a(__a0);
1483
    unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1484
    ::new ((void*)_VSTD::addressof(*__hold.get())) _State(__a0);
1485
    __state_ = _VSTD::addressof(*__hold.release());
1486
}
1487
1488
template <class _Rp>
1489
promise<_Rp&>::~promise()
1490
{
1491
    if (__state_)
1492
    {
1493
        if (!__state_->__has_value() && __state_->use_count() > 1)
1494
            __state_->set_exception(make_exception_ptr(
1495
                      future_error(make_error_code(future_errc::broken_promise))
1496
                                                      ));
1497
        __state_->__release_shared();
1498
    }
1499
}
1500
1501
template <class _Rp>
1502
future<_Rp&>
1503
promise<_Rp&>::get_future()
1504
{
1505
    if (__state_ == nullptr)
1506
        __throw_future_error(future_errc::no_state);
1507
    return future<_Rp&>(__state_);
1508
}
1509
1510
template <class _Rp>
1511
void
1512
promise<_Rp&>::set_value(_Rp& __r)
1513
{
1514
    if (__state_ == nullptr)
1515
        __throw_future_error(future_errc::no_state);
1516
    __state_->set_value(__r);
1517
}
1518
1519
template <class _Rp>
1520
void
1521
promise<_Rp&>::set_exception(exception_ptr __p)
1522
{
1523
    _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception: received nullptr" );
1524
    if (__state_ == nullptr)
1525
        __throw_future_error(future_errc::no_state);
1526
    __state_->set_exception(__p);
1527
}
1528
1529
template <class _Rp>
1530
void
1531
promise<_Rp&>::set_value_at_thread_exit(_Rp& __r)
1532
{
1533
    if (__state_ == nullptr)
1534
        __throw_future_error(future_errc::no_state);
1535
    __state_->set_value_at_thread_exit(__r);
1536
}
1537
1538
template <class _Rp>
1539
void
1540
promise<_Rp&>::set_exception_at_thread_exit(exception_ptr __p)
1541
{
1542
    _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception_at_thread_exit: received nullptr" );
1543
    if (__state_ == nullptr)
1544
        __throw_future_error(future_errc::no_state);
1545
    __state_->set_exception_at_thread_exit(__p);
1546
}
1547
1548
// promise<void>
1549
1550
template <>
1551
class _LIBCPP_TYPE_VIS _LIBCPP_AVAILABILITY_FUTURE promise<void>
1552
{
1553
    __assoc_sub_state* __state_;
1554
1555
    _LIBCPP_INLINE_VISIBILITY
1556
0
    explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
1557
1558
    template <class> friend class packaged_task;
1559
1560
public:
1561
    promise();
1562
    template <class _Allocator>
1563
        _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1564
        promise(allocator_arg_t, const _Allocator& __a);
1565
    _LIBCPP_INLINE_VISIBILITY
1566
    promise(promise&& __rhs) _NOEXCEPT
1567
0
        : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1568
    promise(const promise& __rhs) = delete;
1569
    ~promise();
1570
1571
    // assignment
1572
    _LIBCPP_INLINE_VISIBILITY
1573
    promise& operator=(promise&& __rhs) _NOEXCEPT
1574
0
        {
1575
0
            promise(_VSTD::move(__rhs)).swap(*this);
1576
0
            return *this;
1577
0
        }
1578
    promise& operator=(const promise& __rhs) = delete;
1579
1580
    _LIBCPP_INLINE_VISIBILITY
1581
0
    void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
1582
1583
    // retrieving the result
1584
    future<void> get_future();
1585
1586
    // setting the result
1587
    void set_value();
1588
    void set_exception(exception_ptr __p);
1589
1590
    // setting the result with deferred notification
1591
    void set_value_at_thread_exit();
1592
    void set_exception_at_thread_exit(exception_ptr __p);
1593
};
1594
1595
template <class _Alloc>
1596
promise<void>::promise(allocator_arg_t, const _Alloc& __a0)
1597
{
1598
    typedef __assoc_sub_state_alloc<_Alloc> _State;
1599
    typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
1600
    typedef __allocator_destructor<_A2> _D2;
1601
    _A2 __a(__a0);
1602
    unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1603
    ::new ((void*)_VSTD::addressof(*__hold.get())) _State(__a0);
1604
    __state_ = _VSTD::addressof(*__hold.release());
1605
}
1606
1607
template <class _Rp>
1608
inline _LIBCPP_INLINE_VISIBILITY
1609
void
1610
swap(promise<_Rp>& __x, promise<_Rp>& __y) _NOEXCEPT
1611
{
1612
    __x.swap(__y);
1613
}
1614
1615
template <class _Rp, class _Alloc>
1616
    struct _LIBCPP_TEMPLATE_VIS uses_allocator<promise<_Rp>, _Alloc>
1617
        : public true_type {};
1618
1619
// packaged_task
1620
1621
template<class _Fp> class __packaged_task_base;
1622
1623
template<class _Rp, class ..._ArgTypes>
1624
class _LIBCPP_AVAILABILITY_FUTURE __packaged_task_base<_Rp(_ArgTypes...)>
1625
{
1626
    __packaged_task_base(const __packaged_task_base&);
1627
    __packaged_task_base& operator=(const __packaged_task_base&);
1628
public:
1629
    _LIBCPP_INLINE_VISIBILITY
1630
    __packaged_task_base() {}
1631
    _LIBCPP_HIDE_FROM_ABI_VIRTUAL
1632
    virtual ~__packaged_task_base() {}
1633
    virtual void __move_to(__packaged_task_base*) _NOEXCEPT = 0;
1634
    virtual void destroy() = 0;
1635
    virtual void destroy_deallocate() = 0;
1636
    virtual _Rp operator()(_ArgTypes&& ...) = 0;
1637
};
1638
1639
template<class _FD, class _Alloc, class _FB> class __packaged_task_func;
1640
1641
template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1642
class _LIBCPP_AVAILABILITY_FUTURE __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>
1643
    : public  __packaged_task_base<_Rp(_ArgTypes...)>
1644
{
1645
    __compressed_pair<_Fp, _Alloc> __f_;
1646
public:
1647
    _LIBCPP_INLINE_VISIBILITY
1648
    explicit __packaged_task_func(const _Fp& __f) : __f_(__f, __default_init_tag()) {}
1649
    _LIBCPP_INLINE_VISIBILITY
1650
    explicit __packaged_task_func(_Fp&& __f) : __f_(_VSTD::move(__f), __default_init_tag()) {}
1651
    _LIBCPP_INLINE_VISIBILITY
1652
    __packaged_task_func(const _Fp& __f, const _Alloc& __a)
1653
        : __f_(__f, __a) {}
1654
    _LIBCPP_INLINE_VISIBILITY
1655
    __packaged_task_func(_Fp&& __f, const _Alloc& __a)
1656
        : __f_(_VSTD::move(__f), __a) {}
1657
    virtual void __move_to(__packaged_task_base<_Rp(_ArgTypes...)>*) _NOEXCEPT;
1658
    virtual void destroy();
1659
    virtual void destroy_deallocate();
1660
    virtual _Rp operator()(_ArgTypes&& ... __args);
1661
};
1662
1663
template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1664
void
1665
__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__move_to(
1666
                              __packaged_task_base<_Rp(_ArgTypes...)>* __p) _NOEXCEPT
1667
{
1668
    ::new ((void*)__p) __packaged_task_func(_VSTD::move(__f_.first()), _VSTD::move(__f_.second()));
1669
}
1670
1671
template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1672
void
1673
__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy()
1674
{
1675
    __f_.~__compressed_pair<_Fp, _Alloc>();
1676
}
1677
1678
template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1679
void
1680
__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate()
1681
{
1682
    typedef typename __allocator_traits_rebind<_Alloc, __packaged_task_func>::type _Ap;
1683
    typedef allocator_traits<_Ap> _ATraits;
1684
    typedef pointer_traits<typename _ATraits::pointer> _PTraits;
1685
    _Ap __a(__f_.second());
1686
    __f_.~__compressed_pair<_Fp, _Alloc>();
1687
    __a.deallocate(_PTraits::pointer_to(*this), 1);
1688
}
1689
1690
template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1691
_Rp
1692
__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
1693
{
1694
    return _VSTD::__invoke(__f_.first(), _VSTD::forward<_ArgTypes>(__arg)...);
1695
}
1696
1697
template <class _Callable> class __packaged_task_function;
1698
1699
template<class _Rp, class ..._ArgTypes>
1700
class _LIBCPP_AVAILABILITY_FUTURE __packaged_task_function<_Rp(_ArgTypes...)>
1701
{
1702
    typedef __packaged_task_base<_Rp(_ArgTypes...)> __base;
1703
1704
    _LIBCPP_INLINE_VISIBILITY _LIBCPP_NO_CFI
1705
    __base* __get_buf() { return (__base*)&__buf_; }
1706
1707
    _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1708
    typename aligned_storage<3*sizeof(void*)>::type __buf_;
1709
    _LIBCPP_SUPPRESS_DEPRECATED_POP
1710
    __base* __f_;
1711
1712
public:
1713
    typedef _Rp result_type;
1714
1715
    // construct/copy/destroy:
1716
    _LIBCPP_INLINE_VISIBILITY
1717
    __packaged_task_function() _NOEXCEPT : __f_(nullptr) {}
1718
    template<class _Fp>
1719
      __packaged_task_function(_Fp&& __f);
1720
    template<class _Fp, class _Alloc>
1721
      __packaged_task_function(allocator_arg_t, const _Alloc& __a, _Fp&& __f);
1722
1723
    __packaged_task_function(__packaged_task_function&&) _NOEXCEPT;
1724
    __packaged_task_function& operator=(__packaged_task_function&&) _NOEXCEPT;
1725
1726
    __packaged_task_function(const __packaged_task_function&) =  delete;
1727
    __packaged_task_function& operator=(const __packaged_task_function&) =  delete;
1728
1729
    ~__packaged_task_function();
1730
1731
    void swap(__packaged_task_function&) _NOEXCEPT;
1732
1733
    _LIBCPP_INLINE_VISIBILITY
1734
    _Rp operator()(_ArgTypes...) const;
1735
};
1736
1737
template<class _Rp, class ..._ArgTypes>
1738
__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f) _NOEXCEPT
1739
{
1740
    if (__f.__f_ == nullptr)
1741
        __f_ = nullptr;
1742
    else if (__f.__f_ == __f.__get_buf())
1743
    {
1744
        __f.__f_->__move_to(__get_buf());
1745
        __f_ = (__base*)&__buf_;
1746
    }
1747
    else
1748
    {
1749
        __f_ = __f.__f_;
1750
        __f.__f_ = nullptr;
1751
    }
1752
}
1753
1754
template<class _Rp, class ..._ArgTypes>
1755
template <class _Fp>
1756
__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(_Fp&& __f)
1757
    : __f_(nullptr)
1758
{
1759
    typedef __libcpp_remove_reference_t<typename decay<_Fp>::type> _FR;
1760
    typedef __packaged_task_func<_FR, allocator<_FR>, _Rp(_ArgTypes...)> _FF;
1761
    if (sizeof(_FF) <= sizeof(__buf_))
1762
    {
1763
        ::new ((void*)&__buf_) _FF(_VSTD::forward<_Fp>(__f));
1764
        __f_ = (__base*)&__buf_;
1765
    }
1766
    else
1767
    {
1768
        typedef allocator<_FF> _Ap;
1769
        _Ap __a;
1770
        typedef __allocator_destructor<_Ap> _Dp;
1771
        unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1772
        ::new ((void*)__hold.get()) _FF(_VSTD::forward<_Fp>(__f), allocator<_FR>(__a));
1773
        __f_ = __hold.release();
1774
    }
1775
}
1776
1777
template<class _Rp, class ..._ArgTypes>
1778
template <class _Fp, class _Alloc>
1779
__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(
1780
                                  allocator_arg_t, const _Alloc& __a0, _Fp&& __f)
1781
    : __f_(nullptr)
1782
{
1783
    typedef __libcpp_remove_reference_t<typename decay<_Fp>::type> _FR;
1784
    typedef __packaged_task_func<_FR, _Alloc, _Rp(_ArgTypes...)> _FF;
1785
    if (sizeof(_FF) <= sizeof(__buf_))
1786
    {
1787
        __f_ = (__base*)&__buf_;
1788
        ::new ((void*)__f_) _FF(_VSTD::forward<_Fp>(__f));
1789
    }
1790
    else
1791
    {
1792
        typedef typename __allocator_traits_rebind<_Alloc, _FF>::type _Ap;
1793
        _Ap __a(__a0);
1794
        typedef __allocator_destructor<_Ap> _Dp;
1795
        unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1796
        ::new ((void*)_VSTD::addressof(*__hold.get()))
1797
            _FF(_VSTD::forward<_Fp>(__f), _Alloc(__a));
1798
        __f_ = _VSTD::addressof(*__hold.release());
1799
    }
1800
}
1801
1802
template<class _Rp, class ..._ArgTypes>
1803
__packaged_task_function<_Rp(_ArgTypes...)>&
1804
__packaged_task_function<_Rp(_ArgTypes...)>::operator=(__packaged_task_function&& __f) _NOEXCEPT
1805
{
1806
    if (__f_ == __get_buf())
1807
        __f_->destroy();
1808
    else if (__f_)
1809
        __f_->destroy_deallocate();
1810
    __f_ = nullptr;
1811
    if (__f.__f_ == nullptr)
1812
        __f_ = nullptr;
1813
    else if (__f.__f_ == __f.__get_buf())
1814
    {
1815
        __f.__f_->__move_to(__get_buf());
1816
        __f_ = __get_buf();
1817
    }
1818
    else
1819
    {
1820
        __f_ = __f.__f_;
1821
        __f.__f_ = nullptr;
1822
    }
1823
    return *this;
1824
}
1825
1826
template<class _Rp, class ..._ArgTypes>
1827
__packaged_task_function<_Rp(_ArgTypes...)>::~__packaged_task_function()
1828
{
1829
    if (__f_ == __get_buf())
1830
        __f_->destroy();
1831
    else if (__f_)
1832
        __f_->destroy_deallocate();
1833
}
1834
1835
template<class _Rp, class ..._ArgTypes>
1836
_LIBCPP_NO_CFI
1837
void
1838
__packaged_task_function<_Rp(_ArgTypes...)>::swap(__packaged_task_function& __f) _NOEXCEPT
1839
{
1840
    if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1841
    {
1842
        _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1843
        typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1844
        _LIBCPP_SUPPRESS_DEPRECATED_POP
1845
        __base* __t = (__base*)&__tempbuf;
1846
        __f_->__move_to(__t);
1847
        __f_->destroy();
1848
        __f_ = nullptr;
1849
        __f.__f_->__move_to((__base*)&__buf_);
1850
        __f.__f_->destroy();
1851
        __f.__f_ = nullptr;
1852
        __f_ = (__base*)&__buf_;
1853
        __t->__move_to((__base*)&__f.__buf_);
1854
        __t->destroy();
1855
        __f.__f_ = (__base*)&__f.__buf_;
1856
    }
1857
    else if (__f_ == (__base*)&__buf_)
1858
    {
1859
        __f_->__move_to((__base*)&__f.__buf_);
1860
        __f_->destroy();
1861
        __f_ = __f.__f_;
1862
        __f.__f_ = (__base*)&__f.__buf_;
1863
    }
1864
    else if (__f.__f_ == (__base*)&__f.__buf_)
1865
    {
1866
        __f.__f_->__move_to((__base*)&__buf_);
1867
        __f.__f_->destroy();
1868
        __f.__f_ = __f_;
1869
        __f_ = (__base*)&__buf_;
1870
    }
1871
    else
1872
        _VSTD::swap(__f_, __f.__f_);
1873
}
1874
1875
template<class _Rp, class ..._ArgTypes>
1876
inline
1877
_Rp
1878
__packaged_task_function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
1879
{
1880
    return (*__f_)(_VSTD::forward<_ArgTypes>(__arg)...);
1881
}
1882
1883
template<class _Rp, class ..._ArgTypes>
1884
class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE packaged_task<_Rp(_ArgTypes...)>
1885
{
1886
public:
1887
    typedef _Rp result_type; // extension
1888
1889
private:
1890
    __packaged_task_function<result_type(_ArgTypes...)> __f_;
1891
    promise<result_type>                                __p_;
1892
1893
public:
1894
    // construction and destruction
1895
    _LIBCPP_INLINE_VISIBILITY
1896
    packaged_task() _NOEXCEPT : __p_(nullptr) {}
1897
    template <class _Fp,
1898
              class = __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value> >
1899
        _LIBCPP_INLINE_VISIBILITY
1900
        explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
1901
    template <class _Fp, class _Allocator,
1902
              class = __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value> >
1903
        _LIBCPP_INLINE_VISIBILITY
1904
        packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
1905
             : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)),
1906
               __p_(allocator_arg, __a) {}
1907
    // ~packaged_task() = default;
1908
1909
    // no copy
1910
    packaged_task(const packaged_task&) = delete;
1911
    packaged_task& operator=(const packaged_task&) = delete;
1912
1913
    // move support
1914
    _LIBCPP_INLINE_VISIBILITY
1915
    packaged_task(packaged_task&& __other) _NOEXCEPT
1916
        : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
1917
    _LIBCPP_INLINE_VISIBILITY
1918
    packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
1919
    {
1920
        __f_ = _VSTD::move(__other.__f_);
1921
        __p_ = _VSTD::move(__other.__p_);
1922
        return *this;
1923
    }
1924
    _LIBCPP_INLINE_VISIBILITY
1925
    void swap(packaged_task& __other) _NOEXCEPT
1926
    {
1927
        __f_.swap(__other.__f_);
1928
        __p_.swap(__other.__p_);
1929
    }
1930
1931
    _LIBCPP_INLINE_VISIBILITY
1932
    bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
1933
1934
    // result retrieval
1935
    _LIBCPP_INLINE_VISIBILITY
1936
    future<result_type> get_future() {return __p_.get_future();}
1937
1938
    // execution
1939
    void operator()(_ArgTypes... __args);
1940
    void make_ready_at_thread_exit(_ArgTypes... __args);
1941
1942
    void reset();
1943
};
1944
1945
template<class _Rp, class ..._ArgTypes>
1946
void
1947
packaged_task<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __args)
1948
{
1949
    if (__p_.__state_ == nullptr)
1950
        __throw_future_error(future_errc::no_state);
1951
    if (__p_.__state_->__has_value())
1952
        __throw_future_error(future_errc::promise_already_satisfied);
1953
#ifndef _LIBCPP_NO_EXCEPTIONS
1954
    try
1955
    {
1956
#endif // _LIBCPP_NO_EXCEPTIONS
1957
        __p_.set_value(__f_(_VSTD::forward<_ArgTypes>(__args)...));
1958
#ifndef _LIBCPP_NO_EXCEPTIONS
1959
    }
1960
    catch (...)
1961
    {
1962
        __p_.set_exception(current_exception());
1963
    }
1964
#endif // _LIBCPP_NO_EXCEPTIONS
1965
}
1966
1967
template<class _Rp, class ..._ArgTypes>
1968
void
1969
packaged_task<_Rp(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
1970
{
1971
    if (__p_.__state_ == nullptr)
1972
        __throw_future_error(future_errc::no_state);
1973
    if (__p_.__state_->__has_value())
1974
        __throw_future_error(future_errc::promise_already_satisfied);
1975
#ifndef _LIBCPP_NO_EXCEPTIONS
1976
    try
1977
    {
1978
#endif // _LIBCPP_NO_EXCEPTIONS
1979
        __p_.set_value_at_thread_exit(__f_(_VSTD::forward<_ArgTypes>(__args)...));
1980
#ifndef _LIBCPP_NO_EXCEPTIONS
1981
    }
1982
    catch (...)
1983
    {
1984
        __p_.set_exception_at_thread_exit(current_exception());
1985
    }
1986
#endif // _LIBCPP_NO_EXCEPTIONS
1987
}
1988
1989
template<class _Rp, class ..._ArgTypes>
1990
void
1991
packaged_task<_Rp(_ArgTypes...)>::reset()
1992
{
1993
    if (!valid())
1994
        __throw_future_error(future_errc::no_state);
1995
    __p_ = promise<result_type>();
1996
}
1997
1998
template<class ..._ArgTypes>
1999
class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE packaged_task<void(_ArgTypes...)>
2000
{
2001
public:
2002
    typedef void result_type; // extension
2003
2004
private:
2005
    __packaged_task_function<result_type(_ArgTypes...)> __f_;
2006
    promise<result_type>                                __p_;
2007
2008
public:
2009
    // construction and destruction
2010
    _LIBCPP_INLINE_VISIBILITY
2011
    packaged_task() _NOEXCEPT : __p_(nullptr) {}
2012
    template <class _Fp,
2013
              class = __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value> >
2014
        _LIBCPP_INLINE_VISIBILITY
2015
        explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
2016
    template <class _Fp, class _Allocator,
2017
              class = __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value> >
2018
        _LIBCPP_INLINE_VISIBILITY
2019
        packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
2020
             : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)),
2021
               __p_(allocator_arg, __a) {}
2022
    // ~packaged_task() = default;
2023
2024
    // no copy
2025
    packaged_task(const packaged_task&) = delete;
2026
    packaged_task& operator=(const packaged_task&) = delete;
2027
2028
    // move support
2029
    _LIBCPP_INLINE_VISIBILITY
2030
    packaged_task(packaged_task&& __other) _NOEXCEPT
2031
        : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
2032
    _LIBCPP_INLINE_VISIBILITY
2033
    packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
2034
    {
2035
        __f_ = _VSTD::move(__other.__f_);
2036
        __p_ = _VSTD::move(__other.__p_);
2037
        return *this;
2038
    }
2039
    _LIBCPP_INLINE_VISIBILITY
2040
    void swap(packaged_task& __other) _NOEXCEPT
2041
    {
2042
        __f_.swap(__other.__f_);
2043
        __p_.swap(__other.__p_);
2044
    }
2045
2046
    _LIBCPP_INLINE_VISIBILITY
2047
    bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
2048
2049
    // result retrieval
2050
    _LIBCPP_INLINE_VISIBILITY
2051
    future<result_type> get_future() {return __p_.get_future();}
2052
2053
    // execution
2054
    void operator()(_ArgTypes... __args);
2055
    void make_ready_at_thread_exit(_ArgTypes... __args);
2056
2057
    void reset();
2058
};
2059
2060
#if _LIBCPP_STD_VER >= 17
2061
2062
template <class _Rp, class... _Args>
2063
packaged_task(_Rp(*)(_Args...)) -> packaged_task<_Rp(_Args...)>;
2064
2065
template <class _Fp, class _Stripped = typename __strip_signature<decltype(&_Fp::operator())>::type>
2066
packaged_task(_Fp) -> packaged_task<_Stripped>;
2067
2068
#endif
2069
2070
template<class ..._ArgTypes>
2071
void
2072
packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args)
2073
{
2074
    if (__p_.__state_ == nullptr)
2075
        __throw_future_error(future_errc::no_state);
2076
    if (__p_.__state_->__has_value())
2077
        __throw_future_error(future_errc::promise_already_satisfied);
2078
#ifndef _LIBCPP_NO_EXCEPTIONS
2079
    try
2080
    {
2081
#endif // _LIBCPP_NO_EXCEPTIONS
2082
        __f_(_VSTD::forward<_ArgTypes>(__args)...);
2083
        __p_.set_value();
2084
#ifndef _LIBCPP_NO_EXCEPTIONS
2085
    }
2086
    catch (...)
2087
    {
2088
        __p_.set_exception(current_exception());
2089
    }
2090
#endif // _LIBCPP_NO_EXCEPTIONS
2091
}
2092
2093
template<class ..._ArgTypes>
2094
void
2095
packaged_task<void(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
2096
{
2097
    if (__p_.__state_ == nullptr)
2098
        __throw_future_error(future_errc::no_state);
2099
    if (__p_.__state_->__has_value())
2100
        __throw_future_error(future_errc::promise_already_satisfied);
2101
#ifndef _LIBCPP_NO_EXCEPTIONS
2102
    try
2103
    {
2104
#endif // _LIBCPP_NO_EXCEPTIONS
2105
        __f_(_VSTD::forward<_ArgTypes>(__args)...);
2106
        __p_.set_value_at_thread_exit();
2107
#ifndef _LIBCPP_NO_EXCEPTIONS
2108
    }
2109
    catch (...)
2110
    {
2111
        __p_.set_exception_at_thread_exit(current_exception());
2112
    }
2113
#endif // _LIBCPP_NO_EXCEPTIONS
2114
}
2115
2116
template<class ..._ArgTypes>
2117
void
2118
packaged_task<void(_ArgTypes...)>::reset()
2119
{
2120
    if (!valid())
2121
        __throw_future_error(future_errc::no_state);
2122
    __p_ = promise<result_type>();
2123
}
2124
2125
template <class _Rp, class... _ArgTypes>
2126
inline _LIBCPP_INLINE_VISIBILITY
2127
void
2128
swap(packaged_task<_Rp(_ArgTypes...)>& __x, packaged_task<_Rp(_ArgTypes...)>& __y) _NOEXCEPT
2129
{
2130
    __x.swap(__y);
2131
}
2132
2133
template <class _Callable, class _Alloc>
2134
struct _LIBCPP_TEMPLATE_VIS uses_allocator<packaged_task<_Callable>, _Alloc>
2135
    : public true_type {};
2136
2137
template <class _Rp, class _Fp>
2138
_LIBCPP_INLINE_VISIBILITY future<_Rp>
2139
__make_deferred_assoc_state(_Fp&& __f)
2140
{
2141
    unique_ptr<__deferred_assoc_state<_Rp, _Fp>, __release_shared_count>
2142
        __h(new __deferred_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
2143
    return future<_Rp>(__h.get());
2144
}
2145
2146
template <class _Rp, class _Fp>
2147
_LIBCPP_INLINE_VISIBILITY future<_Rp>
2148
__make_async_assoc_state(_Fp&& __f)
2149
{
2150
    unique_ptr<__async_assoc_state<_Rp, _Fp>, __release_shared_count>
2151
        __h(new __async_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
2152
    _VSTD::thread(&__async_assoc_state<_Rp, _Fp>::__execute, __h.get()).detach();
2153
    return future<_Rp>(__h.get());
2154
}
2155
2156
#ifndef _LIBCPP_CXX03_LANG
2157
2158
template <class _Fp, class... _Args>
2159
class _LIBCPP_HIDDEN __async_func
2160
{
2161
    tuple<_Fp, _Args...> __f_;
2162
2163
public:
2164
    typedef typename __invoke_of<_Fp, _Args...>::type _Rp;
2165
2166
    _LIBCPP_INLINE_VISIBILITY
2167
    explicit __async_func(_Fp&& __f, _Args&&... __args)
2168
        : __f_(_VSTD::move(__f), _VSTD::move(__args)...) {}
2169
2170
    _LIBCPP_INLINE_VISIBILITY
2171
    __async_func(__async_func&& __f) : __f_(_VSTD::move(__f.__f_)) {}
2172
2173
    _Rp operator()()
2174
    {
2175
        typedef typename __make_tuple_indices<1+sizeof...(_Args), 1>::type _Index;
2176
        return __execute(_Index());
2177
    }
2178
private:
2179
    template <size_t ..._Indices>
2180
    _Rp
2181
    __execute(__tuple_indices<_Indices...>)
2182
    {
2183
        return _VSTD::__invoke(_VSTD::move(_VSTD::get<0>(__f_)), _VSTD::move(_VSTD::get<_Indices>(__f_))...);
2184
    }
2185
};
2186
2187
inline _LIBCPP_INLINE_VISIBILITY bool __does_policy_contain(launch __policy, launch __value )
2188
0
{ return (int(__policy) & int(__value)) != 0; }
2189
2190
template <class _Fp, class... _Args>
2191
_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
2192
future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
2193
async(launch __policy, _Fp&& __f, _Args&&... __args)
2194
{
2195
    typedef __async_func<typename decay<_Fp>::type, typename decay<_Args>::type...> _BF;
2196
    typedef typename _BF::_Rp _Rp;
2197
2198
#ifndef _LIBCPP_NO_EXCEPTIONS
2199
    try
2200
    {
2201
#endif
2202
        if (__does_policy_contain(__policy, launch::async))
2203
        return _VSTD::__make_async_assoc_state<_Rp>(_BF(_LIBCPP_AUTO_CAST(_VSTD::forward<_Fp>(__f)),
2204
                                                        _LIBCPP_AUTO_CAST(_VSTD::forward<_Args>(__args))...));
2205
#ifndef _LIBCPP_NO_EXCEPTIONS
2206
    }
2207
    catch ( ... ) { if (__policy == launch::async) throw ; }
2208
#endif
2209
2210
    if (__does_policy_contain(__policy, launch::deferred))
2211
        return _VSTD::__make_deferred_assoc_state<_Rp>(_BF(_LIBCPP_AUTO_CAST(_VSTD::forward<_Fp>(__f)),
2212
                                                           _LIBCPP_AUTO_CAST(_VSTD::forward<_Args>(__args))...));
2213
    return future<_Rp>{};
2214
}
2215
2216
template <class _Fp, class... _Args>
2217
_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY
2218
future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
2219
async(_Fp&& __f, _Args&&... __args)
2220
{
2221
    return _VSTD::async(launch::any, _VSTD::forward<_Fp>(__f),
2222
                                    _VSTD::forward<_Args>(__args)...);
2223
}
2224
2225
#endif // C++03
2226
2227
// shared_future
2228
2229
template <class _Rp>
2230
class _LIBCPP_TEMPLATE_VIS shared_future
2231
{
2232
    __assoc_state<_Rp>* __state_;
2233
2234
public:
2235
    _LIBCPP_INLINE_VISIBILITY
2236
    shared_future() _NOEXCEPT : __state_(nullptr) {}
2237
    _LIBCPP_INLINE_VISIBILITY
2238
    shared_future(const shared_future& __rhs)  _NOEXCEPT : __state_(__rhs.__state_)
2239
        {if (__state_) __state_->__add_shared();}
2240
    _LIBCPP_INLINE_VISIBILITY
2241
    shared_future(future<_Rp>&& __f) _NOEXCEPT : __state_(__f.__state_)
2242
        {__f.__state_ = nullptr;}
2243
    _LIBCPP_INLINE_VISIBILITY
2244
    shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
2245
        {__rhs.__state_ = nullptr;}
2246
    ~shared_future();
2247
    shared_future& operator=(const shared_future& __rhs) _NOEXCEPT;
2248
    _LIBCPP_INLINE_VISIBILITY
2249
    shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
2250
        {
2251
            shared_future(_VSTD::move(__rhs)).swap(*this);
2252
            return *this;
2253
        }
2254
2255
    // retrieving the value
2256
    _LIBCPP_INLINE_VISIBILITY
2257
    const _Rp& get() const {return __state_->copy();}
2258
2259
    _LIBCPP_INLINE_VISIBILITY
2260
    void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
2261
2262
    // functions to check state
2263
    _LIBCPP_INLINE_VISIBILITY
2264
    bool valid() const _NOEXCEPT {return __state_ != nullptr;}
2265
2266
    _LIBCPP_INLINE_VISIBILITY
2267
    void wait() const {__state_->wait();}
2268
    template <class _Rep, class _Period>
2269
        _LIBCPP_INLINE_VISIBILITY
2270
        future_status
2271
        wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2272
            {return __state_->wait_for(__rel_time);}
2273
    template <class _Clock, class _Duration>
2274
        _LIBCPP_INLINE_VISIBILITY
2275
        future_status
2276
        wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2277
            {return __state_->wait_until(__abs_time);}
2278
};
2279
2280
template <class _Rp>
2281
shared_future<_Rp>::~shared_future()
2282
{
2283
    if (__state_)
2284
        __state_->__release_shared();
2285
}
2286
2287
template <class _Rp>
2288
shared_future<_Rp>&
2289
shared_future<_Rp>::operator=(const shared_future& __rhs) _NOEXCEPT
2290
{
2291
    if (__rhs.__state_)
2292
        __rhs.__state_->__add_shared();
2293
    if (__state_)
2294
        __state_->__release_shared();
2295
    __state_ = __rhs.__state_;
2296
    return *this;
2297
}
2298
2299
template <class _Rp>
2300
class _LIBCPP_TEMPLATE_VIS shared_future<_Rp&>
2301
{
2302
    __assoc_state<_Rp&>* __state_;
2303
2304
public:
2305
    _LIBCPP_INLINE_VISIBILITY
2306
    shared_future() _NOEXCEPT : __state_(nullptr) {}
2307
    _LIBCPP_INLINE_VISIBILITY
2308
    shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2309
        {if (__state_) __state_->__add_shared();}
2310
    _LIBCPP_INLINE_VISIBILITY
2311
    shared_future(future<_Rp&>&& __f) _NOEXCEPT : __state_(__f.__state_)
2312
        {__f.__state_ = nullptr;}
2313
    _LIBCPP_INLINE_VISIBILITY
2314
    shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
2315
        {__rhs.__state_ = nullptr;}
2316
    ~shared_future();
2317
    shared_future& operator=(const shared_future& __rhs);
2318
    _LIBCPP_INLINE_VISIBILITY
2319
    shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
2320
        {
2321
            shared_future(_VSTD::move(__rhs)).swap(*this);
2322
            return *this;
2323
        }
2324
2325
    // retrieving the value
2326
    _LIBCPP_INLINE_VISIBILITY
2327
    _Rp& get() const {return __state_->copy();}
2328
2329
    _LIBCPP_INLINE_VISIBILITY
2330
    void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
2331
2332
    // functions to check state
2333
    _LIBCPP_INLINE_VISIBILITY
2334
    bool valid() const _NOEXCEPT {return __state_ != nullptr;}
2335
2336
    _LIBCPP_INLINE_VISIBILITY
2337
    void wait() const {__state_->wait();}
2338
    template <class _Rep, class _Period>
2339
        _LIBCPP_INLINE_VISIBILITY
2340
        future_status
2341
        wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2342
            {return __state_->wait_for(__rel_time);}
2343
    template <class _Clock, class _Duration>
2344
        _LIBCPP_INLINE_VISIBILITY
2345
        future_status
2346
        wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2347
            {return __state_->wait_until(__abs_time);}
2348
};
2349
2350
template <class _Rp>
2351
shared_future<_Rp&>::~shared_future()
2352
{
2353
    if (__state_)
2354
        __state_->__release_shared();
2355
}
2356
2357
template <class _Rp>
2358
shared_future<_Rp&>&
2359
shared_future<_Rp&>::operator=(const shared_future& __rhs)
2360
{
2361
    if (__rhs.__state_)
2362
        __rhs.__state_->__add_shared();
2363
    if (__state_)
2364
        __state_->__release_shared();
2365
    __state_ = __rhs.__state_;
2366
    return *this;
2367
}
2368
2369
template <>
2370
class _LIBCPP_TYPE_VIS _LIBCPP_AVAILABILITY_FUTURE shared_future<void>
2371
{
2372
    __assoc_sub_state* __state_;
2373
2374
public:
2375
    _LIBCPP_INLINE_VISIBILITY
2376
0
    shared_future() _NOEXCEPT : __state_(nullptr) {}
2377
    _LIBCPP_INLINE_VISIBILITY
2378
    shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2379
0
        {if (__state_) __state_->__add_shared();}
2380
    _LIBCPP_INLINE_VISIBILITY
2381
    shared_future(future<void>&& __f) _NOEXCEPT : __state_(__f.__state_)
2382
0
        {__f.__state_ = nullptr;}
2383
    _LIBCPP_INLINE_VISIBILITY
2384
    shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
2385
0
        {__rhs.__state_ = nullptr;}
2386
    ~shared_future();
2387
    shared_future& operator=(const shared_future& __rhs);
2388
    _LIBCPP_INLINE_VISIBILITY
2389
    shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
2390
0
        {
2391
0
            shared_future(_VSTD::move(__rhs)).swap(*this);
2392
0
            return *this;
2393
0
        }
2394
2395
    // retrieving the value
2396
    _LIBCPP_INLINE_VISIBILITY
2397
0
    void get() const {__state_->copy();}
2398
2399
    _LIBCPP_INLINE_VISIBILITY
2400
0
    void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
2401
2402
    // functions to check state
2403
    _LIBCPP_INLINE_VISIBILITY
2404
0
    bool valid() const _NOEXCEPT {return __state_ != nullptr;}
2405
2406
    _LIBCPP_INLINE_VISIBILITY
2407
0
    void wait() const {__state_->wait();}
2408
    template <class _Rep, class _Period>
2409
        _LIBCPP_INLINE_VISIBILITY
2410
        future_status
2411
        wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2412
            {return __state_->wait_for(__rel_time);}
2413
    template <class _Clock, class _Duration>
2414
        _LIBCPP_INLINE_VISIBILITY
2415
        future_status
2416
        wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2417
            {return __state_->wait_until(__abs_time);}
2418
};
2419
2420
template <class _Rp>
2421
inline _LIBCPP_INLINE_VISIBILITY
2422
void
2423
swap(shared_future<_Rp>& __x, shared_future<_Rp>& __y) _NOEXCEPT
2424
{
2425
    __x.swap(__y);
2426
}
2427
2428
template <class _Rp>
2429
inline
2430
shared_future<_Rp>
2431
future<_Rp>::share() _NOEXCEPT
2432
{
2433
    return shared_future<_Rp>(_VSTD::move(*this));
2434
}
2435
2436
template <class _Rp>
2437
inline
2438
shared_future<_Rp&>
2439
future<_Rp&>::share() _NOEXCEPT
2440
{
2441
    return shared_future<_Rp&>(_VSTD::move(*this));
2442
}
2443
2444
inline
2445
shared_future<void>
2446
future<void>::share() _NOEXCEPT
2447
0
{
2448
0
    return shared_future<void>(_VSTD::move(*this));
2449
0
}
2450
2451
_LIBCPP_END_NAMESPACE_STD
2452
2453
#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 17
2454
#  include <chrono>
2455
#endif
2456
2457
#endif // _LIBCPP_FUTURE