blob: 25482c6cad2a430791f731f0448fb7343af1e1ea [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:161// -*- C++ -*-
2//===--------------------------- future -----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:014// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:165//
Howard Hinnantb64f8b02010-11-16 22:09:026// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:168//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_FUTURE
12#define _LIBCPP_FUTURE
13
14/*
15 future synopsis
16
17namespace std
18{
19
20enum class future_errc
21{
Howard Hinnantcd942f12013-09-14 18:20:1022 future_already_retrieved = 1,
Howard Hinnantbc8d3f92010-05-11 19:42:1623 promise_already_satisfied,
Howard Hinnantcd942f12013-09-14 18:20:1024 no_state,
25 broken_promise
Howard Hinnantbc8d3f92010-05-11 19:42:1626};
27
28enum class launch
29{
Howard Hinnant66895642010-11-23 18:33:5430 async = 1,
31 deferred = 2,
32 any = async | deferred
Howard Hinnantbc8d3f92010-05-11 19:42:1633};
34
35enum class future_status
36{
37 ready,
38 timeout,
39 deferred
40};
41
42template <> struct is_error_code_enum<future_errc> : public true_type { };
Howard Hinnant8bf01dd2012-07-21 17:46:5543error_code make_error_code(future_errc e) noexcept;
44error_condition make_error_condition(future_errc e) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1645
Howard Hinnant8bf01dd2012-07-21 17:46:5546const error_category& future_category() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1647
48class future_error
49 : public logic_error
50{
51public:
52 future_error(error_code ec); // exposition only
53
Howard Hinnant8bf01dd2012-07-21 17:46:5554 const error_code& code() const noexcept;
55 const char* what() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1656};
57
58template <class R>
59class promise
60{
61public:
62 promise();
63 template <class Allocator>
64 promise(allocator_arg_t, const Allocator& a);
Howard Hinnant8bf01dd2012-07-21 17:46:5565 promise(promise&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1666 promise(const promise& rhs) = delete;
67 ~promise();
68
69 // assignment
Howard Hinnant8bf01dd2012-07-21 17:46:5570 promise& operator=(promise&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1671 promise& operator=(const promise& rhs) = delete;
Howard Hinnant8bf01dd2012-07-21 17:46:5572 void swap(promise& other) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1673
74 // retrieving the result
75 future<R> get_future();
76
77 // setting the result
78 void set_value(const R& r);
79 void set_value(R&& r);
80 void set_exception(exception_ptr p);
81
82 // setting the result with deferred notification
83 void set_value_at_thread_exit(const R& r);
84 void set_value_at_thread_exit(R&& r);
85 void set_exception_at_thread_exit(exception_ptr p);
86};
87
88template <class R>
89class promise<R&>
90{
91public:
92 promise();
93 template <class Allocator>
94 promise(allocator_arg_t, const Allocator& a);
Howard Hinnant8bf01dd2012-07-21 17:46:5595 promise(promise&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1696 promise(const promise& rhs) = delete;
97 ~promise();
98
99 // assignment
Howard Hinnant8bf01dd2012-07-21 17:46:55100 promise& operator=(promise&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16101 promise& operator=(const promise& rhs) = delete;
Howard Hinnant8bf01dd2012-07-21 17:46:55102 void swap(promise& other) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16103
104 // retrieving the result
Howard Hinnant47499b12010-08-27 20:10:19105 future<R&> get_future();
Howard Hinnantbc8d3f92010-05-11 19:42:16106
107 // setting the result
108 void set_value(R& r);
109 void set_exception(exception_ptr p);
110
111 // setting the result with deferred notification
112 void set_value_at_thread_exit(R&);
113 void set_exception_at_thread_exit(exception_ptr p);
114};
115
116template <>
117class promise<void>
118{
119public:
120 promise();
121 template <class Allocator>
122 promise(allocator_arg_t, const Allocator& a);
Howard Hinnant8bf01dd2012-07-21 17:46:55123 promise(promise&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16124 promise(const promise& rhs) = delete;
125 ~promise();
126
127 // assignment
Howard Hinnant8bf01dd2012-07-21 17:46:55128 promise& operator=(promise&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16129 promise& operator=(const promise& rhs) = delete;
Howard Hinnant8bf01dd2012-07-21 17:46:55130 void swap(promise& other) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16131
132 // retrieving the result
Howard Hinnant47499b12010-08-27 20:10:19133 future<void> get_future();
Howard Hinnantbc8d3f92010-05-11 19:42:16134
135 // setting the result
136 void set_value();
137 void set_exception(exception_ptr p);
138
139 // setting the result with deferred notification
140 void set_value_at_thread_exit();
141 void set_exception_at_thread_exit(exception_ptr p);
142};
143
Howard Hinnant8bf01dd2012-07-21 17:46:55144template <class R> void swap(promise<R>& x, promise<R>& y) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16145
146template <class R, class Alloc>
147 struct uses_allocator<promise<R>, Alloc> : public true_type {};
148
149template <class R>
150class future
151{
152public:
Howard Hinnant8bf01dd2012-07-21 17:46:55153 future() noexcept;
154 future(future&&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16155 future(const future& rhs) = delete;
156 ~future();
157 future& operator=(const future& rhs) = delete;
Howard Hinnant8bf01dd2012-07-21 17:46:55158 future& operator=(future&&) noexcept;
159 shared_future<R> share();
Howard Hinnantbc8d3f92010-05-11 19:42:16160
161 // retrieving the value
162 R get();
163
164 // functions to check state
Howard Hinnant8bf01dd2012-07-21 17:46:55165 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16166
167 void wait() const;
168 template <class Rep, class Period>
169 future_status
170 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
171 template <class Clock, class Duration>
172 future_status
173 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
174};
175
176template <class R>
177class future<R&>
178{
179public:
Howard Hinnant8bf01dd2012-07-21 17:46:55180 future() noexcept;
181 future(future&&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16182 future(const future& rhs) = delete;
183 ~future();
184 future& operator=(const future& rhs) = delete;
Howard Hinnant8bf01dd2012-07-21 17:46:55185 future& operator=(future&&) noexcept;
186 shared_future<R&> share();
Howard Hinnantbc8d3f92010-05-11 19:42:16187
188 // retrieving the value
189 R& get();
190
191 // functions to check state
Howard Hinnant8bf01dd2012-07-21 17:46:55192 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16193
194 void wait() const;
195 template <class Rep, class Period>
196 future_status
197 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
198 template <class Clock, class Duration>
199 future_status
200 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
201};
202
203template <>
204class future<void>
205{
206public:
Howard Hinnant8bf01dd2012-07-21 17:46:55207 future() noexcept;
208 future(future&&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16209 future(const future& rhs) = delete;
210 ~future();
211 future& operator=(const future& rhs) = delete;
Howard Hinnant8bf01dd2012-07-21 17:46:55212 future& operator=(future&&) noexcept;
213 shared_future<void> share();
Howard Hinnantbc8d3f92010-05-11 19:42:16214
215 // retrieving the value
216 void get();
217
218 // functions to check state
Howard Hinnant8bf01dd2012-07-21 17:46:55219 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16220
221 void wait() const;
222 template <class Rep, class Period>
223 future_status
224 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
225 template <class Clock, class Duration>
226 future_status
227 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
228};
229
230template <class R>
231class shared_future
232{
233public:
Howard Hinnant8bf01dd2012-07-21 17:46:55234 shared_future() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16235 shared_future(const shared_future& rhs);
Howard Hinnant8bf01dd2012-07-21 17:46:55236 shared_future(future<R>&&) noexcept;
237 shared_future(shared_future&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16238 ~shared_future();
239 shared_future& operator=(const shared_future& rhs);
Howard Hinnant8bf01dd2012-07-21 17:46:55240 shared_future& operator=(shared_future&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16241
242 // retrieving the value
243 const R& get() const;
244
245 // functions to check state
Howard Hinnant8bf01dd2012-07-21 17:46:55246 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16247
248 void wait() const;
249 template <class Rep, class Period>
250 future_status
251 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
252 template <class Clock, class Duration>
253 future_status
254 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
255};
256
257template <class R>
258class shared_future<R&>
259{
260public:
Howard Hinnant8bf01dd2012-07-21 17:46:55261 shared_future() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16262 shared_future(const shared_future& rhs);
Howard Hinnant8bf01dd2012-07-21 17:46:55263 shared_future(future<R&>&&) noexcept;
264 shared_future(shared_future&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16265 ~shared_future();
266 shared_future& operator=(const shared_future& rhs);
Howard Hinnant8bf01dd2012-07-21 17:46:55267 shared_future& operator=(shared_future&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16268
269 // retrieving the value
270 R& get() const;
271
272 // functions to check state
Howard Hinnant8bf01dd2012-07-21 17:46:55273 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16274
275 void wait() const;
276 template <class Rep, class Period>
277 future_status
278 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
279 template <class Clock, class Duration>
280 future_status
281 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
282};
283
284template <>
285class shared_future<void>
286{
287public:
Howard Hinnant8bf01dd2012-07-21 17:46:55288 shared_future() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16289 shared_future(const shared_future& rhs);
Howard Hinnant8bf01dd2012-07-21 17:46:55290 shared_future(future<void>&&) noexcept;
291 shared_future(shared_future&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16292 ~shared_future();
293 shared_future& operator=(const shared_future& rhs);
Howard Hinnant8bf01dd2012-07-21 17:46:55294 shared_future& operator=(shared_future&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16295
296 // retrieving the value
297 void get() const;
298
299 // functions to check state
Howard Hinnant8bf01dd2012-07-21 17:46:55300 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16301
302 void wait() const;
303 template <class Rep, class Period>
304 future_status
305 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
306 template <class Clock, class Duration>
307 future_status
308 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
309};
310
Howard Hinnantbc8d3f92010-05-11 19:42:16311template <class F, class... Args>
Howard Hinnant0836f872013-09-21 18:17:23312 future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16313 async(F&& f, Args&&... args);
314
315template <class F, class... Args>
Howard Hinnant0836f872013-09-21 18:17:23316 future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16317 async(launch policy, F&& f, Args&&... args);
318
Howard Hinnantf5256e12010-05-11 21:36:01319template <class> class packaged_task; // undefined
Howard Hinnantbc8d3f92010-05-11 19:42:16320
321template <class R, class... ArgTypes>
322class packaged_task<R(ArgTypes...)>
323{
324public:
325 typedef R result_type;
326
327 // construction and destruction
Howard Hinnant8bf01dd2012-07-21 17:46:55328 packaged_task() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16329 template <class F>
Howard Hinnantbc8d3f92010-05-11 19:42:16330 explicit packaged_task(F&& f);
331 template <class F, class Allocator>
Marshall Clow07546f32015-06-30 14:16:49332 packaged_task(allocator_arg_t, const Allocator& a, F&& f);
Howard Hinnantbc8d3f92010-05-11 19:42:16333 ~packaged_task();
334
335 // no copy
Howard Hinnant8131a012012-07-21 19:34:12336 packaged_task(const packaged_task&) = delete;
337 packaged_task& operator=(const packaged_task&) = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16338
339 // move support
Howard Hinnant8bf01dd2012-07-21 17:46:55340 packaged_task(packaged_task&& other) noexcept;
341 packaged_task& operator=(packaged_task&& other) noexcept;
342 void swap(packaged_task& other) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16343
Howard Hinnant8bf01dd2012-07-21 17:46:55344 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16345
346 // result retrieval
347 future<R> get_future();
348
349 // execution
350 void operator()(ArgTypes... );
351 void make_ready_at_thread_exit(ArgTypes...);
352
353 void reset();
354};
355
356template <class R>
Howard Hinnant8bf01dd2012-07-21 17:46:55357 void swap(packaged_task<R(ArgTypes...)&, packaged_task<R(ArgTypes...)>&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16358
359template <class R, class Alloc> struct uses_allocator<packaged_task<R>, Alloc>;
360
361} // std
362
363*/
364
365#include <__config>
366#include <system_error>
Howard Hinnant47499b12010-08-27 20:10:19367#include <memory>
368#include <chrono>
369#include <exception>
Howard Hinnante6e4d012010-09-03 21:46:37370#include <mutex>
Howard Hinnant47499b12010-08-27 20:10:19371#include <thread>
Howard Hinnantbc8d3f92010-05-11 19:42:16372
Howard Hinnant08e17472011-10-17 20:05:10373#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16374#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10375#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16376
Jonathan Roelofsbaed05d2014-09-05 20:28:44377#ifdef _LIBCPP_HAS_NO_THREADS
Jonathan Roelofs8d86b2e2014-09-05 19:45:05378#error <future> is not supported on this single threaded system
379#else // !_LIBCPP_HAS_NO_THREADS
380
Howard Hinnantbc8d3f92010-05-11 19:42:16381_LIBCPP_BEGIN_NAMESPACE_STD
382
383//enum class future_errc
Howard Hinnantf6d875f2011-12-02 19:36:40384_LIBCPP_DECLARE_STRONG_ENUM(future_errc)
Howard Hinnantbc8d3f92010-05-11 19:42:16385{
Howard Hinnantcd942f12013-09-14 18:20:10386 future_already_retrieved = 1,
Howard Hinnantbc8d3f92010-05-11 19:42:16387 promise_already_satisfied,
Howard Hinnantcd942f12013-09-14 18:20:10388 no_state,
389 broken_promise
Howard Hinnantbc8d3f92010-05-11 19:42:16390};
Howard Hinnantf6d875f2011-12-02 19:36:40391_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_errc)
Howard Hinnantbc8d3f92010-05-11 19:42:16392
Howard Hinnant8c6cbb22010-09-22 14:16:26393template <>
Howard Hinnant0f678bd2013-08-12 18:38:34394struct _LIBCPP_TYPE_VIS_ONLY is_error_code_enum<future_errc> : public true_type {};
Howard Hinnanta6521722010-08-25 17:32:05395
Howard Hinnantf6d875f2011-12-02 19:36:40396#ifdef _LIBCPP_HAS_NO_STRONG_ENUMS
397template <>
Howard Hinnant0f678bd2013-08-12 18:38:34398struct _LIBCPP_TYPE_VIS_ONLY is_error_code_enum<future_errc::__lx> : public true_type { };
Howard Hinnantf6d875f2011-12-02 19:36:40399#endif
400
Howard Hinnantbc8d3f92010-05-11 19:42:16401//enum class launch
Howard Hinnantf6d875f2011-12-02 19:36:40402_LIBCPP_DECLARE_STRONG_ENUM(launch)
Howard Hinnantbc8d3f92010-05-11 19:42:16403{
Howard Hinnant66895642010-11-23 18:33:54404 async = 1,
405 deferred = 2,
406 any = async | deferred
Howard Hinnantbc8d3f92010-05-11 19:42:16407};
Howard Hinnantf6d875f2011-12-02 19:36:40408_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(launch)
Howard Hinnantbc8d3f92010-05-11 19:42:16409
Howard Hinnantf491e512013-06-29 18:38:17410#ifndef _LIBCPP_HAS_NO_STRONG_ENUMS
411
412#ifdef _LIBCXX_UNDERLYING_TYPE
413typedef underlying_type<launch>::type __launch_underlying_type;
414#else
415typedef int __launch_underlying_type;
416#endif
417
418inline _LIBCPP_INLINE_VISIBILITY
419_LIBCPP_CONSTEXPR
420launch
421operator&(launch __x, launch __y)
422{
423 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) &
424 static_cast<__launch_underlying_type>(__y));
425}
426
427inline _LIBCPP_INLINE_VISIBILITY
428_LIBCPP_CONSTEXPR
429launch
430operator|(launch __x, launch __y)
431{
432 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) |
433 static_cast<__launch_underlying_type>(__y));
434}
435
436inline _LIBCPP_INLINE_VISIBILITY
437_LIBCPP_CONSTEXPR
438launch
439operator^(launch __x, launch __y)
440{
441 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) ^
442 static_cast<__launch_underlying_type>(__y));
443}
444
445inline _LIBCPP_INLINE_VISIBILITY
446_LIBCPP_CONSTEXPR
447launch
448operator~(launch __x)
449{
Howard Hinnant6a683bf2013-07-02 18:01:41450 return static_cast<launch>(~static_cast<__launch_underlying_type>(__x) & 3);
Howard Hinnantf491e512013-06-29 18:38:17451}
452
453inline _LIBCPP_INLINE_VISIBILITY
454launch&
455operator&=(launch& __x, launch __y)
456{
457 __x = __x & __y; return __x;
458}
459
460inline _LIBCPP_INLINE_VISIBILITY
461launch&
462operator|=(launch& __x, launch __y)
463{
464 __x = __x | __y; return __x;
465}
466
467inline _LIBCPP_INLINE_VISIBILITY
468launch&
469operator^=(launch& __x, launch __y)
470{
471 __x = __x ^ __y; return __x;
472}
473
474#endif // !_LIBCPP_HAS_NO_STRONG_ENUMS
475
Howard Hinnantbc8d3f92010-05-11 19:42:16476//enum class future_status
Howard Hinnantf6d875f2011-12-02 19:36:40477_LIBCPP_DECLARE_STRONG_ENUM(future_status)
Howard Hinnantbc8d3f92010-05-11 19:42:16478{
Howard Hinnantbc8d3f92010-05-11 19:42:16479 ready,
480 timeout,
481 deferred
482};
Howard Hinnantf6d875f2011-12-02 19:36:40483_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_status)
Howard Hinnantbc8d3f92010-05-11 19:42:16484
Howard Hinnant83eade62013-03-06 23:30:19485_LIBCPP_FUNC_VIS
Howard Hinnant8bf01dd2012-07-21 17:46:55486const error_category& future_category() _NOEXCEPT;
Howard Hinnanta6521722010-08-25 17:32:05487
488inline _LIBCPP_INLINE_VISIBILITY
489error_code
Howard Hinnant8bf01dd2012-07-21 17:46:55490make_error_code(future_errc __e) _NOEXCEPT
Howard Hinnanta6521722010-08-25 17:32:05491{
492 return error_code(static_cast<int>(__e), future_category());
493}
494
495inline _LIBCPP_INLINE_VISIBILITY
496error_condition
Howard Hinnant8bf01dd2012-07-21 17:46:55497make_error_condition(future_errc __e) _NOEXCEPT
Howard Hinnanta6521722010-08-25 17:32:05498{
499 return error_condition(static_cast<int>(__e), future_category());
500}
501
Howard Hinnant8c6cbb22010-09-22 14:16:26502class _LIBCPP_EXCEPTION_ABI future_error
Howard Hinnanta6521722010-08-25 17:32:05503 : public logic_error
504{
505 error_code __ec_;
506public:
507 future_error(error_code __ec);
508
Howard Hinnant8c6cbb22010-09-22 14:16:26509 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55510 const error_code& code() const _NOEXCEPT {return __ec_;}
Howard Hinnantac6de542011-07-07 21:03:52511
512 virtual ~future_error() _NOEXCEPT;
Howard Hinnanta6521722010-08-25 17:32:05513};
514
Howard Hinnant0f678bd2013-08-12 18:38:34515class _LIBCPP_TYPE_VIS __assoc_sub_state
Howard Hinnant47499b12010-08-27 20:10:19516 : public __shared_count
517{
518protected:
519 exception_ptr __exception_;
520 mutable mutex __mut_;
521 mutable condition_variable __cv_;
522 unsigned __state_;
523
Howard Hinnant1694d232011-05-28 14:41:13524 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant54da3382010-08-30 18:46:21525 void __sub_wait(unique_lock<mutex>& __lk);
Howard Hinnant47499b12010-08-27 20:10:19526public:
527 enum
528 {
529 __constructed = 1,
530 __future_attached = 2,
531 ready = 4,
532 deferred = 8
533 };
534
Howard Hinnant8c6cbb22010-09-22 14:16:26535 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19536 __assoc_sub_state() : __state_(0) {}
537
Howard Hinnant8c6cbb22010-09-22 14:16:26538 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19539 bool __has_value() const
540 {return (__state_ & __constructed) || (__exception_ != nullptr);}
541
Howard Hinnant8c6cbb22010-09-22 14:16:26542 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1b031c92013-01-14 20:01:24543 void __set_future_attached()
544 {
545 lock_guard<mutex> __lk(__mut_);
546 __state_ |= __future_attached;
547 }
Howard Hinnant8c6cbb22010-09-22 14:16:26548 _LIBCPP_INLINE_VISIBILITY
Marshall Clow9de3d4c2013-10-13 01:02:45549 bool __has_future_attached() const {return (__state_ & __future_attached) != 0;}
Howard Hinnant47499b12010-08-27 20:10:19550
Howard Hinnant8c6cbb22010-09-22 14:16:26551 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21552 void __set_deferred() {__state_ |= deferred;}
553
Howard Hinnant47499b12010-08-27 20:10:19554 void __make_ready();
Howard Hinnant8c6cbb22010-09-22 14:16:26555 _LIBCPP_INLINE_VISIBILITY
Marshall Clow9de3d4c2013-10-13 01:02:45556 bool __is_ready() const {return (__state_ & ready) != 0;}
Howard Hinnant47499b12010-08-27 20:10:19557
558 void set_value();
559 void set_value_at_thread_exit();
560
561 void set_exception(exception_ptr __p);
562 void set_exception_at_thread_exit(exception_ptr __p);
563
564 void copy();
565
Howard Hinnant54da3382010-08-30 18:46:21566 void wait();
Howard Hinnant47499b12010-08-27 20:10:19567 template <class _Rep, class _Period>
568 future_status
569 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const;
570 template <class _Clock, class _Duration>
571 future_status
572 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const;
Howard Hinnant54da3382010-08-30 18:46:21573
574 virtual void __execute();
Howard Hinnant47499b12010-08-27 20:10:19575};
576
Howard Hinnantf39daa82010-08-28 21:01:06577template <class _Clock, class _Duration>
578future_status
579__assoc_sub_state::wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
580{
581 unique_lock<mutex> __lk(__mut_);
Howard Hinnant54da3382010-08-30 18:46:21582 if (__state_ & deferred)
583 return future_status::deferred;
584 while (!(__state_ & ready) && _Clock::now() < __abs_time)
Howard Hinnantf39daa82010-08-28 21:01:06585 __cv_.wait_until(__lk, __abs_time);
586 if (__state_ & ready)
587 return future_status::ready;
Howard Hinnantf39daa82010-08-28 21:01:06588 return future_status::timeout;
589}
590
591template <class _Rep, class _Period>
592inline _LIBCPP_INLINE_VISIBILITY
593future_status
594__assoc_sub_state::wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
595{
Howard Hinnantf8f85212010-11-20 19:16:30596 return wait_until(chrono::steady_clock::now() + __rel_time);
Howard Hinnantf39daa82010-08-28 21:01:06597}
598
Howard Hinnant99968442011-11-29 18:15:50599template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19600class __assoc_state
601 : public __assoc_sub_state
602{
603 typedef __assoc_sub_state base;
Howard Hinnant99968442011-11-29 18:15:50604 typedef typename aligned_storage<sizeof(_Rp), alignment_of<_Rp>::value>::type _Up;
Howard Hinnant47499b12010-08-27 20:10:19605protected:
Howard Hinnant99968442011-11-29 18:15:50606 _Up __value_;
Howard Hinnant47499b12010-08-27 20:10:19607
Howard Hinnant1694d232011-05-28 14:41:13608 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant47499b12010-08-27 20:10:19609public:
610
611 template <class _Arg>
Howard Hinnant73d21a42010-09-04 23:28:19612#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19613 void set_value(_Arg&& __arg);
614#else
615 void set_value(_Arg& __arg);
616#endif
617
618 template <class _Arg>
Howard Hinnant73d21a42010-09-04 23:28:19619#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19620 void set_value_at_thread_exit(_Arg&& __arg);
621#else
622 void set_value_at_thread_exit(_Arg& __arg);
623#endif
624
Howard Hinnant99968442011-11-29 18:15:50625 _Rp move();
626 typename add_lvalue_reference<_Rp>::type copy();
Howard Hinnant47499b12010-08-27 20:10:19627};
628
Howard Hinnant99968442011-11-29 18:15:50629template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19630void
Howard Hinnant99968442011-11-29 18:15:50631__assoc_state<_Rp>::__on_zero_shared() _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19632{
633 if (this->__state_ & base::__constructed)
Howard Hinnant99968442011-11-29 18:15:50634 reinterpret_cast<_Rp*>(&__value_)->~_Rp();
Howard Hinnant47499b12010-08-27 20:10:19635 delete this;
636}
637
Howard Hinnant99968442011-11-29 18:15:50638template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19639template <class _Arg>
640void
Howard Hinnant73d21a42010-09-04 23:28:19641#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50642__assoc_state<_Rp>::set_value(_Arg&& __arg)
Howard Hinnant47499b12010-08-27 20:10:19643#else
Howard Hinnant99968442011-11-29 18:15:50644__assoc_state<_Rp>::set_value(_Arg& __arg)
Howard Hinnant47499b12010-08-27 20:10:19645#endif
646{
647 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant22ba71b2011-07-13 16:00:50648#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:19649 if (this->__has_value())
650 throw future_error(make_error_code(future_errc::promise_already_satisfied));
Howard Hinnant22ba71b2011-07-13 16:00:50651#endif
Howard Hinnant99968442011-11-29 18:15:50652 ::new(&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
Howard Hinnant47499b12010-08-27 20:10:19653 this->__state_ |= base::__constructed | base::ready;
Howard Hinnant47499b12010-08-27 20:10:19654 __cv_.notify_all();
655}
656
Howard Hinnant99968442011-11-29 18:15:50657template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19658template <class _Arg>
659void
Howard Hinnant73d21a42010-09-04 23:28:19660#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50661__assoc_state<_Rp>::set_value_at_thread_exit(_Arg&& __arg)
Howard Hinnant47499b12010-08-27 20:10:19662#else
Howard Hinnant99968442011-11-29 18:15:50663__assoc_state<_Rp>::set_value_at_thread_exit(_Arg& __arg)
Howard Hinnant47499b12010-08-27 20:10:19664#endif
665{
666 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant22ba71b2011-07-13 16:00:50667#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:19668 if (this->__has_value())
669 throw future_error(make_error_code(future_errc::promise_already_satisfied));
Howard Hinnant22ba71b2011-07-13 16:00:50670#endif
Howard Hinnant99968442011-11-29 18:15:50671 ::new(&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
Howard Hinnant47499b12010-08-27 20:10:19672 this->__state_ |= base::__constructed;
Howard Hinnant5306d682010-10-14 19:18:04673 __thread_local_data()->__make_ready_at_thread_exit(this);
Howard Hinnant47499b12010-08-27 20:10:19674}
675
Howard Hinnant99968442011-11-29 18:15:50676template <class _Rp>
677_Rp
678__assoc_state<_Rp>::move()
Howard Hinnant47499b12010-08-27 20:10:19679{
680 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant54da3382010-08-30 18:46:21681 this->__sub_wait(__lk);
Howard Hinnant47499b12010-08-27 20:10:19682 if (this->__exception_ != nullptr)
683 rethrow_exception(this->__exception_);
Howard Hinnant99968442011-11-29 18:15:50684 return _VSTD::move(*reinterpret_cast<_Rp*>(&__value_));
Howard Hinnant47499b12010-08-27 20:10:19685}
686
Howard Hinnant99968442011-11-29 18:15:50687template <class _Rp>
688typename add_lvalue_reference<_Rp>::type
689__assoc_state<_Rp>::copy()
Howard Hinnant47499b12010-08-27 20:10:19690{
691 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant54da3382010-08-30 18:46:21692 this->__sub_wait(__lk);
Howard Hinnant47499b12010-08-27 20:10:19693 if (this->__exception_ != nullptr)
694 rethrow_exception(this->__exception_);
Howard Hinnant99968442011-11-29 18:15:50695 return *reinterpret_cast<_Rp*>(&__value_);
Howard Hinnant47499b12010-08-27 20:10:19696}
697
Howard Hinnant99968442011-11-29 18:15:50698template <class _Rp>
699class __assoc_state<_Rp&>
Howard Hinnantf39daa82010-08-28 21:01:06700 : public __assoc_sub_state
701{
702 typedef __assoc_sub_state base;
Howard Hinnant99968442011-11-29 18:15:50703 typedef _Rp* _Up;
Howard Hinnantf39daa82010-08-28 21:01:06704protected:
Howard Hinnant99968442011-11-29 18:15:50705 _Up __value_;
Howard Hinnantf39daa82010-08-28 21:01:06706
Howard Hinnant1694d232011-05-28 14:41:13707 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnantf39daa82010-08-28 21:01:06708public:
709
Howard Hinnant99968442011-11-29 18:15:50710 void set_value(_Rp& __arg);
711 void set_value_at_thread_exit(_Rp& __arg);
Howard Hinnantf39daa82010-08-28 21:01:06712
Howard Hinnant99968442011-11-29 18:15:50713 _Rp& copy();
Howard Hinnantf39daa82010-08-28 21:01:06714};
715
Howard Hinnant99968442011-11-29 18:15:50716template <class _Rp>
Howard Hinnantf39daa82010-08-28 21:01:06717void
Howard Hinnant99968442011-11-29 18:15:50718__assoc_state<_Rp&>::__on_zero_shared() _NOEXCEPT
Howard Hinnantf39daa82010-08-28 21:01:06719{
720 delete this;
721}
722
Howard Hinnant99968442011-11-29 18:15:50723template <class _Rp>
Howard Hinnantf39daa82010-08-28 21:01:06724void
Howard Hinnant99968442011-11-29 18:15:50725__assoc_state<_Rp&>::set_value(_Rp& __arg)
Howard Hinnantf39daa82010-08-28 21:01:06726{
727 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant22ba71b2011-07-13 16:00:50728#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantf39daa82010-08-28 21:01:06729 if (this->__has_value())
730 throw future_error(make_error_code(future_errc::promise_already_satisfied));
Howard Hinnant22ba71b2011-07-13 16:00:50731#endif
Howard Hinnanta4e87ab2013-08-08 18:38:55732 __value_ = _VSTD::addressof(__arg);
Howard Hinnantf39daa82010-08-28 21:01:06733 this->__state_ |= base::__constructed | base::ready;
Howard Hinnantf39daa82010-08-28 21:01:06734 __cv_.notify_all();
735}
736
Howard Hinnant99968442011-11-29 18:15:50737template <class _Rp>
Howard Hinnantf39daa82010-08-28 21:01:06738void
Howard Hinnant99968442011-11-29 18:15:50739__assoc_state<_Rp&>::set_value_at_thread_exit(_Rp& __arg)
Howard Hinnantf39daa82010-08-28 21:01:06740{
741 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant22ba71b2011-07-13 16:00:50742#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantf39daa82010-08-28 21:01:06743 if (this->__has_value())
744 throw future_error(make_error_code(future_errc::promise_already_satisfied));
Howard Hinnant22ba71b2011-07-13 16:00:50745#endif
Howard Hinnanta4e87ab2013-08-08 18:38:55746 __value_ = _VSTD::addressof(__arg);
Howard Hinnantf39daa82010-08-28 21:01:06747 this->__state_ |= base::__constructed;
Howard Hinnant5306d682010-10-14 19:18:04748 __thread_local_data()->__make_ready_at_thread_exit(this);
Howard Hinnantf39daa82010-08-28 21:01:06749}
750
Howard Hinnant99968442011-11-29 18:15:50751template <class _Rp>
752_Rp&
753__assoc_state<_Rp&>::copy()
Howard Hinnantf39daa82010-08-28 21:01:06754{
755 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant54da3382010-08-30 18:46:21756 this->__sub_wait(__lk);
Howard Hinnantf39daa82010-08-28 21:01:06757 if (this->__exception_ != nullptr)
758 rethrow_exception(this->__exception_);
759 return *__value_;
760}
761
Howard Hinnant99968442011-11-29 18:15:50762template <class _Rp, class _Alloc>
Howard Hinnant47499b12010-08-27 20:10:19763class __assoc_state_alloc
Howard Hinnant99968442011-11-29 18:15:50764 : public __assoc_state<_Rp>
Howard Hinnant47499b12010-08-27 20:10:19765{
Howard Hinnant99968442011-11-29 18:15:50766 typedef __assoc_state<_Rp> base;
Howard Hinnant47499b12010-08-27 20:10:19767 _Alloc __alloc_;
768
Howard Hinnant1694d232011-05-28 14:41:13769 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant47499b12010-08-27 20:10:19770public:
Howard Hinnant8c6cbb22010-09-22 14:16:26771 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19772 explicit __assoc_state_alloc(const _Alloc& __a)
773 : __alloc_(__a) {}
774};
775
Howard Hinnant99968442011-11-29 18:15:50776template <class _Rp, class _Alloc>
Howard Hinnant47499b12010-08-27 20:10:19777void
Howard Hinnant99968442011-11-29 18:15:50778__assoc_state_alloc<_Rp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19779{
780 if (this->__state_ & base::__constructed)
Howard Hinnanta4e87ab2013-08-08 18:38:55781 reinterpret_cast<_Rp*>(_VSTD::addressof(this->__value_))->~_Rp();
Eric Fiselier8492cd82015-02-05 23:01:40782 typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
783 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4d2413c2014-10-23 06:24:45784 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselier8492cd82015-02-05 23:01:40785 _Al __a(__alloc_);
Howard Hinnant47499b12010-08-27 20:10:19786 this->~__assoc_state_alloc();
Eric Fiselier4d2413c2014-10-23 06:24:45787 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnant47499b12010-08-27 20:10:19788}
789
Howard Hinnant99968442011-11-29 18:15:50790template <class _Rp, class _Alloc>
791class __assoc_state_alloc<_Rp&, _Alloc>
792 : public __assoc_state<_Rp&>
Howard Hinnantf39daa82010-08-28 21:01:06793{
Howard Hinnant99968442011-11-29 18:15:50794 typedef __assoc_state<_Rp&> base;
Howard Hinnantf39daa82010-08-28 21:01:06795 _Alloc __alloc_;
796
Howard Hinnant1694d232011-05-28 14:41:13797 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnantf39daa82010-08-28 21:01:06798public:
Howard Hinnant8c6cbb22010-09-22 14:16:26799 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39daa82010-08-28 21:01:06800 explicit __assoc_state_alloc(const _Alloc& __a)
801 : __alloc_(__a) {}
802};
803
Howard Hinnant99968442011-11-29 18:15:50804template <class _Rp, class _Alloc>
Howard Hinnantf39daa82010-08-28 21:01:06805void
Howard Hinnant99968442011-11-29 18:15:50806__assoc_state_alloc<_Rp&, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantf39daa82010-08-28 21:01:06807{
Eric Fiselier8492cd82015-02-05 23:01:40808 typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
809 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4d2413c2014-10-23 06:24:45810 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselier8492cd82015-02-05 23:01:40811 _Al __a(__alloc_);
Howard Hinnantf39daa82010-08-28 21:01:06812 this->~__assoc_state_alloc();
Eric Fiselier4d2413c2014-10-23 06:24:45813 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantf39daa82010-08-28 21:01:06814}
815
Howard Hinnant47499b12010-08-27 20:10:19816template <class _Alloc>
817class __assoc_sub_state_alloc
818 : public __assoc_sub_state
819{
820 typedef __assoc_sub_state base;
821 _Alloc __alloc_;
822
Howard Hinnant1694d232011-05-28 14:41:13823 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant47499b12010-08-27 20:10:19824public:
Howard Hinnant8c6cbb22010-09-22 14:16:26825 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19826 explicit __assoc_sub_state_alloc(const _Alloc& __a)
827 : __alloc_(__a) {}
828};
829
830template <class _Alloc>
831void
Howard Hinnant1694d232011-05-28 14:41:13832__assoc_sub_state_alloc<_Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19833{
Eric Fiselier8492cd82015-02-05 23:01:40834 typedef typename __allocator_traits_rebind<_Alloc, __assoc_sub_state_alloc>::type _Al;
835 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4d2413c2014-10-23 06:24:45836 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselier8492cd82015-02-05 23:01:40837 _Al __a(__alloc_);
Howard Hinnant47499b12010-08-27 20:10:19838 this->~__assoc_sub_state_alloc();
Eric Fiselier4d2413c2014-10-23 06:24:45839 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnant47499b12010-08-27 20:10:19840}
841
Howard Hinnant99968442011-11-29 18:15:50842template <class _Rp, class _Fp>
Howard Hinnant54da3382010-08-30 18:46:21843class __deferred_assoc_state
Howard Hinnant99968442011-11-29 18:15:50844 : public __assoc_state<_Rp>
Howard Hinnant54da3382010-08-30 18:46:21845{
Howard Hinnant99968442011-11-29 18:15:50846 typedef __assoc_state<_Rp> base;
Howard Hinnant54da3382010-08-30 18:46:21847
Howard Hinnant99968442011-11-29 18:15:50848 _Fp __func_;
Howard Hinnant54da3382010-08-30 18:46:21849
850public:
Howard Hinnant73d21a42010-09-04 23:28:19851#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50852 explicit __deferred_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:21853#endif
854
855 virtual void __execute();
856};
857
Howard Hinnant73d21a42010-09-04 23:28:19858#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21859
Howard Hinnant99968442011-11-29 18:15:50860template <class _Rp, class _Fp>
Howard Hinnant54da3382010-08-30 18:46:21861inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50862__deferred_assoc_state<_Rp, _Fp>::__deferred_assoc_state(_Fp&& __f)
863 : __func_(_VSTD::forward<_Fp>(__f))
Howard Hinnant54da3382010-08-30 18:46:21864{
865 this->__set_deferred();
866}
867
Howard Hinnant73d21a42010-09-04 23:28:19868#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21869
Howard Hinnant99968442011-11-29 18:15:50870template <class _Rp, class _Fp>
Howard Hinnant54da3382010-08-30 18:46:21871void
Howard Hinnant99968442011-11-29 18:15:50872__deferred_assoc_state<_Rp, _Fp>::__execute()
Howard Hinnant54da3382010-08-30 18:46:21873{
874#ifndef _LIBCPP_NO_EXCEPTIONS
875 try
876 {
877#endif // _LIBCPP_NO_EXCEPTIONS
878 this->set_value(__func_());
879#ifndef _LIBCPP_NO_EXCEPTIONS
880 }
881 catch (...)
882 {
883 this->set_exception(current_exception());
884 }
885#endif // _LIBCPP_NO_EXCEPTIONS
886}
887
Howard Hinnant99968442011-11-29 18:15:50888template <class _Fp>
889class __deferred_assoc_state<void, _Fp>
Howard Hinnant54da3382010-08-30 18:46:21890 : public __assoc_sub_state
891{
892 typedef __assoc_sub_state base;
893
Howard Hinnant99968442011-11-29 18:15:50894 _Fp __func_;
Howard Hinnant54da3382010-08-30 18:46:21895
896public:
Howard Hinnant73d21a42010-09-04 23:28:19897#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50898 explicit __deferred_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:21899#endif
900
901 virtual void __execute();
902};
903
Howard Hinnant73d21a42010-09-04 23:28:19904#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21905
Howard Hinnant99968442011-11-29 18:15:50906template <class _Fp>
Howard Hinnant54da3382010-08-30 18:46:21907inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50908__deferred_assoc_state<void, _Fp>::__deferred_assoc_state(_Fp&& __f)
909 : __func_(_VSTD::forward<_Fp>(__f))
Howard Hinnant54da3382010-08-30 18:46:21910{
911 this->__set_deferred();
912}
913
Howard Hinnant73d21a42010-09-04 23:28:19914#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21915
Howard Hinnant99968442011-11-29 18:15:50916template <class _Fp>
Howard Hinnant54da3382010-08-30 18:46:21917void
Howard Hinnant99968442011-11-29 18:15:50918__deferred_assoc_state<void, _Fp>::__execute()
Howard Hinnant54da3382010-08-30 18:46:21919{
920#ifndef _LIBCPP_NO_EXCEPTIONS
921 try
922 {
923#endif // _LIBCPP_NO_EXCEPTIONS
924 __func_();
925 this->set_value();
926#ifndef _LIBCPP_NO_EXCEPTIONS
927 }
928 catch (...)
929 {
930 this->set_exception(current_exception());
931 }
932#endif // _LIBCPP_NO_EXCEPTIONS
933}
934
Howard Hinnant99968442011-11-29 18:15:50935template <class _Rp, class _Fp>
Howard Hinnant57cff292011-05-19 15:05:04936class __async_assoc_state
Howard Hinnant99968442011-11-29 18:15:50937 : public __assoc_state<_Rp>
Howard Hinnant57cff292011-05-19 15:05:04938{
Howard Hinnant99968442011-11-29 18:15:50939 typedef __assoc_state<_Rp> base;
Howard Hinnant57cff292011-05-19 15:05:04940
Howard Hinnant99968442011-11-29 18:15:50941 _Fp __func_;
Howard Hinnant57cff292011-05-19 15:05:04942
Howard Hinnant1694d232011-05-28 14:41:13943 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant57cff292011-05-19 15:05:04944public:
945#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50946 explicit __async_assoc_state(_Fp&& __f);
Howard Hinnant57cff292011-05-19 15:05:04947#endif
948
949 virtual void __execute();
950};
951
952#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
953
Howard Hinnant99968442011-11-29 18:15:50954template <class _Rp, class _Fp>
Howard Hinnant57cff292011-05-19 15:05:04955inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50956__async_assoc_state<_Rp, _Fp>::__async_assoc_state(_Fp&& __f)
957 : __func_(_VSTD::forward<_Fp>(__f))
Howard Hinnant57cff292011-05-19 15:05:04958{
959}
960
961#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
962
Howard Hinnant99968442011-11-29 18:15:50963template <class _Rp, class _Fp>
Howard Hinnant57cff292011-05-19 15:05:04964void
Howard Hinnant99968442011-11-29 18:15:50965__async_assoc_state<_Rp, _Fp>::__execute()
Howard Hinnant57cff292011-05-19 15:05:04966{
967#ifndef _LIBCPP_NO_EXCEPTIONS
968 try
969 {
970#endif // _LIBCPP_NO_EXCEPTIONS
971 this->set_value(__func_());
972#ifndef _LIBCPP_NO_EXCEPTIONS
973 }
974 catch (...)
975 {
976 this->set_exception(current_exception());
977 }
978#endif // _LIBCPP_NO_EXCEPTIONS
979}
980
Howard Hinnant99968442011-11-29 18:15:50981template <class _Rp, class _Fp>
Howard Hinnant57cff292011-05-19 15:05:04982void
Howard Hinnant99968442011-11-29 18:15:50983__async_assoc_state<_Rp, _Fp>::__on_zero_shared() _NOEXCEPT
Howard Hinnant57cff292011-05-19 15:05:04984{
985 this->wait();
986 base::__on_zero_shared();
987}
988
Howard Hinnant99968442011-11-29 18:15:50989template <class _Fp>
990class __async_assoc_state<void, _Fp>
Howard Hinnant57cff292011-05-19 15:05:04991 : public __assoc_sub_state
992{
993 typedef __assoc_sub_state base;
994
Howard Hinnant99968442011-11-29 18:15:50995 _Fp __func_;
Howard Hinnant57cff292011-05-19 15:05:04996
Howard Hinnant1694d232011-05-28 14:41:13997 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant57cff292011-05-19 15:05:04998public:
999#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501000 explicit __async_assoc_state(_Fp&& __f);
Howard Hinnant57cff292011-05-19 15:05:041001#endif
1002
1003 virtual void __execute();
1004};
1005
1006#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1007
Howard Hinnant99968442011-11-29 18:15:501008template <class _Fp>
Howard Hinnant57cff292011-05-19 15:05:041009inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:501010__async_assoc_state<void, _Fp>::__async_assoc_state(_Fp&& __f)
1011 : __func_(_VSTD::forward<_Fp>(__f))
Howard Hinnant57cff292011-05-19 15:05:041012{
1013}
1014
1015#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1016
Howard Hinnant99968442011-11-29 18:15:501017template <class _Fp>
Howard Hinnant57cff292011-05-19 15:05:041018void
Howard Hinnant99968442011-11-29 18:15:501019__async_assoc_state<void, _Fp>::__execute()
Howard Hinnant57cff292011-05-19 15:05:041020{
1021#ifndef _LIBCPP_NO_EXCEPTIONS
1022 try
1023 {
1024#endif // _LIBCPP_NO_EXCEPTIONS
1025 __func_();
1026 this->set_value();
1027#ifndef _LIBCPP_NO_EXCEPTIONS
1028 }
1029 catch (...)
1030 {
1031 this->set_exception(current_exception());
1032 }
1033#endif // _LIBCPP_NO_EXCEPTIONS
1034}
1035
Howard Hinnant99968442011-11-29 18:15:501036template <class _Fp>
Howard Hinnant57cff292011-05-19 15:05:041037void
Howard Hinnant99968442011-11-29 18:15:501038__async_assoc_state<void, _Fp>::__on_zero_shared() _NOEXCEPT
Howard Hinnant57cff292011-05-19 15:05:041039{
1040 this->wait();
1041 base::__on_zero_shared();
1042}
1043
Howard Hinnant0f678bd2013-08-12 18:38:341044template <class _Rp> class _LIBCPP_TYPE_VIS_ONLY promise;
1045template <class _Rp> class _LIBCPP_TYPE_VIS_ONLY shared_future;
Howard Hinnant47499b12010-08-27 20:10:191046
1047// future
1048
Howard Hinnant0f678bd2013-08-12 18:38:341049template <class _Rp> class _LIBCPP_TYPE_VIS_ONLY future;
Howard Hinnant54da3382010-08-30 18:46:211050
Howard Hinnant99968442011-11-29 18:15:501051template <class _Rp, class _Fp>
1052future<_Rp>
Howard Hinnant73d21a42010-09-04 23:28:191053#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501054__make_deferred_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:211055#else
Howard Hinnant99968442011-11-29 18:15:501056__make_deferred_assoc_state(_Fp __f);
Howard Hinnant54da3382010-08-30 18:46:211057#endif
1058
Howard Hinnant99968442011-11-29 18:15:501059template <class _Rp, class _Fp>
1060future<_Rp>
Howard Hinnant57cff292011-05-19 15:05:041061#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501062__make_async_assoc_state(_Fp&& __f);
Howard Hinnant57cff292011-05-19 15:05:041063#else
Howard Hinnant99968442011-11-29 18:15:501064__make_async_assoc_state(_Fp __f);
Howard Hinnant57cff292011-05-19 15:05:041065#endif
1066
Howard Hinnant99968442011-11-29 18:15:501067template <class _Rp>
Howard Hinnant0f678bd2013-08-12 18:38:341068class _LIBCPP_TYPE_VIS_ONLY future
Howard Hinnant47499b12010-08-27 20:10:191069{
Howard Hinnant99968442011-11-29 18:15:501070 __assoc_state<_Rp>* __state_;
Howard Hinnant47499b12010-08-27 20:10:191071
Howard Hinnant99968442011-11-29 18:15:501072 explicit future(__assoc_state<_Rp>* __state);
Howard Hinnant47499b12010-08-27 20:10:191073
1074 template <class> friend class promise;
Howard Hinnant99be8232010-09-03 18:39:251075 template <class> friend class shared_future;
Howard Hinnant54da3382010-08-30 18:46:211076
Howard Hinnant73d21a42010-09-04 23:28:191077#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501078 template <class _R1, class _Fp>
1079 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1080 template <class _R1, class _Fp>
1081 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:211082#else
Howard Hinnant99968442011-11-29 18:15:501083 template <class _R1, class _Fp>
1084 friend future<_R1> __make_deferred_assoc_state(_Fp __f);
1085 template <class _R1, class _Fp>
1086 friend future<_R1> __make_async_assoc_state(_Fp __f);
Howard Hinnant54da3382010-08-30 18:46:211087#endif
1088
Howard Hinnant47499b12010-08-27 20:10:191089public:
Howard Hinnant8c6cbb22010-09-22 14:16:261090 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551091 future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant73d21a42010-09-04 23:28:191092#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261093 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551094 future(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191095 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1096 future(const future&) = delete;
1097 future& operator=(const future&) = delete;
Howard Hinnant8c6cbb22010-09-22 14:16:261098 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551099 future& operator=(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191100 {
1101 future(std::move(__rhs)).swap(*this);
1102 return *this;
1103 }
Howard Hinnant73d21a42010-09-04 23:28:191104#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191105private:
1106 future(const future&);
1107 future& operator=(const future&);
1108public:
Howard Hinnant73d21a42010-09-04 23:28:191109#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191110 ~future();
Howard Hinnant99968442011-11-29 18:15:501111 shared_future<_Rp> share();
Howard Hinnant47499b12010-08-27 20:10:191112
1113 // retrieving the value
Howard Hinnant99968442011-11-29 18:15:501114 _Rp get();
Howard Hinnant47499b12010-08-27 20:10:191115
Howard Hinnant8c6cbb22010-09-22 14:16:261116 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551117 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:191118
1119 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:261120 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551121 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant47499b12010-08-27 20:10:191122
Howard Hinnant8c6cbb22010-09-22 14:16:261123 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191124 void wait() const {__state_->wait();}
1125 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:261126 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191127 future_status
1128 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1129 {return __state_->wait_for(__rel_time);}
1130 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:261131 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191132 future_status
1133 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1134 {return __state_->wait_until(__abs_time);}
1135};
1136
Howard Hinnant99968442011-11-29 18:15:501137template <class _Rp>
1138future<_Rp>::future(__assoc_state<_Rp>* __state)
Howard Hinnant47499b12010-08-27 20:10:191139 : __state_(__state)
1140{
Howard Hinnant22ba71b2011-07-13 16:00:501141#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:191142 if (__state_->__has_future_attached())
1143 throw future_error(make_error_code(future_errc::future_already_retrieved));
Howard Hinnant22ba71b2011-07-13 16:00:501144#endif
Howard Hinnant47499b12010-08-27 20:10:191145 __state_->__add_shared();
Howard Hinnant54da3382010-08-30 18:46:211146 __state_->__set_future_attached();
Howard Hinnant47499b12010-08-27 20:10:191147}
1148
Howard Hinnant54da3382010-08-30 18:46:211149struct __release_shared_count
1150{
1151 void operator()(__shared_count* p) {p->__release_shared();}
1152};
1153
Howard Hinnant99968442011-11-29 18:15:501154template <class _Rp>
1155future<_Rp>::~future()
Howard Hinnant47499b12010-08-27 20:10:191156{
1157 if (__state_)
1158 __state_->__release_shared();
1159}
1160
Howard Hinnant99968442011-11-29 18:15:501161template <class _Rp>
1162_Rp
1163future<_Rp>::get()
Howard Hinnant47499b12010-08-27 20:10:191164{
Howard Hinnant54da3382010-08-30 18:46:211165 unique_ptr<__shared_count, __release_shared_count> __(__state_);
Howard Hinnant99968442011-11-29 18:15:501166 __assoc_state<_Rp>* __s = __state_;
Howard Hinnant47499b12010-08-27 20:10:191167 __state_ = nullptr;
1168 return __s->move();
1169}
1170
Howard Hinnant99968442011-11-29 18:15:501171template <class _Rp>
Howard Hinnant0f678bd2013-08-12 18:38:341172class _LIBCPP_TYPE_VIS_ONLY future<_Rp&>
Howard Hinnant47499b12010-08-27 20:10:191173{
Howard Hinnant99968442011-11-29 18:15:501174 __assoc_state<_Rp&>* __state_;
Howard Hinnant47499b12010-08-27 20:10:191175
Howard Hinnant99968442011-11-29 18:15:501176 explicit future(__assoc_state<_Rp&>* __state);
Howard Hinnant47499b12010-08-27 20:10:191177
1178 template <class> friend class promise;
Howard Hinnant99be8232010-09-03 18:39:251179 template <class> friend class shared_future;
Howard Hinnant54da3382010-08-30 18:46:211180
Howard Hinnant73d21a42010-09-04 23:28:191181#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501182 template <class _R1, class _Fp>
1183 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1184 template <class _R1, class _Fp>
1185 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:211186#else
Howard Hinnant99968442011-11-29 18:15:501187 template <class _R1, class _Fp>
1188 friend future<_R1> __make_deferred_assoc_state(_Fp __f);
1189 template <class _R1, class _Fp>
1190 friend future<_R1> __make_async_assoc_state(_Fp __f);
Howard Hinnant54da3382010-08-30 18:46:211191#endif
1192
Howard Hinnant47499b12010-08-27 20:10:191193public:
Howard Hinnant8c6cbb22010-09-22 14:16:261194 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551195 future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant73d21a42010-09-04 23:28:191196#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261197 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551198 future(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191199 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1200 future(const future&) = delete;
1201 future& operator=(const future&) = delete;
Howard Hinnant8c6cbb22010-09-22 14:16:261202 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551203 future& operator=(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191204 {
1205 future(std::move(__rhs)).swap(*this);
1206 return *this;
1207 }
Howard Hinnant73d21a42010-09-04 23:28:191208#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191209private:
1210 future(const future&);
1211 future& operator=(const future&);
1212public:
Howard Hinnant73d21a42010-09-04 23:28:191213#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191214 ~future();
Howard Hinnant99968442011-11-29 18:15:501215 shared_future<_Rp&> share();
Howard Hinnant47499b12010-08-27 20:10:191216
1217 // retrieving the value
Howard Hinnant99968442011-11-29 18:15:501218 _Rp& get();
Howard Hinnant47499b12010-08-27 20:10:191219
Howard Hinnant8c6cbb22010-09-22 14:16:261220 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551221 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:191222
1223 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:261224 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551225 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant47499b12010-08-27 20:10:191226
Howard Hinnant8c6cbb22010-09-22 14:16:261227 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191228 void wait() const {__state_->wait();}
1229 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:261230 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191231 future_status
1232 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1233 {return __state_->wait_for(__rel_time);}
1234 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:261235 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191236 future_status
1237 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1238 {return __state_->wait_until(__abs_time);}
1239};
1240
Howard Hinnant99968442011-11-29 18:15:501241template <class _Rp>
1242future<_Rp&>::future(__assoc_state<_Rp&>* __state)
Howard Hinnant47499b12010-08-27 20:10:191243 : __state_(__state)
1244{
Howard Hinnant22ba71b2011-07-13 16:00:501245#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:191246 if (__state_->__has_future_attached())
1247 throw future_error(make_error_code(future_errc::future_already_retrieved));
Howard Hinnant22ba71b2011-07-13 16:00:501248#endif
Howard Hinnant47499b12010-08-27 20:10:191249 __state_->__add_shared();
Howard Hinnant54da3382010-08-30 18:46:211250 __state_->__set_future_attached();
Howard Hinnant47499b12010-08-27 20:10:191251}
1252
Howard Hinnant99968442011-11-29 18:15:501253template <class _Rp>
1254future<_Rp&>::~future()
Howard Hinnant47499b12010-08-27 20:10:191255{
1256 if (__state_)
1257 __state_->__release_shared();
1258}
1259
Howard Hinnant99968442011-11-29 18:15:501260template <class _Rp>
1261_Rp&
1262future<_Rp&>::get()
Howard Hinnant47499b12010-08-27 20:10:191263{
Howard Hinnant54da3382010-08-30 18:46:211264 unique_ptr<__shared_count, __release_shared_count> __(__state_);
Howard Hinnant99968442011-11-29 18:15:501265 __assoc_state<_Rp&>* __s = __state_;
Howard Hinnant47499b12010-08-27 20:10:191266 __state_ = nullptr;
1267 return __s->copy();
1268}
1269
1270template <>
Howard Hinnant83eade62013-03-06 23:30:191271class _LIBCPP_TYPE_VIS future<void>
Howard Hinnant47499b12010-08-27 20:10:191272{
1273 __assoc_sub_state* __state_;
1274
1275 explicit future(__assoc_sub_state* __state);
1276
1277 template <class> friend class promise;
Howard Hinnant99be8232010-09-03 18:39:251278 template <class> friend class shared_future;
Howard Hinnant54da3382010-08-30 18:46:211279
Howard Hinnant73d21a42010-09-04 23:28:191280#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501281 template <class _R1, class _Fp>
1282 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1283 template <class _R1, class _Fp>
1284 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:211285#else
Howard Hinnant99968442011-11-29 18:15:501286 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:211290#endif
1291
Howard Hinnant47499b12010-08-27 20:10:191292public:
Howard Hinnant8c6cbb22010-09-22 14:16:261293 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551294 future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant73d21a42010-09-04 23:28:191295#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261296 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551297 future(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191298 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1299 future(const future&) = delete;
1300 future& operator=(const future&) = delete;
Howard Hinnant8c6cbb22010-09-22 14:16:261301 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551302 future& operator=(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191303 {
1304 future(std::move(__rhs)).swap(*this);
1305 return *this;
1306 }
Howard Hinnant73d21a42010-09-04 23:28:191307#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191308private:
1309 future(const future&);
1310 future& operator=(const future&);
1311public:
Howard Hinnant73d21a42010-09-04 23:28:191312#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191313 ~future();
Howard Hinnant7de47902010-11-30 20:23:321314 shared_future<void> share();
Howard Hinnant47499b12010-08-27 20:10:191315
1316 // retrieving the value
1317 void get();
1318
Howard Hinnant8c6cbb22010-09-22 14:16:261319 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551320 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:191321
1322 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:261323 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551324 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant47499b12010-08-27 20:10:191325
Howard Hinnant8c6cbb22010-09-22 14:16:261326 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191327 void wait() const {__state_->wait();}
1328 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:261329 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191330 future_status
1331 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1332 {return __state_->wait_for(__rel_time);}
1333 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:261334 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191335 future_status
1336 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1337 {return __state_->wait_until(__abs_time);}
1338};
1339
Howard Hinnant99968442011-11-29 18:15:501340template <class _Rp>
Howard Hinnant99be8232010-09-03 18:39:251341inline _LIBCPP_INLINE_VISIBILITY
1342void
Howard Hinnant8bf01dd2012-07-21 17:46:551343swap(future<_Rp>& __x, future<_Rp>& __y) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:251344{
1345 __x.swap(__y);
1346}
1347
Howard Hinnant47499b12010-08-27 20:10:191348// promise<R>
1349
Howard Hinnant2b1b2d42011-06-14 19:58:171350template <class _Callable> class packaged_task;
Howard Hinnant54da3382010-08-30 18:46:211351
Howard Hinnant99968442011-11-29 18:15:501352template <class _Rp>
Howard Hinnant0f678bd2013-08-12 18:38:341353class _LIBCPP_TYPE_VIS_ONLY promise
Howard Hinnant47499b12010-08-27 20:10:191354{
Howard Hinnant99968442011-11-29 18:15:501355 __assoc_state<_Rp>* __state_;
Howard Hinnant54da3382010-08-30 18:46:211356
Howard Hinnant8c6cbb22010-09-22 14:16:261357 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551358 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant54da3382010-08-30 18:46:211359
1360 template <class> friend class packaged_task;
Howard Hinnant47499b12010-08-27 20:10:191361public:
1362 promise();
1363 template <class _Alloc>
1364 promise(allocator_arg_t, const _Alloc& __a);
Howard Hinnant73d21a42010-09-04 23:28:191365#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261366 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551367 promise(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191368 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1369 promise(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:191370#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191371private:
1372 promise(const promise& __rhs);
1373public:
Howard Hinnant73d21a42010-09-04 23:28:191374#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191375 ~promise();
1376
1377 // assignment
Howard Hinnant73d21a42010-09-04 23:28:191378#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261379 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551380 promise& operator=(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191381 {
1382 promise(std::move(__rhs)).swap(*this);
1383 return *this;
1384 }
1385 promise& operator=(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:191386#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191387private:
1388 promise& operator=(const promise& __rhs);
1389public:
Howard Hinnant73d21a42010-09-04 23:28:191390#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261391 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551392 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:191393
1394 // retrieving the result
Howard Hinnant99968442011-11-29 18:15:501395 future<_Rp> get_future();
Howard Hinnant47499b12010-08-27 20:10:191396
1397 // setting the result
Howard Hinnant99968442011-11-29 18:15:501398 void set_value(const _Rp& __r);
Howard Hinnant73d21a42010-09-04 23:28:191399#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501400 void set_value(_Rp&& __r);
Howard Hinnant47499b12010-08-27 20:10:191401#endif
1402 void set_exception(exception_ptr __p);
1403
1404 // setting the result with deferred notification
Howard Hinnant99968442011-11-29 18:15:501405 void set_value_at_thread_exit(const _Rp& __r);
Howard Hinnant73d21a42010-09-04 23:28:191406#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501407 void set_value_at_thread_exit(_Rp&& __r);
Howard Hinnant47499b12010-08-27 20:10:191408#endif
1409 void set_exception_at_thread_exit(exception_ptr __p);
1410};
1411
Howard Hinnant99968442011-11-29 18:15:501412template <class _Rp>
1413promise<_Rp>::promise()
1414 : __state_(new __assoc_state<_Rp>)
Howard Hinnant47499b12010-08-27 20:10:191415{
1416}
1417
Howard Hinnant99968442011-11-29 18:15:501418template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191419template <class _Alloc>
Howard Hinnant99968442011-11-29 18:15:501420promise<_Rp>::promise(allocator_arg_t, const _Alloc& __a0)
Howard Hinnant47499b12010-08-27 20:10:191421{
Eric Fiselier4d2413c2014-10-23 06:24:451422 typedef __assoc_state_alloc<_Rp, _Alloc> _State;
1423 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
Howard Hinnant47499b12010-08-27 20:10:191424 typedef __allocator_destructor<_A2> _D2;
1425 _A2 __a(__a0);
Eric Fiselier4d2413c2014-10-23 06:24:451426 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1427 ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0);
1428 __state_ = _VSTD::addressof(*__hold.release());
Howard Hinnant47499b12010-08-27 20:10:191429}
1430
Howard Hinnant99968442011-11-29 18:15:501431template <class _Rp>
1432promise<_Rp>::~promise()
Howard Hinnant47499b12010-08-27 20:10:191433{
1434 if (__state_)
1435 {
1436 if (!__state_->__has_value() && __state_->use_count() > 1)
1437 __state_->set_exception(make_exception_ptr(
1438 future_error(make_error_code(future_errc::broken_promise))
1439 ));
1440 __state_->__release_shared();
1441 }
1442}
1443
Howard Hinnant99968442011-11-29 18:15:501444template <class _Rp>
1445future<_Rp>
1446promise<_Rp>::get_future()
Howard Hinnant47499b12010-08-27 20:10:191447{
Howard Hinnant22ba71b2011-07-13 16:00:501448#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:191449 if (__state_ == nullptr)
1450 throw future_error(make_error_code(future_errc::no_state));
Howard Hinnant22ba71b2011-07-13 16:00:501451#endif
Howard Hinnant99968442011-11-29 18:15:501452 return future<_Rp>(__state_);
Howard Hinnant47499b12010-08-27 20:10:191453}
1454
Howard Hinnant99968442011-11-29 18:15:501455template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191456void
Howard Hinnant99968442011-11-29 18:15:501457promise<_Rp>::set_value(const _Rp& __r)
Howard Hinnant47499b12010-08-27 20:10:191458{
Howard Hinnant22ba71b2011-07-13 16:00:501459#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:191460 if (__state_ == nullptr)
1461 throw future_error(make_error_code(future_errc::no_state));
Howard Hinnant22ba71b2011-07-13 16:00:501462#endif
Howard Hinnant47499b12010-08-27 20:10:191463 __state_->set_value(__r);
1464}
1465
Howard Hinnant73d21a42010-09-04 23:28:191466#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191467
Howard Hinnant99968442011-11-29 18:15:501468template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191469void
Howard Hinnant99968442011-11-29 18:15:501470promise<_Rp>::set_value(_Rp&& __r)
Howard Hinnant47499b12010-08-27 20:10:191471{
Howard Hinnant22ba71b2011-07-13 16:00:501472#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:191473 if (__state_ == nullptr)
1474 throw future_error(make_error_code(future_errc::no_state));
Howard Hinnant22ba71b2011-07-13 16:00:501475#endif
Howard Hinnant0949eed2011-06-30 21:18:191476 __state_->set_value(_VSTD::move(__r));
Howard Hinnant47499b12010-08-27 20:10:191477}
1478
Howard Hinnant73d21a42010-09-04 23:28:191479#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191480
Howard Hinnant99968442011-11-29 18:15:501481template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191482void
Howard Hinnant99968442011-11-29 18:15:501483promise<_Rp>::set_exception(exception_ptr __p)
Howard Hinnant47499b12010-08-27 20:10:191484{
Howard Hinnant22ba71b2011-07-13 16:00:501485#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:191486 if (__state_ == nullptr)
1487 throw future_error(make_error_code(future_errc::no_state));
Howard Hinnant22ba71b2011-07-13 16:00:501488#endif
Howard Hinnant47499b12010-08-27 20:10:191489 __state_->set_exception(__p);
1490}
1491
Howard Hinnant99968442011-11-29 18:15:501492template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191493void
Howard Hinnant99968442011-11-29 18:15:501494promise<_Rp>::set_value_at_thread_exit(const _Rp& __r)
Howard Hinnant47499b12010-08-27 20:10:191495{
Howard Hinnant22ba71b2011-07-13 16:00:501496#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:191497 if (__state_ == nullptr)
1498 throw future_error(make_error_code(future_errc::no_state));
Howard Hinnant22ba71b2011-07-13 16:00:501499#endif
Howard Hinnant47499b12010-08-27 20:10:191500 __state_->set_value_at_thread_exit(__r);
1501}
1502
Howard Hinnant73d21a42010-09-04 23:28:191503#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191504
Howard Hinnant99968442011-11-29 18:15:501505template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191506void
Howard Hinnant99968442011-11-29 18:15:501507promise<_Rp>::set_value_at_thread_exit(_Rp&& __r)
Howard Hinnant47499b12010-08-27 20:10:191508{
Howard Hinnant22ba71b2011-07-13 16:00:501509#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:191510 if (__state_ == nullptr)
1511 throw future_error(make_error_code(future_errc::no_state));
Howard Hinnant22ba71b2011-07-13 16:00:501512#endif
Howard Hinnant0949eed2011-06-30 21:18:191513 __state_->set_value_at_thread_exit(_VSTD::move(__r));
Howard Hinnant47499b12010-08-27 20:10:191514}
1515
Howard Hinnant73d21a42010-09-04 23:28:191516#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191517
Howard Hinnant99968442011-11-29 18:15:501518template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191519void
Howard Hinnant99968442011-11-29 18:15:501520promise<_Rp>::set_exception_at_thread_exit(exception_ptr __p)
Howard Hinnant47499b12010-08-27 20:10:191521{
Howard Hinnant22ba71b2011-07-13 16:00:501522#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:191523 if (__state_ == nullptr)
1524 throw future_error(make_error_code(future_errc::no_state));
Howard Hinnant22ba71b2011-07-13 16:00:501525#endif
Howard Hinnant47499b12010-08-27 20:10:191526 __state_->set_exception_at_thread_exit(__p);
1527}
1528
1529// promise<R&>
1530
Howard Hinnant99968442011-11-29 18:15:501531template <class _Rp>
Howard Hinnant0f678bd2013-08-12 18:38:341532class _LIBCPP_TYPE_VIS_ONLY promise<_Rp&>
Howard Hinnant47499b12010-08-27 20:10:191533{
Howard Hinnant99968442011-11-29 18:15:501534 __assoc_state<_Rp&>* __state_;
Howard Hinnant54da3382010-08-30 18:46:211535
Howard Hinnant8c6cbb22010-09-22 14:16:261536 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551537 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant54da3382010-08-30 18:46:211538
1539 template <class> friend class packaged_task;
1540
Howard Hinnant47499b12010-08-27 20:10:191541public:
1542 promise();
1543 template <class _Allocator>
1544 promise(allocator_arg_t, const _Allocator& __a);
Howard Hinnant73d21a42010-09-04 23:28:191545#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261546 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551547 promise(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191548 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1549 promise(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:191550#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191551private:
1552 promise(const promise& __rhs);
1553public:
Howard Hinnant73d21a42010-09-04 23:28:191554#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191555 ~promise();
1556
1557 // assignment
Howard Hinnant73d21a42010-09-04 23:28:191558#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261559 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551560 promise& operator=(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191561 {
1562 promise(std::move(__rhs)).swap(*this);
1563 return *this;
1564 }
1565 promise& operator=(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:191566#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191567private:
1568 promise& operator=(const promise& __rhs);
1569public:
Howard Hinnant73d21a42010-09-04 23:28:191570#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261571 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551572 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:191573
1574 // retrieving the result
Howard Hinnant99968442011-11-29 18:15:501575 future<_Rp&> get_future();
Howard Hinnant47499b12010-08-27 20:10:191576
1577 // setting the result
Howard Hinnant99968442011-11-29 18:15:501578 void set_value(_Rp& __r);
Howard Hinnant47499b12010-08-27 20:10:191579 void set_exception(exception_ptr __p);
1580
1581 // setting the result with deferred notification
Howard Hinnant99968442011-11-29 18:15:501582 void set_value_at_thread_exit(_Rp&);
Howard Hinnant47499b12010-08-27 20:10:191583 void set_exception_at_thread_exit(exception_ptr __p);
1584};
1585
Howard Hinnant99968442011-11-29 18:15:501586template <class _Rp>
1587promise<_Rp&>::promise()
1588 : __state_(new __assoc_state<_Rp&>)
Howard Hinnant47499b12010-08-27 20:10:191589{
1590}
1591
Howard Hinnant99968442011-11-29 18:15:501592template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191593template <class _Alloc>
Howard Hinnant99968442011-11-29 18:15:501594promise<_Rp&>::promise(allocator_arg_t, const _Alloc& __a0)
Howard Hinnant47499b12010-08-27 20:10:191595{
Eric Fiselier4d2413c2014-10-23 06:24:451596 typedef __assoc_state_alloc<_Rp&, _Alloc> _State;
1597 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
Howard Hinnant47499b12010-08-27 20:10:191598 typedef __allocator_destructor<_A2> _D2;
1599 _A2 __a(__a0);
Eric Fiselier4d2413c2014-10-23 06:24:451600 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1601 ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0);
1602 __state_ = _VSTD::addressof(*__hold.release());
Howard Hinnant47499b12010-08-27 20:10:191603}
1604
Howard Hinnant99968442011-11-29 18:15:501605template <class _Rp>
1606promise<_Rp&>::~promise()
Howard Hinnant47499b12010-08-27 20:10:191607{
1608 if (__state_)
1609 {
1610 if (!__state_->__has_value() && __state_->use_count() > 1)
1611 __state_->set_exception(make_exception_ptr(
1612 future_error(make_error_code(future_errc::broken_promise))
1613 ));
1614 __state_->__release_shared();
1615 }
1616}
1617
Howard Hinnant99968442011-11-29 18:15:501618template <class _Rp>
1619future<_Rp&>
1620promise<_Rp&>::get_future()
Howard Hinnant47499b12010-08-27 20:10:191621{
Howard Hinnant22ba71b2011-07-13 16:00:501622#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:191623 if (__state_ == nullptr)
1624 throw future_error(make_error_code(future_errc::no_state));
Howard Hinnant22ba71b2011-07-13 16:00:501625#endif
Howard Hinnant99968442011-11-29 18:15:501626 return future<_Rp&>(__state_);
Howard Hinnant47499b12010-08-27 20:10:191627}
1628
Howard Hinnant99968442011-11-29 18:15:501629template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191630void
Howard Hinnant99968442011-11-29 18:15:501631promise<_Rp&>::set_value(_Rp& __r)
Howard Hinnant47499b12010-08-27 20:10:191632{
Howard Hinnant22ba71b2011-07-13 16:00:501633#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:191634 if (__state_ == nullptr)
1635 throw future_error(make_error_code(future_errc::no_state));
Howard Hinnant22ba71b2011-07-13 16:00:501636#endif
Howard Hinnant47499b12010-08-27 20:10:191637 __state_->set_value(__r);
1638}
1639
Howard Hinnant99968442011-11-29 18:15:501640template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191641void
Howard Hinnant99968442011-11-29 18:15:501642promise<_Rp&>::set_exception(exception_ptr __p)
Howard Hinnant47499b12010-08-27 20:10:191643{
Howard Hinnant22ba71b2011-07-13 16:00:501644#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:191645 if (__state_ == nullptr)
1646 throw future_error(make_error_code(future_errc::no_state));
Howard Hinnant22ba71b2011-07-13 16:00:501647#endif
Howard Hinnant47499b12010-08-27 20:10:191648 __state_->set_exception(__p);
1649}
1650
Howard Hinnant99968442011-11-29 18:15:501651template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191652void
Howard Hinnant99968442011-11-29 18:15:501653promise<_Rp&>::set_value_at_thread_exit(_Rp& __r)
Howard Hinnant47499b12010-08-27 20:10:191654{
Howard Hinnant22ba71b2011-07-13 16:00:501655#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:191656 if (__state_ == nullptr)
1657 throw future_error(make_error_code(future_errc::no_state));
Howard Hinnant22ba71b2011-07-13 16:00:501658#endif
Howard Hinnant47499b12010-08-27 20:10:191659 __state_->set_value_at_thread_exit(__r);
1660}
1661
Howard Hinnant99968442011-11-29 18:15:501662template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191663void
Howard Hinnant99968442011-11-29 18:15:501664promise<_Rp&>::set_exception_at_thread_exit(exception_ptr __p)
Howard Hinnant47499b12010-08-27 20:10:191665{
Howard Hinnant22ba71b2011-07-13 16:00:501666#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:191667 if (__state_ == nullptr)
1668 throw future_error(make_error_code(future_errc::no_state));
Howard Hinnant22ba71b2011-07-13 16:00:501669#endif
Howard Hinnant47499b12010-08-27 20:10:191670 __state_->set_exception_at_thread_exit(__p);
1671}
1672
1673// promise<void>
1674
1675template <>
Howard Hinnant83eade62013-03-06 23:30:191676class _LIBCPP_TYPE_VIS promise<void>
Howard Hinnant47499b12010-08-27 20:10:191677{
1678 __assoc_sub_state* __state_;
Howard Hinnant54da3382010-08-30 18:46:211679
Howard Hinnant8c6cbb22010-09-22 14:16:261680 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551681 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant54da3382010-08-30 18:46:211682
1683 template <class> friend class packaged_task;
1684
Howard Hinnant47499b12010-08-27 20:10:191685public:
1686 promise();
1687 template <class _Allocator>
1688 promise(allocator_arg_t, const _Allocator& __a);
Howard Hinnant73d21a42010-09-04 23:28:191689#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261690 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551691 promise(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191692 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1693 promise(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:191694#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191695private:
1696 promise(const promise& __rhs);
1697public:
Howard Hinnant73d21a42010-09-04 23:28:191698#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191699 ~promise();
1700
1701 // assignment
Howard Hinnant73d21a42010-09-04 23:28:191702#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261703 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551704 promise& operator=(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191705 {
1706 promise(std::move(__rhs)).swap(*this);
1707 return *this;
1708 }
1709 promise& operator=(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:191710#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191711private:
1712 promise& operator=(const promise& __rhs);
1713public:
Howard Hinnant73d21a42010-09-04 23:28:191714#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261715 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551716 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:191717
1718 // retrieving the result
1719 future<void> get_future();
1720
1721 // setting the result
1722 void set_value();
1723 void set_exception(exception_ptr __p);
1724
1725 // setting the result with deferred notification
1726 void set_value_at_thread_exit();
1727 void set_exception_at_thread_exit(exception_ptr __p);
1728};
1729
1730template <class _Alloc>
1731promise<void>::promise(allocator_arg_t, const _Alloc& __a0)
1732{
Eric Fiselier4d2413c2014-10-23 06:24:451733 typedef __assoc_sub_state_alloc<_Alloc> _State;
1734 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
Howard Hinnant47499b12010-08-27 20:10:191735 typedef __allocator_destructor<_A2> _D2;
1736 _A2 __a(__a0);
Eric Fiselier4d2413c2014-10-23 06:24:451737 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1738 ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0);
1739 __state_ = _VSTD::addressof(*__hold.release());
Howard Hinnant47499b12010-08-27 20:10:191740}
1741
Howard Hinnant99968442011-11-29 18:15:501742template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191743inline _LIBCPP_INLINE_VISIBILITY
1744void
Howard Hinnant8bf01dd2012-07-21 17:46:551745swap(promise<_Rp>& __x, promise<_Rp>& __y) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191746{
1747 __x.swap(__y);
1748}
1749
Howard Hinnant99968442011-11-29 18:15:501750template <class _Rp, class _Alloc>
Howard Hinnant0f678bd2013-08-12 18:38:341751 struct _LIBCPP_TYPE_VIS_ONLY uses_allocator<promise<_Rp>, _Alloc>
Howard Hinnant8c6cbb22010-09-22 14:16:261752 : public true_type {};
Howard Hinnant47499b12010-08-27 20:10:191753
Howard Hinnant54da3382010-08-30 18:46:211754#ifndef _LIBCPP_HAS_NO_VARIADICS
1755
1756// packaged_task
1757
1758template<class _Fp> class __packaged_task_base;
1759
Howard Hinnant99968442011-11-29 18:15:501760template<class _Rp, class ..._ArgTypes>
1761class __packaged_task_base<_Rp(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:211762{
1763 __packaged_task_base(const __packaged_task_base&);
1764 __packaged_task_base& operator=(const __packaged_task_base&);
1765public:
Howard Hinnant8c6cbb22010-09-22 14:16:261766 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:211767 __packaged_task_base() {}
Howard Hinnant8c6cbb22010-09-22 14:16:261768 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:211769 virtual ~__packaged_task_base() {}
Howard Hinnant8bf01dd2012-07-21 17:46:551770 virtual void __move_to(__packaged_task_base*) _NOEXCEPT = 0;
Howard Hinnant54da3382010-08-30 18:46:211771 virtual void destroy() = 0;
1772 virtual void destroy_deallocate() = 0;
Howard Hinnant99968442011-11-29 18:15:501773 virtual _Rp operator()(_ArgTypes&& ...) = 0;
Howard Hinnant54da3382010-08-30 18:46:211774};
1775
1776template<class _FD, class _Alloc, class _FB> class __packaged_task_func;
1777
Howard Hinnant99968442011-11-29 18:15:501778template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1779class __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>
1780 : public __packaged_task_base<_Rp(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:211781{
Howard Hinnant99968442011-11-29 18:15:501782 __compressed_pair<_Fp, _Alloc> __f_;
Howard Hinnant54da3382010-08-30 18:46:211783public:
Howard Hinnant8c6cbb22010-09-22 14:16:261784 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:501785 explicit __packaged_task_func(const _Fp& __f) : __f_(__f) {}
Howard Hinnant8c6cbb22010-09-22 14:16:261786 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:501787 explicit __packaged_task_func(_Fp&& __f) : __f_(_VSTD::move(__f)) {}
Howard Hinnant8c6cbb22010-09-22 14:16:261788 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:501789 __packaged_task_func(const _Fp& __f, const _Alloc& __a)
Howard Hinnant54da3382010-08-30 18:46:211790 : __f_(__f, __a) {}
Howard Hinnant8c6cbb22010-09-22 14:16:261791 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:501792 __packaged_task_func(_Fp&& __f, const _Alloc& __a)
Howard Hinnant0949eed2011-06-30 21:18:191793 : __f_(_VSTD::move(__f), __a) {}
Howard Hinnant8bf01dd2012-07-21 17:46:551794 virtual void __move_to(__packaged_task_base<_Rp(_ArgTypes...)>*) _NOEXCEPT;
Howard Hinnant54da3382010-08-30 18:46:211795 virtual void destroy();
1796 virtual void destroy_deallocate();
Howard Hinnant99968442011-11-29 18:15:501797 virtual _Rp operator()(_ArgTypes&& ... __args);
Howard Hinnant54da3382010-08-30 18:46:211798};
1799
Howard Hinnant99968442011-11-29 18:15:501800template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:211801void
Howard Hinnant99968442011-11-29 18:15:501802__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__move_to(
Howard Hinnant8bf01dd2012-07-21 17:46:551803 __packaged_task_base<_Rp(_ArgTypes...)>* __p) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:211804{
Howard Hinnant0949eed2011-06-30 21:18:191805 ::new (__p) __packaged_task_func(_VSTD::move(__f_.first()), _VSTD::move(__f_.second()));
Howard Hinnant54da3382010-08-30 18:46:211806}
1807
Howard Hinnant99968442011-11-29 18:15:501808template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:211809void
Howard Hinnant99968442011-11-29 18:15:501810__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy()
Howard Hinnant54da3382010-08-30 18:46:211811{
Howard Hinnant99968442011-11-29 18:15:501812 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnant54da3382010-08-30 18:46:211813}
1814
Howard Hinnant99968442011-11-29 18:15:501815template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:211816void
Howard Hinnant99968442011-11-29 18:15:501817__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate()
Howard Hinnant54da3382010-08-30 18:46:211818{
Eric Fiselier4d2413c2014-10-23 06:24:451819 typedef typename __allocator_traits_rebind<_Alloc, __packaged_task_func>::type _Ap;
1820 typedef allocator_traits<_Ap> _ATraits;
1821 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Howard Hinnant99968442011-11-29 18:15:501822 _Ap __a(__f_.second());
1823 __f_.~__compressed_pair<_Fp, _Alloc>();
Eric Fiselier4d2413c2014-10-23 06:24:451824 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnant54da3382010-08-30 18:46:211825}
1826
Howard Hinnant99968442011-11-29 18:15:501827template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1828_Rp
1829__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
Howard Hinnant54da3382010-08-30 18:46:211830{
Howard Hinnant0949eed2011-06-30 21:18:191831 return __invoke(__f_.first(), _VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnant54da3382010-08-30 18:46:211832}
1833
Howard Hinnant2b1b2d42011-06-14 19:58:171834template <class _Callable> class __packaged_task_function;
Howard Hinnant54da3382010-08-30 18:46:211835
Howard Hinnant99968442011-11-29 18:15:501836template<class _Rp, class ..._ArgTypes>
1837class __packaged_task_function<_Rp(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:211838{
Howard Hinnant99968442011-11-29 18:15:501839 typedef __packaged_task_base<_Rp(_ArgTypes...)> __base;
Howard Hinnant78f0de22013-01-21 17:26:551840 typename aligned_storage<3*sizeof(void*)>::type __buf_;
Howard Hinnant54da3382010-08-30 18:46:211841 __base* __f_;
1842
1843public:
Howard Hinnant99968442011-11-29 18:15:501844 typedef _Rp result_type;
Howard Hinnant54da3382010-08-30 18:46:211845
1846 // construct/copy/destroy:
Howard Hinnant8c6cbb22010-09-22 14:16:261847 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551848 __packaged_task_function() _NOEXCEPT : __f_(nullptr) {}
Howard Hinnant99968442011-11-29 18:15:501849 template<class _Fp>
1850 __packaged_task_function(_Fp&& __f);
1851 template<class _Fp, class _Alloc>
1852 __packaged_task_function(allocator_arg_t, const _Alloc& __a, _Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:211853
Howard Hinnant8bf01dd2012-07-21 17:46:551854 __packaged_task_function(__packaged_task_function&&) _NOEXCEPT;
1855 __packaged_task_function& operator=(__packaged_task_function&&) _NOEXCEPT;
Howard Hinnant54da3382010-08-30 18:46:211856
1857 __packaged_task_function(const __packaged_task_function&) = delete;
1858 __packaged_task_function& operator=(const __packaged_task_function&) = delete;
1859
1860 ~__packaged_task_function();
1861
Howard Hinnant8bf01dd2012-07-21 17:46:551862 void swap(__packaged_task_function&) _NOEXCEPT;
Howard Hinnant54da3382010-08-30 18:46:211863
Howard Hinnant99968442011-11-29 18:15:501864 _Rp operator()(_ArgTypes...) const;
Howard Hinnant54da3382010-08-30 18:46:211865};
1866
Howard Hinnant99968442011-11-29 18:15:501867template<class _Rp, class ..._ArgTypes>
Howard Hinnant8bf01dd2012-07-21 17:46:551868__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:211869{
1870 if (__f.__f_ == nullptr)
1871 __f_ = nullptr;
1872 else if (__f.__f_ == (__base*)&__f.__buf_)
1873 {
1874 __f_ = (__base*)&__buf_;
1875 __f.__f_->__move_to(__f_);
1876 }
1877 else
1878 {
1879 __f_ = __f.__f_;
1880 __f.__f_ = nullptr;
1881 }
1882}
1883
Howard Hinnant99968442011-11-29 18:15:501884template<class _Rp, class ..._ArgTypes>
1885template <class _Fp>
1886__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(_Fp&& __f)
Howard Hinnant54da3382010-08-30 18:46:211887 : __f_(nullptr)
1888{
Marshall Clowf1264e72014-04-07 13:32:261889 typedef typename remove_reference<typename decay<_Fp>::type>::type _FR;
Howard Hinnant99968442011-11-29 18:15:501890 typedef __packaged_task_func<_FR, allocator<_FR>, _Rp(_ArgTypes...)> _FF;
Howard Hinnant54da3382010-08-30 18:46:211891 if (sizeof(_FF) <= sizeof(__buf_))
1892 {
1893 __f_ = (__base*)&__buf_;
Howard Hinnant99968442011-11-29 18:15:501894 ::new (__f_) _FF(_VSTD::forward<_Fp>(__f));
Howard Hinnant54da3382010-08-30 18:46:211895 }
1896 else
1897 {
Howard Hinnant99968442011-11-29 18:15:501898 typedef allocator<_FF> _Ap;
1899 _Ap __a;
1900 typedef __allocator_destructor<_Ap> _Dp;
1901 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1902 ::new (__hold.get()) _FF(_VSTD::forward<_Fp>(__f), allocator<_FR>(__a));
Howard Hinnant54da3382010-08-30 18:46:211903 __f_ = __hold.release();
1904 }
1905}
1906
Howard Hinnant99968442011-11-29 18:15:501907template<class _Rp, class ..._ArgTypes>
1908template <class _Fp, class _Alloc>
1909__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(
1910 allocator_arg_t, const _Alloc& __a0, _Fp&& __f)
Howard Hinnant54da3382010-08-30 18:46:211911 : __f_(nullptr)
1912{
Marshall Clowf1264e72014-04-07 13:32:261913 typedef typename remove_reference<typename decay<_Fp>::type>::type _FR;
Howard Hinnant99968442011-11-29 18:15:501914 typedef __packaged_task_func<_FR, _Alloc, _Rp(_ArgTypes...)> _FF;
Howard Hinnant54da3382010-08-30 18:46:211915 if (sizeof(_FF) <= sizeof(__buf_))
1916 {
1917 __f_ = (__base*)&__buf_;
Howard Hinnant99968442011-11-29 18:15:501918 ::new (__f_) _FF(_VSTD::forward<_Fp>(__f));
Howard Hinnant54da3382010-08-30 18:46:211919 }
1920 else
1921 {
Eric Fiselier4d2413c2014-10-23 06:24:451922 typedef typename __allocator_traits_rebind<_Alloc, _FF>::type _Ap;
Howard Hinnant99968442011-11-29 18:15:501923 _Ap __a(__a0);
1924 typedef __allocator_destructor<_Ap> _Dp;
1925 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Eric Fiselier4d2413c2014-10-23 06:24:451926 ::new (static_cast<void*>(_VSTD::addressof(*__hold.get())))
1927 _FF(_VSTD::forward<_Fp>(__f), _Alloc(__a));
1928 __f_ = _VSTD::addressof(*__hold.release());
Howard Hinnant54da3382010-08-30 18:46:211929 }
1930}
1931
Howard Hinnant99968442011-11-29 18:15:501932template<class _Rp, class ..._ArgTypes>
1933__packaged_task_function<_Rp(_ArgTypes...)>&
Howard Hinnant8bf01dd2012-07-21 17:46:551934__packaged_task_function<_Rp(_ArgTypes...)>::operator=(__packaged_task_function&& __f) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:211935{
1936 if (__f_ == (__base*)&__buf_)
1937 __f_->destroy();
1938 else if (__f_)
1939 __f_->destroy_deallocate();
1940 __f_ = nullptr;
1941 if (__f.__f_ == nullptr)
1942 __f_ = nullptr;
1943 else if (__f.__f_ == (__base*)&__f.__buf_)
1944 {
1945 __f_ = (__base*)&__buf_;
1946 __f.__f_->__move_to(__f_);
1947 }
1948 else
1949 {
1950 __f_ = __f.__f_;
1951 __f.__f_ = nullptr;
1952 }
Argyrios Kyrtzidis1dc6f7a2012-10-13 02:03:451953 return *this;
Howard Hinnant54da3382010-08-30 18:46:211954}
1955
Howard Hinnant99968442011-11-29 18:15:501956template<class _Rp, class ..._ArgTypes>
1957__packaged_task_function<_Rp(_ArgTypes...)>::~__packaged_task_function()
Howard Hinnant54da3382010-08-30 18:46:211958{
1959 if (__f_ == (__base*)&__buf_)
1960 __f_->destroy();
1961 else if (__f_)
1962 __f_->destroy_deallocate();
1963}
1964
Howard Hinnant99968442011-11-29 18:15:501965template<class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:211966void
Howard Hinnant8bf01dd2012-07-21 17:46:551967__packaged_task_function<_Rp(_ArgTypes...)>::swap(__packaged_task_function& __f) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:211968{
1969 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1970 {
1971 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1972 __base* __t = (__base*)&__tempbuf;
1973 __f_->__move_to(__t);
1974 __f_->destroy();
1975 __f_ = nullptr;
1976 __f.__f_->__move_to((__base*)&__buf_);
1977 __f.__f_->destroy();
1978 __f.__f_ = nullptr;
1979 __f_ = (__base*)&__buf_;
1980 __t->__move_to((__base*)&__f.__buf_);
1981 __t->destroy();
1982 __f.__f_ = (__base*)&__f.__buf_;
1983 }
1984 else if (__f_ == (__base*)&__buf_)
1985 {
1986 __f_->__move_to((__base*)&__f.__buf_);
1987 __f_->destroy();
1988 __f_ = __f.__f_;
1989 __f.__f_ = (__base*)&__f.__buf_;
1990 }
1991 else if (__f.__f_ == (__base*)&__f.__buf_)
1992 {
1993 __f.__f_->__move_to((__base*)&__buf_);
1994 __f.__f_->destroy();
1995 __f.__f_ = __f_;
1996 __f_ = (__base*)&__buf_;
1997 }
1998 else
Howard Hinnant0949eed2011-06-30 21:18:191999 _VSTD::swap(__f_, __f.__f_);
Howard Hinnant54da3382010-08-30 18:46:212000}
2001
Howard Hinnant99968442011-11-29 18:15:502002template<class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:212003inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502004_Rp
2005__packaged_task_function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
Howard Hinnant54da3382010-08-30 18:46:212006{
Howard Hinnant0949eed2011-06-30 21:18:192007 return (*__f_)(_VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnant54da3382010-08-30 18:46:212008}
2009
Howard Hinnant99968442011-11-29 18:15:502010template<class _Rp, class ..._ArgTypes>
Howard Hinnant0f678bd2013-08-12 18:38:342011class _LIBCPP_TYPE_VIS_ONLY packaged_task<_Rp(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:212012{
2013public:
Howard Hinnant99968442011-11-29 18:15:502014 typedef _Rp result_type;
Howard Hinnant54da3382010-08-30 18:46:212015
2016private:
2017 __packaged_task_function<result_type(_ArgTypes...)> __f_;
2018 promise<result_type> __p_;
2019
2020public:
2021 // construction and destruction
Howard Hinnant8c6cbb22010-09-22 14:16:262022 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552023 packaged_task() _NOEXCEPT : __p_(nullptr) {}
Marshall Clow5f2d5b92013-10-12 22:49:172024 template <class _Fp,
2025 class = typename enable_if
2026 <
2027 !is_same<
2028 typename decay<_Fp>::type,
2029 packaged_task
2030 >::value
2031 >::type
2032 >
Howard Hinnant8c6cbb22010-09-22 14:16:262033 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502034 explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
Marshall Clow5f2d5b92013-10-12 22:49:172035 template <class _Fp, class _Allocator,
2036 class = typename enable_if
2037 <
2038 !is_same<
2039 typename decay<_Fp>::type,
2040 packaged_task
2041 >::value
2042 >::type
2043 >
Howard Hinnant8c6cbb22010-09-22 14:16:262044 _LIBCPP_INLINE_VISIBILITY
Marshall Clow07546f32015-06-30 14:16:492045 packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
Howard Hinnant99968442011-11-29 18:15:502046 : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)),
Howard Hinnant54da3382010-08-30 18:46:212047 __p_(allocator_arg, __a) {}
2048 // ~packaged_task() = default;
2049
2050 // no copy
Howard Hinnant8131a012012-07-21 19:34:122051 packaged_task(const packaged_task&) = delete;
2052 packaged_task& operator=(const packaged_task&) = delete;
Howard Hinnant54da3382010-08-30 18:46:212053
2054 // move support
Howard Hinnant8c6cbb22010-09-22 14:16:262055 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552056 packaged_task(packaged_task&& __other) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:192057 : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
Howard Hinnant8c6cbb22010-09-22 14:16:262058 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552059 packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:212060 {
Howard Hinnant0949eed2011-06-30 21:18:192061 __f_ = _VSTD::move(__other.__f_);
2062 __p_ = _VSTD::move(__other.__p_);
Howard Hinnant54da3382010-08-30 18:46:212063 return *this;
2064 }
Howard Hinnant8c6cbb22010-09-22 14:16:262065 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552066 void swap(packaged_task& __other) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:212067 {
2068 __f_.swap(__other.__f_);
2069 __p_.swap(__other.__p_);
2070 }
2071
Howard Hinnant8c6cbb22010-09-22 14:16:262072 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552073 bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
Howard Hinnant54da3382010-08-30 18:46:212074
2075 // result retrieval
Howard Hinnant8c6cbb22010-09-22 14:16:262076 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:212077 future<result_type> get_future() {return __p_.get_future();}
2078
2079 // execution
2080 void operator()(_ArgTypes... __args);
2081 void make_ready_at_thread_exit(_ArgTypes... __args);
2082
2083 void reset();
2084};
2085
Howard Hinnant99968442011-11-29 18:15:502086template<class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:212087void
Howard Hinnant99968442011-11-29 18:15:502088packaged_task<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __args)
Howard Hinnant54da3382010-08-30 18:46:212089{
2090#ifndef _LIBCPP_NO_EXCEPTIONS
2091 if (__p_.__state_ == nullptr)
2092 throw future_error(make_error_code(future_errc::no_state));
2093 if (__p_.__state_->__has_value())
2094 throw future_error(make_error_code(future_errc::promise_already_satisfied));
2095 try
2096 {
2097#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:192098 __p_.set_value(__f_(_VSTD::forward<_ArgTypes>(__args)...));
Howard Hinnant54da3382010-08-30 18:46:212099#ifndef _LIBCPP_NO_EXCEPTIONS
2100 }
2101 catch (...)
2102 {
2103 __p_.set_exception(current_exception());
2104 }
2105#endif // _LIBCPP_NO_EXCEPTIONS
2106}
2107
Howard Hinnant99968442011-11-29 18:15:502108template<class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:212109void
Howard Hinnant99968442011-11-29 18:15:502110packaged_task<_Rp(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
Howard Hinnant54da3382010-08-30 18:46:212111{
2112#ifndef _LIBCPP_NO_EXCEPTIONS
2113 if (__p_.__state_ == nullptr)
2114 throw future_error(make_error_code(future_errc::no_state));
2115 if (__p_.__state_->__has_value())
2116 throw future_error(make_error_code(future_errc::promise_already_satisfied));
2117 try
2118 {
2119#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:192120 __p_.set_value_at_thread_exit(__f_(_VSTD::forward<_ArgTypes>(__args)...));
Howard Hinnant54da3382010-08-30 18:46:212121#ifndef _LIBCPP_NO_EXCEPTIONS
2122 }
2123 catch (...)
2124 {
2125 __p_.set_exception_at_thread_exit(current_exception());
2126 }
2127#endif // _LIBCPP_NO_EXCEPTIONS
2128}
2129
Howard Hinnant99968442011-11-29 18:15:502130template<class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:212131void
Howard Hinnant99968442011-11-29 18:15:502132packaged_task<_Rp(_ArgTypes...)>::reset()
Howard Hinnant54da3382010-08-30 18:46:212133{
2134#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant7de47902010-11-30 20:23:322135 if (!valid())
Howard Hinnant54da3382010-08-30 18:46:212136 throw future_error(make_error_code(future_errc::no_state));
2137#endif // _LIBCPP_NO_EXCEPTIONS
2138 __p_ = promise<result_type>();
2139}
2140
2141template<class ..._ArgTypes>
Howard Hinnant0f678bd2013-08-12 18:38:342142class _LIBCPP_TYPE_VIS_ONLY packaged_task<void(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:212143{
2144public:
2145 typedef void result_type;
2146
2147private:
2148 __packaged_task_function<result_type(_ArgTypes...)> __f_;
2149 promise<result_type> __p_;
2150
2151public:
2152 // construction and destruction
Howard Hinnant8c6cbb22010-09-22 14:16:262153 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552154 packaged_task() _NOEXCEPT : __p_(nullptr) {}
Marshall Clow5f2d5b92013-10-12 22:49:172155 template <class _Fp,
2156 class = typename enable_if
2157 <
2158 !is_same<
2159 typename decay<_Fp>::type,
2160 packaged_task
2161 >::value
2162 >::type
2163 >
Howard Hinnant8c6cbb22010-09-22 14:16:262164 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502165 explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
Marshall Clow5f2d5b92013-10-12 22:49:172166 template <class _Fp, class _Allocator,
2167 class = typename enable_if
2168 <
2169 !is_same<
2170 typename decay<_Fp>::type,
2171 packaged_task
2172 >::value
2173 >::type
2174 >
Howard Hinnant8c6cbb22010-09-22 14:16:262175 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502176 explicit packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
2177 : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)),
Howard Hinnant54da3382010-08-30 18:46:212178 __p_(allocator_arg, __a) {}
2179 // ~packaged_task() = default;
2180
2181 // no copy
Howard Hinnant8131a012012-07-21 19:34:122182 packaged_task(const packaged_task&) = delete;
2183 packaged_task& operator=(const packaged_task&) = delete;
Howard Hinnant54da3382010-08-30 18:46:212184
2185 // move support
Howard Hinnant8c6cbb22010-09-22 14:16:262186 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552187 packaged_task(packaged_task&& __other) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:192188 : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
Howard Hinnant8c6cbb22010-09-22 14:16:262189 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552190 packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:212191 {
Howard Hinnant0949eed2011-06-30 21:18:192192 __f_ = _VSTD::move(__other.__f_);
2193 __p_ = _VSTD::move(__other.__p_);
Howard Hinnant54da3382010-08-30 18:46:212194 return *this;
2195 }
Howard Hinnant8c6cbb22010-09-22 14:16:262196 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552197 void swap(packaged_task& __other) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:212198 {
2199 __f_.swap(__other.__f_);
2200 __p_.swap(__other.__p_);
2201 }
2202
Howard Hinnant8c6cbb22010-09-22 14:16:262203 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552204 bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
Howard Hinnant54da3382010-08-30 18:46:212205
2206 // result retrieval
Howard Hinnant8c6cbb22010-09-22 14:16:262207 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:212208 future<result_type> get_future() {return __p_.get_future();}
2209
2210 // execution
2211 void operator()(_ArgTypes... __args);
2212 void make_ready_at_thread_exit(_ArgTypes... __args);
2213
2214 void reset();
2215};
2216
2217template<class ..._ArgTypes>
2218void
2219packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args)
2220{
2221#ifndef _LIBCPP_NO_EXCEPTIONS
2222 if (__p_.__state_ == nullptr)
2223 throw future_error(make_error_code(future_errc::no_state));
2224 if (__p_.__state_->__has_value())
2225 throw future_error(make_error_code(future_errc::promise_already_satisfied));
2226 try
2227 {
2228#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:192229 __f_(_VSTD::forward<_ArgTypes>(__args)...);
Howard Hinnant54da3382010-08-30 18:46:212230 __p_.set_value();
2231#ifndef _LIBCPP_NO_EXCEPTIONS
2232 }
2233 catch (...)
2234 {
2235 __p_.set_exception(current_exception());
2236 }
2237#endif // _LIBCPP_NO_EXCEPTIONS
2238}
2239
2240template<class ..._ArgTypes>
2241void
2242packaged_task<void(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
2243{
2244#ifndef _LIBCPP_NO_EXCEPTIONS
2245 if (__p_.__state_ == nullptr)
2246 throw future_error(make_error_code(future_errc::no_state));
2247 if (__p_.__state_->__has_value())
2248 throw future_error(make_error_code(future_errc::promise_already_satisfied));
2249 try
2250 {
2251#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:192252 __f_(_VSTD::forward<_ArgTypes>(__args)...);
Howard Hinnant54da3382010-08-30 18:46:212253 __p_.set_value_at_thread_exit();
2254#ifndef _LIBCPP_NO_EXCEPTIONS
2255 }
2256 catch (...)
2257 {
2258 __p_.set_exception_at_thread_exit(current_exception());
2259 }
2260#endif // _LIBCPP_NO_EXCEPTIONS
2261}
2262
2263template<class ..._ArgTypes>
2264void
2265packaged_task<void(_ArgTypes...)>::reset()
2266{
2267#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant7de47902010-11-30 20:23:322268 if (!valid())
Howard Hinnant54da3382010-08-30 18:46:212269 throw future_error(make_error_code(future_errc::no_state));
2270#endif // _LIBCPP_NO_EXCEPTIONS
2271 __p_ = promise<result_type>();
2272}
2273
2274template <class _Callable>
2275inline _LIBCPP_INLINE_VISIBILITY
2276void
Howard Hinnant8bf01dd2012-07-21 17:46:552277swap(packaged_task<_Callable>& __x, packaged_task<_Callable>& __y) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:212278{
2279 __x.swap(__y);
2280}
2281
2282template <class _Callable, class _Alloc>
Howard Hinnant0f678bd2013-08-12 18:38:342283struct _LIBCPP_TYPE_VIS_ONLY uses_allocator<packaged_task<_Callable>, _Alloc>
Howard Hinnant8c6cbb22010-09-22 14:16:262284 : public true_type {};
Howard Hinnant54da3382010-08-30 18:46:212285
Howard Hinnant99968442011-11-29 18:15:502286template <class _Rp, class _Fp>
2287future<_Rp>
Howard Hinnant73d21a42010-09-04 23:28:192288#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:502289__make_deferred_assoc_state(_Fp&& __f)
Howard Hinnant54da3382010-08-30 18:46:212290#else
Howard Hinnant99968442011-11-29 18:15:502291__make_deferred_assoc_state(_Fp __f)
Howard Hinnant54da3382010-08-30 18:46:212292#endif
2293{
Howard Hinnant99968442011-11-29 18:15:502294 unique_ptr<__deferred_assoc_state<_Rp, _Fp>, __release_shared_count>
2295 __h(new __deferred_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
2296 return future<_Rp>(__h.get());
Howard Hinnant54da3382010-08-30 18:46:212297}
2298
Howard Hinnant99968442011-11-29 18:15:502299template <class _Rp, class _Fp>
2300future<_Rp>
Howard Hinnant57cff292011-05-19 15:05:042301#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:502302__make_async_assoc_state(_Fp&& __f)
Howard Hinnant57cff292011-05-19 15:05:042303#else
Howard Hinnant99968442011-11-29 18:15:502304__make_async_assoc_state(_Fp __f)
Howard Hinnant57cff292011-05-19 15:05:042305#endif
2306{
Howard Hinnant99968442011-11-29 18:15:502307 unique_ptr<__async_assoc_state<_Rp, _Fp>, __release_shared_count>
2308 __h(new __async_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
2309 _VSTD::thread(&__async_assoc_state<_Rp, _Fp>::__execute, __h.get()).detach();
2310 return future<_Rp>(__h.get());
Howard Hinnant57cff292011-05-19 15:05:042311}
2312
Howard Hinnant99968442011-11-29 18:15:502313template <class _Fp, class... _Args>
Howard Hinnant57cff292011-05-19 15:05:042314class __async_func
2315{
Howard Hinnant99968442011-11-29 18:15:502316 tuple<_Fp, _Args...> __f_;
Howard Hinnant57cff292011-05-19 15:05:042317
2318public:
Howard Hinnant99968442011-11-29 18:15:502319 typedef typename __invoke_of<_Fp, _Args...>::type _Rp;
Howard Hinnant57cff292011-05-19 15:05:042320
2321 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502322 explicit __async_func(_Fp&& __f, _Args&&... __args)
Howard Hinnant0949eed2011-06-30 21:18:192323 : __f_(_VSTD::move(__f), _VSTD::move(__args)...) {}
Howard Hinnant57cff292011-05-19 15:05:042324
2325 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0949eed2011-06-30 21:18:192326 __async_func(__async_func&& __f) : __f_(_VSTD::move(__f.__f_)) {}
Howard Hinnant57cff292011-05-19 15:05:042327
Howard Hinnant99968442011-11-29 18:15:502328 _Rp operator()()
Howard Hinnant57cff292011-05-19 15:05:042329 {
2330 typedef typename __make_tuple_indices<1+sizeof...(_Args), 1>::type _Index;
2331 return __execute(_Index());
2332 }
2333private:
2334 template <size_t ..._Indices>
Howard Hinnant99968442011-11-29 18:15:502335 _Rp
Howard Hinnant57cff292011-05-19 15:05:042336 __execute(__tuple_indices<_Indices...>)
2337 {
Howard Hinnant0949eed2011-06-30 21:18:192338 return __invoke(_VSTD::move(_VSTD::get<0>(__f_)), _VSTD::move(_VSTD::get<_Indices>(__f_))...);
Howard Hinnant57cff292011-05-19 15:05:042339 }
2340};
2341
Marshall Clow3b3108e2013-11-03 22:06:532342inline _LIBCPP_INLINE_VISIBILITY bool __does_policy_contain(launch __policy, launch __value )
Marshall Clowad2a6002013-11-03 15:43:352343{ return (int(__policy) & int(__value)) != 0; }
2344
Howard Hinnant99968442011-11-29 18:15:502345template <class _Fp, class... _Args>
2346future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
2347async(launch __policy, _Fp&& __f, _Args&&... __args)
Howard Hinnant54da3382010-08-30 18:46:212348{
Howard Hinnant99968442011-11-29 18:15:502349 typedef __async_func<typename decay<_Fp>::type, typename decay<_Args>::type...> _BF;
2350 typedef typename _BF::_Rp _Rp;
Marshall Clowad2a6002013-11-03 15:43:352351
2352#ifndef _LIBCPP_NO_EXCEPTIONS
2353 try
2354 {
2355#endif
2356 if (__does_policy_contain(__policy, launch::async))
2357 return _VSTD::__make_async_assoc_state<_Rp>(_BF(__decay_copy(_VSTD::forward<_Fp>(__f)),
Howard Hinnant0949eed2011-06-30 21:18:192358 __decay_copy(_VSTD::forward<_Args>(__args))...));
Marshall Clowad2a6002013-11-03 15:43:352359#ifndef _LIBCPP_NO_EXCEPTIONS
2360 }
2361 catch ( ... ) { if (__policy == launch::async) throw ; }
2362#endif
2363
2364 if (__does_policy_contain(__policy, launch::deferred))
2365 return _VSTD::__make_deferred_assoc_state<_Rp>(_BF(__decay_copy(_VSTD::forward<_Fp>(__f)),
Howard Hinnant0949eed2011-06-30 21:18:192366 __decay_copy(_VSTD::forward<_Args>(__args))...));
Marshall Clowad2a6002013-11-03 15:43:352367 return future<_Rp>{};
Howard Hinnant54da3382010-08-30 18:46:212368}
2369
Howard Hinnant99968442011-11-29 18:15:502370template <class _Fp, class... _Args>
Howard Hinnant54da3382010-08-30 18:46:212371inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502372future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
2373async(_Fp&& __f, _Args&&... __args)
Howard Hinnant54da3382010-08-30 18:46:212374{
Howard Hinnant99968442011-11-29 18:15:502375 return _VSTD::async(launch::any, _VSTD::forward<_Fp>(__f),
Howard Hinnant0949eed2011-06-30 21:18:192376 _VSTD::forward<_Args>(__args)...);
Howard Hinnant54da3382010-08-30 18:46:212377}
2378
2379#endif // _LIBCPP_HAS_NO_VARIADICS
2380
Howard Hinnante6e4d012010-09-03 21:46:372381// shared_future
2382
Howard Hinnant99968442011-11-29 18:15:502383template <class _Rp>
Howard Hinnant0f678bd2013-08-12 18:38:342384class _LIBCPP_TYPE_VIS_ONLY shared_future
Howard Hinnant99be8232010-09-03 18:39:252385{
Howard Hinnant99968442011-11-29 18:15:502386 __assoc_state<_Rp>* __state_;
Howard Hinnant99be8232010-09-03 18:39:252387
2388public:
Howard Hinnant8c6cbb22010-09-22 14:16:262389 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552390 shared_future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant8c6cbb22010-09-22 14:16:262391 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252392 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2393 {if (__state_) __state_->__add_shared();}
Howard Hinnant73d21a42010-09-04 23:28:192394#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:262395 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552396 shared_future(future<_Rp>&& __f) _NOEXCEPT : __state_(__f.__state_)
Howard Hinnant99be8232010-09-03 18:39:252397 {__f.__state_ = nullptr;}
Howard Hinnant8c6cbb22010-09-22 14:16:262398 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552399 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
Howard Hinnant99be8232010-09-03 18:39:252400 {__rhs.__state_ = nullptr;}
Howard Hinnant73d21a42010-09-04 23:28:192401#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:252402 ~shared_future();
2403 shared_future& operator=(const shared_future& __rhs);
Howard Hinnant73d21a42010-09-04 23:28:192404#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:262405 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552406 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:252407 {
2408 shared_future(std::move(__rhs)).swap(*this);
2409 return *this;
2410 }
Howard Hinnant73d21a42010-09-04 23:28:192411#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:252412
2413 // retrieving the value
Howard Hinnant8c6cbb22010-09-22 14:16:262414 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502415 const _Rp& get() const {return __state_->copy();}
Howard Hinnant99be8232010-09-03 18:39:252416
Howard Hinnant8c6cbb22010-09-22 14:16:262417 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552418 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant99be8232010-09-03 18:39:252419
2420 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:262421 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552422 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant99be8232010-09-03 18:39:252423
Howard Hinnant8c6cbb22010-09-22 14:16:262424 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252425 void wait() const {__state_->wait();}
2426 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:262427 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252428 future_status
2429 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2430 {return __state_->wait_for(__rel_time);}
2431 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:262432 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252433 future_status
2434 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2435 {return __state_->wait_until(__abs_time);}
2436};
2437
Howard Hinnant99968442011-11-29 18:15:502438template <class _Rp>
2439shared_future<_Rp>::~shared_future()
Howard Hinnant99be8232010-09-03 18:39:252440{
2441 if (__state_)
2442 __state_->__release_shared();
2443}
2444
Howard Hinnant99968442011-11-29 18:15:502445template <class _Rp>
2446shared_future<_Rp>&
2447shared_future<_Rp>::operator=(const shared_future& __rhs)
Howard Hinnant99be8232010-09-03 18:39:252448{
2449 if (__rhs.__state_)
2450 __rhs.__state_->__add_shared();
2451 if (__state_)
2452 __state_->__release_shared();
2453 __state_ = __rhs.__state_;
2454 return *this;
2455}
2456
Howard Hinnant99968442011-11-29 18:15:502457template <class _Rp>
Howard Hinnant0f678bd2013-08-12 18:38:342458class _LIBCPP_TYPE_VIS_ONLY shared_future<_Rp&>
Howard Hinnant99be8232010-09-03 18:39:252459{
Howard Hinnant99968442011-11-29 18:15:502460 __assoc_state<_Rp&>* __state_;
Howard Hinnant99be8232010-09-03 18:39:252461
2462public:
Howard Hinnant8c6cbb22010-09-22 14:16:262463 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552464 shared_future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant8c6cbb22010-09-22 14:16:262465 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252466 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2467 {if (__state_) __state_->__add_shared();}
Howard Hinnant73d21a42010-09-04 23:28:192468#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:262469 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552470 shared_future(future<_Rp&>&& __f) _NOEXCEPT : __state_(__f.__state_)
Howard Hinnant99be8232010-09-03 18:39:252471 {__f.__state_ = nullptr;}
Howard Hinnant8c6cbb22010-09-22 14:16:262472 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552473 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
Howard Hinnant99be8232010-09-03 18:39:252474 {__rhs.__state_ = nullptr;}
Howard Hinnant73d21a42010-09-04 23:28:192475#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:252476 ~shared_future();
2477 shared_future& operator=(const shared_future& __rhs);
Howard Hinnant73d21a42010-09-04 23:28:192478#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:262479 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552480 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:252481 {
2482 shared_future(std::move(__rhs)).swap(*this);
2483 return *this;
2484 }
Howard Hinnant73d21a42010-09-04 23:28:192485#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:252486
2487 // retrieving the value
Howard Hinnant8c6cbb22010-09-22 14:16:262488 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502489 _Rp& get() const {return __state_->copy();}
Howard Hinnant99be8232010-09-03 18:39:252490
Howard Hinnant8c6cbb22010-09-22 14:16:262491 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552492 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant99be8232010-09-03 18:39:252493
2494 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:262495 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552496 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant99be8232010-09-03 18:39:252497
Howard Hinnant8c6cbb22010-09-22 14:16:262498 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252499 void wait() const {__state_->wait();}
2500 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:262501 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252502 future_status
2503 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2504 {return __state_->wait_for(__rel_time);}
2505 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:262506 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252507 future_status
2508 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2509 {return __state_->wait_until(__abs_time);}
2510};
2511
Howard Hinnant99968442011-11-29 18:15:502512template <class _Rp>
2513shared_future<_Rp&>::~shared_future()
Howard Hinnant99be8232010-09-03 18:39:252514{
2515 if (__state_)
2516 __state_->__release_shared();
2517}
2518
Howard Hinnant99968442011-11-29 18:15:502519template <class _Rp>
2520shared_future<_Rp&>&
2521shared_future<_Rp&>::operator=(const shared_future& __rhs)
Howard Hinnant99be8232010-09-03 18:39:252522{
2523 if (__rhs.__state_)
2524 __rhs.__state_->__add_shared();
2525 if (__state_)
2526 __state_->__release_shared();
2527 __state_ = __rhs.__state_;
2528 return *this;
2529}
2530
2531template <>
Howard Hinnant83eade62013-03-06 23:30:192532class _LIBCPP_TYPE_VIS shared_future<void>
Howard Hinnant99be8232010-09-03 18:39:252533{
2534 __assoc_sub_state* __state_;
2535
2536public:
Howard Hinnant8c6cbb22010-09-22 14:16:262537 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552538 shared_future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant8c6cbb22010-09-22 14:16:262539 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252540 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2541 {if (__state_) __state_->__add_shared();}
Howard Hinnant73d21a42010-09-04 23:28:192542#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:262543 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552544 shared_future(future<void>&& __f) _NOEXCEPT : __state_(__f.__state_)
Howard Hinnant99be8232010-09-03 18:39:252545 {__f.__state_ = nullptr;}
Howard Hinnant8c6cbb22010-09-22 14:16:262546 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552547 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
Howard Hinnant99be8232010-09-03 18:39:252548 {__rhs.__state_ = nullptr;}
Howard Hinnant73d21a42010-09-04 23:28:192549#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:252550 ~shared_future();
2551 shared_future& operator=(const shared_future& __rhs);
Howard Hinnant73d21a42010-09-04 23:28:192552#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:262553 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552554 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:252555 {
2556 shared_future(std::move(__rhs)).swap(*this);
2557 return *this;
2558 }
Howard Hinnant73d21a42010-09-04 23:28:192559#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:252560
2561 // retrieving the value
Howard Hinnant8c6cbb22010-09-22 14:16:262562 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252563 void get() const {__state_->copy();}
2564
Howard Hinnant8c6cbb22010-09-22 14:16:262565 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552566 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant99be8232010-09-03 18:39:252567
2568 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:262569 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552570 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant99be8232010-09-03 18:39:252571
Howard Hinnant8c6cbb22010-09-22 14:16:262572 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252573 void wait() const {__state_->wait();}
2574 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:262575 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252576 future_status
2577 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2578 {return __state_->wait_for(__rel_time);}
2579 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:262580 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252581 future_status
2582 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2583 {return __state_->wait_until(__abs_time);}
2584};
2585
Howard Hinnant99968442011-11-29 18:15:502586template <class _Rp>
Howard Hinnant99be8232010-09-03 18:39:252587inline _LIBCPP_INLINE_VISIBILITY
2588void
Howard Hinnant8bf01dd2012-07-21 17:46:552589swap(shared_future<_Rp>& __x, shared_future<_Rp>& __y) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:252590{
2591 __x.swap(__y);
2592}
2593
Howard Hinnant99968442011-11-29 18:15:502594template <class _Rp>
Howard Hinnant7de47902010-11-30 20:23:322595inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502596shared_future<_Rp>
2597future<_Rp>::share()
Howard Hinnante6e4d012010-09-03 21:46:372598{
Howard Hinnant99968442011-11-29 18:15:502599 return shared_future<_Rp>(_VSTD::move(*this));
Howard Hinnante6e4d012010-09-03 21:46:372600}
2601
Howard Hinnant99968442011-11-29 18:15:502602template <class _Rp>
Howard Hinnante6e4d012010-09-03 21:46:372603inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502604shared_future<_Rp&>
2605future<_Rp&>::share()
Howard Hinnante6e4d012010-09-03 21:46:372606{
Howard Hinnant99968442011-11-29 18:15:502607 return shared_future<_Rp&>(_VSTD::move(*this));
Howard Hinnant7de47902010-11-30 20:23:322608}
2609
Howard Hinnanta4451512010-12-02 16:45:212610#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2611
Howard Hinnant7de47902010-11-30 20:23:322612inline _LIBCPP_INLINE_VISIBILITY
2613shared_future<void>
2614future<void>::share()
2615{
Howard Hinnant0949eed2011-06-30 21:18:192616 return shared_future<void>(_VSTD::move(*this));
Howard Hinnante6e4d012010-09-03 21:46:372617}
2618
Howard Hinnanta4451512010-12-02 16:45:212619#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2620
Howard Hinnantbc8d3f92010-05-11 19:42:162621_LIBCPP_END_NAMESPACE_STD
2622
Jonathan Roelofs8d86b2e2014-09-05 19:45:052623#endif // !_LIBCPP_HAS_NO_THREADS
2624
Howard Hinnantbc8d3f92010-05-11 19:42:162625#endif // _LIBCPP_FUTURE