blob: ce15eafbf7e4ce4bad26eefd3f96679dff5730ae [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:161// -*- C++ -*-
2//===--------------------------- future -----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:014// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:165//
Howard Hinnantb64f8b02010-11-16 22:09:026// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:168//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_FUTURE
12#define _LIBCPP_FUTURE
13
14/*
15 future synopsis
16
17namespace std
18{
19
20enum class future_errc
21{
Howard Hinnantcd942f12013-09-14 18:20:1022 future_already_retrieved = 1,
Howard Hinnantbc8d3f92010-05-11 19:42:1623 promise_already_satisfied,
Howard Hinnantcd942f12013-09-14 18:20:1024 no_state,
25 broken_promise
Howard Hinnantbc8d3f92010-05-11 19:42:1626};
27
28enum class launch
29{
Howard Hinnant66895642010-11-23 18:33:5430 async = 1,
31 deferred = 2,
32 any = async | deferred
Howard Hinnantbc8d3f92010-05-11 19:42:1633};
34
35enum class future_status
36{
37 ready,
38 timeout,
39 deferred
40};
41
42template <> struct is_error_code_enum<future_errc> : public true_type { };
Howard Hinnant8bf01dd2012-07-21 17:46:5543error_code make_error_code(future_errc e) noexcept;
44error_condition make_error_condition(future_errc e) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1645
Howard Hinnant8bf01dd2012-07-21 17:46:5546const error_category& future_category() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1647
48class future_error
49 : public logic_error
50{
51public:
52 future_error(error_code ec); // exposition only
53
Howard Hinnant8bf01dd2012-07-21 17:46:5554 const error_code& code() const noexcept;
55 const char* what() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1656};
57
58template <class R>
59class promise
60{
61public:
62 promise();
63 template <class Allocator>
64 promise(allocator_arg_t, const Allocator& a);
Howard Hinnant8bf01dd2012-07-21 17:46:5565 promise(promise&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1666 promise(const promise& rhs) = delete;
67 ~promise();
68
69 // assignment
Howard Hinnant8bf01dd2012-07-21 17:46:5570 promise& operator=(promise&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1671 promise& operator=(const promise& rhs) = delete;
Howard Hinnant8bf01dd2012-07-21 17:46:5572 void swap(promise& other) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1673
74 // retrieving the result
75 future<R> get_future();
76
77 // setting the result
78 void set_value(const R& r);
79 void set_value(R&& r);
80 void set_exception(exception_ptr p);
81
82 // setting the result with deferred notification
83 void set_value_at_thread_exit(const R& r);
84 void set_value_at_thread_exit(R&& r);
85 void set_exception_at_thread_exit(exception_ptr p);
86};
87
88template <class R>
89class promise<R&>
90{
91public:
92 promise();
93 template <class Allocator>
94 promise(allocator_arg_t, const Allocator& a);
Howard Hinnant8bf01dd2012-07-21 17:46:5595 promise(promise&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1696 promise(const promise& rhs) = delete;
97 ~promise();
98
99 // assignment
Howard Hinnant8bf01dd2012-07-21 17:46:55100 promise& operator=(promise&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16101 promise& operator=(const promise& rhs) = delete;
Howard Hinnant8bf01dd2012-07-21 17:46:55102 void swap(promise& other) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16103
104 // retrieving the result
Howard Hinnant47499b12010-08-27 20:10:19105 future<R&> get_future();
Howard Hinnantbc8d3f92010-05-11 19:42:16106
107 // setting the result
108 void set_value(R& r);
109 void set_exception(exception_ptr p);
110
111 // setting the result with deferred notification
112 void set_value_at_thread_exit(R&);
113 void set_exception_at_thread_exit(exception_ptr p);
114};
115
116template <>
117class promise<void>
118{
119public:
120 promise();
121 template <class Allocator>
122 promise(allocator_arg_t, const Allocator& a);
Howard Hinnant8bf01dd2012-07-21 17:46:55123 promise(promise&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16124 promise(const promise& rhs) = delete;
125 ~promise();
126
127 // assignment
Howard Hinnant8bf01dd2012-07-21 17:46:55128 promise& operator=(promise&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16129 promise& operator=(const promise& rhs) = delete;
Howard Hinnant8bf01dd2012-07-21 17:46:55130 void swap(promise& other) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16131
132 // retrieving the result
Howard Hinnant47499b12010-08-27 20:10:19133 future<void> get_future();
Howard Hinnantbc8d3f92010-05-11 19:42:16134
135 // setting the result
136 void set_value();
137 void set_exception(exception_ptr p);
138
139 // setting the result with deferred notification
140 void set_value_at_thread_exit();
141 void set_exception_at_thread_exit(exception_ptr p);
142};
143
Howard Hinnant8bf01dd2012-07-21 17:46:55144template <class R> void swap(promise<R>& x, promise<R>& y) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16145
146template <class R, class Alloc>
147 struct uses_allocator<promise<R>, Alloc> : public true_type {};
148
149template <class R>
150class future
151{
152public:
Howard Hinnant8bf01dd2012-07-21 17:46:55153 future() noexcept;
154 future(future&&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16155 future(const future& rhs) = delete;
156 ~future();
157 future& operator=(const future& rhs) = delete;
Howard Hinnant8bf01dd2012-07-21 17:46:55158 future& operator=(future&&) noexcept;
159 shared_future<R> share();
Howard Hinnantbc8d3f92010-05-11 19:42:16160
161 // retrieving the value
162 R get();
163
164 // functions to check state
Howard Hinnant8bf01dd2012-07-21 17:46:55165 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16166
167 void wait() const;
168 template <class Rep, class Period>
169 future_status
170 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
171 template <class Clock, class Duration>
172 future_status
173 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
174};
175
176template <class R>
177class future<R&>
178{
179public:
Howard Hinnant8bf01dd2012-07-21 17:46:55180 future() noexcept;
181 future(future&&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16182 future(const future& rhs) = delete;
183 ~future();
184 future& operator=(const future& rhs) = delete;
Howard Hinnant8bf01dd2012-07-21 17:46:55185 future& operator=(future&&) noexcept;
186 shared_future<R&> share();
Howard Hinnantbc8d3f92010-05-11 19:42:16187
188 // retrieving the value
189 R& get();
190
191 // functions to check state
Howard Hinnant8bf01dd2012-07-21 17:46:55192 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16193
194 void wait() const;
195 template <class Rep, class Period>
196 future_status
197 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
198 template <class Clock, class Duration>
199 future_status
200 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
201};
202
203template <>
204class future<void>
205{
206public:
Howard Hinnant8bf01dd2012-07-21 17:46:55207 future() noexcept;
208 future(future&&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16209 future(const future& rhs) = delete;
210 ~future();
211 future& operator=(const future& rhs) = delete;
Howard Hinnant8bf01dd2012-07-21 17:46:55212 future& operator=(future&&) noexcept;
213 shared_future<void> share();
Howard Hinnantbc8d3f92010-05-11 19:42:16214
215 // retrieving the value
216 void get();
217
218 // functions to check state
Howard Hinnant8bf01dd2012-07-21 17:46:55219 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16220
221 void wait() const;
222 template <class Rep, class Period>
223 future_status
224 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
225 template <class Clock, class Duration>
226 future_status
227 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
228};
229
230template <class R>
231class shared_future
232{
233public:
Howard Hinnant8bf01dd2012-07-21 17:46:55234 shared_future() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16235 shared_future(const shared_future& rhs);
Howard Hinnant8bf01dd2012-07-21 17:46:55236 shared_future(future<R>&&) noexcept;
237 shared_future(shared_future&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16238 ~shared_future();
239 shared_future& operator=(const shared_future& rhs);
Howard Hinnant8bf01dd2012-07-21 17:46:55240 shared_future& operator=(shared_future&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16241
242 // retrieving the value
243 const R& get() const;
244
245 // functions to check state
Howard Hinnant8bf01dd2012-07-21 17:46:55246 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16247
248 void wait() const;
249 template <class Rep, class Period>
250 future_status
251 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
252 template <class Clock, class Duration>
253 future_status
254 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
255};
256
257template <class R>
258class shared_future<R&>
259{
260public:
Howard Hinnant8bf01dd2012-07-21 17:46:55261 shared_future() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16262 shared_future(const shared_future& rhs);
Howard Hinnant8bf01dd2012-07-21 17:46:55263 shared_future(future<R&>&&) noexcept;
264 shared_future(shared_future&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16265 ~shared_future();
266 shared_future& operator=(const shared_future& rhs);
Howard Hinnant8bf01dd2012-07-21 17:46:55267 shared_future& operator=(shared_future&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16268
269 // retrieving the value
270 R& get() const;
271
272 // functions to check state
Howard Hinnant8bf01dd2012-07-21 17:46:55273 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16274
275 void wait() const;
276 template <class Rep, class Period>
277 future_status
278 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
279 template <class Clock, class Duration>
280 future_status
281 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
282};
283
284template <>
285class shared_future<void>
286{
287public:
Howard Hinnant8bf01dd2012-07-21 17:46:55288 shared_future() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16289 shared_future(const shared_future& rhs);
Howard Hinnant8bf01dd2012-07-21 17:46:55290 shared_future(future<void>&&) noexcept;
291 shared_future(shared_future&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16292 ~shared_future();
293 shared_future& operator=(const shared_future& rhs);
Howard Hinnant8bf01dd2012-07-21 17:46:55294 shared_future& operator=(shared_future&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16295
296 // retrieving the value
297 void get() const;
298
299 // functions to check state
Howard Hinnant8bf01dd2012-07-21 17:46:55300 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16301
302 void wait() const;
303 template <class Rep, class Period>
304 future_status
305 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
306 template <class Clock, class Duration>
307 future_status
308 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
309};
310
Howard Hinnantbc8d3f92010-05-11 19:42:16311template <class F, class... Args>
Howard Hinnant0836f872013-09-21 18:17:23312 future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16313 async(F&& f, Args&&... args);
314
315template <class F, class... Args>
Howard Hinnant0836f872013-09-21 18:17:23316 future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16317 async(launch policy, F&& f, Args&&... args);
318
Howard Hinnantf5256e12010-05-11 21:36:01319template <class> class packaged_task; // undefined
Howard Hinnantbc8d3f92010-05-11 19:42:16320
321template <class R, class... ArgTypes>
322class packaged_task<R(ArgTypes...)>
323{
324public:
325 typedef R result_type;
326
327 // construction and destruction
Howard Hinnant8bf01dd2012-07-21 17:46:55328 packaged_task() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16329 template <class F>
Howard Hinnantbc8d3f92010-05-11 19:42:16330 explicit packaged_task(F&& f);
331 template <class F, class Allocator>
Marshall Clow07546f32015-06-30 14:16:49332 packaged_task(allocator_arg_t, const Allocator& a, F&& f);
Howard Hinnantbc8d3f92010-05-11 19:42:16333 ~packaged_task();
334
335 // no copy
Howard Hinnant8131a012012-07-21 19:34:12336 packaged_task(const packaged_task&) = delete;
337 packaged_task& operator=(const packaged_task&) = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16338
339 // move support
Howard Hinnant8bf01dd2012-07-21 17:46:55340 packaged_task(packaged_task&& other) noexcept;
341 packaged_task& operator=(packaged_task&& other) noexcept;
342 void swap(packaged_task& other) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16343
Howard Hinnant8bf01dd2012-07-21 17:46:55344 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16345
346 // result retrieval
347 future<R> get_future();
348
349 // execution
350 void operator()(ArgTypes... );
351 void make_ready_at_thread_exit(ArgTypes...);
352
353 void reset();
354};
355
356template <class R>
Howard Hinnant8bf01dd2012-07-21 17:46:55357 void swap(packaged_task<R(ArgTypes...)&, packaged_task<R(ArgTypes...)>&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16358
359template <class R, class Alloc> struct uses_allocator<packaged_task<R>, Alloc>;
360
361} // std
362
363*/
364
365#include <__config>
366#include <system_error>
Howard Hinnant47499b12010-08-27 20:10:19367#include <memory>
368#include <chrono>
369#include <exception>
Howard Hinnante6e4d012010-09-03 21:46:37370#include <mutex>
Howard Hinnant47499b12010-08-27 20:10:19371#include <thread>
Howard Hinnantbc8d3f92010-05-11 19:42:16372
Howard Hinnant08e17472011-10-17 20:05:10373#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16374#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10375#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16376
Jonathan Roelofsbaed05d2014-09-05 20:28:44377#ifdef _LIBCPP_HAS_NO_THREADS
Jonathan Roelofs8d86b2e2014-09-05 19:45:05378#error <future> is not supported on this single threaded system
379#else // !_LIBCPP_HAS_NO_THREADS
380
Howard Hinnantbc8d3f92010-05-11 19:42:16381_LIBCPP_BEGIN_NAMESPACE_STD
382
383//enum class future_errc
Howard Hinnantf6d875f2011-12-02 19:36:40384_LIBCPP_DECLARE_STRONG_ENUM(future_errc)
Howard Hinnantbc8d3f92010-05-11 19:42:16385{
Howard Hinnantcd942f12013-09-14 18:20:10386 future_already_retrieved = 1,
Howard Hinnantbc8d3f92010-05-11 19:42:16387 promise_already_satisfied,
Howard Hinnantcd942f12013-09-14 18:20:10388 no_state,
389 broken_promise
Howard Hinnantbc8d3f92010-05-11 19:42:16390};
Howard Hinnantf6d875f2011-12-02 19:36:40391_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_errc)
Howard Hinnantbc8d3f92010-05-11 19:42:16392
Howard Hinnant8c6cbb22010-09-22 14:16:26393template <>
Howard Hinnant0f678bd2013-08-12 18:38:34394struct _LIBCPP_TYPE_VIS_ONLY is_error_code_enum<future_errc> : public true_type {};
Howard Hinnanta6521722010-08-25 17:32:05395
Howard Hinnantf6d875f2011-12-02 19:36:40396#ifdef _LIBCPP_HAS_NO_STRONG_ENUMS
397template <>
Howard Hinnant0f678bd2013-08-12 18:38:34398struct _LIBCPP_TYPE_VIS_ONLY is_error_code_enum<future_errc::__lx> : public true_type { };
Howard Hinnantf6d875f2011-12-02 19:36:40399#endif
400
Howard Hinnantbc8d3f92010-05-11 19:42:16401//enum class launch
Howard Hinnantf6d875f2011-12-02 19:36:40402_LIBCPP_DECLARE_STRONG_ENUM(launch)
Howard Hinnantbc8d3f92010-05-11 19:42:16403{
Howard Hinnant66895642010-11-23 18:33:54404 async = 1,
405 deferred = 2,
406 any = async | deferred
Howard Hinnantbc8d3f92010-05-11 19:42:16407};
Howard Hinnantf6d875f2011-12-02 19:36:40408_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(launch)
Howard Hinnantbc8d3f92010-05-11 19:42:16409
Howard Hinnantf491e512013-06-29 18:38:17410#ifndef _LIBCPP_HAS_NO_STRONG_ENUMS
411
412#ifdef _LIBCXX_UNDERLYING_TYPE
413typedef underlying_type<launch>::type __launch_underlying_type;
414#else
415typedef int __launch_underlying_type;
416#endif
417
418inline _LIBCPP_INLINE_VISIBILITY
419_LIBCPP_CONSTEXPR
420launch
421operator&(launch __x, launch __y)
422{
423 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) &
424 static_cast<__launch_underlying_type>(__y));
425}
426
427inline _LIBCPP_INLINE_VISIBILITY
428_LIBCPP_CONSTEXPR
429launch
430operator|(launch __x, launch __y)
431{
432 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) |
433 static_cast<__launch_underlying_type>(__y));
434}
435
436inline _LIBCPP_INLINE_VISIBILITY
437_LIBCPP_CONSTEXPR
438launch
439operator^(launch __x, launch __y)
440{
441 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) ^
442 static_cast<__launch_underlying_type>(__y));
443}
444
445inline _LIBCPP_INLINE_VISIBILITY
446_LIBCPP_CONSTEXPR
447launch
448operator~(launch __x)
449{
Howard Hinnant6a683bf2013-07-02 18:01:41450 return static_cast<launch>(~static_cast<__launch_underlying_type>(__x) & 3);
Howard Hinnantf491e512013-06-29 18:38:17451}
452
453inline _LIBCPP_INLINE_VISIBILITY
454launch&
455operator&=(launch& __x, launch __y)
456{
457 __x = __x & __y; return __x;
458}
459
460inline _LIBCPP_INLINE_VISIBILITY
461launch&
462operator|=(launch& __x, launch __y)
463{
464 __x = __x | __y; return __x;
465}
466
467inline _LIBCPP_INLINE_VISIBILITY
468launch&
469operator^=(launch& __x, launch __y)
470{
471 __x = __x ^ __y; return __x;
472}
473
474#endif // !_LIBCPP_HAS_NO_STRONG_ENUMS
475
Howard Hinnantbc8d3f92010-05-11 19:42:16476//enum class future_status
Howard Hinnantf6d875f2011-12-02 19:36:40477_LIBCPP_DECLARE_STRONG_ENUM(future_status)
Howard Hinnantbc8d3f92010-05-11 19:42:16478{
Howard Hinnantbc8d3f92010-05-11 19:42:16479 ready,
480 timeout,
481 deferred
482};
Howard Hinnantf6d875f2011-12-02 19:36:40483_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_status)
Howard Hinnantbc8d3f92010-05-11 19:42:16484
Howard Hinnant83eade62013-03-06 23:30:19485_LIBCPP_FUNC_VIS
Howard Hinnant8bf01dd2012-07-21 17:46:55486const error_category& future_category() _NOEXCEPT;
Howard Hinnanta6521722010-08-25 17:32:05487
488inline _LIBCPP_INLINE_VISIBILITY
489error_code
Howard Hinnant8bf01dd2012-07-21 17:46:55490make_error_code(future_errc __e) _NOEXCEPT
Howard Hinnanta6521722010-08-25 17:32:05491{
492 return error_code(static_cast<int>(__e), future_category());
493}
494
495inline _LIBCPP_INLINE_VISIBILITY
496error_condition
Howard Hinnant8bf01dd2012-07-21 17:46:55497make_error_condition(future_errc __e) _NOEXCEPT
Howard Hinnanta6521722010-08-25 17:32:05498{
499 return error_condition(static_cast<int>(__e), future_category());
500}
501
Howard Hinnant8c6cbb22010-09-22 14:16:26502class _LIBCPP_EXCEPTION_ABI future_error
Howard Hinnanta6521722010-08-25 17:32:05503 : public logic_error
504{
505 error_code __ec_;
506public:
507 future_error(error_code __ec);
508
Howard Hinnant8c6cbb22010-09-22 14:16:26509 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55510 const error_code& code() const _NOEXCEPT {return __ec_;}
Howard Hinnantac6de542011-07-07 21:03:52511
512 virtual ~future_error() _NOEXCEPT;
Howard Hinnanta6521722010-08-25 17:32:05513};
514
Eric Fiselier423ca202015-10-02 21:25:15515inline _LIBCPP_ALWAYS_INLINE
516void __throw_future_error(future_errc _Ev)
Marshall Clowa1899742015-09-03 15:11:32517{
518#ifndef _LIBCPP_NO_EXCEPTIONS
519 throw future_error(make_error_code(_Ev));
520#else
521 assert(!"future_error");
522#endif
523}
524
Howard Hinnant0f678bd2013-08-12 18:38:34525class _LIBCPP_TYPE_VIS __assoc_sub_state
Howard Hinnant47499b12010-08-27 20:10:19526 : public __shared_count
527{
528protected:
529 exception_ptr __exception_;
530 mutable mutex __mut_;
531 mutable condition_variable __cv_;
532 unsigned __state_;
533
Howard Hinnant1694d232011-05-28 14:41:13534 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant54da3382010-08-30 18:46:21535 void __sub_wait(unique_lock<mutex>& __lk);
Howard Hinnant47499b12010-08-27 20:10:19536public:
537 enum
538 {
539 __constructed = 1,
540 __future_attached = 2,
541 ready = 4,
542 deferred = 8
543 };
544
Howard Hinnant8c6cbb22010-09-22 14:16:26545 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19546 __assoc_sub_state() : __state_(0) {}
547
Howard Hinnant8c6cbb22010-09-22 14:16:26548 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19549 bool __has_value() const
550 {return (__state_ & __constructed) || (__exception_ != nullptr);}
551
Howard Hinnant8c6cbb22010-09-22 14:16:26552 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1b031c92013-01-14 20:01:24553 void __set_future_attached()
554 {
555 lock_guard<mutex> __lk(__mut_);
556 __state_ |= __future_attached;
557 }
Howard Hinnant8c6cbb22010-09-22 14:16:26558 _LIBCPP_INLINE_VISIBILITY
Marshall Clow9de3d4c2013-10-13 01:02:45559 bool __has_future_attached() const {return (__state_ & __future_attached) != 0;}
Howard Hinnant47499b12010-08-27 20:10:19560
Howard Hinnant8c6cbb22010-09-22 14:16:26561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21562 void __set_deferred() {__state_ |= deferred;}
563
Howard Hinnant47499b12010-08-27 20:10:19564 void __make_ready();
Howard Hinnant8c6cbb22010-09-22 14:16:26565 _LIBCPP_INLINE_VISIBILITY
Marshall Clow9de3d4c2013-10-13 01:02:45566 bool __is_ready() const {return (__state_ & ready) != 0;}
Howard Hinnant47499b12010-08-27 20:10:19567
568 void set_value();
569 void set_value_at_thread_exit();
570
571 void set_exception(exception_ptr __p);
572 void set_exception_at_thread_exit(exception_ptr __p);
573
574 void copy();
575
Howard Hinnant54da3382010-08-30 18:46:21576 void wait();
Howard Hinnant47499b12010-08-27 20:10:19577 template <class _Rep, class _Period>
578 future_status
Evgeniy Stepanova3b25f82015-11-07 01:22:13579 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19580 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const;
581 template <class _Clock, class _Duration>
582 future_status
583 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const;
Howard Hinnant54da3382010-08-30 18:46:21584
585 virtual void __execute();
Howard Hinnant47499b12010-08-27 20:10:19586};
587
Howard Hinnantf39daa82010-08-28 21:01:06588template <class _Clock, class _Duration>
589future_status
590__assoc_sub_state::wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
591{
592 unique_lock<mutex> __lk(__mut_);
Howard Hinnant54da3382010-08-30 18:46:21593 if (__state_ & deferred)
594 return future_status::deferred;
595 while (!(__state_ & ready) && _Clock::now() < __abs_time)
Howard Hinnantf39daa82010-08-28 21:01:06596 __cv_.wait_until(__lk, __abs_time);
597 if (__state_ & ready)
598 return future_status::ready;
Howard Hinnantf39daa82010-08-28 21:01:06599 return future_status::timeout;
600}
601
602template <class _Rep, class _Period>
Evgeniy Stepanova3b25f82015-11-07 01:22:13603inline
Howard Hinnantf39daa82010-08-28 21:01:06604future_status
605__assoc_sub_state::wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
606{
Howard Hinnantf8f85212010-11-20 19:16:30607 return wait_until(chrono::steady_clock::now() + __rel_time);
Howard Hinnantf39daa82010-08-28 21:01:06608}
609
Howard Hinnant99968442011-11-29 18:15:50610template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19611class __assoc_state
612 : public __assoc_sub_state
613{
614 typedef __assoc_sub_state base;
Howard Hinnant99968442011-11-29 18:15:50615 typedef typename aligned_storage<sizeof(_Rp), alignment_of<_Rp>::value>::type _Up;
Howard Hinnant47499b12010-08-27 20:10:19616protected:
Howard Hinnant99968442011-11-29 18:15:50617 _Up __value_;
Howard Hinnant47499b12010-08-27 20:10:19618
Howard Hinnant1694d232011-05-28 14:41:13619 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant47499b12010-08-27 20:10:19620public:
621
622 template <class _Arg>
Howard Hinnant73d21a42010-09-04 23:28:19623#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19624 void set_value(_Arg&& __arg);
625#else
626 void set_value(_Arg& __arg);
627#endif
628
629 template <class _Arg>
Howard Hinnant73d21a42010-09-04 23:28:19630#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19631 void set_value_at_thread_exit(_Arg&& __arg);
632#else
633 void set_value_at_thread_exit(_Arg& __arg);
634#endif
635
Howard Hinnant99968442011-11-29 18:15:50636 _Rp move();
637 typename add_lvalue_reference<_Rp>::type copy();
Howard Hinnant47499b12010-08-27 20:10:19638};
639
Howard Hinnant99968442011-11-29 18:15:50640template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19641void
Howard Hinnant99968442011-11-29 18:15:50642__assoc_state<_Rp>::__on_zero_shared() _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19643{
644 if (this->__state_ & base::__constructed)
Howard Hinnant99968442011-11-29 18:15:50645 reinterpret_cast<_Rp*>(&__value_)->~_Rp();
Howard Hinnant47499b12010-08-27 20:10:19646 delete this;
647}
648
Howard Hinnant99968442011-11-29 18:15:50649template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19650template <class _Arg>
651void
Howard Hinnant73d21a42010-09-04 23:28:19652#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50653__assoc_state<_Rp>::set_value(_Arg&& __arg)
Howard Hinnant47499b12010-08-27 20:10:19654#else
Howard Hinnant99968442011-11-29 18:15:50655__assoc_state<_Rp>::set_value(_Arg& __arg)
Howard Hinnant47499b12010-08-27 20:10:19656#endif
657{
658 unique_lock<mutex> __lk(this->__mut_);
659 if (this->__has_value())
Eric Fiselier423ca202015-10-02 21:25:15660 __throw_future_error(future_errc::promise_already_satisfied);
Howard Hinnant99968442011-11-29 18:15:50661 ::new(&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
Howard Hinnant47499b12010-08-27 20:10:19662 this->__state_ |= base::__constructed | base::ready;
Howard Hinnant47499b12010-08-27 20:10:19663 __cv_.notify_all();
664}
665
Howard Hinnant99968442011-11-29 18:15:50666template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19667template <class _Arg>
668void
Howard Hinnant73d21a42010-09-04 23:28:19669#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50670__assoc_state<_Rp>::set_value_at_thread_exit(_Arg&& __arg)
Howard Hinnant47499b12010-08-27 20:10:19671#else
Howard Hinnant99968442011-11-29 18:15:50672__assoc_state<_Rp>::set_value_at_thread_exit(_Arg& __arg)
Howard Hinnant47499b12010-08-27 20:10:19673#endif
674{
675 unique_lock<mutex> __lk(this->__mut_);
676 if (this->__has_value())
Eric Fiselier423ca202015-10-02 21:25:15677 __throw_future_error(future_errc::promise_already_satisfied);
Howard Hinnant99968442011-11-29 18:15:50678 ::new(&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
Howard Hinnant47499b12010-08-27 20:10:19679 this->__state_ |= base::__constructed;
Howard Hinnant5306d682010-10-14 19:18:04680 __thread_local_data()->__make_ready_at_thread_exit(this);
Howard Hinnant47499b12010-08-27 20:10:19681}
682
Howard Hinnant99968442011-11-29 18:15:50683template <class _Rp>
684_Rp
685__assoc_state<_Rp>::move()
Howard Hinnant47499b12010-08-27 20:10:19686{
687 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant54da3382010-08-30 18:46:21688 this->__sub_wait(__lk);
Howard Hinnant47499b12010-08-27 20:10:19689 if (this->__exception_ != nullptr)
690 rethrow_exception(this->__exception_);
Howard Hinnant99968442011-11-29 18:15:50691 return _VSTD::move(*reinterpret_cast<_Rp*>(&__value_));
Howard Hinnant47499b12010-08-27 20:10:19692}
693
Howard Hinnant99968442011-11-29 18:15:50694template <class _Rp>
695typename add_lvalue_reference<_Rp>::type
696__assoc_state<_Rp>::copy()
Howard Hinnant47499b12010-08-27 20:10:19697{
698 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant54da3382010-08-30 18:46:21699 this->__sub_wait(__lk);
Howard Hinnant47499b12010-08-27 20:10:19700 if (this->__exception_ != nullptr)
701 rethrow_exception(this->__exception_);
Howard Hinnant99968442011-11-29 18:15:50702 return *reinterpret_cast<_Rp*>(&__value_);
Howard Hinnant47499b12010-08-27 20:10:19703}
704
Howard Hinnant99968442011-11-29 18:15:50705template <class _Rp>
706class __assoc_state<_Rp&>
Howard Hinnantf39daa82010-08-28 21:01:06707 : public __assoc_sub_state
708{
709 typedef __assoc_sub_state base;
Howard Hinnant99968442011-11-29 18:15:50710 typedef _Rp* _Up;
Howard Hinnantf39daa82010-08-28 21:01:06711protected:
Howard Hinnant99968442011-11-29 18:15:50712 _Up __value_;
Howard Hinnantf39daa82010-08-28 21:01:06713
Howard Hinnant1694d232011-05-28 14:41:13714 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnantf39daa82010-08-28 21:01:06715public:
716
Howard Hinnant99968442011-11-29 18:15:50717 void set_value(_Rp& __arg);
718 void set_value_at_thread_exit(_Rp& __arg);
Howard Hinnantf39daa82010-08-28 21:01:06719
Howard Hinnant99968442011-11-29 18:15:50720 _Rp& copy();
Howard Hinnantf39daa82010-08-28 21:01:06721};
722
Howard Hinnant99968442011-11-29 18:15:50723template <class _Rp>
Howard Hinnantf39daa82010-08-28 21:01:06724void
Howard Hinnant99968442011-11-29 18:15:50725__assoc_state<_Rp&>::__on_zero_shared() _NOEXCEPT
Howard Hinnantf39daa82010-08-28 21:01:06726{
727 delete this;
728}
729
Howard Hinnant99968442011-11-29 18:15:50730template <class _Rp>
Howard Hinnantf39daa82010-08-28 21:01:06731void
Howard Hinnant99968442011-11-29 18:15:50732__assoc_state<_Rp&>::set_value(_Rp& __arg)
Howard Hinnantf39daa82010-08-28 21:01:06733{
734 unique_lock<mutex> __lk(this->__mut_);
735 if (this->__has_value())
Eric Fiselier423ca202015-10-02 21:25:15736 __throw_future_error(future_errc::promise_already_satisfied);
Howard Hinnanta4e87ab2013-08-08 18:38:55737 __value_ = _VSTD::addressof(__arg);
Howard Hinnantf39daa82010-08-28 21:01:06738 this->__state_ |= base::__constructed | base::ready;
Howard Hinnantf39daa82010-08-28 21:01:06739 __cv_.notify_all();
740}
741
Howard Hinnant99968442011-11-29 18:15:50742template <class _Rp>
Howard Hinnantf39daa82010-08-28 21:01:06743void
Howard Hinnant99968442011-11-29 18:15:50744__assoc_state<_Rp&>::set_value_at_thread_exit(_Rp& __arg)
Howard Hinnantf39daa82010-08-28 21:01:06745{
746 unique_lock<mutex> __lk(this->__mut_);
747 if (this->__has_value())
Eric Fiselier423ca202015-10-02 21:25:15748 __throw_future_error(future_errc::promise_already_satisfied);
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}
753
Howard Hinnant99968442011-11-29 18:15:50754template <class _Rp>
755_Rp&
756__assoc_state<_Rp&>::copy()
Howard Hinnantf39daa82010-08-28 21:01:06757{
758 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant54da3382010-08-30 18:46:21759 this->__sub_wait(__lk);
Howard Hinnantf39daa82010-08-28 21:01:06760 if (this->__exception_ != nullptr)
761 rethrow_exception(this->__exception_);
762 return *__value_;
763}
764
Howard Hinnant99968442011-11-29 18:15:50765template <class _Rp, class _Alloc>
Howard Hinnant47499b12010-08-27 20:10:19766class __assoc_state_alloc
Howard Hinnant99968442011-11-29 18:15:50767 : public __assoc_state<_Rp>
Howard Hinnant47499b12010-08-27 20:10:19768{
Howard Hinnant99968442011-11-29 18:15:50769 typedef __assoc_state<_Rp> base;
Howard Hinnant47499b12010-08-27 20:10:19770 _Alloc __alloc_;
771
Howard Hinnant1694d232011-05-28 14:41:13772 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant47499b12010-08-27 20:10:19773public:
Howard Hinnant8c6cbb22010-09-22 14:16:26774 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19775 explicit __assoc_state_alloc(const _Alloc& __a)
776 : __alloc_(__a) {}
777};
778
Howard Hinnant99968442011-11-29 18:15:50779template <class _Rp, class _Alloc>
Howard Hinnant47499b12010-08-27 20:10:19780void
Howard Hinnant99968442011-11-29 18:15:50781__assoc_state_alloc<_Rp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19782{
783 if (this->__state_ & base::__constructed)
Howard Hinnanta4e87ab2013-08-08 18:38:55784 reinterpret_cast<_Rp*>(_VSTD::addressof(this->__value_))->~_Rp();
Eric Fiselier8492cd82015-02-05 23:01:40785 typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
786 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4d2413c2014-10-23 06:24:45787 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselier8492cd82015-02-05 23:01:40788 _Al __a(__alloc_);
Howard Hinnant47499b12010-08-27 20:10:19789 this->~__assoc_state_alloc();
Eric Fiselier4d2413c2014-10-23 06:24:45790 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnant47499b12010-08-27 20:10:19791}
792
Howard Hinnant99968442011-11-29 18:15:50793template <class _Rp, class _Alloc>
794class __assoc_state_alloc<_Rp&, _Alloc>
795 : public __assoc_state<_Rp&>
Howard Hinnantf39daa82010-08-28 21:01:06796{
Howard Hinnant99968442011-11-29 18:15:50797 typedef __assoc_state<_Rp&> base;
Howard Hinnantf39daa82010-08-28 21:01:06798 _Alloc __alloc_;
799
Howard Hinnant1694d232011-05-28 14:41:13800 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnantf39daa82010-08-28 21:01:06801public:
Howard Hinnant8c6cbb22010-09-22 14:16:26802 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39daa82010-08-28 21:01:06803 explicit __assoc_state_alloc(const _Alloc& __a)
804 : __alloc_(__a) {}
805};
806
Howard Hinnant99968442011-11-29 18:15:50807template <class _Rp, class _Alloc>
Howard Hinnantf39daa82010-08-28 21:01:06808void
Howard Hinnant99968442011-11-29 18:15:50809__assoc_state_alloc<_Rp&, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantf39daa82010-08-28 21:01:06810{
Eric Fiselier8492cd82015-02-05 23:01:40811 typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
812 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4d2413c2014-10-23 06:24:45813 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselier8492cd82015-02-05 23:01:40814 _Al __a(__alloc_);
Howard Hinnantf39daa82010-08-28 21:01:06815 this->~__assoc_state_alloc();
Eric Fiselier4d2413c2014-10-23 06:24:45816 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantf39daa82010-08-28 21:01:06817}
818
Howard Hinnant47499b12010-08-27 20:10:19819template <class _Alloc>
820class __assoc_sub_state_alloc
821 : public __assoc_sub_state
822{
823 typedef __assoc_sub_state base;
824 _Alloc __alloc_;
825
Howard Hinnant1694d232011-05-28 14:41:13826 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant47499b12010-08-27 20:10:19827public:
Howard Hinnant8c6cbb22010-09-22 14:16:26828 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19829 explicit __assoc_sub_state_alloc(const _Alloc& __a)
830 : __alloc_(__a) {}
831};
832
833template <class _Alloc>
834void
Howard Hinnant1694d232011-05-28 14:41:13835__assoc_sub_state_alloc<_Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19836{
Eric Fiselier8492cd82015-02-05 23:01:40837 typedef typename __allocator_traits_rebind<_Alloc, __assoc_sub_state_alloc>::type _Al;
838 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4d2413c2014-10-23 06:24:45839 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselier8492cd82015-02-05 23:01:40840 _Al __a(__alloc_);
Howard Hinnant47499b12010-08-27 20:10:19841 this->~__assoc_sub_state_alloc();
Eric Fiselier4d2413c2014-10-23 06:24:45842 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnant47499b12010-08-27 20:10:19843}
844
Howard Hinnant99968442011-11-29 18:15:50845template <class _Rp, class _Fp>
Howard Hinnant54da3382010-08-30 18:46:21846class __deferred_assoc_state
Howard Hinnant99968442011-11-29 18:15:50847 : public __assoc_state<_Rp>
Howard Hinnant54da3382010-08-30 18:46:21848{
Howard Hinnant99968442011-11-29 18:15:50849 typedef __assoc_state<_Rp> base;
Howard Hinnant54da3382010-08-30 18:46:21850
Howard Hinnant99968442011-11-29 18:15:50851 _Fp __func_;
Howard Hinnant54da3382010-08-30 18:46:21852
853public:
Howard Hinnant73d21a42010-09-04 23:28:19854#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanova3b25f82015-11-07 01:22:13855 _LIBCPP_INLINE_VISIBILITY
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>
Evgeniy Stepanova3b25f82015-11-07 01:22:13865inline
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
Evgeniy Stepanova3b25f82015-11-07 01:22:13902 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50903 explicit __deferred_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:21904#endif
905
906 virtual void __execute();
907};
908
Howard Hinnant73d21a42010-09-04 23:28:19909#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21910
Howard Hinnant99968442011-11-29 18:15:50911template <class _Fp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13912inline
Howard Hinnant99968442011-11-29 18:15:50913__deferred_assoc_state<void, _Fp>::__deferred_assoc_state(_Fp&& __f)
914 : __func_(_VSTD::forward<_Fp>(__f))
Howard Hinnant54da3382010-08-30 18:46:21915{
916 this->__set_deferred();
917}
918
Howard Hinnant73d21a42010-09-04 23:28:19919#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21920
Howard Hinnant99968442011-11-29 18:15:50921template <class _Fp>
Howard Hinnant54da3382010-08-30 18:46:21922void
Howard Hinnant99968442011-11-29 18:15:50923__deferred_assoc_state<void, _Fp>::__execute()
Howard Hinnant54da3382010-08-30 18:46:21924{
925#ifndef _LIBCPP_NO_EXCEPTIONS
926 try
927 {
928#endif // _LIBCPP_NO_EXCEPTIONS
929 __func_();
930 this->set_value();
931#ifndef _LIBCPP_NO_EXCEPTIONS
932 }
933 catch (...)
934 {
935 this->set_exception(current_exception());
936 }
937#endif // _LIBCPP_NO_EXCEPTIONS
938}
939
Howard Hinnant99968442011-11-29 18:15:50940template <class _Rp, class _Fp>
Howard Hinnant57cff292011-05-19 15:05:04941class __async_assoc_state
Howard Hinnant99968442011-11-29 18:15:50942 : public __assoc_state<_Rp>
Howard Hinnant57cff292011-05-19 15:05:04943{
Howard Hinnant99968442011-11-29 18:15:50944 typedef __assoc_state<_Rp> base;
Howard Hinnant57cff292011-05-19 15:05:04945
Howard Hinnant99968442011-11-29 18:15:50946 _Fp __func_;
Howard Hinnant57cff292011-05-19 15:05:04947
Howard Hinnant1694d232011-05-28 14:41:13948 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant57cff292011-05-19 15:05:04949public:
950#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanova3b25f82015-11-07 01:22:13951 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50952 explicit __async_assoc_state(_Fp&& __f);
Howard Hinnant57cff292011-05-19 15:05:04953#endif
954
955 virtual void __execute();
956};
957
958#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
959
Howard Hinnant99968442011-11-29 18:15:50960template <class _Rp, class _Fp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13961inline
Howard Hinnant99968442011-11-29 18:15:50962__async_assoc_state<_Rp, _Fp>::__async_assoc_state(_Fp&& __f)
963 : __func_(_VSTD::forward<_Fp>(__f))
Howard Hinnant57cff292011-05-19 15:05:04964{
965}
966
967#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
968
Howard Hinnant99968442011-11-29 18:15:50969template <class _Rp, class _Fp>
Howard Hinnant57cff292011-05-19 15:05:04970void
Howard Hinnant99968442011-11-29 18:15:50971__async_assoc_state<_Rp, _Fp>::__execute()
Howard Hinnant57cff292011-05-19 15:05:04972{
973#ifndef _LIBCPP_NO_EXCEPTIONS
974 try
975 {
976#endif // _LIBCPP_NO_EXCEPTIONS
977 this->set_value(__func_());
978#ifndef _LIBCPP_NO_EXCEPTIONS
979 }
980 catch (...)
981 {
982 this->set_exception(current_exception());
983 }
984#endif // _LIBCPP_NO_EXCEPTIONS
985}
986
Howard Hinnant99968442011-11-29 18:15:50987template <class _Rp, class _Fp>
Howard Hinnant57cff292011-05-19 15:05:04988void
Howard Hinnant99968442011-11-29 18:15:50989__async_assoc_state<_Rp, _Fp>::__on_zero_shared() _NOEXCEPT
Howard Hinnant57cff292011-05-19 15:05:04990{
991 this->wait();
992 base::__on_zero_shared();
993}
994
Howard Hinnant99968442011-11-29 18:15:50995template <class _Fp>
996class __async_assoc_state<void, _Fp>
Howard Hinnant57cff292011-05-19 15:05:04997 : public __assoc_sub_state
998{
999 typedef __assoc_sub_state base;
1000
Howard Hinnant99968442011-11-29 18:15:501001 _Fp __func_;
Howard Hinnant57cff292011-05-19 15:05:041002
Howard Hinnant1694d232011-05-28 14:41:131003 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant57cff292011-05-19 15:05:041004public:
1005#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanova3b25f82015-11-07 01:22:131006 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:501007 explicit __async_assoc_state(_Fp&& __f);
Howard Hinnant57cff292011-05-19 15:05:041008#endif
1009
1010 virtual void __execute();
1011};
1012
1013#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1014
Howard Hinnant99968442011-11-29 18:15:501015template <class _Fp>
Evgeniy Stepanova3b25f82015-11-07 01:22:131016inline
Howard Hinnant99968442011-11-29 18:15:501017__async_assoc_state<void, _Fp>::__async_assoc_state(_Fp&& __f)
1018 : __func_(_VSTD::forward<_Fp>(__f))
Howard Hinnant57cff292011-05-19 15:05:041019{
1020}
1021
1022#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1023
Howard Hinnant99968442011-11-29 18:15:501024template <class _Fp>
Howard Hinnant57cff292011-05-19 15:05:041025void
Howard Hinnant99968442011-11-29 18:15:501026__async_assoc_state<void, _Fp>::__execute()
Howard Hinnant57cff292011-05-19 15:05:041027{
1028#ifndef _LIBCPP_NO_EXCEPTIONS
1029 try
1030 {
1031#endif // _LIBCPP_NO_EXCEPTIONS
1032 __func_();
1033 this->set_value();
1034#ifndef _LIBCPP_NO_EXCEPTIONS
1035 }
1036 catch (...)
1037 {
1038 this->set_exception(current_exception());
1039 }
1040#endif // _LIBCPP_NO_EXCEPTIONS
1041}
1042
Howard Hinnant99968442011-11-29 18:15:501043template <class _Fp>
Howard Hinnant57cff292011-05-19 15:05:041044void
Howard Hinnant99968442011-11-29 18:15:501045__async_assoc_state<void, _Fp>::__on_zero_shared() _NOEXCEPT
Howard Hinnant57cff292011-05-19 15:05:041046{
1047 this->wait();
1048 base::__on_zero_shared();
1049}
1050
Howard Hinnant0f678bd2013-08-12 18:38:341051template <class _Rp> class _LIBCPP_TYPE_VIS_ONLY promise;
1052template <class _Rp> class _LIBCPP_TYPE_VIS_ONLY shared_future;
Howard Hinnant47499b12010-08-27 20:10:191053
1054// future
1055
Howard Hinnant0f678bd2013-08-12 18:38:341056template <class _Rp> class _LIBCPP_TYPE_VIS_ONLY future;
Howard Hinnant54da3382010-08-30 18:46:211057
Howard Hinnant99968442011-11-29 18:15:501058template <class _Rp, class _Fp>
1059future<_Rp>
Howard Hinnant73d21a42010-09-04 23:28:191060#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501061__make_deferred_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:211062#else
Howard Hinnant99968442011-11-29 18:15:501063__make_deferred_assoc_state(_Fp __f);
Howard Hinnant54da3382010-08-30 18:46:211064#endif
1065
Howard Hinnant99968442011-11-29 18:15:501066template <class _Rp, class _Fp>
1067future<_Rp>
Howard Hinnant57cff292011-05-19 15:05:041068#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501069__make_async_assoc_state(_Fp&& __f);
Howard Hinnant57cff292011-05-19 15:05:041070#else
Howard Hinnant99968442011-11-29 18:15:501071__make_async_assoc_state(_Fp __f);
Howard Hinnant57cff292011-05-19 15:05:041072#endif
1073
Howard Hinnant99968442011-11-29 18:15:501074template <class _Rp>
Howard Hinnant0f678bd2013-08-12 18:38:341075class _LIBCPP_TYPE_VIS_ONLY future
Howard Hinnant47499b12010-08-27 20:10:191076{
Howard Hinnant99968442011-11-29 18:15:501077 __assoc_state<_Rp>* __state_;
Howard Hinnant47499b12010-08-27 20:10:191078
Howard Hinnant99968442011-11-29 18:15:501079 explicit future(__assoc_state<_Rp>* __state);
Howard Hinnant47499b12010-08-27 20:10:191080
1081 template <class> friend class promise;
Howard Hinnant99be8232010-09-03 18:39:251082 template <class> friend class shared_future;
Howard Hinnant54da3382010-08-30 18:46:211083
Howard Hinnant73d21a42010-09-04 23:28:191084#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501085 template <class _R1, class _Fp>
1086 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1087 template <class _R1, class _Fp>
1088 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:211089#else
Howard Hinnant99968442011-11-29 18:15:501090 template <class _R1, class _Fp>
1091 friend future<_R1> __make_deferred_assoc_state(_Fp __f);
1092 template <class _R1, class _Fp>
1093 friend future<_R1> __make_async_assoc_state(_Fp __f);
Howard Hinnant54da3382010-08-30 18:46:211094#endif
1095
Howard Hinnant47499b12010-08-27 20:10:191096public:
Howard Hinnant8c6cbb22010-09-22 14:16:261097 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551098 future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant73d21a42010-09-04 23:28:191099#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261100 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551101 future(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191102 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1103 future(const future&) = delete;
1104 future& operator=(const future&) = delete;
Howard Hinnant8c6cbb22010-09-22 14:16:261105 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551106 future& operator=(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191107 {
1108 future(std::move(__rhs)).swap(*this);
1109 return *this;
1110 }
Howard Hinnant73d21a42010-09-04 23:28:191111#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191112private:
1113 future(const future&);
1114 future& operator=(const future&);
1115public:
Howard Hinnant73d21a42010-09-04 23:28:191116#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191117 ~future();
Evgeniy Stepanova3b25f82015-11-07 01:22:131118 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:501119 shared_future<_Rp> share();
Howard Hinnant47499b12010-08-27 20:10:191120
1121 // retrieving the value
Howard Hinnant99968442011-11-29 18:15:501122 _Rp get();
Howard Hinnant47499b12010-08-27 20:10:191123
Howard Hinnant8c6cbb22010-09-22 14:16:261124 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551125 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:191126
1127 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:261128 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551129 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant47499b12010-08-27 20:10:191130
Howard Hinnant8c6cbb22010-09-22 14:16:261131 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191132 void wait() const {__state_->wait();}
1133 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:261134 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191135 future_status
1136 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1137 {return __state_->wait_for(__rel_time);}
1138 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:261139 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191140 future_status
1141 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1142 {return __state_->wait_until(__abs_time);}
1143};
1144
Howard Hinnant99968442011-11-29 18:15:501145template <class _Rp>
1146future<_Rp>::future(__assoc_state<_Rp>* __state)
Howard Hinnant47499b12010-08-27 20:10:191147 : __state_(__state)
1148{
1149 if (__state_->__has_future_attached())
Eric Fiselier423ca202015-10-02 21:25:151150 __throw_future_error(future_errc::future_already_retrieved);
Howard Hinnant47499b12010-08-27 20:10:191151 __state_->__add_shared();
Howard Hinnant54da3382010-08-30 18:46:211152 __state_->__set_future_attached();
Howard Hinnant47499b12010-08-27 20:10:191153}
1154
Howard Hinnant54da3382010-08-30 18:46:211155struct __release_shared_count
1156{
1157 void operator()(__shared_count* p) {p->__release_shared();}
1158};
1159
Howard Hinnant99968442011-11-29 18:15:501160template <class _Rp>
1161future<_Rp>::~future()
Howard Hinnant47499b12010-08-27 20:10:191162{
1163 if (__state_)
1164 __state_->__release_shared();
1165}
1166
Howard Hinnant99968442011-11-29 18:15:501167template <class _Rp>
1168_Rp
1169future<_Rp>::get()
Howard Hinnant47499b12010-08-27 20:10:191170{
Howard Hinnant54da3382010-08-30 18:46:211171 unique_ptr<__shared_count, __release_shared_count> __(__state_);
Howard Hinnant99968442011-11-29 18:15:501172 __assoc_state<_Rp>* __s = __state_;
Howard Hinnant47499b12010-08-27 20:10:191173 __state_ = nullptr;
1174 return __s->move();
1175}
1176
Howard Hinnant99968442011-11-29 18:15:501177template <class _Rp>
Howard Hinnant0f678bd2013-08-12 18:38:341178class _LIBCPP_TYPE_VIS_ONLY future<_Rp&>
Howard Hinnant47499b12010-08-27 20:10:191179{
Howard Hinnant99968442011-11-29 18:15:501180 __assoc_state<_Rp&>* __state_;
Howard Hinnant47499b12010-08-27 20:10:191181
Howard Hinnant99968442011-11-29 18:15:501182 explicit future(__assoc_state<_Rp&>* __state);
Howard Hinnant47499b12010-08-27 20:10:191183
1184 template <class> friend class promise;
Howard Hinnant99be8232010-09-03 18:39:251185 template <class> friend class shared_future;
Howard Hinnant54da3382010-08-30 18:46:211186
Howard Hinnant73d21a42010-09-04 23:28:191187#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501188 template <class _R1, class _Fp>
1189 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1190 template <class _R1, class _Fp>
1191 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:211192#else
Howard Hinnant99968442011-11-29 18:15:501193 template <class _R1, class _Fp>
1194 friend future<_R1> __make_deferred_assoc_state(_Fp __f);
1195 template <class _R1, class _Fp>
1196 friend future<_R1> __make_async_assoc_state(_Fp __f);
Howard Hinnant54da3382010-08-30 18:46:211197#endif
1198
Howard Hinnant47499b12010-08-27 20:10:191199public:
Howard Hinnant8c6cbb22010-09-22 14:16:261200 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551201 future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant73d21a42010-09-04 23:28:191202#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261203 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551204 future(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191205 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1206 future(const future&) = delete;
1207 future& operator=(const future&) = delete;
Howard Hinnant8c6cbb22010-09-22 14:16:261208 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551209 future& operator=(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191210 {
1211 future(std::move(__rhs)).swap(*this);
1212 return *this;
1213 }
Howard Hinnant73d21a42010-09-04 23:28:191214#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191215private:
1216 future(const future&);
1217 future& operator=(const future&);
1218public:
Howard Hinnant73d21a42010-09-04 23:28:191219#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191220 ~future();
Evgeniy Stepanova3b25f82015-11-07 01:22:131221 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:501222 shared_future<_Rp&> share();
Howard Hinnant47499b12010-08-27 20:10:191223
1224 // retrieving the value
Howard Hinnant99968442011-11-29 18:15:501225 _Rp& get();
Howard Hinnant47499b12010-08-27 20:10:191226
Howard Hinnant8c6cbb22010-09-22 14:16:261227 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551228 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:191229
1230 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:261231 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551232 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant47499b12010-08-27 20:10:191233
Howard Hinnant8c6cbb22010-09-22 14:16:261234 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191235 void wait() const {__state_->wait();}
1236 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:261237 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191238 future_status
1239 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1240 {return __state_->wait_for(__rel_time);}
1241 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:261242 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191243 future_status
1244 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1245 {return __state_->wait_until(__abs_time);}
1246};
1247
Howard Hinnant99968442011-11-29 18:15:501248template <class _Rp>
1249future<_Rp&>::future(__assoc_state<_Rp&>* __state)
Howard Hinnant47499b12010-08-27 20:10:191250 : __state_(__state)
1251{
1252 if (__state_->__has_future_attached())
Eric Fiselier423ca202015-10-02 21:25:151253 __throw_future_error(future_errc::future_already_retrieved);
Howard Hinnant47499b12010-08-27 20:10:191254 __state_->__add_shared();
Howard Hinnant54da3382010-08-30 18:46:211255 __state_->__set_future_attached();
Howard Hinnant47499b12010-08-27 20:10:191256}
1257
Howard Hinnant99968442011-11-29 18:15:501258template <class _Rp>
1259future<_Rp&>::~future()
Howard Hinnant47499b12010-08-27 20:10:191260{
1261 if (__state_)
1262 __state_->__release_shared();
1263}
1264
Howard Hinnant99968442011-11-29 18:15:501265template <class _Rp>
1266_Rp&
1267future<_Rp&>::get()
Howard Hinnant47499b12010-08-27 20:10:191268{
Howard Hinnant54da3382010-08-30 18:46:211269 unique_ptr<__shared_count, __release_shared_count> __(__state_);
Howard Hinnant99968442011-11-29 18:15:501270 __assoc_state<_Rp&>* __s = __state_;
Howard Hinnant47499b12010-08-27 20:10:191271 __state_ = nullptr;
1272 return __s->copy();
1273}
1274
1275template <>
Howard Hinnant83eade62013-03-06 23:30:191276class _LIBCPP_TYPE_VIS future<void>
Howard Hinnant47499b12010-08-27 20:10:191277{
1278 __assoc_sub_state* __state_;
1279
1280 explicit future(__assoc_sub_state* __state);
1281
1282 template <class> friend class promise;
Howard Hinnant99be8232010-09-03 18:39:251283 template <class> friend class shared_future;
Howard Hinnant54da3382010-08-30 18:46:211284
Howard Hinnant73d21a42010-09-04 23:28:191285#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501286 template <class _R1, class _Fp>
1287 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1288 template <class _R1, class _Fp>
1289 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:211290#else
Howard Hinnant99968442011-11-29 18:15:501291 template <class _R1, class _Fp>
1292 friend future<_R1> __make_deferred_assoc_state(_Fp __f);
1293 template <class _R1, class _Fp>
1294 friend future<_R1> __make_async_assoc_state(_Fp __f);
Howard Hinnant54da3382010-08-30 18:46:211295#endif
1296
Howard Hinnant47499b12010-08-27 20:10:191297public:
Howard Hinnant8c6cbb22010-09-22 14:16:261298 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551299 future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant73d21a42010-09-04 23:28:191300#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261301 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551302 future(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191303 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1304 future(const future&) = delete;
1305 future& operator=(const future&) = delete;
Howard Hinnant8c6cbb22010-09-22 14:16:261306 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551307 future& operator=(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191308 {
1309 future(std::move(__rhs)).swap(*this);
1310 return *this;
1311 }
Howard Hinnant73d21a42010-09-04 23:28:191312#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191313private:
1314 future(const future&);
1315 future& operator=(const future&);
1316public:
Howard Hinnant73d21a42010-09-04 23:28:191317#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191318 ~future();
Evgeniy Stepanova3b25f82015-11-07 01:22:131319 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7de47902010-11-30 20:23:321320 shared_future<void> share();
Howard Hinnant47499b12010-08-27 20:10:191321
1322 // retrieving the value
1323 void get();
1324
Howard Hinnant8c6cbb22010-09-22 14:16:261325 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551326 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:191327
1328 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:261329 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551330 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant47499b12010-08-27 20:10:191331
Howard Hinnant8c6cbb22010-09-22 14:16:261332 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191333 void wait() const {__state_->wait();}
1334 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:261335 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191336 future_status
1337 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1338 {return __state_->wait_for(__rel_time);}
1339 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:261340 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191341 future_status
1342 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1343 {return __state_->wait_until(__abs_time);}
1344};
1345
Howard Hinnant99968442011-11-29 18:15:501346template <class _Rp>
Howard Hinnant99be8232010-09-03 18:39:251347inline _LIBCPP_INLINE_VISIBILITY
1348void
Howard Hinnant8bf01dd2012-07-21 17:46:551349swap(future<_Rp>& __x, future<_Rp>& __y) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:251350{
1351 __x.swap(__y);
1352}
1353
Howard Hinnant47499b12010-08-27 20:10:191354// promise<R>
1355
Howard Hinnant2b1b2d42011-06-14 19:58:171356template <class _Callable> class packaged_task;
Howard Hinnant54da3382010-08-30 18:46:211357
Howard Hinnant99968442011-11-29 18:15:501358template <class _Rp>
Howard Hinnant0f678bd2013-08-12 18:38:341359class _LIBCPP_TYPE_VIS_ONLY promise
Howard Hinnant47499b12010-08-27 20:10:191360{
Howard Hinnant99968442011-11-29 18:15:501361 __assoc_state<_Rp>* __state_;
Howard Hinnant54da3382010-08-30 18:46:211362
Howard Hinnant8c6cbb22010-09-22 14:16:261363 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551364 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant54da3382010-08-30 18:46:211365
1366 template <class> friend class packaged_task;
Howard Hinnant47499b12010-08-27 20:10:191367public:
1368 promise();
1369 template <class _Alloc>
1370 promise(allocator_arg_t, const _Alloc& __a);
Howard Hinnant73d21a42010-09-04 23:28:191371#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261372 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551373 promise(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191374 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1375 promise(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:191376#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191377private:
1378 promise(const promise& __rhs);
1379public:
Howard Hinnant73d21a42010-09-04 23:28:191380#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191381 ~promise();
1382
1383 // assignment
Howard Hinnant73d21a42010-09-04 23:28:191384#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261385 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551386 promise& operator=(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191387 {
1388 promise(std::move(__rhs)).swap(*this);
1389 return *this;
1390 }
1391 promise& operator=(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:191392#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191393private:
1394 promise& operator=(const promise& __rhs);
1395public:
Howard Hinnant73d21a42010-09-04 23:28:191396#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261397 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551398 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:191399
1400 // retrieving the result
Howard Hinnant99968442011-11-29 18:15:501401 future<_Rp> get_future();
Howard Hinnant47499b12010-08-27 20:10:191402
1403 // setting the result
Howard Hinnant99968442011-11-29 18:15:501404 void set_value(const _Rp& __r);
Howard Hinnant73d21a42010-09-04 23:28:191405#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501406 void set_value(_Rp&& __r);
Howard Hinnant47499b12010-08-27 20:10:191407#endif
1408 void set_exception(exception_ptr __p);
1409
1410 // setting the result with deferred notification
Howard Hinnant99968442011-11-29 18:15:501411 void set_value_at_thread_exit(const _Rp& __r);
Howard Hinnant73d21a42010-09-04 23:28:191412#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501413 void set_value_at_thread_exit(_Rp&& __r);
Howard Hinnant47499b12010-08-27 20:10:191414#endif
1415 void set_exception_at_thread_exit(exception_ptr __p);
1416};
1417
Howard Hinnant99968442011-11-29 18:15:501418template <class _Rp>
1419promise<_Rp>::promise()
1420 : __state_(new __assoc_state<_Rp>)
Howard Hinnant47499b12010-08-27 20:10:191421{
1422}
1423
Howard Hinnant99968442011-11-29 18:15:501424template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191425template <class _Alloc>
Howard Hinnant99968442011-11-29 18:15:501426promise<_Rp>::promise(allocator_arg_t, const _Alloc& __a0)
Howard Hinnant47499b12010-08-27 20:10:191427{
Eric Fiselier4d2413c2014-10-23 06:24:451428 typedef __assoc_state_alloc<_Rp, _Alloc> _State;
1429 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
Howard Hinnant47499b12010-08-27 20:10:191430 typedef __allocator_destructor<_A2> _D2;
1431 _A2 __a(__a0);
Eric Fiselier4d2413c2014-10-23 06:24:451432 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1433 ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0);
1434 __state_ = _VSTD::addressof(*__hold.release());
Howard Hinnant47499b12010-08-27 20:10:191435}
1436
Howard Hinnant99968442011-11-29 18:15:501437template <class _Rp>
1438promise<_Rp>::~promise()
Howard Hinnant47499b12010-08-27 20:10:191439{
1440 if (__state_)
1441 {
1442 if (!__state_->__has_value() && __state_->use_count() > 1)
1443 __state_->set_exception(make_exception_ptr(
1444 future_error(make_error_code(future_errc::broken_promise))
1445 ));
1446 __state_->__release_shared();
1447 }
1448}
1449
Howard Hinnant99968442011-11-29 18:15:501450template <class _Rp>
1451future<_Rp>
1452promise<_Rp>::get_future()
Howard Hinnant47499b12010-08-27 20:10:191453{
1454 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:151455 __throw_future_error(future_errc::no_state);
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{
1463 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:151464 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:191465 __state_->set_value(__r);
1466}
1467
Howard Hinnant73d21a42010-09-04 23:28:191468#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191469
Howard Hinnant99968442011-11-29 18:15:501470template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191471void
Howard Hinnant99968442011-11-29 18:15:501472promise<_Rp>::set_value(_Rp&& __r)
Howard Hinnant47499b12010-08-27 20:10:191473{
1474 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:151475 __throw_future_error(future_errc::no_state);
Howard Hinnant0949eed2011-06-30 21:18:191476 __state_->set_value(_VSTD::move(__r));
Howard Hinnant47499b12010-08-27 20:10:191477}
1478
Howard Hinnant73d21a42010-09-04 23:28:191479#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191480
Howard Hinnant99968442011-11-29 18:15:501481template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191482void
Howard Hinnant99968442011-11-29 18:15:501483promise<_Rp>::set_exception(exception_ptr __p)
Howard Hinnant47499b12010-08-27 20:10:191484{
1485 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:151486 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:191487 __state_->set_exception(__p);
1488}
1489
Howard Hinnant99968442011-11-29 18:15:501490template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191491void
Howard Hinnant99968442011-11-29 18:15:501492promise<_Rp>::set_value_at_thread_exit(const _Rp& __r)
Howard Hinnant47499b12010-08-27 20:10:191493{
1494 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:151495 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:191496 __state_->set_value_at_thread_exit(__r);
1497}
1498
Howard Hinnant73d21a42010-09-04 23:28:191499#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191500
Howard Hinnant99968442011-11-29 18:15:501501template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191502void
Howard Hinnant99968442011-11-29 18:15:501503promise<_Rp>::set_value_at_thread_exit(_Rp&& __r)
Howard Hinnant47499b12010-08-27 20:10:191504{
1505 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:151506 __throw_future_error(future_errc::no_state);
Howard Hinnant0949eed2011-06-30 21:18:191507 __state_->set_value_at_thread_exit(_VSTD::move(__r));
Howard Hinnant47499b12010-08-27 20:10:191508}
1509
Howard Hinnant73d21a42010-09-04 23:28:191510#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191511
Howard Hinnant99968442011-11-29 18:15:501512template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191513void
Howard Hinnant99968442011-11-29 18:15:501514promise<_Rp>::set_exception_at_thread_exit(exception_ptr __p)
Howard Hinnant47499b12010-08-27 20:10:191515{
1516 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:151517 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:191518 __state_->set_exception_at_thread_exit(__p);
1519}
1520
1521// promise<R&>
1522
Howard Hinnant99968442011-11-29 18:15:501523template <class _Rp>
Howard Hinnant0f678bd2013-08-12 18:38:341524class _LIBCPP_TYPE_VIS_ONLY promise<_Rp&>
Howard Hinnant47499b12010-08-27 20:10:191525{
Howard Hinnant99968442011-11-29 18:15:501526 __assoc_state<_Rp&>* __state_;
Howard Hinnant54da3382010-08-30 18:46:211527
Howard Hinnant8c6cbb22010-09-22 14:16:261528 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551529 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant54da3382010-08-30 18:46:211530
1531 template <class> friend class packaged_task;
1532
Howard Hinnant47499b12010-08-27 20:10:191533public:
1534 promise();
1535 template <class _Allocator>
1536 promise(allocator_arg_t, const _Allocator& __a);
Howard Hinnant73d21a42010-09-04 23:28:191537#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261538 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551539 promise(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191540 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1541 promise(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:191542#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191543private:
1544 promise(const promise& __rhs);
1545public:
Howard Hinnant73d21a42010-09-04 23:28:191546#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191547 ~promise();
1548
1549 // assignment
Howard Hinnant73d21a42010-09-04 23:28:191550#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261551 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551552 promise& operator=(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191553 {
1554 promise(std::move(__rhs)).swap(*this);
1555 return *this;
1556 }
1557 promise& operator=(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:191558#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191559private:
1560 promise& operator=(const promise& __rhs);
1561public:
Howard Hinnant73d21a42010-09-04 23:28:191562#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261563 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551564 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:191565
1566 // retrieving the result
Howard Hinnant99968442011-11-29 18:15:501567 future<_Rp&> get_future();
Howard Hinnant47499b12010-08-27 20:10:191568
1569 // setting the result
Howard Hinnant99968442011-11-29 18:15:501570 void set_value(_Rp& __r);
Howard Hinnant47499b12010-08-27 20:10:191571 void set_exception(exception_ptr __p);
1572
1573 // setting the result with deferred notification
Howard Hinnant99968442011-11-29 18:15:501574 void set_value_at_thread_exit(_Rp&);
Howard Hinnant47499b12010-08-27 20:10:191575 void set_exception_at_thread_exit(exception_ptr __p);
1576};
1577
Howard Hinnant99968442011-11-29 18:15:501578template <class _Rp>
1579promise<_Rp&>::promise()
1580 : __state_(new __assoc_state<_Rp&>)
Howard Hinnant47499b12010-08-27 20:10:191581{
1582}
1583
Howard Hinnant99968442011-11-29 18:15:501584template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191585template <class _Alloc>
Howard Hinnant99968442011-11-29 18:15:501586promise<_Rp&>::promise(allocator_arg_t, const _Alloc& __a0)
Howard Hinnant47499b12010-08-27 20:10:191587{
Eric Fiselier4d2413c2014-10-23 06:24:451588 typedef __assoc_state_alloc<_Rp&, _Alloc> _State;
1589 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
Howard Hinnant47499b12010-08-27 20:10:191590 typedef __allocator_destructor<_A2> _D2;
1591 _A2 __a(__a0);
Eric Fiselier4d2413c2014-10-23 06:24:451592 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1593 ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0);
1594 __state_ = _VSTD::addressof(*__hold.release());
Howard Hinnant47499b12010-08-27 20:10:191595}
1596
Howard Hinnant99968442011-11-29 18:15:501597template <class _Rp>
1598promise<_Rp&>::~promise()
Howard Hinnant47499b12010-08-27 20:10:191599{
1600 if (__state_)
1601 {
1602 if (!__state_->__has_value() && __state_->use_count() > 1)
1603 __state_->set_exception(make_exception_ptr(
1604 future_error(make_error_code(future_errc::broken_promise))
1605 ));
1606 __state_->__release_shared();
1607 }
1608}
1609
Howard Hinnant99968442011-11-29 18:15:501610template <class _Rp>
1611future<_Rp&>
1612promise<_Rp&>::get_future()
Howard Hinnant47499b12010-08-27 20:10:191613{
1614 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:151615 __throw_future_error(future_errc::no_state);
Howard Hinnant99968442011-11-29 18:15:501616 return future<_Rp&>(__state_);
Howard Hinnant47499b12010-08-27 20:10:191617}
1618
Howard Hinnant99968442011-11-29 18:15:501619template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191620void
Howard Hinnant99968442011-11-29 18:15:501621promise<_Rp&>::set_value(_Rp& __r)
Howard Hinnant47499b12010-08-27 20:10:191622{
1623 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:151624 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:191625 __state_->set_value(__r);
1626}
1627
Howard Hinnant99968442011-11-29 18:15:501628template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191629void
Howard Hinnant99968442011-11-29 18:15:501630promise<_Rp&>::set_exception(exception_ptr __p)
Howard Hinnant47499b12010-08-27 20:10:191631{
1632 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:151633 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:191634 __state_->set_exception(__p);
1635}
1636
Howard Hinnant99968442011-11-29 18:15:501637template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191638void
Howard Hinnant99968442011-11-29 18:15:501639promise<_Rp&>::set_value_at_thread_exit(_Rp& __r)
Howard Hinnant47499b12010-08-27 20:10:191640{
1641 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:151642 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:191643 __state_->set_value_at_thread_exit(__r);
1644}
1645
Howard Hinnant99968442011-11-29 18:15:501646template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191647void
Howard Hinnant99968442011-11-29 18:15:501648promise<_Rp&>::set_exception_at_thread_exit(exception_ptr __p)
Howard Hinnant47499b12010-08-27 20:10:191649{
1650 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:151651 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:191652 __state_->set_exception_at_thread_exit(__p);
1653}
1654
1655// promise<void>
1656
1657template <>
Howard Hinnant83eade62013-03-06 23:30:191658class _LIBCPP_TYPE_VIS promise<void>
Howard Hinnant47499b12010-08-27 20:10:191659{
1660 __assoc_sub_state* __state_;
Howard Hinnant54da3382010-08-30 18:46:211661
Howard Hinnant8c6cbb22010-09-22 14:16:261662 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551663 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant54da3382010-08-30 18:46:211664
1665 template <class> friend class packaged_task;
1666
Howard Hinnant47499b12010-08-27 20:10:191667public:
1668 promise();
1669 template <class _Allocator>
1670 promise(allocator_arg_t, const _Allocator& __a);
Howard Hinnant73d21a42010-09-04 23:28:191671#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261672 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551673 promise(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191674 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1675 promise(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:191676#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191677private:
1678 promise(const promise& __rhs);
1679public:
Howard Hinnant73d21a42010-09-04 23:28:191680#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191681 ~promise();
1682
1683 // assignment
Howard Hinnant73d21a42010-09-04 23:28:191684#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261685 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551686 promise& operator=(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191687 {
1688 promise(std::move(__rhs)).swap(*this);
1689 return *this;
1690 }
1691 promise& operator=(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:191692#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191693private:
1694 promise& operator=(const promise& __rhs);
1695public:
Howard Hinnant73d21a42010-09-04 23:28:191696#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261697 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551698 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:191699
1700 // retrieving the result
1701 future<void> get_future();
1702
1703 // setting the result
1704 void set_value();
1705 void set_exception(exception_ptr __p);
1706
1707 // setting the result with deferred notification
1708 void set_value_at_thread_exit();
1709 void set_exception_at_thread_exit(exception_ptr __p);
1710};
1711
1712template <class _Alloc>
1713promise<void>::promise(allocator_arg_t, const _Alloc& __a0)
1714{
Eric Fiselier4d2413c2014-10-23 06:24:451715 typedef __assoc_sub_state_alloc<_Alloc> _State;
1716 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
Howard Hinnant47499b12010-08-27 20:10:191717 typedef __allocator_destructor<_A2> _D2;
1718 _A2 __a(__a0);
Eric Fiselier4d2413c2014-10-23 06:24:451719 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1720 ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0);
1721 __state_ = _VSTD::addressof(*__hold.release());
Howard Hinnant47499b12010-08-27 20:10:191722}
1723
Howard Hinnant99968442011-11-29 18:15:501724template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191725inline _LIBCPP_INLINE_VISIBILITY
1726void
Howard Hinnant8bf01dd2012-07-21 17:46:551727swap(promise<_Rp>& __x, promise<_Rp>& __y) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191728{
1729 __x.swap(__y);
1730}
1731
Howard Hinnant99968442011-11-29 18:15:501732template <class _Rp, class _Alloc>
Howard Hinnant0f678bd2013-08-12 18:38:341733 struct _LIBCPP_TYPE_VIS_ONLY uses_allocator<promise<_Rp>, _Alloc>
Howard Hinnant8c6cbb22010-09-22 14:16:261734 : public true_type {};
Howard Hinnant47499b12010-08-27 20:10:191735
Howard Hinnant54da3382010-08-30 18:46:211736#ifndef _LIBCPP_HAS_NO_VARIADICS
1737
1738// packaged_task
1739
1740template<class _Fp> class __packaged_task_base;
1741
Howard Hinnant99968442011-11-29 18:15:501742template<class _Rp, class ..._ArgTypes>
1743class __packaged_task_base<_Rp(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:211744{
1745 __packaged_task_base(const __packaged_task_base&);
1746 __packaged_task_base& operator=(const __packaged_task_base&);
1747public:
Howard Hinnant8c6cbb22010-09-22 14:16:261748 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:211749 __packaged_task_base() {}
Howard Hinnant8c6cbb22010-09-22 14:16:261750 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:211751 virtual ~__packaged_task_base() {}
Howard Hinnant8bf01dd2012-07-21 17:46:551752 virtual void __move_to(__packaged_task_base*) _NOEXCEPT = 0;
Howard Hinnant54da3382010-08-30 18:46:211753 virtual void destroy() = 0;
1754 virtual void destroy_deallocate() = 0;
Howard Hinnant99968442011-11-29 18:15:501755 virtual _Rp operator()(_ArgTypes&& ...) = 0;
Howard Hinnant54da3382010-08-30 18:46:211756};
1757
1758template<class _FD, class _Alloc, class _FB> class __packaged_task_func;
1759
Howard Hinnant99968442011-11-29 18:15:501760template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1761class __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>
1762 : public __packaged_task_base<_Rp(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:211763{
Howard Hinnant99968442011-11-29 18:15:501764 __compressed_pair<_Fp, _Alloc> __f_;
Howard Hinnant54da3382010-08-30 18:46:211765public:
Howard Hinnant8c6cbb22010-09-22 14:16:261766 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:501767 explicit __packaged_task_func(const _Fp& __f) : __f_(__f) {}
Howard Hinnant8c6cbb22010-09-22 14:16:261768 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:501769 explicit __packaged_task_func(_Fp&& __f) : __f_(_VSTD::move(__f)) {}
Howard Hinnant8c6cbb22010-09-22 14:16:261770 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:501771 __packaged_task_func(const _Fp& __f, const _Alloc& __a)
Howard Hinnant54da3382010-08-30 18:46:211772 : __f_(__f, __a) {}
Howard Hinnant8c6cbb22010-09-22 14:16:261773 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:501774 __packaged_task_func(_Fp&& __f, const _Alloc& __a)
Howard Hinnant0949eed2011-06-30 21:18:191775 : __f_(_VSTD::move(__f), __a) {}
Howard Hinnant8bf01dd2012-07-21 17:46:551776 virtual void __move_to(__packaged_task_base<_Rp(_ArgTypes...)>*) _NOEXCEPT;
Howard Hinnant54da3382010-08-30 18:46:211777 virtual void destroy();
1778 virtual void destroy_deallocate();
Howard Hinnant99968442011-11-29 18:15:501779 virtual _Rp operator()(_ArgTypes&& ... __args);
Howard Hinnant54da3382010-08-30 18:46:211780};
1781
Howard Hinnant99968442011-11-29 18:15:501782template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:211783void
Howard Hinnant99968442011-11-29 18:15:501784__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__move_to(
Howard Hinnant8bf01dd2012-07-21 17:46:551785 __packaged_task_base<_Rp(_ArgTypes...)>* __p) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:211786{
Howard Hinnant0949eed2011-06-30 21:18:191787 ::new (__p) __packaged_task_func(_VSTD::move(__f_.first()), _VSTD::move(__f_.second()));
Howard Hinnant54da3382010-08-30 18:46:211788}
1789
Howard Hinnant99968442011-11-29 18:15:501790template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:211791void
Howard Hinnant99968442011-11-29 18:15:501792__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy()
Howard Hinnant54da3382010-08-30 18:46:211793{
Howard Hinnant99968442011-11-29 18:15:501794 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnant54da3382010-08-30 18:46:211795}
1796
Howard Hinnant99968442011-11-29 18:15:501797template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:211798void
Howard Hinnant99968442011-11-29 18:15:501799__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate()
Howard Hinnant54da3382010-08-30 18:46:211800{
Eric Fiselier4d2413c2014-10-23 06:24:451801 typedef typename __allocator_traits_rebind<_Alloc, __packaged_task_func>::type _Ap;
1802 typedef allocator_traits<_Ap> _ATraits;
1803 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Howard Hinnant99968442011-11-29 18:15:501804 _Ap __a(__f_.second());
1805 __f_.~__compressed_pair<_Fp, _Alloc>();
Eric Fiselier4d2413c2014-10-23 06:24:451806 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnant54da3382010-08-30 18:46:211807}
1808
Howard Hinnant99968442011-11-29 18:15:501809template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1810_Rp
1811__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
Howard Hinnant54da3382010-08-30 18:46:211812{
Howard Hinnant0949eed2011-06-30 21:18:191813 return __invoke(__f_.first(), _VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnant54da3382010-08-30 18:46:211814}
1815
Howard Hinnant2b1b2d42011-06-14 19:58:171816template <class _Callable> class __packaged_task_function;
Howard Hinnant54da3382010-08-30 18:46:211817
Howard Hinnant99968442011-11-29 18:15:501818template<class _Rp, class ..._ArgTypes>
1819class __packaged_task_function<_Rp(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:211820{
Howard Hinnant99968442011-11-29 18:15:501821 typedef __packaged_task_base<_Rp(_ArgTypes...)> __base;
Howard Hinnant78f0de22013-01-21 17:26:551822 typename aligned_storage<3*sizeof(void*)>::type __buf_;
Howard Hinnant54da3382010-08-30 18:46:211823 __base* __f_;
1824
1825public:
Howard Hinnant99968442011-11-29 18:15:501826 typedef _Rp result_type;
Howard Hinnant54da3382010-08-30 18:46:211827
1828 // construct/copy/destroy:
Howard Hinnant8c6cbb22010-09-22 14:16:261829 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551830 __packaged_task_function() _NOEXCEPT : __f_(nullptr) {}
Howard Hinnant99968442011-11-29 18:15:501831 template<class _Fp>
1832 __packaged_task_function(_Fp&& __f);
1833 template<class _Fp, class _Alloc>
1834 __packaged_task_function(allocator_arg_t, const _Alloc& __a, _Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:211835
Howard Hinnant8bf01dd2012-07-21 17:46:551836 __packaged_task_function(__packaged_task_function&&) _NOEXCEPT;
1837 __packaged_task_function& operator=(__packaged_task_function&&) _NOEXCEPT;
Howard Hinnant54da3382010-08-30 18:46:211838
1839 __packaged_task_function(const __packaged_task_function&) = delete;
1840 __packaged_task_function& operator=(const __packaged_task_function&) = delete;
1841
1842 ~__packaged_task_function();
1843
Howard Hinnant8bf01dd2012-07-21 17:46:551844 void swap(__packaged_task_function&) _NOEXCEPT;
Howard Hinnant54da3382010-08-30 18:46:211845
Evgeniy Stepanova3b25f82015-11-07 01:22:131846 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:501847 _Rp operator()(_ArgTypes...) const;
Howard Hinnant54da3382010-08-30 18:46:211848};
1849
Howard Hinnant99968442011-11-29 18:15:501850template<class _Rp, class ..._ArgTypes>
Howard Hinnant8bf01dd2012-07-21 17:46:551851__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:211852{
1853 if (__f.__f_ == nullptr)
1854 __f_ = nullptr;
1855 else if (__f.__f_ == (__base*)&__f.__buf_)
1856 {
1857 __f_ = (__base*)&__buf_;
1858 __f.__f_->__move_to(__f_);
1859 }
1860 else
1861 {
1862 __f_ = __f.__f_;
1863 __f.__f_ = nullptr;
1864 }
1865}
1866
Howard Hinnant99968442011-11-29 18:15:501867template<class _Rp, class ..._ArgTypes>
1868template <class _Fp>
1869__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(_Fp&& __f)
Howard Hinnant54da3382010-08-30 18:46:211870 : __f_(nullptr)
1871{
Marshall Clowf1264e72014-04-07 13:32:261872 typedef typename remove_reference<typename decay<_Fp>::type>::type _FR;
Howard Hinnant99968442011-11-29 18:15:501873 typedef __packaged_task_func<_FR, allocator<_FR>, _Rp(_ArgTypes...)> _FF;
Howard Hinnant54da3382010-08-30 18:46:211874 if (sizeof(_FF) <= sizeof(__buf_))
1875 {
1876 __f_ = (__base*)&__buf_;
Howard Hinnant99968442011-11-29 18:15:501877 ::new (__f_) _FF(_VSTD::forward<_Fp>(__f));
Howard Hinnant54da3382010-08-30 18:46:211878 }
1879 else
1880 {
Howard Hinnant99968442011-11-29 18:15:501881 typedef allocator<_FF> _Ap;
1882 _Ap __a;
1883 typedef __allocator_destructor<_Ap> _Dp;
1884 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1885 ::new (__hold.get()) _FF(_VSTD::forward<_Fp>(__f), allocator<_FR>(__a));
Howard Hinnant54da3382010-08-30 18:46:211886 __f_ = __hold.release();
1887 }
1888}
1889
Howard Hinnant99968442011-11-29 18:15:501890template<class _Rp, class ..._ArgTypes>
1891template <class _Fp, class _Alloc>
1892__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(
1893 allocator_arg_t, const _Alloc& __a0, _Fp&& __f)
Howard Hinnant54da3382010-08-30 18:46:211894 : __f_(nullptr)
1895{
Marshall Clowf1264e72014-04-07 13:32:261896 typedef typename remove_reference<typename decay<_Fp>::type>::type _FR;
Howard Hinnant99968442011-11-29 18:15:501897 typedef __packaged_task_func<_FR, _Alloc, _Rp(_ArgTypes...)> _FF;
Howard Hinnant54da3382010-08-30 18:46:211898 if (sizeof(_FF) <= sizeof(__buf_))
1899 {
1900 __f_ = (__base*)&__buf_;
Howard Hinnant99968442011-11-29 18:15:501901 ::new (__f_) _FF(_VSTD::forward<_Fp>(__f));
Howard Hinnant54da3382010-08-30 18:46:211902 }
1903 else
1904 {
Eric Fiselier4d2413c2014-10-23 06:24:451905 typedef typename __allocator_traits_rebind<_Alloc, _FF>::type _Ap;
Howard Hinnant99968442011-11-29 18:15:501906 _Ap __a(__a0);
1907 typedef __allocator_destructor<_Ap> _Dp;
1908 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Eric Fiselier4d2413c2014-10-23 06:24:451909 ::new (static_cast<void*>(_VSTD::addressof(*__hold.get())))
1910 _FF(_VSTD::forward<_Fp>(__f), _Alloc(__a));
1911 __f_ = _VSTD::addressof(*__hold.release());
Howard Hinnant54da3382010-08-30 18:46:211912 }
1913}
1914
Howard Hinnant99968442011-11-29 18:15:501915template<class _Rp, class ..._ArgTypes>
1916__packaged_task_function<_Rp(_ArgTypes...)>&
Howard Hinnant8bf01dd2012-07-21 17:46:551917__packaged_task_function<_Rp(_ArgTypes...)>::operator=(__packaged_task_function&& __f) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:211918{
1919 if (__f_ == (__base*)&__buf_)
1920 __f_->destroy();
1921 else if (__f_)
1922 __f_->destroy_deallocate();
1923 __f_ = nullptr;
1924 if (__f.__f_ == nullptr)
1925 __f_ = nullptr;
1926 else if (__f.__f_ == (__base*)&__f.__buf_)
1927 {
1928 __f_ = (__base*)&__buf_;
1929 __f.__f_->__move_to(__f_);
1930 }
1931 else
1932 {
1933 __f_ = __f.__f_;
1934 __f.__f_ = nullptr;
1935 }
Argyrios Kyrtzidis1dc6f7a2012-10-13 02:03:451936 return *this;
Howard Hinnant54da3382010-08-30 18:46:211937}
1938
Howard Hinnant99968442011-11-29 18:15:501939template<class _Rp, class ..._ArgTypes>
1940__packaged_task_function<_Rp(_ArgTypes...)>::~__packaged_task_function()
Howard Hinnant54da3382010-08-30 18:46:211941{
1942 if (__f_ == (__base*)&__buf_)
1943 __f_->destroy();
1944 else if (__f_)
1945 __f_->destroy_deallocate();
1946}
1947
Howard Hinnant99968442011-11-29 18:15:501948template<class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:211949void
Howard Hinnant8bf01dd2012-07-21 17:46:551950__packaged_task_function<_Rp(_ArgTypes...)>::swap(__packaged_task_function& __f) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:211951{
1952 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1953 {
1954 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1955 __base* __t = (__base*)&__tempbuf;
1956 __f_->__move_to(__t);
1957 __f_->destroy();
1958 __f_ = nullptr;
1959 __f.__f_->__move_to((__base*)&__buf_);
1960 __f.__f_->destroy();
1961 __f.__f_ = nullptr;
1962 __f_ = (__base*)&__buf_;
1963 __t->__move_to((__base*)&__f.__buf_);
1964 __t->destroy();
1965 __f.__f_ = (__base*)&__f.__buf_;
1966 }
1967 else if (__f_ == (__base*)&__buf_)
1968 {
1969 __f_->__move_to((__base*)&__f.__buf_);
1970 __f_->destroy();
1971 __f_ = __f.__f_;
1972 __f.__f_ = (__base*)&__f.__buf_;
1973 }
1974 else if (__f.__f_ == (__base*)&__f.__buf_)
1975 {
1976 __f.__f_->__move_to((__base*)&__buf_);
1977 __f.__f_->destroy();
1978 __f.__f_ = __f_;
1979 __f_ = (__base*)&__buf_;
1980 }
1981 else
Howard Hinnant0949eed2011-06-30 21:18:191982 _VSTD::swap(__f_, __f.__f_);
Howard Hinnant54da3382010-08-30 18:46:211983}
1984
Howard Hinnant99968442011-11-29 18:15:501985template<class _Rp, class ..._ArgTypes>
Evgeniy Stepanova3b25f82015-11-07 01:22:131986inline
Howard Hinnant99968442011-11-29 18:15:501987_Rp
1988__packaged_task_function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
Howard Hinnant54da3382010-08-30 18:46:211989{
Howard Hinnant0949eed2011-06-30 21:18:191990 return (*__f_)(_VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnant54da3382010-08-30 18:46:211991}
1992
Howard Hinnant99968442011-11-29 18:15:501993template<class _Rp, class ..._ArgTypes>
Howard Hinnant0f678bd2013-08-12 18:38:341994class _LIBCPP_TYPE_VIS_ONLY packaged_task<_Rp(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:211995{
1996public:
Howard Hinnant99968442011-11-29 18:15:501997 typedef _Rp result_type;
Howard Hinnant54da3382010-08-30 18:46:211998
1999private:
2000 __packaged_task_function<result_type(_ArgTypes...)> __f_;
2001 promise<result_type> __p_;
2002
2003public:
2004 // construction and destruction
Howard Hinnant8c6cbb22010-09-22 14:16:262005 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552006 packaged_task() _NOEXCEPT : __p_(nullptr) {}
Marshall Clow5f2d5b92013-10-12 22:49:172007 template <class _Fp,
2008 class = typename enable_if
2009 <
2010 !is_same<
2011 typename decay<_Fp>::type,
2012 packaged_task
2013 >::value
2014 >::type
2015 >
Howard Hinnant8c6cbb22010-09-22 14:16:262016 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502017 explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
Marshall Clow5f2d5b92013-10-12 22:49:172018 template <class _Fp, class _Allocator,
2019 class = typename enable_if
2020 <
2021 !is_same<
2022 typename decay<_Fp>::type,
2023 packaged_task
2024 >::value
2025 >::type
2026 >
Howard Hinnant8c6cbb22010-09-22 14:16:262027 _LIBCPP_INLINE_VISIBILITY
Marshall Clow07546f32015-06-30 14:16:492028 packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
Howard Hinnant99968442011-11-29 18:15:502029 : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)),
Howard Hinnant54da3382010-08-30 18:46:212030 __p_(allocator_arg, __a) {}
2031 // ~packaged_task() = default;
2032
2033 // no copy
Howard Hinnant8131a012012-07-21 19:34:122034 packaged_task(const packaged_task&) = delete;
2035 packaged_task& operator=(const packaged_task&) = delete;
Howard Hinnant54da3382010-08-30 18:46:212036
2037 // move support
Howard Hinnant8c6cbb22010-09-22 14:16:262038 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552039 packaged_task(packaged_task&& __other) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:192040 : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
Howard Hinnant8c6cbb22010-09-22 14:16:262041 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552042 packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:212043 {
Howard Hinnant0949eed2011-06-30 21:18:192044 __f_ = _VSTD::move(__other.__f_);
2045 __p_ = _VSTD::move(__other.__p_);
Howard Hinnant54da3382010-08-30 18:46:212046 return *this;
2047 }
Howard Hinnant8c6cbb22010-09-22 14:16:262048 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552049 void swap(packaged_task& __other) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:212050 {
2051 __f_.swap(__other.__f_);
2052 __p_.swap(__other.__p_);
2053 }
2054
Howard Hinnant8c6cbb22010-09-22 14:16:262055 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552056 bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
Howard Hinnant54da3382010-08-30 18:46:212057
2058 // result retrieval
Howard Hinnant8c6cbb22010-09-22 14:16:262059 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:212060 future<result_type> get_future() {return __p_.get_future();}
2061
2062 // execution
2063 void operator()(_ArgTypes... __args);
2064 void make_ready_at_thread_exit(_ArgTypes... __args);
2065
2066 void reset();
2067};
2068
Howard Hinnant99968442011-11-29 18:15:502069template<class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:212070void
Howard Hinnant99968442011-11-29 18:15:502071packaged_task<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __args)
Howard Hinnant54da3382010-08-30 18:46:212072{
Howard Hinnant54da3382010-08-30 18:46:212073 if (__p_.__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:152074 __throw_future_error(future_errc::no_state);
Howard Hinnant54da3382010-08-30 18:46:212075 if (__p_.__state_->__has_value())
Eric Fiselier423ca202015-10-02 21:25:152076 __throw_future_error(future_errc::promise_already_satisfied);
Marshall Clowa1899742015-09-03 15:11:322077#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant54da3382010-08-30 18:46:212078 try
2079 {
2080#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:192081 __p_.set_value(__f_(_VSTD::forward<_ArgTypes>(__args)...));
Howard Hinnant54da3382010-08-30 18:46:212082#ifndef _LIBCPP_NO_EXCEPTIONS
2083 }
2084 catch (...)
2085 {
2086 __p_.set_exception(current_exception());
2087 }
2088#endif // _LIBCPP_NO_EXCEPTIONS
2089}
2090
Howard Hinnant99968442011-11-29 18:15:502091template<class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:212092void
Howard Hinnant99968442011-11-29 18:15:502093packaged_task<_Rp(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
Howard Hinnant54da3382010-08-30 18:46:212094{
Howard Hinnant54da3382010-08-30 18:46:212095 if (__p_.__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:152096 __throw_future_error(future_errc::no_state);
Howard Hinnant54da3382010-08-30 18:46:212097 if (__p_.__state_->__has_value())
Eric Fiselier423ca202015-10-02 21:25:152098 __throw_future_error(future_errc::promise_already_satisfied);
Marshall Clowa1899742015-09-03 15:11:322099#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant54da3382010-08-30 18:46:212100 try
2101 {
2102#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:192103 __p_.set_value_at_thread_exit(__f_(_VSTD::forward<_ArgTypes>(__args)...));
Howard Hinnant54da3382010-08-30 18:46:212104#ifndef _LIBCPP_NO_EXCEPTIONS
2105 }
2106 catch (...)
2107 {
2108 __p_.set_exception_at_thread_exit(current_exception());
2109 }
2110#endif // _LIBCPP_NO_EXCEPTIONS
2111}
2112
Howard Hinnant99968442011-11-29 18:15:502113template<class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:212114void
Howard Hinnant99968442011-11-29 18:15:502115packaged_task<_Rp(_ArgTypes...)>::reset()
Howard Hinnant54da3382010-08-30 18:46:212116{
Howard Hinnant7de47902010-11-30 20:23:322117 if (!valid())
Eric Fiselier423ca202015-10-02 21:25:152118 __throw_future_error(future_errc::no_state);
Howard Hinnant54da3382010-08-30 18:46:212119 __p_ = promise<result_type>();
2120}
2121
2122template<class ..._ArgTypes>
Howard Hinnant0f678bd2013-08-12 18:38:342123class _LIBCPP_TYPE_VIS_ONLY packaged_task<void(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:212124{
2125public:
2126 typedef void result_type;
2127
2128private:
2129 __packaged_task_function<result_type(_ArgTypes...)> __f_;
2130 promise<result_type> __p_;
2131
2132public:
2133 // construction and destruction
Howard Hinnant8c6cbb22010-09-22 14:16:262134 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552135 packaged_task() _NOEXCEPT : __p_(nullptr) {}
Marshall Clow5f2d5b92013-10-12 22:49:172136 template <class _Fp,
2137 class = typename enable_if
2138 <
2139 !is_same<
2140 typename decay<_Fp>::type,
2141 packaged_task
2142 >::value
2143 >::type
2144 >
Howard Hinnant8c6cbb22010-09-22 14:16:262145 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502146 explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
Marshall Clow5f2d5b92013-10-12 22:49:172147 template <class _Fp, class _Allocator,
2148 class = typename enable_if
2149 <
2150 !is_same<
2151 typename decay<_Fp>::type,
2152 packaged_task
2153 >::value
2154 >::type
2155 >
Howard Hinnant8c6cbb22010-09-22 14:16:262156 _LIBCPP_INLINE_VISIBILITY
Marshall Clow5706c372015-06-30 18:28:352157 packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
Howard Hinnant99968442011-11-29 18:15:502158 : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)),
Howard Hinnant54da3382010-08-30 18:46:212159 __p_(allocator_arg, __a) {}
2160 // ~packaged_task() = default;
2161
2162 // no copy
Howard Hinnant8131a012012-07-21 19:34:122163 packaged_task(const packaged_task&) = delete;
2164 packaged_task& operator=(const packaged_task&) = delete;
Howard Hinnant54da3382010-08-30 18:46:212165
2166 // move support
Howard Hinnant8c6cbb22010-09-22 14:16:262167 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552168 packaged_task(packaged_task&& __other) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:192169 : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
Howard Hinnant8c6cbb22010-09-22 14:16:262170 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552171 packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:212172 {
Howard Hinnant0949eed2011-06-30 21:18:192173 __f_ = _VSTD::move(__other.__f_);
2174 __p_ = _VSTD::move(__other.__p_);
Howard Hinnant54da3382010-08-30 18:46:212175 return *this;
2176 }
Howard Hinnant8c6cbb22010-09-22 14:16:262177 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552178 void swap(packaged_task& __other) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:212179 {
2180 __f_.swap(__other.__f_);
2181 __p_.swap(__other.__p_);
2182 }
2183
Howard Hinnant8c6cbb22010-09-22 14:16:262184 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552185 bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
Howard Hinnant54da3382010-08-30 18:46:212186
2187 // result retrieval
Howard Hinnant8c6cbb22010-09-22 14:16:262188 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:212189 future<result_type> get_future() {return __p_.get_future();}
2190
2191 // execution
2192 void operator()(_ArgTypes... __args);
2193 void make_ready_at_thread_exit(_ArgTypes... __args);
2194
2195 void reset();
2196};
2197
2198template<class ..._ArgTypes>
2199void
2200packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args)
2201{
Howard Hinnant54da3382010-08-30 18:46:212202 if (__p_.__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:152203 __throw_future_error(future_errc::no_state);
Howard Hinnant54da3382010-08-30 18:46:212204 if (__p_.__state_->__has_value())
Eric Fiselier423ca202015-10-02 21:25:152205 __throw_future_error(future_errc::promise_already_satisfied);
Marshall Clowa1899742015-09-03 15:11:322206#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant54da3382010-08-30 18:46:212207 try
2208 {
2209#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:192210 __f_(_VSTD::forward<_ArgTypes>(__args)...);
Howard Hinnant54da3382010-08-30 18:46:212211 __p_.set_value();
2212#ifndef _LIBCPP_NO_EXCEPTIONS
2213 }
2214 catch (...)
2215 {
2216 __p_.set_exception(current_exception());
2217 }
2218#endif // _LIBCPP_NO_EXCEPTIONS
2219}
2220
2221template<class ..._ArgTypes>
2222void
2223packaged_task<void(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
2224{
Howard Hinnant54da3382010-08-30 18:46:212225 if (__p_.__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:152226 __throw_future_error(future_errc::no_state);
Howard Hinnant54da3382010-08-30 18:46:212227 if (__p_.__state_->__has_value())
Eric Fiselier423ca202015-10-02 21:25:152228 __throw_future_error(future_errc::promise_already_satisfied);
Marshall Clowa1899742015-09-03 15:11:322229#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant54da3382010-08-30 18:46:212230 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_at_thread_exit();
2235#ifndef _LIBCPP_NO_EXCEPTIONS
2236 }
2237 catch (...)
2238 {
2239 __p_.set_exception_at_thread_exit(current_exception());
2240 }
2241#endif // _LIBCPP_NO_EXCEPTIONS
2242}
2243
2244template<class ..._ArgTypes>
2245void
2246packaged_task<void(_ArgTypes...)>::reset()
2247{
Howard Hinnant7de47902010-11-30 20:23:322248 if (!valid())
Eric Fiselier423ca202015-10-02 21:25:152249 __throw_future_error(future_errc::no_state);
Howard Hinnant54da3382010-08-30 18:46:212250 __p_ = promise<result_type>();
2251}
2252
2253template <class _Callable>
2254inline _LIBCPP_INLINE_VISIBILITY
2255void
Howard Hinnant8bf01dd2012-07-21 17:46:552256swap(packaged_task<_Callable>& __x, packaged_task<_Callable>& __y) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:212257{
2258 __x.swap(__y);
2259}
2260
2261template <class _Callable, class _Alloc>
Howard Hinnant0f678bd2013-08-12 18:38:342262struct _LIBCPP_TYPE_VIS_ONLY uses_allocator<packaged_task<_Callable>, _Alloc>
Howard Hinnant8c6cbb22010-09-22 14:16:262263 : public true_type {};
Howard Hinnant54da3382010-08-30 18:46:212264
Howard Hinnant99968442011-11-29 18:15:502265template <class _Rp, class _Fp>
2266future<_Rp>
Howard Hinnant73d21a42010-09-04 23:28:192267#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:502268__make_deferred_assoc_state(_Fp&& __f)
Howard Hinnant54da3382010-08-30 18:46:212269#else
Howard Hinnant99968442011-11-29 18:15:502270__make_deferred_assoc_state(_Fp __f)
Howard Hinnant54da3382010-08-30 18:46:212271#endif
2272{
Howard Hinnant99968442011-11-29 18:15:502273 unique_ptr<__deferred_assoc_state<_Rp, _Fp>, __release_shared_count>
2274 __h(new __deferred_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
2275 return future<_Rp>(__h.get());
Howard Hinnant54da3382010-08-30 18:46:212276}
2277
Howard Hinnant99968442011-11-29 18:15:502278template <class _Rp, class _Fp>
2279future<_Rp>
Howard Hinnant57cff292011-05-19 15:05:042280#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:502281__make_async_assoc_state(_Fp&& __f)
Howard Hinnant57cff292011-05-19 15:05:042282#else
Howard Hinnant99968442011-11-29 18:15:502283__make_async_assoc_state(_Fp __f)
Howard Hinnant57cff292011-05-19 15:05:042284#endif
2285{
Howard Hinnant99968442011-11-29 18:15:502286 unique_ptr<__async_assoc_state<_Rp, _Fp>, __release_shared_count>
2287 __h(new __async_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
2288 _VSTD::thread(&__async_assoc_state<_Rp, _Fp>::__execute, __h.get()).detach();
2289 return future<_Rp>(__h.get());
Howard Hinnant57cff292011-05-19 15:05:042290}
2291
Howard Hinnant99968442011-11-29 18:15:502292template <class _Fp, class... _Args>
Howard Hinnant57cff292011-05-19 15:05:042293class __async_func
2294{
Howard Hinnant99968442011-11-29 18:15:502295 tuple<_Fp, _Args...> __f_;
Howard Hinnant57cff292011-05-19 15:05:042296
2297public:
Howard Hinnant99968442011-11-29 18:15:502298 typedef typename __invoke_of<_Fp, _Args...>::type _Rp;
Howard Hinnant57cff292011-05-19 15:05:042299
2300 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502301 explicit __async_func(_Fp&& __f, _Args&&... __args)
Howard Hinnant0949eed2011-06-30 21:18:192302 : __f_(_VSTD::move(__f), _VSTD::move(__args)...) {}
Howard Hinnant57cff292011-05-19 15:05:042303
2304 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0949eed2011-06-30 21:18:192305 __async_func(__async_func&& __f) : __f_(_VSTD::move(__f.__f_)) {}
Howard Hinnant57cff292011-05-19 15:05:042306
Howard Hinnant99968442011-11-29 18:15:502307 _Rp operator()()
Howard Hinnant57cff292011-05-19 15:05:042308 {
2309 typedef typename __make_tuple_indices<1+sizeof...(_Args), 1>::type _Index;
2310 return __execute(_Index());
2311 }
2312private:
2313 template <size_t ..._Indices>
Howard Hinnant99968442011-11-29 18:15:502314 _Rp
Howard Hinnant57cff292011-05-19 15:05:042315 __execute(__tuple_indices<_Indices...>)
2316 {
Howard Hinnant0949eed2011-06-30 21:18:192317 return __invoke(_VSTD::move(_VSTD::get<0>(__f_)), _VSTD::move(_VSTD::get<_Indices>(__f_))...);
Howard Hinnant57cff292011-05-19 15:05:042318 }
2319};
2320
Marshall Clow3b3108e2013-11-03 22:06:532321inline _LIBCPP_INLINE_VISIBILITY bool __does_policy_contain(launch __policy, launch __value )
Marshall Clowad2a6002013-11-03 15:43:352322{ return (int(__policy) & int(__value)) != 0; }
2323
Howard Hinnant99968442011-11-29 18:15:502324template <class _Fp, class... _Args>
2325future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
2326async(launch __policy, _Fp&& __f, _Args&&... __args)
Howard Hinnant54da3382010-08-30 18:46:212327{
Howard Hinnant99968442011-11-29 18:15:502328 typedef __async_func<typename decay<_Fp>::type, typename decay<_Args>::type...> _BF;
2329 typedef typename _BF::_Rp _Rp;
Marshall Clowad2a6002013-11-03 15:43:352330
2331#ifndef _LIBCPP_NO_EXCEPTIONS
2332 try
2333 {
2334#endif
2335 if (__does_policy_contain(__policy, launch::async))
2336 return _VSTD::__make_async_assoc_state<_Rp>(_BF(__decay_copy(_VSTD::forward<_Fp>(__f)),
Howard Hinnant0949eed2011-06-30 21:18:192337 __decay_copy(_VSTD::forward<_Args>(__args))...));
Marshall Clowad2a6002013-11-03 15:43:352338#ifndef _LIBCPP_NO_EXCEPTIONS
2339 }
2340 catch ( ... ) { if (__policy == launch::async) throw ; }
2341#endif
2342
2343 if (__does_policy_contain(__policy, launch::deferred))
2344 return _VSTD::__make_deferred_assoc_state<_Rp>(_BF(__decay_copy(_VSTD::forward<_Fp>(__f)),
Howard Hinnant0949eed2011-06-30 21:18:192345 __decay_copy(_VSTD::forward<_Args>(__args))...));
Marshall Clowad2a6002013-11-03 15:43:352346 return future<_Rp>{};
Howard Hinnant54da3382010-08-30 18:46:212347}
2348
Howard Hinnant99968442011-11-29 18:15:502349template <class _Fp, class... _Args>
Howard Hinnant54da3382010-08-30 18:46:212350inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502351future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
2352async(_Fp&& __f, _Args&&... __args)
Howard Hinnant54da3382010-08-30 18:46:212353{
Howard Hinnant99968442011-11-29 18:15:502354 return _VSTD::async(launch::any, _VSTD::forward<_Fp>(__f),
Howard Hinnant0949eed2011-06-30 21:18:192355 _VSTD::forward<_Args>(__args)...);
Howard Hinnant54da3382010-08-30 18:46:212356}
2357
2358#endif // _LIBCPP_HAS_NO_VARIADICS
2359
Howard Hinnante6e4d012010-09-03 21:46:372360// shared_future
2361
Howard Hinnant99968442011-11-29 18:15:502362template <class _Rp>
Howard Hinnant0f678bd2013-08-12 18:38:342363class _LIBCPP_TYPE_VIS_ONLY shared_future
Howard Hinnant99be8232010-09-03 18:39:252364{
Howard Hinnant99968442011-11-29 18:15:502365 __assoc_state<_Rp>* __state_;
Howard Hinnant99be8232010-09-03 18:39:252366
2367public:
Howard Hinnant8c6cbb22010-09-22 14:16:262368 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552369 shared_future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant8c6cbb22010-09-22 14:16:262370 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252371 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2372 {if (__state_) __state_->__add_shared();}
Howard Hinnant73d21a42010-09-04 23:28:192373#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:262374 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552375 shared_future(future<_Rp>&& __f) _NOEXCEPT : __state_(__f.__state_)
Howard Hinnant99be8232010-09-03 18:39:252376 {__f.__state_ = nullptr;}
Howard Hinnant8c6cbb22010-09-22 14:16:262377 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552378 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
Howard Hinnant99be8232010-09-03 18:39:252379 {__rhs.__state_ = nullptr;}
Howard Hinnant73d21a42010-09-04 23:28:192380#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:252381 ~shared_future();
2382 shared_future& operator=(const shared_future& __rhs);
Howard Hinnant73d21a42010-09-04 23:28:192383#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:262384 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552385 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:252386 {
2387 shared_future(std::move(__rhs)).swap(*this);
2388 return *this;
2389 }
Howard Hinnant73d21a42010-09-04 23:28:192390#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:252391
2392 // retrieving the value
Howard Hinnant8c6cbb22010-09-22 14:16:262393 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502394 const _Rp& get() const {return __state_->copy();}
Howard Hinnant99be8232010-09-03 18:39:252395
Howard Hinnant8c6cbb22010-09-22 14:16:262396 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552397 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant99be8232010-09-03 18:39:252398
2399 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:262400 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552401 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant99be8232010-09-03 18:39:252402
Howard Hinnant8c6cbb22010-09-22 14:16:262403 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252404 void wait() const {__state_->wait();}
2405 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:262406 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252407 future_status
2408 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2409 {return __state_->wait_for(__rel_time);}
2410 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:262411 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252412 future_status
2413 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2414 {return __state_->wait_until(__abs_time);}
2415};
2416
Howard Hinnant99968442011-11-29 18:15:502417template <class _Rp>
2418shared_future<_Rp>::~shared_future()
Howard Hinnant99be8232010-09-03 18:39:252419{
2420 if (__state_)
2421 __state_->__release_shared();
2422}
2423
Howard Hinnant99968442011-11-29 18:15:502424template <class _Rp>
2425shared_future<_Rp>&
2426shared_future<_Rp>::operator=(const shared_future& __rhs)
Howard Hinnant99be8232010-09-03 18:39:252427{
2428 if (__rhs.__state_)
2429 __rhs.__state_->__add_shared();
2430 if (__state_)
2431 __state_->__release_shared();
2432 __state_ = __rhs.__state_;
2433 return *this;
2434}
2435
Howard Hinnant99968442011-11-29 18:15:502436template <class _Rp>
Howard Hinnant0f678bd2013-08-12 18:38:342437class _LIBCPP_TYPE_VIS_ONLY shared_future<_Rp&>
Howard Hinnant99be8232010-09-03 18:39:252438{
Howard Hinnant99968442011-11-29 18:15:502439 __assoc_state<_Rp&>* __state_;
Howard Hinnant99be8232010-09-03 18:39:252440
2441public:
Howard Hinnant8c6cbb22010-09-22 14:16:262442 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552443 shared_future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant8c6cbb22010-09-22 14:16:262444 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252445 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2446 {if (__state_) __state_->__add_shared();}
Howard Hinnant73d21a42010-09-04 23:28:192447#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:262448 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552449 shared_future(future<_Rp&>&& __f) _NOEXCEPT : __state_(__f.__state_)
Howard Hinnant99be8232010-09-03 18:39:252450 {__f.__state_ = nullptr;}
Howard Hinnant8c6cbb22010-09-22 14:16:262451 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552452 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
Howard Hinnant99be8232010-09-03 18:39:252453 {__rhs.__state_ = nullptr;}
Howard Hinnant73d21a42010-09-04 23:28:192454#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:252455 ~shared_future();
2456 shared_future& operator=(const shared_future& __rhs);
Howard Hinnant73d21a42010-09-04 23:28:192457#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:262458 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552459 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:252460 {
2461 shared_future(std::move(__rhs)).swap(*this);
2462 return *this;
2463 }
Howard Hinnant73d21a42010-09-04 23:28:192464#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:252465
2466 // retrieving the value
Howard Hinnant8c6cbb22010-09-22 14:16:262467 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502468 _Rp& get() const {return __state_->copy();}
Howard Hinnant99be8232010-09-03 18:39:252469
Howard Hinnant8c6cbb22010-09-22 14:16:262470 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552471 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant99be8232010-09-03 18:39:252472
2473 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:262474 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552475 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant99be8232010-09-03 18:39:252476
Howard Hinnant8c6cbb22010-09-22 14:16:262477 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252478 void wait() const {__state_->wait();}
2479 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:262480 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252481 future_status
2482 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2483 {return __state_->wait_for(__rel_time);}
2484 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:262485 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252486 future_status
2487 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2488 {return __state_->wait_until(__abs_time);}
2489};
2490
Howard Hinnant99968442011-11-29 18:15:502491template <class _Rp>
2492shared_future<_Rp&>::~shared_future()
Howard Hinnant99be8232010-09-03 18:39:252493{
2494 if (__state_)
2495 __state_->__release_shared();
2496}
2497
Howard Hinnant99968442011-11-29 18:15:502498template <class _Rp>
2499shared_future<_Rp&>&
2500shared_future<_Rp&>::operator=(const shared_future& __rhs)
Howard Hinnant99be8232010-09-03 18:39:252501{
2502 if (__rhs.__state_)
2503 __rhs.__state_->__add_shared();
2504 if (__state_)
2505 __state_->__release_shared();
2506 __state_ = __rhs.__state_;
2507 return *this;
2508}
2509
2510template <>
Howard Hinnant83eade62013-03-06 23:30:192511class _LIBCPP_TYPE_VIS shared_future<void>
Howard Hinnant99be8232010-09-03 18:39:252512{
2513 __assoc_sub_state* __state_;
2514
2515public:
Howard Hinnant8c6cbb22010-09-22 14:16:262516 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552517 shared_future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant8c6cbb22010-09-22 14:16:262518 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252519 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2520 {if (__state_) __state_->__add_shared();}
Howard Hinnant73d21a42010-09-04 23:28:192521#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:262522 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552523 shared_future(future<void>&& __f) _NOEXCEPT : __state_(__f.__state_)
Howard Hinnant99be8232010-09-03 18:39:252524 {__f.__state_ = nullptr;}
Howard Hinnant8c6cbb22010-09-22 14:16:262525 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552526 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
Howard Hinnant99be8232010-09-03 18:39:252527 {__rhs.__state_ = nullptr;}
Howard Hinnant73d21a42010-09-04 23:28:192528#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:252529 ~shared_future();
2530 shared_future& operator=(const shared_future& __rhs);
Howard Hinnant73d21a42010-09-04 23:28:192531#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:262532 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552533 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:252534 {
2535 shared_future(std::move(__rhs)).swap(*this);
2536 return *this;
2537 }
Howard Hinnant73d21a42010-09-04 23:28:192538#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:252539
2540 // retrieving the value
Howard Hinnant8c6cbb22010-09-22 14:16:262541 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252542 void get() const {__state_->copy();}
2543
Howard Hinnant8c6cbb22010-09-22 14:16:262544 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552545 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant99be8232010-09-03 18:39:252546
2547 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:262548 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552549 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant99be8232010-09-03 18:39:252550
Howard Hinnant8c6cbb22010-09-22 14:16:262551 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252552 void wait() const {__state_->wait();}
2553 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:262554 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252555 future_status
2556 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2557 {return __state_->wait_for(__rel_time);}
2558 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:262559 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252560 future_status
2561 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2562 {return __state_->wait_until(__abs_time);}
2563};
2564
Howard Hinnant99968442011-11-29 18:15:502565template <class _Rp>
Howard Hinnant99be8232010-09-03 18:39:252566inline _LIBCPP_INLINE_VISIBILITY
2567void
Howard Hinnant8bf01dd2012-07-21 17:46:552568swap(shared_future<_Rp>& __x, shared_future<_Rp>& __y) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:252569{
2570 __x.swap(__y);
2571}
2572
Howard Hinnant99968442011-11-29 18:15:502573template <class _Rp>
Evgeniy Stepanova3b25f82015-11-07 01:22:132574inline
Howard Hinnant99968442011-11-29 18:15:502575shared_future<_Rp>
2576future<_Rp>::share()
Howard Hinnante6e4d012010-09-03 21:46:372577{
Howard Hinnant99968442011-11-29 18:15:502578 return shared_future<_Rp>(_VSTD::move(*this));
Howard Hinnante6e4d012010-09-03 21:46:372579}
2580
Howard Hinnant99968442011-11-29 18:15:502581template <class _Rp>
Evgeniy Stepanova3b25f82015-11-07 01:22:132582inline
Howard Hinnant99968442011-11-29 18:15:502583shared_future<_Rp&>
2584future<_Rp&>::share()
Howard Hinnante6e4d012010-09-03 21:46:372585{
Howard Hinnant99968442011-11-29 18:15:502586 return shared_future<_Rp&>(_VSTD::move(*this));
Howard Hinnant7de47902010-11-30 20:23:322587}
2588
Howard Hinnanta4451512010-12-02 16:45:212589#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2590
Evgeniy Stepanova3b25f82015-11-07 01:22:132591inline
Howard Hinnant7de47902010-11-30 20:23:322592shared_future<void>
2593future<void>::share()
2594{
Howard Hinnant0949eed2011-06-30 21:18:192595 return shared_future<void>(_VSTD::move(*this));
Howard Hinnante6e4d012010-09-03 21:46:372596}
2597
Howard Hinnanta4451512010-12-02 16:45:212598#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2599
Howard Hinnantbc8d3f92010-05-11 19:42:162600_LIBCPP_END_NAMESPACE_STD
2601
Jonathan Roelofs8d86b2e2014-09-05 19:45:052602#endif // !_LIBCPP_HAS_NO_THREADS
2603
Howard Hinnantbc8d3f92010-05-11 19:42:162604#endif // _LIBCPP_FUTURE