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