blob: e38876758e1381528fecff2ccaf50d71a2dc269b [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:161// -*- C++ -*-
2//===--------------------------- future -----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:014// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:165//
Howard Hinnantb64f8b02010-11-16 22:09:026// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:168//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_FUTURE
12#define _LIBCPP_FUTURE
13
14/*
15 future synopsis
16
17namespace std
18{
19
20enum class future_errc
21{
Howard Hinnantcd942f12013-09-14 18:20:1022 future_already_retrieved = 1,
Howard Hinnantbc8d3f92010-05-11 19:42:1623 promise_already_satisfied,
Howard Hinnantcd942f12013-09-14 18:20:1024 no_state,
25 broken_promise
Howard Hinnantbc8d3f92010-05-11 19:42:1626};
27
28enum class launch
29{
Howard Hinnant66895642010-11-23 18:33:5430 async = 1,
31 deferred = 2,
32 any = async | deferred
Howard Hinnantbc8d3f92010-05-11 19:42:1633};
34
35enum class future_status
36{
37 ready,
38 timeout,
39 deferred
40};
41
42template <> struct is_error_code_enum<future_errc> : public true_type { };
Howard Hinnant8bf01dd2012-07-21 17:46:5543error_code make_error_code(future_errc e) noexcept;
44error_condition make_error_condition(future_errc e) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1645
Howard Hinnant8bf01dd2012-07-21 17:46:5546const error_category& future_category() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1647
48class future_error
49 : public logic_error
50{
51public:
52 future_error(error_code ec); // exposition only
Marshall Clow5ec20df2016-11-14 18:56:2453 explicit future_error(future_errc); // C++17
Howard Hinnant8bf01dd2012-07-21 17:46:5554 const error_code& code() const noexcept;
55 const char* what() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1656};
57
58template <class R>
59class promise
60{
61public:
62 promise();
63 template <class Allocator>
64 promise(allocator_arg_t, const Allocator& a);
Howard Hinnant8bf01dd2012-07-21 17:46:5565 promise(promise&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1666 promise(const promise& rhs) = delete;
67 ~promise();
68
69 // assignment
Howard Hinnant8bf01dd2012-07-21 17:46:5570 promise& operator=(promise&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1671 promise& operator=(const promise& rhs) = delete;
Howard Hinnant8bf01dd2012-07-21 17:46:5572 void swap(promise& other) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1673
74 // retrieving the result
75 future<R> get_future();
76
77 // setting the result
78 void set_value(const R& r);
79 void set_value(R&& r);
80 void set_exception(exception_ptr p);
81
82 // setting the result with deferred notification
83 void set_value_at_thread_exit(const R& r);
84 void set_value_at_thread_exit(R&& r);
85 void set_exception_at_thread_exit(exception_ptr p);
86};
87
88template <class R>
89class promise<R&>
90{
91public:
92 promise();
93 template <class Allocator>
94 promise(allocator_arg_t, const Allocator& a);
Howard Hinnant8bf01dd2012-07-21 17:46:5595 promise(promise&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1696 promise(const promise& rhs) = delete;
97 ~promise();
98
99 // assignment
Howard Hinnant8bf01dd2012-07-21 17:46:55100 promise& operator=(promise&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16101 promise& operator=(const promise& rhs) = delete;
Howard Hinnant8bf01dd2012-07-21 17:46:55102 void swap(promise& other) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16103
104 // retrieving the result
Howard Hinnant47499b12010-08-27 20:10:19105 future<R&> get_future();
Howard Hinnantbc8d3f92010-05-11 19:42:16106
107 // setting the result
108 void set_value(R& r);
109 void set_exception(exception_ptr p);
110
111 // setting the result with deferred notification
112 void set_value_at_thread_exit(R&);
113 void set_exception_at_thread_exit(exception_ptr p);
114};
115
116template <>
117class promise<void>
118{
119public:
120 promise();
121 template <class Allocator>
122 promise(allocator_arg_t, const Allocator& a);
Howard Hinnant8bf01dd2012-07-21 17:46:55123 promise(promise&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16124 promise(const promise& rhs) = delete;
125 ~promise();
126
127 // assignment
Howard Hinnant8bf01dd2012-07-21 17:46:55128 promise& operator=(promise&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16129 promise& operator=(const promise& rhs) = delete;
Howard Hinnant8bf01dd2012-07-21 17:46:55130 void swap(promise& other) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16131
132 // retrieving the result
Howard Hinnant47499b12010-08-27 20:10:19133 future<void> get_future();
Howard Hinnantbc8d3f92010-05-11 19:42:16134
135 // setting the result
136 void set_value();
137 void set_exception(exception_ptr p);
138
139 // setting the result with deferred notification
140 void set_value_at_thread_exit();
141 void set_exception_at_thread_exit(exception_ptr p);
142};
143
Howard Hinnant8bf01dd2012-07-21 17:46:55144template <class R> void swap(promise<R>& x, promise<R>& y) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16145
146template <class R, class Alloc>
147 struct uses_allocator<promise<R>, Alloc> : public true_type {};
148
149template <class R>
150class future
151{
152public:
Howard Hinnant8bf01dd2012-07-21 17:46:55153 future() noexcept;
154 future(future&&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16155 future(const future& rhs) = delete;
156 ~future();
157 future& operator=(const future& rhs) = delete;
Howard Hinnant8bf01dd2012-07-21 17:46:55158 future& operator=(future&&) noexcept;
Marshall Clow19cd3fd2017-01-25 20:14:03159 shared_future<R> share() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16160
161 // retrieving the value
162 R get();
163
164 // functions to check state
Howard Hinnant8bf01dd2012-07-21 17:46:55165 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16166
167 void wait() const;
168 template <class Rep, class Period>
169 future_status
170 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
171 template <class Clock, class Duration>
172 future_status
173 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
174};
175
176template <class R>
177class future<R&>
178{
179public:
Howard Hinnant8bf01dd2012-07-21 17:46:55180 future() noexcept;
181 future(future&&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16182 future(const future& rhs) = delete;
183 ~future();
184 future& operator=(const future& rhs) = delete;
Howard Hinnant8bf01dd2012-07-21 17:46:55185 future& operator=(future&&) noexcept;
Marshall Clow9bb0cca2017-01-24 23:28:25186 shared_future<R&> share() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16187
188 // retrieving the value
189 R& get();
190
191 // functions to check state
Howard Hinnant8bf01dd2012-07-21 17:46:55192 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16193
194 void wait() const;
195 template <class Rep, class Period>
196 future_status
197 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
198 template <class Clock, class Duration>
199 future_status
200 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
201};
202
203template <>
204class future<void>
205{
206public:
Howard Hinnant8bf01dd2012-07-21 17:46:55207 future() noexcept;
208 future(future&&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16209 future(const future& rhs) = delete;
210 ~future();
211 future& operator=(const future& rhs) = delete;
Howard Hinnant8bf01dd2012-07-21 17:46:55212 future& operator=(future&&) noexcept;
Marshall Clow9bb0cca2017-01-24 23:28:25213 shared_future<void> share() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16214
215 // retrieving the value
216 void get();
217
218 // functions to check state
Howard Hinnant8bf01dd2012-07-21 17:46:55219 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16220
221 void wait() const;
222 template <class Rep, class Period>
223 future_status
224 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
225 template <class Clock, class Duration>
226 future_status
227 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
228};
229
230template <class R>
231class shared_future
232{
233public:
Howard Hinnant8bf01dd2012-07-21 17:46:55234 shared_future() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16235 shared_future(const shared_future& rhs);
Howard Hinnant8bf01dd2012-07-21 17:46:55236 shared_future(future<R>&&) noexcept;
237 shared_future(shared_future&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16238 ~shared_future();
239 shared_future& operator=(const shared_future& rhs);
Howard Hinnant8bf01dd2012-07-21 17:46:55240 shared_future& operator=(shared_future&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16241
242 // retrieving the value
243 const R& get() const;
244
245 // functions to check state
Howard Hinnant8bf01dd2012-07-21 17:46:55246 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16247
248 void wait() const;
249 template <class Rep, class Period>
250 future_status
251 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
252 template <class Clock, class Duration>
253 future_status
254 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
255};
256
257template <class R>
258class shared_future<R&>
259{
260public:
Howard Hinnant8bf01dd2012-07-21 17:46:55261 shared_future() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16262 shared_future(const shared_future& rhs);
Howard Hinnant8bf01dd2012-07-21 17:46:55263 shared_future(future<R&>&&) noexcept;
264 shared_future(shared_future&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16265 ~shared_future();
266 shared_future& operator=(const shared_future& rhs);
Howard Hinnant8bf01dd2012-07-21 17:46:55267 shared_future& operator=(shared_future&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16268
269 // retrieving the value
270 R& get() const;
271
272 // functions to check state
Howard Hinnant8bf01dd2012-07-21 17:46:55273 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16274
275 void wait() const;
276 template <class Rep, class Period>
277 future_status
278 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
279 template <class Clock, class Duration>
280 future_status
281 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
282};
283
284template <>
285class shared_future<void>
286{
287public:
Howard Hinnant8bf01dd2012-07-21 17:46:55288 shared_future() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16289 shared_future(const shared_future& rhs);
Howard Hinnant8bf01dd2012-07-21 17:46:55290 shared_future(future<void>&&) noexcept;
291 shared_future(shared_future&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16292 ~shared_future();
293 shared_future& operator=(const shared_future& rhs);
Howard Hinnant8bf01dd2012-07-21 17:46:55294 shared_future& operator=(shared_future&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16295
296 // retrieving the value
297 void get() const;
298
299 // functions to check state
Howard Hinnant8bf01dd2012-07-21 17:46:55300 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16301
302 void wait() const;
303 template <class Rep, class Period>
304 future_status
305 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
306 template <class Clock, class Duration>
307 future_status
308 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
309};
310
Howard Hinnantbc8d3f92010-05-11 19:42:16311template <class F, class... Args>
Howard Hinnant0836f872013-09-21 18:17:23312 future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16313 async(F&& f, Args&&... args);
314
315template <class F, class... Args>
Howard Hinnant0836f872013-09-21 18:17:23316 future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16317 async(launch policy, F&& f, Args&&... args);
318
Howard Hinnantf5256e12010-05-11 21:36:01319template <class> class packaged_task; // undefined
Howard Hinnantbc8d3f92010-05-11 19:42:16320
321template <class R, class... ArgTypes>
322class packaged_task<R(ArgTypes...)>
323{
324public:
Eric Fiselier68db6cd2016-06-01 21:05:53325 typedef R result_type; // extension
Howard Hinnantbc8d3f92010-05-11 19:42:16326
327 // construction and destruction
Howard Hinnant8bf01dd2012-07-21 17:46:55328 packaged_task() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16329 template <class F>
Howard Hinnantbc8d3f92010-05-11 19:42:16330 explicit packaged_task(F&& f);
331 template <class F, class Allocator>
Marshall Clow07546f32015-06-30 14:16:49332 packaged_task(allocator_arg_t, const Allocator& a, F&& f);
Howard Hinnantbc8d3f92010-05-11 19:42:16333 ~packaged_task();
334
335 // no copy
Howard Hinnant8131a012012-07-21 19:34:12336 packaged_task(const packaged_task&) = delete;
337 packaged_task& operator=(const packaged_task&) = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16338
339 // move support
Howard Hinnant8bf01dd2012-07-21 17:46:55340 packaged_task(packaged_task&& other) noexcept;
341 packaged_task& operator=(packaged_task&& other) noexcept;
342 void swap(packaged_task& other) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16343
Howard Hinnant8bf01dd2012-07-21 17:46:55344 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16345
346 // result retrieval
347 future<R> get_future();
348
349 // execution
350 void operator()(ArgTypes... );
351 void make_ready_at_thread_exit(ArgTypes...);
352
353 void reset();
354};
355
356template <class R>
Howard Hinnant8bf01dd2012-07-21 17:46:55357 void swap(packaged_task<R(ArgTypes...)&, packaged_task<R(ArgTypes...)>&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16358
359template <class R, class Alloc> struct uses_allocator<packaged_task<R>, Alloc>;
360
361} // std
362
363*/
364
365#include <__config>
366#include <system_error>
Howard Hinnant47499b12010-08-27 20:10:19367#include <memory>
368#include <chrono>
369#include <exception>
Howard Hinnante6e4d012010-09-03 21:46:37370#include <mutex>
Howard Hinnant47499b12010-08-27 20:10:19371#include <thread>
Howard Hinnantbc8d3f92010-05-11 19:42:16372
Howard Hinnant08e17472011-10-17 20:05:10373#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16374#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10375#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16376
Jonathan Roelofsbaed05d2014-09-05 20:28:44377#ifdef _LIBCPP_HAS_NO_THREADS
Jonathan Roelofs8d86b2e2014-09-05 19:45:05378#error <future> is not supported on this single threaded system
379#else // !_LIBCPP_HAS_NO_THREADS
380
Howard Hinnantbc8d3f92010-05-11 19:42:16381_LIBCPP_BEGIN_NAMESPACE_STD
382
383//enum class future_errc
Howard Hinnantf6d875f2011-12-02 19:36:40384_LIBCPP_DECLARE_STRONG_ENUM(future_errc)
Howard Hinnantbc8d3f92010-05-11 19:42:16385{
Howard Hinnantcd942f12013-09-14 18:20:10386 future_already_retrieved = 1,
Howard Hinnantbc8d3f92010-05-11 19:42:16387 promise_already_satisfied,
Howard Hinnantcd942f12013-09-14 18:20:10388 no_state,
389 broken_promise
Howard Hinnantbc8d3f92010-05-11 19:42:16390};
Howard Hinnantf6d875f2011-12-02 19:36:40391_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_errc)
Howard Hinnantbc8d3f92010-05-11 19:42:16392
Howard Hinnant8c6cbb22010-09-22 14:16:26393template <>
Eric Fiselierc3589a82017-01-04 23:56:00394struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<future_errc> : public true_type {};
Howard Hinnanta6521722010-08-25 17:32:05395
Howard Hinnantf6d875f2011-12-02 19:36:40396#ifdef _LIBCPP_HAS_NO_STRONG_ENUMS
397template <>
Eric Fiselierc3589a82017-01-04 23:56:00398struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<future_errc::__lx> : public true_type { };
Howard Hinnantf6d875f2011-12-02 19:36:40399#endif
400
Howard Hinnantbc8d3f92010-05-11 19:42:16401//enum class launch
Howard Hinnantf6d875f2011-12-02 19:36:40402_LIBCPP_DECLARE_STRONG_ENUM(launch)
Howard Hinnantbc8d3f92010-05-11 19:42:16403{
Howard Hinnant66895642010-11-23 18:33:54404 async = 1,
405 deferred = 2,
406 any = async | deferred
Howard Hinnantbc8d3f92010-05-11 19:42:16407};
Howard Hinnantf6d875f2011-12-02 19:36:40408_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(launch)
Howard Hinnantbc8d3f92010-05-11 19:42:16409
Howard Hinnantf491e512013-06-29 18:38:17410#ifndef _LIBCPP_HAS_NO_STRONG_ENUMS
411
412#ifdef _LIBCXX_UNDERLYING_TYPE
413typedef underlying_type<launch>::type __launch_underlying_type;
414#else
415typedef int __launch_underlying_type;
416#endif
417
418inline _LIBCPP_INLINE_VISIBILITY
419_LIBCPP_CONSTEXPR
420launch
421operator&(launch __x, launch __y)
422{
423 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) &
424 static_cast<__launch_underlying_type>(__y));
425}
426
427inline _LIBCPP_INLINE_VISIBILITY
428_LIBCPP_CONSTEXPR
429launch
430operator|(launch __x, launch __y)
431{
432 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) |
433 static_cast<__launch_underlying_type>(__y));
434}
435
436inline _LIBCPP_INLINE_VISIBILITY
437_LIBCPP_CONSTEXPR
438launch
439operator^(launch __x, launch __y)
440{
441 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) ^
442 static_cast<__launch_underlying_type>(__y));
443}
444
445inline _LIBCPP_INLINE_VISIBILITY
446_LIBCPP_CONSTEXPR
447launch
448operator~(launch __x)
449{
Howard Hinnant6a683bf2013-07-02 18:01:41450 return static_cast<launch>(~static_cast<__launch_underlying_type>(__x) & 3);
Howard Hinnantf491e512013-06-29 18:38:17451}
452
453inline _LIBCPP_INLINE_VISIBILITY
454launch&
455operator&=(launch& __x, launch __y)
456{
457 __x = __x & __y; return __x;
458}
459
460inline _LIBCPP_INLINE_VISIBILITY
461launch&
462operator|=(launch& __x, launch __y)
463{
464 __x = __x | __y; return __x;
465}
466
467inline _LIBCPP_INLINE_VISIBILITY
468launch&
469operator^=(launch& __x, launch __y)
470{
471 __x = __x ^ __y; return __x;
472}
473
474#endif // !_LIBCPP_HAS_NO_STRONG_ENUMS
475
Howard Hinnantbc8d3f92010-05-11 19:42:16476//enum class future_status
Howard Hinnantf6d875f2011-12-02 19:36:40477_LIBCPP_DECLARE_STRONG_ENUM(future_status)
Howard Hinnantbc8d3f92010-05-11 19:42:16478{
Howard Hinnantbc8d3f92010-05-11 19:42:16479 ready,
480 timeout,
481 deferred
482};
Howard Hinnantf6d875f2011-12-02 19:36:40483_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_status)
Howard Hinnantbc8d3f92010-05-11 19:42:16484
Howard Hinnant83eade62013-03-06 23:30:19485_LIBCPP_FUNC_VIS
Howard Hinnant8bf01dd2012-07-21 17:46:55486const error_category& future_category() _NOEXCEPT;
Howard Hinnanta6521722010-08-25 17:32:05487
488inline _LIBCPP_INLINE_VISIBILITY
489error_code
Howard Hinnant8bf01dd2012-07-21 17:46:55490make_error_code(future_errc __e) _NOEXCEPT
Howard Hinnanta6521722010-08-25 17:32:05491{
492 return error_code(static_cast<int>(__e), future_category());
493}
494
495inline _LIBCPP_INLINE_VISIBILITY
496error_condition
Howard Hinnant8bf01dd2012-07-21 17:46:55497make_error_condition(future_errc __e) _NOEXCEPT
Howard Hinnanta6521722010-08-25 17:32:05498{
499 return error_condition(static_cast<int>(__e), future_category());
500}
501
Mehdi Amini907c1192017-05-04 17:08:54502class _LIBCPP_EXCEPTION_ABI _LIBCPP_AVAILABILITY_FUTURE_ERROR future_error
Howard Hinnanta6521722010-08-25 17:32:05503 : public logic_error
504{
505 error_code __ec_;
506public:
507 future_error(error_code __ec);
Marshall Clow5ec20df2016-11-14 18:56:24508#if _LIBCPP_STD_VERS > 14
509 explicit future_error(future_errc _Ev) : logic_error(), __ec_(make_error_code(_Ev)) {}
510#endif
Howard Hinnant8c6cbb22010-09-22 14:16:26511 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55512 const error_code& code() const _NOEXCEPT {return __ec_;}
Howard Hinnantac6de542011-07-07 21:03:52513
514 virtual ~future_error() _NOEXCEPT;
Howard Hinnanta6521722010-08-25 17:32:05515};
516
Marshall Clow14c09a22016-08-25 15:09:01517_LIBCPP_NORETURN inline _LIBCPP_ALWAYS_INLINE
Mehdi Amini907c1192017-05-04 17:08:54518#ifndef _LIBCPP_NO_EXCEPTIONS
519_LIBCPP_AVAILABILITY_FUTURE_ERROR
520#endif
Eric Fiselier423ca202015-10-02 21:25:15521void __throw_future_error(future_errc _Ev)
Marshall Clowa1899742015-09-03 15:11:32522{
523#ifndef _LIBCPP_NO_EXCEPTIONS
524 throw future_error(make_error_code(_Ev));
525#else
Eric Fiselier2ab8f622016-12-24 01:56:25526 ((void)_Ev);
Marshall Clow14c09a22016-08-25 15:09:01527 _VSTD::abort();
Marshall Clowa1899742015-09-03 15:11:32528#endif
529}
530
Mehdi Amini907c1192017-05-04 17:08:54531class _LIBCPP_TYPE_VIS _LIBCPP_AVAILABILITY_FUTURE __assoc_sub_state
Howard Hinnant47499b12010-08-27 20:10:19532 : public __shared_count
533{
534protected:
535 exception_ptr __exception_;
536 mutable mutex __mut_;
537 mutable condition_variable __cv_;
538 unsigned __state_;
539
Howard Hinnant1694d232011-05-28 14:41:13540 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant54da3382010-08-30 18:46:21541 void __sub_wait(unique_lock<mutex>& __lk);
Howard Hinnant47499b12010-08-27 20:10:19542public:
543 enum
544 {
545 __constructed = 1,
546 __future_attached = 2,
547 ready = 4,
548 deferred = 8
549 };
550
Howard Hinnant8c6cbb22010-09-22 14:16:26551 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19552 __assoc_sub_state() : __state_(0) {}
553
Howard Hinnant8c6cbb22010-09-22 14:16:26554 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19555 bool __has_value() const
556 {return (__state_ & __constructed) || (__exception_ != nullptr);}
557
Howard Hinnant8c6cbb22010-09-22 14:16:26558 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1b031c92013-01-14 20:01:24559 void __set_future_attached()
560 {
561 lock_guard<mutex> __lk(__mut_);
562 __state_ |= __future_attached;
563 }
Howard Hinnant8c6cbb22010-09-22 14:16:26564 _LIBCPP_INLINE_VISIBILITY
Marshall Clow9de3d4c2013-10-13 01:02:45565 bool __has_future_attached() const {return (__state_ & __future_attached) != 0;}
Howard Hinnant47499b12010-08-27 20:10:19566
Howard Hinnant8c6cbb22010-09-22 14:16:26567 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21568 void __set_deferred() {__state_ |= deferred;}
569
Howard Hinnant47499b12010-08-27 20:10:19570 void __make_ready();
Howard Hinnant8c6cbb22010-09-22 14:16:26571 _LIBCPP_INLINE_VISIBILITY
Marshall Clow9de3d4c2013-10-13 01:02:45572 bool __is_ready() const {return (__state_ & ready) != 0;}
Howard Hinnant47499b12010-08-27 20:10:19573
574 void set_value();
575 void set_value_at_thread_exit();
576
577 void set_exception(exception_ptr __p);
578 void set_exception_at_thread_exit(exception_ptr __p);
579
580 void copy();
581
Howard Hinnant54da3382010-08-30 18:46:21582 void wait();
Howard Hinnant47499b12010-08-27 20:10:19583 template <class _Rep, class _Period>
584 future_status
Evgeniy Stepanova3b25f82015-11-07 01:22:13585 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19586 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const;
587 template <class _Clock, class _Duration>
Shoaib Meenai6b734922017-03-02 03:22:18588 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Howard Hinnant47499b12010-08-27 20:10:19589 future_status
590 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const;
Howard Hinnant54da3382010-08-30 18:46:21591
592 virtual void __execute();
Howard Hinnant47499b12010-08-27 20:10:19593};
594
Howard Hinnantf39daa82010-08-28 21:01:06595template <class _Clock, class _Duration>
596future_status
597__assoc_sub_state::wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
598{
599 unique_lock<mutex> __lk(__mut_);
Howard Hinnant54da3382010-08-30 18:46:21600 if (__state_ & deferred)
601 return future_status::deferred;
602 while (!(__state_ & ready) && _Clock::now() < __abs_time)
Howard Hinnantf39daa82010-08-28 21:01:06603 __cv_.wait_until(__lk, __abs_time);
604 if (__state_ & ready)
605 return future_status::ready;
Howard Hinnantf39daa82010-08-28 21:01:06606 return future_status::timeout;
607}
608
609template <class _Rep, class _Period>
Evgeniy Stepanova3b25f82015-11-07 01:22:13610inline
Howard Hinnantf39daa82010-08-28 21:01:06611future_status
612__assoc_sub_state::wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
613{
Howard Hinnantf8f85212010-11-20 19:16:30614 return wait_until(chrono::steady_clock::now() + __rel_time);
Howard Hinnantf39daa82010-08-28 21:01:06615}
616
Howard Hinnant99968442011-11-29 18:15:50617template <class _Rp>
Mehdi Amini907c1192017-05-04 17:08:54618class _LIBCPP_AVAILABILITY_FUTURE __assoc_state
Howard Hinnant47499b12010-08-27 20:10:19619 : public __assoc_sub_state
620{
621 typedef __assoc_sub_state base;
Howard Hinnant99968442011-11-29 18:15:50622 typedef typename aligned_storage<sizeof(_Rp), alignment_of<_Rp>::value>::type _Up;
Howard Hinnant47499b12010-08-27 20:10:19623protected:
Howard Hinnant99968442011-11-29 18:15:50624 _Up __value_;
Howard Hinnant47499b12010-08-27 20:10:19625
Howard Hinnant1694d232011-05-28 14:41:13626 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant47499b12010-08-27 20:10:19627public:
628
629 template <class _Arg>
Howard Hinnant73d21a42010-09-04 23:28:19630#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19631 void set_value(_Arg&& __arg);
632#else
633 void set_value(_Arg& __arg);
634#endif
635
636 template <class _Arg>
Howard Hinnant73d21a42010-09-04 23:28:19637#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19638 void set_value_at_thread_exit(_Arg&& __arg);
639#else
640 void set_value_at_thread_exit(_Arg& __arg);
641#endif
642
Howard Hinnant99968442011-11-29 18:15:50643 _Rp move();
644 typename add_lvalue_reference<_Rp>::type copy();
Howard Hinnant47499b12010-08-27 20:10:19645};
646
Howard Hinnant99968442011-11-29 18:15:50647template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19648void
Howard Hinnant99968442011-11-29 18:15:50649__assoc_state<_Rp>::__on_zero_shared() _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19650{
651 if (this->__state_ & base::__constructed)
Howard Hinnant99968442011-11-29 18:15:50652 reinterpret_cast<_Rp*>(&__value_)->~_Rp();
Howard Hinnant47499b12010-08-27 20:10:19653 delete this;
654}
655
Howard Hinnant99968442011-11-29 18:15:50656template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19657template <class _Arg>
Mehdi Amini907c1192017-05-04 17:08:54658_LIBCPP_AVAILABILITY_FUTURE
Howard Hinnant47499b12010-08-27 20:10:19659void
Howard Hinnant73d21a42010-09-04 23:28:19660#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50661__assoc_state<_Rp>::set_value(_Arg&& __arg)
Howard Hinnant47499b12010-08-27 20:10:19662#else
Howard Hinnant99968442011-11-29 18:15:50663__assoc_state<_Rp>::set_value(_Arg& __arg)
Howard Hinnant47499b12010-08-27 20:10:19664#endif
665{
666 unique_lock<mutex> __lk(this->__mut_);
667 if (this->__has_value())
Eric Fiselier423ca202015-10-02 21:25:15668 __throw_future_error(future_errc::promise_already_satisfied);
Howard Hinnant99968442011-11-29 18:15:50669 ::new(&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
Howard Hinnant47499b12010-08-27 20:10:19670 this->__state_ |= base::__constructed | base::ready;
Howard Hinnant47499b12010-08-27 20:10:19671 __cv_.notify_all();
672}
673
Howard Hinnant99968442011-11-29 18:15:50674template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19675template <class _Arg>
676void
Howard Hinnant73d21a42010-09-04 23:28:19677#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50678__assoc_state<_Rp>::set_value_at_thread_exit(_Arg&& __arg)
Howard Hinnant47499b12010-08-27 20:10:19679#else
Howard Hinnant99968442011-11-29 18:15:50680__assoc_state<_Rp>::set_value_at_thread_exit(_Arg& __arg)
Howard Hinnant47499b12010-08-27 20:10:19681#endif
682{
683 unique_lock<mutex> __lk(this->__mut_);
684 if (this->__has_value())
Eric Fiselier423ca202015-10-02 21:25:15685 __throw_future_error(future_errc::promise_already_satisfied);
Howard Hinnant99968442011-11-29 18:15:50686 ::new(&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
Howard Hinnant47499b12010-08-27 20:10:19687 this->__state_ |= base::__constructed;
Howard Hinnant5306d682010-10-14 19:18:04688 __thread_local_data()->__make_ready_at_thread_exit(this);
Howard Hinnant47499b12010-08-27 20:10:19689}
690
Howard Hinnant99968442011-11-29 18:15:50691template <class _Rp>
692_Rp
693__assoc_state<_Rp>::move()
Howard Hinnant47499b12010-08-27 20:10:19694{
695 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant54da3382010-08-30 18:46:21696 this->__sub_wait(__lk);
Howard Hinnant47499b12010-08-27 20:10:19697 if (this->__exception_ != nullptr)
698 rethrow_exception(this->__exception_);
Howard Hinnant99968442011-11-29 18:15:50699 return _VSTD::move(*reinterpret_cast<_Rp*>(&__value_));
Howard Hinnant47499b12010-08-27 20:10:19700}
701
Howard Hinnant99968442011-11-29 18:15:50702template <class _Rp>
703typename add_lvalue_reference<_Rp>::type
704__assoc_state<_Rp>::copy()
Howard Hinnant47499b12010-08-27 20:10:19705{
706 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant54da3382010-08-30 18:46:21707 this->__sub_wait(__lk);
Howard Hinnant47499b12010-08-27 20:10:19708 if (this->__exception_ != nullptr)
709 rethrow_exception(this->__exception_);
Howard Hinnant99968442011-11-29 18:15:50710 return *reinterpret_cast<_Rp*>(&__value_);
Howard Hinnant47499b12010-08-27 20:10:19711}
712
Howard Hinnant99968442011-11-29 18:15:50713template <class _Rp>
Mehdi Amini907c1192017-05-04 17:08:54714class _LIBCPP_AVAILABILITY_FUTURE __assoc_state<_Rp&>
Howard Hinnantf39daa82010-08-28 21:01:06715 : public __assoc_sub_state
716{
717 typedef __assoc_sub_state base;
Howard Hinnant99968442011-11-29 18:15:50718 typedef _Rp* _Up;
Howard Hinnantf39daa82010-08-28 21:01:06719protected:
Howard Hinnant99968442011-11-29 18:15:50720 _Up __value_;
Howard Hinnantf39daa82010-08-28 21:01:06721
Howard Hinnant1694d232011-05-28 14:41:13722 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnantf39daa82010-08-28 21:01:06723public:
724
Howard Hinnant99968442011-11-29 18:15:50725 void set_value(_Rp& __arg);
726 void set_value_at_thread_exit(_Rp& __arg);
Howard Hinnantf39daa82010-08-28 21:01:06727
Howard Hinnant99968442011-11-29 18:15:50728 _Rp& copy();
Howard Hinnantf39daa82010-08-28 21:01:06729};
730
Howard Hinnant99968442011-11-29 18:15:50731template <class _Rp>
Howard Hinnantf39daa82010-08-28 21:01:06732void
Howard Hinnant99968442011-11-29 18:15:50733__assoc_state<_Rp&>::__on_zero_shared() _NOEXCEPT
Howard Hinnantf39daa82010-08-28 21:01:06734{
735 delete this;
736}
737
Howard Hinnant99968442011-11-29 18:15:50738template <class _Rp>
Howard Hinnantf39daa82010-08-28 21:01:06739void
Howard Hinnant99968442011-11-29 18:15:50740__assoc_state<_Rp&>::set_value(_Rp& __arg)
Howard Hinnantf39daa82010-08-28 21:01:06741{
742 unique_lock<mutex> __lk(this->__mut_);
743 if (this->__has_value())
Eric Fiselier423ca202015-10-02 21:25:15744 __throw_future_error(future_errc::promise_already_satisfied);
Howard Hinnanta4e87ab2013-08-08 18:38:55745 __value_ = _VSTD::addressof(__arg);
Howard Hinnantf39daa82010-08-28 21:01:06746 this->__state_ |= base::__constructed | base::ready;
Howard Hinnantf39daa82010-08-28 21:01:06747 __cv_.notify_all();
748}
749
Howard Hinnant99968442011-11-29 18:15:50750template <class _Rp>
Howard Hinnantf39daa82010-08-28 21:01:06751void
Howard Hinnant99968442011-11-29 18:15:50752__assoc_state<_Rp&>::set_value_at_thread_exit(_Rp& __arg)
Howard Hinnantf39daa82010-08-28 21:01:06753{
754 unique_lock<mutex> __lk(this->__mut_);
755 if (this->__has_value())
Eric Fiselier423ca202015-10-02 21:25:15756 __throw_future_error(future_errc::promise_already_satisfied);
Howard Hinnanta4e87ab2013-08-08 18:38:55757 __value_ = _VSTD::addressof(__arg);
Howard Hinnantf39daa82010-08-28 21:01:06758 this->__state_ |= base::__constructed;
Howard Hinnant5306d682010-10-14 19:18:04759 __thread_local_data()->__make_ready_at_thread_exit(this);
Howard Hinnantf39daa82010-08-28 21:01:06760}
761
Howard Hinnant99968442011-11-29 18:15:50762template <class _Rp>
763_Rp&
764__assoc_state<_Rp&>::copy()
Howard Hinnantf39daa82010-08-28 21:01:06765{
766 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant54da3382010-08-30 18:46:21767 this->__sub_wait(__lk);
Howard Hinnantf39daa82010-08-28 21:01:06768 if (this->__exception_ != nullptr)
769 rethrow_exception(this->__exception_);
770 return *__value_;
771}
772
Howard Hinnant99968442011-11-29 18:15:50773template <class _Rp, class _Alloc>
Mehdi Amini907c1192017-05-04 17:08:54774class _LIBCPP_AVAILABILITY_FUTURE __assoc_state_alloc
Howard Hinnant99968442011-11-29 18:15:50775 : public __assoc_state<_Rp>
Howard Hinnant47499b12010-08-27 20:10:19776{
Howard Hinnant99968442011-11-29 18:15:50777 typedef __assoc_state<_Rp> base;
Howard Hinnant47499b12010-08-27 20:10:19778 _Alloc __alloc_;
779
Howard Hinnant1694d232011-05-28 14:41:13780 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant47499b12010-08-27 20:10:19781public:
Howard Hinnant8c6cbb22010-09-22 14:16:26782 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19783 explicit __assoc_state_alloc(const _Alloc& __a)
784 : __alloc_(__a) {}
785};
786
Howard Hinnant99968442011-11-29 18:15:50787template <class _Rp, class _Alloc>
Howard Hinnant47499b12010-08-27 20:10:19788void
Howard Hinnant99968442011-11-29 18:15:50789__assoc_state_alloc<_Rp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19790{
791 if (this->__state_ & base::__constructed)
Howard Hinnanta4e87ab2013-08-08 18:38:55792 reinterpret_cast<_Rp*>(_VSTD::addressof(this->__value_))->~_Rp();
Eric Fiselier8492cd82015-02-05 23:01:40793 typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
794 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4d2413c2014-10-23 06:24:45795 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselier8492cd82015-02-05 23:01:40796 _Al __a(__alloc_);
Howard Hinnant47499b12010-08-27 20:10:19797 this->~__assoc_state_alloc();
Eric Fiselier4d2413c2014-10-23 06:24:45798 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnant47499b12010-08-27 20:10:19799}
800
Howard Hinnant99968442011-11-29 18:15:50801template <class _Rp, class _Alloc>
Mehdi Amini907c1192017-05-04 17:08:54802class _LIBCPP_AVAILABILITY_FUTURE __assoc_state_alloc<_Rp&, _Alloc>
Howard Hinnant99968442011-11-29 18:15:50803 : public __assoc_state<_Rp&>
Howard Hinnantf39daa82010-08-28 21:01:06804{
Howard Hinnant99968442011-11-29 18:15:50805 typedef __assoc_state<_Rp&> base;
Howard Hinnantf39daa82010-08-28 21:01:06806 _Alloc __alloc_;
807
Howard Hinnant1694d232011-05-28 14:41:13808 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnantf39daa82010-08-28 21:01:06809public:
Howard Hinnant8c6cbb22010-09-22 14:16:26810 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39daa82010-08-28 21:01:06811 explicit __assoc_state_alloc(const _Alloc& __a)
812 : __alloc_(__a) {}
813};
814
Howard Hinnant99968442011-11-29 18:15:50815template <class _Rp, class _Alloc>
Howard Hinnantf39daa82010-08-28 21:01:06816void
Howard Hinnant99968442011-11-29 18:15:50817__assoc_state_alloc<_Rp&, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantf39daa82010-08-28 21:01:06818{
Eric Fiselier8492cd82015-02-05 23:01:40819 typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
820 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4d2413c2014-10-23 06:24:45821 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselier8492cd82015-02-05 23:01:40822 _Al __a(__alloc_);
Howard Hinnantf39daa82010-08-28 21:01:06823 this->~__assoc_state_alloc();
Eric Fiselier4d2413c2014-10-23 06:24:45824 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantf39daa82010-08-28 21:01:06825}
826
Howard Hinnant47499b12010-08-27 20:10:19827template <class _Alloc>
Mehdi Amini907c1192017-05-04 17:08:54828class _LIBCPP_AVAILABILITY_FUTURE __assoc_sub_state_alloc
Howard Hinnant47499b12010-08-27 20:10:19829 : public __assoc_sub_state
830{
831 typedef __assoc_sub_state base;
832 _Alloc __alloc_;
833
Howard Hinnant1694d232011-05-28 14:41:13834 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant47499b12010-08-27 20:10:19835public:
Howard Hinnant8c6cbb22010-09-22 14:16:26836 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19837 explicit __assoc_sub_state_alloc(const _Alloc& __a)
838 : __alloc_(__a) {}
839};
840
841template <class _Alloc>
842void
Howard Hinnant1694d232011-05-28 14:41:13843__assoc_sub_state_alloc<_Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19844{
Eric Fiselier8492cd82015-02-05 23:01:40845 typedef typename __allocator_traits_rebind<_Alloc, __assoc_sub_state_alloc>::type _Al;
846 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4d2413c2014-10-23 06:24:45847 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselier8492cd82015-02-05 23:01:40848 _Al __a(__alloc_);
Howard Hinnant47499b12010-08-27 20:10:19849 this->~__assoc_sub_state_alloc();
Eric Fiselier4d2413c2014-10-23 06:24:45850 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnant47499b12010-08-27 20:10:19851}
852
Howard Hinnant99968442011-11-29 18:15:50853template <class _Rp, class _Fp>
Mehdi Amini907c1192017-05-04 17:08:54854class _LIBCPP_AVAILABILITY_FUTURE __deferred_assoc_state
Howard Hinnant99968442011-11-29 18:15:50855 : public __assoc_state<_Rp>
Howard Hinnant54da3382010-08-30 18:46:21856{
Howard Hinnant99968442011-11-29 18:15:50857 typedef __assoc_state<_Rp> base;
Howard Hinnant54da3382010-08-30 18:46:21858
Howard Hinnant99968442011-11-29 18:15:50859 _Fp __func_;
Howard Hinnant54da3382010-08-30 18:46:21860
861public:
Howard Hinnant73d21a42010-09-04 23:28:19862#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanova3b25f82015-11-07 01:22:13863 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50864 explicit __deferred_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:21865#endif
866
867 virtual void __execute();
868};
869
Howard Hinnant73d21a42010-09-04 23:28:19870#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21871
Howard Hinnant99968442011-11-29 18:15:50872template <class _Rp, class _Fp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13873inline
Howard Hinnant99968442011-11-29 18:15:50874__deferred_assoc_state<_Rp, _Fp>::__deferred_assoc_state(_Fp&& __f)
875 : __func_(_VSTD::forward<_Fp>(__f))
Howard Hinnant54da3382010-08-30 18:46:21876{
877 this->__set_deferred();
878}
879
Howard Hinnant73d21a42010-09-04 23:28:19880#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21881
Howard Hinnant99968442011-11-29 18:15:50882template <class _Rp, class _Fp>
Howard Hinnant54da3382010-08-30 18:46:21883void
Howard Hinnant99968442011-11-29 18:15:50884__deferred_assoc_state<_Rp, _Fp>::__execute()
Howard Hinnant54da3382010-08-30 18:46:21885{
886#ifndef _LIBCPP_NO_EXCEPTIONS
887 try
888 {
889#endif // _LIBCPP_NO_EXCEPTIONS
890 this->set_value(__func_());
891#ifndef _LIBCPP_NO_EXCEPTIONS
892 }
893 catch (...)
894 {
895 this->set_exception(current_exception());
896 }
897#endif // _LIBCPP_NO_EXCEPTIONS
898}
899
Howard Hinnant99968442011-11-29 18:15:50900template <class _Fp>
Mehdi Amini907c1192017-05-04 17:08:54901class _LIBCPP_AVAILABILITY_FUTURE __deferred_assoc_state<void, _Fp>
Howard Hinnant54da3382010-08-30 18:46:21902 : public __assoc_sub_state
903{
904 typedef __assoc_sub_state base;
905
Howard Hinnant99968442011-11-29 18:15:50906 _Fp __func_;
Howard Hinnant54da3382010-08-30 18:46:21907
908public:
Howard Hinnant73d21a42010-09-04 23:28:19909#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanova3b25f82015-11-07 01:22:13910 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50911 explicit __deferred_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:21912#endif
913
914 virtual void __execute();
915};
916
Howard Hinnant73d21a42010-09-04 23:28:19917#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21918
Howard Hinnant99968442011-11-29 18:15:50919template <class _Fp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13920inline
Howard Hinnant99968442011-11-29 18:15:50921__deferred_assoc_state<void, _Fp>::__deferred_assoc_state(_Fp&& __f)
922 : __func_(_VSTD::forward<_Fp>(__f))
Howard Hinnant54da3382010-08-30 18:46:21923{
924 this->__set_deferred();
925}
926
Howard Hinnant73d21a42010-09-04 23:28:19927#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21928
Howard Hinnant99968442011-11-29 18:15:50929template <class _Fp>
Howard Hinnant54da3382010-08-30 18:46:21930void
Howard Hinnant99968442011-11-29 18:15:50931__deferred_assoc_state<void, _Fp>::__execute()
Howard Hinnant54da3382010-08-30 18:46:21932{
933#ifndef _LIBCPP_NO_EXCEPTIONS
934 try
935 {
936#endif // _LIBCPP_NO_EXCEPTIONS
937 __func_();
938 this->set_value();
939#ifndef _LIBCPP_NO_EXCEPTIONS
940 }
941 catch (...)
942 {
943 this->set_exception(current_exception());
944 }
945#endif // _LIBCPP_NO_EXCEPTIONS
946}
947
Howard Hinnant99968442011-11-29 18:15:50948template <class _Rp, class _Fp>
Mehdi Amini907c1192017-05-04 17:08:54949class _LIBCPP_AVAILABILITY_FUTURE __async_assoc_state
Howard Hinnant99968442011-11-29 18:15:50950 : public __assoc_state<_Rp>
Howard Hinnant57cff292011-05-19 15:05:04951{
Howard Hinnant99968442011-11-29 18:15:50952 typedef __assoc_state<_Rp> base;
Howard Hinnant57cff292011-05-19 15:05:04953
Howard Hinnant99968442011-11-29 18:15:50954 _Fp __func_;
Howard Hinnant57cff292011-05-19 15:05:04955
Howard Hinnant1694d232011-05-28 14:41:13956 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant57cff292011-05-19 15:05:04957public:
958#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanova3b25f82015-11-07 01:22:13959 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50960 explicit __async_assoc_state(_Fp&& __f);
Howard Hinnant57cff292011-05-19 15:05:04961#endif
962
963 virtual void __execute();
964};
965
966#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
967
Howard Hinnant99968442011-11-29 18:15:50968template <class _Rp, class _Fp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13969inline
Howard Hinnant99968442011-11-29 18:15:50970__async_assoc_state<_Rp, _Fp>::__async_assoc_state(_Fp&& __f)
971 : __func_(_VSTD::forward<_Fp>(__f))
Howard Hinnant57cff292011-05-19 15:05:04972{
973}
974
975#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
976
Howard Hinnant99968442011-11-29 18:15:50977template <class _Rp, class _Fp>
Howard Hinnant57cff292011-05-19 15:05:04978void
Howard Hinnant99968442011-11-29 18:15:50979__async_assoc_state<_Rp, _Fp>::__execute()
Howard Hinnant57cff292011-05-19 15:05:04980{
981#ifndef _LIBCPP_NO_EXCEPTIONS
982 try
983 {
984#endif // _LIBCPP_NO_EXCEPTIONS
985 this->set_value(__func_());
986#ifndef _LIBCPP_NO_EXCEPTIONS
987 }
988 catch (...)
989 {
990 this->set_exception(current_exception());
991 }
992#endif // _LIBCPP_NO_EXCEPTIONS
993}
994
Howard Hinnant99968442011-11-29 18:15:50995template <class _Rp, class _Fp>
Howard Hinnant57cff292011-05-19 15:05:04996void
Howard Hinnant99968442011-11-29 18:15:50997__async_assoc_state<_Rp, _Fp>::__on_zero_shared() _NOEXCEPT
Howard Hinnant57cff292011-05-19 15:05:04998{
999 this->wait();
1000 base::__on_zero_shared();
1001}
1002
Howard Hinnant99968442011-11-29 18:15:501003template <class _Fp>
Mehdi Amini907c1192017-05-04 17:08:541004class _LIBCPP_AVAILABILITY_FUTURE __async_assoc_state<void, _Fp>
Howard Hinnant57cff292011-05-19 15:05:041005 : public __assoc_sub_state
1006{
1007 typedef __assoc_sub_state base;
1008
Howard Hinnant99968442011-11-29 18:15:501009 _Fp __func_;
Howard Hinnant57cff292011-05-19 15:05:041010
Howard Hinnant1694d232011-05-28 14:41:131011 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant57cff292011-05-19 15:05:041012public:
1013#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanova3b25f82015-11-07 01:22:131014 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:501015 explicit __async_assoc_state(_Fp&& __f);
Howard Hinnant57cff292011-05-19 15:05:041016#endif
1017
1018 virtual void __execute();
1019};
1020
1021#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1022
Howard Hinnant99968442011-11-29 18:15:501023template <class _Fp>
Evgeniy Stepanova3b25f82015-11-07 01:22:131024inline
Howard Hinnant99968442011-11-29 18:15:501025__async_assoc_state<void, _Fp>::__async_assoc_state(_Fp&& __f)
1026 : __func_(_VSTD::forward<_Fp>(__f))
Howard Hinnant57cff292011-05-19 15:05:041027{
1028}
1029
1030#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1031
Howard Hinnant99968442011-11-29 18:15:501032template <class _Fp>
Howard Hinnant57cff292011-05-19 15:05:041033void
Howard Hinnant99968442011-11-29 18:15:501034__async_assoc_state<void, _Fp>::__execute()
Howard Hinnant57cff292011-05-19 15:05:041035{
1036#ifndef _LIBCPP_NO_EXCEPTIONS
1037 try
1038 {
1039#endif // _LIBCPP_NO_EXCEPTIONS
1040 __func_();
1041 this->set_value();
1042#ifndef _LIBCPP_NO_EXCEPTIONS
1043 }
1044 catch (...)
1045 {
1046 this->set_exception(current_exception());
1047 }
1048#endif // _LIBCPP_NO_EXCEPTIONS
1049}
1050
Howard Hinnant99968442011-11-29 18:15:501051template <class _Fp>
Howard Hinnant57cff292011-05-19 15:05:041052void
Howard Hinnant99968442011-11-29 18:15:501053__async_assoc_state<void, _Fp>::__on_zero_shared() _NOEXCEPT
Howard Hinnant57cff292011-05-19 15:05:041054{
1055 this->wait();
1056 base::__on_zero_shared();
1057}
1058
Eric Fiselierc3589a82017-01-04 23:56:001059template <class _Rp> class _LIBCPP_TEMPLATE_VIS promise;
1060template <class _Rp> class _LIBCPP_TEMPLATE_VIS shared_future;
Howard Hinnant47499b12010-08-27 20:10:191061
1062// future
1063
Eric Fiselierc3589a82017-01-04 23:56:001064template <class _Rp> class _LIBCPP_TEMPLATE_VIS future;
Howard Hinnant54da3382010-08-30 18:46:211065
Howard Hinnant99968442011-11-29 18:15:501066template <class _Rp, class _Fp>
1067future<_Rp>
Howard Hinnant73d21a42010-09-04 23:28:191068#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501069__make_deferred_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:211070#else
Howard Hinnant99968442011-11-29 18:15:501071__make_deferred_assoc_state(_Fp __f);
Howard Hinnant54da3382010-08-30 18:46:211072#endif
1073
Howard Hinnant99968442011-11-29 18:15:501074template <class _Rp, class _Fp>
1075future<_Rp>
Howard Hinnant57cff292011-05-19 15:05:041076#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501077__make_async_assoc_state(_Fp&& __f);
Howard Hinnant57cff292011-05-19 15:05:041078#else
Howard Hinnant99968442011-11-29 18:15:501079__make_async_assoc_state(_Fp __f);
Howard Hinnant57cff292011-05-19 15:05:041080#endif
1081
Howard Hinnant99968442011-11-29 18:15:501082template <class _Rp>
Mehdi Amini907c1192017-05-04 17:08:541083class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE future
Howard Hinnant47499b12010-08-27 20:10:191084{
Howard Hinnant99968442011-11-29 18:15:501085 __assoc_state<_Rp>* __state_;
Howard Hinnant47499b12010-08-27 20:10:191086
Howard Hinnant99968442011-11-29 18:15:501087 explicit future(__assoc_state<_Rp>* __state);
Howard Hinnant47499b12010-08-27 20:10:191088
1089 template <class> friend class promise;
Howard Hinnant99be8232010-09-03 18:39:251090 template <class> friend class shared_future;
Howard Hinnant54da3382010-08-30 18:46:211091
Howard Hinnant73d21a42010-09-04 23:28:191092#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501093 template <class _R1, class _Fp>
1094 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1095 template <class _R1, class _Fp>
1096 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:211097#else
Howard Hinnant99968442011-11-29 18:15:501098 template <class _R1, class _Fp>
1099 friend future<_R1> __make_deferred_assoc_state(_Fp __f);
1100 template <class _R1, class _Fp>
1101 friend future<_R1> __make_async_assoc_state(_Fp __f);
Howard Hinnant54da3382010-08-30 18:46:211102#endif
1103
Howard Hinnant47499b12010-08-27 20:10:191104public:
Howard Hinnant8c6cbb22010-09-22 14:16:261105 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551106 future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant73d21a42010-09-04 23:28:191107#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261108 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551109 future(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191110 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1111 future(const future&) = delete;
1112 future& operator=(const future&) = delete;
Howard Hinnant8c6cbb22010-09-22 14:16:261113 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551114 future& operator=(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191115 {
1116 future(std::move(__rhs)).swap(*this);
1117 return *this;
1118 }
Howard Hinnant73d21a42010-09-04 23:28:191119#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191120private:
1121 future(const future&);
1122 future& operator=(const future&);
1123public:
Howard Hinnant73d21a42010-09-04 23:28:191124#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191125 ~future();
Evgeniy Stepanova3b25f82015-11-07 01:22:131126 _LIBCPP_INLINE_VISIBILITY
Marshall Clow9bb0cca2017-01-24 23:28:251127 shared_future<_Rp> share() _NOEXCEPT;
Howard Hinnant47499b12010-08-27 20:10:191128
1129 // retrieving the value
Howard Hinnant99968442011-11-29 18:15:501130 _Rp get();
Howard Hinnant47499b12010-08-27 20:10:191131
Howard Hinnant8c6cbb22010-09-22 14:16:261132 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551133 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:191134
1135 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:261136 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551137 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant47499b12010-08-27 20:10:191138
Howard Hinnant8c6cbb22010-09-22 14:16:261139 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191140 void wait() const {__state_->wait();}
1141 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:261142 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191143 future_status
1144 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1145 {return __state_->wait_for(__rel_time);}
1146 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:261147 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191148 future_status
1149 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1150 {return __state_->wait_until(__abs_time);}
1151};
1152
Howard Hinnant99968442011-11-29 18:15:501153template <class _Rp>
1154future<_Rp>::future(__assoc_state<_Rp>* __state)
Howard Hinnant47499b12010-08-27 20:10:191155 : __state_(__state)
1156{
1157 if (__state_->__has_future_attached())
Eric Fiselier423ca202015-10-02 21:25:151158 __throw_future_error(future_errc::future_already_retrieved);
Howard Hinnant47499b12010-08-27 20:10:191159 __state_->__add_shared();
Howard Hinnant54da3382010-08-30 18:46:211160 __state_->__set_future_attached();
Howard Hinnant47499b12010-08-27 20:10:191161}
1162
Howard Hinnant54da3382010-08-30 18:46:211163struct __release_shared_count
1164{
1165 void operator()(__shared_count* p) {p->__release_shared();}
1166};
1167
Howard Hinnant99968442011-11-29 18:15:501168template <class _Rp>
1169future<_Rp>::~future()
Howard Hinnant47499b12010-08-27 20:10:191170{
1171 if (__state_)
1172 __state_->__release_shared();
1173}
1174
Howard Hinnant99968442011-11-29 18:15:501175template <class _Rp>
1176_Rp
1177future<_Rp>::get()
Howard Hinnant47499b12010-08-27 20:10:191178{
Howard Hinnant54da3382010-08-30 18:46:211179 unique_ptr<__shared_count, __release_shared_count> __(__state_);
Howard Hinnant99968442011-11-29 18:15:501180 __assoc_state<_Rp>* __s = __state_;
Howard Hinnant47499b12010-08-27 20:10:191181 __state_ = nullptr;
1182 return __s->move();
1183}
1184
Howard Hinnant99968442011-11-29 18:15:501185template <class _Rp>
Mehdi Amini907c1192017-05-04 17:08:541186class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE future<_Rp&>
Howard Hinnant47499b12010-08-27 20:10:191187{
Howard Hinnant99968442011-11-29 18:15:501188 __assoc_state<_Rp&>* __state_;
Howard Hinnant47499b12010-08-27 20:10:191189
Howard Hinnant99968442011-11-29 18:15:501190 explicit future(__assoc_state<_Rp&>* __state);
Howard Hinnant47499b12010-08-27 20:10:191191
1192 template <class> friend class promise;
Howard Hinnant99be8232010-09-03 18:39:251193 template <class> friend class shared_future;
Howard Hinnant54da3382010-08-30 18:46:211194
Howard Hinnant73d21a42010-09-04 23:28:191195#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501196 template <class _R1, class _Fp>
1197 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1198 template <class _R1, class _Fp>
1199 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:211200#else
Howard Hinnant99968442011-11-29 18:15:501201 template <class _R1, class _Fp>
1202 friend future<_R1> __make_deferred_assoc_state(_Fp __f);
1203 template <class _R1, class _Fp>
1204 friend future<_R1> __make_async_assoc_state(_Fp __f);
Howard Hinnant54da3382010-08-30 18:46:211205#endif
1206
Howard Hinnant47499b12010-08-27 20:10:191207public:
Howard Hinnant8c6cbb22010-09-22 14:16:261208 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551209 future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant73d21a42010-09-04 23:28:191210#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261211 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551212 future(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191213 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1214 future(const future&) = delete;
1215 future& operator=(const future&) = delete;
Howard Hinnant8c6cbb22010-09-22 14:16:261216 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551217 future& operator=(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191218 {
1219 future(std::move(__rhs)).swap(*this);
1220 return *this;
1221 }
Howard Hinnant73d21a42010-09-04 23:28:191222#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191223private:
1224 future(const future&);
1225 future& operator=(const future&);
1226public:
Howard Hinnant73d21a42010-09-04 23:28:191227#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191228 ~future();
Evgeniy Stepanova3b25f82015-11-07 01:22:131229 _LIBCPP_INLINE_VISIBILITY
Marshall Clow9bb0cca2017-01-24 23:28:251230 shared_future<_Rp&> share() _NOEXCEPT;
Howard Hinnant47499b12010-08-27 20:10:191231
1232 // retrieving the value
Howard Hinnant99968442011-11-29 18:15:501233 _Rp& get();
Howard Hinnant47499b12010-08-27 20:10:191234
Howard Hinnant8c6cbb22010-09-22 14:16:261235 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551236 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:191237
1238 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:261239 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551240 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant47499b12010-08-27 20:10:191241
Howard Hinnant8c6cbb22010-09-22 14:16:261242 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191243 void wait() const {__state_->wait();}
1244 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:261245 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191246 future_status
1247 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1248 {return __state_->wait_for(__rel_time);}
1249 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:261250 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191251 future_status
1252 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1253 {return __state_->wait_until(__abs_time);}
1254};
1255
Howard Hinnant99968442011-11-29 18:15:501256template <class _Rp>
1257future<_Rp&>::future(__assoc_state<_Rp&>* __state)
Howard Hinnant47499b12010-08-27 20:10:191258 : __state_(__state)
1259{
1260 if (__state_->__has_future_attached())
Eric Fiselier423ca202015-10-02 21:25:151261 __throw_future_error(future_errc::future_already_retrieved);
Howard Hinnant47499b12010-08-27 20:10:191262 __state_->__add_shared();
Howard Hinnant54da3382010-08-30 18:46:211263 __state_->__set_future_attached();
Howard Hinnant47499b12010-08-27 20:10:191264}
1265
Howard Hinnant99968442011-11-29 18:15:501266template <class _Rp>
1267future<_Rp&>::~future()
Howard Hinnant47499b12010-08-27 20:10:191268{
1269 if (__state_)
1270 __state_->__release_shared();
1271}
1272
Howard Hinnant99968442011-11-29 18:15:501273template <class _Rp>
1274_Rp&
1275future<_Rp&>::get()
Howard Hinnant47499b12010-08-27 20:10:191276{
Howard Hinnant54da3382010-08-30 18:46:211277 unique_ptr<__shared_count, __release_shared_count> __(__state_);
Howard Hinnant99968442011-11-29 18:15:501278 __assoc_state<_Rp&>* __s = __state_;
Howard Hinnant47499b12010-08-27 20:10:191279 __state_ = nullptr;
1280 return __s->copy();
1281}
1282
1283template <>
Mehdi Amini907c1192017-05-04 17:08:541284class _LIBCPP_TYPE_VIS _LIBCPP_AVAILABILITY_FUTURE future<void>
Howard Hinnant47499b12010-08-27 20:10:191285{
1286 __assoc_sub_state* __state_;
1287
1288 explicit future(__assoc_sub_state* __state);
1289
1290 template <class> friend class promise;
Howard Hinnant99be8232010-09-03 18:39:251291 template <class> friend class shared_future;
Howard Hinnant54da3382010-08-30 18:46:211292
Howard Hinnant73d21a42010-09-04 23:28:191293#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501294 template <class _R1, class _Fp>
1295 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1296 template <class _R1, class _Fp>
1297 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:211298#else
Howard Hinnant99968442011-11-29 18:15:501299 template <class _R1, class _Fp>
1300 friend future<_R1> __make_deferred_assoc_state(_Fp __f);
1301 template <class _R1, class _Fp>
1302 friend future<_R1> __make_async_assoc_state(_Fp __f);
Howard Hinnant54da3382010-08-30 18:46:211303#endif
1304
Howard Hinnant47499b12010-08-27 20:10:191305public:
Howard Hinnant8c6cbb22010-09-22 14:16:261306 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551307 future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant73d21a42010-09-04 23:28:191308#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261309 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551310 future(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191311 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1312 future(const future&) = delete;
1313 future& operator=(const future&) = delete;
Howard Hinnant8c6cbb22010-09-22 14:16:261314 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551315 future& operator=(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191316 {
1317 future(std::move(__rhs)).swap(*this);
1318 return *this;
1319 }
Howard Hinnant73d21a42010-09-04 23:28:191320#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191321private:
1322 future(const future&);
1323 future& operator=(const future&);
1324public:
Howard Hinnant73d21a42010-09-04 23:28:191325#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191326 ~future();
Evgeniy Stepanova3b25f82015-11-07 01:22:131327 _LIBCPP_INLINE_VISIBILITY
Marshall Clow9bb0cca2017-01-24 23:28:251328 shared_future<void> share() _NOEXCEPT;
Howard Hinnant47499b12010-08-27 20:10:191329
1330 // retrieving the value
1331 void get();
1332
Howard Hinnant8c6cbb22010-09-22 14:16:261333 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551334 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:191335
1336 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:261337 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551338 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant47499b12010-08-27 20:10:191339
Howard Hinnant8c6cbb22010-09-22 14:16:261340 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191341 void wait() const {__state_->wait();}
1342 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:261343 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191344 future_status
1345 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1346 {return __state_->wait_for(__rel_time);}
1347 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:261348 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191349 future_status
1350 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1351 {return __state_->wait_until(__abs_time);}
1352};
1353
Howard Hinnant99968442011-11-29 18:15:501354template <class _Rp>
Howard Hinnant99be8232010-09-03 18:39:251355inline _LIBCPP_INLINE_VISIBILITY
1356void
Howard Hinnant8bf01dd2012-07-21 17:46:551357swap(future<_Rp>& __x, future<_Rp>& __y) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:251358{
1359 __x.swap(__y);
1360}
1361
Howard Hinnant47499b12010-08-27 20:10:191362// promise<R>
1363
Howard Hinnant2b1b2d42011-06-14 19:58:171364template <class _Callable> class packaged_task;
Howard Hinnant54da3382010-08-30 18:46:211365
Howard Hinnant99968442011-11-29 18:15:501366template <class _Rp>
Mehdi Amini907c1192017-05-04 17:08:541367class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE promise
Howard Hinnant47499b12010-08-27 20:10:191368{
Howard Hinnant99968442011-11-29 18:15:501369 __assoc_state<_Rp>* __state_;
Howard Hinnant54da3382010-08-30 18:46:211370
Howard Hinnant8c6cbb22010-09-22 14:16:261371 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551372 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant54da3382010-08-30 18:46:211373
1374 template <class> friend class packaged_task;
Howard Hinnant47499b12010-08-27 20:10:191375public:
1376 promise();
1377 template <class _Alloc>
1378 promise(allocator_arg_t, const _Alloc& __a);
Howard Hinnant73d21a42010-09-04 23:28:191379#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261380 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551381 promise(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191382 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1383 promise(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:191384#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191385private:
1386 promise(const promise& __rhs);
1387public:
Howard Hinnant73d21a42010-09-04 23:28:191388#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191389 ~promise();
1390
1391 // assignment
Howard Hinnant73d21a42010-09-04 23:28:191392#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261393 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551394 promise& operator=(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191395 {
1396 promise(std::move(__rhs)).swap(*this);
1397 return *this;
1398 }
1399 promise& operator=(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:191400#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191401private:
1402 promise& operator=(const promise& __rhs);
1403public:
Howard Hinnant73d21a42010-09-04 23:28:191404#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261405 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551406 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:191407
1408 // retrieving the result
Howard Hinnant99968442011-11-29 18:15:501409 future<_Rp> get_future();
Howard Hinnant47499b12010-08-27 20:10:191410
1411 // setting the result
Howard Hinnant99968442011-11-29 18:15:501412 void set_value(const _Rp& __r);
Howard Hinnant73d21a42010-09-04 23:28:191413#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501414 void set_value(_Rp&& __r);
Howard Hinnant47499b12010-08-27 20:10:191415#endif
1416 void set_exception(exception_ptr __p);
1417
1418 // setting the result with deferred notification
Howard Hinnant99968442011-11-29 18:15:501419 void set_value_at_thread_exit(const _Rp& __r);
Howard Hinnant73d21a42010-09-04 23:28:191420#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501421 void set_value_at_thread_exit(_Rp&& __r);
Howard Hinnant47499b12010-08-27 20:10:191422#endif
1423 void set_exception_at_thread_exit(exception_ptr __p);
1424};
1425
Howard Hinnant99968442011-11-29 18:15:501426template <class _Rp>
1427promise<_Rp>::promise()
1428 : __state_(new __assoc_state<_Rp>)
Howard Hinnant47499b12010-08-27 20:10:191429{
1430}
1431
Howard Hinnant99968442011-11-29 18:15:501432template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191433template <class _Alloc>
Howard Hinnant99968442011-11-29 18:15:501434promise<_Rp>::promise(allocator_arg_t, const _Alloc& __a0)
Howard Hinnant47499b12010-08-27 20:10:191435{
Eric Fiselier4d2413c2014-10-23 06:24:451436 typedef __assoc_state_alloc<_Rp, _Alloc> _State;
1437 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
Howard Hinnant47499b12010-08-27 20:10:191438 typedef __allocator_destructor<_A2> _D2;
1439 _A2 __a(__a0);
Eric Fiselier4d2413c2014-10-23 06:24:451440 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1441 ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0);
1442 __state_ = _VSTD::addressof(*__hold.release());
Howard Hinnant47499b12010-08-27 20:10:191443}
1444
Howard Hinnant99968442011-11-29 18:15:501445template <class _Rp>
1446promise<_Rp>::~promise()
Howard Hinnant47499b12010-08-27 20:10:191447{
1448 if (__state_)
1449 {
1450 if (!__state_->__has_value() && __state_->use_count() > 1)
1451 __state_->set_exception(make_exception_ptr(
1452 future_error(make_error_code(future_errc::broken_promise))
1453 ));
1454 __state_->__release_shared();
1455 }
1456}
1457
Howard Hinnant99968442011-11-29 18:15:501458template <class _Rp>
1459future<_Rp>
1460promise<_Rp>::get_future()
Howard Hinnant47499b12010-08-27 20:10:191461{
1462 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:151463 __throw_future_error(future_errc::no_state);
Howard Hinnant99968442011-11-29 18:15:501464 return future<_Rp>(__state_);
Howard Hinnant47499b12010-08-27 20:10:191465}
1466
Howard Hinnant99968442011-11-29 18:15:501467template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191468void
Howard Hinnant99968442011-11-29 18:15:501469promise<_Rp>::set_value(const _Rp& __r)
Howard Hinnant47499b12010-08-27 20:10:191470{
1471 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:151472 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:191473 __state_->set_value(__r);
1474}
1475
Howard Hinnant73d21a42010-09-04 23:28:191476#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191477
Howard Hinnant99968442011-11-29 18:15:501478template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191479void
Howard Hinnant99968442011-11-29 18:15:501480promise<_Rp>::set_value(_Rp&& __r)
Howard Hinnant47499b12010-08-27 20:10:191481{
1482 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:151483 __throw_future_error(future_errc::no_state);
Howard Hinnant0949eed2011-06-30 21:18:191484 __state_->set_value(_VSTD::move(__r));
Howard Hinnant47499b12010-08-27 20:10:191485}
1486
Howard Hinnant73d21a42010-09-04 23:28:191487#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191488
Howard Hinnant99968442011-11-29 18:15:501489template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191490void
Howard Hinnant99968442011-11-29 18:15:501491promise<_Rp>::set_exception(exception_ptr __p)
Howard Hinnant47499b12010-08-27 20:10:191492{
Marshall Cloweaba7bb2016-05-16 16:55:321493 _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception: received nullptr" );
Howard Hinnant47499b12010-08-27 20:10:191494 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:151495 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:191496 __state_->set_exception(__p);
1497}
1498
Howard Hinnant99968442011-11-29 18:15:501499template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191500void
Howard Hinnant99968442011-11-29 18:15:501501promise<_Rp>::set_value_at_thread_exit(const _Rp& __r)
Howard Hinnant47499b12010-08-27 20:10:191502{
1503 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:151504 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:191505 __state_->set_value_at_thread_exit(__r);
1506}
1507
Howard Hinnant73d21a42010-09-04 23:28:191508#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191509
Howard Hinnant99968442011-11-29 18:15:501510template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191511void
Howard Hinnant99968442011-11-29 18:15:501512promise<_Rp>::set_value_at_thread_exit(_Rp&& __r)
Howard Hinnant47499b12010-08-27 20:10:191513{
1514 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:151515 __throw_future_error(future_errc::no_state);
Howard Hinnant0949eed2011-06-30 21:18:191516 __state_->set_value_at_thread_exit(_VSTD::move(__r));
Howard Hinnant47499b12010-08-27 20:10:191517}
1518
Howard Hinnant73d21a42010-09-04 23:28:191519#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191520
Howard Hinnant99968442011-11-29 18:15:501521template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191522void
Howard Hinnant99968442011-11-29 18:15:501523promise<_Rp>::set_exception_at_thread_exit(exception_ptr __p)
Howard Hinnant47499b12010-08-27 20:10:191524{
Eric Fiselierb169bb02016-05-31 01:50:551525 _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception_at_thread_exit: received nullptr" );
Howard Hinnant47499b12010-08-27 20:10:191526 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:151527 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:191528 __state_->set_exception_at_thread_exit(__p);
1529}
1530
1531// promise<R&>
1532
Howard Hinnant99968442011-11-29 18:15:501533template <class _Rp>
Mehdi Amini907c1192017-05-04 17:08:541534class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE promise<_Rp&>
Howard Hinnant47499b12010-08-27 20:10:191535{
Howard Hinnant99968442011-11-29 18:15:501536 __assoc_state<_Rp&>* __state_;
Howard Hinnant54da3382010-08-30 18:46:211537
Howard Hinnant8c6cbb22010-09-22 14:16:261538 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551539 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant54da3382010-08-30 18:46:211540
1541 template <class> friend class packaged_task;
1542
Howard Hinnant47499b12010-08-27 20:10:191543public:
1544 promise();
1545 template <class _Allocator>
1546 promise(allocator_arg_t, const _Allocator& __a);
Howard Hinnant73d21a42010-09-04 23:28:191547#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261548 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551549 promise(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191550 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1551 promise(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:191552#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191553private:
1554 promise(const promise& __rhs);
1555public:
Howard Hinnant73d21a42010-09-04 23:28:191556#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191557 ~promise();
1558
1559 // assignment
Howard Hinnant73d21a42010-09-04 23:28:191560#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551562 promise& operator=(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191563 {
1564 promise(std::move(__rhs)).swap(*this);
1565 return *this;
1566 }
1567 promise& operator=(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:191568#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191569private:
1570 promise& operator=(const promise& __rhs);
1571public:
Howard Hinnant73d21a42010-09-04 23:28:191572#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261573 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551574 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:191575
1576 // retrieving the result
Howard Hinnant99968442011-11-29 18:15:501577 future<_Rp&> get_future();
Howard Hinnant47499b12010-08-27 20:10:191578
1579 // setting the result
Howard Hinnant99968442011-11-29 18:15:501580 void set_value(_Rp& __r);
Howard Hinnant47499b12010-08-27 20:10:191581 void set_exception(exception_ptr __p);
1582
1583 // setting the result with deferred notification
Howard Hinnant99968442011-11-29 18:15:501584 void set_value_at_thread_exit(_Rp&);
Howard Hinnant47499b12010-08-27 20:10:191585 void set_exception_at_thread_exit(exception_ptr __p);
1586};
1587
Howard Hinnant99968442011-11-29 18:15:501588template <class _Rp>
1589promise<_Rp&>::promise()
1590 : __state_(new __assoc_state<_Rp&>)
Howard Hinnant47499b12010-08-27 20:10:191591{
1592}
1593
Howard Hinnant99968442011-11-29 18:15:501594template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191595template <class _Alloc>
Howard Hinnant99968442011-11-29 18:15:501596promise<_Rp&>::promise(allocator_arg_t, const _Alloc& __a0)
Howard Hinnant47499b12010-08-27 20:10:191597{
Eric Fiselier4d2413c2014-10-23 06:24:451598 typedef __assoc_state_alloc<_Rp&, _Alloc> _State;
1599 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
Howard Hinnant47499b12010-08-27 20:10:191600 typedef __allocator_destructor<_A2> _D2;
1601 _A2 __a(__a0);
Eric Fiselier4d2413c2014-10-23 06:24:451602 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1603 ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0);
1604 __state_ = _VSTD::addressof(*__hold.release());
Howard Hinnant47499b12010-08-27 20:10:191605}
1606
Howard Hinnant99968442011-11-29 18:15:501607template <class _Rp>
1608promise<_Rp&>::~promise()
Howard Hinnant47499b12010-08-27 20:10:191609{
1610 if (__state_)
1611 {
1612 if (!__state_->__has_value() && __state_->use_count() > 1)
1613 __state_->set_exception(make_exception_ptr(
1614 future_error(make_error_code(future_errc::broken_promise))
1615 ));
1616 __state_->__release_shared();
1617 }
1618}
1619
Howard Hinnant99968442011-11-29 18:15:501620template <class _Rp>
1621future<_Rp&>
1622promise<_Rp&>::get_future()
Howard Hinnant47499b12010-08-27 20:10:191623{
1624 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:151625 __throw_future_error(future_errc::no_state);
Howard Hinnant99968442011-11-29 18:15:501626 return future<_Rp&>(__state_);
Howard Hinnant47499b12010-08-27 20:10:191627}
1628
Howard Hinnant99968442011-11-29 18:15:501629template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191630void
Howard Hinnant99968442011-11-29 18:15:501631promise<_Rp&>::set_value(_Rp& __r)
Howard Hinnant47499b12010-08-27 20:10:191632{
1633 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:151634 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:191635 __state_->set_value(__r);
1636}
1637
Howard Hinnant99968442011-11-29 18:15:501638template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191639void
Howard Hinnant99968442011-11-29 18:15:501640promise<_Rp&>::set_exception(exception_ptr __p)
Howard Hinnant47499b12010-08-27 20:10:191641{
Marshall Cloweaba7bb2016-05-16 16:55:321642 _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception: received nullptr" );
Howard Hinnant47499b12010-08-27 20:10:191643 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:151644 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:191645 __state_->set_exception(__p);
1646}
1647
Howard Hinnant99968442011-11-29 18:15:501648template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191649void
Howard Hinnant99968442011-11-29 18:15:501650promise<_Rp&>::set_value_at_thread_exit(_Rp& __r)
Howard Hinnant47499b12010-08-27 20:10:191651{
1652 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:151653 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:191654 __state_->set_value_at_thread_exit(__r);
1655}
1656
Howard Hinnant99968442011-11-29 18:15:501657template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191658void
Howard Hinnant99968442011-11-29 18:15:501659promise<_Rp&>::set_exception_at_thread_exit(exception_ptr __p)
Howard Hinnant47499b12010-08-27 20:10:191660{
Eric Fiselierb169bb02016-05-31 01:50:551661 _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception_at_thread_exit: received nullptr" );
Howard Hinnant47499b12010-08-27 20:10:191662 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:151663 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:191664 __state_->set_exception_at_thread_exit(__p);
1665}
1666
1667// promise<void>
1668
1669template <>
Mehdi Amini907c1192017-05-04 17:08:541670class _LIBCPP_TYPE_VIS _LIBCPP_AVAILABILITY_FUTURE promise<void>
Howard Hinnant47499b12010-08-27 20:10:191671{
1672 __assoc_sub_state* __state_;
Howard Hinnant54da3382010-08-30 18:46:211673
Howard Hinnant8c6cbb22010-09-22 14:16:261674 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551675 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant54da3382010-08-30 18:46:211676
1677 template <class> friend class packaged_task;
1678
Howard Hinnant47499b12010-08-27 20:10:191679public:
1680 promise();
1681 template <class _Allocator>
Shoaib Meenai6b734922017-03-02 03:22:181682 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Howard Hinnant47499b12010-08-27 20:10:191683 promise(allocator_arg_t, const _Allocator& __a);
Howard Hinnant73d21a42010-09-04 23:28:191684#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261685 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551686 promise(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191687 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1688 promise(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:191689#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191690private:
1691 promise(const promise& __rhs);
1692public:
Howard Hinnant73d21a42010-09-04 23:28:191693#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191694 ~promise();
1695
1696 // assignment
Howard Hinnant73d21a42010-09-04 23:28:191697#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261698 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551699 promise& operator=(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191700 {
1701 promise(std::move(__rhs)).swap(*this);
1702 return *this;
1703 }
1704 promise& operator=(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:191705#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191706private:
1707 promise& operator=(const promise& __rhs);
1708public:
Howard Hinnant73d21a42010-09-04 23:28:191709#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261710 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551711 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:191712
1713 // retrieving the result
1714 future<void> get_future();
1715
1716 // setting the result
1717 void set_value();
1718 void set_exception(exception_ptr __p);
1719
1720 // setting the result with deferred notification
1721 void set_value_at_thread_exit();
1722 void set_exception_at_thread_exit(exception_ptr __p);
1723};
1724
1725template <class _Alloc>
1726promise<void>::promise(allocator_arg_t, const _Alloc& __a0)
1727{
Eric Fiselier4d2413c2014-10-23 06:24:451728 typedef __assoc_sub_state_alloc<_Alloc> _State;
1729 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
Howard Hinnant47499b12010-08-27 20:10:191730 typedef __allocator_destructor<_A2> _D2;
1731 _A2 __a(__a0);
Eric Fiselier4d2413c2014-10-23 06:24:451732 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1733 ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0);
1734 __state_ = _VSTD::addressof(*__hold.release());
Howard Hinnant47499b12010-08-27 20:10:191735}
1736
Howard Hinnant99968442011-11-29 18:15:501737template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191738inline _LIBCPP_INLINE_VISIBILITY
1739void
Howard Hinnant8bf01dd2012-07-21 17:46:551740swap(promise<_Rp>& __x, promise<_Rp>& __y) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191741{
1742 __x.swap(__y);
1743}
1744
Howard Hinnant99968442011-11-29 18:15:501745template <class _Rp, class _Alloc>
Eric Fiselierc3589a82017-01-04 23:56:001746 struct _LIBCPP_TEMPLATE_VIS uses_allocator<promise<_Rp>, _Alloc>
Howard Hinnant8c6cbb22010-09-22 14:16:261747 : public true_type {};
Howard Hinnant47499b12010-08-27 20:10:191748
Howard Hinnant54da3382010-08-30 18:46:211749#ifndef _LIBCPP_HAS_NO_VARIADICS
1750
1751// packaged_task
1752
1753template<class _Fp> class __packaged_task_base;
1754
Howard Hinnant99968442011-11-29 18:15:501755template<class _Rp, class ..._ArgTypes>
Mehdi Amini907c1192017-05-04 17:08:541756class _LIBCPP_AVAILABILITY_FUTURE __packaged_task_base<_Rp(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:211757{
1758 __packaged_task_base(const __packaged_task_base&);
1759 __packaged_task_base& operator=(const __packaged_task_base&);
1760public:
Howard Hinnant8c6cbb22010-09-22 14:16:261761 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:211762 __packaged_task_base() {}
Howard Hinnant8c6cbb22010-09-22 14:16:261763 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:211764 virtual ~__packaged_task_base() {}
Howard Hinnant8bf01dd2012-07-21 17:46:551765 virtual void __move_to(__packaged_task_base*) _NOEXCEPT = 0;
Howard Hinnant54da3382010-08-30 18:46:211766 virtual void destroy() = 0;
1767 virtual void destroy_deallocate() = 0;
Howard Hinnant99968442011-11-29 18:15:501768 virtual _Rp operator()(_ArgTypes&& ...) = 0;
Howard Hinnant54da3382010-08-30 18:46:211769};
1770
1771template<class _FD, class _Alloc, class _FB> class __packaged_task_func;
1772
Howard Hinnant99968442011-11-29 18:15:501773template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Mehdi Amini907c1192017-05-04 17:08:541774class _LIBCPP_AVAILABILITY_FUTURE __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>
Howard Hinnant99968442011-11-29 18:15:501775 : public __packaged_task_base<_Rp(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:211776{
Howard Hinnant99968442011-11-29 18:15:501777 __compressed_pair<_Fp, _Alloc> __f_;
Howard Hinnant54da3382010-08-30 18:46:211778public:
Howard Hinnant8c6cbb22010-09-22 14:16:261779 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:501780 explicit __packaged_task_func(const _Fp& __f) : __f_(__f) {}
Howard Hinnant8c6cbb22010-09-22 14:16:261781 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:501782 explicit __packaged_task_func(_Fp&& __f) : __f_(_VSTD::move(__f)) {}
Howard Hinnant8c6cbb22010-09-22 14:16:261783 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:501784 __packaged_task_func(const _Fp& __f, const _Alloc& __a)
Howard Hinnant54da3382010-08-30 18:46:211785 : __f_(__f, __a) {}
Howard Hinnant8c6cbb22010-09-22 14:16:261786 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:501787 __packaged_task_func(_Fp&& __f, const _Alloc& __a)
Howard Hinnant0949eed2011-06-30 21:18:191788 : __f_(_VSTD::move(__f), __a) {}
Howard Hinnant8bf01dd2012-07-21 17:46:551789 virtual void __move_to(__packaged_task_base<_Rp(_ArgTypes...)>*) _NOEXCEPT;
Howard Hinnant54da3382010-08-30 18:46:211790 virtual void destroy();
1791 virtual void destroy_deallocate();
Howard Hinnant99968442011-11-29 18:15:501792 virtual _Rp operator()(_ArgTypes&& ... __args);
Howard Hinnant54da3382010-08-30 18:46:211793};
1794
Howard Hinnant99968442011-11-29 18:15:501795template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:211796void
Howard Hinnant99968442011-11-29 18:15:501797__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__move_to(
Howard Hinnant8bf01dd2012-07-21 17:46:551798 __packaged_task_base<_Rp(_ArgTypes...)>* __p) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:211799{
Howard Hinnant0949eed2011-06-30 21:18:191800 ::new (__p) __packaged_task_func(_VSTD::move(__f_.first()), _VSTD::move(__f_.second()));
Howard Hinnant54da3382010-08-30 18:46:211801}
1802
Howard Hinnant99968442011-11-29 18:15:501803template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:211804void
Howard Hinnant99968442011-11-29 18:15:501805__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy()
Howard Hinnant54da3382010-08-30 18:46:211806{
Howard Hinnant99968442011-11-29 18:15:501807 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnant54da3382010-08-30 18:46:211808}
1809
Howard Hinnant99968442011-11-29 18:15:501810template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:211811void
Howard Hinnant99968442011-11-29 18:15:501812__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate()
Howard Hinnant54da3382010-08-30 18:46:211813{
Eric Fiselier4d2413c2014-10-23 06:24:451814 typedef typename __allocator_traits_rebind<_Alloc, __packaged_task_func>::type _Ap;
1815 typedef allocator_traits<_Ap> _ATraits;
1816 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Howard Hinnant99968442011-11-29 18:15:501817 _Ap __a(__f_.second());
1818 __f_.~__compressed_pair<_Fp, _Alloc>();
Eric Fiselier4d2413c2014-10-23 06:24:451819 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnant54da3382010-08-30 18:46:211820}
1821
Howard Hinnant99968442011-11-29 18:15:501822template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1823_Rp
1824__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
Howard Hinnant54da3382010-08-30 18:46:211825{
Howard Hinnant0949eed2011-06-30 21:18:191826 return __invoke(__f_.first(), _VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnant54da3382010-08-30 18:46:211827}
1828
Howard Hinnant2b1b2d42011-06-14 19:58:171829template <class _Callable> class __packaged_task_function;
Howard Hinnant54da3382010-08-30 18:46:211830
Howard Hinnant99968442011-11-29 18:15:501831template<class _Rp, class ..._ArgTypes>
Mehdi Amini907c1192017-05-04 17:08:541832class _LIBCPP_AVAILABILITY_FUTURE __packaged_task_function<_Rp(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:211833{
Howard Hinnant99968442011-11-29 18:15:501834 typedef __packaged_task_base<_Rp(_ArgTypes...)> __base;
Howard Hinnant78f0de22013-01-21 17:26:551835 typename aligned_storage<3*sizeof(void*)>::type __buf_;
Howard Hinnant54da3382010-08-30 18:46:211836 __base* __f_;
1837
1838public:
Howard Hinnant99968442011-11-29 18:15:501839 typedef _Rp result_type;
Howard Hinnant54da3382010-08-30 18:46:211840
1841 // construct/copy/destroy:
Howard Hinnant8c6cbb22010-09-22 14:16:261842 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551843 __packaged_task_function() _NOEXCEPT : __f_(nullptr) {}
Howard Hinnant99968442011-11-29 18:15:501844 template<class _Fp>
1845 __packaged_task_function(_Fp&& __f);
1846 template<class _Fp, class _Alloc>
1847 __packaged_task_function(allocator_arg_t, const _Alloc& __a, _Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:211848
Howard Hinnant8bf01dd2012-07-21 17:46:551849 __packaged_task_function(__packaged_task_function&&) _NOEXCEPT;
1850 __packaged_task_function& operator=(__packaged_task_function&&) _NOEXCEPT;
Howard Hinnant54da3382010-08-30 18:46:211851
1852 __packaged_task_function(const __packaged_task_function&) = delete;
1853 __packaged_task_function& operator=(const __packaged_task_function&) = delete;
1854
1855 ~__packaged_task_function();
1856
Howard Hinnant8bf01dd2012-07-21 17:46:551857 void swap(__packaged_task_function&) _NOEXCEPT;
Howard Hinnant54da3382010-08-30 18:46:211858
Evgeniy Stepanova3b25f82015-11-07 01:22:131859 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:501860 _Rp operator()(_ArgTypes...) const;
Howard Hinnant54da3382010-08-30 18:46:211861};
1862
Howard Hinnant99968442011-11-29 18:15:501863template<class _Rp, class ..._ArgTypes>
Howard Hinnant8bf01dd2012-07-21 17:46:551864__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:211865{
1866 if (__f.__f_ == nullptr)
1867 __f_ = nullptr;
1868 else if (__f.__f_ == (__base*)&__f.__buf_)
1869 {
1870 __f_ = (__base*)&__buf_;
1871 __f.__f_->__move_to(__f_);
1872 }
1873 else
1874 {
1875 __f_ = __f.__f_;
1876 __f.__f_ = nullptr;
1877 }
1878}
1879
Howard Hinnant99968442011-11-29 18:15:501880template<class _Rp, class ..._ArgTypes>
1881template <class _Fp>
1882__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(_Fp&& __f)
Howard Hinnant54da3382010-08-30 18:46:211883 : __f_(nullptr)
1884{
Marshall Clowf1264e72014-04-07 13:32:261885 typedef typename remove_reference<typename decay<_Fp>::type>::type _FR;
Howard Hinnant99968442011-11-29 18:15:501886 typedef __packaged_task_func<_FR, allocator<_FR>, _Rp(_ArgTypes...)> _FF;
Howard Hinnant54da3382010-08-30 18:46:211887 if (sizeof(_FF) <= sizeof(__buf_))
1888 {
1889 __f_ = (__base*)&__buf_;
Howard Hinnant99968442011-11-29 18:15:501890 ::new (__f_) _FF(_VSTD::forward<_Fp>(__f));
Howard Hinnant54da3382010-08-30 18:46:211891 }
1892 else
1893 {
Howard Hinnant99968442011-11-29 18:15:501894 typedef allocator<_FF> _Ap;
1895 _Ap __a;
1896 typedef __allocator_destructor<_Ap> _Dp;
1897 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1898 ::new (__hold.get()) _FF(_VSTD::forward<_Fp>(__f), allocator<_FR>(__a));
Howard Hinnant54da3382010-08-30 18:46:211899 __f_ = __hold.release();
1900 }
1901}
1902
Howard Hinnant99968442011-11-29 18:15:501903template<class _Rp, class ..._ArgTypes>
1904template <class _Fp, class _Alloc>
1905__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(
1906 allocator_arg_t, const _Alloc& __a0, _Fp&& __f)
Howard Hinnant54da3382010-08-30 18:46:211907 : __f_(nullptr)
1908{
Marshall Clowf1264e72014-04-07 13:32:261909 typedef typename remove_reference<typename decay<_Fp>::type>::type _FR;
Howard Hinnant99968442011-11-29 18:15:501910 typedef __packaged_task_func<_FR, _Alloc, _Rp(_ArgTypes...)> _FF;
Howard Hinnant54da3382010-08-30 18:46:211911 if (sizeof(_FF) <= sizeof(__buf_))
1912 {
1913 __f_ = (__base*)&__buf_;
Howard Hinnant99968442011-11-29 18:15:501914 ::new (__f_) _FF(_VSTD::forward<_Fp>(__f));
Howard Hinnant54da3382010-08-30 18:46:211915 }
1916 else
1917 {
Eric Fiselier4d2413c2014-10-23 06:24:451918 typedef typename __allocator_traits_rebind<_Alloc, _FF>::type _Ap;
Howard Hinnant99968442011-11-29 18:15:501919 _Ap __a(__a0);
1920 typedef __allocator_destructor<_Ap> _Dp;
1921 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Eric Fiselier4d2413c2014-10-23 06:24:451922 ::new (static_cast<void*>(_VSTD::addressof(*__hold.get())))
1923 _FF(_VSTD::forward<_Fp>(__f), _Alloc(__a));
1924 __f_ = _VSTD::addressof(*__hold.release());
Howard Hinnant54da3382010-08-30 18:46:211925 }
1926}
1927
Howard Hinnant99968442011-11-29 18:15:501928template<class _Rp, class ..._ArgTypes>
1929__packaged_task_function<_Rp(_ArgTypes...)>&
Howard Hinnant8bf01dd2012-07-21 17:46:551930__packaged_task_function<_Rp(_ArgTypes...)>::operator=(__packaged_task_function&& __f) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:211931{
1932 if (__f_ == (__base*)&__buf_)
1933 __f_->destroy();
1934 else if (__f_)
1935 __f_->destroy_deallocate();
1936 __f_ = nullptr;
1937 if (__f.__f_ == nullptr)
1938 __f_ = nullptr;
1939 else if (__f.__f_ == (__base*)&__f.__buf_)
1940 {
1941 __f_ = (__base*)&__buf_;
1942 __f.__f_->__move_to(__f_);
1943 }
1944 else
1945 {
1946 __f_ = __f.__f_;
1947 __f.__f_ = nullptr;
1948 }
Argyrios Kyrtzidis1dc6f7a2012-10-13 02:03:451949 return *this;
Howard Hinnant54da3382010-08-30 18:46:211950}
1951
Howard Hinnant99968442011-11-29 18:15:501952template<class _Rp, class ..._ArgTypes>
1953__packaged_task_function<_Rp(_ArgTypes...)>::~__packaged_task_function()
Howard Hinnant54da3382010-08-30 18:46:211954{
1955 if (__f_ == (__base*)&__buf_)
1956 __f_->destroy();
1957 else if (__f_)
1958 __f_->destroy_deallocate();
1959}
1960
Howard Hinnant99968442011-11-29 18:15:501961template<class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:211962void
Howard Hinnant8bf01dd2012-07-21 17:46:551963__packaged_task_function<_Rp(_ArgTypes...)>::swap(__packaged_task_function& __f) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:211964{
1965 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1966 {
1967 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1968 __base* __t = (__base*)&__tempbuf;
1969 __f_->__move_to(__t);
1970 __f_->destroy();
1971 __f_ = nullptr;
1972 __f.__f_->__move_to((__base*)&__buf_);
1973 __f.__f_->destroy();
1974 __f.__f_ = nullptr;
1975 __f_ = (__base*)&__buf_;
1976 __t->__move_to((__base*)&__f.__buf_);
1977 __t->destroy();
1978 __f.__f_ = (__base*)&__f.__buf_;
1979 }
1980 else if (__f_ == (__base*)&__buf_)
1981 {
1982 __f_->__move_to((__base*)&__f.__buf_);
1983 __f_->destroy();
1984 __f_ = __f.__f_;
1985 __f.__f_ = (__base*)&__f.__buf_;
1986 }
1987 else if (__f.__f_ == (__base*)&__f.__buf_)
1988 {
1989 __f.__f_->__move_to((__base*)&__buf_);
1990 __f.__f_->destroy();
1991 __f.__f_ = __f_;
1992 __f_ = (__base*)&__buf_;
1993 }
1994 else
Howard Hinnant0949eed2011-06-30 21:18:191995 _VSTD::swap(__f_, __f.__f_);
Howard Hinnant54da3382010-08-30 18:46:211996}
1997
Howard Hinnant99968442011-11-29 18:15:501998template<class _Rp, class ..._ArgTypes>
Evgeniy Stepanova3b25f82015-11-07 01:22:131999inline
Howard Hinnant99968442011-11-29 18:15:502000_Rp
2001__packaged_task_function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
Howard Hinnant54da3382010-08-30 18:46:212002{
Howard Hinnant0949eed2011-06-30 21:18:192003 return (*__f_)(_VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnant54da3382010-08-30 18:46:212004}
2005
Howard Hinnant99968442011-11-29 18:15:502006template<class _Rp, class ..._ArgTypes>
Mehdi Amini907c1192017-05-04 17:08:542007class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE packaged_task<_Rp(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:212008{
2009public:
Eric Fiselier68db6cd2016-06-01 21:05:532010 typedef _Rp result_type; // extension
Howard Hinnant54da3382010-08-30 18:46:212011
2012private:
2013 __packaged_task_function<result_type(_ArgTypes...)> __f_;
2014 promise<result_type> __p_;
2015
2016public:
2017 // construction and destruction
Howard Hinnant8c6cbb22010-09-22 14:16:262018 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552019 packaged_task() _NOEXCEPT : __p_(nullptr) {}
Marshall Clow5f2d5b92013-10-12 22:49:172020 template <class _Fp,
2021 class = typename enable_if
2022 <
2023 !is_same<
2024 typename decay<_Fp>::type,
2025 packaged_task
2026 >::value
2027 >::type
2028 >
Howard Hinnant8c6cbb22010-09-22 14:16:262029 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502030 explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
Marshall Clow5f2d5b92013-10-12 22:49:172031 template <class _Fp, class _Allocator,
2032 class = typename enable_if
2033 <
2034 !is_same<
2035 typename decay<_Fp>::type,
2036 packaged_task
2037 >::value
2038 >::type
2039 >
Howard Hinnant8c6cbb22010-09-22 14:16:262040 _LIBCPP_INLINE_VISIBILITY
Marshall Clow07546f32015-06-30 14:16:492041 packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
Howard Hinnant99968442011-11-29 18:15:502042 : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)),
Howard Hinnant54da3382010-08-30 18:46:212043 __p_(allocator_arg, __a) {}
2044 // ~packaged_task() = default;
2045
2046 // no copy
Howard Hinnant8131a012012-07-21 19:34:122047 packaged_task(const packaged_task&) = delete;
2048 packaged_task& operator=(const packaged_task&) = delete;
Howard Hinnant54da3382010-08-30 18:46:212049
2050 // move support
Howard Hinnant8c6cbb22010-09-22 14:16:262051 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552052 packaged_task(packaged_task&& __other) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:192053 : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
Howard Hinnant8c6cbb22010-09-22 14:16:262054 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552055 packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:212056 {
Howard Hinnant0949eed2011-06-30 21:18:192057 __f_ = _VSTD::move(__other.__f_);
2058 __p_ = _VSTD::move(__other.__p_);
Howard Hinnant54da3382010-08-30 18:46:212059 return *this;
2060 }
Howard Hinnant8c6cbb22010-09-22 14:16:262061 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552062 void swap(packaged_task& __other) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:212063 {
2064 __f_.swap(__other.__f_);
2065 __p_.swap(__other.__p_);
2066 }
2067
Howard Hinnant8c6cbb22010-09-22 14:16:262068 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552069 bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
Howard Hinnant54da3382010-08-30 18:46:212070
2071 // result retrieval
Howard Hinnant8c6cbb22010-09-22 14:16:262072 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:212073 future<result_type> get_future() {return __p_.get_future();}
2074
2075 // execution
2076 void operator()(_ArgTypes... __args);
2077 void make_ready_at_thread_exit(_ArgTypes... __args);
2078
2079 void reset();
2080};
2081
Howard Hinnant99968442011-11-29 18:15:502082template<class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:212083void
Howard Hinnant99968442011-11-29 18:15:502084packaged_task<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __args)
Howard Hinnant54da3382010-08-30 18:46:212085{
Howard Hinnant54da3382010-08-30 18:46:212086 if (__p_.__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:152087 __throw_future_error(future_errc::no_state);
Howard Hinnant54da3382010-08-30 18:46:212088 if (__p_.__state_->__has_value())
Eric Fiselier423ca202015-10-02 21:25:152089 __throw_future_error(future_errc::promise_already_satisfied);
Marshall Clowa1899742015-09-03 15:11:322090#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant54da3382010-08-30 18:46:212091 try
2092 {
2093#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:192094 __p_.set_value(__f_(_VSTD::forward<_ArgTypes>(__args)...));
Howard Hinnant54da3382010-08-30 18:46:212095#ifndef _LIBCPP_NO_EXCEPTIONS
2096 }
2097 catch (...)
2098 {
2099 __p_.set_exception(current_exception());
2100 }
2101#endif // _LIBCPP_NO_EXCEPTIONS
2102}
2103
Howard Hinnant99968442011-11-29 18:15:502104template<class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:212105void
Howard Hinnant99968442011-11-29 18:15:502106packaged_task<_Rp(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
Howard Hinnant54da3382010-08-30 18:46:212107{
Howard Hinnant54da3382010-08-30 18:46:212108 if (__p_.__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:152109 __throw_future_error(future_errc::no_state);
Howard Hinnant54da3382010-08-30 18:46:212110 if (__p_.__state_->__has_value())
Eric Fiselier423ca202015-10-02 21:25:152111 __throw_future_error(future_errc::promise_already_satisfied);
Marshall Clowa1899742015-09-03 15:11:322112#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant54da3382010-08-30 18:46:212113 try
2114 {
2115#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:192116 __p_.set_value_at_thread_exit(__f_(_VSTD::forward<_ArgTypes>(__args)...));
Howard Hinnant54da3382010-08-30 18:46:212117#ifndef _LIBCPP_NO_EXCEPTIONS
2118 }
2119 catch (...)
2120 {
2121 __p_.set_exception_at_thread_exit(current_exception());
2122 }
2123#endif // _LIBCPP_NO_EXCEPTIONS
2124}
2125
Howard Hinnant99968442011-11-29 18:15:502126template<class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:212127void
Howard Hinnant99968442011-11-29 18:15:502128packaged_task<_Rp(_ArgTypes...)>::reset()
Howard Hinnant54da3382010-08-30 18:46:212129{
Howard Hinnant7de47902010-11-30 20:23:322130 if (!valid())
Eric Fiselier423ca202015-10-02 21:25:152131 __throw_future_error(future_errc::no_state);
Howard Hinnant54da3382010-08-30 18:46:212132 __p_ = promise<result_type>();
2133}
2134
2135template<class ..._ArgTypes>
Mehdi Amini907c1192017-05-04 17:08:542136class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE packaged_task<void(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:212137{
2138public:
Eric Fiselier68db6cd2016-06-01 21:05:532139 typedef void result_type; // extension
Howard Hinnant54da3382010-08-30 18:46:212140
2141private:
2142 __packaged_task_function<result_type(_ArgTypes...)> __f_;
2143 promise<result_type> __p_;
2144
2145public:
2146 // construction and destruction
Howard Hinnant8c6cbb22010-09-22 14:16:262147 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552148 packaged_task() _NOEXCEPT : __p_(nullptr) {}
Marshall Clow5f2d5b92013-10-12 22:49:172149 template <class _Fp,
2150 class = typename enable_if
2151 <
2152 !is_same<
2153 typename decay<_Fp>::type,
2154 packaged_task
2155 >::value
2156 >::type
2157 >
Howard Hinnant8c6cbb22010-09-22 14:16:262158 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502159 explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
Marshall Clow5f2d5b92013-10-12 22:49:172160 template <class _Fp, class _Allocator,
2161 class = typename enable_if
2162 <
2163 !is_same<
2164 typename decay<_Fp>::type,
2165 packaged_task
2166 >::value
2167 >::type
2168 >
Howard Hinnant8c6cbb22010-09-22 14:16:262169 _LIBCPP_INLINE_VISIBILITY
Marshall Clow5706c372015-06-30 18:28:352170 packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
Howard Hinnant99968442011-11-29 18:15:502171 : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)),
Howard Hinnant54da3382010-08-30 18:46:212172 __p_(allocator_arg, __a) {}
2173 // ~packaged_task() = default;
2174
2175 // no copy
Howard Hinnant8131a012012-07-21 19:34:122176 packaged_task(const packaged_task&) = delete;
2177 packaged_task& operator=(const packaged_task&) = delete;
Howard Hinnant54da3382010-08-30 18:46:212178
2179 // move support
Howard Hinnant8c6cbb22010-09-22 14:16:262180 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552181 packaged_task(packaged_task&& __other) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:192182 : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
Howard Hinnant8c6cbb22010-09-22 14:16:262183 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552184 packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:212185 {
Howard Hinnant0949eed2011-06-30 21:18:192186 __f_ = _VSTD::move(__other.__f_);
2187 __p_ = _VSTD::move(__other.__p_);
Howard Hinnant54da3382010-08-30 18:46:212188 return *this;
2189 }
Howard Hinnant8c6cbb22010-09-22 14:16:262190 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552191 void swap(packaged_task& __other) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:212192 {
2193 __f_.swap(__other.__f_);
2194 __p_.swap(__other.__p_);
2195 }
2196
Howard Hinnant8c6cbb22010-09-22 14:16:262197 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552198 bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
Howard Hinnant54da3382010-08-30 18:46:212199
2200 // result retrieval
Howard Hinnant8c6cbb22010-09-22 14:16:262201 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:212202 future<result_type> get_future() {return __p_.get_future();}
2203
2204 // execution
2205 void operator()(_ArgTypes... __args);
2206 void make_ready_at_thread_exit(_ArgTypes... __args);
2207
2208 void reset();
2209};
2210
2211template<class ..._ArgTypes>
2212void
2213packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args)
2214{
Howard Hinnant54da3382010-08-30 18:46:212215 if (__p_.__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:152216 __throw_future_error(future_errc::no_state);
Howard Hinnant54da3382010-08-30 18:46:212217 if (__p_.__state_->__has_value())
Eric Fiselier423ca202015-10-02 21:25:152218 __throw_future_error(future_errc::promise_already_satisfied);
Marshall Clowa1899742015-09-03 15:11:322219#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant54da3382010-08-30 18:46:212220 try
2221 {
2222#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:192223 __f_(_VSTD::forward<_ArgTypes>(__args)...);
Howard Hinnant54da3382010-08-30 18:46:212224 __p_.set_value();
2225#ifndef _LIBCPP_NO_EXCEPTIONS
2226 }
2227 catch (...)
2228 {
2229 __p_.set_exception(current_exception());
2230 }
2231#endif // _LIBCPP_NO_EXCEPTIONS
2232}
2233
2234template<class ..._ArgTypes>
2235void
2236packaged_task<void(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
2237{
Howard Hinnant54da3382010-08-30 18:46:212238 if (__p_.__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:152239 __throw_future_error(future_errc::no_state);
Howard Hinnant54da3382010-08-30 18:46:212240 if (__p_.__state_->__has_value())
Eric Fiselier423ca202015-10-02 21:25:152241 __throw_future_error(future_errc::promise_already_satisfied);
Marshall Clowa1899742015-09-03 15:11:322242#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant54da3382010-08-30 18:46:212243 try
2244 {
2245#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:192246 __f_(_VSTD::forward<_ArgTypes>(__args)...);
Howard Hinnant54da3382010-08-30 18:46:212247 __p_.set_value_at_thread_exit();
2248#ifndef _LIBCPP_NO_EXCEPTIONS
2249 }
2250 catch (...)
2251 {
2252 __p_.set_exception_at_thread_exit(current_exception());
2253 }
2254#endif // _LIBCPP_NO_EXCEPTIONS
2255}
2256
2257template<class ..._ArgTypes>
2258void
2259packaged_task<void(_ArgTypes...)>::reset()
2260{
Howard Hinnant7de47902010-11-30 20:23:322261 if (!valid())
Eric Fiselier423ca202015-10-02 21:25:152262 __throw_future_error(future_errc::no_state);
Howard Hinnant54da3382010-08-30 18:46:212263 __p_ = promise<result_type>();
2264}
2265
2266template <class _Callable>
2267inline _LIBCPP_INLINE_VISIBILITY
2268void
Howard Hinnant8bf01dd2012-07-21 17:46:552269swap(packaged_task<_Callable>& __x, packaged_task<_Callable>& __y) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:212270{
2271 __x.swap(__y);
2272}
2273
2274template <class _Callable, class _Alloc>
Eric Fiselierc3589a82017-01-04 23:56:002275struct _LIBCPP_TEMPLATE_VIS uses_allocator<packaged_task<_Callable>, _Alloc>
Howard Hinnant8c6cbb22010-09-22 14:16:262276 : public true_type {};
Howard Hinnant54da3382010-08-30 18:46:212277
Howard Hinnant99968442011-11-29 18:15:502278template <class _Rp, class _Fp>
2279future<_Rp>
Howard Hinnant73d21a42010-09-04 23:28:192280#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:502281__make_deferred_assoc_state(_Fp&& __f)
Howard Hinnant54da3382010-08-30 18:46:212282#else
Howard Hinnant99968442011-11-29 18:15:502283__make_deferred_assoc_state(_Fp __f)
Howard Hinnant54da3382010-08-30 18:46:212284#endif
2285{
Howard Hinnant99968442011-11-29 18:15:502286 unique_ptr<__deferred_assoc_state<_Rp, _Fp>, __release_shared_count>
2287 __h(new __deferred_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
2288 return future<_Rp>(__h.get());
Howard Hinnant54da3382010-08-30 18:46:212289}
2290
Howard Hinnant99968442011-11-29 18:15:502291template <class _Rp, class _Fp>
2292future<_Rp>
Howard Hinnant57cff292011-05-19 15:05:042293#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:502294__make_async_assoc_state(_Fp&& __f)
Howard Hinnant57cff292011-05-19 15:05:042295#else
Howard Hinnant99968442011-11-29 18:15:502296__make_async_assoc_state(_Fp __f)
Howard Hinnant57cff292011-05-19 15:05:042297#endif
2298{
Howard Hinnant99968442011-11-29 18:15:502299 unique_ptr<__async_assoc_state<_Rp, _Fp>, __release_shared_count>
2300 __h(new __async_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
2301 _VSTD::thread(&__async_assoc_state<_Rp, _Fp>::__execute, __h.get()).detach();
2302 return future<_Rp>(__h.get());
Howard Hinnant57cff292011-05-19 15:05:042303}
2304
Howard Hinnant99968442011-11-29 18:15:502305template <class _Fp, class... _Args>
Howard Hinnant57cff292011-05-19 15:05:042306class __async_func
2307{
Howard Hinnant99968442011-11-29 18:15:502308 tuple<_Fp, _Args...> __f_;
Howard Hinnant57cff292011-05-19 15:05:042309
2310public:
Howard Hinnant99968442011-11-29 18:15:502311 typedef typename __invoke_of<_Fp, _Args...>::type _Rp;
Howard Hinnant57cff292011-05-19 15:05:042312
2313 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502314 explicit __async_func(_Fp&& __f, _Args&&... __args)
Howard Hinnant0949eed2011-06-30 21:18:192315 : __f_(_VSTD::move(__f), _VSTD::move(__args)...) {}
Howard Hinnant57cff292011-05-19 15:05:042316
2317 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0949eed2011-06-30 21:18:192318 __async_func(__async_func&& __f) : __f_(_VSTD::move(__f.__f_)) {}
Howard Hinnant57cff292011-05-19 15:05:042319
Howard Hinnant99968442011-11-29 18:15:502320 _Rp operator()()
Howard Hinnant57cff292011-05-19 15:05:042321 {
2322 typedef typename __make_tuple_indices<1+sizeof...(_Args), 1>::type _Index;
2323 return __execute(_Index());
2324 }
2325private:
2326 template <size_t ..._Indices>
Howard Hinnant99968442011-11-29 18:15:502327 _Rp
Howard Hinnant57cff292011-05-19 15:05:042328 __execute(__tuple_indices<_Indices...>)
2329 {
Howard Hinnant0949eed2011-06-30 21:18:192330 return __invoke(_VSTD::move(_VSTD::get<0>(__f_)), _VSTD::move(_VSTD::get<_Indices>(__f_))...);
Howard Hinnant57cff292011-05-19 15:05:042331 }
2332};
2333
Marshall Clow3b3108e2013-11-03 22:06:532334inline _LIBCPP_INLINE_VISIBILITY bool __does_policy_contain(launch __policy, launch __value )
Marshall Clowad2a6002013-11-03 15:43:352335{ return (int(__policy) & int(__value)) != 0; }
2336
Howard Hinnant99968442011-11-29 18:15:502337template <class _Fp, class... _Args>
2338future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
2339async(launch __policy, _Fp&& __f, _Args&&... __args)
Howard Hinnant54da3382010-08-30 18:46:212340{
Howard Hinnant99968442011-11-29 18:15:502341 typedef __async_func<typename decay<_Fp>::type, typename decay<_Args>::type...> _BF;
2342 typedef typename _BF::_Rp _Rp;
Marshall Clowad2a6002013-11-03 15:43:352343
2344#ifndef _LIBCPP_NO_EXCEPTIONS
2345 try
2346 {
2347#endif
2348 if (__does_policy_contain(__policy, launch::async))
2349 return _VSTD::__make_async_assoc_state<_Rp>(_BF(__decay_copy(_VSTD::forward<_Fp>(__f)),
Howard Hinnant0949eed2011-06-30 21:18:192350 __decay_copy(_VSTD::forward<_Args>(__args))...));
Marshall Clowad2a6002013-11-03 15:43:352351#ifndef _LIBCPP_NO_EXCEPTIONS
2352 }
2353 catch ( ... ) { if (__policy == launch::async) throw ; }
2354#endif
2355
2356 if (__does_policy_contain(__policy, launch::deferred))
2357 return _VSTD::__make_deferred_assoc_state<_Rp>(_BF(__decay_copy(_VSTD::forward<_Fp>(__f)),
Howard Hinnant0949eed2011-06-30 21:18:192358 __decay_copy(_VSTD::forward<_Args>(__args))...));
Marshall Clowad2a6002013-11-03 15:43:352359 return future<_Rp>{};
Howard Hinnant54da3382010-08-30 18:46:212360}
2361
Howard Hinnant99968442011-11-29 18:15:502362template <class _Fp, class... _Args>
Howard Hinnant54da3382010-08-30 18:46:212363inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502364future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
2365async(_Fp&& __f, _Args&&... __args)
Howard Hinnant54da3382010-08-30 18:46:212366{
Howard Hinnant99968442011-11-29 18:15:502367 return _VSTD::async(launch::any, _VSTD::forward<_Fp>(__f),
Howard Hinnant0949eed2011-06-30 21:18:192368 _VSTD::forward<_Args>(__args)...);
Howard Hinnant54da3382010-08-30 18:46:212369}
2370
2371#endif // _LIBCPP_HAS_NO_VARIADICS
2372
Howard Hinnante6e4d012010-09-03 21:46:372373// shared_future
2374
Howard Hinnant99968442011-11-29 18:15:502375template <class _Rp>
Eric Fiselierc3589a82017-01-04 23:56:002376class _LIBCPP_TEMPLATE_VIS shared_future
Howard Hinnant99be8232010-09-03 18:39:252377{
Howard Hinnant99968442011-11-29 18:15:502378 __assoc_state<_Rp>* __state_;
Howard Hinnant99be8232010-09-03 18:39:252379
2380public:
Howard Hinnant8c6cbb22010-09-22 14:16:262381 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552382 shared_future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant8c6cbb22010-09-22 14:16:262383 _LIBCPP_INLINE_VISIBILITY
Marshall Clow3d7c49b2016-11-14 19:58:052384 shared_future(const shared_future& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
Howard Hinnant99be8232010-09-03 18:39:252385 {if (__state_) __state_->__add_shared();}
Howard Hinnant73d21a42010-09-04 23:28:192386#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:262387 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552388 shared_future(future<_Rp>&& __f) _NOEXCEPT : __state_(__f.__state_)
Howard Hinnant99be8232010-09-03 18:39:252389 {__f.__state_ = nullptr;}
Howard Hinnant8c6cbb22010-09-22 14:16:262390 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552391 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
Howard Hinnant99be8232010-09-03 18:39:252392 {__rhs.__state_ = nullptr;}
Howard Hinnant73d21a42010-09-04 23:28:192393#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:252394 ~shared_future();
Marshall Clow3d7c49b2016-11-14 19:58:052395 shared_future& operator=(const shared_future& __rhs) _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:192396#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:262397 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552398 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:252399 {
2400 shared_future(std::move(__rhs)).swap(*this);
2401 return *this;
2402 }
Howard Hinnant73d21a42010-09-04 23:28:192403#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:252404
2405 // retrieving the value
Howard Hinnant8c6cbb22010-09-22 14:16:262406 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502407 const _Rp& get() const {return __state_->copy();}
Howard Hinnant99be8232010-09-03 18:39:252408
Howard Hinnant8c6cbb22010-09-22 14:16:262409 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552410 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant99be8232010-09-03 18:39:252411
2412 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:262413 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552414 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant99be8232010-09-03 18:39:252415
Howard Hinnant8c6cbb22010-09-22 14:16:262416 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252417 void wait() const {__state_->wait();}
2418 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:262419 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252420 future_status
2421 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2422 {return __state_->wait_for(__rel_time);}
2423 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:262424 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252425 future_status
2426 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2427 {return __state_->wait_until(__abs_time);}
2428};
2429
Howard Hinnant99968442011-11-29 18:15:502430template <class _Rp>
2431shared_future<_Rp>::~shared_future()
Howard Hinnant99be8232010-09-03 18:39:252432{
2433 if (__state_)
2434 __state_->__release_shared();
2435}
2436
Howard Hinnant99968442011-11-29 18:15:502437template <class _Rp>
2438shared_future<_Rp>&
Marshall Clow3d7c49b2016-11-14 19:58:052439shared_future<_Rp>::operator=(const shared_future& __rhs) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:252440{
2441 if (__rhs.__state_)
2442 __rhs.__state_->__add_shared();
2443 if (__state_)
2444 __state_->__release_shared();
2445 __state_ = __rhs.__state_;
2446 return *this;
2447}
2448
Howard Hinnant99968442011-11-29 18:15:502449template <class _Rp>
Eric Fiselierc3589a82017-01-04 23:56:002450class _LIBCPP_TEMPLATE_VIS shared_future<_Rp&>
Howard Hinnant99be8232010-09-03 18:39:252451{
Howard Hinnant99968442011-11-29 18:15:502452 __assoc_state<_Rp&>* __state_;
Howard Hinnant99be8232010-09-03 18:39:252453
2454public:
Howard Hinnant8c6cbb22010-09-22 14:16:262455 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552456 shared_future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant8c6cbb22010-09-22 14:16:262457 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252458 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2459 {if (__state_) __state_->__add_shared();}
Howard Hinnant73d21a42010-09-04 23:28:192460#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:262461 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552462 shared_future(future<_Rp&>&& __f) _NOEXCEPT : __state_(__f.__state_)
Howard Hinnant99be8232010-09-03 18:39:252463 {__f.__state_ = nullptr;}
Howard Hinnant8c6cbb22010-09-22 14:16:262464 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552465 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
Howard Hinnant99be8232010-09-03 18:39:252466 {__rhs.__state_ = nullptr;}
Howard Hinnant73d21a42010-09-04 23:28:192467#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:252468 ~shared_future();
2469 shared_future& operator=(const shared_future& __rhs);
Howard Hinnant73d21a42010-09-04 23:28:192470#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:262471 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552472 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:252473 {
2474 shared_future(std::move(__rhs)).swap(*this);
2475 return *this;
2476 }
Howard Hinnant73d21a42010-09-04 23:28:192477#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:252478
2479 // retrieving the value
Howard Hinnant8c6cbb22010-09-22 14:16:262480 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502481 _Rp& get() const {return __state_->copy();}
Howard Hinnant99be8232010-09-03 18:39:252482
Howard Hinnant8c6cbb22010-09-22 14:16:262483 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552484 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant99be8232010-09-03 18:39:252485
2486 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:262487 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552488 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant99be8232010-09-03 18:39:252489
Howard Hinnant8c6cbb22010-09-22 14:16:262490 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252491 void wait() const {__state_->wait();}
2492 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:262493 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252494 future_status
2495 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2496 {return __state_->wait_for(__rel_time);}
2497 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:262498 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252499 future_status
2500 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2501 {return __state_->wait_until(__abs_time);}
2502};
2503
Howard Hinnant99968442011-11-29 18:15:502504template <class _Rp>
2505shared_future<_Rp&>::~shared_future()
Howard Hinnant99be8232010-09-03 18:39:252506{
2507 if (__state_)
2508 __state_->__release_shared();
2509}
2510
Howard Hinnant99968442011-11-29 18:15:502511template <class _Rp>
2512shared_future<_Rp&>&
2513shared_future<_Rp&>::operator=(const shared_future& __rhs)
Howard Hinnant99be8232010-09-03 18:39:252514{
2515 if (__rhs.__state_)
2516 __rhs.__state_->__add_shared();
2517 if (__state_)
2518 __state_->__release_shared();
2519 __state_ = __rhs.__state_;
2520 return *this;
2521}
2522
2523template <>
Mehdi Amini907c1192017-05-04 17:08:542524class _LIBCPP_TYPE_VIS _LIBCPP_AVAILABILITY_FUTURE shared_future<void>
Howard Hinnant99be8232010-09-03 18:39:252525{
2526 __assoc_sub_state* __state_;
2527
2528public:
Howard Hinnant8c6cbb22010-09-22 14:16:262529 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552530 shared_future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant8c6cbb22010-09-22 14:16:262531 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252532 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2533 {if (__state_) __state_->__add_shared();}
Howard Hinnant73d21a42010-09-04 23:28:192534#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:262535 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552536 shared_future(future<void>&& __f) _NOEXCEPT : __state_(__f.__state_)
Howard Hinnant99be8232010-09-03 18:39:252537 {__f.__state_ = nullptr;}
Howard Hinnant8c6cbb22010-09-22 14:16:262538 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552539 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
Howard Hinnant99be8232010-09-03 18:39:252540 {__rhs.__state_ = nullptr;}
Howard Hinnant73d21a42010-09-04 23:28:192541#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:252542 ~shared_future();
2543 shared_future& operator=(const shared_future& __rhs);
Howard Hinnant73d21a42010-09-04 23:28:192544#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:262545 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552546 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:252547 {
2548 shared_future(std::move(__rhs)).swap(*this);
2549 return *this;
2550 }
Howard Hinnant73d21a42010-09-04 23:28:192551#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:252552
2553 // retrieving the value
Howard Hinnant8c6cbb22010-09-22 14:16:262554 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252555 void get() const {__state_->copy();}
2556
Howard Hinnant8c6cbb22010-09-22 14:16:262557 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552558 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant99be8232010-09-03 18:39:252559
2560 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:262561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552562 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant99be8232010-09-03 18:39:252563
Howard Hinnant8c6cbb22010-09-22 14:16:262564 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252565 void wait() const {__state_->wait();}
2566 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:262567 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252568 future_status
2569 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2570 {return __state_->wait_for(__rel_time);}
2571 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:262572 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252573 future_status
2574 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2575 {return __state_->wait_until(__abs_time);}
2576};
2577
Howard Hinnant99968442011-11-29 18:15:502578template <class _Rp>
Howard Hinnant99be8232010-09-03 18:39:252579inline _LIBCPP_INLINE_VISIBILITY
2580void
Howard Hinnant8bf01dd2012-07-21 17:46:552581swap(shared_future<_Rp>& __x, shared_future<_Rp>& __y) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:252582{
2583 __x.swap(__y);
2584}
2585
Howard Hinnant99968442011-11-29 18:15:502586template <class _Rp>
Evgeniy Stepanova3b25f82015-11-07 01:22:132587inline
Howard Hinnant99968442011-11-29 18:15:502588shared_future<_Rp>
Marshall Clow9bb0cca2017-01-24 23:28:252589future<_Rp>::share() _NOEXCEPT
Howard Hinnante6e4d012010-09-03 21:46:372590{
Howard Hinnant99968442011-11-29 18:15:502591 return shared_future<_Rp>(_VSTD::move(*this));
Howard Hinnante6e4d012010-09-03 21:46:372592}
2593
Howard Hinnant99968442011-11-29 18:15:502594template <class _Rp>
Evgeniy Stepanova3b25f82015-11-07 01:22:132595inline
Howard Hinnant99968442011-11-29 18:15:502596shared_future<_Rp&>
Marshall Clow9bb0cca2017-01-24 23:28:252597future<_Rp&>::share() _NOEXCEPT
Howard Hinnante6e4d012010-09-03 21:46:372598{
Howard Hinnant99968442011-11-29 18:15:502599 return shared_future<_Rp&>(_VSTD::move(*this));
Howard Hinnant7de47902010-11-30 20:23:322600}
2601
Howard Hinnanta4451512010-12-02 16:45:212602#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2603
Evgeniy Stepanova3b25f82015-11-07 01:22:132604inline
Howard Hinnant7de47902010-11-30 20:23:322605shared_future<void>
Marshall Clow9bb0cca2017-01-24 23:28:252606future<void>::share() _NOEXCEPT
Howard Hinnant7de47902010-11-30 20:23:322607{
Howard Hinnant0949eed2011-06-30 21:18:192608 return shared_future<void>(_VSTD::move(*this));
Howard Hinnante6e4d012010-09-03 21:46:372609}
2610
Howard Hinnanta4451512010-12-02 16:45:212611#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2612
Howard Hinnantbc8d3f92010-05-11 19:42:162613_LIBCPP_END_NAMESPACE_STD
2614
Jonathan Roelofs8d86b2e2014-09-05 19:45:052615#endif // !_LIBCPP_HAS_NO_THREADS
2616
Howard Hinnantbc8d3f92010-05-11 19:42:162617#endif // _LIBCPP_FUTURE