blob: 957a23cd8d986cd2ac0e8b647cceb8775b546ad8 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===--------------------------- future -----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// 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:16 +00008//
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:10 +000022 future_already_retrieved = 1,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000023 promise_already_satisfied,
Howard Hinnantcd942f12013-09-14 18:20:10 +000024 no_state,
25 broken_promise
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000026};
27
28enum class launch
29{
Howard Hinnant66895642010-11-23 18:33:54 +000030 async = 1,
31 deferred = 2,
32 any = async | deferred
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000033};
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:55 +000043error_code make_error_code(future_errc e) noexcept;
44error_condition make_error_condition(future_errc e) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000045
Howard Hinnant8bf01dd2012-07-21 17:46:55 +000046const error_category& future_category() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000047
48class future_error
49 : public logic_error
50{
51public:
52 future_error(error_code ec); // exposition only
53
Howard Hinnant8bf01dd2012-07-21 17:46:55 +000054 const error_code& code() const noexcept;
55 const char* what() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000056};
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:55 +000065 promise(promise&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000066 promise(const promise& rhs) = delete;
67 ~promise();
68
69 // assignment
Howard Hinnant8bf01dd2012-07-21 17:46:55 +000070 promise& operator=(promise&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000071 promise& operator=(const promise& rhs) = delete;
Howard Hinnant8bf01dd2012-07-21 17:46:55 +000072 void swap(promise& other) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000073
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:55 +000095 promise(promise&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000096 promise(const promise& rhs) = delete;
97 ~promise();
98
99 // assignment
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000100 promise& operator=(promise&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000101 promise& operator=(const promise& rhs) = delete;
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000102 void swap(promise& other) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000103
104 // retrieving the result
Howard Hinnant47499b12010-08-27 20:10:19 +0000105 future<R&> get_future();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000106
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:55 +0000123 promise(promise&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000124 promise(const promise& rhs) = delete;
125 ~promise();
126
127 // assignment
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000128 promise& operator=(promise&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000129 promise& operator=(const promise& rhs) = delete;
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000130 void swap(promise& other) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000131
132 // retrieving the result
Howard Hinnant47499b12010-08-27 20:10:19 +0000133 future<void> get_future();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000134
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:55 +0000144template <class R> void swap(promise<R>& x, promise<R>& y) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000145
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:55 +0000153 future() noexcept;
154 future(future&&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000155 future(const future& rhs) = delete;
156 ~future();
157 future& operator=(const future& rhs) = delete;
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000158 future& operator=(future&&) noexcept;
159 shared_future<R> share();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000160
161 // retrieving the value
162 R get();
163
164 // functions to check state
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000165 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000166
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:55 +0000180 future() noexcept;
181 future(future&&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000182 future(const future& rhs) = delete;
183 ~future();
184 future& operator=(const future& rhs) = delete;
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000185 future& operator=(future&&) noexcept;
186 shared_future<R&> share();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000187
188 // retrieving the value
189 R& get();
190
191 // functions to check state
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000192 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000193
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:55 +0000207 future() noexcept;
208 future(future&&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000209 future(const future& rhs) = delete;
210 ~future();
211 future& operator=(const future& rhs) = delete;
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000212 future& operator=(future&&) noexcept;
213 shared_future<void> share();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000214
215 // retrieving the value
216 void get();
217
218 // functions to check state
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000219 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000220
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:55 +0000234 shared_future() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000235 shared_future(const shared_future& rhs);
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000236 shared_future(future<R>&&) noexcept;
237 shared_future(shared_future&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000238 ~shared_future();
239 shared_future& operator=(const shared_future& rhs);
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000240 shared_future& operator=(shared_future&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000241
242 // retrieving the value
243 const R& get() const;
244
245 // functions to check state
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000246 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000247
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:55 +0000261 shared_future() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000262 shared_future(const shared_future& rhs);
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000263 shared_future(future<R&>&&) noexcept;
264 shared_future(shared_future&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000265 ~shared_future();
266 shared_future& operator=(const shared_future& rhs);
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000267 shared_future& operator=(shared_future&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000268
269 // retrieving the value
270 R& get() const;
271
272 // functions to check state
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000273 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000274
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:55 +0000288 shared_future() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000289 shared_future(const shared_future& rhs);
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000290 shared_future(future<void>&&) noexcept;
291 shared_future(shared_future&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000292 ~shared_future();
293 shared_future& operator=(const shared_future& rhs);
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000294 shared_future& operator=(shared_future&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000295
296 // retrieving the value
297 void get() const;
298
299 // functions to check state
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000300 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000301
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:16 +0000311template <class F, class... Args>
Howard Hinnant0836f872013-09-21 18:17:23 +0000312 future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000313 async(F&& f, Args&&... args);
314
315template <class F, class... Args>
Howard Hinnant0836f872013-09-21 18:17:23 +0000316 future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000317 async(launch policy, F&& f, Args&&... args);
318
Howard Hinnantf5256e12010-05-11 21:36:01 +0000319template <class> class packaged_task; // undefined
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000320
321template <class R, class... ArgTypes>
322class packaged_task<R(ArgTypes...)>
323{
324public:
325 typedef R result_type;
326
327 // construction and destruction
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000328 packaged_task() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000329 template <class F>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000330 explicit packaged_task(F&& f);
331 template <class F, class Allocator>
Marshall Clow07546f32015-06-30 14:16:49 +0000332 packaged_task(allocator_arg_t, const Allocator& a, F&& f);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000333 ~packaged_task();
334
335 // no copy
Howard Hinnant8131a012012-07-21 19:34:12 +0000336 packaged_task(const packaged_task&) = delete;
337 packaged_task& operator=(const packaged_task&) = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000338
339 // move support
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000340 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:16 +0000343
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000344 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000345
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:55 +0000357 void swap(packaged_task<R(ArgTypes...)&, packaged_task<R(ArgTypes...)>&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000358
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:19 +0000367#include <memory>
368#include <chrono>
369#include <exception>
Howard Hinnante6e4d012010-09-03 21:46:37 +0000370#include <mutex>
Howard Hinnant47499b12010-08-27 20:10:19 +0000371#include <thread>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000372
Howard Hinnant08e17472011-10-17 20:05:10 +0000373#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000374#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000375#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000376
Jonathan Roelofsbaed05d2014-09-05 20:28:44 +0000377#ifdef _LIBCPP_HAS_NO_THREADS
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +0000378#error <future> is not supported on this single threaded system
379#else // !_LIBCPP_HAS_NO_THREADS
380
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000381_LIBCPP_BEGIN_NAMESPACE_STD
382
383//enum class future_errc
Howard Hinnantf6d875f2011-12-02 19:36:40 +0000384_LIBCPP_DECLARE_STRONG_ENUM(future_errc)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000385{
Howard Hinnantcd942f12013-09-14 18:20:10 +0000386 future_already_retrieved = 1,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000387 promise_already_satisfied,
Howard Hinnantcd942f12013-09-14 18:20:10 +0000388 no_state,
389 broken_promise
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000390};
Howard Hinnantf6d875f2011-12-02 19:36:40 +0000391_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_errc)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000392
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000393template <>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000394struct _LIBCPP_TYPE_VIS_ONLY is_error_code_enum<future_errc> : public true_type {};
Howard Hinnanta6521722010-08-25 17:32:05 +0000395
Howard Hinnantf6d875f2011-12-02 19:36:40 +0000396#ifdef _LIBCPP_HAS_NO_STRONG_ENUMS
397template <>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000398struct _LIBCPP_TYPE_VIS_ONLY is_error_code_enum<future_errc::__lx> : public true_type { };
Howard Hinnantf6d875f2011-12-02 19:36:40 +0000399#endif
400
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000401//enum class launch
Howard Hinnantf6d875f2011-12-02 19:36:40 +0000402_LIBCPP_DECLARE_STRONG_ENUM(launch)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000403{
Howard Hinnant66895642010-11-23 18:33:54 +0000404 async = 1,
405 deferred = 2,
406 any = async | deferred
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000407};
Howard Hinnantf6d875f2011-12-02 19:36:40 +0000408_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(launch)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000409
Howard Hinnantf491e512013-06-29 18:38:17 +0000410#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:41 +0000450 return static_cast<launch>(~static_cast<__launch_underlying_type>(__x) & 3);
Howard Hinnantf491e512013-06-29 18:38:17 +0000451}
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:16 +0000476//enum class future_status
Howard Hinnantf6d875f2011-12-02 19:36:40 +0000477_LIBCPP_DECLARE_STRONG_ENUM(future_status)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000478{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000479 ready,
480 timeout,
481 deferred
482};
Howard Hinnantf6d875f2011-12-02 19:36:40 +0000483_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_status)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000484
Howard Hinnant83eade62013-03-06 23:30:19 +0000485_LIBCPP_FUNC_VIS
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000486const error_category& future_category() _NOEXCEPT;
Howard Hinnanta6521722010-08-25 17:32:05 +0000487
488inline _LIBCPP_INLINE_VISIBILITY
489error_code
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000490make_error_code(future_errc __e) _NOEXCEPT
Howard Hinnanta6521722010-08-25 17:32:05 +0000491{
492 return error_code(static_cast<int>(__e), future_category());
493}
494
495inline _LIBCPP_INLINE_VISIBILITY
496error_condition
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000497make_error_condition(future_errc __e) _NOEXCEPT
Howard Hinnanta6521722010-08-25 17:32:05 +0000498{
499 return error_condition(static_cast<int>(__e), future_category());
500}
501
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000502class _LIBCPP_EXCEPTION_ABI future_error
Howard Hinnanta6521722010-08-25 17:32:05 +0000503 : public logic_error
504{
505 error_code __ec_;
506public:
507 future_error(error_code __ec);
508
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000509 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000510 const error_code& code() const _NOEXCEPT {return __ec_;}
Howard Hinnantac6de542011-07-07 21:03:52 +0000511
512 virtual ~future_error() _NOEXCEPT;
Howard Hinnanta6521722010-08-25 17:32:05 +0000513};
514
Eric Fiselier423ca202015-10-02 21:25:15 +0000515inline _LIBCPP_ALWAYS_INLINE
516void __throw_future_error(future_errc _Ev)
Marshall Clowa1899742015-09-03 15:11:32 +0000517{
518#ifndef _LIBCPP_NO_EXCEPTIONS
519 throw future_error(make_error_code(_Ev));
520#else
521 assert(!"future_error");
522#endif
523}
524
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000525class _LIBCPP_TYPE_VIS __assoc_sub_state
Howard Hinnant47499b12010-08-27 20:10:19 +0000526 : public __shared_count
527{
528protected:
529 exception_ptr __exception_;
530 mutable mutex __mut_;
531 mutable condition_variable __cv_;
532 unsigned __state_;
533
Howard Hinnant1694d232011-05-28 14:41:13 +0000534 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant54da3382010-08-30 18:46:21 +0000535 void __sub_wait(unique_lock<mutex>& __lk);
Howard Hinnant47499b12010-08-27 20:10:19 +0000536public:
537 enum
538 {
539 __constructed = 1,
540 __future_attached = 2,
541 ready = 4,
542 deferred = 8
543 };
544
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000545 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000546 __assoc_sub_state() : __state_(0) {}
547
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000548 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000549 bool __has_value() const
550 {return (__state_ & __constructed) || (__exception_ != nullptr);}
551
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000552 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1b031c92013-01-14 20:01:24 +0000553 void __set_future_attached()
554 {
555 lock_guard<mutex> __lk(__mut_);
556 __state_ |= __future_attached;
557 }
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000558 _LIBCPP_INLINE_VISIBILITY
Marshall Clow9de3d4c2013-10-13 01:02:45 +0000559 bool __has_future_attached() const {return (__state_ & __future_attached) != 0;}
Howard Hinnant47499b12010-08-27 20:10:19 +0000560
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +0000562 void __set_deferred() {__state_ |= deferred;}
563
Howard Hinnant47499b12010-08-27 20:10:19 +0000564 void __make_ready();
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000565 _LIBCPP_INLINE_VISIBILITY
Marshall Clow9de3d4c2013-10-13 01:02:45 +0000566 bool __is_ready() const {return (__state_ & ready) != 0;}
Howard Hinnant47499b12010-08-27 20:10:19 +0000567
568 void set_value();
569 void set_value_at_thread_exit();
570
571 void set_exception(exception_ptr __p);
572 void set_exception_at_thread_exit(exception_ptr __p);
573
574 void copy();
575
Howard Hinnant54da3382010-08-30 18:46:21 +0000576 void wait();
Howard Hinnant47499b12010-08-27 20:10:19 +0000577 template <class _Rep, class _Period>
578 future_status
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000579 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000580 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const;
581 template <class _Clock, class _Duration>
582 future_status
583 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const;
Howard Hinnant54da3382010-08-30 18:46:21 +0000584
585 virtual void __execute();
Howard Hinnant47499b12010-08-27 20:10:19 +0000586};
587
Howard Hinnantf39daa82010-08-28 21:01:06 +0000588template <class _Clock, class _Duration>
589future_status
590__assoc_sub_state::wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
591{
592 unique_lock<mutex> __lk(__mut_);
Howard Hinnant54da3382010-08-30 18:46:21 +0000593 if (__state_ & deferred)
594 return future_status::deferred;
595 while (!(__state_ & ready) && _Clock::now() < __abs_time)
Howard Hinnantf39daa82010-08-28 21:01:06 +0000596 __cv_.wait_until(__lk, __abs_time);
597 if (__state_ & ready)
598 return future_status::ready;
Howard Hinnantf39daa82010-08-28 21:01:06 +0000599 return future_status::timeout;
600}
601
602template <class _Rep, class _Period>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000603inline
Howard Hinnantf39daa82010-08-28 21:01:06 +0000604future_status
605__assoc_sub_state::wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
606{
Howard Hinnantf8f85212010-11-20 19:16:30 +0000607 return wait_until(chrono::steady_clock::now() + __rel_time);
Howard Hinnantf39daa82010-08-28 21:01:06 +0000608}
609
Howard Hinnant99968442011-11-29 18:15:50 +0000610template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +0000611class __assoc_state
612 : public __assoc_sub_state
613{
614 typedef __assoc_sub_state base;
Howard Hinnant99968442011-11-29 18:15:50 +0000615 typedef typename aligned_storage<sizeof(_Rp), alignment_of<_Rp>::value>::type _Up;
Howard Hinnant47499b12010-08-27 20:10:19 +0000616protected:
Howard Hinnant99968442011-11-29 18:15:50 +0000617 _Up __value_;
Howard Hinnant47499b12010-08-27 20:10:19 +0000618
Howard Hinnant1694d232011-05-28 14:41:13 +0000619 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant47499b12010-08-27 20:10:19 +0000620public:
621
622 template <class _Arg>
Howard Hinnant73d21a42010-09-04 23:28:19 +0000623#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +0000624 void set_value(_Arg&& __arg);
625#else
626 void set_value(_Arg& __arg);
627#endif
628
629 template <class _Arg>
Howard Hinnant73d21a42010-09-04 23:28:19 +0000630#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +0000631 void set_value_at_thread_exit(_Arg&& __arg);
632#else
633 void set_value_at_thread_exit(_Arg& __arg);
634#endif
635
Howard Hinnant99968442011-11-29 18:15:50 +0000636 _Rp move();
637 typename add_lvalue_reference<_Rp>::type copy();
Howard Hinnant47499b12010-08-27 20:10:19 +0000638};
639
Howard Hinnant99968442011-11-29 18:15:50 +0000640template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +0000641void
Howard Hinnant99968442011-11-29 18:15:50 +0000642__assoc_state<_Rp>::__on_zero_shared() _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +0000643{
644 if (this->__state_ & base::__constructed)
Howard Hinnant99968442011-11-29 18:15:50 +0000645 reinterpret_cast<_Rp*>(&__value_)->~_Rp();
Howard Hinnant47499b12010-08-27 20:10:19 +0000646 delete this;
647}
648
Howard Hinnant99968442011-11-29 18:15:50 +0000649template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +0000650template <class _Arg>
651void
Howard Hinnant73d21a42010-09-04 23:28:19 +0000652#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +0000653__assoc_state<_Rp>::set_value(_Arg&& __arg)
Howard Hinnant47499b12010-08-27 20:10:19 +0000654#else
Howard Hinnant99968442011-11-29 18:15:50 +0000655__assoc_state<_Rp>::set_value(_Arg& __arg)
Howard Hinnant47499b12010-08-27 20:10:19 +0000656#endif
657{
658 unique_lock<mutex> __lk(this->__mut_);
659 if (this->__has_value())
Eric Fiselier423ca202015-10-02 21:25:15 +0000660 __throw_future_error(future_errc::promise_already_satisfied);
Howard Hinnant99968442011-11-29 18:15:50 +0000661 ::new(&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
Howard Hinnant47499b12010-08-27 20:10:19 +0000662 this->__state_ |= base::__constructed | base::ready;
Howard Hinnant47499b12010-08-27 20:10:19 +0000663 __cv_.notify_all();
664}
665
Howard Hinnant99968442011-11-29 18:15:50 +0000666template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +0000667template <class _Arg>
668void
Howard Hinnant73d21a42010-09-04 23:28:19 +0000669#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +0000670__assoc_state<_Rp>::set_value_at_thread_exit(_Arg&& __arg)
Howard Hinnant47499b12010-08-27 20:10:19 +0000671#else
Howard Hinnant99968442011-11-29 18:15:50 +0000672__assoc_state<_Rp>::set_value_at_thread_exit(_Arg& __arg)
Howard Hinnant47499b12010-08-27 20:10:19 +0000673#endif
674{
675 unique_lock<mutex> __lk(this->__mut_);
676 if (this->__has_value())
Eric Fiselier423ca202015-10-02 21:25:15 +0000677 __throw_future_error(future_errc::promise_already_satisfied);
Howard Hinnant99968442011-11-29 18:15:50 +0000678 ::new(&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
Howard Hinnant47499b12010-08-27 20:10:19 +0000679 this->__state_ |= base::__constructed;
Howard Hinnant5306d682010-10-14 19:18:04 +0000680 __thread_local_data()->__make_ready_at_thread_exit(this);
Howard Hinnant47499b12010-08-27 20:10:19 +0000681}
682
Howard Hinnant99968442011-11-29 18:15:50 +0000683template <class _Rp>
684_Rp
685__assoc_state<_Rp>::move()
Howard Hinnant47499b12010-08-27 20:10:19 +0000686{
687 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant54da3382010-08-30 18:46:21 +0000688 this->__sub_wait(__lk);
Howard Hinnant47499b12010-08-27 20:10:19 +0000689 if (this->__exception_ != nullptr)
690 rethrow_exception(this->__exception_);
Howard Hinnant99968442011-11-29 18:15:50 +0000691 return _VSTD::move(*reinterpret_cast<_Rp*>(&__value_));
Howard Hinnant47499b12010-08-27 20:10:19 +0000692}
693
Howard Hinnant99968442011-11-29 18:15:50 +0000694template <class _Rp>
695typename add_lvalue_reference<_Rp>::type
696__assoc_state<_Rp>::copy()
Howard Hinnant47499b12010-08-27 20:10:19 +0000697{
698 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant54da3382010-08-30 18:46:21 +0000699 this->__sub_wait(__lk);
Howard Hinnant47499b12010-08-27 20:10:19 +0000700 if (this->__exception_ != nullptr)
701 rethrow_exception(this->__exception_);
Howard Hinnant99968442011-11-29 18:15:50 +0000702 return *reinterpret_cast<_Rp*>(&__value_);
Howard Hinnant47499b12010-08-27 20:10:19 +0000703}
704
Howard Hinnant99968442011-11-29 18:15:50 +0000705template <class _Rp>
706class __assoc_state<_Rp&>
Howard Hinnantf39daa82010-08-28 21:01:06 +0000707 : public __assoc_sub_state
708{
709 typedef __assoc_sub_state base;
Howard Hinnant99968442011-11-29 18:15:50 +0000710 typedef _Rp* _Up;
Howard Hinnantf39daa82010-08-28 21:01:06 +0000711protected:
Howard Hinnant99968442011-11-29 18:15:50 +0000712 _Up __value_;
Howard Hinnantf39daa82010-08-28 21:01:06 +0000713
Howard Hinnant1694d232011-05-28 14:41:13 +0000714 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnantf39daa82010-08-28 21:01:06 +0000715public:
716
Howard Hinnant99968442011-11-29 18:15:50 +0000717 void set_value(_Rp& __arg);
718 void set_value_at_thread_exit(_Rp& __arg);
Howard Hinnantf39daa82010-08-28 21:01:06 +0000719
Howard Hinnant99968442011-11-29 18:15:50 +0000720 _Rp& copy();
Howard Hinnantf39daa82010-08-28 21:01:06 +0000721};
722
Howard Hinnant99968442011-11-29 18:15:50 +0000723template <class _Rp>
Howard Hinnantf39daa82010-08-28 21:01:06 +0000724void
Howard Hinnant99968442011-11-29 18:15:50 +0000725__assoc_state<_Rp&>::__on_zero_shared() _NOEXCEPT
Howard Hinnantf39daa82010-08-28 21:01:06 +0000726{
727 delete this;
728}
729
Howard Hinnant99968442011-11-29 18:15:50 +0000730template <class _Rp>
Howard Hinnantf39daa82010-08-28 21:01:06 +0000731void
Howard Hinnant99968442011-11-29 18:15:50 +0000732__assoc_state<_Rp&>::set_value(_Rp& __arg)
Howard Hinnantf39daa82010-08-28 21:01:06 +0000733{
734 unique_lock<mutex> __lk(this->__mut_);
735 if (this->__has_value())
Eric Fiselier423ca202015-10-02 21:25:15 +0000736 __throw_future_error(future_errc::promise_already_satisfied);
Howard Hinnanta4e87ab2013-08-08 18:38:55 +0000737 __value_ = _VSTD::addressof(__arg);
Howard Hinnantf39daa82010-08-28 21:01:06 +0000738 this->__state_ |= base::__constructed | base::ready;
Howard Hinnantf39daa82010-08-28 21:01:06 +0000739 __cv_.notify_all();
740}
741
Howard Hinnant99968442011-11-29 18:15:50 +0000742template <class _Rp>
Howard Hinnantf39daa82010-08-28 21:01:06 +0000743void
Howard Hinnant99968442011-11-29 18:15:50 +0000744__assoc_state<_Rp&>::set_value_at_thread_exit(_Rp& __arg)
Howard Hinnantf39daa82010-08-28 21:01:06 +0000745{
746 unique_lock<mutex> __lk(this->__mut_);
747 if (this->__has_value())
Eric Fiselier423ca202015-10-02 21:25:15 +0000748 __throw_future_error(future_errc::promise_already_satisfied);
Howard Hinnanta4e87ab2013-08-08 18:38:55 +0000749 __value_ = _VSTD::addressof(__arg);
Howard Hinnantf39daa82010-08-28 21:01:06 +0000750 this->__state_ |= base::__constructed;
Howard Hinnant5306d682010-10-14 19:18:04 +0000751 __thread_local_data()->__make_ready_at_thread_exit(this);
Howard Hinnantf39daa82010-08-28 21:01:06 +0000752}
753
Howard Hinnant99968442011-11-29 18:15:50 +0000754template <class _Rp>
755_Rp&
756__assoc_state<_Rp&>::copy()
Howard Hinnantf39daa82010-08-28 21:01:06 +0000757{
758 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant54da3382010-08-30 18:46:21 +0000759 this->__sub_wait(__lk);
Howard Hinnantf39daa82010-08-28 21:01:06 +0000760 if (this->__exception_ != nullptr)
761 rethrow_exception(this->__exception_);
762 return *__value_;
763}
764
Howard Hinnant99968442011-11-29 18:15:50 +0000765template <class _Rp, class _Alloc>
Howard Hinnant47499b12010-08-27 20:10:19 +0000766class __assoc_state_alloc
Howard Hinnant99968442011-11-29 18:15:50 +0000767 : public __assoc_state<_Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +0000768{
Howard Hinnant99968442011-11-29 18:15:50 +0000769 typedef __assoc_state<_Rp> base;
Howard Hinnant47499b12010-08-27 20:10:19 +0000770 _Alloc __alloc_;
771
Howard Hinnant1694d232011-05-28 14:41:13 +0000772 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant47499b12010-08-27 20:10:19 +0000773public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000774 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000775 explicit __assoc_state_alloc(const _Alloc& __a)
776 : __alloc_(__a) {}
777};
778
Howard Hinnant99968442011-11-29 18:15:50 +0000779template <class _Rp, class _Alloc>
Howard Hinnant47499b12010-08-27 20:10:19 +0000780void
Howard Hinnant99968442011-11-29 18:15:50 +0000781__assoc_state_alloc<_Rp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +0000782{
783 if (this->__state_ & base::__constructed)
Howard Hinnanta4e87ab2013-08-08 18:38:55 +0000784 reinterpret_cast<_Rp*>(_VSTD::addressof(this->__value_))->~_Rp();
Eric Fiselier8492cd82015-02-05 23:01:40 +0000785 typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
786 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4d2413c2014-10-23 06:24:45 +0000787 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselier8492cd82015-02-05 23:01:40 +0000788 _Al __a(__alloc_);
Howard Hinnant47499b12010-08-27 20:10:19 +0000789 this->~__assoc_state_alloc();
Eric Fiselier4d2413c2014-10-23 06:24:45 +0000790 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnant47499b12010-08-27 20:10:19 +0000791}
792
Howard Hinnant99968442011-11-29 18:15:50 +0000793template <class _Rp, class _Alloc>
794class __assoc_state_alloc<_Rp&, _Alloc>
795 : public __assoc_state<_Rp&>
Howard Hinnantf39daa82010-08-28 21:01:06 +0000796{
Howard Hinnant99968442011-11-29 18:15:50 +0000797 typedef __assoc_state<_Rp&> base;
Howard Hinnantf39daa82010-08-28 21:01:06 +0000798 _Alloc __alloc_;
799
Howard Hinnant1694d232011-05-28 14:41:13 +0000800 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnantf39daa82010-08-28 21:01:06 +0000801public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000802 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39daa82010-08-28 21:01:06 +0000803 explicit __assoc_state_alloc(const _Alloc& __a)
804 : __alloc_(__a) {}
805};
806
Howard Hinnant99968442011-11-29 18:15:50 +0000807template <class _Rp, class _Alloc>
Howard Hinnantf39daa82010-08-28 21:01:06 +0000808void
Howard Hinnant99968442011-11-29 18:15:50 +0000809__assoc_state_alloc<_Rp&, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantf39daa82010-08-28 21:01:06 +0000810{
Eric Fiselier8492cd82015-02-05 23:01:40 +0000811 typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
812 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4d2413c2014-10-23 06:24:45 +0000813 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselier8492cd82015-02-05 23:01:40 +0000814 _Al __a(__alloc_);
Howard Hinnantf39daa82010-08-28 21:01:06 +0000815 this->~__assoc_state_alloc();
Eric Fiselier4d2413c2014-10-23 06:24:45 +0000816 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantf39daa82010-08-28 21:01:06 +0000817}
818
Howard Hinnant47499b12010-08-27 20:10:19 +0000819template <class _Alloc>
820class __assoc_sub_state_alloc
821 : public __assoc_sub_state
822{
823 typedef __assoc_sub_state base;
824 _Alloc __alloc_;
825
Howard Hinnant1694d232011-05-28 14:41:13 +0000826 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant47499b12010-08-27 20:10:19 +0000827public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000828 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000829 explicit __assoc_sub_state_alloc(const _Alloc& __a)
830 : __alloc_(__a) {}
831};
832
833template <class _Alloc>
834void
Howard Hinnant1694d232011-05-28 14:41:13 +0000835__assoc_sub_state_alloc<_Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +0000836{
Eric Fiselier8492cd82015-02-05 23:01:40 +0000837 typedef typename __allocator_traits_rebind<_Alloc, __assoc_sub_state_alloc>::type _Al;
838 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4d2413c2014-10-23 06:24:45 +0000839 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselier8492cd82015-02-05 23:01:40 +0000840 _Al __a(__alloc_);
Howard Hinnant47499b12010-08-27 20:10:19 +0000841 this->~__assoc_sub_state_alloc();
Eric Fiselier4d2413c2014-10-23 06:24:45 +0000842 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnant47499b12010-08-27 20:10:19 +0000843}
844
Howard Hinnant99968442011-11-29 18:15:50 +0000845template <class _Rp, class _Fp>
Howard Hinnant54da3382010-08-30 18:46:21 +0000846class __deferred_assoc_state
Howard Hinnant99968442011-11-29 18:15:50 +0000847 : public __assoc_state<_Rp>
Howard Hinnant54da3382010-08-30 18:46:21 +0000848{
Howard Hinnant99968442011-11-29 18:15:50 +0000849 typedef __assoc_state<_Rp> base;
Howard Hinnant54da3382010-08-30 18:46:21 +0000850
Howard Hinnant99968442011-11-29 18:15:50 +0000851 _Fp __func_;
Howard Hinnant54da3382010-08-30 18:46:21 +0000852
853public:
Howard Hinnant73d21a42010-09-04 23:28:19 +0000854#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000855 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000856 explicit __deferred_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:21 +0000857#endif
858
859 virtual void __execute();
860};
861
Howard Hinnant73d21a42010-09-04 23:28:19 +0000862#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21 +0000863
Howard Hinnant99968442011-11-29 18:15:50 +0000864template <class _Rp, class _Fp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000865inline
Howard Hinnant99968442011-11-29 18:15:50 +0000866__deferred_assoc_state<_Rp, _Fp>::__deferred_assoc_state(_Fp&& __f)
867 : __func_(_VSTD::forward<_Fp>(__f))
Howard Hinnant54da3382010-08-30 18:46:21 +0000868{
869 this->__set_deferred();
870}
871
Howard Hinnant73d21a42010-09-04 23:28:19 +0000872#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21 +0000873
Howard Hinnant99968442011-11-29 18:15:50 +0000874template <class _Rp, class _Fp>
Howard Hinnant54da3382010-08-30 18:46:21 +0000875void
Howard Hinnant99968442011-11-29 18:15:50 +0000876__deferred_assoc_state<_Rp, _Fp>::__execute()
Howard Hinnant54da3382010-08-30 18:46:21 +0000877{
878#ifndef _LIBCPP_NO_EXCEPTIONS
879 try
880 {
881#endif // _LIBCPP_NO_EXCEPTIONS
882 this->set_value(__func_());
883#ifndef _LIBCPP_NO_EXCEPTIONS
884 }
885 catch (...)
886 {
887 this->set_exception(current_exception());
888 }
889#endif // _LIBCPP_NO_EXCEPTIONS
890}
891
Howard Hinnant99968442011-11-29 18:15:50 +0000892template <class _Fp>
893class __deferred_assoc_state<void, _Fp>
Howard Hinnant54da3382010-08-30 18:46:21 +0000894 : public __assoc_sub_state
895{
896 typedef __assoc_sub_state base;
897
Howard Hinnant99968442011-11-29 18:15:50 +0000898 _Fp __func_;
Howard Hinnant54da3382010-08-30 18:46:21 +0000899
900public:
Howard Hinnant73d21a42010-09-04 23:28:19 +0000901#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000902 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000903 explicit __deferred_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:21 +0000904#endif
905
906 virtual void __execute();
907};
908
Howard Hinnant73d21a42010-09-04 23:28:19 +0000909#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21 +0000910
Howard Hinnant99968442011-11-29 18:15:50 +0000911template <class _Fp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000912inline
Howard Hinnant99968442011-11-29 18:15:50 +0000913__deferred_assoc_state<void, _Fp>::__deferred_assoc_state(_Fp&& __f)
914 : __func_(_VSTD::forward<_Fp>(__f))
Howard Hinnant54da3382010-08-30 18:46:21 +0000915{
916 this->__set_deferred();
917}
918
Howard Hinnant73d21a42010-09-04 23:28:19 +0000919#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21 +0000920
Howard Hinnant99968442011-11-29 18:15:50 +0000921template <class _Fp>
Howard Hinnant54da3382010-08-30 18:46:21 +0000922void
Howard Hinnant99968442011-11-29 18:15:50 +0000923__deferred_assoc_state<void, _Fp>::__execute()
Howard Hinnant54da3382010-08-30 18:46:21 +0000924{
925#ifndef _LIBCPP_NO_EXCEPTIONS
926 try
927 {
928#endif // _LIBCPP_NO_EXCEPTIONS
929 __func_();
930 this->set_value();
931#ifndef _LIBCPP_NO_EXCEPTIONS
932 }
933 catch (...)
934 {
935 this->set_exception(current_exception());
936 }
937#endif // _LIBCPP_NO_EXCEPTIONS
938}
939
Howard Hinnant99968442011-11-29 18:15:50 +0000940template <class _Rp, class _Fp>
Howard Hinnant57cff292011-05-19 15:05:04 +0000941class __async_assoc_state
Howard Hinnant99968442011-11-29 18:15:50 +0000942 : public __assoc_state<_Rp>
Howard Hinnant57cff292011-05-19 15:05:04 +0000943{
Howard Hinnant99968442011-11-29 18:15:50 +0000944 typedef __assoc_state<_Rp> base;
Howard Hinnant57cff292011-05-19 15:05:04 +0000945
Howard Hinnant99968442011-11-29 18:15:50 +0000946 _Fp __func_;
Howard Hinnant57cff292011-05-19 15:05:04 +0000947
Howard Hinnant1694d232011-05-28 14:41:13 +0000948 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant57cff292011-05-19 15:05:04 +0000949public:
950#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000951 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000952 explicit __async_assoc_state(_Fp&& __f);
Howard Hinnant57cff292011-05-19 15:05:04 +0000953#endif
954
955 virtual void __execute();
956};
957
958#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
959
Howard Hinnant99968442011-11-29 18:15:50 +0000960template <class _Rp, class _Fp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000961inline
Howard Hinnant99968442011-11-29 18:15:50 +0000962__async_assoc_state<_Rp, _Fp>::__async_assoc_state(_Fp&& __f)
963 : __func_(_VSTD::forward<_Fp>(__f))
Howard Hinnant57cff292011-05-19 15:05:04 +0000964{
965}
966
967#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
968
Howard Hinnant99968442011-11-29 18:15:50 +0000969template <class _Rp, class _Fp>
Howard Hinnant57cff292011-05-19 15:05:04 +0000970void
Howard Hinnant99968442011-11-29 18:15:50 +0000971__async_assoc_state<_Rp, _Fp>::__execute()
Howard Hinnant57cff292011-05-19 15:05:04 +0000972{
973#ifndef _LIBCPP_NO_EXCEPTIONS
974 try
975 {
976#endif // _LIBCPP_NO_EXCEPTIONS
977 this->set_value(__func_());
978#ifndef _LIBCPP_NO_EXCEPTIONS
979 }
980 catch (...)
981 {
982 this->set_exception(current_exception());
983 }
984#endif // _LIBCPP_NO_EXCEPTIONS
985}
986
Howard Hinnant99968442011-11-29 18:15:50 +0000987template <class _Rp, class _Fp>
Howard Hinnant57cff292011-05-19 15:05:04 +0000988void
Howard Hinnant99968442011-11-29 18:15:50 +0000989__async_assoc_state<_Rp, _Fp>::__on_zero_shared() _NOEXCEPT
Howard Hinnant57cff292011-05-19 15:05:04 +0000990{
991 this->wait();
992 base::__on_zero_shared();
993}
994
Howard Hinnant99968442011-11-29 18:15:50 +0000995template <class _Fp>
996class __async_assoc_state<void, _Fp>
Howard Hinnant57cff292011-05-19 15:05:04 +0000997 : public __assoc_sub_state
998{
999 typedef __assoc_sub_state base;
1000
Howard Hinnant99968442011-11-29 18:15:50 +00001001 _Fp __func_;
Howard Hinnant57cff292011-05-19 15:05:04 +00001002
Howard Hinnant1694d232011-05-28 14:41:13 +00001003 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant57cff292011-05-19 15:05:04 +00001004public:
1005#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001006 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001007 explicit __async_assoc_state(_Fp&& __f);
Howard Hinnant57cff292011-05-19 15:05:04 +00001008#endif
1009
1010 virtual void __execute();
1011};
1012
1013#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1014
Howard Hinnant99968442011-11-29 18:15:50 +00001015template <class _Fp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001016inline
Howard Hinnant99968442011-11-29 18:15:50 +00001017__async_assoc_state<void, _Fp>::__async_assoc_state(_Fp&& __f)
1018 : __func_(_VSTD::forward<_Fp>(__f))
Howard Hinnant57cff292011-05-19 15:05:04 +00001019{
1020}
1021
1022#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1023
Howard Hinnant99968442011-11-29 18:15:50 +00001024template <class _Fp>
Howard Hinnant57cff292011-05-19 15:05:04 +00001025void
Howard Hinnant99968442011-11-29 18:15:50 +00001026__async_assoc_state<void, _Fp>::__execute()
Howard Hinnant57cff292011-05-19 15:05:04 +00001027{
1028#ifndef _LIBCPP_NO_EXCEPTIONS
1029 try
1030 {
1031#endif // _LIBCPP_NO_EXCEPTIONS
1032 __func_();
1033 this->set_value();
1034#ifndef _LIBCPP_NO_EXCEPTIONS
1035 }
1036 catch (...)
1037 {
1038 this->set_exception(current_exception());
1039 }
1040#endif // _LIBCPP_NO_EXCEPTIONS
1041}
1042
Howard Hinnant99968442011-11-29 18:15:50 +00001043template <class _Fp>
Howard Hinnant57cff292011-05-19 15:05:04 +00001044void
Howard Hinnant99968442011-11-29 18:15:50 +00001045__async_assoc_state<void, _Fp>::__on_zero_shared() _NOEXCEPT
Howard Hinnant57cff292011-05-19 15:05:04 +00001046{
1047 this->wait();
1048 base::__on_zero_shared();
1049}
1050
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001051template <class _Rp> class _LIBCPP_TYPE_VIS_ONLY promise;
1052template <class _Rp> class _LIBCPP_TYPE_VIS_ONLY shared_future;
Howard Hinnant47499b12010-08-27 20:10:19 +00001053
1054// future
1055
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001056template <class _Rp> class _LIBCPP_TYPE_VIS_ONLY future;
Howard Hinnant54da3382010-08-30 18:46:21 +00001057
Howard Hinnant99968442011-11-29 18:15:50 +00001058template <class _Rp, class _Fp>
1059future<_Rp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00001060#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00001061__make_deferred_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001062#else
Howard Hinnant99968442011-11-29 18:15:50 +00001063__make_deferred_assoc_state(_Fp __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001064#endif
1065
Howard Hinnant99968442011-11-29 18:15:50 +00001066template <class _Rp, class _Fp>
1067future<_Rp>
Howard Hinnant57cff292011-05-19 15:05:04 +00001068#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00001069__make_async_assoc_state(_Fp&& __f);
Howard Hinnant57cff292011-05-19 15:05:04 +00001070#else
Howard Hinnant99968442011-11-29 18:15:50 +00001071__make_async_assoc_state(_Fp __f);
Howard Hinnant57cff292011-05-19 15:05:04 +00001072#endif
1073
Howard Hinnant99968442011-11-29 18:15:50 +00001074template <class _Rp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001075class _LIBCPP_TYPE_VIS_ONLY future
Howard Hinnant47499b12010-08-27 20:10:19 +00001076{
Howard Hinnant99968442011-11-29 18:15:50 +00001077 __assoc_state<_Rp>* __state_;
Howard Hinnant47499b12010-08-27 20:10:19 +00001078
Howard Hinnant99968442011-11-29 18:15:50 +00001079 explicit future(__assoc_state<_Rp>* __state);
Howard Hinnant47499b12010-08-27 20:10:19 +00001080
1081 template <class> friend class promise;
Howard Hinnant99be8232010-09-03 18:39:25 +00001082 template <class> friend class shared_future;
Howard Hinnant54da3382010-08-30 18:46:21 +00001083
Howard Hinnant73d21a42010-09-04 23:28:19 +00001084#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00001085 template <class _R1, class _Fp>
1086 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1087 template <class _R1, class _Fp>
1088 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001089#else
Howard Hinnant99968442011-11-29 18:15:50 +00001090 template <class _R1, class _Fp>
1091 friend future<_R1> __make_deferred_assoc_state(_Fp __f);
1092 template <class _R1, class _Fp>
1093 friend future<_R1> __make_async_assoc_state(_Fp __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001094#endif
1095
Howard Hinnant47499b12010-08-27 20:10:19 +00001096public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001097 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001098 future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001099#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001100 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001101 future(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +00001102 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1103 future(const future&) = delete;
1104 future& operator=(const future&) = delete;
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001105 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001106 future& operator=(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +00001107 {
1108 future(std::move(__rhs)).swap(*this);
1109 return *this;
1110 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001111#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001112private:
1113 future(const future&);
1114 future& operator=(const future&);
1115public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001116#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001117 ~future();
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001118 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001119 shared_future<_Rp> share();
Howard Hinnant47499b12010-08-27 20:10:19 +00001120
1121 // retrieving the value
Howard Hinnant99968442011-11-29 18:15:50 +00001122 _Rp get();
Howard Hinnant47499b12010-08-27 20:10:19 +00001123
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001124 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001125 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:19 +00001126
1127 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001128 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001129 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant47499b12010-08-27 20:10:19 +00001130
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001131 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001132 void wait() const {__state_->wait();}
1133 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001134 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001135 future_status
1136 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1137 {return __state_->wait_for(__rel_time);}
1138 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001139 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001140 future_status
1141 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1142 {return __state_->wait_until(__abs_time);}
1143};
1144
Howard Hinnant99968442011-11-29 18:15:50 +00001145template <class _Rp>
1146future<_Rp>::future(__assoc_state<_Rp>* __state)
Howard Hinnant47499b12010-08-27 20:10:19 +00001147 : __state_(__state)
1148{
1149 if (__state_->__has_future_attached())
Eric Fiselier423ca202015-10-02 21:25:15 +00001150 __throw_future_error(future_errc::future_already_retrieved);
Howard Hinnant47499b12010-08-27 20:10:19 +00001151 __state_->__add_shared();
Howard Hinnant54da3382010-08-30 18:46:21 +00001152 __state_->__set_future_attached();
Howard Hinnant47499b12010-08-27 20:10:19 +00001153}
1154
Howard Hinnant54da3382010-08-30 18:46:21 +00001155struct __release_shared_count
1156{
1157 void operator()(__shared_count* p) {p->__release_shared();}
1158};
1159
Howard Hinnant99968442011-11-29 18:15:50 +00001160template <class _Rp>
1161future<_Rp>::~future()
Howard Hinnant47499b12010-08-27 20:10:19 +00001162{
1163 if (__state_)
1164 __state_->__release_shared();
1165}
1166
Howard Hinnant99968442011-11-29 18:15:50 +00001167template <class _Rp>
1168_Rp
1169future<_Rp>::get()
Howard Hinnant47499b12010-08-27 20:10:19 +00001170{
Howard Hinnant54da3382010-08-30 18:46:21 +00001171 unique_ptr<__shared_count, __release_shared_count> __(__state_);
Howard Hinnant99968442011-11-29 18:15:50 +00001172 __assoc_state<_Rp>* __s = __state_;
Howard Hinnant47499b12010-08-27 20:10:19 +00001173 __state_ = nullptr;
1174 return __s->move();
1175}
1176
Howard Hinnant99968442011-11-29 18:15:50 +00001177template <class _Rp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001178class _LIBCPP_TYPE_VIS_ONLY future<_Rp&>
Howard Hinnant47499b12010-08-27 20:10:19 +00001179{
Howard Hinnant99968442011-11-29 18:15:50 +00001180 __assoc_state<_Rp&>* __state_;
Howard Hinnant47499b12010-08-27 20:10:19 +00001181
Howard Hinnant99968442011-11-29 18:15:50 +00001182 explicit future(__assoc_state<_Rp&>* __state);
Howard Hinnant47499b12010-08-27 20:10:19 +00001183
1184 template <class> friend class promise;
Howard Hinnant99be8232010-09-03 18:39:25 +00001185 template <class> friend class shared_future;
Howard Hinnant54da3382010-08-30 18:46:21 +00001186
Howard Hinnant73d21a42010-09-04 23:28:19 +00001187#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00001188 template <class _R1, class _Fp>
1189 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1190 template <class _R1, class _Fp>
1191 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001192#else
Howard Hinnant99968442011-11-29 18:15:50 +00001193 template <class _R1, class _Fp>
1194 friend future<_R1> __make_deferred_assoc_state(_Fp __f);
1195 template <class _R1, class _Fp>
1196 friend future<_R1> __make_async_assoc_state(_Fp __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001197#endif
1198
Howard Hinnant47499b12010-08-27 20:10:19 +00001199public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001200 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001201 future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001202#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001203 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001204 future(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +00001205 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1206 future(const future&) = delete;
1207 future& operator=(const future&) = delete;
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001208 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001209 future& operator=(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +00001210 {
1211 future(std::move(__rhs)).swap(*this);
1212 return *this;
1213 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001214#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001215private:
1216 future(const future&);
1217 future& operator=(const future&);
1218public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001219#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001220 ~future();
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001221 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001222 shared_future<_Rp&> share();
Howard Hinnant47499b12010-08-27 20:10:19 +00001223
1224 // retrieving the value
Howard Hinnant99968442011-11-29 18:15:50 +00001225 _Rp& get();
Howard Hinnant47499b12010-08-27 20:10:19 +00001226
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001227 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001228 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:19 +00001229
1230 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001231 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001232 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant47499b12010-08-27 20:10:19 +00001233
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001234 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001235 void wait() const {__state_->wait();}
1236 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001237 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001238 future_status
1239 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1240 {return __state_->wait_for(__rel_time);}
1241 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001242 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001243 future_status
1244 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1245 {return __state_->wait_until(__abs_time);}
1246};
1247
Howard Hinnant99968442011-11-29 18:15:50 +00001248template <class _Rp>
1249future<_Rp&>::future(__assoc_state<_Rp&>* __state)
Howard Hinnant47499b12010-08-27 20:10:19 +00001250 : __state_(__state)
1251{
1252 if (__state_->__has_future_attached())
Eric Fiselier423ca202015-10-02 21:25:15 +00001253 __throw_future_error(future_errc::future_already_retrieved);
Howard Hinnant47499b12010-08-27 20:10:19 +00001254 __state_->__add_shared();
Howard Hinnant54da3382010-08-30 18:46:21 +00001255 __state_->__set_future_attached();
Howard Hinnant47499b12010-08-27 20:10:19 +00001256}
1257
Howard Hinnant99968442011-11-29 18:15:50 +00001258template <class _Rp>
1259future<_Rp&>::~future()
Howard Hinnant47499b12010-08-27 20:10:19 +00001260{
1261 if (__state_)
1262 __state_->__release_shared();
1263}
1264
Howard Hinnant99968442011-11-29 18:15:50 +00001265template <class _Rp>
1266_Rp&
1267future<_Rp&>::get()
Howard Hinnant47499b12010-08-27 20:10:19 +00001268{
Howard Hinnant54da3382010-08-30 18:46:21 +00001269 unique_ptr<__shared_count, __release_shared_count> __(__state_);
Howard Hinnant99968442011-11-29 18:15:50 +00001270 __assoc_state<_Rp&>* __s = __state_;
Howard Hinnant47499b12010-08-27 20:10:19 +00001271 __state_ = nullptr;
1272 return __s->copy();
1273}
1274
1275template <>
Howard Hinnant83eade62013-03-06 23:30:19 +00001276class _LIBCPP_TYPE_VIS future<void>
Howard Hinnant47499b12010-08-27 20:10:19 +00001277{
1278 __assoc_sub_state* __state_;
1279
1280 explicit future(__assoc_sub_state* __state);
1281
1282 template <class> friend class promise;
Howard Hinnant99be8232010-09-03 18:39:25 +00001283 template <class> friend class shared_future;
Howard Hinnant54da3382010-08-30 18:46:21 +00001284
Howard Hinnant73d21a42010-09-04 23:28:19 +00001285#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00001286 template <class _R1, class _Fp>
1287 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1288 template <class _R1, class _Fp>
1289 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001290#else
Howard Hinnant99968442011-11-29 18:15:50 +00001291 template <class _R1, class _Fp>
1292 friend future<_R1> __make_deferred_assoc_state(_Fp __f);
1293 template <class _R1, class _Fp>
1294 friend future<_R1> __make_async_assoc_state(_Fp __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001295#endif
1296
Howard Hinnant47499b12010-08-27 20:10:19 +00001297public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001298 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001299 future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001300#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001301 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001302 future(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +00001303 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1304 future(const future&) = delete;
1305 future& operator=(const future&) = delete;
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001306 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001307 future& operator=(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +00001308 {
1309 future(std::move(__rhs)).swap(*this);
1310 return *this;
1311 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001312#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001313private:
1314 future(const future&);
1315 future& operator=(const future&);
1316public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001317#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001318 ~future();
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001319 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7de47902010-11-30 20:23:32 +00001320 shared_future<void> share();
Howard Hinnant47499b12010-08-27 20:10:19 +00001321
1322 // retrieving the value
1323 void get();
1324
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001325 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001326 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:19 +00001327
1328 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001329 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001330 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant47499b12010-08-27 20:10:19 +00001331
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001332 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001333 void wait() const {__state_->wait();}
1334 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001335 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001336 future_status
1337 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1338 {return __state_->wait_for(__rel_time);}
1339 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001340 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001341 future_status
1342 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1343 {return __state_->wait_until(__abs_time);}
1344};
1345
Howard Hinnant99968442011-11-29 18:15:50 +00001346template <class _Rp>
Howard Hinnant99be8232010-09-03 18:39:25 +00001347inline _LIBCPP_INLINE_VISIBILITY
1348void
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001349swap(future<_Rp>& __x, future<_Rp>& __y) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:25 +00001350{
1351 __x.swap(__y);
1352}
1353
Howard Hinnant47499b12010-08-27 20:10:19 +00001354// promise<R>
1355
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00001356template <class _Callable> class packaged_task;
Howard Hinnant54da3382010-08-30 18:46:21 +00001357
Howard Hinnant99968442011-11-29 18:15:50 +00001358template <class _Rp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001359class _LIBCPP_TYPE_VIS_ONLY promise
Howard Hinnant47499b12010-08-27 20:10:19 +00001360{
Howard Hinnant99968442011-11-29 18:15:50 +00001361 __assoc_state<_Rp>* __state_;
Howard Hinnant54da3382010-08-30 18:46:21 +00001362
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001363 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001364 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant54da3382010-08-30 18:46:21 +00001365
1366 template <class> friend class packaged_task;
Howard Hinnant47499b12010-08-27 20:10:19 +00001367public:
1368 promise();
1369 template <class _Alloc>
1370 promise(allocator_arg_t, const _Alloc& __a);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001371#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001372 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001373 promise(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +00001374 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1375 promise(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +00001376#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001377private:
1378 promise(const promise& __rhs);
1379public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001380#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001381 ~promise();
1382
1383 // assignment
Howard Hinnant73d21a42010-09-04 23:28:19 +00001384#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001385 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001386 promise& operator=(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +00001387 {
1388 promise(std::move(__rhs)).swap(*this);
1389 return *this;
1390 }
1391 promise& operator=(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +00001392#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001393private:
1394 promise& operator=(const promise& __rhs);
1395public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001396#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001397 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001398 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:19 +00001399
1400 // retrieving the result
Howard Hinnant99968442011-11-29 18:15:50 +00001401 future<_Rp> get_future();
Howard Hinnant47499b12010-08-27 20:10:19 +00001402
1403 // setting the result
Howard Hinnant99968442011-11-29 18:15:50 +00001404 void set_value(const _Rp& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001405#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00001406 void set_value(_Rp&& __r);
Howard Hinnant47499b12010-08-27 20:10:19 +00001407#endif
1408 void set_exception(exception_ptr __p);
1409
1410 // setting the result with deferred notification
Howard Hinnant99968442011-11-29 18:15:50 +00001411 void set_value_at_thread_exit(const _Rp& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001412#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00001413 void set_value_at_thread_exit(_Rp&& __r);
Howard Hinnant47499b12010-08-27 20:10:19 +00001414#endif
1415 void set_exception_at_thread_exit(exception_ptr __p);
1416};
1417
Howard Hinnant99968442011-11-29 18:15:50 +00001418template <class _Rp>
1419promise<_Rp>::promise()
1420 : __state_(new __assoc_state<_Rp>)
Howard Hinnant47499b12010-08-27 20:10:19 +00001421{
1422}
1423
Howard Hinnant99968442011-11-29 18:15:50 +00001424template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +00001425template <class _Alloc>
Howard Hinnant99968442011-11-29 18:15:50 +00001426promise<_Rp>::promise(allocator_arg_t, const _Alloc& __a0)
Howard Hinnant47499b12010-08-27 20:10:19 +00001427{
Eric Fiselier4d2413c2014-10-23 06:24:45 +00001428 typedef __assoc_state_alloc<_Rp, _Alloc> _State;
1429 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
Howard Hinnant47499b12010-08-27 20:10:19 +00001430 typedef __allocator_destructor<_A2> _D2;
1431 _A2 __a(__a0);
Eric Fiselier4d2413c2014-10-23 06:24:45 +00001432 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1433 ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0);
1434 __state_ = _VSTD::addressof(*__hold.release());
Howard Hinnant47499b12010-08-27 20:10:19 +00001435}
1436
Howard Hinnant99968442011-11-29 18:15:50 +00001437template <class _Rp>
1438promise<_Rp>::~promise()
Howard Hinnant47499b12010-08-27 20:10:19 +00001439{
1440 if (__state_)
1441 {
1442 if (!__state_->__has_value() && __state_->use_count() > 1)
1443 __state_->set_exception(make_exception_ptr(
1444 future_error(make_error_code(future_errc::broken_promise))
1445 ));
1446 __state_->__release_shared();
1447 }
1448}
1449
Howard Hinnant99968442011-11-29 18:15:50 +00001450template <class _Rp>
1451future<_Rp>
1452promise<_Rp>::get_future()
Howard Hinnant47499b12010-08-27 20:10:19 +00001453{
1454 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00001455 __throw_future_error(future_errc::no_state);
Howard Hinnant99968442011-11-29 18:15:50 +00001456 return future<_Rp>(__state_);
Howard Hinnant47499b12010-08-27 20:10:19 +00001457}
1458
Howard Hinnant99968442011-11-29 18:15:50 +00001459template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +00001460void
Howard Hinnant99968442011-11-29 18:15:50 +00001461promise<_Rp>::set_value(const _Rp& __r)
Howard Hinnant47499b12010-08-27 20:10:19 +00001462{
1463 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00001464 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:19 +00001465 __state_->set_value(__r);
1466}
1467
Howard Hinnant73d21a42010-09-04 23:28:19 +00001468#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001469
Howard Hinnant99968442011-11-29 18:15:50 +00001470template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +00001471void
Howard Hinnant99968442011-11-29 18:15:50 +00001472promise<_Rp>::set_value(_Rp&& __r)
Howard Hinnant47499b12010-08-27 20:10:19 +00001473{
1474 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00001475 __throw_future_error(future_errc::no_state);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001476 __state_->set_value(_VSTD::move(__r));
Howard Hinnant47499b12010-08-27 20:10:19 +00001477}
1478
Howard Hinnant73d21a42010-09-04 23:28:19 +00001479#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001480
Howard Hinnant99968442011-11-29 18:15:50 +00001481template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +00001482void
Howard Hinnant99968442011-11-29 18:15:50 +00001483promise<_Rp>::set_exception(exception_ptr __p)
Howard Hinnant47499b12010-08-27 20:10:19 +00001484{
Marshall Cloweaba7bb2016-05-16 16:55:32 +00001485 _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception: received nullptr" );
Howard Hinnant47499b12010-08-27 20:10:19 +00001486 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00001487 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:19 +00001488 __state_->set_exception(__p);
1489}
1490
Howard Hinnant99968442011-11-29 18:15:50 +00001491template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +00001492void
Howard Hinnant99968442011-11-29 18:15:50 +00001493promise<_Rp>::set_value_at_thread_exit(const _Rp& __r)
Howard Hinnant47499b12010-08-27 20:10:19 +00001494{
1495 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00001496 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:19 +00001497 __state_->set_value_at_thread_exit(__r);
1498}
1499
Howard Hinnant73d21a42010-09-04 23:28:19 +00001500#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001501
Howard Hinnant99968442011-11-29 18:15:50 +00001502template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +00001503void
Howard Hinnant99968442011-11-29 18:15:50 +00001504promise<_Rp>::set_value_at_thread_exit(_Rp&& __r)
Howard Hinnant47499b12010-08-27 20:10:19 +00001505{
1506 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00001507 __throw_future_error(future_errc::no_state);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001508 __state_->set_value_at_thread_exit(_VSTD::move(__r));
Howard Hinnant47499b12010-08-27 20:10:19 +00001509}
1510
Howard Hinnant73d21a42010-09-04 23:28:19 +00001511#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001512
Howard Hinnant99968442011-11-29 18:15:50 +00001513template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +00001514void
Howard Hinnant99968442011-11-29 18:15:50 +00001515promise<_Rp>::set_exception_at_thread_exit(exception_ptr __p)
Howard Hinnant47499b12010-08-27 20:10:19 +00001516{
1517 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00001518 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:19 +00001519 __state_->set_exception_at_thread_exit(__p);
1520}
1521
1522// promise<R&>
1523
Howard Hinnant99968442011-11-29 18:15:50 +00001524template <class _Rp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001525class _LIBCPP_TYPE_VIS_ONLY promise<_Rp&>
Howard Hinnant47499b12010-08-27 20:10:19 +00001526{
Howard Hinnant99968442011-11-29 18:15:50 +00001527 __assoc_state<_Rp&>* __state_;
Howard Hinnant54da3382010-08-30 18:46:21 +00001528
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001529 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001530 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant54da3382010-08-30 18:46:21 +00001531
1532 template <class> friend class packaged_task;
1533
Howard Hinnant47499b12010-08-27 20:10:19 +00001534public:
1535 promise();
1536 template <class _Allocator>
1537 promise(allocator_arg_t, const _Allocator& __a);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001538#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001539 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001540 promise(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +00001541 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1542 promise(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +00001543#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001544private:
1545 promise(const promise& __rhs);
1546public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001547#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001548 ~promise();
1549
1550 // assignment
Howard Hinnant73d21a42010-09-04 23:28:19 +00001551#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001552 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001553 promise& operator=(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +00001554 {
1555 promise(std::move(__rhs)).swap(*this);
1556 return *this;
1557 }
1558 promise& operator=(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +00001559#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001560private:
1561 promise& operator=(const promise& __rhs);
1562public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001563#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001564 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001565 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:19 +00001566
1567 // retrieving the result
Howard Hinnant99968442011-11-29 18:15:50 +00001568 future<_Rp&> get_future();
Howard Hinnant47499b12010-08-27 20:10:19 +00001569
1570 // setting the result
Howard Hinnant99968442011-11-29 18:15:50 +00001571 void set_value(_Rp& __r);
Howard Hinnant47499b12010-08-27 20:10:19 +00001572 void set_exception(exception_ptr __p);
1573
1574 // setting the result with deferred notification
Howard Hinnant99968442011-11-29 18:15:50 +00001575 void set_value_at_thread_exit(_Rp&);
Howard Hinnant47499b12010-08-27 20:10:19 +00001576 void set_exception_at_thread_exit(exception_ptr __p);
1577};
1578
Howard Hinnant99968442011-11-29 18:15:50 +00001579template <class _Rp>
1580promise<_Rp&>::promise()
1581 : __state_(new __assoc_state<_Rp&>)
Howard Hinnant47499b12010-08-27 20:10:19 +00001582{
1583}
1584
Howard Hinnant99968442011-11-29 18:15:50 +00001585template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +00001586template <class _Alloc>
Howard Hinnant99968442011-11-29 18:15:50 +00001587promise<_Rp&>::promise(allocator_arg_t, const _Alloc& __a0)
Howard Hinnant47499b12010-08-27 20:10:19 +00001588{
Eric Fiselier4d2413c2014-10-23 06:24:45 +00001589 typedef __assoc_state_alloc<_Rp&, _Alloc> _State;
1590 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
Howard Hinnant47499b12010-08-27 20:10:19 +00001591 typedef __allocator_destructor<_A2> _D2;
1592 _A2 __a(__a0);
Eric Fiselier4d2413c2014-10-23 06:24:45 +00001593 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1594 ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0);
1595 __state_ = _VSTD::addressof(*__hold.release());
Howard Hinnant47499b12010-08-27 20:10:19 +00001596}
1597
Howard Hinnant99968442011-11-29 18:15:50 +00001598template <class _Rp>
1599promise<_Rp&>::~promise()
Howard Hinnant47499b12010-08-27 20:10:19 +00001600{
1601 if (__state_)
1602 {
1603 if (!__state_->__has_value() && __state_->use_count() > 1)
1604 __state_->set_exception(make_exception_ptr(
1605 future_error(make_error_code(future_errc::broken_promise))
1606 ));
1607 __state_->__release_shared();
1608 }
1609}
1610
Howard Hinnant99968442011-11-29 18:15:50 +00001611template <class _Rp>
1612future<_Rp&>
1613promise<_Rp&>::get_future()
Howard Hinnant47499b12010-08-27 20:10:19 +00001614{
1615 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00001616 __throw_future_error(future_errc::no_state);
Howard Hinnant99968442011-11-29 18:15:50 +00001617 return future<_Rp&>(__state_);
Howard Hinnant47499b12010-08-27 20:10:19 +00001618}
1619
Howard Hinnant99968442011-11-29 18:15:50 +00001620template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +00001621void
Howard Hinnant99968442011-11-29 18:15:50 +00001622promise<_Rp&>::set_value(_Rp& __r)
Howard Hinnant47499b12010-08-27 20:10:19 +00001623{
1624 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00001625 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:19 +00001626 __state_->set_value(__r);
1627}
1628
Howard Hinnant99968442011-11-29 18:15:50 +00001629template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +00001630void
Howard Hinnant99968442011-11-29 18:15:50 +00001631promise<_Rp&>::set_exception(exception_ptr __p)
Howard Hinnant47499b12010-08-27 20:10:19 +00001632{
Marshall Cloweaba7bb2016-05-16 16:55:32 +00001633 _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception: received nullptr" );
Howard Hinnant47499b12010-08-27 20:10:19 +00001634 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00001635 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:19 +00001636 __state_->set_exception(__p);
1637}
1638
Howard Hinnant99968442011-11-29 18:15:50 +00001639template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +00001640void
Howard Hinnant99968442011-11-29 18:15:50 +00001641promise<_Rp&>::set_value_at_thread_exit(_Rp& __r)
Howard Hinnant47499b12010-08-27 20:10:19 +00001642{
1643 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00001644 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:19 +00001645 __state_->set_value_at_thread_exit(__r);
1646}
1647
Howard Hinnant99968442011-11-29 18:15:50 +00001648template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +00001649void
Howard Hinnant99968442011-11-29 18:15:50 +00001650promise<_Rp&>::set_exception_at_thread_exit(exception_ptr __p)
Howard Hinnant47499b12010-08-27 20:10:19 +00001651{
1652 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00001653 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:19 +00001654 __state_->set_exception_at_thread_exit(__p);
1655}
1656
1657// promise<void>
1658
1659template <>
Howard Hinnant83eade62013-03-06 23:30:19 +00001660class _LIBCPP_TYPE_VIS promise<void>
Howard Hinnant47499b12010-08-27 20:10:19 +00001661{
1662 __assoc_sub_state* __state_;
Howard Hinnant54da3382010-08-30 18:46:21 +00001663
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001664 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001665 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant54da3382010-08-30 18:46:21 +00001666
1667 template <class> friend class packaged_task;
1668
Howard Hinnant47499b12010-08-27 20:10:19 +00001669public:
1670 promise();
1671 template <class _Allocator>
1672 promise(allocator_arg_t, const _Allocator& __a);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001673#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001674 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001675 promise(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +00001676 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1677 promise(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +00001678#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001679private:
1680 promise(const promise& __rhs);
1681public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001682#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001683 ~promise();
1684
1685 // assignment
Howard Hinnant73d21a42010-09-04 23:28:19 +00001686#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001687 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001688 promise& operator=(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +00001689 {
1690 promise(std::move(__rhs)).swap(*this);
1691 return *this;
1692 }
1693 promise& operator=(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +00001694#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001695private:
1696 promise& operator=(const promise& __rhs);
1697public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001698#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001699 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001700 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:19 +00001701
1702 // retrieving the result
1703 future<void> get_future();
1704
1705 // setting the result
1706 void set_value();
1707 void set_exception(exception_ptr __p);
1708
1709 // setting the result with deferred notification
1710 void set_value_at_thread_exit();
1711 void set_exception_at_thread_exit(exception_ptr __p);
1712};
1713
1714template <class _Alloc>
1715promise<void>::promise(allocator_arg_t, const _Alloc& __a0)
1716{
Eric Fiselier4d2413c2014-10-23 06:24:45 +00001717 typedef __assoc_sub_state_alloc<_Alloc> _State;
1718 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
Howard Hinnant47499b12010-08-27 20:10:19 +00001719 typedef __allocator_destructor<_A2> _D2;
1720 _A2 __a(__a0);
Eric Fiselier4d2413c2014-10-23 06:24:45 +00001721 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1722 ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0);
1723 __state_ = _VSTD::addressof(*__hold.release());
Howard Hinnant47499b12010-08-27 20:10:19 +00001724}
1725
Howard Hinnant99968442011-11-29 18:15:50 +00001726template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +00001727inline _LIBCPP_INLINE_VISIBILITY
1728void
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001729swap(promise<_Rp>& __x, promise<_Rp>& __y) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +00001730{
1731 __x.swap(__y);
1732}
1733
Howard Hinnant99968442011-11-29 18:15:50 +00001734template <class _Rp, class _Alloc>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001735 struct _LIBCPP_TYPE_VIS_ONLY uses_allocator<promise<_Rp>, _Alloc>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001736 : public true_type {};
Howard Hinnant47499b12010-08-27 20:10:19 +00001737
Howard Hinnant54da3382010-08-30 18:46:21 +00001738#ifndef _LIBCPP_HAS_NO_VARIADICS
1739
1740// packaged_task
1741
1742template<class _Fp> class __packaged_task_base;
1743
Howard Hinnant99968442011-11-29 18:15:50 +00001744template<class _Rp, class ..._ArgTypes>
1745class __packaged_task_base<_Rp(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:21 +00001746{
1747 __packaged_task_base(const __packaged_task_base&);
1748 __packaged_task_base& operator=(const __packaged_task_base&);
1749public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001750 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001751 __packaged_task_base() {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001752 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001753 virtual ~__packaged_task_base() {}
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001754 virtual void __move_to(__packaged_task_base*) _NOEXCEPT = 0;
Howard Hinnant54da3382010-08-30 18:46:21 +00001755 virtual void destroy() = 0;
1756 virtual void destroy_deallocate() = 0;
Howard Hinnant99968442011-11-29 18:15:50 +00001757 virtual _Rp operator()(_ArgTypes&& ...) = 0;
Howard Hinnant54da3382010-08-30 18:46:21 +00001758};
1759
1760template<class _FD, class _Alloc, class _FB> class __packaged_task_func;
1761
Howard Hinnant99968442011-11-29 18:15:50 +00001762template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1763class __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>
1764 : public __packaged_task_base<_Rp(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:21 +00001765{
Howard Hinnant99968442011-11-29 18:15:50 +00001766 __compressed_pair<_Fp, _Alloc> __f_;
Howard Hinnant54da3382010-08-30 18:46:21 +00001767public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001768 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001769 explicit __packaged_task_func(const _Fp& __f) : __f_(__f) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001770 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001771 explicit __packaged_task_func(_Fp&& __f) : __f_(_VSTD::move(__f)) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001772 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001773 __packaged_task_func(const _Fp& __f, const _Alloc& __a)
Howard Hinnant54da3382010-08-30 18:46:21 +00001774 : __f_(__f, __a) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001775 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001776 __packaged_task_func(_Fp&& __f, const _Alloc& __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001777 : __f_(_VSTD::move(__f), __a) {}
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001778 virtual void __move_to(__packaged_task_base<_Rp(_ArgTypes...)>*) _NOEXCEPT;
Howard Hinnant54da3382010-08-30 18:46:21 +00001779 virtual void destroy();
1780 virtual void destroy_deallocate();
Howard Hinnant99968442011-11-29 18:15:50 +00001781 virtual _Rp operator()(_ArgTypes&& ... __args);
Howard Hinnant54da3382010-08-30 18:46:21 +00001782};
1783
Howard Hinnant99968442011-11-29 18:15:50 +00001784template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:21 +00001785void
Howard Hinnant99968442011-11-29 18:15:50 +00001786__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__move_to(
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001787 __packaged_task_base<_Rp(_ArgTypes...)>* __p) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:21 +00001788{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001789 ::new (__p) __packaged_task_func(_VSTD::move(__f_.first()), _VSTD::move(__f_.second()));
Howard Hinnant54da3382010-08-30 18:46:21 +00001790}
1791
Howard Hinnant99968442011-11-29 18:15:50 +00001792template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:21 +00001793void
Howard Hinnant99968442011-11-29 18:15:50 +00001794__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy()
Howard Hinnant54da3382010-08-30 18:46:21 +00001795{
Howard Hinnant99968442011-11-29 18:15:50 +00001796 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnant54da3382010-08-30 18:46:21 +00001797}
1798
Howard Hinnant99968442011-11-29 18:15:50 +00001799template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:21 +00001800void
Howard Hinnant99968442011-11-29 18:15:50 +00001801__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate()
Howard Hinnant54da3382010-08-30 18:46:21 +00001802{
Eric Fiselier4d2413c2014-10-23 06:24:45 +00001803 typedef typename __allocator_traits_rebind<_Alloc, __packaged_task_func>::type _Ap;
1804 typedef allocator_traits<_Ap> _ATraits;
1805 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Howard Hinnant99968442011-11-29 18:15:50 +00001806 _Ap __a(__f_.second());
1807 __f_.~__compressed_pair<_Fp, _Alloc>();
Eric Fiselier4d2413c2014-10-23 06:24:45 +00001808 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnant54da3382010-08-30 18:46:21 +00001809}
1810
Howard Hinnant99968442011-11-29 18:15:50 +00001811template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1812_Rp
1813__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
Howard Hinnant54da3382010-08-30 18:46:21 +00001814{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001815 return __invoke(__f_.first(), _VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnant54da3382010-08-30 18:46:21 +00001816}
1817
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00001818template <class _Callable> class __packaged_task_function;
Howard Hinnant54da3382010-08-30 18:46:21 +00001819
Howard Hinnant99968442011-11-29 18:15:50 +00001820template<class _Rp, class ..._ArgTypes>
1821class __packaged_task_function<_Rp(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:21 +00001822{
Howard Hinnant99968442011-11-29 18:15:50 +00001823 typedef __packaged_task_base<_Rp(_ArgTypes...)> __base;
Howard Hinnant78f0de22013-01-21 17:26:55 +00001824 typename aligned_storage<3*sizeof(void*)>::type __buf_;
Howard Hinnant54da3382010-08-30 18:46:21 +00001825 __base* __f_;
1826
1827public:
Howard Hinnant99968442011-11-29 18:15:50 +00001828 typedef _Rp result_type;
Howard Hinnant54da3382010-08-30 18:46:21 +00001829
1830 // construct/copy/destroy:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001831 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001832 __packaged_task_function() _NOEXCEPT : __f_(nullptr) {}
Howard Hinnant99968442011-11-29 18:15:50 +00001833 template<class _Fp>
1834 __packaged_task_function(_Fp&& __f);
1835 template<class _Fp, class _Alloc>
1836 __packaged_task_function(allocator_arg_t, const _Alloc& __a, _Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001837
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001838 __packaged_task_function(__packaged_task_function&&) _NOEXCEPT;
1839 __packaged_task_function& operator=(__packaged_task_function&&) _NOEXCEPT;
Howard Hinnant54da3382010-08-30 18:46:21 +00001840
1841 __packaged_task_function(const __packaged_task_function&) = delete;
1842 __packaged_task_function& operator=(const __packaged_task_function&) = delete;
1843
1844 ~__packaged_task_function();
1845
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001846 void swap(__packaged_task_function&) _NOEXCEPT;
Howard Hinnant54da3382010-08-30 18:46:21 +00001847
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001848 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001849 _Rp operator()(_ArgTypes...) const;
Howard Hinnant54da3382010-08-30 18:46:21 +00001850};
1851
Howard Hinnant99968442011-11-29 18:15:50 +00001852template<class _Rp, class ..._ArgTypes>
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001853__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:21 +00001854{
1855 if (__f.__f_ == nullptr)
1856 __f_ = nullptr;
1857 else if (__f.__f_ == (__base*)&__f.__buf_)
1858 {
1859 __f_ = (__base*)&__buf_;
1860 __f.__f_->__move_to(__f_);
1861 }
1862 else
1863 {
1864 __f_ = __f.__f_;
1865 __f.__f_ = nullptr;
1866 }
1867}
1868
Howard Hinnant99968442011-11-29 18:15:50 +00001869template<class _Rp, class ..._ArgTypes>
1870template <class _Fp>
1871__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(_Fp&& __f)
Howard Hinnant54da3382010-08-30 18:46:21 +00001872 : __f_(nullptr)
1873{
Marshall Clowf1264e72014-04-07 13:32:26 +00001874 typedef typename remove_reference<typename decay<_Fp>::type>::type _FR;
Howard Hinnant99968442011-11-29 18:15:50 +00001875 typedef __packaged_task_func<_FR, allocator<_FR>, _Rp(_ArgTypes...)> _FF;
Howard Hinnant54da3382010-08-30 18:46:21 +00001876 if (sizeof(_FF) <= sizeof(__buf_))
1877 {
1878 __f_ = (__base*)&__buf_;
Howard Hinnant99968442011-11-29 18:15:50 +00001879 ::new (__f_) _FF(_VSTD::forward<_Fp>(__f));
Howard Hinnant54da3382010-08-30 18:46:21 +00001880 }
1881 else
1882 {
Howard Hinnant99968442011-11-29 18:15:50 +00001883 typedef allocator<_FF> _Ap;
1884 _Ap __a;
1885 typedef __allocator_destructor<_Ap> _Dp;
1886 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1887 ::new (__hold.get()) _FF(_VSTD::forward<_Fp>(__f), allocator<_FR>(__a));
Howard Hinnant54da3382010-08-30 18:46:21 +00001888 __f_ = __hold.release();
1889 }
1890}
1891
Howard Hinnant99968442011-11-29 18:15:50 +00001892template<class _Rp, class ..._ArgTypes>
1893template <class _Fp, class _Alloc>
1894__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(
1895 allocator_arg_t, const _Alloc& __a0, _Fp&& __f)
Howard Hinnant54da3382010-08-30 18:46:21 +00001896 : __f_(nullptr)
1897{
Marshall Clowf1264e72014-04-07 13:32:26 +00001898 typedef typename remove_reference<typename decay<_Fp>::type>::type _FR;
Howard Hinnant99968442011-11-29 18:15:50 +00001899 typedef __packaged_task_func<_FR, _Alloc, _Rp(_ArgTypes...)> _FF;
Howard Hinnant54da3382010-08-30 18:46:21 +00001900 if (sizeof(_FF) <= sizeof(__buf_))
1901 {
1902 __f_ = (__base*)&__buf_;
Howard Hinnant99968442011-11-29 18:15:50 +00001903 ::new (__f_) _FF(_VSTD::forward<_Fp>(__f));
Howard Hinnant54da3382010-08-30 18:46:21 +00001904 }
1905 else
1906 {
Eric Fiselier4d2413c2014-10-23 06:24:45 +00001907 typedef typename __allocator_traits_rebind<_Alloc, _FF>::type _Ap;
Howard Hinnant99968442011-11-29 18:15:50 +00001908 _Ap __a(__a0);
1909 typedef __allocator_destructor<_Ap> _Dp;
1910 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Eric Fiselier4d2413c2014-10-23 06:24:45 +00001911 ::new (static_cast<void*>(_VSTD::addressof(*__hold.get())))
1912 _FF(_VSTD::forward<_Fp>(__f), _Alloc(__a));
1913 __f_ = _VSTD::addressof(*__hold.release());
Howard Hinnant54da3382010-08-30 18:46:21 +00001914 }
1915}
1916
Howard Hinnant99968442011-11-29 18:15:50 +00001917template<class _Rp, class ..._ArgTypes>
1918__packaged_task_function<_Rp(_ArgTypes...)>&
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001919__packaged_task_function<_Rp(_ArgTypes...)>::operator=(__packaged_task_function&& __f) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:21 +00001920{
1921 if (__f_ == (__base*)&__buf_)
1922 __f_->destroy();
1923 else if (__f_)
1924 __f_->destroy_deallocate();
1925 __f_ = nullptr;
1926 if (__f.__f_ == nullptr)
1927 __f_ = nullptr;
1928 else if (__f.__f_ == (__base*)&__f.__buf_)
1929 {
1930 __f_ = (__base*)&__buf_;
1931 __f.__f_->__move_to(__f_);
1932 }
1933 else
1934 {
1935 __f_ = __f.__f_;
1936 __f.__f_ = nullptr;
1937 }
Argyrios Kyrtzidis1dc6f7a2012-10-13 02:03:45 +00001938 return *this;
Howard Hinnant54da3382010-08-30 18:46:21 +00001939}
1940
Howard Hinnant99968442011-11-29 18:15:50 +00001941template<class _Rp, class ..._ArgTypes>
1942__packaged_task_function<_Rp(_ArgTypes...)>::~__packaged_task_function()
Howard Hinnant54da3382010-08-30 18:46:21 +00001943{
1944 if (__f_ == (__base*)&__buf_)
1945 __f_->destroy();
1946 else if (__f_)
1947 __f_->destroy_deallocate();
1948}
1949
Howard Hinnant99968442011-11-29 18:15:50 +00001950template<class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:21 +00001951void
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001952__packaged_task_function<_Rp(_ArgTypes...)>::swap(__packaged_task_function& __f) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:21 +00001953{
1954 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1955 {
1956 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1957 __base* __t = (__base*)&__tempbuf;
1958 __f_->__move_to(__t);
1959 __f_->destroy();
1960 __f_ = nullptr;
1961 __f.__f_->__move_to((__base*)&__buf_);
1962 __f.__f_->destroy();
1963 __f.__f_ = nullptr;
1964 __f_ = (__base*)&__buf_;
1965 __t->__move_to((__base*)&__f.__buf_);
1966 __t->destroy();
1967 __f.__f_ = (__base*)&__f.__buf_;
1968 }
1969 else if (__f_ == (__base*)&__buf_)
1970 {
1971 __f_->__move_to((__base*)&__f.__buf_);
1972 __f_->destroy();
1973 __f_ = __f.__f_;
1974 __f.__f_ = (__base*)&__f.__buf_;
1975 }
1976 else if (__f.__f_ == (__base*)&__f.__buf_)
1977 {
1978 __f.__f_->__move_to((__base*)&__buf_);
1979 __f.__f_->destroy();
1980 __f.__f_ = __f_;
1981 __f_ = (__base*)&__buf_;
1982 }
1983 else
Howard Hinnant0949eed2011-06-30 21:18:19 +00001984 _VSTD::swap(__f_, __f.__f_);
Howard Hinnant54da3382010-08-30 18:46:21 +00001985}
1986
Howard Hinnant99968442011-11-29 18:15:50 +00001987template<class _Rp, class ..._ArgTypes>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001988inline
Howard Hinnant99968442011-11-29 18:15:50 +00001989_Rp
1990__packaged_task_function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
Howard Hinnant54da3382010-08-30 18:46:21 +00001991{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001992 return (*__f_)(_VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnant54da3382010-08-30 18:46:21 +00001993}
1994
Howard Hinnant99968442011-11-29 18:15:50 +00001995template<class _Rp, class ..._ArgTypes>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001996class _LIBCPP_TYPE_VIS_ONLY packaged_task<_Rp(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:21 +00001997{
1998public:
Howard Hinnant99968442011-11-29 18:15:50 +00001999 typedef _Rp result_type;
Howard Hinnant54da3382010-08-30 18:46:21 +00002000
2001private:
2002 __packaged_task_function<result_type(_ArgTypes...)> __f_;
2003 promise<result_type> __p_;
2004
2005public:
2006 // construction and destruction
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002007 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002008 packaged_task() _NOEXCEPT : __p_(nullptr) {}
Marshall Clow5f2d5b92013-10-12 22:49:17 +00002009 template <class _Fp,
2010 class = typename enable_if
2011 <
2012 !is_same<
2013 typename decay<_Fp>::type,
2014 packaged_task
2015 >::value
2016 >::type
2017 >
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002018 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00002019 explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
Marshall Clow5f2d5b92013-10-12 22:49:17 +00002020 template <class _Fp, class _Allocator,
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:26 +00002029 _LIBCPP_INLINE_VISIBILITY
Marshall Clow07546f32015-06-30 14:16:49 +00002030 packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
Howard Hinnant99968442011-11-29 18:15:50 +00002031 : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)),
Howard Hinnant54da3382010-08-30 18:46:21 +00002032 __p_(allocator_arg, __a) {}
2033 // ~packaged_task() = default;
2034
2035 // no copy
Howard Hinnant8131a012012-07-21 19:34:12 +00002036 packaged_task(const packaged_task&) = delete;
2037 packaged_task& operator=(const packaged_task&) = delete;
Howard Hinnant54da3382010-08-30 18:46:21 +00002038
2039 // move support
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002040 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002041 packaged_task(packaged_task&& __other) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002042 : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002043 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002044 packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:21 +00002045 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002046 __f_ = _VSTD::move(__other.__f_);
2047 __p_ = _VSTD::move(__other.__p_);
Howard Hinnant54da3382010-08-30 18:46:21 +00002048 return *this;
2049 }
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002050 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002051 void swap(packaged_task& __other) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:21 +00002052 {
2053 __f_.swap(__other.__f_);
2054 __p_.swap(__other.__p_);
2055 }
2056
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002057 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002058 bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
Howard Hinnant54da3382010-08-30 18:46:21 +00002059
2060 // result retrieval
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002061 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00002062 future<result_type> get_future() {return __p_.get_future();}
2063
2064 // execution
2065 void operator()(_ArgTypes... __args);
2066 void make_ready_at_thread_exit(_ArgTypes... __args);
2067
2068 void reset();
2069};
2070
Howard Hinnant99968442011-11-29 18:15:50 +00002071template<class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:21 +00002072void
Howard Hinnant99968442011-11-29 18:15:50 +00002073packaged_task<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __args)
Howard Hinnant54da3382010-08-30 18:46:21 +00002074{
Howard Hinnant54da3382010-08-30 18:46:21 +00002075 if (__p_.__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00002076 __throw_future_error(future_errc::no_state);
Howard Hinnant54da3382010-08-30 18:46:21 +00002077 if (__p_.__state_->__has_value())
Eric Fiselier423ca202015-10-02 21:25:15 +00002078 __throw_future_error(future_errc::promise_already_satisfied);
Marshall Clowa1899742015-09-03 15:11:32 +00002079#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant54da3382010-08-30 18:46:21 +00002080 try
2081 {
2082#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:19 +00002083 __p_.set_value(__f_(_VSTD::forward<_ArgTypes>(__args)...));
Howard Hinnant54da3382010-08-30 18:46:21 +00002084#ifndef _LIBCPP_NO_EXCEPTIONS
2085 }
2086 catch (...)
2087 {
2088 __p_.set_exception(current_exception());
2089 }
2090#endif // _LIBCPP_NO_EXCEPTIONS
2091}
2092
Howard Hinnant99968442011-11-29 18:15:50 +00002093template<class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:21 +00002094void
Howard Hinnant99968442011-11-29 18:15:50 +00002095packaged_task<_Rp(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
Howard Hinnant54da3382010-08-30 18:46:21 +00002096{
Howard Hinnant54da3382010-08-30 18:46:21 +00002097 if (__p_.__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00002098 __throw_future_error(future_errc::no_state);
Howard Hinnant54da3382010-08-30 18:46:21 +00002099 if (__p_.__state_->__has_value())
Eric Fiselier423ca202015-10-02 21:25:15 +00002100 __throw_future_error(future_errc::promise_already_satisfied);
Marshall Clowa1899742015-09-03 15:11:32 +00002101#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant54da3382010-08-30 18:46:21 +00002102 try
2103 {
2104#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:19 +00002105 __p_.set_value_at_thread_exit(__f_(_VSTD::forward<_ArgTypes>(__args)...));
Howard Hinnant54da3382010-08-30 18:46:21 +00002106#ifndef _LIBCPP_NO_EXCEPTIONS
2107 }
2108 catch (...)
2109 {
2110 __p_.set_exception_at_thread_exit(current_exception());
2111 }
2112#endif // _LIBCPP_NO_EXCEPTIONS
2113}
2114
Howard Hinnant99968442011-11-29 18:15:50 +00002115template<class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:21 +00002116void
Howard Hinnant99968442011-11-29 18:15:50 +00002117packaged_task<_Rp(_ArgTypes...)>::reset()
Howard Hinnant54da3382010-08-30 18:46:21 +00002118{
Howard Hinnant7de47902010-11-30 20:23:32 +00002119 if (!valid())
Eric Fiselier423ca202015-10-02 21:25:15 +00002120 __throw_future_error(future_errc::no_state);
Howard Hinnant54da3382010-08-30 18:46:21 +00002121 __p_ = promise<result_type>();
2122}
2123
2124template<class ..._ArgTypes>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002125class _LIBCPP_TYPE_VIS_ONLY packaged_task<void(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:21 +00002126{
2127public:
2128 typedef void result_type;
2129
2130private:
2131 __packaged_task_function<result_type(_ArgTypes...)> __f_;
2132 promise<result_type> __p_;
2133
2134public:
2135 // construction and destruction
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002136 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002137 packaged_task() _NOEXCEPT : __p_(nullptr) {}
Marshall Clow5f2d5b92013-10-12 22:49:17 +00002138 template <class _Fp,
2139 class = typename enable_if
2140 <
2141 !is_same<
2142 typename decay<_Fp>::type,
2143 packaged_task
2144 >::value
2145 >::type
2146 >
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002147 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00002148 explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
Marshall Clow5f2d5b92013-10-12 22:49:17 +00002149 template <class _Fp, class _Allocator,
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:26 +00002158 _LIBCPP_INLINE_VISIBILITY
Marshall Clow5706c372015-06-30 18:28:35 +00002159 packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
Howard Hinnant99968442011-11-29 18:15:50 +00002160 : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)),
Howard Hinnant54da3382010-08-30 18:46:21 +00002161 __p_(allocator_arg, __a) {}
2162 // ~packaged_task() = default;
2163
2164 // no copy
Howard Hinnant8131a012012-07-21 19:34:12 +00002165 packaged_task(const packaged_task&) = delete;
2166 packaged_task& operator=(const packaged_task&) = delete;
Howard Hinnant54da3382010-08-30 18:46:21 +00002167
2168 // move support
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002169 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002170 packaged_task(packaged_task&& __other) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002171 : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002172 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002173 packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:21 +00002174 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002175 __f_ = _VSTD::move(__other.__f_);
2176 __p_ = _VSTD::move(__other.__p_);
Howard Hinnant54da3382010-08-30 18:46:21 +00002177 return *this;
2178 }
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002179 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002180 void swap(packaged_task& __other) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:21 +00002181 {
2182 __f_.swap(__other.__f_);
2183 __p_.swap(__other.__p_);
2184 }
2185
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002186 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002187 bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
Howard Hinnant54da3382010-08-30 18:46:21 +00002188
2189 // result retrieval
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002190 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00002191 future<result_type> get_future() {return __p_.get_future();}
2192
2193 // execution
2194 void operator()(_ArgTypes... __args);
2195 void make_ready_at_thread_exit(_ArgTypes... __args);
2196
2197 void reset();
2198};
2199
2200template<class ..._ArgTypes>
2201void
2202packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args)
2203{
Howard Hinnant54da3382010-08-30 18:46:21 +00002204 if (__p_.__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00002205 __throw_future_error(future_errc::no_state);
Howard Hinnant54da3382010-08-30 18:46:21 +00002206 if (__p_.__state_->__has_value())
Eric Fiselier423ca202015-10-02 21:25:15 +00002207 __throw_future_error(future_errc::promise_already_satisfied);
Marshall Clowa1899742015-09-03 15:11:32 +00002208#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant54da3382010-08-30 18:46:21 +00002209 try
2210 {
2211#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:19 +00002212 __f_(_VSTD::forward<_ArgTypes>(__args)...);
Howard Hinnant54da3382010-08-30 18:46:21 +00002213 __p_.set_value();
2214#ifndef _LIBCPP_NO_EXCEPTIONS
2215 }
2216 catch (...)
2217 {
2218 __p_.set_exception(current_exception());
2219 }
2220#endif // _LIBCPP_NO_EXCEPTIONS
2221}
2222
2223template<class ..._ArgTypes>
2224void
2225packaged_task<void(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
2226{
Howard Hinnant54da3382010-08-30 18:46:21 +00002227 if (__p_.__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00002228 __throw_future_error(future_errc::no_state);
Howard Hinnant54da3382010-08-30 18:46:21 +00002229 if (__p_.__state_->__has_value())
Eric Fiselier423ca202015-10-02 21:25:15 +00002230 __throw_future_error(future_errc::promise_already_satisfied);
Marshall Clowa1899742015-09-03 15:11:32 +00002231#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant54da3382010-08-30 18:46:21 +00002232 try
2233 {
2234#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:19 +00002235 __f_(_VSTD::forward<_ArgTypes>(__args)...);
Howard Hinnant54da3382010-08-30 18:46:21 +00002236 __p_.set_value_at_thread_exit();
2237#ifndef _LIBCPP_NO_EXCEPTIONS
2238 }
2239 catch (...)
2240 {
2241 __p_.set_exception_at_thread_exit(current_exception());
2242 }
2243#endif // _LIBCPP_NO_EXCEPTIONS
2244}
2245
2246template<class ..._ArgTypes>
2247void
2248packaged_task<void(_ArgTypes...)>::reset()
2249{
Howard Hinnant7de47902010-11-30 20:23:32 +00002250 if (!valid())
Eric Fiselier423ca202015-10-02 21:25:15 +00002251 __throw_future_error(future_errc::no_state);
Howard Hinnant54da3382010-08-30 18:46:21 +00002252 __p_ = promise<result_type>();
2253}
2254
2255template <class _Callable>
2256inline _LIBCPP_INLINE_VISIBILITY
2257void
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002258swap(packaged_task<_Callable>& __x, packaged_task<_Callable>& __y) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:21 +00002259{
2260 __x.swap(__y);
2261}
2262
2263template <class _Callable, class _Alloc>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002264struct _LIBCPP_TYPE_VIS_ONLY uses_allocator<packaged_task<_Callable>, _Alloc>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002265 : public true_type {};
Howard Hinnant54da3382010-08-30 18:46:21 +00002266
Howard Hinnant99968442011-11-29 18:15:50 +00002267template <class _Rp, class _Fp>
2268future<_Rp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00002269#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00002270__make_deferred_assoc_state(_Fp&& __f)
Howard Hinnant54da3382010-08-30 18:46:21 +00002271#else
Howard Hinnant99968442011-11-29 18:15:50 +00002272__make_deferred_assoc_state(_Fp __f)
Howard Hinnant54da3382010-08-30 18:46:21 +00002273#endif
2274{
Howard Hinnant99968442011-11-29 18:15:50 +00002275 unique_ptr<__deferred_assoc_state<_Rp, _Fp>, __release_shared_count>
2276 __h(new __deferred_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
2277 return future<_Rp>(__h.get());
Howard Hinnant54da3382010-08-30 18:46:21 +00002278}
2279
Howard Hinnant99968442011-11-29 18:15:50 +00002280template <class _Rp, class _Fp>
2281future<_Rp>
Howard Hinnant57cff292011-05-19 15:05:04 +00002282#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00002283__make_async_assoc_state(_Fp&& __f)
Howard Hinnant57cff292011-05-19 15:05:04 +00002284#else
Howard Hinnant99968442011-11-29 18:15:50 +00002285__make_async_assoc_state(_Fp __f)
Howard Hinnant57cff292011-05-19 15:05:04 +00002286#endif
2287{
Howard Hinnant99968442011-11-29 18:15:50 +00002288 unique_ptr<__async_assoc_state<_Rp, _Fp>, __release_shared_count>
2289 __h(new __async_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
2290 _VSTD::thread(&__async_assoc_state<_Rp, _Fp>::__execute, __h.get()).detach();
2291 return future<_Rp>(__h.get());
Howard Hinnant57cff292011-05-19 15:05:04 +00002292}
2293
Howard Hinnant99968442011-11-29 18:15:50 +00002294template <class _Fp, class... _Args>
Howard Hinnant57cff292011-05-19 15:05:04 +00002295class __async_func
2296{
Howard Hinnant99968442011-11-29 18:15:50 +00002297 tuple<_Fp, _Args...> __f_;
Howard Hinnant57cff292011-05-19 15:05:04 +00002298
2299public:
Howard Hinnant99968442011-11-29 18:15:50 +00002300 typedef typename __invoke_of<_Fp, _Args...>::type _Rp;
Howard Hinnant57cff292011-05-19 15:05:04 +00002301
2302 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00002303 explicit __async_func(_Fp&& __f, _Args&&... __args)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002304 : __f_(_VSTD::move(__f), _VSTD::move(__args)...) {}
Howard Hinnant57cff292011-05-19 15:05:04 +00002305
2306 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0949eed2011-06-30 21:18:19 +00002307 __async_func(__async_func&& __f) : __f_(_VSTD::move(__f.__f_)) {}
Howard Hinnant57cff292011-05-19 15:05:04 +00002308
Howard Hinnant99968442011-11-29 18:15:50 +00002309 _Rp operator()()
Howard Hinnant57cff292011-05-19 15:05:04 +00002310 {
2311 typedef typename __make_tuple_indices<1+sizeof...(_Args), 1>::type _Index;
2312 return __execute(_Index());
2313 }
2314private:
2315 template <size_t ..._Indices>
Howard Hinnant99968442011-11-29 18:15:50 +00002316 _Rp
Howard Hinnant57cff292011-05-19 15:05:04 +00002317 __execute(__tuple_indices<_Indices...>)
2318 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002319 return __invoke(_VSTD::move(_VSTD::get<0>(__f_)), _VSTD::move(_VSTD::get<_Indices>(__f_))...);
Howard Hinnant57cff292011-05-19 15:05:04 +00002320 }
2321};
2322
Marshall Clow3b3108e2013-11-03 22:06:53 +00002323inline _LIBCPP_INLINE_VISIBILITY bool __does_policy_contain(launch __policy, launch __value )
Marshall Clowad2a6002013-11-03 15:43:35 +00002324{ return (int(__policy) & int(__value)) != 0; }
2325
Howard Hinnant99968442011-11-29 18:15:50 +00002326template <class _Fp, class... _Args>
2327future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
2328async(launch __policy, _Fp&& __f, _Args&&... __args)
Howard Hinnant54da3382010-08-30 18:46:21 +00002329{
Howard Hinnant99968442011-11-29 18:15:50 +00002330 typedef __async_func<typename decay<_Fp>::type, typename decay<_Args>::type...> _BF;
2331 typedef typename _BF::_Rp _Rp;
Marshall Clowad2a6002013-11-03 15:43:35 +00002332
2333#ifndef _LIBCPP_NO_EXCEPTIONS
2334 try
2335 {
2336#endif
2337 if (__does_policy_contain(__policy, launch::async))
2338 return _VSTD::__make_async_assoc_state<_Rp>(_BF(__decay_copy(_VSTD::forward<_Fp>(__f)),
Howard Hinnant0949eed2011-06-30 21:18:19 +00002339 __decay_copy(_VSTD::forward<_Args>(__args))...));
Marshall Clowad2a6002013-11-03 15:43:35 +00002340#ifndef _LIBCPP_NO_EXCEPTIONS
2341 }
2342 catch ( ... ) { if (__policy == launch::async) throw ; }
2343#endif
2344
2345 if (__does_policy_contain(__policy, launch::deferred))
2346 return _VSTD::__make_deferred_assoc_state<_Rp>(_BF(__decay_copy(_VSTD::forward<_Fp>(__f)),
Howard Hinnant0949eed2011-06-30 21:18:19 +00002347 __decay_copy(_VSTD::forward<_Args>(__args))...));
Marshall Clowad2a6002013-11-03 15:43:35 +00002348 return future<_Rp>{};
Howard Hinnant54da3382010-08-30 18:46:21 +00002349}
2350
Howard Hinnant99968442011-11-29 18:15:50 +00002351template <class _Fp, class... _Args>
Howard Hinnant54da3382010-08-30 18:46:21 +00002352inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00002353future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
2354async(_Fp&& __f, _Args&&... __args)
Howard Hinnant54da3382010-08-30 18:46:21 +00002355{
Howard Hinnant99968442011-11-29 18:15:50 +00002356 return _VSTD::async(launch::any, _VSTD::forward<_Fp>(__f),
Howard Hinnant0949eed2011-06-30 21:18:19 +00002357 _VSTD::forward<_Args>(__args)...);
Howard Hinnant54da3382010-08-30 18:46:21 +00002358}
2359
2360#endif // _LIBCPP_HAS_NO_VARIADICS
2361
Howard Hinnante6e4d012010-09-03 21:46:37 +00002362// shared_future
2363
Howard Hinnant99968442011-11-29 18:15:50 +00002364template <class _Rp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002365class _LIBCPP_TYPE_VIS_ONLY shared_future
Howard Hinnant99be8232010-09-03 18:39:25 +00002366{
Howard Hinnant99968442011-11-29 18:15:50 +00002367 __assoc_state<_Rp>* __state_;
Howard Hinnant99be8232010-09-03 18:39:25 +00002368
2369public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002370 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002371 shared_future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002372 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002373 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2374 {if (__state_) __state_->__add_shared();}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002375#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002376 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002377 shared_future(future<_Rp>&& __f) _NOEXCEPT : __state_(__f.__state_)
Howard Hinnant99be8232010-09-03 18:39:25 +00002378 {__f.__state_ = nullptr;}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002379 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002380 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
Howard Hinnant99be8232010-09-03 18:39:25 +00002381 {__rhs.__state_ = nullptr;}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002382#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:25 +00002383 ~shared_future();
2384 shared_future& operator=(const shared_future& __rhs);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002385#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002386 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002387 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:25 +00002388 {
2389 shared_future(std::move(__rhs)).swap(*this);
2390 return *this;
2391 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002392#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:25 +00002393
2394 // retrieving the value
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002395 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00002396 const _Rp& get() const {return __state_->copy();}
Howard Hinnant99be8232010-09-03 18:39:25 +00002397
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002398 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002399 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant99be8232010-09-03 18:39:25 +00002400
2401 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002402 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002403 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant99be8232010-09-03 18:39:25 +00002404
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002405 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002406 void wait() const {__state_->wait();}
2407 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002408 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002409 future_status
2410 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2411 {return __state_->wait_for(__rel_time);}
2412 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002413 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002414 future_status
2415 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2416 {return __state_->wait_until(__abs_time);}
2417};
2418
Howard Hinnant99968442011-11-29 18:15:50 +00002419template <class _Rp>
2420shared_future<_Rp>::~shared_future()
Howard Hinnant99be8232010-09-03 18:39:25 +00002421{
2422 if (__state_)
2423 __state_->__release_shared();
2424}
2425
Howard Hinnant99968442011-11-29 18:15:50 +00002426template <class _Rp>
2427shared_future<_Rp>&
2428shared_future<_Rp>::operator=(const shared_future& __rhs)
Howard Hinnant99be8232010-09-03 18:39:25 +00002429{
2430 if (__rhs.__state_)
2431 __rhs.__state_->__add_shared();
2432 if (__state_)
2433 __state_->__release_shared();
2434 __state_ = __rhs.__state_;
2435 return *this;
2436}
2437
Howard Hinnant99968442011-11-29 18:15:50 +00002438template <class _Rp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002439class _LIBCPP_TYPE_VIS_ONLY shared_future<_Rp&>
Howard Hinnant99be8232010-09-03 18:39:25 +00002440{
Howard Hinnant99968442011-11-29 18:15:50 +00002441 __assoc_state<_Rp&>* __state_;
Howard Hinnant99be8232010-09-03 18:39:25 +00002442
2443public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002444 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002445 shared_future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002446 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002447 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2448 {if (__state_) __state_->__add_shared();}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002449#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002450 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002451 shared_future(future<_Rp&>&& __f) _NOEXCEPT : __state_(__f.__state_)
Howard Hinnant99be8232010-09-03 18:39:25 +00002452 {__f.__state_ = nullptr;}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002453 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002454 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
Howard Hinnant99be8232010-09-03 18:39:25 +00002455 {__rhs.__state_ = nullptr;}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002456#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:25 +00002457 ~shared_future();
2458 shared_future& operator=(const shared_future& __rhs);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002459#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002460 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002461 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:25 +00002462 {
2463 shared_future(std::move(__rhs)).swap(*this);
2464 return *this;
2465 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002466#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:25 +00002467
2468 // retrieving the value
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002469 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00002470 _Rp& get() const {return __state_->copy();}
Howard Hinnant99be8232010-09-03 18:39:25 +00002471
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002472 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002473 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant99be8232010-09-03 18:39:25 +00002474
2475 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002476 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002477 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant99be8232010-09-03 18:39:25 +00002478
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002479 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002480 void wait() const {__state_->wait();}
2481 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002482 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002483 future_status
2484 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2485 {return __state_->wait_for(__rel_time);}
2486 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002487 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002488 future_status
2489 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2490 {return __state_->wait_until(__abs_time);}
2491};
2492
Howard Hinnant99968442011-11-29 18:15:50 +00002493template <class _Rp>
2494shared_future<_Rp&>::~shared_future()
Howard Hinnant99be8232010-09-03 18:39:25 +00002495{
2496 if (__state_)
2497 __state_->__release_shared();
2498}
2499
Howard Hinnant99968442011-11-29 18:15:50 +00002500template <class _Rp>
2501shared_future<_Rp&>&
2502shared_future<_Rp&>::operator=(const shared_future& __rhs)
Howard Hinnant99be8232010-09-03 18:39:25 +00002503{
2504 if (__rhs.__state_)
2505 __rhs.__state_->__add_shared();
2506 if (__state_)
2507 __state_->__release_shared();
2508 __state_ = __rhs.__state_;
2509 return *this;
2510}
2511
2512template <>
Howard Hinnant83eade62013-03-06 23:30:19 +00002513class _LIBCPP_TYPE_VIS shared_future<void>
Howard Hinnant99be8232010-09-03 18:39:25 +00002514{
2515 __assoc_sub_state* __state_;
2516
2517public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002518 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002519 shared_future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002520 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002521 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2522 {if (__state_) __state_->__add_shared();}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002523#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002524 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002525 shared_future(future<void>&& __f) _NOEXCEPT : __state_(__f.__state_)
Howard Hinnant99be8232010-09-03 18:39:25 +00002526 {__f.__state_ = nullptr;}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002527 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002528 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
Howard Hinnant99be8232010-09-03 18:39:25 +00002529 {__rhs.__state_ = nullptr;}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002530#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:25 +00002531 ~shared_future();
2532 shared_future& operator=(const shared_future& __rhs);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002533#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002534 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002535 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:25 +00002536 {
2537 shared_future(std::move(__rhs)).swap(*this);
2538 return *this;
2539 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002540#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:25 +00002541
2542 // retrieving the value
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002543 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002544 void get() const {__state_->copy();}
2545
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002546 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002547 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant99be8232010-09-03 18:39:25 +00002548
2549 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002550 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002551 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant99be8232010-09-03 18:39:25 +00002552
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002553 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002554 void wait() const {__state_->wait();}
2555 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002556 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002557 future_status
2558 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2559 {return __state_->wait_for(__rel_time);}
2560 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002562 future_status
2563 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2564 {return __state_->wait_until(__abs_time);}
2565};
2566
Howard Hinnant99968442011-11-29 18:15:50 +00002567template <class _Rp>
Howard Hinnant99be8232010-09-03 18:39:25 +00002568inline _LIBCPP_INLINE_VISIBILITY
2569void
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002570swap(shared_future<_Rp>& __x, shared_future<_Rp>& __y) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:25 +00002571{
2572 __x.swap(__y);
2573}
2574
Howard Hinnant99968442011-11-29 18:15:50 +00002575template <class _Rp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00002576inline
Howard Hinnant99968442011-11-29 18:15:50 +00002577shared_future<_Rp>
2578future<_Rp>::share()
Howard Hinnante6e4d012010-09-03 21:46:37 +00002579{
Howard Hinnant99968442011-11-29 18:15:50 +00002580 return shared_future<_Rp>(_VSTD::move(*this));
Howard Hinnante6e4d012010-09-03 21:46:37 +00002581}
2582
Howard Hinnant99968442011-11-29 18:15:50 +00002583template <class _Rp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00002584inline
Howard Hinnant99968442011-11-29 18:15:50 +00002585shared_future<_Rp&>
2586future<_Rp&>::share()
Howard Hinnante6e4d012010-09-03 21:46:37 +00002587{
Howard Hinnant99968442011-11-29 18:15:50 +00002588 return shared_future<_Rp&>(_VSTD::move(*this));
Howard Hinnant7de47902010-11-30 20:23:32 +00002589}
2590
Howard Hinnanta4451512010-12-02 16:45:21 +00002591#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2592
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00002593inline
Howard Hinnant7de47902010-11-30 20:23:32 +00002594shared_future<void>
2595future<void>::share()
2596{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002597 return shared_future<void>(_VSTD::move(*this));
Howard Hinnante6e4d012010-09-03 21:46:37 +00002598}
2599
Howard Hinnanta4451512010-12-02 16:45:21 +00002600#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2601
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002602_LIBCPP_END_NAMESPACE_STD
2603
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +00002604#endif // !_LIBCPP_HAS_NO_THREADS
2605
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002606#endif // _LIBCPP_FUTURE