blob: b17394571b81d0cf670556318577c103b6d35d37 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===--------------------------- future -----------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_FUTURE
11#define _LIBCPP_FUTURE
12
13/*
14 future synopsis
15
16namespace std
17{
18
19enum class future_errc
20{
Howard Hinnantf2fd64d2013-09-14 18:20:10 +000021 future_already_retrieved = 1,
Howard Hinnantc51e1022010-05-11 19:42:16 +000022 promise_already_satisfied,
Howard Hinnantf2fd64d2013-09-14 18:20:10 +000023 no_state,
24 broken_promise
Howard Hinnantc51e1022010-05-11 19:42:16 +000025};
26
27enum class launch
28{
Howard Hinnante3df4ea2010-11-23 18:33:54 +000029 async = 1,
30 deferred = 2,
31 any = async | deferred
Howard Hinnantc51e1022010-05-11 19:42:16 +000032};
33
34enum class future_status
35{
36 ready,
37 timeout,
38 deferred
39};
40
41template <> struct is_error_code_enum<future_errc> : public true_type { };
Howard Hinnant22448042012-07-21 17:46:55 +000042error_code make_error_code(future_errc e) noexcept;
43error_condition make_error_condition(future_errc e) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000044
Howard Hinnant22448042012-07-21 17:46:55 +000045const error_category& future_category() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000046
47class future_error
48 : public logic_error
49{
50public:
51 future_error(error_code ec); // exposition only
Marshall Clowd4a669c2016-11-14 18:56:24 +000052 explicit future_error(future_errc); // C++17
Howard Hinnant22448042012-07-21 17:46:55 +000053 const error_code& code() const noexcept;
54 const char* what() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000055};
56
57template <class R>
58class promise
59{
60public:
61 promise();
62 template <class Allocator>
63 promise(allocator_arg_t, const Allocator& a);
Howard Hinnant22448042012-07-21 17:46:55 +000064 promise(promise&& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000065 promise(const promise& rhs) = delete;
66 ~promise();
67
68 // assignment
Howard Hinnant22448042012-07-21 17:46:55 +000069 promise& operator=(promise&& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000070 promise& operator=(const promise& rhs) = delete;
Howard Hinnant22448042012-07-21 17:46:55 +000071 void swap(promise& other) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000072
73 // retrieving the result
74 future<R> get_future();
75
76 // setting the result
77 void set_value(const R& r);
78 void set_value(R&& r);
79 void set_exception(exception_ptr p);
80
81 // setting the result with deferred notification
82 void set_value_at_thread_exit(const R& r);
83 void set_value_at_thread_exit(R&& r);
84 void set_exception_at_thread_exit(exception_ptr p);
85};
86
87template <class R>
88class promise<R&>
89{
90public:
91 promise();
92 template <class Allocator>
93 promise(allocator_arg_t, const Allocator& a);
Howard Hinnant22448042012-07-21 17:46:55 +000094 promise(promise&& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000095 promise(const promise& rhs) = delete;
96 ~promise();
97
98 // assignment
Howard Hinnant22448042012-07-21 17:46:55 +000099 promise& operator=(promise&& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000100 promise& operator=(const promise& rhs) = delete;
Howard Hinnant22448042012-07-21 17:46:55 +0000101 void swap(promise& other) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000102
103 // retrieving the result
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000104 future<R&> get_future();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000105
106 // setting the result
107 void set_value(R& r);
108 void set_exception(exception_ptr p);
109
110 // setting the result with deferred notification
111 void set_value_at_thread_exit(R&);
112 void set_exception_at_thread_exit(exception_ptr p);
113};
114
115template <>
116class promise<void>
117{
118public:
119 promise();
120 template <class Allocator>
121 promise(allocator_arg_t, const Allocator& a);
Howard Hinnant22448042012-07-21 17:46:55 +0000122 promise(promise&& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000123 promise(const promise& rhs) = delete;
124 ~promise();
125
126 // assignment
Howard Hinnant22448042012-07-21 17:46:55 +0000127 promise& operator=(promise&& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000128 promise& operator=(const promise& rhs) = delete;
Howard Hinnant22448042012-07-21 17:46:55 +0000129 void swap(promise& other) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000130
131 // retrieving the result
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000132 future<void> get_future();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000133
134 // setting the result
135 void set_value();
136 void set_exception(exception_ptr p);
137
138 // setting the result with deferred notification
139 void set_value_at_thread_exit();
140 void set_exception_at_thread_exit(exception_ptr p);
141};
142
Howard Hinnant22448042012-07-21 17:46:55 +0000143template <class R> void swap(promise<R>& x, promise<R>& y) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000144
145template <class R, class Alloc>
146 struct uses_allocator<promise<R>, Alloc> : public true_type {};
147
148template <class R>
149class future
150{
151public:
Howard Hinnant22448042012-07-21 17:46:55 +0000152 future() noexcept;
153 future(future&&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000154 future(const future& rhs) = delete;
155 ~future();
156 future& operator=(const future& rhs) = delete;
Howard Hinnant22448042012-07-21 17:46:55 +0000157 future& operator=(future&&) noexcept;
Marshall Clow597881d2017-01-25 20:14:03 +0000158 shared_future<R> share() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000159
160 // retrieving the value
161 R get();
162
163 // functions to check state
Howard Hinnant22448042012-07-21 17:46:55 +0000164 bool valid() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000165
166 void wait() const;
167 template <class Rep, class Period>
168 future_status
169 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
170 template <class Clock, class Duration>
171 future_status
172 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
173};
174
175template <class R>
176class future<R&>
177{
178public:
Howard Hinnant22448042012-07-21 17:46:55 +0000179 future() noexcept;
180 future(future&&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000181 future(const future& rhs) = delete;
182 ~future();
183 future& operator=(const future& rhs) = delete;
Howard Hinnant22448042012-07-21 17:46:55 +0000184 future& operator=(future&&) noexcept;
Marshall Clow79e02112017-01-24 23:28:25 +0000185 shared_future<R&> share() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000186
187 // retrieving the value
188 R& get();
189
190 // functions to check state
Howard Hinnant22448042012-07-21 17:46:55 +0000191 bool valid() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000192
193 void wait() const;
194 template <class Rep, class Period>
195 future_status
196 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
197 template <class Clock, class Duration>
198 future_status
199 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
200};
201
202template <>
203class future<void>
204{
205public:
Howard Hinnant22448042012-07-21 17:46:55 +0000206 future() noexcept;
207 future(future&&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000208 future(const future& rhs) = delete;
209 ~future();
210 future& operator=(const future& rhs) = delete;
Howard Hinnant22448042012-07-21 17:46:55 +0000211 future& operator=(future&&) noexcept;
Marshall Clow79e02112017-01-24 23:28:25 +0000212 shared_future<void> share() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000213
214 // retrieving the value
215 void get();
216
217 // functions to check state
Howard Hinnant22448042012-07-21 17:46:55 +0000218 bool valid() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000219
220 void wait() const;
221 template <class Rep, class Period>
222 future_status
223 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
224 template <class Clock, class Duration>
225 future_status
226 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
227};
228
229template <class R>
230class shared_future
231{
232public:
Howard Hinnant22448042012-07-21 17:46:55 +0000233 shared_future() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000234 shared_future(const shared_future& rhs);
Howard Hinnant22448042012-07-21 17:46:55 +0000235 shared_future(future<R>&&) noexcept;
236 shared_future(shared_future&& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000237 ~shared_future();
238 shared_future& operator=(const shared_future& rhs);
Howard Hinnant22448042012-07-21 17:46:55 +0000239 shared_future& operator=(shared_future&& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000240
241 // retrieving the value
242 const R& get() const;
243
244 // functions to check state
Howard Hinnant22448042012-07-21 17:46:55 +0000245 bool valid() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000246
247 void wait() const;
248 template <class Rep, class Period>
249 future_status
250 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
251 template <class Clock, class Duration>
252 future_status
253 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
254};
255
256template <class R>
257class shared_future<R&>
258{
259public:
Howard Hinnant22448042012-07-21 17:46:55 +0000260 shared_future() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000261 shared_future(const shared_future& rhs);
Howard Hinnant22448042012-07-21 17:46:55 +0000262 shared_future(future<R&>&&) noexcept;
263 shared_future(shared_future&& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000264 ~shared_future();
265 shared_future& operator=(const shared_future& rhs);
Howard Hinnant22448042012-07-21 17:46:55 +0000266 shared_future& operator=(shared_future&& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000267
268 // retrieving the value
269 R& get() const;
270
271 // functions to check state
Howard Hinnant22448042012-07-21 17:46:55 +0000272 bool valid() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000273
274 void wait() const;
275 template <class Rep, class Period>
276 future_status
277 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
278 template <class Clock, class Duration>
279 future_status
280 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
281};
282
283template <>
284class shared_future<void>
285{
286public:
Howard Hinnant22448042012-07-21 17:46:55 +0000287 shared_future() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000288 shared_future(const shared_future& rhs);
Howard Hinnant22448042012-07-21 17:46:55 +0000289 shared_future(future<void>&&) noexcept;
290 shared_future(shared_future&& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000291 ~shared_future();
292 shared_future& operator=(const shared_future& rhs);
Howard Hinnant22448042012-07-21 17:46:55 +0000293 shared_future& operator=(shared_future&& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000294
295 // retrieving the value
296 void get() const;
297
298 // functions to check state
Howard Hinnant22448042012-07-21 17:46:55 +0000299 bool valid() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000300
301 void wait() const;
302 template <class Rep, class Period>
303 future_status
304 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
305 template <class Clock, class Duration>
306 future_status
307 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
308};
309
Howard Hinnantc51e1022010-05-11 19:42:16 +0000310template <class F, class... Args>
Howard Hinnant15579d22013-09-21 18:17:23 +0000311 future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000312 async(F&& f, Args&&... args);
313
314template <class F, class... Args>
Howard Hinnant15579d22013-09-21 18:17:23 +0000315 future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000316 async(launch policy, F&& f, Args&&... args);
317
Howard Hinnantc566dc32010-05-11 21:36:01 +0000318template <class> class packaged_task; // undefined
Howard Hinnantc51e1022010-05-11 19:42:16 +0000319
320template <class R, class... ArgTypes>
321class packaged_task<R(ArgTypes...)>
322{
323public:
Eric Fiselier17f39ed2016-06-01 21:05:53 +0000324 typedef R result_type; // extension
Howard Hinnantc51e1022010-05-11 19:42:16 +0000325
326 // construction and destruction
Howard Hinnant22448042012-07-21 17:46:55 +0000327 packaged_task() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000328 template <class F>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000329 explicit packaged_task(F&& f);
Marshall Clowa0a057e2017-11-27 20:47:54 +0000330 template <class F, class Allocator>
331 packaged_task(allocator_arg_t, const Allocator& a, F&& f);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000332 ~packaged_task();
333
334 // no copy
Howard Hinnantab500072012-07-21 19:34:12 +0000335 packaged_task(const packaged_task&) = delete;
336 packaged_task& operator=(const packaged_task&) = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000337
338 // move support
Howard Hinnant22448042012-07-21 17:46:55 +0000339 packaged_task(packaged_task&& other) noexcept;
340 packaged_task& operator=(packaged_task&& other) noexcept;
341 void swap(packaged_task& other) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000342
Howard Hinnant22448042012-07-21 17:46:55 +0000343 bool valid() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000344
345 // result retrieval
346 future<R> get_future();
347
348 // execution
349 void operator()(ArgTypes... );
350 void make_ready_at_thread_exit(ArgTypes...);
351
352 void reset();
353};
354
355template <class R>
Howard Hinnant22448042012-07-21 17:46:55 +0000356 void swap(packaged_task<R(ArgTypes...)&, packaged_task<R(ArgTypes...)>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000357
Marshall Clowa0a057e2017-11-27 20:47:54 +0000358template <class R, class Alloc> struct uses_allocator<packaged_task<R>, Alloc>;
359
Howard Hinnantc51e1022010-05-11 19:42:16 +0000360} // std
361
362*/
363
364#include <__config>
Louis Dionne73912b22020-11-04 15:01:25 -0500365#include <__availability>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000366#include <system_error>
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000367#include <memory>
368#include <chrono>
369#include <exception>
Howard Hinnante6a10852010-09-03 21:46:37 +0000370#include <mutex>
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000371#include <thread>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000372
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000373#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000374#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000375#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000376
Jonathan Roelofs067218a2014-09-05 20:28:44 +0000377#ifdef _LIBCPP_HAS_NO_THREADS
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000378#error <future> is not supported on this single threaded system
379#else // !_LIBCPP_HAS_NO_THREADS
380
Howard Hinnantc51e1022010-05-11 19:42:16 +0000381_LIBCPP_BEGIN_NAMESPACE_STD
382
383//enum class future_errc
Howard Hinnant09bc0c92011-12-02 19:36:40 +0000384_LIBCPP_DECLARE_STRONG_ENUM(future_errc)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000385{
Howard Hinnantf2fd64d2013-09-14 18:20:10 +0000386 future_already_retrieved = 1,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000387 promise_already_satisfied,
Howard Hinnantf2fd64d2013-09-14 18:20:10 +0000388 no_state,
389 broken_promise
Howard Hinnantc51e1022010-05-11 19:42:16 +0000390};
Howard Hinnant09bc0c92011-12-02 19:36:40 +0000391_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_errc)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000392
Howard Hinnant684902d2010-09-22 14:16:26 +0000393template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000394struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<future_errc> : public true_type {};
Howard Hinnant96803d92010-08-25 17:32:05 +0000395
Howard Hinnant09bc0c92011-12-02 19:36:40 +0000396#ifdef _LIBCPP_HAS_NO_STRONG_ENUMS
397template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000398struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<future_errc::__lx> : public true_type { };
Howard Hinnant09bc0c92011-12-02 19:36:40 +0000399#endif
400
Howard Hinnantc51e1022010-05-11 19:42:16 +0000401//enum class launch
Howard Hinnant09bc0c92011-12-02 19:36:40 +0000402_LIBCPP_DECLARE_STRONG_ENUM(launch)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000403{
Howard Hinnante3df4ea2010-11-23 18:33:54 +0000404 async = 1,
405 deferred = 2,
406 any = async | deferred
Howard Hinnantc51e1022010-05-11 19:42:16 +0000407};
Howard Hinnant09bc0c92011-12-02 19:36:40 +0000408_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(launch)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000409
Howard Hinnantda760a82013-06-29 18:38:17 +0000410#ifndef _LIBCPP_HAS_NO_STRONG_ENUMS
411
Howard Hinnantda760a82013-06-29 18:38:17 +0000412typedef underlying_type<launch>::type __launch_underlying_type;
Howard Hinnantda760a82013-06-29 18:38:17 +0000413
414inline _LIBCPP_INLINE_VISIBILITY
415_LIBCPP_CONSTEXPR
416launch
417operator&(launch __x, launch __y)
418{
419 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) &
420 static_cast<__launch_underlying_type>(__y));
421}
422
423inline _LIBCPP_INLINE_VISIBILITY
424_LIBCPP_CONSTEXPR
425launch
426operator|(launch __x, launch __y)
427{
428 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) |
429 static_cast<__launch_underlying_type>(__y));
430}
431
432inline _LIBCPP_INLINE_VISIBILITY
433_LIBCPP_CONSTEXPR
434launch
435operator^(launch __x, launch __y)
436{
437 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) ^
438 static_cast<__launch_underlying_type>(__y));
439}
440
441inline _LIBCPP_INLINE_VISIBILITY
442_LIBCPP_CONSTEXPR
443launch
444operator~(launch __x)
445{
Howard Hinnant373d7602013-07-02 18:01:41 +0000446 return static_cast<launch>(~static_cast<__launch_underlying_type>(__x) & 3);
Howard Hinnantda760a82013-06-29 18:38:17 +0000447}
448
449inline _LIBCPP_INLINE_VISIBILITY
450launch&
451operator&=(launch& __x, launch __y)
452{
453 __x = __x & __y; return __x;
454}
455
456inline _LIBCPP_INLINE_VISIBILITY
457launch&
458operator|=(launch& __x, launch __y)
459{
460 __x = __x | __y; return __x;
461}
462
463inline _LIBCPP_INLINE_VISIBILITY
464launch&
465operator^=(launch& __x, launch __y)
466{
467 __x = __x ^ __y; return __x;
468}
469
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400470#endif // !_LIBCPP_HAS_NO_STRONG_ENUMS
Howard Hinnantda760a82013-06-29 18:38:17 +0000471
Howard Hinnantc51e1022010-05-11 19:42:16 +0000472//enum class future_status
Howard Hinnant09bc0c92011-12-02 19:36:40 +0000473_LIBCPP_DECLARE_STRONG_ENUM(future_status)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000474{
Howard Hinnantc51e1022010-05-11 19:42:16 +0000475 ready,
476 timeout,
477 deferred
478};
Howard Hinnant09bc0c92011-12-02 19:36:40 +0000479_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_status)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000480
Howard Hinnant8331b762013-03-06 23:30:19 +0000481_LIBCPP_FUNC_VIS
Howard Hinnant22448042012-07-21 17:46:55 +0000482const error_category& future_category() _NOEXCEPT;
Howard Hinnant96803d92010-08-25 17:32:05 +0000483
484inline _LIBCPP_INLINE_VISIBILITY
485error_code
Howard Hinnant22448042012-07-21 17:46:55 +0000486make_error_code(future_errc __e) _NOEXCEPT
Howard Hinnant96803d92010-08-25 17:32:05 +0000487{
488 return error_code(static_cast<int>(__e), future_category());
489}
490
491inline _LIBCPP_INLINE_VISIBILITY
492error_condition
Howard Hinnant22448042012-07-21 17:46:55 +0000493make_error_condition(future_errc __e) _NOEXCEPT
Howard Hinnant96803d92010-08-25 17:32:05 +0000494{
495 return error_condition(static_cast<int>(__e), future_category());
496}
497
Mehdi Amini228053d2017-05-04 17:08:54 +0000498class _LIBCPP_EXCEPTION_ABI _LIBCPP_AVAILABILITY_FUTURE_ERROR future_error
Howard Hinnant96803d92010-08-25 17:32:05 +0000499 : public logic_error
500{
501 error_code __ec_;
502public:
503 future_error(error_code __ec);
Marek Kurdejf3197922021-04-01 08:29:55 +0200504
Howard Hinnant684902d2010-09-22 14:16:26 +0000505 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +0000506 const error_code& code() const _NOEXCEPT {return __ec_;}
Howard Hinnant0dcdf022011-07-07 21:03:52 +0000507
Dimitry Andric47269ce2020-03-13 19:36:26 +0100508 future_error(const future_error&) _NOEXCEPT = default;
Howard Hinnant0dcdf022011-07-07 21:03:52 +0000509 virtual ~future_error() _NOEXCEPT;
Howard Hinnant96803d92010-08-25 17:32:05 +0000510};
511
Louis Dionne16fe2952018-07-11 23:14:33 +0000512_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +0000513#ifndef _LIBCPP_NO_EXCEPTIONS
514_LIBCPP_AVAILABILITY_FUTURE_ERROR
515#endif
Eric Fiselier43641592015-10-02 21:25:15 +0000516void __throw_future_error(future_errc _Ev)
Marshall Clow7b3742d2015-09-03 15:11:32 +0000517{
518#ifndef _LIBCPP_NO_EXCEPTIONS
519 throw future_error(make_error_code(_Ev));
520#else
Eric Fiselierb2c89f22016-12-24 01:56:25 +0000521 ((void)_Ev);
Marshall Clow8fea1612016-08-25 15:09:01 +0000522 _VSTD::abort();
Marshall Clow7b3742d2015-09-03 15:11:32 +0000523#endif
524}
525
Mehdi Amini228053d2017-05-04 17:08:54 +0000526class _LIBCPP_TYPE_VIS _LIBCPP_AVAILABILITY_FUTURE __assoc_sub_state
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000527 : public __shared_count
528{
529protected:
530 exception_ptr __exception_;
531 mutable mutex __mut_;
532 mutable condition_variable __cv_;
533 unsigned __state_;
534
Howard Hinnant719bda32011-05-28 14:41:13 +0000535 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnantccdd2032010-08-30 18:46:21 +0000536 void __sub_wait(unique_lock<mutex>& __lk);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000537public:
538 enum
539 {
540 __constructed = 1,
541 __future_attached = 2,
542 ready = 4,
543 deferred = 8
544 };
545
Howard Hinnant684902d2010-09-22 14:16:26 +0000546 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000547 __assoc_sub_state() : __state_(0) {}
548
Howard Hinnant684902d2010-09-22 14:16:26 +0000549 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000550 bool __has_value() const
551 {return (__state_ & __constructed) || (__exception_ != nullptr);}
552
Howard Hinnant684902d2010-09-22 14:16:26 +0000553 _LIBCPP_INLINE_VISIBILITY
Louis Dionne2c0d2262018-08-24 14:00:59 +0000554 void __attach_future() {
Howard Hinnante667ecc2013-01-14 20:01:24 +0000555 lock_guard<mutex> __lk(__mut_);
Louis Dionne2c0d2262018-08-24 14:00:59 +0000556 bool __has_future_attached = (__state_ & __future_attached) != 0;
557 if (__has_future_attached)
558 __throw_future_error(future_errc::future_already_retrieved);
559 this->__add_shared();
Howard Hinnante667ecc2013-01-14 20:01:24 +0000560 __state_ |= __future_attached;
561 }
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000562
Howard Hinnant684902d2010-09-22 14:16:26 +0000563 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantccdd2032010-08-30 18:46:21 +0000564 void __set_deferred() {__state_ |= deferred;}
565
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000566 void __make_ready();
Howard Hinnant684902d2010-09-22 14:16:26 +0000567 _LIBCPP_INLINE_VISIBILITY
Marshall Clowdfcbb432013-10-13 01:02:45 +0000568 bool __is_ready() const {return (__state_ & ready) != 0;}
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000569
570 void set_value();
571 void set_value_at_thread_exit();
572
573 void set_exception(exception_ptr __p);
574 void set_exception_at_thread_exit(exception_ptr __p);
575
576 void copy();
577
Howard Hinnantccdd2032010-08-30 18:46:21 +0000578 void wait();
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000579 template <class _Rep, class _Period>
580 future_status
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000581 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000582 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const;
583 template <class _Clock, class _Duration>
Shoaib Meenai55f3a462017-03-02 03:22:18 +0000584 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000585 future_status
586 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const;
Howard Hinnantccdd2032010-08-30 18:46:21 +0000587
588 virtual void __execute();
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000589};
590
Howard Hinnantf4712b92010-08-28 21:01:06 +0000591template <class _Clock, class _Duration>
592future_status
593__assoc_sub_state::wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
594{
595 unique_lock<mutex> __lk(__mut_);
Howard Hinnantccdd2032010-08-30 18:46:21 +0000596 if (__state_ & deferred)
597 return future_status::deferred;
598 while (!(__state_ & ready) && _Clock::now() < __abs_time)
Howard Hinnantf4712b92010-08-28 21:01:06 +0000599 __cv_.wait_until(__lk, __abs_time);
600 if (__state_ & ready)
601 return future_status::ready;
Howard Hinnantf4712b92010-08-28 21:01:06 +0000602 return future_status::timeout;
603}
604
605template <class _Rep, class _Period>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000606inline
Howard Hinnantf4712b92010-08-28 21:01:06 +0000607future_status
608__assoc_sub_state::wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
609{
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000610 return wait_until(chrono::steady_clock::now() + __rel_time);
Howard Hinnantf4712b92010-08-28 21:01:06 +0000611}
612
Howard Hinnantc834c512011-11-29 18:15:50 +0000613template <class _Rp>
Louis Dionne3b967d02019-12-10 18:00:42 -0500614class _LIBCPP_AVAILABILITY_FUTURE _LIBCPP_HIDDEN __assoc_state
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000615 : public __assoc_sub_state
616{
617 typedef __assoc_sub_state base;
Howard Hinnantc834c512011-11-29 18:15:50 +0000618 typedef typename aligned_storage<sizeof(_Rp), alignment_of<_Rp>::value>::type _Up;
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000619protected:
Howard Hinnantc834c512011-11-29 18:15:50 +0000620 _Up __value_;
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000621
Howard Hinnant719bda32011-05-28 14:41:13 +0000622 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000623public:
624
625 template <class _Arg>
Louis Dionne7b844362020-07-30 09:42:23 -0400626 void set_value(_Arg&& __arg);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000627
628 template <class _Arg>
Louis Dionne7b844362020-07-30 09:42:23 -0400629 void set_value_at_thread_exit(_Arg&& __arg);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000630
Howard Hinnantc834c512011-11-29 18:15:50 +0000631 _Rp move();
632 typename add_lvalue_reference<_Rp>::type copy();
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000633};
634
Howard Hinnantc834c512011-11-29 18:15:50 +0000635template <class _Rp>
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000636void
Howard Hinnantc834c512011-11-29 18:15:50 +0000637__assoc_state<_Rp>::__on_zero_shared() _NOEXCEPT
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000638{
639 if (this->__state_ & base::__constructed)
Howard Hinnantc834c512011-11-29 18:15:50 +0000640 reinterpret_cast<_Rp*>(&__value_)->~_Rp();
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000641 delete this;
642}
643
Howard Hinnantc834c512011-11-29 18:15:50 +0000644template <class _Rp>
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000645template <class _Arg>
Mehdi Amini228053d2017-05-04 17:08:54 +0000646_LIBCPP_AVAILABILITY_FUTURE
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000647void
Howard Hinnantc834c512011-11-29 18:15:50 +0000648__assoc_state<_Rp>::set_value(_Arg&& __arg)
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000649{
650 unique_lock<mutex> __lk(this->__mut_);
651 if (this->__has_value())
Eric Fiselier43641592015-10-02 21:25:15 +0000652 __throw_future_error(future_errc::promise_already_satisfied);
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -0500653 ::new ((void*)&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000654 this->__state_ |= base::__constructed | base::ready;
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000655 __cv_.notify_all();
656}
657
Howard Hinnantc834c512011-11-29 18:15:50 +0000658template <class _Rp>
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000659template <class _Arg>
660void
Howard Hinnantc834c512011-11-29 18:15:50 +0000661__assoc_state<_Rp>::set_value_at_thread_exit(_Arg&& __arg)
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000662{
663 unique_lock<mutex> __lk(this->__mut_);
664 if (this->__has_value())
Eric Fiselier43641592015-10-02 21:25:15 +0000665 __throw_future_error(future_errc::promise_already_satisfied);
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -0500666 ::new ((void*)&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000667 this->__state_ |= base::__constructed;
Howard Hinnant15d55052010-10-14 19:18:04 +0000668 __thread_local_data()->__make_ready_at_thread_exit(this);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000669}
670
Howard Hinnantc834c512011-11-29 18:15:50 +0000671template <class _Rp>
672_Rp
673__assoc_state<_Rp>::move()
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000674{
675 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnantccdd2032010-08-30 18:46:21 +0000676 this->__sub_wait(__lk);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000677 if (this->__exception_ != nullptr)
678 rethrow_exception(this->__exception_);
Howard Hinnantc834c512011-11-29 18:15:50 +0000679 return _VSTD::move(*reinterpret_cast<_Rp*>(&__value_));
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000680}
681
Howard Hinnantc834c512011-11-29 18:15:50 +0000682template <class _Rp>
683typename add_lvalue_reference<_Rp>::type
684__assoc_state<_Rp>::copy()
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000685{
686 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnantccdd2032010-08-30 18:46:21 +0000687 this->__sub_wait(__lk);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000688 if (this->__exception_ != nullptr)
689 rethrow_exception(this->__exception_);
Howard Hinnantc834c512011-11-29 18:15:50 +0000690 return *reinterpret_cast<_Rp*>(&__value_);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000691}
692
Howard Hinnantc834c512011-11-29 18:15:50 +0000693template <class _Rp>
Mehdi Amini228053d2017-05-04 17:08:54 +0000694class _LIBCPP_AVAILABILITY_FUTURE __assoc_state<_Rp&>
Howard Hinnantf4712b92010-08-28 21:01:06 +0000695 : public __assoc_sub_state
696{
697 typedef __assoc_sub_state base;
Howard Hinnantc834c512011-11-29 18:15:50 +0000698 typedef _Rp* _Up;
Howard Hinnantf4712b92010-08-28 21:01:06 +0000699protected:
Howard Hinnantc834c512011-11-29 18:15:50 +0000700 _Up __value_;
Howard Hinnantf4712b92010-08-28 21:01:06 +0000701
Howard Hinnant719bda32011-05-28 14:41:13 +0000702 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnantf4712b92010-08-28 21:01:06 +0000703public:
704
Howard Hinnantc834c512011-11-29 18:15:50 +0000705 void set_value(_Rp& __arg);
706 void set_value_at_thread_exit(_Rp& __arg);
Howard Hinnantf4712b92010-08-28 21:01:06 +0000707
Howard Hinnantc834c512011-11-29 18:15:50 +0000708 _Rp& copy();
Howard Hinnantf4712b92010-08-28 21:01:06 +0000709};
710
Howard Hinnantc834c512011-11-29 18:15:50 +0000711template <class _Rp>
Howard Hinnantf4712b92010-08-28 21:01:06 +0000712void
Howard Hinnantc834c512011-11-29 18:15:50 +0000713__assoc_state<_Rp&>::__on_zero_shared() _NOEXCEPT
Howard Hinnantf4712b92010-08-28 21:01:06 +0000714{
715 delete this;
716}
717
Howard Hinnantc834c512011-11-29 18:15:50 +0000718template <class _Rp>
Howard Hinnantf4712b92010-08-28 21:01:06 +0000719void
Howard Hinnantc834c512011-11-29 18:15:50 +0000720__assoc_state<_Rp&>::set_value(_Rp& __arg)
Howard Hinnantf4712b92010-08-28 21:01:06 +0000721{
722 unique_lock<mutex> __lk(this->__mut_);
723 if (this->__has_value())
Eric Fiselier43641592015-10-02 21:25:15 +0000724 __throw_future_error(future_errc::promise_already_satisfied);
Howard Hinnant8bea6da2013-08-08 18:38:55 +0000725 __value_ = _VSTD::addressof(__arg);
Howard Hinnantf4712b92010-08-28 21:01:06 +0000726 this->__state_ |= base::__constructed | base::ready;
Howard Hinnantf4712b92010-08-28 21:01:06 +0000727 __cv_.notify_all();
728}
729
Howard Hinnantc834c512011-11-29 18:15:50 +0000730template <class _Rp>
Howard Hinnantf4712b92010-08-28 21:01:06 +0000731void
Howard Hinnantc834c512011-11-29 18:15:50 +0000732__assoc_state<_Rp&>::set_value_at_thread_exit(_Rp& __arg)
Howard Hinnantf4712b92010-08-28 21:01:06 +0000733{
734 unique_lock<mutex> __lk(this->__mut_);
735 if (this->__has_value())
Eric Fiselier43641592015-10-02 21:25:15 +0000736 __throw_future_error(future_errc::promise_already_satisfied);
Howard Hinnant8bea6da2013-08-08 18:38:55 +0000737 __value_ = _VSTD::addressof(__arg);
Howard Hinnantf4712b92010-08-28 21:01:06 +0000738 this->__state_ |= base::__constructed;
Howard Hinnant15d55052010-10-14 19:18:04 +0000739 __thread_local_data()->__make_ready_at_thread_exit(this);
Howard Hinnantf4712b92010-08-28 21:01:06 +0000740}
741
Howard Hinnantc834c512011-11-29 18:15:50 +0000742template <class _Rp>
743_Rp&
744__assoc_state<_Rp&>::copy()
Howard Hinnantf4712b92010-08-28 21:01:06 +0000745{
746 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnantccdd2032010-08-30 18:46:21 +0000747 this->__sub_wait(__lk);
Howard Hinnantf4712b92010-08-28 21:01:06 +0000748 if (this->__exception_ != nullptr)
749 rethrow_exception(this->__exception_);
750 return *__value_;
751}
752
Howard Hinnantc834c512011-11-29 18:15:50 +0000753template <class _Rp, class _Alloc>
Mehdi Amini228053d2017-05-04 17:08:54 +0000754class _LIBCPP_AVAILABILITY_FUTURE __assoc_state_alloc
Howard Hinnantc834c512011-11-29 18:15:50 +0000755 : public __assoc_state<_Rp>
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000756{
Howard Hinnantc834c512011-11-29 18:15:50 +0000757 typedef __assoc_state<_Rp> base;
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000758 _Alloc __alloc_;
759
Howard Hinnant719bda32011-05-28 14:41:13 +0000760 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000761public:
Howard Hinnant684902d2010-09-22 14:16:26 +0000762 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000763 explicit __assoc_state_alloc(const _Alloc& __a)
764 : __alloc_(__a) {}
765};
766
Howard Hinnantc834c512011-11-29 18:15:50 +0000767template <class _Rp, class _Alloc>
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000768void
Howard Hinnantc834c512011-11-29 18:15:50 +0000769__assoc_state_alloc<_Rp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000770{
771 if (this->__state_ & base::__constructed)
Howard Hinnant8bea6da2013-08-08 18:38:55 +0000772 reinterpret_cast<_Rp*>(_VSTD::addressof(this->__value_))->~_Rp();
Eric Fiselierf8898c82015-02-05 23:01:40 +0000773 typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
774 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier0d109272014-10-23 06:24:45 +0000775 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselierf8898c82015-02-05 23:01:40 +0000776 _Al __a(__alloc_);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000777 this->~__assoc_state_alloc();
Eric Fiselier0d109272014-10-23 06:24:45 +0000778 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000779}
780
Howard Hinnantc834c512011-11-29 18:15:50 +0000781template <class _Rp, class _Alloc>
Mehdi Amini228053d2017-05-04 17:08:54 +0000782class _LIBCPP_AVAILABILITY_FUTURE __assoc_state_alloc<_Rp&, _Alloc>
Howard Hinnantc834c512011-11-29 18:15:50 +0000783 : public __assoc_state<_Rp&>
Howard Hinnantf4712b92010-08-28 21:01:06 +0000784{
Howard Hinnantc834c512011-11-29 18:15:50 +0000785 typedef __assoc_state<_Rp&> base;
Howard Hinnantf4712b92010-08-28 21:01:06 +0000786 _Alloc __alloc_;
787
Howard Hinnant719bda32011-05-28 14:41:13 +0000788 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnantf4712b92010-08-28 21:01:06 +0000789public:
Howard Hinnant684902d2010-09-22 14:16:26 +0000790 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf4712b92010-08-28 21:01:06 +0000791 explicit __assoc_state_alloc(const _Alloc& __a)
792 : __alloc_(__a) {}
793};
794
Howard Hinnantc834c512011-11-29 18:15:50 +0000795template <class _Rp, class _Alloc>
Howard Hinnantf4712b92010-08-28 21:01:06 +0000796void
Howard Hinnantc834c512011-11-29 18:15:50 +0000797__assoc_state_alloc<_Rp&, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantf4712b92010-08-28 21:01:06 +0000798{
Eric Fiselierf8898c82015-02-05 23:01:40 +0000799 typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
800 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier0d109272014-10-23 06:24:45 +0000801 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselierf8898c82015-02-05 23:01:40 +0000802 _Al __a(__alloc_);
Howard Hinnantf4712b92010-08-28 21:01:06 +0000803 this->~__assoc_state_alloc();
Eric Fiselier0d109272014-10-23 06:24:45 +0000804 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantf4712b92010-08-28 21:01:06 +0000805}
806
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000807template <class _Alloc>
Mehdi Amini228053d2017-05-04 17:08:54 +0000808class _LIBCPP_AVAILABILITY_FUTURE __assoc_sub_state_alloc
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000809 : public __assoc_sub_state
810{
811 typedef __assoc_sub_state base;
812 _Alloc __alloc_;
813
Howard Hinnant719bda32011-05-28 14:41:13 +0000814 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000815public:
Howard Hinnant684902d2010-09-22 14:16:26 +0000816 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000817 explicit __assoc_sub_state_alloc(const _Alloc& __a)
818 : __alloc_(__a) {}
819};
820
821template <class _Alloc>
822void
Howard Hinnant719bda32011-05-28 14:41:13 +0000823__assoc_sub_state_alloc<_Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000824{
Eric Fiselierf8898c82015-02-05 23:01:40 +0000825 typedef typename __allocator_traits_rebind<_Alloc, __assoc_sub_state_alloc>::type _Al;
826 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier0d109272014-10-23 06:24:45 +0000827 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselierf8898c82015-02-05 23:01:40 +0000828 _Al __a(__alloc_);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000829 this->~__assoc_sub_state_alloc();
Eric Fiselier0d109272014-10-23 06:24:45 +0000830 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000831}
832
Howard Hinnantc834c512011-11-29 18:15:50 +0000833template <class _Rp, class _Fp>
Mehdi Amini228053d2017-05-04 17:08:54 +0000834class _LIBCPP_AVAILABILITY_FUTURE __deferred_assoc_state
Howard Hinnantc834c512011-11-29 18:15:50 +0000835 : public __assoc_state<_Rp>
Howard Hinnantccdd2032010-08-30 18:46:21 +0000836{
Howard Hinnantc834c512011-11-29 18:15:50 +0000837 typedef __assoc_state<_Rp> base;
Howard Hinnantccdd2032010-08-30 18:46:21 +0000838
Howard Hinnantc834c512011-11-29 18:15:50 +0000839 _Fp __func_;
Howard Hinnantccdd2032010-08-30 18:46:21 +0000840
841public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000842 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +0000843 explicit __deferred_assoc_state(_Fp&& __f);
Howard Hinnantccdd2032010-08-30 18:46:21 +0000844
845 virtual void __execute();
846};
847
Howard Hinnantc834c512011-11-29 18:15:50 +0000848template <class _Rp, class _Fp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000849inline
Howard Hinnantc834c512011-11-29 18:15:50 +0000850__deferred_assoc_state<_Rp, _Fp>::__deferred_assoc_state(_Fp&& __f)
851 : __func_(_VSTD::forward<_Fp>(__f))
Howard Hinnantccdd2032010-08-30 18:46:21 +0000852{
853 this->__set_deferred();
854}
855
Howard Hinnantc834c512011-11-29 18:15:50 +0000856template <class _Rp, class _Fp>
Howard Hinnantccdd2032010-08-30 18:46:21 +0000857void
Howard Hinnantc834c512011-11-29 18:15:50 +0000858__deferred_assoc_state<_Rp, _Fp>::__execute()
Howard Hinnantccdd2032010-08-30 18:46:21 +0000859{
860#ifndef _LIBCPP_NO_EXCEPTIONS
861 try
862 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400863#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantccdd2032010-08-30 18:46:21 +0000864 this->set_value(__func_());
865#ifndef _LIBCPP_NO_EXCEPTIONS
866 }
867 catch (...)
868 {
869 this->set_exception(current_exception());
870 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400871#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantccdd2032010-08-30 18:46:21 +0000872}
873
Howard Hinnantc834c512011-11-29 18:15:50 +0000874template <class _Fp>
Mehdi Amini228053d2017-05-04 17:08:54 +0000875class _LIBCPP_AVAILABILITY_FUTURE __deferred_assoc_state<void, _Fp>
Howard Hinnantccdd2032010-08-30 18:46:21 +0000876 : public __assoc_sub_state
877{
878 typedef __assoc_sub_state base;
879
Howard Hinnantc834c512011-11-29 18:15:50 +0000880 _Fp __func_;
Howard Hinnantccdd2032010-08-30 18:46:21 +0000881
882public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000883 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +0000884 explicit __deferred_assoc_state(_Fp&& __f);
Howard Hinnantccdd2032010-08-30 18:46:21 +0000885
886 virtual void __execute();
887};
888
Howard Hinnantc834c512011-11-29 18:15:50 +0000889template <class _Fp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000890inline
Howard Hinnantc834c512011-11-29 18:15:50 +0000891__deferred_assoc_state<void, _Fp>::__deferred_assoc_state(_Fp&& __f)
892 : __func_(_VSTD::forward<_Fp>(__f))
Howard Hinnantccdd2032010-08-30 18:46:21 +0000893{
894 this->__set_deferred();
895}
896
Howard Hinnantc834c512011-11-29 18:15:50 +0000897template <class _Fp>
Howard Hinnantccdd2032010-08-30 18:46:21 +0000898void
Howard Hinnantc834c512011-11-29 18:15:50 +0000899__deferred_assoc_state<void, _Fp>::__execute()
Howard Hinnantccdd2032010-08-30 18:46:21 +0000900{
901#ifndef _LIBCPP_NO_EXCEPTIONS
902 try
903 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400904#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantccdd2032010-08-30 18:46:21 +0000905 __func_();
906 this->set_value();
907#ifndef _LIBCPP_NO_EXCEPTIONS
908 }
909 catch (...)
910 {
911 this->set_exception(current_exception());
912 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400913#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantccdd2032010-08-30 18:46:21 +0000914}
915
Howard Hinnantc834c512011-11-29 18:15:50 +0000916template <class _Rp, class _Fp>
Mehdi Amini228053d2017-05-04 17:08:54 +0000917class _LIBCPP_AVAILABILITY_FUTURE __async_assoc_state
Howard Hinnantc834c512011-11-29 18:15:50 +0000918 : public __assoc_state<_Rp>
Howard Hinnant95cfd872011-05-19 15:05:04 +0000919{
Howard Hinnantc834c512011-11-29 18:15:50 +0000920 typedef __assoc_state<_Rp> base;
Howard Hinnant95cfd872011-05-19 15:05:04 +0000921
Howard Hinnantc834c512011-11-29 18:15:50 +0000922 _Fp __func_;
Howard Hinnant95cfd872011-05-19 15:05:04 +0000923
Howard Hinnant719bda32011-05-28 14:41:13 +0000924 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant95cfd872011-05-19 15:05:04 +0000925public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000926 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +0000927 explicit __async_assoc_state(_Fp&& __f);
Howard Hinnant95cfd872011-05-19 15:05:04 +0000928
929 virtual void __execute();
930};
931
Howard Hinnantc834c512011-11-29 18:15:50 +0000932template <class _Rp, class _Fp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000933inline
Howard Hinnantc834c512011-11-29 18:15:50 +0000934__async_assoc_state<_Rp, _Fp>::__async_assoc_state(_Fp&& __f)
935 : __func_(_VSTD::forward<_Fp>(__f))
Howard Hinnant95cfd872011-05-19 15:05:04 +0000936{
937}
938
Howard Hinnantc834c512011-11-29 18:15:50 +0000939template <class _Rp, class _Fp>
Howard Hinnant95cfd872011-05-19 15:05:04 +0000940void
Howard Hinnantc834c512011-11-29 18:15:50 +0000941__async_assoc_state<_Rp, _Fp>::__execute()
Howard Hinnant95cfd872011-05-19 15:05:04 +0000942{
943#ifndef _LIBCPP_NO_EXCEPTIONS
944 try
945 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400946#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant95cfd872011-05-19 15:05:04 +0000947 this->set_value(__func_());
948#ifndef _LIBCPP_NO_EXCEPTIONS
949 }
950 catch (...)
951 {
952 this->set_exception(current_exception());
953 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400954#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant95cfd872011-05-19 15:05:04 +0000955}
956
Howard Hinnantc834c512011-11-29 18:15:50 +0000957template <class _Rp, class _Fp>
Howard Hinnant95cfd872011-05-19 15:05:04 +0000958void
Howard Hinnantc834c512011-11-29 18:15:50 +0000959__async_assoc_state<_Rp, _Fp>::__on_zero_shared() _NOEXCEPT
Howard Hinnant95cfd872011-05-19 15:05:04 +0000960{
961 this->wait();
962 base::__on_zero_shared();
963}
964
Howard Hinnantc834c512011-11-29 18:15:50 +0000965template <class _Fp>
Mehdi Amini228053d2017-05-04 17:08:54 +0000966class _LIBCPP_AVAILABILITY_FUTURE __async_assoc_state<void, _Fp>
Howard Hinnant95cfd872011-05-19 15:05:04 +0000967 : public __assoc_sub_state
968{
969 typedef __assoc_sub_state base;
970
Howard Hinnantc834c512011-11-29 18:15:50 +0000971 _Fp __func_;
Howard Hinnant95cfd872011-05-19 15:05:04 +0000972
Howard Hinnant719bda32011-05-28 14:41:13 +0000973 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant95cfd872011-05-19 15:05:04 +0000974public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000975 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +0000976 explicit __async_assoc_state(_Fp&& __f);
Howard Hinnant95cfd872011-05-19 15:05:04 +0000977
978 virtual void __execute();
979};
980
Howard Hinnantc834c512011-11-29 18:15:50 +0000981template <class _Fp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000982inline
Howard Hinnantc834c512011-11-29 18:15:50 +0000983__async_assoc_state<void, _Fp>::__async_assoc_state(_Fp&& __f)
984 : __func_(_VSTD::forward<_Fp>(__f))
Howard Hinnant95cfd872011-05-19 15:05:04 +0000985{
986}
987
Howard Hinnantc834c512011-11-29 18:15:50 +0000988template <class _Fp>
Howard Hinnant95cfd872011-05-19 15:05:04 +0000989void
Howard Hinnantc834c512011-11-29 18:15:50 +0000990__async_assoc_state<void, _Fp>::__execute()
Howard Hinnant95cfd872011-05-19 15:05:04 +0000991{
992#ifndef _LIBCPP_NO_EXCEPTIONS
993 try
994 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400995#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant95cfd872011-05-19 15:05:04 +0000996 __func_();
997 this->set_value();
998#ifndef _LIBCPP_NO_EXCEPTIONS
999 }
1000 catch (...)
1001 {
1002 this->set_exception(current_exception());
1003 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001004#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant95cfd872011-05-19 15:05:04 +00001005}
1006
Howard Hinnantc834c512011-11-29 18:15:50 +00001007template <class _Fp>
Howard Hinnant95cfd872011-05-19 15:05:04 +00001008void
Howard Hinnantc834c512011-11-29 18:15:50 +00001009__async_assoc_state<void, _Fp>::__on_zero_shared() _NOEXCEPT
Howard Hinnant95cfd872011-05-19 15:05:04 +00001010{
1011 this->wait();
1012 base::__on_zero_shared();
1013}
1014
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001015template <class _Rp> class _LIBCPP_TEMPLATE_VIS promise;
1016template <class _Rp> class _LIBCPP_TEMPLATE_VIS shared_future;
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001017
1018// future
1019
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001020template <class _Rp> class _LIBCPP_TEMPLATE_VIS future;
Howard Hinnantccdd2032010-08-30 18:46:21 +00001021
Howard Hinnantc834c512011-11-29 18:15:50 +00001022template <class _Rp, class _Fp>
Louis Dionne3b967d02019-12-10 18:00:42 -05001023_LIBCPP_INLINE_VISIBILITY future<_Rp>
Howard Hinnantc834c512011-11-29 18:15:50 +00001024__make_deferred_assoc_state(_Fp&& __f);
Howard Hinnantccdd2032010-08-30 18:46:21 +00001025
Howard Hinnantc834c512011-11-29 18:15:50 +00001026template <class _Rp, class _Fp>
Louis Dionne3b967d02019-12-10 18:00:42 -05001027_LIBCPP_INLINE_VISIBILITY future<_Rp>
Howard Hinnantc834c512011-11-29 18:15:50 +00001028__make_async_assoc_state(_Fp&& __f);
Howard Hinnant95cfd872011-05-19 15:05:04 +00001029
Howard Hinnantc834c512011-11-29 18:15:50 +00001030template <class _Rp>
Mehdi Amini228053d2017-05-04 17:08:54 +00001031class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE future
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001032{
Howard Hinnantc834c512011-11-29 18:15:50 +00001033 __assoc_state<_Rp>* __state_;
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001034
Howard Hinnantc834c512011-11-29 18:15:50 +00001035 explicit future(__assoc_state<_Rp>* __state);
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001036
1037 template <class> friend class promise;
Howard Hinnant92646c42010-09-03 18:39:25 +00001038 template <class> friend class shared_future;
Howard Hinnantccdd2032010-08-30 18:46:21 +00001039
Howard Hinnantc834c512011-11-29 18:15:50 +00001040 template <class _R1, class _Fp>
1041 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1042 template <class _R1, class _Fp>
1043 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
Howard Hinnantccdd2032010-08-30 18:46:21 +00001044
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001045public:
Howard Hinnant684902d2010-09-22 14:16:26 +00001046 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00001047 future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant684902d2010-09-22 14:16:26 +00001048 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00001049 future(future&& __rhs) _NOEXCEPT
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001050 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1051 future(const future&) = delete;
1052 future& operator=(const future&) = delete;
Howard Hinnant684902d2010-09-22 14:16:26 +00001053 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00001054 future& operator=(future&& __rhs) _NOEXCEPT
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001055 {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001056 future(_VSTD::move(__rhs)).swap(*this);
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001057 return *this;
1058 }
Louis Dionne7b844362020-07-30 09:42:23 -04001059
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001060 ~future();
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001061 _LIBCPP_INLINE_VISIBILITY
Marshall Clow79e02112017-01-24 23:28:25 +00001062 shared_future<_Rp> share() _NOEXCEPT;
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001063
1064 // retrieving the value
Howard Hinnantc834c512011-11-29 18:15:50 +00001065 _Rp get();
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001066
Howard Hinnant684902d2010-09-22 14:16:26 +00001067 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00001068 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001069
1070 // functions to check state
Howard Hinnant684902d2010-09-22 14:16:26 +00001071 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00001072 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001073
Howard Hinnant684902d2010-09-22 14:16:26 +00001074 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001075 void wait() const {__state_->wait();}
1076 template <class _Rep, class _Period>
Howard Hinnant684902d2010-09-22 14:16:26 +00001077 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001078 future_status
1079 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1080 {return __state_->wait_for(__rel_time);}
1081 template <class _Clock, class _Duration>
Howard Hinnant684902d2010-09-22 14:16:26 +00001082 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001083 future_status
1084 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1085 {return __state_->wait_until(__abs_time);}
1086};
1087
Howard Hinnantc834c512011-11-29 18:15:50 +00001088template <class _Rp>
1089future<_Rp>::future(__assoc_state<_Rp>* __state)
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001090 : __state_(__state)
1091{
Louis Dionne2c0d2262018-08-24 14:00:59 +00001092 __state_->__attach_future();
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001093}
1094
Howard Hinnantccdd2032010-08-30 18:46:21 +00001095struct __release_shared_count
1096{
1097 void operator()(__shared_count* p) {p->__release_shared();}
1098};
1099
Howard Hinnantc834c512011-11-29 18:15:50 +00001100template <class _Rp>
1101future<_Rp>::~future()
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001102{
1103 if (__state_)
1104 __state_->__release_shared();
1105}
1106
Howard Hinnantc834c512011-11-29 18:15:50 +00001107template <class _Rp>
1108_Rp
1109future<_Rp>::get()
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001110{
Howard Hinnantccdd2032010-08-30 18:46:21 +00001111 unique_ptr<__shared_count, __release_shared_count> __(__state_);
Howard Hinnantc834c512011-11-29 18:15:50 +00001112 __assoc_state<_Rp>* __s = __state_;
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001113 __state_ = nullptr;
1114 return __s->move();
1115}
1116
Howard Hinnantc834c512011-11-29 18:15:50 +00001117template <class _Rp>
Mehdi Amini228053d2017-05-04 17:08:54 +00001118class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE future<_Rp&>
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001119{
Howard Hinnantc834c512011-11-29 18:15:50 +00001120 __assoc_state<_Rp&>* __state_;
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001121
Howard Hinnantc834c512011-11-29 18:15:50 +00001122 explicit future(__assoc_state<_Rp&>* __state);
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001123
1124 template <class> friend class promise;
Howard Hinnant92646c42010-09-03 18:39:25 +00001125 template <class> friend class shared_future;
Howard Hinnantccdd2032010-08-30 18:46:21 +00001126
Howard Hinnantc834c512011-11-29 18:15:50 +00001127 template <class _R1, class _Fp>
1128 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1129 template <class _R1, class _Fp>
1130 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
Howard Hinnantccdd2032010-08-30 18:46:21 +00001131
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001132public:
Howard Hinnant684902d2010-09-22 14:16:26 +00001133 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00001134 future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant684902d2010-09-22 14:16:26 +00001135 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00001136 future(future&& __rhs) _NOEXCEPT
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001137 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1138 future(const future&) = delete;
1139 future& operator=(const future&) = delete;
Howard Hinnant684902d2010-09-22 14:16:26 +00001140 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00001141 future& operator=(future&& __rhs) _NOEXCEPT
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001142 {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001143 future(_VSTD::move(__rhs)).swap(*this);
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001144 return *this;
1145 }
Louis Dionne7b844362020-07-30 09:42:23 -04001146
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001147 ~future();
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001148 _LIBCPP_INLINE_VISIBILITY
Marshall Clow79e02112017-01-24 23:28:25 +00001149 shared_future<_Rp&> share() _NOEXCEPT;
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001150
1151 // retrieving the value
Howard Hinnantc834c512011-11-29 18:15:50 +00001152 _Rp& get();
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001153
Howard Hinnant684902d2010-09-22 14:16:26 +00001154 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00001155 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001156
1157 // functions to check state
Howard Hinnant684902d2010-09-22 14:16:26 +00001158 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00001159 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001160
Howard Hinnant684902d2010-09-22 14:16:26 +00001161 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001162 void wait() const {__state_->wait();}
1163 template <class _Rep, class _Period>
Howard Hinnant684902d2010-09-22 14:16:26 +00001164 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001165 future_status
1166 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1167 {return __state_->wait_for(__rel_time);}
1168 template <class _Clock, class _Duration>
Howard Hinnant684902d2010-09-22 14:16:26 +00001169 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001170 future_status
1171 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1172 {return __state_->wait_until(__abs_time);}
1173};
1174
Howard Hinnantc834c512011-11-29 18:15:50 +00001175template <class _Rp>
1176future<_Rp&>::future(__assoc_state<_Rp&>* __state)
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001177 : __state_(__state)
1178{
Louis Dionne2c0d2262018-08-24 14:00:59 +00001179 __state_->__attach_future();
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001180}
1181
Howard Hinnantc834c512011-11-29 18:15:50 +00001182template <class _Rp>
1183future<_Rp&>::~future()
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001184{
1185 if (__state_)
1186 __state_->__release_shared();
1187}
1188
Howard Hinnantc834c512011-11-29 18:15:50 +00001189template <class _Rp>
1190_Rp&
1191future<_Rp&>::get()
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001192{
Howard Hinnantccdd2032010-08-30 18:46:21 +00001193 unique_ptr<__shared_count, __release_shared_count> __(__state_);
Howard Hinnantc834c512011-11-29 18:15:50 +00001194 __assoc_state<_Rp&>* __s = __state_;
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001195 __state_ = nullptr;
1196 return __s->copy();
1197}
1198
1199template <>
Mehdi Amini228053d2017-05-04 17:08:54 +00001200class _LIBCPP_TYPE_VIS _LIBCPP_AVAILABILITY_FUTURE future<void>
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001201{
1202 __assoc_sub_state* __state_;
1203
1204 explicit future(__assoc_sub_state* __state);
1205
1206 template <class> friend class promise;
Howard Hinnant92646c42010-09-03 18:39:25 +00001207 template <class> friend class shared_future;
Howard Hinnantccdd2032010-08-30 18:46:21 +00001208
Howard Hinnantc834c512011-11-29 18:15:50 +00001209 template <class _R1, class _Fp>
1210 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1211 template <class _R1, class _Fp>
1212 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
Howard Hinnantccdd2032010-08-30 18:46:21 +00001213
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001214public:
Howard Hinnant684902d2010-09-22 14:16:26 +00001215 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00001216 future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant684902d2010-09-22 14:16:26 +00001217 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00001218 future(future&& __rhs) _NOEXCEPT
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001219 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1220 future(const future&) = delete;
1221 future& operator=(const future&) = delete;
Howard Hinnant684902d2010-09-22 14:16:26 +00001222 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00001223 future& operator=(future&& __rhs) _NOEXCEPT
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001224 {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001225 future(_VSTD::move(__rhs)).swap(*this);
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001226 return *this;
1227 }
Louis Dionne7b844362020-07-30 09:42:23 -04001228
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001229 ~future();
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001230 _LIBCPP_INLINE_VISIBILITY
Marshall Clow79e02112017-01-24 23:28:25 +00001231 shared_future<void> share() _NOEXCEPT;
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001232
1233 // retrieving the value
1234 void get();
1235
Howard Hinnant684902d2010-09-22 14:16:26 +00001236 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00001237 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001238
1239 // functions to check state
Howard Hinnant684902d2010-09-22 14:16:26 +00001240 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00001241 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001242
Howard Hinnant684902d2010-09-22 14:16:26 +00001243 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001244 void wait() const {__state_->wait();}
1245 template <class _Rep, class _Period>
Howard Hinnant684902d2010-09-22 14:16:26 +00001246 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001247 future_status
1248 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1249 {return __state_->wait_for(__rel_time);}
1250 template <class _Clock, class _Duration>
Howard Hinnant684902d2010-09-22 14:16:26 +00001251 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001252 future_status
1253 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1254 {return __state_->wait_until(__abs_time);}
1255};
1256
Howard Hinnantc834c512011-11-29 18:15:50 +00001257template <class _Rp>
Howard Hinnant92646c42010-09-03 18:39:25 +00001258inline _LIBCPP_INLINE_VISIBILITY
1259void
Howard Hinnant22448042012-07-21 17:46:55 +00001260swap(future<_Rp>& __x, future<_Rp>& __y) _NOEXCEPT
Howard Hinnant92646c42010-09-03 18:39:25 +00001261{
1262 __x.swap(__y);
1263}
1264
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001265// promise<R>
1266
Howard Hinnant944510a2011-06-14 19:58:17 +00001267template <class _Callable> class packaged_task;
Howard Hinnantccdd2032010-08-30 18:46:21 +00001268
Howard Hinnantc834c512011-11-29 18:15:50 +00001269template <class _Rp>
Mehdi Amini228053d2017-05-04 17:08:54 +00001270class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE promise
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001271{
Howard Hinnantc834c512011-11-29 18:15:50 +00001272 __assoc_state<_Rp>* __state_;
Howard Hinnantccdd2032010-08-30 18:46:21 +00001273
Howard Hinnant684902d2010-09-22 14:16:26 +00001274 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00001275 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
Howard Hinnantccdd2032010-08-30 18:46:21 +00001276
1277 template <class> friend class packaged_task;
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001278public:
1279 promise();
1280 template <class _Alloc>
1281 promise(allocator_arg_t, const _Alloc& __a);
Howard Hinnant684902d2010-09-22 14:16:26 +00001282 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00001283 promise(promise&& __rhs) _NOEXCEPT
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001284 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1285 promise(const promise& __rhs) = delete;
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001286 ~promise();
1287
1288 // assignment
Howard Hinnant684902d2010-09-22 14:16:26 +00001289 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00001290 promise& operator=(promise&& __rhs) _NOEXCEPT
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001291 {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001292 promise(_VSTD::move(__rhs)).swap(*this);
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001293 return *this;
1294 }
1295 promise& operator=(const promise& __rhs) = delete;
Louis Dionne7b844362020-07-30 09:42:23 -04001296
Howard Hinnant684902d2010-09-22 14:16:26 +00001297 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00001298 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001299
1300 // retrieving the result
Howard Hinnantc834c512011-11-29 18:15:50 +00001301 future<_Rp> get_future();
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001302
1303 // setting the result
Howard Hinnantc834c512011-11-29 18:15:50 +00001304 void set_value(const _Rp& __r);
Howard Hinnantc834c512011-11-29 18:15:50 +00001305 void set_value(_Rp&& __r);
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001306 void set_exception(exception_ptr __p);
1307
1308 // setting the result with deferred notification
Howard Hinnantc834c512011-11-29 18:15:50 +00001309 void set_value_at_thread_exit(const _Rp& __r);
Howard Hinnantc834c512011-11-29 18:15:50 +00001310 void set_value_at_thread_exit(_Rp&& __r);
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001311 void set_exception_at_thread_exit(exception_ptr __p);
1312};
1313
Howard Hinnantc834c512011-11-29 18:15:50 +00001314template <class _Rp>
1315promise<_Rp>::promise()
1316 : __state_(new __assoc_state<_Rp>)
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001317{
1318}
1319
Howard Hinnantc834c512011-11-29 18:15:50 +00001320template <class _Rp>
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001321template <class _Alloc>
Howard Hinnantc834c512011-11-29 18:15:50 +00001322promise<_Rp>::promise(allocator_arg_t, const _Alloc& __a0)
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001323{
Eric Fiselier0d109272014-10-23 06:24:45 +00001324 typedef __assoc_state_alloc<_Rp, _Alloc> _State;
1325 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001326 typedef __allocator_destructor<_A2> _D2;
1327 _A2 __a(__a0);
Eric Fiselier0d109272014-10-23 06:24:45 +00001328 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001329 ::new ((void*)_VSTD::addressof(*__hold.get())) _State(__a0);
Eric Fiselier0d109272014-10-23 06:24:45 +00001330 __state_ = _VSTD::addressof(*__hold.release());
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001331}
1332
Howard Hinnantc834c512011-11-29 18:15:50 +00001333template <class _Rp>
1334promise<_Rp>::~promise()
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001335{
1336 if (__state_)
1337 {
1338 if (!__state_->__has_value() && __state_->use_count() > 1)
1339 __state_->set_exception(make_exception_ptr(
1340 future_error(make_error_code(future_errc::broken_promise))
1341 ));
1342 __state_->__release_shared();
1343 }
1344}
1345
Howard Hinnantc834c512011-11-29 18:15:50 +00001346template <class _Rp>
1347future<_Rp>
1348promise<_Rp>::get_future()
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001349{
1350 if (__state_ == nullptr)
Eric Fiselier43641592015-10-02 21:25:15 +00001351 __throw_future_error(future_errc::no_state);
Howard Hinnantc834c512011-11-29 18:15:50 +00001352 return future<_Rp>(__state_);
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001353}
1354
Howard Hinnantc834c512011-11-29 18:15:50 +00001355template <class _Rp>
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001356void
Howard Hinnantc834c512011-11-29 18:15:50 +00001357promise<_Rp>::set_value(const _Rp& __r)
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001358{
1359 if (__state_ == nullptr)
Eric Fiselier43641592015-10-02 21:25:15 +00001360 __throw_future_error(future_errc::no_state);
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001361 __state_->set_value(__r);
1362}
1363
Howard Hinnantc834c512011-11-29 18:15:50 +00001364template <class _Rp>
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001365void
Howard Hinnantc834c512011-11-29 18:15:50 +00001366promise<_Rp>::set_value(_Rp&& __r)
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001367{
1368 if (__state_ == nullptr)
Eric Fiselier43641592015-10-02 21:25:15 +00001369 __throw_future_error(future_errc::no_state);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001370 __state_->set_value(_VSTD::move(__r));
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001371}
1372
Howard Hinnantc834c512011-11-29 18:15:50 +00001373template <class _Rp>
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001374void
Howard Hinnantc834c512011-11-29 18:15:50 +00001375promise<_Rp>::set_exception(exception_ptr __p)
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001376{
Marshall Clow2b36f572016-05-16 16:55:32 +00001377 _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception: received nullptr" );
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001378 if (__state_ == nullptr)
Eric Fiselier43641592015-10-02 21:25:15 +00001379 __throw_future_error(future_errc::no_state);
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001380 __state_->set_exception(__p);
1381}
1382
Howard Hinnantc834c512011-11-29 18:15:50 +00001383template <class _Rp>
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001384void
Howard Hinnantc834c512011-11-29 18:15:50 +00001385promise<_Rp>::set_value_at_thread_exit(const _Rp& __r)
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001386{
1387 if (__state_ == nullptr)
Eric Fiselier43641592015-10-02 21:25:15 +00001388 __throw_future_error(future_errc::no_state);
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001389 __state_->set_value_at_thread_exit(__r);
1390}
1391
Howard Hinnantc834c512011-11-29 18:15:50 +00001392template <class _Rp>
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001393void
Howard Hinnantc834c512011-11-29 18:15:50 +00001394promise<_Rp>::set_value_at_thread_exit(_Rp&& __r)
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001395{
1396 if (__state_ == nullptr)
Eric Fiselier43641592015-10-02 21:25:15 +00001397 __throw_future_error(future_errc::no_state);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001398 __state_->set_value_at_thread_exit(_VSTD::move(__r));
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001399}
1400
Howard Hinnantc834c512011-11-29 18:15:50 +00001401template <class _Rp>
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001402void
Howard Hinnantc834c512011-11-29 18:15:50 +00001403promise<_Rp>::set_exception_at_thread_exit(exception_ptr __p)
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001404{
Eric Fiselier9dce2062016-05-31 01:50:55 +00001405 _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception_at_thread_exit: received nullptr" );
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001406 if (__state_ == nullptr)
Eric Fiselier43641592015-10-02 21:25:15 +00001407 __throw_future_error(future_errc::no_state);
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001408 __state_->set_exception_at_thread_exit(__p);
1409}
1410
1411// promise<R&>
1412
Howard Hinnantc834c512011-11-29 18:15:50 +00001413template <class _Rp>
Mehdi Amini228053d2017-05-04 17:08:54 +00001414class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE promise<_Rp&>
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001415{
Howard Hinnantc834c512011-11-29 18:15:50 +00001416 __assoc_state<_Rp&>* __state_;
Howard Hinnantccdd2032010-08-30 18:46:21 +00001417
Howard Hinnant684902d2010-09-22 14:16:26 +00001418 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00001419 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
Howard Hinnantccdd2032010-08-30 18:46:21 +00001420
1421 template <class> friend class packaged_task;
1422
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001423public:
1424 promise();
1425 template <class _Allocator>
1426 promise(allocator_arg_t, const _Allocator& __a);
Howard Hinnant684902d2010-09-22 14:16:26 +00001427 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00001428 promise(promise&& __rhs) _NOEXCEPT
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001429 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1430 promise(const promise& __rhs) = delete;
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001431 ~promise();
1432
1433 // assignment
Howard Hinnant684902d2010-09-22 14:16:26 +00001434 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00001435 promise& operator=(promise&& __rhs) _NOEXCEPT
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001436 {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001437 promise(_VSTD::move(__rhs)).swap(*this);
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001438 return *this;
1439 }
1440 promise& operator=(const promise& __rhs) = delete;
Louis Dionne7b844362020-07-30 09:42:23 -04001441
Howard Hinnant684902d2010-09-22 14:16:26 +00001442 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00001443 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001444
1445 // retrieving the result
Howard Hinnantc834c512011-11-29 18:15:50 +00001446 future<_Rp&> get_future();
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001447
1448 // setting the result
Howard Hinnantc834c512011-11-29 18:15:50 +00001449 void set_value(_Rp& __r);
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001450 void set_exception(exception_ptr __p);
1451
1452 // setting the result with deferred notification
Howard Hinnantc834c512011-11-29 18:15:50 +00001453 void set_value_at_thread_exit(_Rp&);
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001454 void set_exception_at_thread_exit(exception_ptr __p);
1455};
1456
Howard Hinnantc834c512011-11-29 18:15:50 +00001457template <class _Rp>
1458promise<_Rp&>::promise()
1459 : __state_(new __assoc_state<_Rp&>)
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001460{
1461}
1462
Howard Hinnantc834c512011-11-29 18:15:50 +00001463template <class _Rp>
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001464template <class _Alloc>
Howard Hinnantc834c512011-11-29 18:15:50 +00001465promise<_Rp&>::promise(allocator_arg_t, const _Alloc& __a0)
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001466{
Eric Fiselier0d109272014-10-23 06:24:45 +00001467 typedef __assoc_state_alloc<_Rp&, _Alloc> _State;
1468 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001469 typedef __allocator_destructor<_A2> _D2;
1470 _A2 __a(__a0);
Eric Fiselier0d109272014-10-23 06:24:45 +00001471 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001472 ::new ((void*)_VSTD::addressof(*__hold.get())) _State(__a0);
Eric Fiselier0d109272014-10-23 06:24:45 +00001473 __state_ = _VSTD::addressof(*__hold.release());
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001474}
1475
Howard Hinnantc834c512011-11-29 18:15:50 +00001476template <class _Rp>
1477promise<_Rp&>::~promise()
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001478{
1479 if (__state_)
1480 {
1481 if (!__state_->__has_value() && __state_->use_count() > 1)
1482 __state_->set_exception(make_exception_ptr(
1483 future_error(make_error_code(future_errc::broken_promise))
1484 ));
1485 __state_->__release_shared();
1486 }
1487}
1488
Howard Hinnantc834c512011-11-29 18:15:50 +00001489template <class _Rp>
1490future<_Rp&>
1491promise<_Rp&>::get_future()
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001492{
1493 if (__state_ == nullptr)
Eric Fiselier43641592015-10-02 21:25:15 +00001494 __throw_future_error(future_errc::no_state);
Howard Hinnantc834c512011-11-29 18:15:50 +00001495 return future<_Rp&>(__state_);
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001496}
1497
Howard Hinnantc834c512011-11-29 18:15:50 +00001498template <class _Rp>
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001499void
Howard Hinnantc834c512011-11-29 18:15:50 +00001500promise<_Rp&>::set_value(_Rp& __r)
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001501{
1502 if (__state_ == nullptr)
Eric Fiselier43641592015-10-02 21:25:15 +00001503 __throw_future_error(future_errc::no_state);
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001504 __state_->set_value(__r);
1505}
1506
Howard Hinnantc834c512011-11-29 18:15:50 +00001507template <class _Rp>
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001508void
Howard Hinnantc834c512011-11-29 18:15:50 +00001509promise<_Rp&>::set_exception(exception_ptr __p)
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001510{
Marshall Clow2b36f572016-05-16 16:55:32 +00001511 _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception: received nullptr" );
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001512 if (__state_ == nullptr)
Eric Fiselier43641592015-10-02 21:25:15 +00001513 __throw_future_error(future_errc::no_state);
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001514 __state_->set_exception(__p);
1515}
1516
Howard Hinnantc834c512011-11-29 18:15:50 +00001517template <class _Rp>
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001518void
Howard Hinnantc834c512011-11-29 18:15:50 +00001519promise<_Rp&>::set_value_at_thread_exit(_Rp& __r)
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001520{
1521 if (__state_ == nullptr)
Eric Fiselier43641592015-10-02 21:25:15 +00001522 __throw_future_error(future_errc::no_state);
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001523 __state_->set_value_at_thread_exit(__r);
1524}
1525
Howard Hinnantc834c512011-11-29 18:15:50 +00001526template <class _Rp>
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001527void
Howard Hinnantc834c512011-11-29 18:15:50 +00001528promise<_Rp&>::set_exception_at_thread_exit(exception_ptr __p)
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001529{
Eric Fiselier9dce2062016-05-31 01:50:55 +00001530 _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception_at_thread_exit: received nullptr" );
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001531 if (__state_ == nullptr)
Eric Fiselier43641592015-10-02 21:25:15 +00001532 __throw_future_error(future_errc::no_state);
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001533 __state_->set_exception_at_thread_exit(__p);
1534}
1535
1536// promise<void>
1537
1538template <>
Mehdi Amini228053d2017-05-04 17:08:54 +00001539class _LIBCPP_TYPE_VIS _LIBCPP_AVAILABILITY_FUTURE promise<void>
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001540{
1541 __assoc_sub_state* __state_;
Howard Hinnantccdd2032010-08-30 18:46:21 +00001542
Howard Hinnant684902d2010-09-22 14:16:26 +00001543 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00001544 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
Howard Hinnantccdd2032010-08-30 18:46:21 +00001545
1546 template <class> friend class packaged_task;
1547
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001548public:
1549 promise();
1550 template <class _Allocator>
Shoaib Meenai55f3a462017-03-02 03:22:18 +00001551 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001552 promise(allocator_arg_t, const _Allocator& __a);
Howard Hinnant684902d2010-09-22 14:16:26 +00001553 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00001554 promise(promise&& __rhs) _NOEXCEPT
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001555 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1556 promise(const promise& __rhs) = delete;
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001557 ~promise();
1558
1559 // assignment
Howard Hinnant684902d2010-09-22 14:16:26 +00001560 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00001561 promise& operator=(promise&& __rhs) _NOEXCEPT
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001562 {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001563 promise(_VSTD::move(__rhs)).swap(*this);
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001564 return *this;
1565 }
1566 promise& operator=(const promise& __rhs) = delete;
Louis Dionne7b844362020-07-30 09:42:23 -04001567
Howard Hinnant684902d2010-09-22 14:16:26 +00001568 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00001569 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001570
1571 // retrieving the result
1572 future<void> get_future();
1573
1574 // setting the result
1575 void set_value();
1576 void set_exception(exception_ptr __p);
1577
1578 // setting the result with deferred notification
1579 void set_value_at_thread_exit();
1580 void set_exception_at_thread_exit(exception_ptr __p);
1581};
1582
1583template <class _Alloc>
1584promise<void>::promise(allocator_arg_t, const _Alloc& __a0)
1585{
Eric Fiselier0d109272014-10-23 06:24:45 +00001586 typedef __assoc_sub_state_alloc<_Alloc> _State;
1587 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001588 typedef __allocator_destructor<_A2> _D2;
1589 _A2 __a(__a0);
Eric Fiselier0d109272014-10-23 06:24:45 +00001590 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001591 ::new ((void*)_VSTD::addressof(*__hold.get())) _State(__a0);
Eric Fiselier0d109272014-10-23 06:24:45 +00001592 __state_ = _VSTD::addressof(*__hold.release());
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001593}
1594
Howard Hinnantc834c512011-11-29 18:15:50 +00001595template <class _Rp>
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001596inline _LIBCPP_INLINE_VISIBILITY
1597void
Howard Hinnant22448042012-07-21 17:46:55 +00001598swap(promise<_Rp>& __x, promise<_Rp>& __y) _NOEXCEPT
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001599{
1600 __x.swap(__y);
1601}
1602
Howard Hinnantc834c512011-11-29 18:15:50 +00001603template <class _Rp, class _Alloc>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001604 struct _LIBCPP_TEMPLATE_VIS uses_allocator<promise<_Rp>, _Alloc>
Howard Hinnant684902d2010-09-22 14:16:26 +00001605 : public true_type {};
Howard Hinnant3820f6b2010-08-27 20:10:19 +00001606
Howard Hinnantccdd2032010-08-30 18:46:21 +00001607// packaged_task
1608
1609template<class _Fp> class __packaged_task_base;
1610
Howard Hinnantc834c512011-11-29 18:15:50 +00001611template<class _Rp, class ..._ArgTypes>
Mehdi Amini228053d2017-05-04 17:08:54 +00001612class _LIBCPP_AVAILABILITY_FUTURE __packaged_task_base<_Rp(_ArgTypes...)>
Howard Hinnantccdd2032010-08-30 18:46:21 +00001613{
1614 __packaged_task_base(const __packaged_task_base&);
1615 __packaged_task_base& operator=(const __packaged_task_base&);
1616public:
Howard Hinnant684902d2010-09-22 14:16:26 +00001617 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantccdd2032010-08-30 18:46:21 +00001618 __packaged_task_base() {}
Howard Hinnant684902d2010-09-22 14:16:26 +00001619 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantccdd2032010-08-30 18:46:21 +00001620 virtual ~__packaged_task_base() {}
Howard Hinnant22448042012-07-21 17:46:55 +00001621 virtual void __move_to(__packaged_task_base*) _NOEXCEPT = 0;
Howard Hinnantccdd2032010-08-30 18:46:21 +00001622 virtual void destroy() = 0;
1623 virtual void destroy_deallocate() = 0;
Howard Hinnantc834c512011-11-29 18:15:50 +00001624 virtual _Rp operator()(_ArgTypes&& ...) = 0;
Howard Hinnantccdd2032010-08-30 18:46:21 +00001625};
1626
1627template<class _FD, class _Alloc, class _FB> class __packaged_task_func;
1628
Howard Hinnantc834c512011-11-29 18:15:50 +00001629template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Mehdi Amini228053d2017-05-04 17:08:54 +00001630class _LIBCPP_AVAILABILITY_FUTURE __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>
Howard Hinnantc834c512011-11-29 18:15:50 +00001631 : public __packaged_task_base<_Rp(_ArgTypes...)>
Howard Hinnantccdd2032010-08-30 18:46:21 +00001632{
Howard Hinnantc834c512011-11-29 18:15:50 +00001633 __compressed_pair<_Fp, _Alloc> __f_;
Howard Hinnantccdd2032010-08-30 18:46:21 +00001634public:
Howard Hinnant684902d2010-09-22 14:16:26 +00001635 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001636 explicit __packaged_task_func(const _Fp& __f) : __f_(__f, __default_init_tag()) {}
Howard Hinnant684902d2010-09-22 14:16:26 +00001637 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001638 explicit __packaged_task_func(_Fp&& __f) : __f_(_VSTD::move(__f), __default_init_tag()) {}
Howard Hinnant684902d2010-09-22 14:16:26 +00001639 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001640 __packaged_task_func(const _Fp& __f, const _Alloc& __a)
Howard Hinnantccdd2032010-08-30 18:46:21 +00001641 : __f_(__f, __a) {}
Howard Hinnant684902d2010-09-22 14:16:26 +00001642 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001643 __packaged_task_func(_Fp&& __f, const _Alloc& __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001644 : __f_(_VSTD::move(__f), __a) {}
Howard Hinnant22448042012-07-21 17:46:55 +00001645 virtual void __move_to(__packaged_task_base<_Rp(_ArgTypes...)>*) _NOEXCEPT;
Howard Hinnantccdd2032010-08-30 18:46:21 +00001646 virtual void destroy();
1647 virtual void destroy_deallocate();
Howard Hinnantc834c512011-11-29 18:15:50 +00001648 virtual _Rp operator()(_ArgTypes&& ... __args);
Howard Hinnantccdd2032010-08-30 18:46:21 +00001649};
1650
Howard Hinnantc834c512011-11-29 18:15:50 +00001651template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantccdd2032010-08-30 18:46:21 +00001652void
Howard Hinnantc834c512011-11-29 18:15:50 +00001653__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__move_to(
Howard Hinnant22448042012-07-21 17:46:55 +00001654 __packaged_task_base<_Rp(_ArgTypes...)>* __p) _NOEXCEPT
Howard Hinnantccdd2032010-08-30 18:46:21 +00001655{
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001656 ::new ((void*)__p) __packaged_task_func(_VSTD::move(__f_.first()), _VSTD::move(__f_.second()));
Howard Hinnantccdd2032010-08-30 18:46:21 +00001657}
1658
Howard Hinnantc834c512011-11-29 18:15:50 +00001659template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantccdd2032010-08-30 18:46:21 +00001660void
Howard Hinnantc834c512011-11-29 18:15:50 +00001661__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy()
Howard Hinnantccdd2032010-08-30 18:46:21 +00001662{
Howard Hinnantc834c512011-11-29 18:15:50 +00001663 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantccdd2032010-08-30 18:46:21 +00001664}
1665
Howard Hinnantc834c512011-11-29 18:15:50 +00001666template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantccdd2032010-08-30 18:46:21 +00001667void
Howard Hinnantc834c512011-11-29 18:15:50 +00001668__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate()
Howard Hinnantccdd2032010-08-30 18:46:21 +00001669{
Eric Fiselier0d109272014-10-23 06:24:45 +00001670 typedef typename __allocator_traits_rebind<_Alloc, __packaged_task_func>::type _Ap;
1671 typedef allocator_traits<_Ap> _ATraits;
1672 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Howard Hinnantc834c512011-11-29 18:15:50 +00001673 _Ap __a(__f_.second());
1674 __f_.~__compressed_pair<_Fp, _Alloc>();
Eric Fiselier0d109272014-10-23 06:24:45 +00001675 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantccdd2032010-08-30 18:46:21 +00001676}
1677
Howard Hinnantc834c512011-11-29 18:15:50 +00001678template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1679_Rp
1680__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
Howard Hinnantccdd2032010-08-30 18:46:21 +00001681{
Arthur O'Dwyer34465da2020-12-15 19:32:29 -05001682 return _VSTD::__invoke(__f_.first(), _VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnantccdd2032010-08-30 18:46:21 +00001683}
1684
Howard Hinnant944510a2011-06-14 19:58:17 +00001685template <class _Callable> class __packaged_task_function;
Howard Hinnantccdd2032010-08-30 18:46:21 +00001686
Howard Hinnantc834c512011-11-29 18:15:50 +00001687template<class _Rp, class ..._ArgTypes>
Mehdi Amini228053d2017-05-04 17:08:54 +00001688class _LIBCPP_AVAILABILITY_FUTURE __packaged_task_function<_Rp(_ArgTypes...)>
Howard Hinnantccdd2032010-08-30 18:46:21 +00001689{
Howard Hinnantc834c512011-11-29 18:15:50 +00001690 typedef __packaged_task_base<_Rp(_ArgTypes...)> __base;
Evgenii Stepanov9dd5fc22020-08-06 11:32:33 -07001691
1692 _LIBCPP_INLINE_VISIBILITY _LIBCPP_NO_CFI
1693 __base* __get_buf() { return (__base*)&__buf_; }
1694
Howard Hinnant022c7482013-01-21 17:26:55 +00001695 typename aligned_storage<3*sizeof(void*)>::type __buf_;
Howard Hinnantccdd2032010-08-30 18:46:21 +00001696 __base* __f_;
1697
1698public:
Howard Hinnantc834c512011-11-29 18:15:50 +00001699 typedef _Rp result_type;
Howard Hinnantccdd2032010-08-30 18:46:21 +00001700
1701 // construct/copy/destroy:
Howard Hinnant684902d2010-09-22 14:16:26 +00001702 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00001703 __packaged_task_function() _NOEXCEPT : __f_(nullptr) {}
Howard Hinnantc834c512011-11-29 18:15:50 +00001704 template<class _Fp>
1705 __packaged_task_function(_Fp&& __f);
1706 template<class _Fp, class _Alloc>
1707 __packaged_task_function(allocator_arg_t, const _Alloc& __a, _Fp&& __f);
Howard Hinnantccdd2032010-08-30 18:46:21 +00001708
Howard Hinnant22448042012-07-21 17:46:55 +00001709 __packaged_task_function(__packaged_task_function&&) _NOEXCEPT;
1710 __packaged_task_function& operator=(__packaged_task_function&&) _NOEXCEPT;
Howard Hinnantccdd2032010-08-30 18:46:21 +00001711
1712 __packaged_task_function(const __packaged_task_function&) = delete;
1713 __packaged_task_function& operator=(const __packaged_task_function&) = delete;
1714
1715 ~__packaged_task_function();
1716
Howard Hinnant22448042012-07-21 17:46:55 +00001717 void swap(__packaged_task_function&) _NOEXCEPT;
Howard Hinnantccdd2032010-08-30 18:46:21 +00001718
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001719 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001720 _Rp operator()(_ArgTypes...) const;
Howard Hinnantccdd2032010-08-30 18:46:21 +00001721};
1722
Howard Hinnantc834c512011-11-29 18:15:50 +00001723template<class _Rp, class ..._ArgTypes>
Howard Hinnant22448042012-07-21 17:46:55 +00001724__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f) _NOEXCEPT
Howard Hinnantccdd2032010-08-30 18:46:21 +00001725{
1726 if (__f.__f_ == nullptr)
1727 __f_ = nullptr;
Evgenii Stepanov9dd5fc22020-08-06 11:32:33 -07001728 else if (__f.__f_ == __f.__get_buf())
Howard Hinnantccdd2032010-08-30 18:46:21 +00001729 {
Evgenii Stepanov9dd5fc22020-08-06 11:32:33 -07001730 __f.__f_->__move_to(__get_buf());
Howard Hinnantccdd2032010-08-30 18:46:21 +00001731 __f_ = (__base*)&__buf_;
Howard Hinnantccdd2032010-08-30 18:46:21 +00001732 }
1733 else
1734 {
1735 __f_ = __f.__f_;
1736 __f.__f_ = nullptr;
1737 }
1738}
1739
Howard Hinnantc834c512011-11-29 18:15:50 +00001740template<class _Rp, class ..._ArgTypes>
1741template <class _Fp>
1742__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(_Fp&& __f)
Howard Hinnantccdd2032010-08-30 18:46:21 +00001743 : __f_(nullptr)
1744{
Marshall Clow733d60e2014-04-07 13:32:26 +00001745 typedef typename remove_reference<typename decay<_Fp>::type>::type _FR;
Howard Hinnantc834c512011-11-29 18:15:50 +00001746 typedef __packaged_task_func<_FR, allocator<_FR>, _Rp(_ArgTypes...)> _FF;
Howard Hinnantccdd2032010-08-30 18:46:21 +00001747 if (sizeof(_FF) <= sizeof(__buf_))
1748 {
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001749 ::new ((void*)&__buf_) _FF(_VSTD::forward<_Fp>(__f));
Howard Hinnantccdd2032010-08-30 18:46:21 +00001750 __f_ = (__base*)&__buf_;
Howard Hinnantccdd2032010-08-30 18:46:21 +00001751 }
1752 else
1753 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001754 typedef allocator<_FF> _Ap;
1755 _Ap __a;
1756 typedef __allocator_destructor<_Ap> _Dp;
1757 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001758 ::new ((void*)__hold.get()) _FF(_VSTD::forward<_Fp>(__f), allocator<_FR>(__a));
Howard Hinnantccdd2032010-08-30 18:46:21 +00001759 __f_ = __hold.release();
1760 }
1761}
1762
Howard Hinnantc834c512011-11-29 18:15:50 +00001763template<class _Rp, class ..._ArgTypes>
1764template <class _Fp, class _Alloc>
1765__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(
1766 allocator_arg_t, const _Alloc& __a0, _Fp&& __f)
Howard Hinnantccdd2032010-08-30 18:46:21 +00001767 : __f_(nullptr)
1768{
Marshall Clow733d60e2014-04-07 13:32:26 +00001769 typedef typename remove_reference<typename decay<_Fp>::type>::type _FR;
Howard Hinnantc834c512011-11-29 18:15:50 +00001770 typedef __packaged_task_func<_FR, _Alloc, _Rp(_ArgTypes...)> _FF;
Howard Hinnantccdd2032010-08-30 18:46:21 +00001771 if (sizeof(_FF) <= sizeof(__buf_))
1772 {
1773 __f_ = (__base*)&__buf_;
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001774 ::new ((void*)__f_) _FF(_VSTD::forward<_Fp>(__f));
Howard Hinnantccdd2032010-08-30 18:46:21 +00001775 }
1776 else
1777 {
Eric Fiselier0d109272014-10-23 06:24:45 +00001778 typedef typename __allocator_traits_rebind<_Alloc, _FF>::type _Ap;
Howard Hinnantc834c512011-11-29 18:15:50 +00001779 _Ap __a(__a0);
1780 typedef __allocator_destructor<_Ap> _Dp;
1781 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001782 ::new ((void*)_VSTD::addressof(*__hold.get()))
Eric Fiselier0d109272014-10-23 06:24:45 +00001783 _FF(_VSTD::forward<_Fp>(__f), _Alloc(__a));
1784 __f_ = _VSTD::addressof(*__hold.release());
Howard Hinnantccdd2032010-08-30 18:46:21 +00001785 }
1786}
1787
Howard Hinnantc834c512011-11-29 18:15:50 +00001788template<class _Rp, class ..._ArgTypes>
1789__packaged_task_function<_Rp(_ArgTypes...)>&
Howard Hinnant22448042012-07-21 17:46:55 +00001790__packaged_task_function<_Rp(_ArgTypes...)>::operator=(__packaged_task_function&& __f) _NOEXCEPT
Howard Hinnantccdd2032010-08-30 18:46:21 +00001791{
Evgenii Stepanov9dd5fc22020-08-06 11:32:33 -07001792 if (__f_ == __get_buf())
Howard Hinnantccdd2032010-08-30 18:46:21 +00001793 __f_->destroy();
1794 else if (__f_)
1795 __f_->destroy_deallocate();
1796 __f_ = nullptr;
1797 if (__f.__f_ == nullptr)
1798 __f_ = nullptr;
Evgenii Stepanov9dd5fc22020-08-06 11:32:33 -07001799 else if (__f.__f_ == __f.__get_buf())
Howard Hinnantccdd2032010-08-30 18:46:21 +00001800 {
Evgenii Stepanov9dd5fc22020-08-06 11:32:33 -07001801 __f.__f_->__move_to(__get_buf());
1802 __f_ = __get_buf();
Howard Hinnantccdd2032010-08-30 18:46:21 +00001803 }
1804 else
1805 {
1806 __f_ = __f.__f_;
1807 __f.__f_ = nullptr;
1808 }
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00001809 return *this;
Howard Hinnantccdd2032010-08-30 18:46:21 +00001810}
1811
Howard Hinnantc834c512011-11-29 18:15:50 +00001812template<class _Rp, class ..._ArgTypes>
1813__packaged_task_function<_Rp(_ArgTypes...)>::~__packaged_task_function()
Howard Hinnantccdd2032010-08-30 18:46:21 +00001814{
Evgenii Stepanov9dd5fc22020-08-06 11:32:33 -07001815 if (__f_ == __get_buf())
Howard Hinnantccdd2032010-08-30 18:46:21 +00001816 __f_->destroy();
1817 else if (__f_)
1818 __f_->destroy_deallocate();
1819}
1820
Howard Hinnantc834c512011-11-29 18:15:50 +00001821template<class _Rp, class ..._ArgTypes>
Evgenii Stepanov9dd5fc22020-08-06 11:32:33 -07001822_LIBCPP_NO_CFI
Howard Hinnantccdd2032010-08-30 18:46:21 +00001823void
Howard Hinnant22448042012-07-21 17:46:55 +00001824__packaged_task_function<_Rp(_ArgTypes...)>::swap(__packaged_task_function& __f) _NOEXCEPT
Howard Hinnantccdd2032010-08-30 18:46:21 +00001825{
1826 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1827 {
1828 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1829 __base* __t = (__base*)&__tempbuf;
1830 __f_->__move_to(__t);
1831 __f_->destroy();
1832 __f_ = nullptr;
1833 __f.__f_->__move_to((__base*)&__buf_);
1834 __f.__f_->destroy();
1835 __f.__f_ = nullptr;
1836 __f_ = (__base*)&__buf_;
1837 __t->__move_to((__base*)&__f.__buf_);
1838 __t->destroy();
1839 __f.__f_ = (__base*)&__f.__buf_;
1840 }
1841 else if (__f_ == (__base*)&__buf_)
1842 {
1843 __f_->__move_to((__base*)&__f.__buf_);
1844 __f_->destroy();
1845 __f_ = __f.__f_;
1846 __f.__f_ = (__base*)&__f.__buf_;
1847 }
1848 else if (__f.__f_ == (__base*)&__f.__buf_)
1849 {
1850 __f.__f_->__move_to((__base*)&__buf_);
1851 __f.__f_->destroy();
1852 __f.__f_ = __f_;
1853 __f_ = (__base*)&__buf_;
1854 }
1855 else
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001856 _VSTD::swap(__f_, __f.__f_);
Howard Hinnantccdd2032010-08-30 18:46:21 +00001857}
1858
Howard Hinnantc834c512011-11-29 18:15:50 +00001859template<class _Rp, class ..._ArgTypes>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001860inline
Howard Hinnantc834c512011-11-29 18:15:50 +00001861_Rp
1862__packaged_task_function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
Howard Hinnantccdd2032010-08-30 18:46:21 +00001863{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001864 return (*__f_)(_VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnantccdd2032010-08-30 18:46:21 +00001865}
1866
Howard Hinnantc834c512011-11-29 18:15:50 +00001867template<class _Rp, class ..._ArgTypes>
Mehdi Amini228053d2017-05-04 17:08:54 +00001868class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE packaged_task<_Rp(_ArgTypes...)>
Howard Hinnantccdd2032010-08-30 18:46:21 +00001869{
1870public:
Eric Fiselier17f39ed2016-06-01 21:05:53 +00001871 typedef _Rp result_type; // extension
Howard Hinnantccdd2032010-08-30 18:46:21 +00001872
1873private:
1874 __packaged_task_function<result_type(_ArgTypes...)> __f_;
1875 promise<result_type> __p_;
1876
1877public:
1878 // construction and destruction
Howard Hinnant684902d2010-09-22 14:16:26 +00001879 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00001880 packaged_task() _NOEXCEPT : __p_(nullptr) {}
Marshall Clow27a55c02013-10-12 22:49:17 +00001881 template <class _Fp,
1882 class = typename enable_if
1883 <
1884 !is_same<
Louis Dionne173f29e2019-05-29 16:01:36 +00001885 typename __uncvref<_Fp>::type,
Marshall Clow27a55c02013-10-12 22:49:17 +00001886 packaged_task
1887 >::value
1888 >::type
1889 >
Howard Hinnant684902d2010-09-22 14:16:26 +00001890 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001891 explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
Marshall Clowa0a057e2017-11-27 20:47:54 +00001892 template <class _Fp, class _Allocator,
1893 class = typename enable_if
1894 <
1895 !is_same<
Louis Dionne173f29e2019-05-29 16:01:36 +00001896 typename __uncvref<_Fp>::type,
Marshall Clowa0a057e2017-11-27 20:47:54 +00001897 packaged_task
1898 >::value
1899 >::type
1900 >
1901 _LIBCPP_INLINE_VISIBILITY
1902 packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
1903 : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)),
1904 __p_(allocator_arg, __a) {}
Howard Hinnantccdd2032010-08-30 18:46:21 +00001905 // ~packaged_task() = default;
1906
1907 // no copy
Howard Hinnantab500072012-07-21 19:34:12 +00001908 packaged_task(const packaged_task&) = delete;
1909 packaged_task& operator=(const packaged_task&) = delete;
Howard Hinnantccdd2032010-08-30 18:46:21 +00001910
1911 // move support
Howard Hinnant684902d2010-09-22 14:16:26 +00001912 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00001913 packaged_task(packaged_task&& __other) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001914 : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
Howard Hinnant684902d2010-09-22 14:16:26 +00001915 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00001916 packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
Howard Hinnantccdd2032010-08-30 18:46:21 +00001917 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001918 __f_ = _VSTD::move(__other.__f_);
1919 __p_ = _VSTD::move(__other.__p_);
Howard Hinnantccdd2032010-08-30 18:46:21 +00001920 return *this;
1921 }
Howard Hinnant684902d2010-09-22 14:16:26 +00001922 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00001923 void swap(packaged_task& __other) _NOEXCEPT
Howard Hinnantccdd2032010-08-30 18:46:21 +00001924 {
1925 __f_.swap(__other.__f_);
1926 __p_.swap(__other.__p_);
1927 }
1928
Howard Hinnant684902d2010-09-22 14:16:26 +00001929 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00001930 bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
Howard Hinnantccdd2032010-08-30 18:46:21 +00001931
1932 // result retrieval
Howard Hinnant684902d2010-09-22 14:16:26 +00001933 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantccdd2032010-08-30 18:46:21 +00001934 future<result_type> get_future() {return __p_.get_future();}
1935
1936 // execution
1937 void operator()(_ArgTypes... __args);
1938 void make_ready_at_thread_exit(_ArgTypes... __args);
1939
1940 void reset();
1941};
1942
Howard Hinnantc834c512011-11-29 18:15:50 +00001943template<class _Rp, class ..._ArgTypes>
Howard Hinnantccdd2032010-08-30 18:46:21 +00001944void
Howard Hinnantc834c512011-11-29 18:15:50 +00001945packaged_task<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __args)
Howard Hinnantccdd2032010-08-30 18:46:21 +00001946{
Howard Hinnantccdd2032010-08-30 18:46:21 +00001947 if (__p_.__state_ == nullptr)
Eric Fiselier43641592015-10-02 21:25:15 +00001948 __throw_future_error(future_errc::no_state);
Howard Hinnantccdd2032010-08-30 18:46:21 +00001949 if (__p_.__state_->__has_value())
Eric Fiselier43641592015-10-02 21:25:15 +00001950 __throw_future_error(future_errc::promise_already_satisfied);
Marshall Clow7b3742d2015-09-03 15:11:32 +00001951#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantccdd2032010-08-30 18:46:21 +00001952 try
1953 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001954#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001955 __p_.set_value(__f_(_VSTD::forward<_ArgTypes>(__args)...));
Howard Hinnantccdd2032010-08-30 18:46:21 +00001956#ifndef _LIBCPP_NO_EXCEPTIONS
1957 }
1958 catch (...)
1959 {
1960 __p_.set_exception(current_exception());
1961 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001962#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantccdd2032010-08-30 18:46:21 +00001963}
1964
Howard Hinnantc834c512011-11-29 18:15:50 +00001965template<class _Rp, class ..._ArgTypes>
Howard Hinnantccdd2032010-08-30 18:46:21 +00001966void
Howard Hinnantc834c512011-11-29 18:15:50 +00001967packaged_task<_Rp(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
Howard Hinnantccdd2032010-08-30 18:46:21 +00001968{
Howard Hinnantccdd2032010-08-30 18:46:21 +00001969 if (__p_.__state_ == nullptr)
Eric Fiselier43641592015-10-02 21:25:15 +00001970 __throw_future_error(future_errc::no_state);
Howard Hinnantccdd2032010-08-30 18:46:21 +00001971 if (__p_.__state_->__has_value())
Eric Fiselier43641592015-10-02 21:25:15 +00001972 __throw_future_error(future_errc::promise_already_satisfied);
Marshall Clow7b3742d2015-09-03 15:11:32 +00001973#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantccdd2032010-08-30 18:46:21 +00001974 try
1975 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001976#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001977 __p_.set_value_at_thread_exit(__f_(_VSTD::forward<_ArgTypes>(__args)...));
Howard Hinnantccdd2032010-08-30 18:46:21 +00001978#ifndef _LIBCPP_NO_EXCEPTIONS
1979 }
1980 catch (...)
1981 {
1982 __p_.set_exception_at_thread_exit(current_exception());
1983 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001984#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantccdd2032010-08-30 18:46:21 +00001985}
1986
Howard Hinnantc834c512011-11-29 18:15:50 +00001987template<class _Rp, class ..._ArgTypes>
Howard Hinnantccdd2032010-08-30 18:46:21 +00001988void
Howard Hinnantc834c512011-11-29 18:15:50 +00001989packaged_task<_Rp(_ArgTypes...)>::reset()
Howard Hinnantccdd2032010-08-30 18:46:21 +00001990{
Howard Hinnant9a7d09c2010-11-30 20:23:32 +00001991 if (!valid())
Eric Fiselier43641592015-10-02 21:25:15 +00001992 __throw_future_error(future_errc::no_state);
Howard Hinnantccdd2032010-08-30 18:46:21 +00001993 __p_ = promise<result_type>();
1994}
1995
1996template<class ..._ArgTypes>
Mehdi Amini228053d2017-05-04 17:08:54 +00001997class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE packaged_task<void(_ArgTypes...)>
Howard Hinnantccdd2032010-08-30 18:46:21 +00001998{
1999public:
Eric Fiselier17f39ed2016-06-01 21:05:53 +00002000 typedef void result_type; // extension
Howard Hinnantccdd2032010-08-30 18:46:21 +00002001
2002private:
2003 __packaged_task_function<result_type(_ArgTypes...)> __f_;
2004 promise<result_type> __p_;
2005
2006public:
2007 // construction and destruction
Howard Hinnant684902d2010-09-22 14:16:26 +00002008 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00002009 packaged_task() _NOEXCEPT : __p_(nullptr) {}
Marshall Clow27a55c02013-10-12 22:49:17 +00002010 template <class _Fp,
2011 class = typename enable_if
2012 <
2013 !is_same<
Louis Dionne173f29e2019-05-29 16:01:36 +00002014 typename __uncvref<_Fp>::type,
Marshall Clow27a55c02013-10-12 22:49:17 +00002015 packaged_task
2016 >::value
2017 >::type
2018 >
Howard Hinnant684902d2010-09-22 14:16:26 +00002019 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002020 explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
Marshall Clowa0a057e2017-11-27 20:47:54 +00002021 template <class _Fp, class _Allocator,
2022 class = typename enable_if
2023 <
2024 !is_same<
Louis Dionne173f29e2019-05-29 16:01:36 +00002025 typename __uncvref<_Fp>::type,
Marshall Clowa0a057e2017-11-27 20:47:54 +00002026 packaged_task
2027 >::value
2028 >::type
Louis Dionne173f29e2019-05-29 16:01:36 +00002029 >
Marshall Clowa0a057e2017-11-27 20:47:54 +00002030 _LIBCPP_INLINE_VISIBILITY
2031 packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
2032 : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)),
2033 __p_(allocator_arg, __a) {}
Howard Hinnantccdd2032010-08-30 18:46:21 +00002034 // ~packaged_task() = default;
2035
2036 // no copy
Howard Hinnantab500072012-07-21 19:34:12 +00002037 packaged_task(const packaged_task&) = delete;
2038 packaged_task& operator=(const packaged_task&) = delete;
Howard Hinnantccdd2032010-08-30 18:46:21 +00002039
2040 // move support
Howard Hinnant684902d2010-09-22 14:16:26 +00002041 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00002042 packaged_task(packaged_task&& __other) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002043 : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
Howard Hinnant684902d2010-09-22 14:16:26 +00002044 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00002045 packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
Howard Hinnantccdd2032010-08-30 18:46:21 +00002046 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002047 __f_ = _VSTD::move(__other.__f_);
2048 __p_ = _VSTD::move(__other.__p_);
Howard Hinnantccdd2032010-08-30 18:46:21 +00002049 return *this;
2050 }
Howard Hinnant684902d2010-09-22 14:16:26 +00002051 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00002052 void swap(packaged_task& __other) _NOEXCEPT
Howard Hinnantccdd2032010-08-30 18:46:21 +00002053 {
2054 __f_.swap(__other.__f_);
2055 __p_.swap(__other.__p_);
2056 }
2057
Howard Hinnant684902d2010-09-22 14:16:26 +00002058 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00002059 bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
Howard Hinnantccdd2032010-08-30 18:46:21 +00002060
2061 // result retrieval
Howard Hinnant684902d2010-09-22 14:16:26 +00002062 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantccdd2032010-08-30 18:46:21 +00002063 future<result_type> get_future() {return __p_.get_future();}
2064
2065 // execution
2066 void operator()(_ArgTypes... __args);
2067 void make_ready_at_thread_exit(_ArgTypes... __args);
2068
2069 void reset();
2070};
2071
2072template<class ..._ArgTypes>
2073void
2074packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args)
2075{
Howard Hinnantccdd2032010-08-30 18:46:21 +00002076 if (__p_.__state_ == nullptr)
Eric Fiselier43641592015-10-02 21:25:15 +00002077 __throw_future_error(future_errc::no_state);
Howard Hinnantccdd2032010-08-30 18:46:21 +00002078 if (__p_.__state_->__has_value())
Eric Fiselier43641592015-10-02 21:25:15 +00002079 __throw_future_error(future_errc::promise_already_satisfied);
Marshall Clow7b3742d2015-09-03 15:11:32 +00002080#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantccdd2032010-08-30 18:46:21 +00002081 try
2082 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002083#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002084 __f_(_VSTD::forward<_ArgTypes>(__args)...);
Howard Hinnantccdd2032010-08-30 18:46:21 +00002085 __p_.set_value();
2086#ifndef _LIBCPP_NO_EXCEPTIONS
2087 }
2088 catch (...)
2089 {
2090 __p_.set_exception(current_exception());
2091 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002092#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantccdd2032010-08-30 18:46:21 +00002093}
2094
2095template<class ..._ArgTypes>
2096void
2097packaged_task<void(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
2098{
Howard Hinnantccdd2032010-08-30 18:46:21 +00002099 if (__p_.__state_ == nullptr)
Eric Fiselier43641592015-10-02 21:25:15 +00002100 __throw_future_error(future_errc::no_state);
Howard Hinnantccdd2032010-08-30 18:46:21 +00002101 if (__p_.__state_->__has_value())
Eric Fiselier43641592015-10-02 21:25:15 +00002102 __throw_future_error(future_errc::promise_already_satisfied);
Marshall Clow7b3742d2015-09-03 15:11:32 +00002103#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantccdd2032010-08-30 18:46:21 +00002104 try
2105 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002106#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002107 __f_(_VSTD::forward<_ArgTypes>(__args)...);
Howard Hinnantccdd2032010-08-30 18:46:21 +00002108 __p_.set_value_at_thread_exit();
2109#ifndef _LIBCPP_NO_EXCEPTIONS
2110 }
2111 catch (...)
2112 {
2113 __p_.set_exception_at_thread_exit(current_exception());
2114 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002115#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantccdd2032010-08-30 18:46:21 +00002116}
2117
2118template<class ..._ArgTypes>
2119void
2120packaged_task<void(_ArgTypes...)>::reset()
2121{
Howard Hinnant9a7d09c2010-11-30 20:23:32 +00002122 if (!valid())
Eric Fiselier43641592015-10-02 21:25:15 +00002123 __throw_future_error(future_errc::no_state);
Howard Hinnantccdd2032010-08-30 18:46:21 +00002124 __p_ = promise<result_type>();
2125}
2126
jasonliubdad63b2021-03-24 22:31:58 +00002127template <class _Rp, class... _ArgTypes>
Howard Hinnantccdd2032010-08-30 18:46:21 +00002128inline _LIBCPP_INLINE_VISIBILITY
2129void
jasonliubdad63b2021-03-24 22:31:58 +00002130swap(packaged_task<_Rp(_ArgTypes...)>& __x, packaged_task<_Rp(_ArgTypes...)>& __y) _NOEXCEPT
Howard Hinnantccdd2032010-08-30 18:46:21 +00002131{
2132 __x.swap(__y);
2133}
2134
Marshall Clowa0a057e2017-11-27 20:47:54 +00002135template <class _Callable, class _Alloc>
2136struct _LIBCPP_TEMPLATE_VIS uses_allocator<packaged_task<_Callable>, _Alloc>
2137 : public true_type {};
2138
Howard Hinnantc834c512011-11-29 18:15:50 +00002139template <class _Rp, class _Fp>
Louis Dionne3b967d02019-12-10 18:00:42 -05002140_LIBCPP_INLINE_VISIBILITY future<_Rp>
Howard Hinnantc834c512011-11-29 18:15:50 +00002141__make_deferred_assoc_state(_Fp&& __f)
Howard Hinnantccdd2032010-08-30 18:46:21 +00002142{
Howard Hinnantc834c512011-11-29 18:15:50 +00002143 unique_ptr<__deferred_assoc_state<_Rp, _Fp>, __release_shared_count>
2144 __h(new __deferred_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
2145 return future<_Rp>(__h.get());
Howard Hinnantccdd2032010-08-30 18:46:21 +00002146}
2147
Howard Hinnantc834c512011-11-29 18:15:50 +00002148template <class _Rp, class _Fp>
Louis Dionne3b967d02019-12-10 18:00:42 -05002149_LIBCPP_INLINE_VISIBILITY future<_Rp>
Howard Hinnantc834c512011-11-29 18:15:50 +00002150__make_async_assoc_state(_Fp&& __f)
Howard Hinnant95cfd872011-05-19 15:05:04 +00002151{
Howard Hinnantc834c512011-11-29 18:15:50 +00002152 unique_ptr<__async_assoc_state<_Rp, _Fp>, __release_shared_count>
2153 __h(new __async_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
2154 _VSTD::thread(&__async_assoc_state<_Rp, _Fp>::__execute, __h.get()).detach();
2155 return future<_Rp>(__h.get());
Howard Hinnant95cfd872011-05-19 15:05:04 +00002156}
2157
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04002158#ifndef _LIBCPP_CXX03_LANG
2159
Howard Hinnantc834c512011-11-29 18:15:50 +00002160template <class _Fp, class... _Args>
Louis Dionne3b967d02019-12-10 18:00:42 -05002161class _LIBCPP_HIDDEN __async_func
Howard Hinnant95cfd872011-05-19 15:05:04 +00002162{
Howard Hinnantc834c512011-11-29 18:15:50 +00002163 tuple<_Fp, _Args...> __f_;
Howard Hinnant95cfd872011-05-19 15:05:04 +00002164
2165public:
Howard Hinnantc834c512011-11-29 18:15:50 +00002166 typedef typename __invoke_of<_Fp, _Args...>::type _Rp;
Howard Hinnant95cfd872011-05-19 15:05:04 +00002167
2168 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002169 explicit __async_func(_Fp&& __f, _Args&&... __args)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002170 : __f_(_VSTD::move(__f), _VSTD::move(__args)...) {}
Howard Hinnant95cfd872011-05-19 15:05:04 +00002171
2172 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002173 __async_func(__async_func&& __f) : __f_(_VSTD::move(__f.__f_)) {}
Howard Hinnant95cfd872011-05-19 15:05:04 +00002174
Howard Hinnantc834c512011-11-29 18:15:50 +00002175 _Rp operator()()
Howard Hinnant95cfd872011-05-19 15:05:04 +00002176 {
2177 typedef typename __make_tuple_indices<1+sizeof...(_Args), 1>::type _Index;
2178 return __execute(_Index());
2179 }
2180private:
2181 template <size_t ..._Indices>
Howard Hinnantc834c512011-11-29 18:15:50 +00002182 _Rp
Howard Hinnant95cfd872011-05-19 15:05:04 +00002183 __execute(__tuple_indices<_Indices...>)
2184 {
Arthur O'Dwyer34465da2020-12-15 19:32:29 -05002185 return _VSTD::__invoke(_VSTD::move(_VSTD::get<0>(__f_)), _VSTD::move(_VSTD::get<_Indices>(__f_))...);
Howard Hinnant95cfd872011-05-19 15:05:04 +00002186 }
2187};
2188
Marshall Clowd56a5b62013-11-03 22:06:53 +00002189inline _LIBCPP_INLINE_VISIBILITY bool __does_policy_contain(launch __policy, launch __value )
Marshall Clow335b7992013-11-03 15:43:35 +00002190{ return (int(__policy) & int(__value)) != 0; }
2191
Howard Hinnantc834c512011-11-29 18:15:50 +00002192template <class _Fp, class... _Args>
Marshall Clow6ac349c2017-11-23 01:25:03 +00002193_LIBCPP_NODISCARD_AFTER_CXX17
Howard Hinnantc834c512011-11-29 18:15:50 +00002194future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
2195async(launch __policy, _Fp&& __f, _Args&&... __args)
Howard Hinnantccdd2032010-08-30 18:46:21 +00002196{
Howard Hinnantc834c512011-11-29 18:15:50 +00002197 typedef __async_func<typename decay<_Fp>::type, typename decay<_Args>::type...> _BF;
2198 typedef typename _BF::_Rp _Rp;
Marshall Clow335b7992013-11-03 15:43:35 +00002199
2200#ifndef _LIBCPP_NO_EXCEPTIONS
2201 try
2202 {
2203#endif
2204 if (__does_policy_contain(__policy, launch::async))
Arthur O'Dwyer34465da2020-12-15 19:32:29 -05002205 return _VSTD::__make_async_assoc_state<_Rp>(_BF(_VSTD::__decay_copy(_VSTD::forward<_Fp>(__f)),
2206 _VSTD::__decay_copy(_VSTD::forward<_Args>(__args))...));
Marshall Clow335b7992013-11-03 15:43:35 +00002207#ifndef _LIBCPP_NO_EXCEPTIONS
2208 }
2209 catch ( ... ) { if (__policy == launch::async) throw ; }
2210#endif
2211
2212 if (__does_policy_contain(__policy, launch::deferred))
Arthur O'Dwyer34465da2020-12-15 19:32:29 -05002213 return _VSTD::__make_deferred_assoc_state<_Rp>(_BF(_VSTD::__decay_copy(_VSTD::forward<_Fp>(__f)),
2214 _VSTD::__decay_copy(_VSTD::forward<_Args>(__args))...));
Marshall Clow335b7992013-11-03 15:43:35 +00002215 return future<_Rp>{};
Howard Hinnantccdd2032010-08-30 18:46:21 +00002216}
2217
Howard Hinnantc834c512011-11-29 18:15:50 +00002218template <class _Fp, class... _Args>
Marshall Clow6ac349c2017-11-23 01:25:03 +00002219_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002220future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
2221async(_Fp&& __f, _Args&&... __args)
Howard Hinnantccdd2032010-08-30 18:46:21 +00002222{
Howard Hinnantc834c512011-11-29 18:15:50 +00002223 return _VSTD::async(launch::any, _VSTD::forward<_Fp>(__f),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002224 _VSTD::forward<_Args>(__args)...);
Howard Hinnantccdd2032010-08-30 18:46:21 +00002225}
2226
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04002227#endif // C++03
Howard Hinnantccdd2032010-08-30 18:46:21 +00002228
Howard Hinnante6a10852010-09-03 21:46:37 +00002229// shared_future
2230
Howard Hinnantc834c512011-11-29 18:15:50 +00002231template <class _Rp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002232class _LIBCPP_TEMPLATE_VIS shared_future
Howard Hinnant92646c42010-09-03 18:39:25 +00002233{
Howard Hinnantc834c512011-11-29 18:15:50 +00002234 __assoc_state<_Rp>* __state_;
Howard Hinnant92646c42010-09-03 18:39:25 +00002235
2236public:
Howard Hinnant684902d2010-09-22 14:16:26 +00002237 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00002238 shared_future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant684902d2010-09-22 14:16:26 +00002239 _LIBCPP_INLINE_VISIBILITY
Marshall Clow6f9c48b2016-11-14 19:58:05 +00002240 shared_future(const shared_future& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
Howard Hinnant92646c42010-09-03 18:39:25 +00002241 {if (__state_) __state_->__add_shared();}
Howard Hinnant684902d2010-09-22 14:16:26 +00002242 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00002243 shared_future(future<_Rp>&& __f) _NOEXCEPT : __state_(__f.__state_)
Howard Hinnant92646c42010-09-03 18:39:25 +00002244 {__f.__state_ = nullptr;}
Howard Hinnant684902d2010-09-22 14:16:26 +00002245 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00002246 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
Howard Hinnant92646c42010-09-03 18:39:25 +00002247 {__rhs.__state_ = nullptr;}
Howard Hinnant92646c42010-09-03 18:39:25 +00002248 ~shared_future();
Marshall Clow6f9c48b2016-11-14 19:58:05 +00002249 shared_future& operator=(const shared_future& __rhs) _NOEXCEPT;
Howard Hinnant684902d2010-09-22 14:16:26 +00002250 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00002251 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
Howard Hinnant92646c42010-09-03 18:39:25 +00002252 {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05002253 shared_future(_VSTD::move(__rhs)).swap(*this);
Howard Hinnant92646c42010-09-03 18:39:25 +00002254 return *this;
2255 }
Howard Hinnant92646c42010-09-03 18:39:25 +00002256
2257 // retrieving the value
Howard Hinnant684902d2010-09-22 14:16:26 +00002258 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002259 const _Rp& get() const {return __state_->copy();}
Howard Hinnant92646c42010-09-03 18:39:25 +00002260
Howard Hinnant684902d2010-09-22 14:16:26 +00002261 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00002262 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant92646c42010-09-03 18:39:25 +00002263
2264 // functions to check state
Howard Hinnant684902d2010-09-22 14:16:26 +00002265 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00002266 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant92646c42010-09-03 18:39:25 +00002267
Howard Hinnant684902d2010-09-22 14:16:26 +00002268 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant92646c42010-09-03 18:39:25 +00002269 void wait() const {__state_->wait();}
2270 template <class _Rep, class _Period>
Howard Hinnant684902d2010-09-22 14:16:26 +00002271 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant92646c42010-09-03 18:39:25 +00002272 future_status
2273 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2274 {return __state_->wait_for(__rel_time);}
2275 template <class _Clock, class _Duration>
Howard Hinnant684902d2010-09-22 14:16:26 +00002276 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant92646c42010-09-03 18:39:25 +00002277 future_status
2278 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2279 {return __state_->wait_until(__abs_time);}
2280};
2281
Howard Hinnantc834c512011-11-29 18:15:50 +00002282template <class _Rp>
2283shared_future<_Rp>::~shared_future()
Howard Hinnant92646c42010-09-03 18:39:25 +00002284{
2285 if (__state_)
2286 __state_->__release_shared();
2287}
2288
Howard Hinnantc834c512011-11-29 18:15:50 +00002289template <class _Rp>
2290shared_future<_Rp>&
Marshall Clow6f9c48b2016-11-14 19:58:05 +00002291shared_future<_Rp>::operator=(const shared_future& __rhs) _NOEXCEPT
Howard Hinnant92646c42010-09-03 18:39:25 +00002292{
2293 if (__rhs.__state_)
2294 __rhs.__state_->__add_shared();
2295 if (__state_)
2296 __state_->__release_shared();
2297 __state_ = __rhs.__state_;
2298 return *this;
2299}
2300
Howard Hinnantc834c512011-11-29 18:15:50 +00002301template <class _Rp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002302class _LIBCPP_TEMPLATE_VIS shared_future<_Rp&>
Howard Hinnant92646c42010-09-03 18:39:25 +00002303{
Howard Hinnantc834c512011-11-29 18:15:50 +00002304 __assoc_state<_Rp&>* __state_;
Howard Hinnant92646c42010-09-03 18:39:25 +00002305
2306public:
Howard Hinnant684902d2010-09-22 14:16:26 +00002307 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00002308 shared_future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant684902d2010-09-22 14:16:26 +00002309 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant92646c42010-09-03 18:39:25 +00002310 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2311 {if (__state_) __state_->__add_shared();}
Howard Hinnant684902d2010-09-22 14:16:26 +00002312 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00002313 shared_future(future<_Rp&>&& __f) _NOEXCEPT : __state_(__f.__state_)
Howard Hinnant92646c42010-09-03 18:39:25 +00002314 {__f.__state_ = nullptr;}
Howard Hinnant684902d2010-09-22 14:16:26 +00002315 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00002316 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
Howard Hinnant92646c42010-09-03 18:39:25 +00002317 {__rhs.__state_ = nullptr;}
Howard Hinnant92646c42010-09-03 18:39:25 +00002318 ~shared_future();
2319 shared_future& operator=(const shared_future& __rhs);
Howard Hinnant684902d2010-09-22 14:16:26 +00002320 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00002321 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
Howard Hinnant92646c42010-09-03 18:39:25 +00002322 {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05002323 shared_future(_VSTD::move(__rhs)).swap(*this);
Howard Hinnant92646c42010-09-03 18:39:25 +00002324 return *this;
2325 }
Howard Hinnant92646c42010-09-03 18:39:25 +00002326
2327 // retrieving the value
Howard Hinnant684902d2010-09-22 14:16:26 +00002328 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002329 _Rp& get() const {return __state_->copy();}
Howard Hinnant92646c42010-09-03 18:39:25 +00002330
Howard Hinnant684902d2010-09-22 14:16:26 +00002331 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00002332 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant92646c42010-09-03 18:39:25 +00002333
2334 // functions to check state
Howard Hinnant684902d2010-09-22 14:16:26 +00002335 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00002336 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant92646c42010-09-03 18:39:25 +00002337
Howard Hinnant684902d2010-09-22 14:16:26 +00002338 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant92646c42010-09-03 18:39:25 +00002339 void wait() const {__state_->wait();}
2340 template <class _Rep, class _Period>
Howard Hinnant684902d2010-09-22 14:16:26 +00002341 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant92646c42010-09-03 18:39:25 +00002342 future_status
2343 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2344 {return __state_->wait_for(__rel_time);}
2345 template <class _Clock, class _Duration>
Howard Hinnant684902d2010-09-22 14:16:26 +00002346 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant92646c42010-09-03 18:39:25 +00002347 future_status
2348 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2349 {return __state_->wait_until(__abs_time);}
2350};
2351
Howard Hinnantc834c512011-11-29 18:15:50 +00002352template <class _Rp>
2353shared_future<_Rp&>::~shared_future()
Howard Hinnant92646c42010-09-03 18:39:25 +00002354{
2355 if (__state_)
2356 __state_->__release_shared();
2357}
2358
Howard Hinnantc834c512011-11-29 18:15:50 +00002359template <class _Rp>
2360shared_future<_Rp&>&
2361shared_future<_Rp&>::operator=(const shared_future& __rhs)
Howard Hinnant92646c42010-09-03 18:39:25 +00002362{
2363 if (__rhs.__state_)
2364 __rhs.__state_->__add_shared();
2365 if (__state_)
2366 __state_->__release_shared();
2367 __state_ = __rhs.__state_;
2368 return *this;
2369}
2370
2371template <>
Mehdi Amini228053d2017-05-04 17:08:54 +00002372class _LIBCPP_TYPE_VIS _LIBCPP_AVAILABILITY_FUTURE shared_future<void>
Howard Hinnant92646c42010-09-03 18:39:25 +00002373{
2374 __assoc_sub_state* __state_;
2375
2376public:
Howard Hinnant684902d2010-09-22 14:16:26 +00002377 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00002378 shared_future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant684902d2010-09-22 14:16:26 +00002379 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant92646c42010-09-03 18:39:25 +00002380 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2381 {if (__state_) __state_->__add_shared();}
Howard Hinnant684902d2010-09-22 14:16:26 +00002382 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00002383 shared_future(future<void>&& __f) _NOEXCEPT : __state_(__f.__state_)
Howard Hinnant92646c42010-09-03 18:39:25 +00002384 {__f.__state_ = nullptr;}
Howard Hinnant684902d2010-09-22 14:16:26 +00002385 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00002386 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
Howard Hinnant92646c42010-09-03 18:39:25 +00002387 {__rhs.__state_ = nullptr;}
Howard Hinnant92646c42010-09-03 18:39:25 +00002388 ~shared_future();
2389 shared_future& operator=(const shared_future& __rhs);
Howard Hinnant684902d2010-09-22 14:16:26 +00002390 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00002391 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
Howard Hinnant92646c42010-09-03 18:39:25 +00002392 {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05002393 shared_future(_VSTD::move(__rhs)).swap(*this);
Howard Hinnant92646c42010-09-03 18:39:25 +00002394 return *this;
2395 }
Howard Hinnant92646c42010-09-03 18:39:25 +00002396
2397 // retrieving the value
Howard Hinnant684902d2010-09-22 14:16:26 +00002398 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant92646c42010-09-03 18:39:25 +00002399 void get() const {__state_->copy();}
2400
Howard Hinnant684902d2010-09-22 14:16:26 +00002401 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00002402 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant92646c42010-09-03 18:39:25 +00002403
2404 // functions to check state
Howard Hinnant684902d2010-09-22 14:16:26 +00002405 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant22448042012-07-21 17:46:55 +00002406 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant92646c42010-09-03 18:39:25 +00002407
Howard Hinnant684902d2010-09-22 14:16:26 +00002408 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant92646c42010-09-03 18:39:25 +00002409 void wait() const {__state_->wait();}
2410 template <class _Rep, class _Period>
Howard Hinnant684902d2010-09-22 14:16:26 +00002411 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant92646c42010-09-03 18:39:25 +00002412 future_status
2413 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2414 {return __state_->wait_for(__rel_time);}
2415 template <class _Clock, class _Duration>
Howard Hinnant684902d2010-09-22 14:16:26 +00002416 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant92646c42010-09-03 18:39:25 +00002417 future_status
2418 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2419 {return __state_->wait_until(__abs_time);}
2420};
2421
Howard Hinnantc834c512011-11-29 18:15:50 +00002422template <class _Rp>
Howard Hinnant92646c42010-09-03 18:39:25 +00002423inline _LIBCPP_INLINE_VISIBILITY
2424void
Howard Hinnant22448042012-07-21 17:46:55 +00002425swap(shared_future<_Rp>& __x, shared_future<_Rp>& __y) _NOEXCEPT
Howard Hinnant92646c42010-09-03 18:39:25 +00002426{
2427 __x.swap(__y);
2428}
2429
Howard Hinnantc834c512011-11-29 18:15:50 +00002430template <class _Rp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002431inline
Howard Hinnantc834c512011-11-29 18:15:50 +00002432shared_future<_Rp>
Marshall Clow79e02112017-01-24 23:28:25 +00002433future<_Rp>::share() _NOEXCEPT
Howard Hinnante6a10852010-09-03 21:46:37 +00002434{
Howard Hinnantc834c512011-11-29 18:15:50 +00002435 return shared_future<_Rp>(_VSTD::move(*this));
Howard Hinnante6a10852010-09-03 21:46:37 +00002436}
2437
Howard Hinnantc834c512011-11-29 18:15:50 +00002438template <class _Rp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002439inline
Howard Hinnantc834c512011-11-29 18:15:50 +00002440shared_future<_Rp&>
Marshall Clow79e02112017-01-24 23:28:25 +00002441future<_Rp&>::share() _NOEXCEPT
Howard Hinnante6a10852010-09-03 21:46:37 +00002442{
Howard Hinnantc834c512011-11-29 18:15:50 +00002443 return shared_future<_Rp&>(_VSTD::move(*this));
Howard Hinnant9a7d09c2010-11-30 20:23:32 +00002444}
2445
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002446inline
Howard Hinnant9a7d09c2010-11-30 20:23:32 +00002447shared_future<void>
Marshall Clow79e02112017-01-24 23:28:25 +00002448future<void>::share() _NOEXCEPT
Howard Hinnant9a7d09c2010-11-30 20:23:32 +00002449{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002450 return shared_future<void>(_VSTD::move(*this));
Howard Hinnante6a10852010-09-03 21:46:37 +00002451}
2452
Howard Hinnantc51e1022010-05-11 19:42:16 +00002453_LIBCPP_END_NAMESPACE_STD
2454
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +00002455#endif // !_LIBCPP_HAS_NO_THREADS
2456
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002457#endif // _LIBCPP_FUTURE