blob: 6fe6f8da5049f86df63f33d05165ba0a1e0efd84 [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>
332 explicit packaged_task(allocator_arg_t, const Allocator& a, F&& f);
333 ~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;
654 __lk.unlock();
655 __cv_.notify_all();
656}
657
Howard Hinnant99968442011-11-29 18:15:50658template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19659template <class _Arg>
660void
Howard Hinnant73d21a42010-09-04 23:28:19661#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50662__assoc_state<_Rp>::set_value_at_thread_exit(_Arg&& __arg)
Howard Hinnant47499b12010-08-27 20:10:19663#else
Howard Hinnant99968442011-11-29 18:15:50664__assoc_state<_Rp>::set_value_at_thread_exit(_Arg& __arg)
Howard Hinnant47499b12010-08-27 20:10:19665#endif
666{
667 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant22ba71b2011-07-13 16:00:50668#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:19669 if (this->__has_value())
670 throw future_error(make_error_code(future_errc::promise_already_satisfied));
Howard Hinnant22ba71b2011-07-13 16:00:50671#endif
Howard Hinnant99968442011-11-29 18:15:50672 ::new(&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
Howard Hinnant47499b12010-08-27 20:10:19673 this->__state_ |= base::__constructed;
Howard Hinnant5306d682010-10-14 19:18:04674 __thread_local_data()->__make_ready_at_thread_exit(this);
Howard Hinnant47499b12010-08-27 20:10:19675 __lk.unlock();
676}
677
Howard Hinnant99968442011-11-29 18:15:50678template <class _Rp>
679_Rp
680__assoc_state<_Rp>::move()
Howard Hinnant47499b12010-08-27 20:10:19681{
682 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant54da3382010-08-30 18:46:21683 this->__sub_wait(__lk);
Howard Hinnant47499b12010-08-27 20:10:19684 if (this->__exception_ != nullptr)
685 rethrow_exception(this->__exception_);
Howard Hinnant99968442011-11-29 18:15:50686 return _VSTD::move(*reinterpret_cast<_Rp*>(&__value_));
Howard Hinnant47499b12010-08-27 20:10:19687}
688
Howard Hinnant99968442011-11-29 18:15:50689template <class _Rp>
690typename add_lvalue_reference<_Rp>::type
691__assoc_state<_Rp>::copy()
Howard Hinnant47499b12010-08-27 20:10:19692{
693 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant54da3382010-08-30 18:46:21694 this->__sub_wait(__lk);
Howard Hinnant47499b12010-08-27 20:10:19695 if (this->__exception_ != nullptr)
696 rethrow_exception(this->__exception_);
Howard Hinnant99968442011-11-29 18:15:50697 return *reinterpret_cast<_Rp*>(&__value_);
Howard Hinnant47499b12010-08-27 20:10:19698}
699
Howard Hinnant99968442011-11-29 18:15:50700template <class _Rp>
701class __assoc_state<_Rp&>
Howard Hinnantf39daa82010-08-28 21:01:06702 : public __assoc_sub_state
703{
704 typedef __assoc_sub_state base;
Howard Hinnant99968442011-11-29 18:15:50705 typedef _Rp* _Up;
Howard Hinnantf39daa82010-08-28 21:01:06706protected:
Howard Hinnant99968442011-11-29 18:15:50707 _Up __value_;
Howard Hinnantf39daa82010-08-28 21:01:06708
Howard Hinnant1694d232011-05-28 14:41:13709 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnantf39daa82010-08-28 21:01:06710public:
711
Howard Hinnant99968442011-11-29 18:15:50712 void set_value(_Rp& __arg);
713 void set_value_at_thread_exit(_Rp& __arg);
Howard Hinnantf39daa82010-08-28 21:01:06714
Howard Hinnant99968442011-11-29 18:15:50715 _Rp& copy();
Howard Hinnantf39daa82010-08-28 21:01:06716};
717
Howard Hinnant99968442011-11-29 18:15:50718template <class _Rp>
Howard Hinnantf39daa82010-08-28 21:01:06719void
Howard Hinnant99968442011-11-29 18:15:50720__assoc_state<_Rp&>::__on_zero_shared() _NOEXCEPT
Howard Hinnantf39daa82010-08-28 21:01:06721{
722 delete this;
723}
724
Howard Hinnant99968442011-11-29 18:15:50725template <class _Rp>
Howard Hinnantf39daa82010-08-28 21:01:06726void
Howard Hinnant99968442011-11-29 18:15:50727__assoc_state<_Rp&>::set_value(_Rp& __arg)
Howard Hinnantf39daa82010-08-28 21:01:06728{
729 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant22ba71b2011-07-13 16:00:50730#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantf39daa82010-08-28 21:01:06731 if (this->__has_value())
732 throw future_error(make_error_code(future_errc::promise_already_satisfied));
Howard Hinnant22ba71b2011-07-13 16:00:50733#endif
Howard Hinnanta4e87ab2013-08-08 18:38:55734 __value_ = _VSTD::addressof(__arg);
Howard Hinnantf39daa82010-08-28 21:01:06735 this->__state_ |= base::__constructed | base::ready;
736 __lk.unlock();
737 __cv_.notify_all();
738}
739
Howard Hinnant99968442011-11-29 18:15:50740template <class _Rp>
Howard Hinnantf39daa82010-08-28 21:01:06741void
Howard Hinnant99968442011-11-29 18:15:50742__assoc_state<_Rp&>::set_value_at_thread_exit(_Rp& __arg)
Howard Hinnantf39daa82010-08-28 21:01:06743{
744 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant22ba71b2011-07-13 16:00:50745#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantf39daa82010-08-28 21:01:06746 if (this->__has_value())
747 throw future_error(make_error_code(future_errc::promise_already_satisfied));
Howard Hinnant22ba71b2011-07-13 16:00:50748#endif
Howard Hinnanta4e87ab2013-08-08 18:38:55749 __value_ = _VSTD::addressof(__arg);
Howard Hinnantf39daa82010-08-28 21:01:06750 this->__state_ |= base::__constructed;
Howard Hinnant5306d682010-10-14 19:18:04751 __thread_local_data()->__make_ready_at_thread_exit(this);
Howard Hinnantf39daa82010-08-28 21:01:06752 __lk.unlock();
753}
754
Howard Hinnant99968442011-11-29 18:15:50755template <class _Rp>
756_Rp&
757__assoc_state<_Rp&>::copy()
Howard Hinnantf39daa82010-08-28 21:01:06758{
759 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant54da3382010-08-30 18:46:21760 this->__sub_wait(__lk);
Howard Hinnantf39daa82010-08-28 21:01:06761 if (this->__exception_ != nullptr)
762 rethrow_exception(this->__exception_);
763 return *__value_;
764}
765
Howard Hinnant99968442011-11-29 18:15:50766template <class _Rp, class _Alloc>
Howard Hinnant47499b12010-08-27 20:10:19767class __assoc_state_alloc
Howard Hinnant99968442011-11-29 18:15:50768 : public __assoc_state<_Rp>
Howard Hinnant47499b12010-08-27 20:10:19769{
Howard Hinnant99968442011-11-29 18:15:50770 typedef __assoc_state<_Rp> base;
Howard Hinnant47499b12010-08-27 20:10:19771 _Alloc __alloc_;
772
Howard Hinnant1694d232011-05-28 14:41:13773 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant47499b12010-08-27 20:10:19774public:
Howard Hinnant8c6cbb22010-09-22 14:16:26775 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19776 explicit __assoc_state_alloc(const _Alloc& __a)
777 : __alloc_(__a) {}
778};
779
Howard Hinnant99968442011-11-29 18:15:50780template <class _Rp, class _Alloc>
Howard Hinnant47499b12010-08-27 20:10:19781void
Howard Hinnant99968442011-11-29 18:15:50782__assoc_state_alloc<_Rp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19783{
784 if (this->__state_ & base::__constructed)
Howard Hinnanta4e87ab2013-08-08 18:38:55785 reinterpret_cast<_Rp*>(_VSTD::addressof(this->__value_))->~_Rp();
Eric Fiselier4d2413c2014-10-23 06:24:45786 typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _A;
787 typedef allocator_traits<_A> _ATraits;
788 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
789 _A __a(__alloc_);
Howard Hinnant47499b12010-08-27 20:10:19790 this->~__assoc_state_alloc();
Eric Fiselier4d2413c2014-10-23 06:24:45791 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnant47499b12010-08-27 20:10:19792}
793
Howard Hinnant99968442011-11-29 18:15:50794template <class _Rp, class _Alloc>
795class __assoc_state_alloc<_Rp&, _Alloc>
796 : public __assoc_state<_Rp&>
Howard Hinnantf39daa82010-08-28 21:01:06797{
Howard Hinnant99968442011-11-29 18:15:50798 typedef __assoc_state<_Rp&> base;
Howard Hinnantf39daa82010-08-28 21:01:06799 _Alloc __alloc_;
800
Howard Hinnant1694d232011-05-28 14:41:13801 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnantf39daa82010-08-28 21:01:06802public:
Howard Hinnant8c6cbb22010-09-22 14:16:26803 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39daa82010-08-28 21:01:06804 explicit __assoc_state_alloc(const _Alloc& __a)
805 : __alloc_(__a) {}
806};
807
Howard Hinnant99968442011-11-29 18:15:50808template <class _Rp, class _Alloc>
Howard Hinnantf39daa82010-08-28 21:01:06809void
Howard Hinnant99968442011-11-29 18:15:50810__assoc_state_alloc<_Rp&, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantf39daa82010-08-28 21:01:06811{
Eric Fiselier4d2413c2014-10-23 06:24:45812 typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _A;
813 typedef allocator_traits<_A> _ATraits;
814 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
815 _A __a(__alloc_);
Howard Hinnantf39daa82010-08-28 21:01:06816 this->~__assoc_state_alloc();
Eric Fiselier4d2413c2014-10-23 06:24:45817 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantf39daa82010-08-28 21:01:06818}
819
Howard Hinnant47499b12010-08-27 20:10:19820template <class _Alloc>
821class __assoc_sub_state_alloc
822 : public __assoc_sub_state
823{
824 typedef __assoc_sub_state base;
825 _Alloc __alloc_;
826
Howard Hinnant1694d232011-05-28 14:41:13827 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant47499b12010-08-27 20:10:19828public:
Howard Hinnant8c6cbb22010-09-22 14:16:26829 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19830 explicit __assoc_sub_state_alloc(const _Alloc& __a)
831 : __alloc_(__a) {}
832};
833
834template <class _Alloc>
835void
Howard Hinnant1694d232011-05-28 14:41:13836__assoc_sub_state_alloc<_Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19837{
Eric Fiselier4d2413c2014-10-23 06:24:45838 typedef typename __allocator_traits_rebind<_Alloc, __assoc_sub_state_alloc>::type _A;
839 typedef allocator_traits<_A> _ATraits;
840 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
841 _A __a(__alloc_);
Howard Hinnant47499b12010-08-27 20:10:19842 this->~__assoc_sub_state_alloc();
Eric Fiselier4d2413c2014-10-23 06:24:45843 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnant47499b12010-08-27 20:10:19844}
845
Howard Hinnant99968442011-11-29 18:15:50846template <class _Rp, class _Fp>
Howard Hinnant54da3382010-08-30 18:46:21847class __deferred_assoc_state
Howard Hinnant99968442011-11-29 18:15:50848 : public __assoc_state<_Rp>
Howard Hinnant54da3382010-08-30 18:46:21849{
Howard Hinnant99968442011-11-29 18:15:50850 typedef __assoc_state<_Rp> base;
Howard Hinnant54da3382010-08-30 18:46:21851
Howard Hinnant99968442011-11-29 18:15:50852 _Fp __func_;
Howard Hinnant54da3382010-08-30 18:46:21853
854public:
Howard Hinnant73d21a42010-09-04 23:28:19855#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50856 explicit __deferred_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:21857#endif
858
859 virtual void __execute();
860};
861
Howard Hinnant73d21a42010-09-04 23:28:19862#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21863
Howard Hinnant99968442011-11-29 18:15:50864template <class _Rp, class _Fp>
Howard Hinnant54da3382010-08-30 18:46:21865inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50866__deferred_assoc_state<_Rp, _Fp>::__deferred_assoc_state(_Fp&& __f)
867 : __func_(_VSTD::forward<_Fp>(__f))
Howard Hinnant54da3382010-08-30 18:46:21868{
869 this->__set_deferred();
870}
871
Howard Hinnant73d21a42010-09-04 23:28:19872#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21873
Howard Hinnant99968442011-11-29 18:15:50874template <class _Rp, class _Fp>
Howard Hinnant54da3382010-08-30 18:46:21875void
Howard Hinnant99968442011-11-29 18:15:50876__deferred_assoc_state<_Rp, _Fp>::__execute()
Howard Hinnant54da3382010-08-30 18:46:21877{
878#ifndef _LIBCPP_NO_EXCEPTIONS
879 try
880 {
881#endif // _LIBCPP_NO_EXCEPTIONS
882 this->set_value(__func_());
883#ifndef _LIBCPP_NO_EXCEPTIONS
884 }
885 catch (...)
886 {
887 this->set_exception(current_exception());
888 }
889#endif // _LIBCPP_NO_EXCEPTIONS
890}
891
Howard Hinnant99968442011-11-29 18:15:50892template <class _Fp>
893class __deferred_assoc_state<void, _Fp>
Howard Hinnant54da3382010-08-30 18:46:21894 : public __assoc_sub_state
895{
896 typedef __assoc_sub_state base;
897
Howard Hinnant99968442011-11-29 18:15:50898 _Fp __func_;
Howard Hinnant54da3382010-08-30 18:46:21899
900public:
Howard Hinnant73d21a42010-09-04 23:28:19901#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50902 explicit __deferred_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:21903#endif
904
905 virtual void __execute();
906};
907
Howard Hinnant73d21a42010-09-04 23:28:19908#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21909
Howard Hinnant99968442011-11-29 18:15:50910template <class _Fp>
Howard Hinnant54da3382010-08-30 18:46:21911inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50912__deferred_assoc_state<void, _Fp>::__deferred_assoc_state(_Fp&& __f)
913 : __func_(_VSTD::forward<_Fp>(__f))
Howard Hinnant54da3382010-08-30 18:46:21914{
915 this->__set_deferred();
916}
917
Howard Hinnant73d21a42010-09-04 23:28:19918#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21919
Howard Hinnant99968442011-11-29 18:15:50920template <class _Fp>
Howard Hinnant54da3382010-08-30 18:46:21921void
Howard Hinnant99968442011-11-29 18:15:50922__deferred_assoc_state<void, _Fp>::__execute()
Howard Hinnant54da3382010-08-30 18:46:21923{
924#ifndef _LIBCPP_NO_EXCEPTIONS
925 try
926 {
927#endif // _LIBCPP_NO_EXCEPTIONS
928 __func_();
929 this->set_value();
930#ifndef _LIBCPP_NO_EXCEPTIONS
931 }
932 catch (...)
933 {
934 this->set_exception(current_exception());
935 }
936#endif // _LIBCPP_NO_EXCEPTIONS
937}
938
Howard Hinnant99968442011-11-29 18:15:50939template <class _Rp, class _Fp>
Howard Hinnant57cff292011-05-19 15:05:04940class __async_assoc_state
Howard Hinnant99968442011-11-29 18:15:50941 : public __assoc_state<_Rp>
Howard Hinnant57cff292011-05-19 15:05:04942{
Howard Hinnant99968442011-11-29 18:15:50943 typedef __assoc_state<_Rp> base;
Howard Hinnant57cff292011-05-19 15:05:04944
Howard Hinnant99968442011-11-29 18:15:50945 _Fp __func_;
Howard Hinnant57cff292011-05-19 15:05:04946
Howard Hinnant1694d232011-05-28 14:41:13947 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant57cff292011-05-19 15:05:04948public:
949#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50950 explicit __async_assoc_state(_Fp&& __f);
Howard Hinnant57cff292011-05-19 15:05:04951#endif
952
953 virtual void __execute();
954};
955
956#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
957
Howard Hinnant99968442011-11-29 18:15:50958template <class _Rp, class _Fp>
Howard Hinnant57cff292011-05-19 15:05:04959inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50960__async_assoc_state<_Rp, _Fp>::__async_assoc_state(_Fp&& __f)
961 : __func_(_VSTD::forward<_Fp>(__f))
Howard Hinnant57cff292011-05-19 15:05:04962{
963}
964
965#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
966
Howard Hinnant99968442011-11-29 18:15:50967template <class _Rp, class _Fp>
Howard Hinnant57cff292011-05-19 15:05:04968void
Howard Hinnant99968442011-11-29 18:15:50969__async_assoc_state<_Rp, _Fp>::__execute()
Howard Hinnant57cff292011-05-19 15:05:04970{
971#ifndef _LIBCPP_NO_EXCEPTIONS
972 try
973 {
974#endif // _LIBCPP_NO_EXCEPTIONS
975 this->set_value(__func_());
976#ifndef _LIBCPP_NO_EXCEPTIONS
977 }
978 catch (...)
979 {
980 this->set_exception(current_exception());
981 }
982#endif // _LIBCPP_NO_EXCEPTIONS
983}
984
Howard Hinnant99968442011-11-29 18:15:50985template <class _Rp, class _Fp>
Howard Hinnant57cff292011-05-19 15:05:04986void
Howard Hinnant99968442011-11-29 18:15:50987__async_assoc_state<_Rp, _Fp>::__on_zero_shared() _NOEXCEPT
Howard Hinnant57cff292011-05-19 15:05:04988{
989 this->wait();
990 base::__on_zero_shared();
991}
992
Howard Hinnant99968442011-11-29 18:15:50993template <class _Fp>
994class __async_assoc_state<void, _Fp>
Howard Hinnant57cff292011-05-19 15:05:04995 : public __assoc_sub_state
996{
997 typedef __assoc_sub_state base;
998
Howard Hinnant99968442011-11-29 18:15:50999 _Fp __func_;
Howard Hinnant57cff292011-05-19 15:05:041000
Howard Hinnant1694d232011-05-28 14:41:131001 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant57cff292011-05-19 15:05:041002public:
1003#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501004 explicit __async_assoc_state(_Fp&& __f);
Howard Hinnant57cff292011-05-19 15:05:041005#endif
1006
1007 virtual void __execute();
1008};
1009
1010#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1011
Howard Hinnant99968442011-11-29 18:15:501012template <class _Fp>
Howard Hinnant57cff292011-05-19 15:05:041013inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:501014__async_assoc_state<void, _Fp>::__async_assoc_state(_Fp&& __f)
1015 : __func_(_VSTD::forward<_Fp>(__f))
Howard Hinnant57cff292011-05-19 15:05:041016{
1017}
1018
1019#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1020
Howard Hinnant99968442011-11-29 18:15:501021template <class _Fp>
Howard Hinnant57cff292011-05-19 15:05:041022void
Howard Hinnant99968442011-11-29 18:15:501023__async_assoc_state<void, _Fp>::__execute()
Howard Hinnant57cff292011-05-19 15:05:041024{
1025#ifndef _LIBCPP_NO_EXCEPTIONS
1026 try
1027 {
1028#endif // _LIBCPP_NO_EXCEPTIONS
1029 __func_();
1030 this->set_value();
1031#ifndef _LIBCPP_NO_EXCEPTIONS
1032 }
1033 catch (...)
1034 {
1035 this->set_exception(current_exception());
1036 }
1037#endif // _LIBCPP_NO_EXCEPTIONS
1038}
1039
Howard Hinnant99968442011-11-29 18:15:501040template <class _Fp>
Howard Hinnant57cff292011-05-19 15:05:041041void
Howard Hinnant99968442011-11-29 18:15:501042__async_assoc_state<void, _Fp>::__on_zero_shared() _NOEXCEPT
Howard Hinnant57cff292011-05-19 15:05:041043{
1044 this->wait();
1045 base::__on_zero_shared();
1046}
1047
Howard Hinnant0f678bd2013-08-12 18:38:341048template <class _Rp> class _LIBCPP_TYPE_VIS_ONLY promise;
1049template <class _Rp> class _LIBCPP_TYPE_VIS_ONLY shared_future;
Howard Hinnant47499b12010-08-27 20:10:191050
1051// future
1052
Howard Hinnant0f678bd2013-08-12 18:38:341053template <class _Rp> class _LIBCPP_TYPE_VIS_ONLY future;
Howard Hinnant54da3382010-08-30 18:46:211054
Howard Hinnant99968442011-11-29 18:15:501055template <class _Rp, class _Fp>
1056future<_Rp>
Howard Hinnant73d21a42010-09-04 23:28:191057#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501058__make_deferred_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:211059#else
Howard Hinnant99968442011-11-29 18:15:501060__make_deferred_assoc_state(_Fp __f);
Howard Hinnant54da3382010-08-30 18:46:211061#endif
1062
Howard Hinnant99968442011-11-29 18:15:501063template <class _Rp, class _Fp>
1064future<_Rp>
Howard Hinnant57cff292011-05-19 15:05:041065#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501066__make_async_assoc_state(_Fp&& __f);
Howard Hinnant57cff292011-05-19 15:05:041067#else
Howard Hinnant99968442011-11-29 18:15:501068__make_async_assoc_state(_Fp __f);
Howard Hinnant57cff292011-05-19 15:05:041069#endif
1070
Howard Hinnant99968442011-11-29 18:15:501071template <class _Rp>
Howard Hinnant0f678bd2013-08-12 18:38:341072class _LIBCPP_TYPE_VIS_ONLY future
Howard Hinnant47499b12010-08-27 20:10:191073{
Howard Hinnant99968442011-11-29 18:15:501074 __assoc_state<_Rp>* __state_;
Howard Hinnant47499b12010-08-27 20:10:191075
Howard Hinnant99968442011-11-29 18:15:501076 explicit future(__assoc_state<_Rp>* __state);
Howard Hinnant47499b12010-08-27 20:10:191077
1078 template <class> friend class promise;
Howard Hinnant99be8232010-09-03 18:39:251079 template <class> friend class shared_future;
Howard Hinnant54da3382010-08-30 18:46:211080
Howard Hinnant73d21a42010-09-04 23:28:191081#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501082 template <class _R1, class _Fp>
1083 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1084 template <class _R1, class _Fp>
1085 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:211086#else
Howard Hinnant99968442011-11-29 18:15:501087 template <class _R1, class _Fp>
1088 friend future<_R1> __make_deferred_assoc_state(_Fp __f);
1089 template <class _R1, class _Fp>
1090 friend future<_R1> __make_async_assoc_state(_Fp __f);
Howard Hinnant54da3382010-08-30 18:46:211091#endif
1092
Howard Hinnant47499b12010-08-27 20:10:191093public:
Howard Hinnant8c6cbb22010-09-22 14:16:261094 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551095 future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant73d21a42010-09-04 23:28:191096#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261097 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551098 future(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191099 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1100 future(const future&) = delete;
1101 future& operator=(const future&) = delete;
Howard Hinnant8c6cbb22010-09-22 14:16:261102 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551103 future& operator=(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191104 {
1105 future(std::move(__rhs)).swap(*this);
1106 return *this;
1107 }
Howard Hinnant73d21a42010-09-04 23:28:191108#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191109private:
1110 future(const future&);
1111 future& operator=(const future&);
1112public:
Howard Hinnant73d21a42010-09-04 23:28:191113#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191114 ~future();
Howard Hinnant99968442011-11-29 18:15:501115 shared_future<_Rp> share();
Howard Hinnant47499b12010-08-27 20:10:191116
1117 // retrieving the value
Howard Hinnant99968442011-11-29 18:15:501118 _Rp get();
Howard Hinnant47499b12010-08-27 20:10:191119
Howard Hinnant8c6cbb22010-09-22 14:16:261120 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551121 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:191122
1123 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:261124 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551125 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant47499b12010-08-27 20:10:191126
Howard Hinnant8c6cbb22010-09-22 14:16:261127 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191128 void wait() const {__state_->wait();}
1129 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:261130 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191131 future_status
1132 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1133 {return __state_->wait_for(__rel_time);}
1134 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:261135 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191136 future_status
1137 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1138 {return __state_->wait_until(__abs_time);}
1139};
1140
Howard Hinnant99968442011-11-29 18:15:501141template <class _Rp>
1142future<_Rp>::future(__assoc_state<_Rp>* __state)
Howard Hinnant47499b12010-08-27 20:10:191143 : __state_(__state)
1144{
Howard Hinnant22ba71b2011-07-13 16:00:501145#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:191146 if (__state_->__has_future_attached())
1147 throw future_error(make_error_code(future_errc::future_already_retrieved));
Howard Hinnant22ba71b2011-07-13 16:00:501148#endif
Howard Hinnant47499b12010-08-27 20:10:191149 __state_->__add_shared();
Howard Hinnant54da3382010-08-30 18:46:211150 __state_->__set_future_attached();
Howard Hinnant47499b12010-08-27 20:10:191151}
1152
Howard Hinnant54da3382010-08-30 18:46:211153struct __release_shared_count
1154{
1155 void operator()(__shared_count* p) {p->__release_shared();}
1156};
1157
Howard Hinnant99968442011-11-29 18:15:501158template <class _Rp>
1159future<_Rp>::~future()
Howard Hinnant47499b12010-08-27 20:10:191160{
1161 if (__state_)
1162 __state_->__release_shared();
1163}
1164
Howard Hinnant99968442011-11-29 18:15:501165template <class _Rp>
1166_Rp
1167future<_Rp>::get()
Howard Hinnant47499b12010-08-27 20:10:191168{
Howard Hinnant54da3382010-08-30 18:46:211169 unique_ptr<__shared_count, __release_shared_count> __(__state_);
Howard Hinnant99968442011-11-29 18:15:501170 __assoc_state<_Rp>* __s = __state_;
Howard Hinnant47499b12010-08-27 20:10:191171 __state_ = nullptr;
1172 return __s->move();
1173}
1174
Howard Hinnant99968442011-11-29 18:15:501175template <class _Rp>
Howard Hinnant0f678bd2013-08-12 18:38:341176class _LIBCPP_TYPE_VIS_ONLY future<_Rp&>
Howard Hinnant47499b12010-08-27 20:10:191177{
Howard Hinnant99968442011-11-29 18:15:501178 __assoc_state<_Rp&>* __state_;
Howard Hinnant47499b12010-08-27 20:10:191179
Howard Hinnant99968442011-11-29 18:15:501180 explicit future(__assoc_state<_Rp&>* __state);
Howard Hinnant47499b12010-08-27 20:10:191181
1182 template <class> friend class promise;
Howard Hinnant99be8232010-09-03 18:39:251183 template <class> friend class shared_future;
Howard Hinnant54da3382010-08-30 18:46:211184
Howard Hinnant73d21a42010-09-04 23:28:191185#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501186 template <class _R1, class _Fp>
1187 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1188 template <class _R1, class _Fp>
1189 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:211190#else
Howard Hinnant99968442011-11-29 18:15:501191 template <class _R1, class _Fp>
1192 friend future<_R1> __make_deferred_assoc_state(_Fp __f);
1193 template <class _R1, class _Fp>
1194 friend future<_R1> __make_async_assoc_state(_Fp __f);
Howard Hinnant54da3382010-08-30 18:46:211195#endif
1196
Howard Hinnant47499b12010-08-27 20:10:191197public:
Howard Hinnant8c6cbb22010-09-22 14:16:261198 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551199 future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant73d21a42010-09-04 23:28:191200#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261201 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551202 future(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191203 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1204 future(const future&) = delete;
1205 future& operator=(const future&) = delete;
Howard Hinnant8c6cbb22010-09-22 14:16:261206 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551207 future& operator=(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191208 {
1209 future(std::move(__rhs)).swap(*this);
1210 return *this;
1211 }
Howard Hinnant73d21a42010-09-04 23:28:191212#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191213private:
1214 future(const future&);
1215 future& operator=(const future&);
1216public:
Howard Hinnant73d21a42010-09-04 23:28:191217#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191218 ~future();
Howard Hinnant99968442011-11-29 18:15:501219 shared_future<_Rp&> share();
Howard Hinnant47499b12010-08-27 20:10:191220
1221 // retrieving the value
Howard Hinnant99968442011-11-29 18:15:501222 _Rp& get();
Howard Hinnant47499b12010-08-27 20:10:191223
Howard Hinnant8c6cbb22010-09-22 14:16:261224 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551225 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:191226
1227 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:261228 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551229 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant47499b12010-08-27 20:10:191230
Howard Hinnant8c6cbb22010-09-22 14:16:261231 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191232 void wait() const {__state_->wait();}
1233 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:261234 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191235 future_status
1236 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1237 {return __state_->wait_for(__rel_time);}
1238 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:261239 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191240 future_status
1241 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1242 {return __state_->wait_until(__abs_time);}
1243};
1244
Howard Hinnant99968442011-11-29 18:15:501245template <class _Rp>
1246future<_Rp&>::future(__assoc_state<_Rp&>* __state)
Howard Hinnant47499b12010-08-27 20:10:191247 : __state_(__state)
1248{
Howard Hinnant22ba71b2011-07-13 16:00:501249#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:191250 if (__state_->__has_future_attached())
1251 throw future_error(make_error_code(future_errc::future_already_retrieved));
Howard Hinnant22ba71b2011-07-13 16:00:501252#endif
Howard Hinnant47499b12010-08-27 20:10:191253 __state_->__add_shared();
Howard Hinnant54da3382010-08-30 18:46:211254 __state_->__set_future_attached();
Howard Hinnant47499b12010-08-27 20:10:191255}
1256
Howard Hinnant99968442011-11-29 18:15:501257template <class _Rp>
1258future<_Rp&>::~future()
Howard Hinnant47499b12010-08-27 20:10:191259{
1260 if (__state_)
1261 __state_->__release_shared();
1262}
1263
Howard Hinnant99968442011-11-29 18:15:501264template <class _Rp>
1265_Rp&
1266future<_Rp&>::get()
Howard Hinnant47499b12010-08-27 20:10:191267{
Howard Hinnant54da3382010-08-30 18:46:211268 unique_ptr<__shared_count, __release_shared_count> __(__state_);
Howard Hinnant99968442011-11-29 18:15:501269 __assoc_state<_Rp&>* __s = __state_;
Howard Hinnant47499b12010-08-27 20:10:191270 __state_ = nullptr;
1271 return __s->copy();
1272}
1273
1274template <>
Howard Hinnant83eade62013-03-06 23:30:191275class _LIBCPP_TYPE_VIS future<void>
Howard Hinnant47499b12010-08-27 20:10:191276{
1277 __assoc_sub_state* __state_;
1278
1279 explicit future(__assoc_sub_state* __state);
1280
1281 template <class> friend class promise;
Howard Hinnant99be8232010-09-03 18:39:251282 template <class> friend class shared_future;
Howard Hinnant54da3382010-08-30 18:46:211283
Howard Hinnant73d21a42010-09-04 23:28:191284#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501285 template <class _R1, class _Fp>
1286 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1287 template <class _R1, class _Fp>
1288 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:211289#else
Howard Hinnant99968442011-11-29 18:15:501290 template <class _R1, class _Fp>
1291 friend future<_R1> __make_deferred_assoc_state(_Fp __f);
1292 template <class _R1, class _Fp>
1293 friend future<_R1> __make_async_assoc_state(_Fp __f);
Howard Hinnant54da3382010-08-30 18:46:211294#endif
1295
Howard Hinnant47499b12010-08-27 20:10:191296public:
Howard Hinnant8c6cbb22010-09-22 14:16:261297 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551298 future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant73d21a42010-09-04 23:28:191299#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261300 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551301 future(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191302 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1303 future(const future&) = delete;
1304 future& operator=(const future&) = delete;
Howard Hinnant8c6cbb22010-09-22 14:16:261305 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551306 future& operator=(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191307 {
1308 future(std::move(__rhs)).swap(*this);
1309 return *this;
1310 }
Howard Hinnant73d21a42010-09-04 23:28:191311#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191312private:
1313 future(const future&);
1314 future& operator=(const future&);
1315public:
Howard Hinnant73d21a42010-09-04 23:28:191316#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191317 ~future();
Howard Hinnant7de47902010-11-30 20:23:321318 shared_future<void> share();
Howard Hinnant47499b12010-08-27 20:10:191319
1320 // retrieving the value
1321 void get();
1322
Howard Hinnant8c6cbb22010-09-22 14:16:261323 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551324 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:191325
1326 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:261327 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551328 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant47499b12010-08-27 20:10:191329
Howard Hinnant8c6cbb22010-09-22 14:16:261330 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191331 void wait() const {__state_->wait();}
1332 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:261333 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191334 future_status
1335 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1336 {return __state_->wait_for(__rel_time);}
1337 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:261338 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191339 future_status
1340 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1341 {return __state_->wait_until(__abs_time);}
1342};
1343
Howard Hinnant99968442011-11-29 18:15:501344template <class _Rp>
Howard Hinnant99be8232010-09-03 18:39:251345inline _LIBCPP_INLINE_VISIBILITY
1346void
Howard Hinnant8bf01dd2012-07-21 17:46:551347swap(future<_Rp>& __x, future<_Rp>& __y) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:251348{
1349 __x.swap(__y);
1350}
1351
Howard Hinnant47499b12010-08-27 20:10:191352// promise<R>
1353
Howard Hinnant2b1b2d42011-06-14 19:58:171354template <class _Callable> class packaged_task;
Howard Hinnant54da3382010-08-30 18:46:211355
Howard Hinnant99968442011-11-29 18:15:501356template <class _Rp>
Howard Hinnant0f678bd2013-08-12 18:38:341357class _LIBCPP_TYPE_VIS_ONLY promise
Howard Hinnant47499b12010-08-27 20:10:191358{
Howard Hinnant99968442011-11-29 18:15:501359 __assoc_state<_Rp>* __state_;
Howard Hinnant54da3382010-08-30 18:46:211360
Howard Hinnant8c6cbb22010-09-22 14:16:261361 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551362 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant54da3382010-08-30 18:46:211363
1364 template <class> friend class packaged_task;
Howard Hinnant47499b12010-08-27 20:10:191365public:
1366 promise();
1367 template <class _Alloc>
1368 promise(allocator_arg_t, const _Alloc& __a);
Howard Hinnant73d21a42010-09-04 23:28:191369#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261370 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551371 promise(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191372 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1373 promise(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:191374#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191375private:
1376 promise(const promise& __rhs);
1377public:
Howard Hinnant73d21a42010-09-04 23:28:191378#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191379 ~promise();
1380
1381 // assignment
Howard Hinnant73d21a42010-09-04 23:28:191382#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261383 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551384 promise& operator=(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191385 {
1386 promise(std::move(__rhs)).swap(*this);
1387 return *this;
1388 }
1389 promise& operator=(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:191390#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191391private:
1392 promise& operator=(const promise& __rhs);
1393public:
Howard Hinnant73d21a42010-09-04 23:28:191394#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261395 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551396 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:191397
1398 // retrieving the result
Howard Hinnant99968442011-11-29 18:15:501399 future<_Rp> get_future();
Howard Hinnant47499b12010-08-27 20:10:191400
1401 // setting the result
Howard Hinnant99968442011-11-29 18:15:501402 void set_value(const _Rp& __r);
Howard Hinnant73d21a42010-09-04 23:28:191403#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501404 void set_value(_Rp&& __r);
Howard Hinnant47499b12010-08-27 20:10:191405#endif
1406 void set_exception(exception_ptr __p);
1407
1408 // setting the result with deferred notification
Howard Hinnant99968442011-11-29 18:15:501409 void set_value_at_thread_exit(const _Rp& __r);
Howard Hinnant73d21a42010-09-04 23:28:191410#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501411 void set_value_at_thread_exit(_Rp&& __r);
Howard Hinnant47499b12010-08-27 20:10:191412#endif
1413 void set_exception_at_thread_exit(exception_ptr __p);
1414};
1415
Howard Hinnant99968442011-11-29 18:15:501416template <class _Rp>
1417promise<_Rp>::promise()
1418 : __state_(new __assoc_state<_Rp>)
Howard Hinnant47499b12010-08-27 20:10:191419{
1420}
1421
Howard Hinnant99968442011-11-29 18:15:501422template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191423template <class _Alloc>
Howard Hinnant99968442011-11-29 18:15:501424promise<_Rp>::promise(allocator_arg_t, const _Alloc& __a0)
Howard Hinnant47499b12010-08-27 20:10:191425{
Eric Fiselier4d2413c2014-10-23 06:24:451426 typedef __assoc_state_alloc<_Rp, _Alloc> _State;
1427 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
Howard Hinnant47499b12010-08-27 20:10:191428 typedef __allocator_destructor<_A2> _D2;
1429 _A2 __a(__a0);
Eric Fiselier4d2413c2014-10-23 06:24:451430 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1431 ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0);
1432 __state_ = _VSTD::addressof(*__hold.release());
Howard Hinnant47499b12010-08-27 20:10:191433}
1434
Howard Hinnant99968442011-11-29 18:15:501435template <class _Rp>
1436promise<_Rp>::~promise()
Howard Hinnant47499b12010-08-27 20:10:191437{
1438 if (__state_)
1439 {
1440 if (!__state_->__has_value() && __state_->use_count() > 1)
1441 __state_->set_exception(make_exception_ptr(
1442 future_error(make_error_code(future_errc::broken_promise))
1443 ));
1444 __state_->__release_shared();
1445 }
1446}
1447
Howard Hinnant99968442011-11-29 18:15:501448template <class _Rp>
1449future<_Rp>
1450promise<_Rp>::get_future()
Howard Hinnant47499b12010-08-27 20:10:191451{
Howard Hinnant22ba71b2011-07-13 16:00:501452#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:191453 if (__state_ == nullptr)
1454 throw future_error(make_error_code(future_errc::no_state));
Howard Hinnant22ba71b2011-07-13 16:00:501455#endif
Howard Hinnant99968442011-11-29 18:15:501456 return future<_Rp>(__state_);
Howard Hinnant47499b12010-08-27 20:10:191457}
1458
Howard Hinnant99968442011-11-29 18:15:501459template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191460void
Howard Hinnant99968442011-11-29 18:15:501461promise<_Rp>::set_value(const _Rp& __r)
Howard Hinnant47499b12010-08-27 20:10:191462{
Howard Hinnant22ba71b2011-07-13 16:00:501463#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:191464 if (__state_ == nullptr)
1465 throw future_error(make_error_code(future_errc::no_state));
Howard Hinnant22ba71b2011-07-13 16:00:501466#endif
Howard Hinnant47499b12010-08-27 20:10:191467 __state_->set_value(__r);
1468}
1469
Howard Hinnant73d21a42010-09-04 23:28:191470#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191471
Howard Hinnant99968442011-11-29 18:15:501472template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191473void
Howard Hinnant99968442011-11-29 18:15:501474promise<_Rp>::set_value(_Rp&& __r)
Howard Hinnant47499b12010-08-27 20:10:191475{
Howard Hinnant22ba71b2011-07-13 16:00:501476#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:191477 if (__state_ == nullptr)
1478 throw future_error(make_error_code(future_errc::no_state));
Howard Hinnant22ba71b2011-07-13 16:00:501479#endif
Howard Hinnant0949eed2011-06-30 21:18:191480 __state_->set_value(_VSTD::move(__r));
Howard Hinnant47499b12010-08-27 20:10:191481}
1482
Howard Hinnant73d21a42010-09-04 23:28:191483#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191484
Howard Hinnant99968442011-11-29 18:15:501485template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191486void
Howard Hinnant99968442011-11-29 18:15:501487promise<_Rp>::set_exception(exception_ptr __p)
Howard Hinnant47499b12010-08-27 20:10:191488{
Howard Hinnant22ba71b2011-07-13 16:00:501489#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:191490 if (__state_ == nullptr)
1491 throw future_error(make_error_code(future_errc::no_state));
Howard Hinnant22ba71b2011-07-13 16:00:501492#endif
Howard Hinnant47499b12010-08-27 20:10:191493 __state_->set_exception(__p);
1494}
1495
Howard Hinnant99968442011-11-29 18:15:501496template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191497void
Howard Hinnant99968442011-11-29 18:15:501498promise<_Rp>::set_value_at_thread_exit(const _Rp& __r)
Howard Hinnant47499b12010-08-27 20:10:191499{
Howard Hinnant22ba71b2011-07-13 16:00:501500#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:191501 if (__state_ == nullptr)
1502 throw future_error(make_error_code(future_errc::no_state));
Howard Hinnant22ba71b2011-07-13 16:00:501503#endif
Howard Hinnant47499b12010-08-27 20:10:191504 __state_->set_value_at_thread_exit(__r);
1505}
1506
Howard Hinnant73d21a42010-09-04 23:28:191507#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191508
Howard Hinnant99968442011-11-29 18:15:501509template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191510void
Howard Hinnant99968442011-11-29 18:15:501511promise<_Rp>::set_value_at_thread_exit(_Rp&& __r)
Howard Hinnant47499b12010-08-27 20:10:191512{
Howard Hinnant22ba71b2011-07-13 16:00:501513#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:191514 if (__state_ == nullptr)
1515 throw future_error(make_error_code(future_errc::no_state));
Howard Hinnant22ba71b2011-07-13 16:00:501516#endif
Howard Hinnant0949eed2011-06-30 21:18:191517 __state_->set_value_at_thread_exit(_VSTD::move(__r));
Howard Hinnant47499b12010-08-27 20:10:191518}
1519
Howard Hinnant73d21a42010-09-04 23:28:191520#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191521
Howard Hinnant99968442011-11-29 18:15:501522template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191523void
Howard Hinnant99968442011-11-29 18:15:501524promise<_Rp>::set_exception_at_thread_exit(exception_ptr __p)
Howard Hinnant47499b12010-08-27 20:10:191525{
Howard Hinnant22ba71b2011-07-13 16:00:501526#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:191527 if (__state_ == nullptr)
1528 throw future_error(make_error_code(future_errc::no_state));
Howard Hinnant22ba71b2011-07-13 16:00:501529#endif
Howard Hinnant47499b12010-08-27 20:10:191530 __state_->set_exception_at_thread_exit(__p);
1531}
1532
1533// promise<R&>
1534
Howard Hinnant99968442011-11-29 18:15:501535template <class _Rp>
Howard Hinnant0f678bd2013-08-12 18:38:341536class _LIBCPP_TYPE_VIS_ONLY promise<_Rp&>
Howard Hinnant47499b12010-08-27 20:10:191537{
Howard Hinnant99968442011-11-29 18:15:501538 __assoc_state<_Rp&>* __state_;
Howard Hinnant54da3382010-08-30 18:46:211539
Howard Hinnant8c6cbb22010-09-22 14:16:261540 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551541 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant54da3382010-08-30 18:46:211542
1543 template <class> friend class packaged_task;
1544
Howard Hinnant47499b12010-08-27 20:10:191545public:
1546 promise();
1547 template <class _Allocator>
1548 promise(allocator_arg_t, const _Allocator& __a);
Howard Hinnant73d21a42010-09-04 23:28:191549#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261550 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551551 promise(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191552 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1553 promise(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:191554#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191555private:
1556 promise(const promise& __rhs);
1557public:
Howard Hinnant73d21a42010-09-04 23:28:191558#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191559 ~promise();
1560
1561 // assignment
Howard Hinnant73d21a42010-09-04 23:28:191562#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261563 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551564 promise& operator=(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191565 {
1566 promise(std::move(__rhs)).swap(*this);
1567 return *this;
1568 }
1569 promise& operator=(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:191570#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191571private:
1572 promise& operator=(const promise& __rhs);
1573public:
Howard Hinnant73d21a42010-09-04 23:28:191574#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261575 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551576 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:191577
1578 // retrieving the result
Howard Hinnant99968442011-11-29 18:15:501579 future<_Rp&> get_future();
Howard Hinnant47499b12010-08-27 20:10:191580
1581 // setting the result
Howard Hinnant99968442011-11-29 18:15:501582 void set_value(_Rp& __r);
Howard Hinnant47499b12010-08-27 20:10:191583 void set_exception(exception_ptr __p);
1584
1585 // setting the result with deferred notification
Howard Hinnant99968442011-11-29 18:15:501586 void set_value_at_thread_exit(_Rp&);
Howard Hinnant47499b12010-08-27 20:10:191587 void set_exception_at_thread_exit(exception_ptr __p);
1588};
1589
Howard Hinnant99968442011-11-29 18:15:501590template <class _Rp>
1591promise<_Rp&>::promise()
1592 : __state_(new __assoc_state<_Rp&>)
Howard Hinnant47499b12010-08-27 20:10:191593{
1594}
1595
Howard Hinnant99968442011-11-29 18:15:501596template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191597template <class _Alloc>
Howard Hinnant99968442011-11-29 18:15:501598promise<_Rp&>::promise(allocator_arg_t, const _Alloc& __a0)
Howard Hinnant47499b12010-08-27 20:10:191599{
Eric Fiselier4d2413c2014-10-23 06:24:451600 typedef __assoc_state_alloc<_Rp&, _Alloc> _State;
1601 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
Howard Hinnant47499b12010-08-27 20:10:191602 typedef __allocator_destructor<_A2> _D2;
1603 _A2 __a(__a0);
Eric Fiselier4d2413c2014-10-23 06:24:451604 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1605 ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0);
1606 __state_ = _VSTD::addressof(*__hold.release());
Howard Hinnant47499b12010-08-27 20:10:191607}
1608
Howard Hinnant99968442011-11-29 18:15:501609template <class _Rp>
1610promise<_Rp&>::~promise()
Howard Hinnant47499b12010-08-27 20:10:191611{
1612 if (__state_)
1613 {
1614 if (!__state_->__has_value() && __state_->use_count() > 1)
1615 __state_->set_exception(make_exception_ptr(
1616 future_error(make_error_code(future_errc::broken_promise))
1617 ));
1618 __state_->__release_shared();
1619 }
1620}
1621
Howard Hinnant99968442011-11-29 18:15:501622template <class _Rp>
1623future<_Rp&>
1624promise<_Rp&>::get_future()
Howard Hinnant47499b12010-08-27 20:10:191625{
Howard Hinnant22ba71b2011-07-13 16:00:501626#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:191627 if (__state_ == nullptr)
1628 throw future_error(make_error_code(future_errc::no_state));
Howard Hinnant22ba71b2011-07-13 16:00:501629#endif
Howard Hinnant99968442011-11-29 18:15:501630 return future<_Rp&>(__state_);
Howard Hinnant47499b12010-08-27 20:10:191631}
1632
Howard Hinnant99968442011-11-29 18:15:501633template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191634void
Howard Hinnant99968442011-11-29 18:15:501635promise<_Rp&>::set_value(_Rp& __r)
Howard Hinnant47499b12010-08-27 20:10:191636{
Howard Hinnant22ba71b2011-07-13 16:00:501637#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:191638 if (__state_ == nullptr)
1639 throw future_error(make_error_code(future_errc::no_state));
Howard Hinnant22ba71b2011-07-13 16:00:501640#endif
Howard Hinnant47499b12010-08-27 20:10:191641 __state_->set_value(__r);
1642}
1643
Howard Hinnant99968442011-11-29 18:15:501644template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191645void
Howard Hinnant99968442011-11-29 18:15:501646promise<_Rp&>::set_exception(exception_ptr __p)
Howard Hinnant47499b12010-08-27 20:10:191647{
Howard Hinnant22ba71b2011-07-13 16:00:501648#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:191649 if (__state_ == nullptr)
1650 throw future_error(make_error_code(future_errc::no_state));
Howard Hinnant22ba71b2011-07-13 16:00:501651#endif
Howard Hinnant47499b12010-08-27 20:10:191652 __state_->set_exception(__p);
1653}
1654
Howard Hinnant99968442011-11-29 18:15:501655template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191656void
Howard Hinnant99968442011-11-29 18:15:501657promise<_Rp&>::set_value_at_thread_exit(_Rp& __r)
Howard Hinnant47499b12010-08-27 20:10:191658{
Howard Hinnant22ba71b2011-07-13 16:00:501659#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:191660 if (__state_ == nullptr)
1661 throw future_error(make_error_code(future_errc::no_state));
Howard Hinnant22ba71b2011-07-13 16:00:501662#endif
Howard Hinnant47499b12010-08-27 20:10:191663 __state_->set_value_at_thread_exit(__r);
1664}
1665
Howard Hinnant99968442011-11-29 18:15:501666template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191667void
Howard Hinnant99968442011-11-29 18:15:501668promise<_Rp&>::set_exception_at_thread_exit(exception_ptr __p)
Howard Hinnant47499b12010-08-27 20:10:191669{
Howard Hinnant22ba71b2011-07-13 16:00:501670#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:191671 if (__state_ == nullptr)
1672 throw future_error(make_error_code(future_errc::no_state));
Howard Hinnant22ba71b2011-07-13 16:00:501673#endif
Howard Hinnant47499b12010-08-27 20:10:191674 __state_->set_exception_at_thread_exit(__p);
1675}
1676
1677// promise<void>
1678
1679template <>
Howard Hinnant83eade62013-03-06 23:30:191680class _LIBCPP_TYPE_VIS promise<void>
Howard Hinnant47499b12010-08-27 20:10:191681{
1682 __assoc_sub_state* __state_;
Howard Hinnant54da3382010-08-30 18:46:211683
Howard Hinnant8c6cbb22010-09-22 14:16:261684 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551685 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant54da3382010-08-30 18:46:211686
1687 template <class> friend class packaged_task;
1688
Howard Hinnant47499b12010-08-27 20:10:191689public:
1690 promise();
1691 template <class _Allocator>
1692 promise(allocator_arg_t, const _Allocator& __a);
Howard Hinnant73d21a42010-09-04 23:28:191693#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261694 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551695 promise(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191696 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1697 promise(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:191698#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191699private:
1700 promise(const promise& __rhs);
1701public:
Howard Hinnant73d21a42010-09-04 23:28:191702#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191703 ~promise();
1704
1705 // assignment
Howard Hinnant73d21a42010-09-04 23:28:191706#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261707 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551708 promise& operator=(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191709 {
1710 promise(std::move(__rhs)).swap(*this);
1711 return *this;
1712 }
1713 promise& operator=(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:191714#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191715private:
1716 promise& operator=(const promise& __rhs);
1717public:
Howard Hinnant73d21a42010-09-04 23:28:191718#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261719 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551720 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:191721
1722 // retrieving the result
1723 future<void> get_future();
1724
1725 // setting the result
1726 void set_value();
1727 void set_exception(exception_ptr __p);
1728
1729 // setting the result with deferred notification
1730 void set_value_at_thread_exit();
1731 void set_exception_at_thread_exit(exception_ptr __p);
1732};
1733
1734template <class _Alloc>
1735promise<void>::promise(allocator_arg_t, const _Alloc& __a0)
1736{
Eric Fiselier4d2413c2014-10-23 06:24:451737 typedef __assoc_sub_state_alloc<_Alloc> _State;
1738 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
Howard Hinnant47499b12010-08-27 20:10:191739 typedef __allocator_destructor<_A2> _D2;
1740 _A2 __a(__a0);
Eric Fiselier4d2413c2014-10-23 06:24:451741 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1742 ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0);
1743 __state_ = _VSTD::addressof(*__hold.release());
Howard Hinnant47499b12010-08-27 20:10:191744}
1745
Howard Hinnant99968442011-11-29 18:15:501746template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191747inline _LIBCPP_INLINE_VISIBILITY
1748void
Howard Hinnant8bf01dd2012-07-21 17:46:551749swap(promise<_Rp>& __x, promise<_Rp>& __y) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191750{
1751 __x.swap(__y);
1752}
1753
Howard Hinnant99968442011-11-29 18:15:501754template <class _Rp, class _Alloc>
Howard Hinnant0f678bd2013-08-12 18:38:341755 struct _LIBCPP_TYPE_VIS_ONLY uses_allocator<promise<_Rp>, _Alloc>
Howard Hinnant8c6cbb22010-09-22 14:16:261756 : public true_type {};
Howard Hinnant47499b12010-08-27 20:10:191757
Howard Hinnant54da3382010-08-30 18:46:211758#ifndef _LIBCPP_HAS_NO_VARIADICS
1759
1760// packaged_task
1761
1762template<class _Fp> class __packaged_task_base;
1763
Howard Hinnant99968442011-11-29 18:15:501764template<class _Rp, class ..._ArgTypes>
1765class __packaged_task_base<_Rp(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:211766{
1767 __packaged_task_base(const __packaged_task_base&);
1768 __packaged_task_base& operator=(const __packaged_task_base&);
1769public:
Howard Hinnant8c6cbb22010-09-22 14:16:261770 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:211771 __packaged_task_base() {}
Howard Hinnant8c6cbb22010-09-22 14:16:261772 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:211773 virtual ~__packaged_task_base() {}
Howard Hinnant8bf01dd2012-07-21 17:46:551774 virtual void __move_to(__packaged_task_base*) _NOEXCEPT = 0;
Howard Hinnant54da3382010-08-30 18:46:211775 virtual void destroy() = 0;
1776 virtual void destroy_deallocate() = 0;
Howard Hinnant99968442011-11-29 18:15:501777 virtual _Rp operator()(_ArgTypes&& ...) = 0;
Howard Hinnant54da3382010-08-30 18:46:211778};
1779
1780template<class _FD, class _Alloc, class _FB> class __packaged_task_func;
1781
Howard Hinnant99968442011-11-29 18:15:501782template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1783class __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>
1784 : public __packaged_task_base<_Rp(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:211785{
Howard Hinnant99968442011-11-29 18:15:501786 __compressed_pair<_Fp, _Alloc> __f_;
Howard Hinnant54da3382010-08-30 18:46:211787public:
Howard Hinnant8c6cbb22010-09-22 14:16:261788 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:501789 explicit __packaged_task_func(const _Fp& __f) : __f_(__f) {}
Howard Hinnant8c6cbb22010-09-22 14:16:261790 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:501791 explicit __packaged_task_func(_Fp&& __f) : __f_(_VSTD::move(__f)) {}
Howard Hinnant8c6cbb22010-09-22 14:16:261792 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:501793 __packaged_task_func(const _Fp& __f, const _Alloc& __a)
Howard Hinnant54da3382010-08-30 18:46:211794 : __f_(__f, __a) {}
Howard Hinnant8c6cbb22010-09-22 14:16:261795 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:501796 __packaged_task_func(_Fp&& __f, const _Alloc& __a)
Howard Hinnant0949eed2011-06-30 21:18:191797 : __f_(_VSTD::move(__f), __a) {}
Howard Hinnant8bf01dd2012-07-21 17:46:551798 virtual void __move_to(__packaged_task_base<_Rp(_ArgTypes...)>*) _NOEXCEPT;
Howard Hinnant54da3382010-08-30 18:46:211799 virtual void destroy();
1800 virtual void destroy_deallocate();
Howard Hinnant99968442011-11-29 18:15:501801 virtual _Rp operator()(_ArgTypes&& ... __args);
Howard Hinnant54da3382010-08-30 18:46:211802};
1803
Howard Hinnant99968442011-11-29 18:15:501804template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:211805void
Howard Hinnant99968442011-11-29 18:15:501806__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__move_to(
Howard Hinnant8bf01dd2012-07-21 17:46:551807 __packaged_task_base<_Rp(_ArgTypes...)>* __p) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:211808{
Howard Hinnant0949eed2011-06-30 21:18:191809 ::new (__p) __packaged_task_func(_VSTD::move(__f_.first()), _VSTD::move(__f_.second()));
Howard Hinnant54da3382010-08-30 18:46:211810}
1811
Howard Hinnant99968442011-11-29 18:15:501812template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:211813void
Howard Hinnant99968442011-11-29 18:15:501814__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy()
Howard Hinnant54da3382010-08-30 18:46:211815{
Howard Hinnant99968442011-11-29 18:15:501816 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnant54da3382010-08-30 18:46:211817}
1818
Howard Hinnant99968442011-11-29 18:15:501819template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:211820void
Howard Hinnant99968442011-11-29 18:15:501821__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate()
Howard Hinnant54da3382010-08-30 18:46:211822{
Eric Fiselier4d2413c2014-10-23 06:24:451823 typedef typename __allocator_traits_rebind<_Alloc, __packaged_task_func>::type _Ap;
1824 typedef allocator_traits<_Ap> _ATraits;
1825 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Howard Hinnant99968442011-11-29 18:15:501826 _Ap __a(__f_.second());
1827 __f_.~__compressed_pair<_Fp, _Alloc>();
Eric Fiselier4d2413c2014-10-23 06:24:451828 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnant54da3382010-08-30 18:46:211829}
1830
Howard Hinnant99968442011-11-29 18:15:501831template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1832_Rp
1833__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
Howard Hinnant54da3382010-08-30 18:46:211834{
Howard Hinnant0949eed2011-06-30 21:18:191835 return __invoke(__f_.first(), _VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnant54da3382010-08-30 18:46:211836}
1837
Howard Hinnant2b1b2d42011-06-14 19:58:171838template <class _Callable> class __packaged_task_function;
Howard Hinnant54da3382010-08-30 18:46:211839
Howard Hinnant99968442011-11-29 18:15:501840template<class _Rp, class ..._ArgTypes>
1841class __packaged_task_function<_Rp(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:211842{
Howard Hinnant99968442011-11-29 18:15:501843 typedef __packaged_task_base<_Rp(_ArgTypes...)> __base;
Howard Hinnant78f0de22013-01-21 17:26:551844 typename aligned_storage<3*sizeof(void*)>::type __buf_;
Howard Hinnant54da3382010-08-30 18:46:211845 __base* __f_;
1846
1847public:
Howard Hinnant99968442011-11-29 18:15:501848 typedef _Rp result_type;
Howard Hinnant54da3382010-08-30 18:46:211849
1850 // construct/copy/destroy:
Howard Hinnant8c6cbb22010-09-22 14:16:261851 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551852 __packaged_task_function() _NOEXCEPT : __f_(nullptr) {}
Howard Hinnant99968442011-11-29 18:15:501853 template<class _Fp>
1854 __packaged_task_function(_Fp&& __f);
1855 template<class _Fp, class _Alloc>
1856 __packaged_task_function(allocator_arg_t, const _Alloc& __a, _Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:211857
Howard Hinnant8bf01dd2012-07-21 17:46:551858 __packaged_task_function(__packaged_task_function&&) _NOEXCEPT;
1859 __packaged_task_function& operator=(__packaged_task_function&&) _NOEXCEPT;
Howard Hinnant54da3382010-08-30 18:46:211860
1861 __packaged_task_function(const __packaged_task_function&) = delete;
1862 __packaged_task_function& operator=(const __packaged_task_function&) = delete;
1863
1864 ~__packaged_task_function();
1865
Howard Hinnant8bf01dd2012-07-21 17:46:551866 void swap(__packaged_task_function&) _NOEXCEPT;
Howard Hinnant54da3382010-08-30 18:46:211867
Howard Hinnant99968442011-11-29 18:15:501868 _Rp operator()(_ArgTypes...) const;
Howard Hinnant54da3382010-08-30 18:46:211869};
1870
Howard Hinnant99968442011-11-29 18:15:501871template<class _Rp, class ..._ArgTypes>
Howard Hinnant8bf01dd2012-07-21 17:46:551872__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:211873{
1874 if (__f.__f_ == nullptr)
1875 __f_ = nullptr;
1876 else if (__f.__f_ == (__base*)&__f.__buf_)
1877 {
1878 __f_ = (__base*)&__buf_;
1879 __f.__f_->__move_to(__f_);
1880 }
1881 else
1882 {
1883 __f_ = __f.__f_;
1884 __f.__f_ = nullptr;
1885 }
1886}
1887
Howard Hinnant99968442011-11-29 18:15:501888template<class _Rp, class ..._ArgTypes>
1889template <class _Fp>
1890__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(_Fp&& __f)
Howard Hinnant54da3382010-08-30 18:46:211891 : __f_(nullptr)
1892{
Marshall Clowf1264e72014-04-07 13:32:261893 typedef typename remove_reference<typename decay<_Fp>::type>::type _FR;
Howard Hinnant99968442011-11-29 18:15:501894 typedef __packaged_task_func<_FR, allocator<_FR>, _Rp(_ArgTypes...)> _FF;
Howard Hinnant54da3382010-08-30 18:46:211895 if (sizeof(_FF) <= sizeof(__buf_))
1896 {
1897 __f_ = (__base*)&__buf_;
Howard Hinnant99968442011-11-29 18:15:501898 ::new (__f_) _FF(_VSTD::forward<_Fp>(__f));
Howard Hinnant54da3382010-08-30 18:46:211899 }
1900 else
1901 {
Howard Hinnant99968442011-11-29 18:15:501902 typedef allocator<_FF> _Ap;
1903 _Ap __a;
1904 typedef __allocator_destructor<_Ap> _Dp;
1905 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1906 ::new (__hold.get()) _FF(_VSTD::forward<_Fp>(__f), allocator<_FR>(__a));
Howard Hinnant54da3382010-08-30 18:46:211907 __f_ = __hold.release();
1908 }
1909}
1910
Howard Hinnant99968442011-11-29 18:15:501911template<class _Rp, class ..._ArgTypes>
1912template <class _Fp, class _Alloc>
1913__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(
1914 allocator_arg_t, const _Alloc& __a0, _Fp&& __f)
Howard Hinnant54da3382010-08-30 18:46:211915 : __f_(nullptr)
1916{
Marshall Clowf1264e72014-04-07 13:32:261917 typedef typename remove_reference<typename decay<_Fp>::type>::type _FR;
Howard Hinnant99968442011-11-29 18:15:501918 typedef __packaged_task_func<_FR, _Alloc, _Rp(_ArgTypes...)> _FF;
Howard Hinnant54da3382010-08-30 18:46:211919 if (sizeof(_FF) <= sizeof(__buf_))
1920 {
1921 __f_ = (__base*)&__buf_;
Howard Hinnant99968442011-11-29 18:15:501922 ::new (__f_) _FF(_VSTD::forward<_Fp>(__f));
Howard Hinnant54da3382010-08-30 18:46:211923 }
1924 else
1925 {
Eric Fiselier4d2413c2014-10-23 06:24:451926 typedef typename __allocator_traits_rebind<_Alloc, _FF>::type _Ap;
Howard Hinnant99968442011-11-29 18:15:501927 _Ap __a(__a0);
1928 typedef __allocator_destructor<_Ap> _Dp;
1929 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Eric Fiselier4d2413c2014-10-23 06:24:451930 ::new (static_cast<void*>(_VSTD::addressof(*__hold.get())))
1931 _FF(_VSTD::forward<_Fp>(__f), _Alloc(__a));
1932 __f_ = _VSTD::addressof(*__hold.release());
Howard Hinnant54da3382010-08-30 18:46:211933 }
1934}
1935
Howard Hinnant99968442011-11-29 18:15:501936template<class _Rp, class ..._ArgTypes>
1937__packaged_task_function<_Rp(_ArgTypes...)>&
Howard Hinnant8bf01dd2012-07-21 17:46:551938__packaged_task_function<_Rp(_ArgTypes...)>::operator=(__packaged_task_function&& __f) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:211939{
1940 if (__f_ == (__base*)&__buf_)
1941 __f_->destroy();
1942 else if (__f_)
1943 __f_->destroy_deallocate();
1944 __f_ = nullptr;
1945 if (__f.__f_ == nullptr)
1946 __f_ = nullptr;
1947 else if (__f.__f_ == (__base*)&__f.__buf_)
1948 {
1949 __f_ = (__base*)&__buf_;
1950 __f.__f_->__move_to(__f_);
1951 }
1952 else
1953 {
1954 __f_ = __f.__f_;
1955 __f.__f_ = nullptr;
1956 }
Argyrios Kyrtzidis1dc6f7a2012-10-13 02:03:451957 return *this;
Howard Hinnant54da3382010-08-30 18:46:211958}
1959
Howard Hinnant99968442011-11-29 18:15:501960template<class _Rp, class ..._ArgTypes>
1961__packaged_task_function<_Rp(_ArgTypes...)>::~__packaged_task_function()
Howard Hinnant54da3382010-08-30 18:46:211962{
1963 if (__f_ == (__base*)&__buf_)
1964 __f_->destroy();
1965 else if (__f_)
1966 __f_->destroy_deallocate();
1967}
1968
Howard Hinnant99968442011-11-29 18:15:501969template<class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:211970void
Howard Hinnant8bf01dd2012-07-21 17:46:551971__packaged_task_function<_Rp(_ArgTypes...)>::swap(__packaged_task_function& __f) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:211972{
1973 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1974 {
1975 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1976 __base* __t = (__base*)&__tempbuf;
1977 __f_->__move_to(__t);
1978 __f_->destroy();
1979 __f_ = nullptr;
1980 __f.__f_->__move_to((__base*)&__buf_);
1981 __f.__f_->destroy();
1982 __f.__f_ = nullptr;
1983 __f_ = (__base*)&__buf_;
1984 __t->__move_to((__base*)&__f.__buf_);
1985 __t->destroy();
1986 __f.__f_ = (__base*)&__f.__buf_;
1987 }
1988 else if (__f_ == (__base*)&__buf_)
1989 {
1990 __f_->__move_to((__base*)&__f.__buf_);
1991 __f_->destroy();
1992 __f_ = __f.__f_;
1993 __f.__f_ = (__base*)&__f.__buf_;
1994 }
1995 else if (__f.__f_ == (__base*)&__f.__buf_)
1996 {
1997 __f.__f_->__move_to((__base*)&__buf_);
1998 __f.__f_->destroy();
1999 __f.__f_ = __f_;
2000 __f_ = (__base*)&__buf_;
2001 }
2002 else
Howard Hinnant0949eed2011-06-30 21:18:192003 _VSTD::swap(__f_, __f.__f_);
Howard Hinnant54da3382010-08-30 18:46:212004}
2005
Howard Hinnant99968442011-11-29 18:15:502006template<class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:212007inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502008_Rp
2009__packaged_task_function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
Howard Hinnant54da3382010-08-30 18:46:212010{
Howard Hinnant0949eed2011-06-30 21:18:192011 return (*__f_)(_VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnant54da3382010-08-30 18:46:212012}
2013
Howard Hinnant99968442011-11-29 18:15:502014template<class _Rp, class ..._ArgTypes>
Howard Hinnant0f678bd2013-08-12 18:38:342015class _LIBCPP_TYPE_VIS_ONLY packaged_task<_Rp(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:212016{
2017public:
Howard Hinnant99968442011-11-29 18:15:502018 typedef _Rp result_type;
Howard Hinnant54da3382010-08-30 18:46:212019
2020private:
2021 __packaged_task_function<result_type(_ArgTypes...)> __f_;
2022 promise<result_type> __p_;
2023
2024public:
2025 // construction and destruction
Howard Hinnant8c6cbb22010-09-22 14:16:262026 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552027 packaged_task() _NOEXCEPT : __p_(nullptr) {}
Marshall Clow5f2d5b92013-10-12 22:49:172028 template <class _Fp,
2029 class = typename enable_if
2030 <
2031 !is_same<
2032 typename decay<_Fp>::type,
2033 packaged_task
2034 >::value
2035 >::type
2036 >
Howard Hinnant8c6cbb22010-09-22 14:16:262037 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502038 explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
Marshall Clow5f2d5b92013-10-12 22:49:172039 template <class _Fp, class _Allocator,
2040 class = typename enable_if
2041 <
2042 !is_same<
2043 typename decay<_Fp>::type,
2044 packaged_task
2045 >::value
2046 >::type
2047 >
Howard Hinnant8c6cbb22010-09-22 14:16:262048 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502049 explicit packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
2050 : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)),
Howard Hinnant54da3382010-08-30 18:46:212051 __p_(allocator_arg, __a) {}
2052 // ~packaged_task() = default;
2053
2054 // no copy
Howard Hinnant8131a012012-07-21 19:34:122055 packaged_task(const packaged_task&) = delete;
2056 packaged_task& operator=(const packaged_task&) = delete;
Howard Hinnant54da3382010-08-30 18:46:212057
2058 // move support
Howard Hinnant8c6cbb22010-09-22 14:16:262059 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552060 packaged_task(packaged_task&& __other) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:192061 : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
Howard Hinnant8c6cbb22010-09-22 14:16:262062 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552063 packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:212064 {
Howard Hinnant0949eed2011-06-30 21:18:192065 __f_ = _VSTD::move(__other.__f_);
2066 __p_ = _VSTD::move(__other.__p_);
Howard Hinnant54da3382010-08-30 18:46:212067 return *this;
2068 }
Howard Hinnant8c6cbb22010-09-22 14:16:262069 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552070 void swap(packaged_task& __other) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:212071 {
2072 __f_.swap(__other.__f_);
2073 __p_.swap(__other.__p_);
2074 }
2075
Howard Hinnant8c6cbb22010-09-22 14:16:262076 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552077 bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
Howard Hinnant54da3382010-08-30 18:46:212078
2079 // result retrieval
Howard Hinnant8c6cbb22010-09-22 14:16:262080 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:212081 future<result_type> get_future() {return __p_.get_future();}
2082
2083 // execution
2084 void operator()(_ArgTypes... __args);
2085 void make_ready_at_thread_exit(_ArgTypes... __args);
2086
2087 void reset();
2088};
2089
Howard Hinnant99968442011-11-29 18:15:502090template<class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:212091void
Howard Hinnant99968442011-11-29 18:15:502092packaged_task<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __args)
Howard Hinnant54da3382010-08-30 18:46:212093{
2094#ifndef _LIBCPP_NO_EXCEPTIONS
2095 if (__p_.__state_ == nullptr)
2096 throw future_error(make_error_code(future_errc::no_state));
2097 if (__p_.__state_->__has_value())
2098 throw future_error(make_error_code(future_errc::promise_already_satisfied));
2099 try
2100 {
2101#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:192102 __p_.set_value(__f_(_VSTD::forward<_ArgTypes>(__args)...));
Howard Hinnant54da3382010-08-30 18:46:212103#ifndef _LIBCPP_NO_EXCEPTIONS
2104 }
2105 catch (...)
2106 {
2107 __p_.set_exception(current_exception());
2108 }
2109#endif // _LIBCPP_NO_EXCEPTIONS
2110}
2111
Howard Hinnant99968442011-11-29 18:15:502112template<class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:212113void
Howard Hinnant99968442011-11-29 18:15:502114packaged_task<_Rp(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
Howard Hinnant54da3382010-08-30 18:46:212115{
2116#ifndef _LIBCPP_NO_EXCEPTIONS
2117 if (__p_.__state_ == nullptr)
2118 throw future_error(make_error_code(future_errc::no_state));
2119 if (__p_.__state_->__has_value())
2120 throw future_error(make_error_code(future_errc::promise_already_satisfied));
2121 try
2122 {
2123#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:192124 __p_.set_value_at_thread_exit(__f_(_VSTD::forward<_ArgTypes>(__args)...));
Howard Hinnant54da3382010-08-30 18:46:212125#ifndef _LIBCPP_NO_EXCEPTIONS
2126 }
2127 catch (...)
2128 {
2129 __p_.set_exception_at_thread_exit(current_exception());
2130 }
2131#endif // _LIBCPP_NO_EXCEPTIONS
2132}
2133
Howard Hinnant99968442011-11-29 18:15:502134template<class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:212135void
Howard Hinnant99968442011-11-29 18:15:502136packaged_task<_Rp(_ArgTypes...)>::reset()
Howard Hinnant54da3382010-08-30 18:46:212137{
2138#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant7de47902010-11-30 20:23:322139 if (!valid())
Howard Hinnant54da3382010-08-30 18:46:212140 throw future_error(make_error_code(future_errc::no_state));
2141#endif // _LIBCPP_NO_EXCEPTIONS
2142 __p_ = promise<result_type>();
2143}
2144
2145template<class ..._ArgTypes>
Howard Hinnant0f678bd2013-08-12 18:38:342146class _LIBCPP_TYPE_VIS_ONLY packaged_task<void(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:212147{
2148public:
2149 typedef void result_type;
2150
2151private:
2152 __packaged_task_function<result_type(_ArgTypes...)> __f_;
2153 promise<result_type> __p_;
2154
2155public:
2156 // construction and destruction
Howard Hinnant8c6cbb22010-09-22 14:16:262157 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552158 packaged_task() _NOEXCEPT : __p_(nullptr) {}
Marshall Clow5f2d5b92013-10-12 22:49:172159 template <class _Fp,
2160 class = typename enable_if
2161 <
2162 !is_same<
2163 typename decay<_Fp>::type,
2164 packaged_task
2165 >::value
2166 >::type
2167 >
Howard Hinnant8c6cbb22010-09-22 14:16:262168 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502169 explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
Marshall Clow5f2d5b92013-10-12 22:49:172170 template <class _Fp, class _Allocator,
2171 class = typename enable_if
2172 <
2173 !is_same<
2174 typename decay<_Fp>::type,
2175 packaged_task
2176 >::value
2177 >::type
2178 >
Howard Hinnant8c6cbb22010-09-22 14:16:262179 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502180 explicit packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
2181 : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)),
Howard Hinnant54da3382010-08-30 18:46:212182 __p_(allocator_arg, __a) {}
2183 // ~packaged_task() = default;
2184
2185 // no copy
Howard Hinnant8131a012012-07-21 19:34:122186 packaged_task(const packaged_task&) = delete;
2187 packaged_task& operator=(const packaged_task&) = delete;
Howard Hinnant54da3382010-08-30 18:46:212188
2189 // move support
Howard Hinnant8c6cbb22010-09-22 14:16:262190 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552191 packaged_task(packaged_task&& __other) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:192192 : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
Howard Hinnant8c6cbb22010-09-22 14:16:262193 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552194 packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:212195 {
Howard Hinnant0949eed2011-06-30 21:18:192196 __f_ = _VSTD::move(__other.__f_);
2197 __p_ = _VSTD::move(__other.__p_);
Howard Hinnant54da3382010-08-30 18:46:212198 return *this;
2199 }
Howard Hinnant8c6cbb22010-09-22 14:16:262200 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552201 void swap(packaged_task& __other) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:212202 {
2203 __f_.swap(__other.__f_);
2204 __p_.swap(__other.__p_);
2205 }
2206
Howard Hinnant8c6cbb22010-09-22 14:16:262207 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552208 bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
Howard Hinnant54da3382010-08-30 18:46:212209
2210 // result retrieval
Howard Hinnant8c6cbb22010-09-22 14:16:262211 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:212212 future<result_type> get_future() {return __p_.get_future();}
2213
2214 // execution
2215 void operator()(_ArgTypes... __args);
2216 void make_ready_at_thread_exit(_ArgTypes... __args);
2217
2218 void reset();
2219};
2220
2221template<class ..._ArgTypes>
2222void
2223packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args)
2224{
2225#ifndef _LIBCPP_NO_EXCEPTIONS
2226 if (__p_.__state_ == nullptr)
2227 throw future_error(make_error_code(future_errc::no_state));
2228 if (__p_.__state_->__has_value())
2229 throw future_error(make_error_code(future_errc::promise_already_satisfied));
2230 try
2231 {
2232#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:192233 __f_(_VSTD::forward<_ArgTypes>(__args)...);
Howard Hinnant54da3382010-08-30 18:46:212234 __p_.set_value();
2235#ifndef _LIBCPP_NO_EXCEPTIONS
2236 }
2237 catch (...)
2238 {
2239 __p_.set_exception(current_exception());
2240 }
2241#endif // _LIBCPP_NO_EXCEPTIONS
2242}
2243
2244template<class ..._ArgTypes>
2245void
2246packaged_task<void(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
2247{
2248#ifndef _LIBCPP_NO_EXCEPTIONS
2249 if (__p_.__state_ == nullptr)
2250 throw future_error(make_error_code(future_errc::no_state));
2251 if (__p_.__state_->__has_value())
2252 throw future_error(make_error_code(future_errc::promise_already_satisfied));
2253 try
2254 {
2255#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:192256 __f_(_VSTD::forward<_ArgTypes>(__args)...);
Howard Hinnant54da3382010-08-30 18:46:212257 __p_.set_value_at_thread_exit();
2258#ifndef _LIBCPP_NO_EXCEPTIONS
2259 }
2260 catch (...)
2261 {
2262 __p_.set_exception_at_thread_exit(current_exception());
2263 }
2264#endif // _LIBCPP_NO_EXCEPTIONS
2265}
2266
2267template<class ..._ArgTypes>
2268void
2269packaged_task<void(_ArgTypes...)>::reset()
2270{
2271#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant7de47902010-11-30 20:23:322272 if (!valid())
Howard Hinnant54da3382010-08-30 18:46:212273 throw future_error(make_error_code(future_errc::no_state));
2274#endif // _LIBCPP_NO_EXCEPTIONS
2275 __p_ = promise<result_type>();
2276}
2277
2278template <class _Callable>
2279inline _LIBCPP_INLINE_VISIBILITY
2280void
Howard Hinnant8bf01dd2012-07-21 17:46:552281swap(packaged_task<_Callable>& __x, packaged_task<_Callable>& __y) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:212282{
2283 __x.swap(__y);
2284}
2285
2286template <class _Callable, class _Alloc>
Howard Hinnant0f678bd2013-08-12 18:38:342287struct _LIBCPP_TYPE_VIS_ONLY uses_allocator<packaged_task<_Callable>, _Alloc>
Howard Hinnant8c6cbb22010-09-22 14:16:262288 : public true_type {};
Howard Hinnant54da3382010-08-30 18:46:212289
Howard Hinnant99968442011-11-29 18:15:502290template <class _Rp, class _Fp>
2291future<_Rp>
Howard Hinnant73d21a42010-09-04 23:28:192292#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:502293__make_deferred_assoc_state(_Fp&& __f)
Howard Hinnant54da3382010-08-30 18:46:212294#else
Howard Hinnant99968442011-11-29 18:15:502295__make_deferred_assoc_state(_Fp __f)
Howard Hinnant54da3382010-08-30 18:46:212296#endif
2297{
Howard Hinnant99968442011-11-29 18:15:502298 unique_ptr<__deferred_assoc_state<_Rp, _Fp>, __release_shared_count>
2299 __h(new __deferred_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
2300 return future<_Rp>(__h.get());
Howard Hinnant54da3382010-08-30 18:46:212301}
2302
Howard Hinnant99968442011-11-29 18:15:502303template <class _Rp, class _Fp>
2304future<_Rp>
Howard Hinnant57cff292011-05-19 15:05:042305#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:502306__make_async_assoc_state(_Fp&& __f)
Howard Hinnant57cff292011-05-19 15:05:042307#else
Howard Hinnant99968442011-11-29 18:15:502308__make_async_assoc_state(_Fp __f)
Howard Hinnant57cff292011-05-19 15:05:042309#endif
2310{
Howard Hinnant99968442011-11-29 18:15:502311 unique_ptr<__async_assoc_state<_Rp, _Fp>, __release_shared_count>
2312 __h(new __async_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
2313 _VSTD::thread(&__async_assoc_state<_Rp, _Fp>::__execute, __h.get()).detach();
2314 return future<_Rp>(__h.get());
Howard Hinnant57cff292011-05-19 15:05:042315}
2316
Howard Hinnant99968442011-11-29 18:15:502317template <class _Fp, class... _Args>
Howard Hinnant57cff292011-05-19 15:05:042318class __async_func
2319{
Howard Hinnant99968442011-11-29 18:15:502320 tuple<_Fp, _Args...> __f_;
Howard Hinnant57cff292011-05-19 15:05:042321
2322public:
Howard Hinnant99968442011-11-29 18:15:502323 typedef typename __invoke_of<_Fp, _Args...>::type _Rp;
Howard Hinnant57cff292011-05-19 15:05:042324
2325 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502326 explicit __async_func(_Fp&& __f, _Args&&... __args)
Howard Hinnant0949eed2011-06-30 21:18:192327 : __f_(_VSTD::move(__f), _VSTD::move(__args)...) {}
Howard Hinnant57cff292011-05-19 15:05:042328
2329 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0949eed2011-06-30 21:18:192330 __async_func(__async_func&& __f) : __f_(_VSTD::move(__f.__f_)) {}
Howard Hinnant57cff292011-05-19 15:05:042331
Howard Hinnant99968442011-11-29 18:15:502332 _Rp operator()()
Howard Hinnant57cff292011-05-19 15:05:042333 {
2334 typedef typename __make_tuple_indices<1+sizeof...(_Args), 1>::type _Index;
2335 return __execute(_Index());
2336 }
2337private:
2338 template <size_t ..._Indices>
Howard Hinnant99968442011-11-29 18:15:502339 _Rp
Howard Hinnant57cff292011-05-19 15:05:042340 __execute(__tuple_indices<_Indices...>)
2341 {
Howard Hinnant0949eed2011-06-30 21:18:192342 return __invoke(_VSTD::move(_VSTD::get<0>(__f_)), _VSTD::move(_VSTD::get<_Indices>(__f_))...);
Howard Hinnant57cff292011-05-19 15:05:042343 }
2344};
2345
Marshall Clow3b3108e2013-11-03 22:06:532346inline _LIBCPP_INLINE_VISIBILITY bool __does_policy_contain(launch __policy, launch __value )
Marshall Clowad2a6002013-11-03 15:43:352347{ return (int(__policy) & int(__value)) != 0; }
2348
Howard Hinnant99968442011-11-29 18:15:502349template <class _Fp, class... _Args>
2350future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
2351async(launch __policy, _Fp&& __f, _Args&&... __args)
Howard Hinnant54da3382010-08-30 18:46:212352{
Howard Hinnant99968442011-11-29 18:15:502353 typedef __async_func<typename decay<_Fp>::type, typename decay<_Args>::type...> _BF;
2354 typedef typename _BF::_Rp _Rp;
Marshall Clowad2a6002013-11-03 15:43:352355
2356#ifndef _LIBCPP_NO_EXCEPTIONS
2357 try
2358 {
2359#endif
2360 if (__does_policy_contain(__policy, launch::async))
2361 return _VSTD::__make_async_assoc_state<_Rp>(_BF(__decay_copy(_VSTD::forward<_Fp>(__f)),
Howard Hinnant0949eed2011-06-30 21:18:192362 __decay_copy(_VSTD::forward<_Args>(__args))...));
Marshall Clowad2a6002013-11-03 15:43:352363#ifndef _LIBCPP_NO_EXCEPTIONS
2364 }
2365 catch ( ... ) { if (__policy == launch::async) throw ; }
2366#endif
2367
2368 if (__does_policy_contain(__policy, launch::deferred))
2369 return _VSTD::__make_deferred_assoc_state<_Rp>(_BF(__decay_copy(_VSTD::forward<_Fp>(__f)),
Howard Hinnant0949eed2011-06-30 21:18:192370 __decay_copy(_VSTD::forward<_Args>(__args))...));
Marshall Clowad2a6002013-11-03 15:43:352371 return future<_Rp>{};
Howard Hinnant54da3382010-08-30 18:46:212372}
2373
Howard Hinnant99968442011-11-29 18:15:502374template <class _Fp, class... _Args>
Howard Hinnant54da3382010-08-30 18:46:212375inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502376future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
2377async(_Fp&& __f, _Args&&... __args)
Howard Hinnant54da3382010-08-30 18:46:212378{
Howard Hinnant99968442011-11-29 18:15:502379 return _VSTD::async(launch::any, _VSTD::forward<_Fp>(__f),
Howard Hinnant0949eed2011-06-30 21:18:192380 _VSTD::forward<_Args>(__args)...);
Howard Hinnant54da3382010-08-30 18:46:212381}
2382
2383#endif // _LIBCPP_HAS_NO_VARIADICS
2384
Howard Hinnante6e4d012010-09-03 21:46:372385// shared_future
2386
Howard Hinnant99968442011-11-29 18:15:502387template <class _Rp>
Howard Hinnant0f678bd2013-08-12 18:38:342388class _LIBCPP_TYPE_VIS_ONLY shared_future
Howard Hinnant99be8232010-09-03 18:39:252389{
Howard Hinnant99968442011-11-29 18:15:502390 __assoc_state<_Rp>* __state_;
Howard Hinnant99be8232010-09-03 18:39:252391
2392public:
Howard Hinnant8c6cbb22010-09-22 14:16:262393 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552394 shared_future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant8c6cbb22010-09-22 14:16:262395 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252396 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2397 {if (__state_) __state_->__add_shared();}
Howard Hinnant73d21a42010-09-04 23:28:192398#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:262399 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552400 shared_future(future<_Rp>&& __f) _NOEXCEPT : __state_(__f.__state_)
Howard Hinnant99be8232010-09-03 18:39:252401 {__f.__state_ = nullptr;}
Howard Hinnant8c6cbb22010-09-22 14:16:262402 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552403 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
Howard Hinnant99be8232010-09-03 18:39:252404 {__rhs.__state_ = nullptr;}
Howard Hinnant73d21a42010-09-04 23:28:192405#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:252406 ~shared_future();
2407 shared_future& operator=(const shared_future& __rhs);
Howard Hinnant73d21a42010-09-04 23:28:192408#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:262409 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552410 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:252411 {
2412 shared_future(std::move(__rhs)).swap(*this);
2413 return *this;
2414 }
Howard Hinnant73d21a42010-09-04 23:28:192415#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:252416
2417 // retrieving the value
Howard Hinnant8c6cbb22010-09-22 14:16:262418 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502419 const _Rp& get() const {return __state_->copy();}
Howard Hinnant99be8232010-09-03 18:39:252420
Howard Hinnant8c6cbb22010-09-22 14:16:262421 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552422 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant99be8232010-09-03 18:39:252423
2424 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:262425 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552426 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant99be8232010-09-03 18:39:252427
Howard Hinnant8c6cbb22010-09-22 14:16:262428 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252429 void wait() const {__state_->wait();}
2430 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:262431 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252432 future_status
2433 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2434 {return __state_->wait_for(__rel_time);}
2435 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:262436 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252437 future_status
2438 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2439 {return __state_->wait_until(__abs_time);}
2440};
2441
Howard Hinnant99968442011-11-29 18:15:502442template <class _Rp>
2443shared_future<_Rp>::~shared_future()
Howard Hinnant99be8232010-09-03 18:39:252444{
2445 if (__state_)
2446 __state_->__release_shared();
2447}
2448
Howard Hinnant99968442011-11-29 18:15:502449template <class _Rp>
2450shared_future<_Rp>&
2451shared_future<_Rp>::operator=(const shared_future& __rhs)
Howard Hinnant99be8232010-09-03 18:39:252452{
2453 if (__rhs.__state_)
2454 __rhs.__state_->__add_shared();
2455 if (__state_)
2456 __state_->__release_shared();
2457 __state_ = __rhs.__state_;
2458 return *this;
2459}
2460
Howard Hinnant99968442011-11-29 18:15:502461template <class _Rp>
Howard Hinnant0f678bd2013-08-12 18:38:342462class _LIBCPP_TYPE_VIS_ONLY shared_future<_Rp&>
Howard Hinnant99be8232010-09-03 18:39:252463{
Howard Hinnant99968442011-11-29 18:15:502464 __assoc_state<_Rp&>* __state_;
Howard Hinnant99be8232010-09-03 18:39:252465
2466public:
Howard Hinnant8c6cbb22010-09-22 14:16:262467 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552468 shared_future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant8c6cbb22010-09-22 14:16:262469 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252470 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2471 {if (__state_) __state_->__add_shared();}
Howard Hinnant73d21a42010-09-04 23:28:192472#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:262473 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552474 shared_future(future<_Rp&>&& __f) _NOEXCEPT : __state_(__f.__state_)
Howard Hinnant99be8232010-09-03 18:39:252475 {__f.__state_ = nullptr;}
Howard Hinnant8c6cbb22010-09-22 14:16:262476 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552477 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
Howard Hinnant99be8232010-09-03 18:39:252478 {__rhs.__state_ = nullptr;}
Howard Hinnant73d21a42010-09-04 23:28:192479#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:252480 ~shared_future();
2481 shared_future& operator=(const shared_future& __rhs);
Howard Hinnant73d21a42010-09-04 23:28:192482#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:262483 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552484 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:252485 {
2486 shared_future(std::move(__rhs)).swap(*this);
2487 return *this;
2488 }
Howard Hinnant73d21a42010-09-04 23:28:192489#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:252490
2491 // retrieving the value
Howard Hinnant8c6cbb22010-09-22 14:16:262492 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502493 _Rp& get() const {return __state_->copy();}
Howard Hinnant99be8232010-09-03 18:39:252494
Howard Hinnant8c6cbb22010-09-22 14:16:262495 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552496 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant99be8232010-09-03 18:39:252497
2498 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:262499 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552500 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant99be8232010-09-03 18:39:252501
Howard Hinnant8c6cbb22010-09-22 14:16:262502 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252503 void wait() const {__state_->wait();}
2504 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:262505 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252506 future_status
2507 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2508 {return __state_->wait_for(__rel_time);}
2509 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:262510 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252511 future_status
2512 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2513 {return __state_->wait_until(__abs_time);}
2514};
2515
Howard Hinnant99968442011-11-29 18:15:502516template <class _Rp>
2517shared_future<_Rp&>::~shared_future()
Howard Hinnant99be8232010-09-03 18:39:252518{
2519 if (__state_)
2520 __state_->__release_shared();
2521}
2522
Howard Hinnant99968442011-11-29 18:15:502523template <class _Rp>
2524shared_future<_Rp&>&
2525shared_future<_Rp&>::operator=(const shared_future& __rhs)
Howard Hinnant99be8232010-09-03 18:39:252526{
2527 if (__rhs.__state_)
2528 __rhs.__state_->__add_shared();
2529 if (__state_)
2530 __state_->__release_shared();
2531 __state_ = __rhs.__state_;
2532 return *this;
2533}
2534
2535template <>
Howard Hinnant83eade62013-03-06 23:30:192536class _LIBCPP_TYPE_VIS shared_future<void>
Howard Hinnant99be8232010-09-03 18:39:252537{
2538 __assoc_sub_state* __state_;
2539
2540public:
Howard Hinnant8c6cbb22010-09-22 14:16:262541 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552542 shared_future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant8c6cbb22010-09-22 14:16:262543 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252544 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2545 {if (__state_) __state_->__add_shared();}
Howard Hinnant73d21a42010-09-04 23:28:192546#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:262547 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552548 shared_future(future<void>&& __f) _NOEXCEPT : __state_(__f.__state_)
Howard Hinnant99be8232010-09-03 18:39:252549 {__f.__state_ = nullptr;}
Howard Hinnant8c6cbb22010-09-22 14:16:262550 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552551 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
Howard Hinnant99be8232010-09-03 18:39:252552 {__rhs.__state_ = nullptr;}
Howard Hinnant73d21a42010-09-04 23:28:192553#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:252554 ~shared_future();
2555 shared_future& operator=(const shared_future& __rhs);
Howard Hinnant73d21a42010-09-04 23:28:192556#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:262557 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552558 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:252559 {
2560 shared_future(std::move(__rhs)).swap(*this);
2561 return *this;
2562 }
Howard Hinnant73d21a42010-09-04 23:28:192563#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:252564
2565 // retrieving the value
Howard Hinnant8c6cbb22010-09-22 14:16:262566 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252567 void get() const {__state_->copy();}
2568
Howard Hinnant8c6cbb22010-09-22 14:16:262569 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552570 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant99be8232010-09-03 18:39:252571
2572 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:262573 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552574 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant99be8232010-09-03 18:39:252575
Howard Hinnant8c6cbb22010-09-22 14:16:262576 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252577 void wait() const {__state_->wait();}
2578 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:262579 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252580 future_status
2581 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2582 {return __state_->wait_for(__rel_time);}
2583 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:262584 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252585 future_status
2586 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2587 {return __state_->wait_until(__abs_time);}
2588};
2589
Howard Hinnant99968442011-11-29 18:15:502590template <class _Rp>
Howard Hinnant99be8232010-09-03 18:39:252591inline _LIBCPP_INLINE_VISIBILITY
2592void
Howard Hinnant8bf01dd2012-07-21 17:46:552593swap(shared_future<_Rp>& __x, shared_future<_Rp>& __y) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:252594{
2595 __x.swap(__y);
2596}
2597
Howard Hinnant99968442011-11-29 18:15:502598template <class _Rp>
Howard Hinnant7de47902010-11-30 20:23:322599inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502600shared_future<_Rp>
2601future<_Rp>::share()
Howard Hinnante6e4d012010-09-03 21:46:372602{
Howard Hinnant99968442011-11-29 18:15:502603 return shared_future<_Rp>(_VSTD::move(*this));
Howard Hinnante6e4d012010-09-03 21:46:372604}
2605
Howard Hinnant99968442011-11-29 18:15:502606template <class _Rp>
Howard Hinnante6e4d012010-09-03 21:46:372607inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502608shared_future<_Rp&>
2609future<_Rp&>::share()
Howard Hinnante6e4d012010-09-03 21:46:372610{
Howard Hinnant99968442011-11-29 18:15:502611 return shared_future<_Rp&>(_VSTD::move(*this));
Howard Hinnant7de47902010-11-30 20:23:322612}
2613
Howard Hinnanta4451512010-12-02 16:45:212614#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2615
Howard Hinnant7de47902010-11-30 20:23:322616inline _LIBCPP_INLINE_VISIBILITY
2617shared_future<void>
2618future<void>::share()
2619{
Howard Hinnant0949eed2011-06-30 21:18:192620 return shared_future<void>(_VSTD::move(*this));
Howard Hinnante6e4d012010-09-03 21:46:372621}
2622
Howard Hinnanta4451512010-12-02 16:45:212623#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2624
Howard Hinnantbc8d3f92010-05-11 19:42:162625_LIBCPP_END_NAMESPACE_STD
2626
Jonathan Roelofs8d86b2e2014-09-05 19:45:052627#endif // !_LIBCPP_HAS_NO_THREADS
2628
Howard Hinnantbc8d3f92010-05-11 19:42:162629#endif // _LIBCPP_FUTURE