blob: ce15eafbf7e4ce4bad26eefd3f96679dff5730ae [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===--------------------------- future -----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_FUTURE
12#define _LIBCPP_FUTURE
13
14/*
15 future synopsis
16
17namespace std
18{
19
20enum class future_errc
21{
Howard Hinnantcd942f12013-09-14 18:20:10 +000022 future_already_retrieved = 1,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000023 promise_already_satisfied,
Howard Hinnantcd942f12013-09-14 18:20:10 +000024 no_state,
25 broken_promise
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000026};
27
28enum class launch
29{
Howard Hinnant66895642010-11-23 18:33:54 +000030 async = 1,
31 deferred = 2,
32 any = async | deferred
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000033};
34
35enum class future_status
36{
37 ready,
38 timeout,
39 deferred
40};
41
42template <> struct is_error_code_enum<future_errc> : public true_type { };
Howard Hinnant8bf01dd2012-07-21 17:46:55 +000043error_code make_error_code(future_errc e) noexcept;
44error_condition make_error_condition(future_errc e) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000045
Howard Hinnant8bf01dd2012-07-21 17:46:55 +000046const error_category& future_category() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000047
48class future_error
49 : public logic_error
50{
51public:
52 future_error(error_code ec); // exposition only
53
Howard Hinnant8bf01dd2012-07-21 17:46:55 +000054 const error_code& code() const noexcept;
55 const char* what() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000056};
57
58template <class R>
59class promise
60{
61public:
62 promise();
63 template <class Allocator>
64 promise(allocator_arg_t, const Allocator& a);
Howard Hinnant8bf01dd2012-07-21 17:46:55 +000065 promise(promise&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000066 promise(const promise& rhs) = delete;
67 ~promise();
68
69 // assignment
Howard Hinnant8bf01dd2012-07-21 17:46:55 +000070 promise& operator=(promise&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000071 promise& operator=(const promise& rhs) = delete;
Howard Hinnant8bf01dd2012-07-21 17:46:55 +000072 void swap(promise& other) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000073
74 // retrieving the result
75 future<R> get_future();
76
77 // setting the result
78 void set_value(const R& r);
79 void set_value(R&& r);
80 void set_exception(exception_ptr p);
81
82 // setting the result with deferred notification
83 void set_value_at_thread_exit(const R& r);
84 void set_value_at_thread_exit(R&& r);
85 void set_exception_at_thread_exit(exception_ptr p);
86};
87
88template <class R>
89class promise<R&>
90{
91public:
92 promise();
93 template <class Allocator>
94 promise(allocator_arg_t, const Allocator& a);
Howard Hinnant8bf01dd2012-07-21 17:46:55 +000095 promise(promise&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000096 promise(const promise& rhs) = delete;
97 ~promise();
98
99 // assignment
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000100 promise& operator=(promise&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000101 promise& operator=(const promise& rhs) = delete;
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000102 void swap(promise& other) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000103
104 // retrieving the result
Howard Hinnant47499b12010-08-27 20:10:19 +0000105 future<R&> get_future();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000106
107 // setting the result
108 void set_value(R& r);
109 void set_exception(exception_ptr p);
110
111 // setting the result with deferred notification
112 void set_value_at_thread_exit(R&);
113 void set_exception_at_thread_exit(exception_ptr p);
114};
115
116template <>
117class promise<void>
118{
119public:
120 promise();
121 template <class Allocator>
122 promise(allocator_arg_t, const Allocator& a);
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000123 promise(promise&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000124 promise(const promise& rhs) = delete;
125 ~promise();
126
127 // assignment
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000128 promise& operator=(promise&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000129 promise& operator=(const promise& rhs) = delete;
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000130 void swap(promise& other) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000131
132 // retrieving the result
Howard Hinnant47499b12010-08-27 20:10:19 +0000133 future<void> get_future();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000134
135 // setting the result
136 void set_value();
137 void set_exception(exception_ptr p);
138
139 // setting the result with deferred notification
140 void set_value_at_thread_exit();
141 void set_exception_at_thread_exit(exception_ptr p);
142};
143
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000144template <class R> void swap(promise<R>& x, promise<R>& y) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000145
146template <class R, class Alloc>
147 struct uses_allocator<promise<R>, Alloc> : public true_type {};
148
149template <class R>
150class future
151{
152public:
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000153 future() noexcept;
154 future(future&&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000155 future(const future& rhs) = delete;
156 ~future();
157 future& operator=(const future& rhs) = delete;
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000158 future& operator=(future&&) noexcept;
159 shared_future<R> share();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000160
161 // retrieving the value
162 R get();
163
164 // functions to check state
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000165 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000166
167 void wait() const;
168 template <class Rep, class Period>
169 future_status
170 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
171 template <class Clock, class Duration>
172 future_status
173 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
174};
175
176template <class R>
177class future<R&>
178{
179public:
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000180 future() noexcept;
181 future(future&&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000182 future(const future& rhs) = delete;
183 ~future();
184 future& operator=(const future& rhs) = delete;
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000185 future& operator=(future&&) noexcept;
186 shared_future<R&> share();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000187
188 // retrieving the value
189 R& get();
190
191 // functions to check state
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000192 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000193
194 void wait() const;
195 template <class Rep, class Period>
196 future_status
197 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
198 template <class Clock, class Duration>
199 future_status
200 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
201};
202
203template <>
204class future<void>
205{
206public:
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000207 future() noexcept;
208 future(future&&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000209 future(const future& rhs) = delete;
210 ~future();
211 future& operator=(const future& rhs) = delete;
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000212 future& operator=(future&&) noexcept;
213 shared_future<void> share();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000214
215 // retrieving the value
216 void get();
217
218 // functions to check state
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000219 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000220
221 void wait() const;
222 template <class Rep, class Period>
223 future_status
224 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
225 template <class Clock, class Duration>
226 future_status
227 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
228};
229
230template <class R>
231class shared_future
232{
233public:
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000234 shared_future() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000235 shared_future(const shared_future& rhs);
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000236 shared_future(future<R>&&) noexcept;
237 shared_future(shared_future&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000238 ~shared_future();
239 shared_future& operator=(const shared_future& rhs);
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000240 shared_future& operator=(shared_future&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000241
242 // retrieving the value
243 const R& get() const;
244
245 // functions to check state
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000246 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000247
248 void wait() const;
249 template <class Rep, class Period>
250 future_status
251 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
252 template <class Clock, class Duration>
253 future_status
254 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
255};
256
257template <class R>
258class shared_future<R&>
259{
260public:
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000261 shared_future() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000262 shared_future(const shared_future& rhs);
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000263 shared_future(future<R&>&&) noexcept;
264 shared_future(shared_future&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000265 ~shared_future();
266 shared_future& operator=(const shared_future& rhs);
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000267 shared_future& operator=(shared_future&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000268
269 // retrieving the value
270 R& get() const;
271
272 // functions to check state
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000273 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000274
275 void wait() const;
276 template <class Rep, class Period>
277 future_status
278 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
279 template <class Clock, class Duration>
280 future_status
281 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
282};
283
284template <>
285class shared_future<void>
286{
287public:
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000288 shared_future() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000289 shared_future(const shared_future& rhs);
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000290 shared_future(future<void>&&) noexcept;
291 shared_future(shared_future&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000292 ~shared_future();
293 shared_future& operator=(const shared_future& rhs);
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000294 shared_future& operator=(shared_future&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000295
296 // retrieving the value
297 void get() const;
298
299 // functions to check state
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000300 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000301
302 void wait() const;
303 template <class Rep, class Period>
304 future_status
305 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
306 template <class Clock, class Duration>
307 future_status
308 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
309};
310
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000311template <class F, class... Args>
Howard Hinnant0836f872013-09-21 18:17:23 +0000312 future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000313 async(F&& f, Args&&... args);
314
315template <class F, class... Args>
Howard Hinnant0836f872013-09-21 18:17:23 +0000316 future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000317 async(launch policy, F&& f, Args&&... args);
318
Howard Hinnantf5256e12010-05-11 21:36:01 +0000319template <class> class packaged_task; // undefined
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000320
321template <class R, class... ArgTypes>
322class packaged_task<R(ArgTypes...)>
323{
324public:
325 typedef R result_type;
326
327 // construction and destruction
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000328 packaged_task() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000329 template <class F>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000330 explicit packaged_task(F&& f);
331 template <class F, class Allocator>
Marshall Clow07546f32015-06-30 14:16:49 +0000332 packaged_task(allocator_arg_t, const Allocator& a, F&& f);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000333 ~packaged_task();
334
335 // no copy
Howard Hinnant8131a012012-07-21 19:34:12 +0000336 packaged_task(const packaged_task&) = delete;
337 packaged_task& operator=(const packaged_task&) = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000338
339 // move support
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000340 packaged_task(packaged_task&& other) noexcept;
341 packaged_task& operator=(packaged_task&& other) noexcept;
342 void swap(packaged_task& other) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000343
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000344 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000345
346 // result retrieval
347 future<R> get_future();
348
349 // execution
350 void operator()(ArgTypes... );
351 void make_ready_at_thread_exit(ArgTypes...);
352
353 void reset();
354};
355
356template <class R>
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000357 void swap(packaged_task<R(ArgTypes...)&, packaged_task<R(ArgTypes...)>&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000358
359template <class R, class Alloc> struct uses_allocator<packaged_task<R>, Alloc>;
360
361} // std
362
363*/
364
365#include <__config>
366#include <system_error>
Howard Hinnant47499b12010-08-27 20:10:19 +0000367#include <memory>
368#include <chrono>
369#include <exception>
Howard Hinnante6e4d012010-09-03 21:46:37 +0000370#include <mutex>
Howard Hinnant47499b12010-08-27 20:10:19 +0000371#include <thread>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000372
Howard Hinnant08e17472011-10-17 20:05:10 +0000373#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000374#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000375#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000376
Jonathan Roelofsbaed05d2014-09-05 20:28:44 +0000377#ifdef _LIBCPP_HAS_NO_THREADS
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +0000378#error <future> is not supported on this single threaded system
379#else // !_LIBCPP_HAS_NO_THREADS
380
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000381_LIBCPP_BEGIN_NAMESPACE_STD
382
383//enum class future_errc
Howard Hinnantf6d875f2011-12-02 19:36:40 +0000384_LIBCPP_DECLARE_STRONG_ENUM(future_errc)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000385{
Howard Hinnantcd942f12013-09-14 18:20:10 +0000386 future_already_retrieved = 1,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000387 promise_already_satisfied,
Howard Hinnantcd942f12013-09-14 18:20:10 +0000388 no_state,
389 broken_promise
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000390};
Howard Hinnantf6d875f2011-12-02 19:36:40 +0000391_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_errc)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000392
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000393template <>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000394struct _LIBCPP_TYPE_VIS_ONLY is_error_code_enum<future_errc> : public true_type {};
Howard Hinnanta6521722010-08-25 17:32:05 +0000395
Howard Hinnantf6d875f2011-12-02 19:36:40 +0000396#ifdef _LIBCPP_HAS_NO_STRONG_ENUMS
397template <>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000398struct _LIBCPP_TYPE_VIS_ONLY is_error_code_enum<future_errc::__lx> : public true_type { };
Howard Hinnantf6d875f2011-12-02 19:36:40 +0000399#endif
400
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000401//enum class launch
Howard Hinnantf6d875f2011-12-02 19:36:40 +0000402_LIBCPP_DECLARE_STRONG_ENUM(launch)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000403{
Howard Hinnant66895642010-11-23 18:33:54 +0000404 async = 1,
405 deferred = 2,
406 any = async | deferred
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000407};
Howard Hinnantf6d875f2011-12-02 19:36:40 +0000408_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(launch)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000409
Howard Hinnantf491e512013-06-29 18:38:17 +0000410#ifndef _LIBCPP_HAS_NO_STRONG_ENUMS
411
412#ifdef _LIBCXX_UNDERLYING_TYPE
413typedef underlying_type<launch>::type __launch_underlying_type;
414#else
415typedef int __launch_underlying_type;
416#endif
417
418inline _LIBCPP_INLINE_VISIBILITY
419_LIBCPP_CONSTEXPR
420launch
421operator&(launch __x, launch __y)
422{
423 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) &
424 static_cast<__launch_underlying_type>(__y));
425}
426
427inline _LIBCPP_INLINE_VISIBILITY
428_LIBCPP_CONSTEXPR
429launch
430operator|(launch __x, launch __y)
431{
432 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) |
433 static_cast<__launch_underlying_type>(__y));
434}
435
436inline _LIBCPP_INLINE_VISIBILITY
437_LIBCPP_CONSTEXPR
438launch
439operator^(launch __x, launch __y)
440{
441 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) ^
442 static_cast<__launch_underlying_type>(__y));
443}
444
445inline _LIBCPP_INLINE_VISIBILITY
446_LIBCPP_CONSTEXPR
447launch
448operator~(launch __x)
449{
Howard Hinnant6a683bf2013-07-02 18:01:41 +0000450 return static_cast<launch>(~static_cast<__launch_underlying_type>(__x) & 3);
Howard Hinnantf491e512013-06-29 18:38:17 +0000451}
452
453inline _LIBCPP_INLINE_VISIBILITY
454launch&
455operator&=(launch& __x, launch __y)
456{
457 __x = __x & __y; return __x;
458}
459
460inline _LIBCPP_INLINE_VISIBILITY
461launch&
462operator|=(launch& __x, launch __y)
463{
464 __x = __x | __y; return __x;
465}
466
467inline _LIBCPP_INLINE_VISIBILITY
468launch&
469operator^=(launch& __x, launch __y)
470{
471 __x = __x ^ __y; return __x;
472}
473
474#endif // !_LIBCPP_HAS_NO_STRONG_ENUMS
475
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000476//enum class future_status
Howard Hinnantf6d875f2011-12-02 19:36:40 +0000477_LIBCPP_DECLARE_STRONG_ENUM(future_status)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000478{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000479 ready,
480 timeout,
481 deferred
482};
Howard Hinnantf6d875f2011-12-02 19:36:40 +0000483_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_status)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000484
Howard Hinnant83eade62013-03-06 23:30:19 +0000485_LIBCPP_FUNC_VIS
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000486const error_category& future_category() _NOEXCEPT;
Howard Hinnanta6521722010-08-25 17:32:05 +0000487
488inline _LIBCPP_INLINE_VISIBILITY
489error_code
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000490make_error_code(future_errc __e) _NOEXCEPT
Howard Hinnanta6521722010-08-25 17:32:05 +0000491{
492 return error_code(static_cast<int>(__e), future_category());
493}
494
495inline _LIBCPP_INLINE_VISIBILITY
496error_condition
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000497make_error_condition(future_errc __e) _NOEXCEPT
Howard Hinnanta6521722010-08-25 17:32:05 +0000498{
499 return error_condition(static_cast<int>(__e), future_category());
500}
501
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000502class _LIBCPP_EXCEPTION_ABI future_error
Howard Hinnanta6521722010-08-25 17:32:05 +0000503 : public logic_error
504{
505 error_code __ec_;
506public:
507 future_error(error_code __ec);
508
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000509 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000510 const error_code& code() const _NOEXCEPT {return __ec_;}
Howard Hinnantac6de542011-07-07 21:03:52 +0000511
512 virtual ~future_error() _NOEXCEPT;
Howard Hinnanta6521722010-08-25 17:32:05 +0000513};
514
Eric Fiselier423ca202015-10-02 21:25:15 +0000515inline _LIBCPP_ALWAYS_INLINE
516void __throw_future_error(future_errc _Ev)
Marshall Clowa1899742015-09-03 15:11:32 +0000517{
518#ifndef _LIBCPP_NO_EXCEPTIONS
519 throw future_error(make_error_code(_Ev));
520#else
521 assert(!"future_error");
522#endif
523}
524
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000525class _LIBCPP_TYPE_VIS __assoc_sub_state
Howard Hinnant47499b12010-08-27 20:10:19 +0000526 : public __shared_count
527{
528protected:
529 exception_ptr __exception_;
530 mutable mutex __mut_;
531 mutable condition_variable __cv_;
532 unsigned __state_;
533
Howard Hinnant1694d232011-05-28 14:41:13 +0000534 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant54da3382010-08-30 18:46:21 +0000535 void __sub_wait(unique_lock<mutex>& __lk);
Howard Hinnant47499b12010-08-27 20:10:19 +0000536public:
537 enum
538 {
539 __constructed = 1,
540 __future_attached = 2,
541 ready = 4,
542 deferred = 8
543 };
544
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000545 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000546 __assoc_sub_state() : __state_(0) {}
547
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000548 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000549 bool __has_value() const
550 {return (__state_ & __constructed) || (__exception_ != nullptr);}
551
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000552 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1b031c92013-01-14 20:01:24 +0000553 void __set_future_attached()
554 {
555 lock_guard<mutex> __lk(__mut_);
556 __state_ |= __future_attached;
557 }
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000558 _LIBCPP_INLINE_VISIBILITY
Marshall Clow9de3d4c2013-10-13 01:02:45 +0000559 bool __has_future_attached() const {return (__state_ & __future_attached) != 0;}
Howard Hinnant47499b12010-08-27 20:10:19 +0000560
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +0000562 void __set_deferred() {__state_ |= deferred;}
563
Howard Hinnant47499b12010-08-27 20:10:19 +0000564 void __make_ready();
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000565 _LIBCPP_INLINE_VISIBILITY
Marshall Clow9de3d4c2013-10-13 01:02:45 +0000566 bool __is_ready() const {return (__state_ & ready) != 0;}
Howard Hinnant47499b12010-08-27 20:10:19 +0000567
568 void set_value();
569 void set_value_at_thread_exit();
570
571 void set_exception(exception_ptr __p);
572 void set_exception_at_thread_exit(exception_ptr __p);
573
574 void copy();
575
Howard Hinnant54da3382010-08-30 18:46:21 +0000576 void wait();
Howard Hinnant47499b12010-08-27 20:10:19 +0000577 template <class _Rep, class _Period>
578 future_status
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000579 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000580 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const;
581 template <class _Clock, class _Duration>
582 future_status
583 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const;
Howard Hinnant54da3382010-08-30 18:46:21 +0000584
585 virtual void __execute();
Howard Hinnant47499b12010-08-27 20:10:19 +0000586};
587
Howard Hinnantf39daa82010-08-28 21:01:06 +0000588template <class _Clock, class _Duration>
589future_status
590__assoc_sub_state::wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
591{
592 unique_lock<mutex> __lk(__mut_);
Howard Hinnant54da3382010-08-30 18:46:21 +0000593 if (__state_ & deferred)
594 return future_status::deferred;
595 while (!(__state_ & ready) && _Clock::now() < __abs_time)
Howard Hinnantf39daa82010-08-28 21:01:06 +0000596 __cv_.wait_until(__lk, __abs_time);
597 if (__state_ & ready)
598 return future_status::ready;
Howard Hinnantf39daa82010-08-28 21:01:06 +0000599 return future_status::timeout;
600}
601
602template <class _Rep, class _Period>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000603inline
Howard Hinnantf39daa82010-08-28 21:01:06 +0000604future_status
605__assoc_sub_state::wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
606{
Howard Hinnantf8f85212010-11-20 19:16:30 +0000607 return wait_until(chrono::steady_clock::now() + __rel_time);
Howard Hinnantf39daa82010-08-28 21:01:06 +0000608}
609
Howard Hinnant99968442011-11-29 18:15:50 +0000610template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +0000611class __assoc_state
612 : public __assoc_sub_state
613{
614 typedef __assoc_sub_state base;
Howard Hinnant99968442011-11-29 18:15:50 +0000615 typedef typename aligned_storage<sizeof(_Rp), alignment_of<_Rp>::value>::type _Up;
Howard Hinnant47499b12010-08-27 20:10:19 +0000616protected:
Howard Hinnant99968442011-11-29 18:15:50 +0000617 _Up __value_;
Howard Hinnant47499b12010-08-27 20:10:19 +0000618
Howard Hinnant1694d232011-05-28 14:41:13 +0000619 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant47499b12010-08-27 20:10:19 +0000620public:
621
622 template <class _Arg>
Howard Hinnant73d21a42010-09-04 23:28:19 +0000623#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +0000624 void set_value(_Arg&& __arg);
625#else
626 void set_value(_Arg& __arg);
627#endif
628
629 template <class _Arg>
Howard Hinnant73d21a42010-09-04 23:28:19 +0000630#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +0000631 void set_value_at_thread_exit(_Arg&& __arg);
632#else
633 void set_value_at_thread_exit(_Arg& __arg);
634#endif
635
Howard Hinnant99968442011-11-29 18:15:50 +0000636 _Rp move();
637 typename add_lvalue_reference<_Rp>::type copy();
Howard Hinnant47499b12010-08-27 20:10:19 +0000638};
639
Howard Hinnant99968442011-11-29 18:15:50 +0000640template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +0000641void
Howard Hinnant99968442011-11-29 18:15:50 +0000642__assoc_state<_Rp>::__on_zero_shared() _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +0000643{
644 if (this->__state_ & base::__constructed)
Howard Hinnant99968442011-11-29 18:15:50 +0000645 reinterpret_cast<_Rp*>(&__value_)->~_Rp();
Howard Hinnant47499b12010-08-27 20:10:19 +0000646 delete this;
647}
648
Howard Hinnant99968442011-11-29 18:15:50 +0000649template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +0000650template <class _Arg>
651void
Howard Hinnant73d21a42010-09-04 23:28:19 +0000652#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +0000653__assoc_state<_Rp>::set_value(_Arg&& __arg)
Howard Hinnant47499b12010-08-27 20:10:19 +0000654#else
Howard Hinnant99968442011-11-29 18:15:50 +0000655__assoc_state<_Rp>::set_value(_Arg& __arg)
Howard Hinnant47499b12010-08-27 20:10:19 +0000656#endif
657{
658 unique_lock<mutex> __lk(this->__mut_);
659 if (this->__has_value())
Eric Fiselier423ca202015-10-02 21:25:15 +0000660 __throw_future_error(future_errc::promise_already_satisfied);
Howard Hinnant99968442011-11-29 18:15:50 +0000661 ::new(&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
Howard Hinnant47499b12010-08-27 20:10:19 +0000662 this->__state_ |= base::__constructed | base::ready;
Howard Hinnant47499b12010-08-27 20:10:19 +0000663 __cv_.notify_all();
664}
665
Howard Hinnant99968442011-11-29 18:15:50 +0000666template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +0000667template <class _Arg>
668void
Howard Hinnant73d21a42010-09-04 23:28:19 +0000669#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +0000670__assoc_state<_Rp>::set_value_at_thread_exit(_Arg&& __arg)
Howard Hinnant47499b12010-08-27 20:10:19 +0000671#else
Howard Hinnant99968442011-11-29 18:15:50 +0000672__assoc_state<_Rp>::set_value_at_thread_exit(_Arg& __arg)
Howard Hinnant47499b12010-08-27 20:10:19 +0000673#endif
674{
675 unique_lock<mutex> __lk(this->__mut_);
676 if (this->__has_value())
Eric Fiselier423ca202015-10-02 21:25:15 +0000677 __throw_future_error(future_errc::promise_already_satisfied);
Howard Hinnant99968442011-11-29 18:15:50 +0000678 ::new(&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
Howard Hinnant47499b12010-08-27 20:10:19 +0000679 this->__state_ |= base::__constructed;
Howard Hinnant5306d682010-10-14 19:18:04 +0000680 __thread_local_data()->__make_ready_at_thread_exit(this);
Howard Hinnant47499b12010-08-27 20:10:19 +0000681}
682
Howard Hinnant99968442011-11-29 18:15:50 +0000683template <class _Rp>
684_Rp
685__assoc_state<_Rp>::move()
Howard Hinnant47499b12010-08-27 20:10:19 +0000686{
687 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant54da3382010-08-30 18:46:21 +0000688 this->__sub_wait(__lk);
Howard Hinnant47499b12010-08-27 20:10:19 +0000689 if (this->__exception_ != nullptr)
690 rethrow_exception(this->__exception_);
Howard Hinnant99968442011-11-29 18:15:50 +0000691 return _VSTD::move(*reinterpret_cast<_Rp*>(&__value_));
Howard Hinnant47499b12010-08-27 20:10:19 +0000692}
693
Howard Hinnant99968442011-11-29 18:15:50 +0000694template <class _Rp>
695typename add_lvalue_reference<_Rp>::type
696__assoc_state<_Rp>::copy()
Howard Hinnant47499b12010-08-27 20:10:19 +0000697{
698 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant54da3382010-08-30 18:46:21 +0000699 this->__sub_wait(__lk);
Howard Hinnant47499b12010-08-27 20:10:19 +0000700 if (this->__exception_ != nullptr)
701 rethrow_exception(this->__exception_);
Howard Hinnant99968442011-11-29 18:15:50 +0000702 return *reinterpret_cast<_Rp*>(&__value_);
Howard Hinnant47499b12010-08-27 20:10:19 +0000703}
704
Howard Hinnant99968442011-11-29 18:15:50 +0000705template <class _Rp>
706class __assoc_state<_Rp&>
Howard Hinnantf39daa82010-08-28 21:01:06 +0000707 : public __assoc_sub_state
708{
709 typedef __assoc_sub_state base;
Howard Hinnant99968442011-11-29 18:15:50 +0000710 typedef _Rp* _Up;
Howard Hinnantf39daa82010-08-28 21:01:06 +0000711protected:
Howard Hinnant99968442011-11-29 18:15:50 +0000712 _Up __value_;
Howard Hinnantf39daa82010-08-28 21:01:06 +0000713
Howard Hinnant1694d232011-05-28 14:41:13 +0000714 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnantf39daa82010-08-28 21:01:06 +0000715public:
716
Howard Hinnant99968442011-11-29 18:15:50 +0000717 void set_value(_Rp& __arg);
718 void set_value_at_thread_exit(_Rp& __arg);
Howard Hinnantf39daa82010-08-28 21:01:06 +0000719
Howard Hinnant99968442011-11-29 18:15:50 +0000720 _Rp& copy();
Howard Hinnantf39daa82010-08-28 21:01:06 +0000721};
722
Howard Hinnant99968442011-11-29 18:15:50 +0000723template <class _Rp>
Howard Hinnantf39daa82010-08-28 21:01:06 +0000724void
Howard Hinnant99968442011-11-29 18:15:50 +0000725__assoc_state<_Rp&>::__on_zero_shared() _NOEXCEPT
Howard Hinnantf39daa82010-08-28 21:01:06 +0000726{
727 delete this;
728}
729
Howard Hinnant99968442011-11-29 18:15:50 +0000730template <class _Rp>
Howard Hinnantf39daa82010-08-28 21:01:06 +0000731void
Howard Hinnant99968442011-11-29 18:15:50 +0000732__assoc_state<_Rp&>::set_value(_Rp& __arg)
Howard Hinnantf39daa82010-08-28 21:01:06 +0000733{
734 unique_lock<mutex> __lk(this->__mut_);
735 if (this->__has_value())
Eric Fiselier423ca202015-10-02 21:25:15 +0000736 __throw_future_error(future_errc::promise_already_satisfied);
Howard Hinnanta4e87ab2013-08-08 18:38:55 +0000737 __value_ = _VSTD::addressof(__arg);
Howard Hinnantf39daa82010-08-28 21:01:06 +0000738 this->__state_ |= base::__constructed | base::ready;
Howard Hinnantf39daa82010-08-28 21:01:06 +0000739 __cv_.notify_all();
740}
741
Howard Hinnant99968442011-11-29 18:15:50 +0000742template <class _Rp>
Howard Hinnantf39daa82010-08-28 21:01:06 +0000743void
Howard Hinnant99968442011-11-29 18:15:50 +0000744__assoc_state<_Rp&>::set_value_at_thread_exit(_Rp& __arg)
Howard Hinnantf39daa82010-08-28 21:01:06 +0000745{
746 unique_lock<mutex> __lk(this->__mut_);
747 if (this->__has_value())
Eric Fiselier423ca202015-10-02 21:25:15 +0000748 __throw_future_error(future_errc::promise_already_satisfied);
Howard Hinnanta4e87ab2013-08-08 18:38:55 +0000749 __value_ = _VSTD::addressof(__arg);
Howard Hinnantf39daa82010-08-28 21:01:06 +0000750 this->__state_ |= base::__constructed;
Howard Hinnant5306d682010-10-14 19:18:04 +0000751 __thread_local_data()->__make_ready_at_thread_exit(this);
Howard Hinnantf39daa82010-08-28 21:01:06 +0000752}
753
Howard Hinnant99968442011-11-29 18:15:50 +0000754template <class _Rp>
755_Rp&
756__assoc_state<_Rp&>::copy()
Howard Hinnantf39daa82010-08-28 21:01:06 +0000757{
758 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant54da3382010-08-30 18:46:21 +0000759 this->__sub_wait(__lk);
Howard Hinnantf39daa82010-08-28 21:01:06 +0000760 if (this->__exception_ != nullptr)
761 rethrow_exception(this->__exception_);
762 return *__value_;
763}
764
Howard Hinnant99968442011-11-29 18:15:50 +0000765template <class _Rp, class _Alloc>
Howard Hinnant47499b12010-08-27 20:10:19 +0000766class __assoc_state_alloc
Howard Hinnant99968442011-11-29 18:15:50 +0000767 : public __assoc_state<_Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +0000768{
Howard Hinnant99968442011-11-29 18:15:50 +0000769 typedef __assoc_state<_Rp> base;
Howard Hinnant47499b12010-08-27 20:10:19 +0000770 _Alloc __alloc_;
771
Howard Hinnant1694d232011-05-28 14:41:13 +0000772 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant47499b12010-08-27 20:10:19 +0000773public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000774 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000775 explicit __assoc_state_alloc(const _Alloc& __a)
776 : __alloc_(__a) {}
777};
778
Howard Hinnant99968442011-11-29 18:15:50 +0000779template <class _Rp, class _Alloc>
Howard Hinnant47499b12010-08-27 20:10:19 +0000780void
Howard Hinnant99968442011-11-29 18:15:50 +0000781__assoc_state_alloc<_Rp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +0000782{
783 if (this->__state_ & base::__constructed)
Howard Hinnanta4e87ab2013-08-08 18:38:55 +0000784 reinterpret_cast<_Rp*>(_VSTD::addressof(this->__value_))->~_Rp();
Eric Fiselier8492cd82015-02-05 23:01:40 +0000785 typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
786 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4d2413c2014-10-23 06:24:45 +0000787 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselier8492cd82015-02-05 23:01:40 +0000788 _Al __a(__alloc_);
Howard Hinnant47499b12010-08-27 20:10:19 +0000789 this->~__assoc_state_alloc();
Eric Fiselier4d2413c2014-10-23 06:24:45 +0000790 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnant47499b12010-08-27 20:10:19 +0000791}
792
Howard Hinnant99968442011-11-29 18:15:50 +0000793template <class _Rp, class _Alloc>
794class __assoc_state_alloc<_Rp&, _Alloc>
795 : public __assoc_state<_Rp&>
Howard Hinnantf39daa82010-08-28 21:01:06 +0000796{
Howard Hinnant99968442011-11-29 18:15:50 +0000797 typedef __assoc_state<_Rp&> base;
Howard Hinnantf39daa82010-08-28 21:01:06 +0000798 _Alloc __alloc_;
799
Howard Hinnant1694d232011-05-28 14:41:13 +0000800 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnantf39daa82010-08-28 21:01:06 +0000801public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000802 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39daa82010-08-28 21:01:06 +0000803 explicit __assoc_state_alloc(const _Alloc& __a)
804 : __alloc_(__a) {}
805};
806
Howard Hinnant99968442011-11-29 18:15:50 +0000807template <class _Rp, class _Alloc>
Howard Hinnantf39daa82010-08-28 21:01:06 +0000808void
Howard Hinnant99968442011-11-29 18:15:50 +0000809__assoc_state_alloc<_Rp&, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantf39daa82010-08-28 21:01:06 +0000810{
Eric Fiselier8492cd82015-02-05 23:01:40 +0000811 typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
812 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4d2413c2014-10-23 06:24:45 +0000813 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselier8492cd82015-02-05 23:01:40 +0000814 _Al __a(__alloc_);
Howard Hinnantf39daa82010-08-28 21:01:06 +0000815 this->~__assoc_state_alloc();
Eric Fiselier4d2413c2014-10-23 06:24:45 +0000816 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantf39daa82010-08-28 21:01:06 +0000817}
818
Howard Hinnant47499b12010-08-27 20:10:19 +0000819template <class _Alloc>
820class __assoc_sub_state_alloc
821 : public __assoc_sub_state
822{
823 typedef __assoc_sub_state base;
824 _Alloc __alloc_;
825
Howard Hinnant1694d232011-05-28 14:41:13 +0000826 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant47499b12010-08-27 20:10:19 +0000827public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000828 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000829 explicit __assoc_sub_state_alloc(const _Alloc& __a)
830 : __alloc_(__a) {}
831};
832
833template <class _Alloc>
834void
Howard Hinnant1694d232011-05-28 14:41:13 +0000835__assoc_sub_state_alloc<_Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +0000836{
Eric Fiselier8492cd82015-02-05 23:01:40 +0000837 typedef typename __allocator_traits_rebind<_Alloc, __assoc_sub_state_alloc>::type _Al;
838 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4d2413c2014-10-23 06:24:45 +0000839 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselier8492cd82015-02-05 23:01:40 +0000840 _Al __a(__alloc_);
Howard Hinnant47499b12010-08-27 20:10:19 +0000841 this->~__assoc_sub_state_alloc();
Eric Fiselier4d2413c2014-10-23 06:24:45 +0000842 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnant47499b12010-08-27 20:10:19 +0000843}
844
Howard Hinnant99968442011-11-29 18:15:50 +0000845template <class _Rp, class _Fp>
Howard Hinnant54da3382010-08-30 18:46:21 +0000846class __deferred_assoc_state
Howard Hinnant99968442011-11-29 18:15:50 +0000847 : public __assoc_state<_Rp>
Howard Hinnant54da3382010-08-30 18:46:21 +0000848{
Howard Hinnant99968442011-11-29 18:15:50 +0000849 typedef __assoc_state<_Rp> base;
Howard Hinnant54da3382010-08-30 18:46:21 +0000850
Howard Hinnant99968442011-11-29 18:15:50 +0000851 _Fp __func_;
Howard Hinnant54da3382010-08-30 18:46:21 +0000852
853public:
Howard Hinnant73d21a42010-09-04 23:28:19 +0000854#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000855 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000856 explicit __deferred_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:21 +0000857#endif
858
859 virtual void __execute();
860};
861
Howard Hinnant73d21a42010-09-04 23:28:19 +0000862#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21 +0000863
Howard Hinnant99968442011-11-29 18:15:50 +0000864template <class _Rp, class _Fp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000865inline
Howard Hinnant99968442011-11-29 18:15:50 +0000866__deferred_assoc_state<_Rp, _Fp>::__deferred_assoc_state(_Fp&& __f)
867 : __func_(_VSTD::forward<_Fp>(__f))
Howard Hinnant54da3382010-08-30 18:46:21 +0000868{
869 this->__set_deferred();
870}
871
Howard Hinnant73d21a42010-09-04 23:28:19 +0000872#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21 +0000873
Howard Hinnant99968442011-11-29 18:15:50 +0000874template <class _Rp, class _Fp>
Howard Hinnant54da3382010-08-30 18:46:21 +0000875void
Howard Hinnant99968442011-11-29 18:15:50 +0000876__deferred_assoc_state<_Rp, _Fp>::__execute()
Howard Hinnant54da3382010-08-30 18:46:21 +0000877{
878#ifndef _LIBCPP_NO_EXCEPTIONS
879 try
880 {
881#endif // _LIBCPP_NO_EXCEPTIONS
882 this->set_value(__func_());
883#ifndef _LIBCPP_NO_EXCEPTIONS
884 }
885 catch (...)
886 {
887 this->set_exception(current_exception());
888 }
889#endif // _LIBCPP_NO_EXCEPTIONS
890}
891
Howard Hinnant99968442011-11-29 18:15:50 +0000892template <class _Fp>
893class __deferred_assoc_state<void, _Fp>
Howard Hinnant54da3382010-08-30 18:46:21 +0000894 : public __assoc_sub_state
895{
896 typedef __assoc_sub_state base;
897
Howard Hinnant99968442011-11-29 18:15:50 +0000898 _Fp __func_;
Howard Hinnant54da3382010-08-30 18:46:21 +0000899
900public:
Howard Hinnant73d21a42010-09-04 23:28:19 +0000901#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000902 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000903 explicit __deferred_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:21 +0000904#endif
905
906 virtual void __execute();
907};
908
Howard Hinnant73d21a42010-09-04 23:28:19 +0000909#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21 +0000910
Howard Hinnant99968442011-11-29 18:15:50 +0000911template <class _Fp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000912inline
Howard Hinnant99968442011-11-29 18:15:50 +0000913__deferred_assoc_state<void, _Fp>::__deferred_assoc_state(_Fp&& __f)
914 : __func_(_VSTD::forward<_Fp>(__f))
Howard Hinnant54da3382010-08-30 18:46:21 +0000915{
916 this->__set_deferred();
917}
918
Howard Hinnant73d21a42010-09-04 23:28:19 +0000919#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21 +0000920
Howard Hinnant99968442011-11-29 18:15:50 +0000921template <class _Fp>
Howard Hinnant54da3382010-08-30 18:46:21 +0000922void
Howard Hinnant99968442011-11-29 18:15:50 +0000923__deferred_assoc_state<void, _Fp>::__execute()
Howard Hinnant54da3382010-08-30 18:46:21 +0000924{
925#ifndef _LIBCPP_NO_EXCEPTIONS
926 try
927 {
928#endif // _LIBCPP_NO_EXCEPTIONS
929 __func_();
930 this->set_value();
931#ifndef _LIBCPP_NO_EXCEPTIONS
932 }
933 catch (...)
934 {
935 this->set_exception(current_exception());
936 }
937#endif // _LIBCPP_NO_EXCEPTIONS
938}
939
Howard Hinnant99968442011-11-29 18:15:50 +0000940template <class _Rp, class _Fp>
Howard Hinnant57cff292011-05-19 15:05:04 +0000941class __async_assoc_state
Howard Hinnant99968442011-11-29 18:15:50 +0000942 : public __assoc_state<_Rp>
Howard Hinnant57cff292011-05-19 15:05:04 +0000943{
Howard Hinnant99968442011-11-29 18:15:50 +0000944 typedef __assoc_state<_Rp> base;
Howard Hinnant57cff292011-05-19 15:05:04 +0000945
Howard Hinnant99968442011-11-29 18:15:50 +0000946 _Fp __func_;
Howard Hinnant57cff292011-05-19 15:05:04 +0000947
Howard Hinnant1694d232011-05-28 14:41:13 +0000948 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant57cff292011-05-19 15:05:04 +0000949public:
950#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000951 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000952 explicit __async_assoc_state(_Fp&& __f);
Howard Hinnant57cff292011-05-19 15:05:04 +0000953#endif
954
955 virtual void __execute();
956};
957
958#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
959
Howard Hinnant99968442011-11-29 18:15:50 +0000960template <class _Rp, class _Fp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000961inline
Howard Hinnant99968442011-11-29 18:15:50 +0000962__async_assoc_state<_Rp, _Fp>::__async_assoc_state(_Fp&& __f)
963 : __func_(_VSTD::forward<_Fp>(__f))
Howard Hinnant57cff292011-05-19 15:05:04 +0000964{
965}
966
967#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
968
Howard Hinnant99968442011-11-29 18:15:50 +0000969template <class _Rp, class _Fp>
Howard Hinnant57cff292011-05-19 15:05:04 +0000970void
Howard Hinnant99968442011-11-29 18:15:50 +0000971__async_assoc_state<_Rp, _Fp>::__execute()
Howard Hinnant57cff292011-05-19 15:05:04 +0000972{
973#ifndef _LIBCPP_NO_EXCEPTIONS
974 try
975 {
976#endif // _LIBCPP_NO_EXCEPTIONS
977 this->set_value(__func_());
978#ifndef _LIBCPP_NO_EXCEPTIONS
979 }
980 catch (...)
981 {
982 this->set_exception(current_exception());
983 }
984#endif // _LIBCPP_NO_EXCEPTIONS
985}
986
Howard Hinnant99968442011-11-29 18:15:50 +0000987template <class _Rp, class _Fp>
Howard Hinnant57cff292011-05-19 15:05:04 +0000988void
Howard Hinnant99968442011-11-29 18:15:50 +0000989__async_assoc_state<_Rp, _Fp>::__on_zero_shared() _NOEXCEPT
Howard Hinnant57cff292011-05-19 15:05:04 +0000990{
991 this->wait();
992 base::__on_zero_shared();
993}
994
Howard Hinnant99968442011-11-29 18:15:50 +0000995template <class _Fp>
996class __async_assoc_state<void, _Fp>
Howard Hinnant57cff292011-05-19 15:05:04 +0000997 : public __assoc_sub_state
998{
999 typedef __assoc_sub_state base;
1000
Howard Hinnant99968442011-11-29 18:15:50 +00001001 _Fp __func_;
Howard Hinnant57cff292011-05-19 15:05:04 +00001002
Howard Hinnant1694d232011-05-28 14:41:13 +00001003 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant57cff292011-05-19 15:05:04 +00001004public:
1005#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001006 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001007 explicit __async_assoc_state(_Fp&& __f);
Howard Hinnant57cff292011-05-19 15:05:04 +00001008#endif
1009
1010 virtual void __execute();
1011};
1012
1013#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1014
Howard Hinnant99968442011-11-29 18:15:50 +00001015template <class _Fp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001016inline
Howard Hinnant99968442011-11-29 18:15:50 +00001017__async_assoc_state<void, _Fp>::__async_assoc_state(_Fp&& __f)
1018 : __func_(_VSTD::forward<_Fp>(__f))
Howard Hinnant57cff292011-05-19 15:05:04 +00001019{
1020}
1021
1022#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1023
Howard Hinnant99968442011-11-29 18:15:50 +00001024template <class _Fp>
Howard Hinnant57cff292011-05-19 15:05:04 +00001025void
Howard Hinnant99968442011-11-29 18:15:50 +00001026__async_assoc_state<void, _Fp>::__execute()
Howard Hinnant57cff292011-05-19 15:05:04 +00001027{
1028#ifndef _LIBCPP_NO_EXCEPTIONS
1029 try
1030 {
1031#endif // _LIBCPP_NO_EXCEPTIONS
1032 __func_();
1033 this->set_value();
1034#ifndef _LIBCPP_NO_EXCEPTIONS
1035 }
1036 catch (...)
1037 {
1038 this->set_exception(current_exception());
1039 }
1040#endif // _LIBCPP_NO_EXCEPTIONS
1041}
1042
Howard Hinnant99968442011-11-29 18:15:50 +00001043template <class _Fp>
Howard Hinnant57cff292011-05-19 15:05:04 +00001044void
Howard Hinnant99968442011-11-29 18:15:50 +00001045__async_assoc_state<void, _Fp>::__on_zero_shared() _NOEXCEPT
Howard Hinnant57cff292011-05-19 15:05:04 +00001046{
1047 this->wait();
1048 base::__on_zero_shared();
1049}
1050
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001051template <class _Rp> class _LIBCPP_TYPE_VIS_ONLY promise;
1052template <class _Rp> class _LIBCPP_TYPE_VIS_ONLY shared_future;
Howard Hinnant47499b12010-08-27 20:10:19 +00001053
1054// future
1055
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001056template <class _Rp> class _LIBCPP_TYPE_VIS_ONLY future;
Howard Hinnant54da3382010-08-30 18:46:21 +00001057
Howard Hinnant99968442011-11-29 18:15:50 +00001058template <class _Rp, class _Fp>
1059future<_Rp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00001060#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00001061__make_deferred_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001062#else
Howard Hinnant99968442011-11-29 18:15:50 +00001063__make_deferred_assoc_state(_Fp __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001064#endif
1065
Howard Hinnant99968442011-11-29 18:15:50 +00001066template <class _Rp, class _Fp>
1067future<_Rp>
Howard Hinnant57cff292011-05-19 15:05:04 +00001068#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00001069__make_async_assoc_state(_Fp&& __f);
Howard Hinnant57cff292011-05-19 15:05:04 +00001070#else
Howard Hinnant99968442011-11-29 18:15:50 +00001071__make_async_assoc_state(_Fp __f);
Howard Hinnant57cff292011-05-19 15:05:04 +00001072#endif
1073
Howard Hinnant99968442011-11-29 18:15:50 +00001074template <class _Rp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001075class _LIBCPP_TYPE_VIS_ONLY future
Howard Hinnant47499b12010-08-27 20:10:19 +00001076{
Howard Hinnant99968442011-11-29 18:15:50 +00001077 __assoc_state<_Rp>* __state_;
Howard Hinnant47499b12010-08-27 20:10:19 +00001078
Howard Hinnant99968442011-11-29 18:15:50 +00001079 explicit future(__assoc_state<_Rp>* __state);
Howard Hinnant47499b12010-08-27 20:10:19 +00001080
1081 template <class> friend class promise;
Howard Hinnant99be8232010-09-03 18:39:25 +00001082 template <class> friend class shared_future;
Howard Hinnant54da3382010-08-30 18:46:21 +00001083
Howard Hinnant73d21a42010-09-04 23:28:19 +00001084#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00001085 template <class _R1, class _Fp>
1086 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1087 template <class _R1, class _Fp>
1088 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001089#else
Howard Hinnant99968442011-11-29 18:15:50 +00001090 template <class _R1, class _Fp>
1091 friend future<_R1> __make_deferred_assoc_state(_Fp __f);
1092 template <class _R1, class _Fp>
1093 friend future<_R1> __make_async_assoc_state(_Fp __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001094#endif
1095
Howard Hinnant47499b12010-08-27 20:10:19 +00001096public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001097 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001098 future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001099#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001100 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001101 future(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +00001102 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1103 future(const future&) = delete;
1104 future& operator=(const future&) = delete;
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001105 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001106 future& operator=(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +00001107 {
1108 future(std::move(__rhs)).swap(*this);
1109 return *this;
1110 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001111#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001112private:
1113 future(const future&);
1114 future& operator=(const future&);
1115public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001116#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001117 ~future();
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001118 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001119 shared_future<_Rp> share();
Howard Hinnant47499b12010-08-27 20:10:19 +00001120
1121 // retrieving the value
Howard Hinnant99968442011-11-29 18:15:50 +00001122 _Rp get();
Howard Hinnant47499b12010-08-27 20:10:19 +00001123
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001124 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001125 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:19 +00001126
1127 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001128 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001129 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant47499b12010-08-27 20:10:19 +00001130
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001131 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001132 void wait() const {__state_->wait();}
1133 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001134 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001135 future_status
1136 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1137 {return __state_->wait_for(__rel_time);}
1138 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001139 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001140 future_status
1141 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1142 {return __state_->wait_until(__abs_time);}
1143};
1144
Howard Hinnant99968442011-11-29 18:15:50 +00001145template <class _Rp>
1146future<_Rp>::future(__assoc_state<_Rp>* __state)
Howard Hinnant47499b12010-08-27 20:10:19 +00001147 : __state_(__state)
1148{
1149 if (__state_->__has_future_attached())
Eric Fiselier423ca202015-10-02 21:25:15 +00001150 __throw_future_error(future_errc::future_already_retrieved);
Howard Hinnant47499b12010-08-27 20:10:19 +00001151 __state_->__add_shared();
Howard Hinnant54da3382010-08-30 18:46:21 +00001152 __state_->__set_future_attached();
Howard Hinnant47499b12010-08-27 20:10:19 +00001153}
1154
Howard Hinnant54da3382010-08-30 18:46:21 +00001155struct __release_shared_count
1156{
1157 void operator()(__shared_count* p) {p->__release_shared();}
1158};
1159
Howard Hinnant99968442011-11-29 18:15:50 +00001160template <class _Rp>
1161future<_Rp>::~future()
Howard Hinnant47499b12010-08-27 20:10:19 +00001162{
1163 if (__state_)
1164 __state_->__release_shared();
1165}
1166
Howard Hinnant99968442011-11-29 18:15:50 +00001167template <class _Rp>
1168_Rp
1169future<_Rp>::get()
Howard Hinnant47499b12010-08-27 20:10:19 +00001170{
Howard Hinnant54da3382010-08-30 18:46:21 +00001171 unique_ptr<__shared_count, __release_shared_count> __(__state_);
Howard Hinnant99968442011-11-29 18:15:50 +00001172 __assoc_state<_Rp>* __s = __state_;
Howard Hinnant47499b12010-08-27 20:10:19 +00001173 __state_ = nullptr;
1174 return __s->move();
1175}
1176
Howard Hinnant99968442011-11-29 18:15:50 +00001177template <class _Rp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001178class _LIBCPP_TYPE_VIS_ONLY future<_Rp&>
Howard Hinnant47499b12010-08-27 20:10:19 +00001179{
Howard Hinnant99968442011-11-29 18:15:50 +00001180 __assoc_state<_Rp&>* __state_;
Howard Hinnant47499b12010-08-27 20:10:19 +00001181
Howard Hinnant99968442011-11-29 18:15:50 +00001182 explicit future(__assoc_state<_Rp&>* __state);
Howard Hinnant47499b12010-08-27 20:10:19 +00001183
1184 template <class> friend class promise;
Howard Hinnant99be8232010-09-03 18:39:25 +00001185 template <class> friend class shared_future;
Howard Hinnant54da3382010-08-30 18:46:21 +00001186
Howard Hinnant73d21a42010-09-04 23:28:19 +00001187#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00001188 template <class _R1, class _Fp>
1189 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1190 template <class _R1, class _Fp>
1191 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001192#else
Howard Hinnant99968442011-11-29 18:15:50 +00001193 template <class _R1, class _Fp>
1194 friend future<_R1> __make_deferred_assoc_state(_Fp __f);
1195 template <class _R1, class _Fp>
1196 friend future<_R1> __make_async_assoc_state(_Fp __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001197#endif
1198
Howard Hinnant47499b12010-08-27 20:10:19 +00001199public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001200 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001201 future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001202#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001203 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001204 future(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +00001205 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1206 future(const future&) = delete;
1207 future& operator=(const future&) = delete;
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001208 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001209 future& operator=(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +00001210 {
1211 future(std::move(__rhs)).swap(*this);
1212 return *this;
1213 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001214#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001215private:
1216 future(const future&);
1217 future& operator=(const future&);
1218public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001219#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001220 ~future();
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001221 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001222 shared_future<_Rp&> share();
Howard Hinnant47499b12010-08-27 20:10:19 +00001223
1224 // retrieving the value
Howard Hinnant99968442011-11-29 18:15:50 +00001225 _Rp& get();
Howard Hinnant47499b12010-08-27 20:10:19 +00001226
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001227 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001228 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:19 +00001229
1230 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001231 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001232 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant47499b12010-08-27 20:10:19 +00001233
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001234 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001235 void wait() const {__state_->wait();}
1236 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001237 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001238 future_status
1239 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1240 {return __state_->wait_for(__rel_time);}
1241 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001242 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001243 future_status
1244 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1245 {return __state_->wait_until(__abs_time);}
1246};
1247
Howard Hinnant99968442011-11-29 18:15:50 +00001248template <class _Rp>
1249future<_Rp&>::future(__assoc_state<_Rp&>* __state)
Howard Hinnant47499b12010-08-27 20:10:19 +00001250 : __state_(__state)
1251{
1252 if (__state_->__has_future_attached())
Eric Fiselier423ca202015-10-02 21:25:15 +00001253 __throw_future_error(future_errc::future_already_retrieved);
Howard Hinnant47499b12010-08-27 20:10:19 +00001254 __state_->__add_shared();
Howard Hinnant54da3382010-08-30 18:46:21 +00001255 __state_->__set_future_attached();
Howard Hinnant47499b12010-08-27 20:10:19 +00001256}
1257
Howard Hinnant99968442011-11-29 18:15:50 +00001258template <class _Rp>
1259future<_Rp&>::~future()
Howard Hinnant47499b12010-08-27 20:10:19 +00001260{
1261 if (__state_)
1262 __state_->__release_shared();
1263}
1264
Howard Hinnant99968442011-11-29 18:15:50 +00001265template <class _Rp>
1266_Rp&
1267future<_Rp&>::get()
Howard Hinnant47499b12010-08-27 20:10:19 +00001268{
Howard Hinnant54da3382010-08-30 18:46:21 +00001269 unique_ptr<__shared_count, __release_shared_count> __(__state_);
Howard Hinnant99968442011-11-29 18:15:50 +00001270 __assoc_state<_Rp&>* __s = __state_;
Howard Hinnant47499b12010-08-27 20:10:19 +00001271 __state_ = nullptr;
1272 return __s->copy();
1273}
1274
1275template <>
Howard Hinnant83eade62013-03-06 23:30:19 +00001276class _LIBCPP_TYPE_VIS future<void>
Howard Hinnant47499b12010-08-27 20:10:19 +00001277{
1278 __assoc_sub_state* __state_;
1279
1280 explicit future(__assoc_sub_state* __state);
1281
1282 template <class> friend class promise;
Howard Hinnant99be8232010-09-03 18:39:25 +00001283 template <class> friend class shared_future;
Howard Hinnant54da3382010-08-30 18:46:21 +00001284
Howard Hinnant73d21a42010-09-04 23:28:19 +00001285#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00001286 template <class _R1, class _Fp>
1287 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1288 template <class _R1, class _Fp>
1289 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001290#else
Howard Hinnant99968442011-11-29 18:15:50 +00001291 template <class _R1, class _Fp>
1292 friend future<_R1> __make_deferred_assoc_state(_Fp __f);
1293 template <class _R1, class _Fp>
1294 friend future<_R1> __make_async_assoc_state(_Fp __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001295#endif
1296
Howard Hinnant47499b12010-08-27 20:10:19 +00001297public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001298 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001299 future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001300#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001301 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001302 future(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +00001303 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1304 future(const future&) = delete;
1305 future& operator=(const future&) = delete;
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001306 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001307 future& operator=(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +00001308 {
1309 future(std::move(__rhs)).swap(*this);
1310 return *this;
1311 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001312#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001313private:
1314 future(const future&);
1315 future& operator=(const future&);
1316public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001317#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001318 ~future();
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001319 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7de47902010-11-30 20:23:32 +00001320 shared_future<void> share();
Howard Hinnant47499b12010-08-27 20:10:19 +00001321
1322 // retrieving the value
1323 void get();
1324
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001325 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001326 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:19 +00001327
1328 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001329 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001330 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant47499b12010-08-27 20:10:19 +00001331
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001332 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001333 void wait() const {__state_->wait();}
1334 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001335 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001336 future_status
1337 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1338 {return __state_->wait_for(__rel_time);}
1339 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001340 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001341 future_status
1342 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1343 {return __state_->wait_until(__abs_time);}
1344};
1345
Howard Hinnant99968442011-11-29 18:15:50 +00001346template <class _Rp>
Howard Hinnant99be8232010-09-03 18:39:25 +00001347inline _LIBCPP_INLINE_VISIBILITY
1348void
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001349swap(future<_Rp>& __x, future<_Rp>& __y) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:25 +00001350{
1351 __x.swap(__y);
1352}
1353
Howard Hinnant47499b12010-08-27 20:10:19 +00001354// promise<R>
1355
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00001356template <class _Callable> class packaged_task;
Howard Hinnant54da3382010-08-30 18:46:21 +00001357
Howard Hinnant99968442011-11-29 18:15:50 +00001358template <class _Rp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001359class _LIBCPP_TYPE_VIS_ONLY promise
Howard Hinnant47499b12010-08-27 20:10:19 +00001360{
Howard Hinnant99968442011-11-29 18:15:50 +00001361 __assoc_state<_Rp>* __state_;
Howard Hinnant54da3382010-08-30 18:46:21 +00001362
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001363 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001364 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant54da3382010-08-30 18:46:21 +00001365
1366 template <class> friend class packaged_task;
Howard Hinnant47499b12010-08-27 20:10:19 +00001367public:
1368 promise();
1369 template <class _Alloc>
1370 promise(allocator_arg_t, const _Alloc& __a);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001371#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001372 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001373 promise(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +00001374 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1375 promise(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +00001376#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001377private:
1378 promise(const promise& __rhs);
1379public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001380#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001381 ~promise();
1382
1383 // assignment
Howard Hinnant73d21a42010-09-04 23:28:19 +00001384#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001385 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001386 promise& operator=(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +00001387 {
1388 promise(std::move(__rhs)).swap(*this);
1389 return *this;
1390 }
1391 promise& operator=(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +00001392#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001393private:
1394 promise& operator=(const promise& __rhs);
1395public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001396#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001397 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001398 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:19 +00001399
1400 // retrieving the result
Howard Hinnant99968442011-11-29 18:15:50 +00001401 future<_Rp> get_future();
Howard Hinnant47499b12010-08-27 20:10:19 +00001402
1403 // setting the result
Howard Hinnant99968442011-11-29 18:15:50 +00001404 void set_value(const _Rp& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001405#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00001406 void set_value(_Rp&& __r);
Howard Hinnant47499b12010-08-27 20:10:19 +00001407#endif
1408 void set_exception(exception_ptr __p);
1409
1410 // setting the result with deferred notification
Howard Hinnant99968442011-11-29 18:15:50 +00001411 void set_value_at_thread_exit(const _Rp& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001412#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00001413 void set_value_at_thread_exit(_Rp&& __r);
Howard Hinnant47499b12010-08-27 20:10:19 +00001414#endif
1415 void set_exception_at_thread_exit(exception_ptr __p);
1416};
1417
Howard Hinnant99968442011-11-29 18:15:50 +00001418template <class _Rp>
1419promise<_Rp>::promise()
1420 : __state_(new __assoc_state<_Rp>)
Howard Hinnant47499b12010-08-27 20:10:19 +00001421{
1422}
1423
Howard Hinnant99968442011-11-29 18:15:50 +00001424template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +00001425template <class _Alloc>
Howard Hinnant99968442011-11-29 18:15:50 +00001426promise<_Rp>::promise(allocator_arg_t, const _Alloc& __a0)
Howard Hinnant47499b12010-08-27 20:10:19 +00001427{
Eric Fiselier4d2413c2014-10-23 06:24:45 +00001428 typedef __assoc_state_alloc<_Rp, _Alloc> _State;
1429 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
Howard Hinnant47499b12010-08-27 20:10:19 +00001430 typedef __allocator_destructor<_A2> _D2;
1431 _A2 __a(__a0);
Eric Fiselier4d2413c2014-10-23 06:24:45 +00001432 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1433 ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0);
1434 __state_ = _VSTD::addressof(*__hold.release());
Howard Hinnant47499b12010-08-27 20:10:19 +00001435}
1436
Howard Hinnant99968442011-11-29 18:15:50 +00001437template <class _Rp>
1438promise<_Rp>::~promise()
Howard Hinnant47499b12010-08-27 20:10:19 +00001439{
1440 if (__state_)
1441 {
1442 if (!__state_->__has_value() && __state_->use_count() > 1)
1443 __state_->set_exception(make_exception_ptr(
1444 future_error(make_error_code(future_errc::broken_promise))
1445 ));
1446 __state_->__release_shared();
1447 }
1448}
1449
Howard Hinnant99968442011-11-29 18:15:50 +00001450template <class _Rp>
1451future<_Rp>
1452promise<_Rp>::get_future()
Howard Hinnant47499b12010-08-27 20:10:19 +00001453{
1454 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00001455 __throw_future_error(future_errc::no_state);
Howard Hinnant99968442011-11-29 18:15:50 +00001456 return future<_Rp>(__state_);
Howard Hinnant47499b12010-08-27 20:10:19 +00001457}
1458
Howard Hinnant99968442011-11-29 18:15:50 +00001459template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +00001460void
Howard Hinnant99968442011-11-29 18:15:50 +00001461promise<_Rp>::set_value(const _Rp& __r)
Howard Hinnant47499b12010-08-27 20:10:19 +00001462{
1463 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00001464 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:19 +00001465 __state_->set_value(__r);
1466}
1467
Howard Hinnant73d21a42010-09-04 23:28:19 +00001468#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001469
Howard Hinnant99968442011-11-29 18:15:50 +00001470template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +00001471void
Howard Hinnant99968442011-11-29 18:15:50 +00001472promise<_Rp>::set_value(_Rp&& __r)
Howard Hinnant47499b12010-08-27 20:10:19 +00001473{
1474 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00001475 __throw_future_error(future_errc::no_state);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001476 __state_->set_value(_VSTD::move(__r));
Howard Hinnant47499b12010-08-27 20:10:19 +00001477}
1478
Howard Hinnant73d21a42010-09-04 23:28:19 +00001479#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001480
Howard Hinnant99968442011-11-29 18:15:50 +00001481template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +00001482void
Howard Hinnant99968442011-11-29 18:15:50 +00001483promise<_Rp>::set_exception(exception_ptr __p)
Howard Hinnant47499b12010-08-27 20:10:19 +00001484{
1485 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00001486 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:19 +00001487 __state_->set_exception(__p);
1488}
1489
Howard Hinnant99968442011-11-29 18:15:50 +00001490template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +00001491void
Howard Hinnant99968442011-11-29 18:15:50 +00001492promise<_Rp>::set_value_at_thread_exit(const _Rp& __r)
Howard Hinnant47499b12010-08-27 20:10:19 +00001493{
1494 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00001495 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:19 +00001496 __state_->set_value_at_thread_exit(__r);
1497}
1498
Howard Hinnant73d21a42010-09-04 23:28:19 +00001499#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001500
Howard Hinnant99968442011-11-29 18:15:50 +00001501template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +00001502void
Howard Hinnant99968442011-11-29 18:15:50 +00001503promise<_Rp>::set_value_at_thread_exit(_Rp&& __r)
Howard Hinnant47499b12010-08-27 20:10:19 +00001504{
1505 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00001506 __throw_future_error(future_errc::no_state);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001507 __state_->set_value_at_thread_exit(_VSTD::move(__r));
Howard Hinnant47499b12010-08-27 20:10:19 +00001508}
1509
Howard Hinnant73d21a42010-09-04 23:28:19 +00001510#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001511
Howard Hinnant99968442011-11-29 18:15:50 +00001512template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +00001513void
Howard Hinnant99968442011-11-29 18:15:50 +00001514promise<_Rp>::set_exception_at_thread_exit(exception_ptr __p)
Howard Hinnant47499b12010-08-27 20:10:19 +00001515{
1516 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00001517 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:19 +00001518 __state_->set_exception_at_thread_exit(__p);
1519}
1520
1521// promise<R&>
1522
Howard Hinnant99968442011-11-29 18:15:50 +00001523template <class _Rp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001524class _LIBCPP_TYPE_VIS_ONLY promise<_Rp&>
Howard Hinnant47499b12010-08-27 20:10:19 +00001525{
Howard Hinnant99968442011-11-29 18:15:50 +00001526 __assoc_state<_Rp&>* __state_;
Howard Hinnant54da3382010-08-30 18:46:21 +00001527
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001528 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001529 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant54da3382010-08-30 18:46:21 +00001530
1531 template <class> friend class packaged_task;
1532
Howard Hinnant47499b12010-08-27 20:10:19 +00001533public:
1534 promise();
1535 template <class _Allocator>
1536 promise(allocator_arg_t, const _Allocator& __a);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001537#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001538 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001539 promise(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +00001540 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1541 promise(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +00001542#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001543private:
1544 promise(const promise& __rhs);
1545public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001546#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001547 ~promise();
1548
1549 // assignment
Howard Hinnant73d21a42010-09-04 23:28:19 +00001550#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001551 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001552 promise& operator=(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +00001553 {
1554 promise(std::move(__rhs)).swap(*this);
1555 return *this;
1556 }
1557 promise& operator=(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +00001558#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001559private:
1560 promise& operator=(const promise& __rhs);
1561public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001562#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001563 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001564 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:19 +00001565
1566 // retrieving the result
Howard Hinnant99968442011-11-29 18:15:50 +00001567 future<_Rp&> get_future();
Howard Hinnant47499b12010-08-27 20:10:19 +00001568
1569 // setting the result
Howard Hinnant99968442011-11-29 18:15:50 +00001570 void set_value(_Rp& __r);
Howard Hinnant47499b12010-08-27 20:10:19 +00001571 void set_exception(exception_ptr __p);
1572
1573 // setting the result with deferred notification
Howard Hinnant99968442011-11-29 18:15:50 +00001574 void set_value_at_thread_exit(_Rp&);
Howard Hinnant47499b12010-08-27 20:10:19 +00001575 void set_exception_at_thread_exit(exception_ptr __p);
1576};
1577
Howard Hinnant99968442011-11-29 18:15:50 +00001578template <class _Rp>
1579promise<_Rp&>::promise()
1580 : __state_(new __assoc_state<_Rp&>)
Howard Hinnant47499b12010-08-27 20:10:19 +00001581{
1582}
1583
Howard Hinnant99968442011-11-29 18:15:50 +00001584template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +00001585template <class _Alloc>
Howard Hinnant99968442011-11-29 18:15:50 +00001586promise<_Rp&>::promise(allocator_arg_t, const _Alloc& __a0)
Howard Hinnant47499b12010-08-27 20:10:19 +00001587{
Eric Fiselier4d2413c2014-10-23 06:24:45 +00001588 typedef __assoc_state_alloc<_Rp&, _Alloc> _State;
1589 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
Howard Hinnant47499b12010-08-27 20:10:19 +00001590 typedef __allocator_destructor<_A2> _D2;
1591 _A2 __a(__a0);
Eric Fiselier4d2413c2014-10-23 06:24:45 +00001592 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:19 +00001595}
1596
Howard Hinnant99968442011-11-29 18:15:50 +00001597template <class _Rp>
1598promise<_Rp&>::~promise()
Howard Hinnant47499b12010-08-27 20:10:19 +00001599{
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:50 +00001610template <class _Rp>
1611future<_Rp&>
1612promise<_Rp&>::get_future()
Howard Hinnant47499b12010-08-27 20:10:19 +00001613{
1614 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00001615 __throw_future_error(future_errc::no_state);
Howard Hinnant99968442011-11-29 18:15:50 +00001616 return future<_Rp&>(__state_);
Howard Hinnant47499b12010-08-27 20:10:19 +00001617}
1618
Howard Hinnant99968442011-11-29 18:15:50 +00001619template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +00001620void
Howard Hinnant99968442011-11-29 18:15:50 +00001621promise<_Rp&>::set_value(_Rp& __r)
Howard Hinnant47499b12010-08-27 20:10:19 +00001622{
1623 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00001624 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:19 +00001625 __state_->set_value(__r);
1626}
1627
Howard Hinnant99968442011-11-29 18:15:50 +00001628template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +00001629void
Howard Hinnant99968442011-11-29 18:15:50 +00001630promise<_Rp&>::set_exception(exception_ptr __p)
Howard Hinnant47499b12010-08-27 20:10:19 +00001631{
1632 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00001633 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:19 +00001634 __state_->set_exception(__p);
1635}
1636
Howard Hinnant99968442011-11-29 18:15:50 +00001637template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +00001638void
Howard Hinnant99968442011-11-29 18:15:50 +00001639promise<_Rp&>::set_value_at_thread_exit(_Rp& __r)
Howard Hinnant47499b12010-08-27 20:10:19 +00001640{
1641 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00001642 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:19 +00001643 __state_->set_value_at_thread_exit(__r);
1644}
1645
Howard Hinnant99968442011-11-29 18:15:50 +00001646template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +00001647void
Howard Hinnant99968442011-11-29 18:15:50 +00001648promise<_Rp&>::set_exception_at_thread_exit(exception_ptr __p)
Howard Hinnant47499b12010-08-27 20:10:19 +00001649{
1650 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00001651 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:19 +00001652 __state_->set_exception_at_thread_exit(__p);
1653}
1654
1655// promise<void>
1656
1657template <>
Howard Hinnant83eade62013-03-06 23:30:19 +00001658class _LIBCPP_TYPE_VIS promise<void>
Howard Hinnant47499b12010-08-27 20:10:19 +00001659{
1660 __assoc_sub_state* __state_;
Howard Hinnant54da3382010-08-30 18:46:21 +00001661
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001662 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001663 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant54da3382010-08-30 18:46:21 +00001664
1665 template <class> friend class packaged_task;
1666
Howard Hinnant47499b12010-08-27 20:10:19 +00001667public:
1668 promise();
1669 template <class _Allocator>
1670 promise(allocator_arg_t, const _Allocator& __a);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001671#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001672 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001673 promise(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +00001674 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1675 promise(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +00001676#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001677private:
1678 promise(const promise& __rhs);
1679public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001680#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001681 ~promise();
1682
1683 // assignment
Howard Hinnant73d21a42010-09-04 23:28:19 +00001684#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001685 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001686 promise& operator=(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +00001687 {
1688 promise(std::move(__rhs)).swap(*this);
1689 return *this;
1690 }
1691 promise& operator=(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +00001692#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001693private:
1694 promise& operator=(const promise& __rhs);
1695public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001696#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001697 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001698 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:19 +00001699
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:45 +00001715 typedef __assoc_sub_state_alloc<_Alloc> _State;
1716 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
Howard Hinnant47499b12010-08-27 20:10:19 +00001717 typedef __allocator_destructor<_A2> _D2;
1718 _A2 __a(__a0);
Eric Fiselier4d2413c2014-10-23 06:24:45 +00001719 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:19 +00001722}
1723
Howard Hinnant99968442011-11-29 18:15:50 +00001724template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +00001725inline _LIBCPP_INLINE_VISIBILITY
1726void
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001727swap(promise<_Rp>& __x, promise<_Rp>& __y) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +00001728{
1729 __x.swap(__y);
1730}
1731
Howard Hinnant99968442011-11-29 18:15:50 +00001732template <class _Rp, class _Alloc>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001733 struct _LIBCPP_TYPE_VIS_ONLY uses_allocator<promise<_Rp>, _Alloc>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001734 : public true_type {};
Howard Hinnant47499b12010-08-27 20:10:19 +00001735
Howard Hinnant54da3382010-08-30 18:46:21 +00001736#ifndef _LIBCPP_HAS_NO_VARIADICS
1737
1738// packaged_task
1739
1740template<class _Fp> class __packaged_task_base;
1741
Howard Hinnant99968442011-11-29 18:15:50 +00001742template<class _Rp, class ..._ArgTypes>
1743class __packaged_task_base<_Rp(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:21 +00001744{
1745 __packaged_task_base(const __packaged_task_base&);
1746 __packaged_task_base& operator=(const __packaged_task_base&);
1747public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001748 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001749 __packaged_task_base() {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001750 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001751 virtual ~__packaged_task_base() {}
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001752 virtual void __move_to(__packaged_task_base*) _NOEXCEPT = 0;
Howard Hinnant54da3382010-08-30 18:46:21 +00001753 virtual void destroy() = 0;
1754 virtual void destroy_deallocate() = 0;
Howard Hinnant99968442011-11-29 18:15:50 +00001755 virtual _Rp operator()(_ArgTypes&& ...) = 0;
Howard Hinnant54da3382010-08-30 18:46:21 +00001756};
1757
1758template<class _FD, class _Alloc, class _FB> class __packaged_task_func;
1759
Howard Hinnant99968442011-11-29 18:15:50 +00001760template<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:21 +00001763{
Howard Hinnant99968442011-11-29 18:15:50 +00001764 __compressed_pair<_Fp, _Alloc> __f_;
Howard Hinnant54da3382010-08-30 18:46:21 +00001765public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001766 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001767 explicit __packaged_task_func(const _Fp& __f) : __f_(__f) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001768 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001769 explicit __packaged_task_func(_Fp&& __f) : __f_(_VSTD::move(__f)) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001770 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001771 __packaged_task_func(const _Fp& __f, const _Alloc& __a)
Howard Hinnant54da3382010-08-30 18:46:21 +00001772 : __f_(__f, __a) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001773 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001774 __packaged_task_func(_Fp&& __f, const _Alloc& __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001775 : __f_(_VSTD::move(__f), __a) {}
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001776 virtual void __move_to(__packaged_task_base<_Rp(_ArgTypes...)>*) _NOEXCEPT;
Howard Hinnant54da3382010-08-30 18:46:21 +00001777 virtual void destroy();
1778 virtual void destroy_deallocate();
Howard Hinnant99968442011-11-29 18:15:50 +00001779 virtual _Rp operator()(_ArgTypes&& ... __args);
Howard Hinnant54da3382010-08-30 18:46:21 +00001780};
1781
Howard Hinnant99968442011-11-29 18:15:50 +00001782template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:21 +00001783void
Howard Hinnant99968442011-11-29 18:15:50 +00001784__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__move_to(
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001785 __packaged_task_base<_Rp(_ArgTypes...)>* __p) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:21 +00001786{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001787 ::new (__p) __packaged_task_func(_VSTD::move(__f_.first()), _VSTD::move(__f_.second()));
Howard Hinnant54da3382010-08-30 18:46:21 +00001788}
1789
Howard Hinnant99968442011-11-29 18:15:50 +00001790template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:21 +00001791void
Howard Hinnant99968442011-11-29 18:15:50 +00001792__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy()
Howard Hinnant54da3382010-08-30 18:46:21 +00001793{
Howard Hinnant99968442011-11-29 18:15:50 +00001794 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnant54da3382010-08-30 18:46:21 +00001795}
1796
Howard Hinnant99968442011-11-29 18:15:50 +00001797template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:21 +00001798void
Howard Hinnant99968442011-11-29 18:15:50 +00001799__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate()
Howard Hinnant54da3382010-08-30 18:46:21 +00001800{
Eric Fiselier4d2413c2014-10-23 06:24:45 +00001801 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:50 +00001804 _Ap __a(__f_.second());
1805 __f_.~__compressed_pair<_Fp, _Alloc>();
Eric Fiselier4d2413c2014-10-23 06:24:45 +00001806 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnant54da3382010-08-30 18:46:21 +00001807}
1808
Howard Hinnant99968442011-11-29 18:15:50 +00001809template<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:21 +00001812{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001813 return __invoke(__f_.first(), _VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnant54da3382010-08-30 18:46:21 +00001814}
1815
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00001816template <class _Callable> class __packaged_task_function;
Howard Hinnant54da3382010-08-30 18:46:21 +00001817
Howard Hinnant99968442011-11-29 18:15:50 +00001818template<class _Rp, class ..._ArgTypes>
1819class __packaged_task_function<_Rp(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:21 +00001820{
Howard Hinnant99968442011-11-29 18:15:50 +00001821 typedef __packaged_task_base<_Rp(_ArgTypes...)> __base;
Howard Hinnant78f0de22013-01-21 17:26:55 +00001822 typename aligned_storage<3*sizeof(void*)>::type __buf_;
Howard Hinnant54da3382010-08-30 18:46:21 +00001823 __base* __f_;
1824
1825public:
Howard Hinnant99968442011-11-29 18:15:50 +00001826 typedef _Rp result_type;
Howard Hinnant54da3382010-08-30 18:46:21 +00001827
1828 // construct/copy/destroy:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001829 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001830 __packaged_task_function() _NOEXCEPT : __f_(nullptr) {}
Howard Hinnant99968442011-11-29 18:15:50 +00001831 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:21 +00001835
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001836 __packaged_task_function(__packaged_task_function&&) _NOEXCEPT;
1837 __packaged_task_function& operator=(__packaged_task_function&&) _NOEXCEPT;
Howard Hinnant54da3382010-08-30 18:46:21 +00001838
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:55 +00001844 void swap(__packaged_task_function&) _NOEXCEPT;
Howard Hinnant54da3382010-08-30 18:46:21 +00001845
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001846 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001847 _Rp operator()(_ArgTypes...) const;
Howard Hinnant54da3382010-08-30 18:46:21 +00001848};
1849
Howard Hinnant99968442011-11-29 18:15:50 +00001850template<class _Rp, class ..._ArgTypes>
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001851__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:21 +00001852{
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:50 +00001867template<class _Rp, class ..._ArgTypes>
1868template <class _Fp>
1869__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(_Fp&& __f)
Howard Hinnant54da3382010-08-30 18:46:21 +00001870 : __f_(nullptr)
1871{
Marshall Clowf1264e72014-04-07 13:32:26 +00001872 typedef typename remove_reference<typename decay<_Fp>::type>::type _FR;
Howard Hinnant99968442011-11-29 18:15:50 +00001873 typedef __packaged_task_func<_FR, allocator<_FR>, _Rp(_ArgTypes...)> _FF;
Howard Hinnant54da3382010-08-30 18:46:21 +00001874 if (sizeof(_FF) <= sizeof(__buf_))
1875 {
1876 __f_ = (__base*)&__buf_;
Howard Hinnant99968442011-11-29 18:15:50 +00001877 ::new (__f_) _FF(_VSTD::forward<_Fp>(__f));
Howard Hinnant54da3382010-08-30 18:46:21 +00001878 }
1879 else
1880 {
Howard Hinnant99968442011-11-29 18:15:50 +00001881 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:21 +00001886 __f_ = __hold.release();
1887 }
1888}
1889
Howard Hinnant99968442011-11-29 18:15:50 +00001890template<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:21 +00001894 : __f_(nullptr)
1895{
Marshall Clowf1264e72014-04-07 13:32:26 +00001896 typedef typename remove_reference<typename decay<_Fp>::type>::type _FR;
Howard Hinnant99968442011-11-29 18:15:50 +00001897 typedef __packaged_task_func<_FR, _Alloc, _Rp(_ArgTypes...)> _FF;
Howard Hinnant54da3382010-08-30 18:46:21 +00001898 if (sizeof(_FF) <= sizeof(__buf_))
1899 {
1900 __f_ = (__base*)&__buf_;
Howard Hinnant99968442011-11-29 18:15:50 +00001901 ::new (__f_) _FF(_VSTD::forward<_Fp>(__f));
Howard Hinnant54da3382010-08-30 18:46:21 +00001902 }
1903 else
1904 {
Eric Fiselier4d2413c2014-10-23 06:24:45 +00001905 typedef typename __allocator_traits_rebind<_Alloc, _FF>::type _Ap;
Howard Hinnant99968442011-11-29 18:15:50 +00001906 _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:45 +00001909 ::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:21 +00001912 }
1913}
1914
Howard Hinnant99968442011-11-29 18:15:50 +00001915template<class _Rp, class ..._ArgTypes>
1916__packaged_task_function<_Rp(_ArgTypes...)>&
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001917__packaged_task_function<_Rp(_ArgTypes...)>::operator=(__packaged_task_function&& __f) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:21 +00001918{
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:45 +00001936 return *this;
Howard Hinnant54da3382010-08-30 18:46:21 +00001937}
1938
Howard Hinnant99968442011-11-29 18:15:50 +00001939template<class _Rp, class ..._ArgTypes>
1940__packaged_task_function<_Rp(_ArgTypes...)>::~__packaged_task_function()
Howard Hinnant54da3382010-08-30 18:46:21 +00001941{
1942 if (__f_ == (__base*)&__buf_)
1943 __f_->destroy();
1944 else if (__f_)
1945 __f_->destroy_deallocate();
1946}
1947
Howard Hinnant99968442011-11-29 18:15:50 +00001948template<class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:21 +00001949void
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001950__packaged_task_function<_Rp(_ArgTypes...)>::swap(__packaged_task_function& __f) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:21 +00001951{
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:19 +00001982 _VSTD::swap(__f_, __f.__f_);
Howard Hinnant54da3382010-08-30 18:46:21 +00001983}
1984
Howard Hinnant99968442011-11-29 18:15:50 +00001985template<class _Rp, class ..._ArgTypes>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001986inline
Howard Hinnant99968442011-11-29 18:15:50 +00001987_Rp
1988__packaged_task_function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
Howard Hinnant54da3382010-08-30 18:46:21 +00001989{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001990 return (*__f_)(_VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnant54da3382010-08-30 18:46:21 +00001991}
1992
Howard Hinnant99968442011-11-29 18:15:50 +00001993template<class _Rp, class ..._ArgTypes>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001994class _LIBCPP_TYPE_VIS_ONLY packaged_task<_Rp(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:21 +00001995{
1996public:
Howard Hinnant99968442011-11-29 18:15:50 +00001997 typedef _Rp result_type;
Howard Hinnant54da3382010-08-30 18:46:21 +00001998
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:26 +00002005 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002006 packaged_task() _NOEXCEPT : __p_(nullptr) {}
Marshall Clow5f2d5b92013-10-12 22:49:17 +00002007 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:26 +00002016 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00002017 explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
Marshall Clow5f2d5b92013-10-12 22:49:17 +00002018 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:26 +00002027 _LIBCPP_INLINE_VISIBILITY
Marshall Clow07546f32015-06-30 14:16:49 +00002028 packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
Howard Hinnant99968442011-11-29 18:15:50 +00002029 : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)),
Howard Hinnant54da3382010-08-30 18:46:21 +00002030 __p_(allocator_arg, __a) {}
2031 // ~packaged_task() = default;
2032
2033 // no copy
Howard Hinnant8131a012012-07-21 19:34:12 +00002034 packaged_task(const packaged_task&) = delete;
2035 packaged_task& operator=(const packaged_task&) = delete;
Howard Hinnant54da3382010-08-30 18:46:21 +00002036
2037 // move support
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002038 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002039 packaged_task(packaged_task&& __other) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002040 : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002041 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002042 packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:21 +00002043 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002044 __f_ = _VSTD::move(__other.__f_);
2045 __p_ = _VSTD::move(__other.__p_);
Howard Hinnant54da3382010-08-30 18:46:21 +00002046 return *this;
2047 }
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002048 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002049 void swap(packaged_task& __other) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:21 +00002050 {
2051 __f_.swap(__other.__f_);
2052 __p_.swap(__other.__p_);
2053 }
2054
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002055 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002056 bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
Howard Hinnant54da3382010-08-30 18:46:21 +00002057
2058 // result retrieval
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002059 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00002060 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:50 +00002069template<class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:21 +00002070void
Howard Hinnant99968442011-11-29 18:15:50 +00002071packaged_task<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __args)
Howard Hinnant54da3382010-08-30 18:46:21 +00002072{
Howard Hinnant54da3382010-08-30 18:46:21 +00002073 if (__p_.__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00002074 __throw_future_error(future_errc::no_state);
Howard Hinnant54da3382010-08-30 18:46:21 +00002075 if (__p_.__state_->__has_value())
Eric Fiselier423ca202015-10-02 21:25:15 +00002076 __throw_future_error(future_errc::promise_already_satisfied);
Marshall Clowa1899742015-09-03 15:11:32 +00002077#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant54da3382010-08-30 18:46:21 +00002078 try
2079 {
2080#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:19 +00002081 __p_.set_value(__f_(_VSTD::forward<_ArgTypes>(__args)...));
Howard Hinnant54da3382010-08-30 18:46:21 +00002082#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:50 +00002091template<class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:21 +00002092void
Howard Hinnant99968442011-11-29 18:15:50 +00002093packaged_task<_Rp(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
Howard Hinnant54da3382010-08-30 18:46:21 +00002094{
Howard Hinnant54da3382010-08-30 18:46:21 +00002095 if (__p_.__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00002096 __throw_future_error(future_errc::no_state);
Howard Hinnant54da3382010-08-30 18:46:21 +00002097 if (__p_.__state_->__has_value())
Eric Fiselier423ca202015-10-02 21:25:15 +00002098 __throw_future_error(future_errc::promise_already_satisfied);
Marshall Clowa1899742015-09-03 15:11:32 +00002099#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant54da3382010-08-30 18:46:21 +00002100 try
2101 {
2102#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:19 +00002103 __p_.set_value_at_thread_exit(__f_(_VSTD::forward<_ArgTypes>(__args)...));
Howard Hinnant54da3382010-08-30 18:46:21 +00002104#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:50 +00002113template<class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:21 +00002114void
Howard Hinnant99968442011-11-29 18:15:50 +00002115packaged_task<_Rp(_ArgTypes...)>::reset()
Howard Hinnant54da3382010-08-30 18:46:21 +00002116{
Howard Hinnant7de47902010-11-30 20:23:32 +00002117 if (!valid())
Eric Fiselier423ca202015-10-02 21:25:15 +00002118 __throw_future_error(future_errc::no_state);
Howard Hinnant54da3382010-08-30 18:46:21 +00002119 __p_ = promise<result_type>();
2120}
2121
2122template<class ..._ArgTypes>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002123class _LIBCPP_TYPE_VIS_ONLY packaged_task<void(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:21 +00002124{
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:26 +00002134 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002135 packaged_task() _NOEXCEPT : __p_(nullptr) {}
Marshall Clow5f2d5b92013-10-12 22:49:17 +00002136 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:26 +00002145 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00002146 explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
Marshall Clow5f2d5b92013-10-12 22:49:17 +00002147 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:26 +00002156 _LIBCPP_INLINE_VISIBILITY
Marshall Clow5706c372015-06-30 18:28:35 +00002157 packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
Howard Hinnant99968442011-11-29 18:15:50 +00002158 : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)),
Howard Hinnant54da3382010-08-30 18:46:21 +00002159 __p_(allocator_arg, __a) {}
2160 // ~packaged_task() = default;
2161
2162 // no copy
Howard Hinnant8131a012012-07-21 19:34:12 +00002163 packaged_task(const packaged_task&) = delete;
2164 packaged_task& operator=(const packaged_task&) = delete;
Howard Hinnant54da3382010-08-30 18:46:21 +00002165
2166 // move support
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002167 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002168 packaged_task(packaged_task&& __other) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002169 : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002170 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002171 packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:21 +00002172 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002173 __f_ = _VSTD::move(__other.__f_);
2174 __p_ = _VSTD::move(__other.__p_);
Howard Hinnant54da3382010-08-30 18:46:21 +00002175 return *this;
2176 }
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002177 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002178 void swap(packaged_task& __other) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:21 +00002179 {
2180 __f_.swap(__other.__f_);
2181 __p_.swap(__other.__p_);
2182 }
2183
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002184 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002185 bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
Howard Hinnant54da3382010-08-30 18:46:21 +00002186
2187 // result retrieval
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002188 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00002189 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:21 +00002202 if (__p_.__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00002203 __throw_future_error(future_errc::no_state);
Howard Hinnant54da3382010-08-30 18:46:21 +00002204 if (__p_.__state_->__has_value())
Eric Fiselier423ca202015-10-02 21:25:15 +00002205 __throw_future_error(future_errc::promise_already_satisfied);
Marshall Clowa1899742015-09-03 15:11:32 +00002206#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant54da3382010-08-30 18:46:21 +00002207 try
2208 {
2209#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:19 +00002210 __f_(_VSTD::forward<_ArgTypes>(__args)...);
Howard Hinnant54da3382010-08-30 18:46:21 +00002211 __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:21 +00002225 if (__p_.__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00002226 __throw_future_error(future_errc::no_state);
Howard Hinnant54da3382010-08-30 18:46:21 +00002227 if (__p_.__state_->__has_value())
Eric Fiselier423ca202015-10-02 21:25:15 +00002228 __throw_future_error(future_errc::promise_already_satisfied);
Marshall Clowa1899742015-09-03 15:11:32 +00002229#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant54da3382010-08-30 18:46:21 +00002230 try
2231 {
2232#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:19 +00002233 __f_(_VSTD::forward<_ArgTypes>(__args)...);
Howard Hinnant54da3382010-08-30 18:46:21 +00002234 __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:32 +00002248 if (!valid())
Eric Fiselier423ca202015-10-02 21:25:15 +00002249 __throw_future_error(future_errc::no_state);
Howard Hinnant54da3382010-08-30 18:46:21 +00002250 __p_ = promise<result_type>();
2251}
2252
2253template <class _Callable>
2254inline _LIBCPP_INLINE_VISIBILITY
2255void
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002256swap(packaged_task<_Callable>& __x, packaged_task<_Callable>& __y) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:21 +00002257{
2258 __x.swap(__y);
2259}
2260
2261template <class _Callable, class _Alloc>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002262struct _LIBCPP_TYPE_VIS_ONLY uses_allocator<packaged_task<_Callable>, _Alloc>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002263 : public true_type {};
Howard Hinnant54da3382010-08-30 18:46:21 +00002264
Howard Hinnant99968442011-11-29 18:15:50 +00002265template <class _Rp, class _Fp>
2266future<_Rp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00002267#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00002268__make_deferred_assoc_state(_Fp&& __f)
Howard Hinnant54da3382010-08-30 18:46:21 +00002269#else
Howard Hinnant99968442011-11-29 18:15:50 +00002270__make_deferred_assoc_state(_Fp __f)
Howard Hinnant54da3382010-08-30 18:46:21 +00002271#endif
2272{
Howard Hinnant99968442011-11-29 18:15:50 +00002273 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:21 +00002276}
2277
Howard Hinnant99968442011-11-29 18:15:50 +00002278template <class _Rp, class _Fp>
2279future<_Rp>
Howard Hinnant57cff292011-05-19 15:05:04 +00002280#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00002281__make_async_assoc_state(_Fp&& __f)
Howard Hinnant57cff292011-05-19 15:05:04 +00002282#else
Howard Hinnant99968442011-11-29 18:15:50 +00002283__make_async_assoc_state(_Fp __f)
Howard Hinnant57cff292011-05-19 15:05:04 +00002284#endif
2285{
Howard Hinnant99968442011-11-29 18:15:50 +00002286 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:04 +00002290}
2291
Howard Hinnant99968442011-11-29 18:15:50 +00002292template <class _Fp, class... _Args>
Howard Hinnant57cff292011-05-19 15:05:04 +00002293class __async_func
2294{
Howard Hinnant99968442011-11-29 18:15:50 +00002295 tuple<_Fp, _Args...> __f_;
Howard Hinnant57cff292011-05-19 15:05:04 +00002296
2297public:
Howard Hinnant99968442011-11-29 18:15:50 +00002298 typedef typename __invoke_of<_Fp, _Args...>::type _Rp;
Howard Hinnant57cff292011-05-19 15:05:04 +00002299
2300 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00002301 explicit __async_func(_Fp&& __f, _Args&&... __args)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002302 : __f_(_VSTD::move(__f), _VSTD::move(__args)...) {}
Howard Hinnant57cff292011-05-19 15:05:04 +00002303
2304 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0949eed2011-06-30 21:18:19 +00002305 __async_func(__async_func&& __f) : __f_(_VSTD::move(__f.__f_)) {}
Howard Hinnant57cff292011-05-19 15:05:04 +00002306
Howard Hinnant99968442011-11-29 18:15:50 +00002307 _Rp operator()()
Howard Hinnant57cff292011-05-19 15:05:04 +00002308 {
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:50 +00002314 _Rp
Howard Hinnant57cff292011-05-19 15:05:04 +00002315 __execute(__tuple_indices<_Indices...>)
2316 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002317 return __invoke(_VSTD::move(_VSTD::get<0>(__f_)), _VSTD::move(_VSTD::get<_Indices>(__f_))...);
Howard Hinnant57cff292011-05-19 15:05:04 +00002318 }
2319};
2320
Marshall Clow3b3108e2013-11-03 22:06:53 +00002321inline _LIBCPP_INLINE_VISIBILITY bool __does_policy_contain(launch __policy, launch __value )
Marshall Clowad2a6002013-11-03 15:43:35 +00002322{ return (int(__policy) & int(__value)) != 0; }
2323
Howard Hinnant99968442011-11-29 18:15:50 +00002324template <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:21 +00002327{
Howard Hinnant99968442011-11-29 18:15:50 +00002328 typedef __async_func<typename decay<_Fp>::type, typename decay<_Args>::type...> _BF;
2329 typedef typename _BF::_Rp _Rp;
Marshall Clowad2a6002013-11-03 15:43:35 +00002330
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:19 +00002337 __decay_copy(_VSTD::forward<_Args>(__args))...));
Marshall Clowad2a6002013-11-03 15:43:35 +00002338#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:19 +00002345 __decay_copy(_VSTD::forward<_Args>(__args))...));
Marshall Clowad2a6002013-11-03 15:43:35 +00002346 return future<_Rp>{};
Howard Hinnant54da3382010-08-30 18:46:21 +00002347}
2348
Howard Hinnant99968442011-11-29 18:15:50 +00002349template <class _Fp, class... _Args>
Howard Hinnant54da3382010-08-30 18:46:21 +00002350inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00002351future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
2352async(_Fp&& __f, _Args&&... __args)
Howard Hinnant54da3382010-08-30 18:46:21 +00002353{
Howard Hinnant99968442011-11-29 18:15:50 +00002354 return _VSTD::async(launch::any, _VSTD::forward<_Fp>(__f),
Howard Hinnant0949eed2011-06-30 21:18:19 +00002355 _VSTD::forward<_Args>(__args)...);
Howard Hinnant54da3382010-08-30 18:46:21 +00002356}
2357
2358#endif // _LIBCPP_HAS_NO_VARIADICS
2359
Howard Hinnante6e4d012010-09-03 21:46:37 +00002360// shared_future
2361
Howard Hinnant99968442011-11-29 18:15:50 +00002362template <class _Rp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002363class _LIBCPP_TYPE_VIS_ONLY shared_future
Howard Hinnant99be8232010-09-03 18:39:25 +00002364{
Howard Hinnant99968442011-11-29 18:15:50 +00002365 __assoc_state<_Rp>* __state_;
Howard Hinnant99be8232010-09-03 18:39:25 +00002366
2367public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002368 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002369 shared_future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002370 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002371 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2372 {if (__state_) __state_->__add_shared();}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002373#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002374 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002375 shared_future(future<_Rp>&& __f) _NOEXCEPT : __state_(__f.__state_)
Howard Hinnant99be8232010-09-03 18:39:25 +00002376 {__f.__state_ = nullptr;}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002377 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002378 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
Howard Hinnant99be8232010-09-03 18:39:25 +00002379 {__rhs.__state_ = nullptr;}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002380#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:25 +00002381 ~shared_future();
2382 shared_future& operator=(const shared_future& __rhs);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002383#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002384 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002385 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:25 +00002386 {
2387 shared_future(std::move(__rhs)).swap(*this);
2388 return *this;
2389 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002390#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:25 +00002391
2392 // retrieving the value
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002393 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00002394 const _Rp& get() const {return __state_->copy();}
Howard Hinnant99be8232010-09-03 18:39:25 +00002395
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002396 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002397 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant99be8232010-09-03 18:39:25 +00002398
2399 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002400 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002401 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant99be8232010-09-03 18:39:25 +00002402
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002403 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002404 void wait() const {__state_->wait();}
2405 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002406 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002407 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:26 +00002411 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002412 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:50 +00002417template <class _Rp>
2418shared_future<_Rp>::~shared_future()
Howard Hinnant99be8232010-09-03 18:39:25 +00002419{
2420 if (__state_)
2421 __state_->__release_shared();
2422}
2423
Howard Hinnant99968442011-11-29 18:15:50 +00002424template <class _Rp>
2425shared_future<_Rp>&
2426shared_future<_Rp>::operator=(const shared_future& __rhs)
Howard Hinnant99be8232010-09-03 18:39:25 +00002427{
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:50 +00002436template <class _Rp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002437class _LIBCPP_TYPE_VIS_ONLY shared_future<_Rp&>
Howard Hinnant99be8232010-09-03 18:39:25 +00002438{
Howard Hinnant99968442011-11-29 18:15:50 +00002439 __assoc_state<_Rp&>* __state_;
Howard Hinnant99be8232010-09-03 18:39:25 +00002440
2441public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002442 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002443 shared_future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002444 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002445 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2446 {if (__state_) __state_->__add_shared();}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002447#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002448 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002449 shared_future(future<_Rp&>&& __f) _NOEXCEPT : __state_(__f.__state_)
Howard Hinnant99be8232010-09-03 18:39:25 +00002450 {__f.__state_ = nullptr;}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002451 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002452 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
Howard Hinnant99be8232010-09-03 18:39:25 +00002453 {__rhs.__state_ = nullptr;}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002454#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:25 +00002455 ~shared_future();
2456 shared_future& operator=(const shared_future& __rhs);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002457#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002458 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002459 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:25 +00002460 {
2461 shared_future(std::move(__rhs)).swap(*this);
2462 return *this;
2463 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002464#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:25 +00002465
2466 // retrieving the value
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002467 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00002468 _Rp& get() const {return __state_->copy();}
Howard Hinnant99be8232010-09-03 18:39:25 +00002469
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002470 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002471 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant99be8232010-09-03 18:39:25 +00002472
2473 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002474 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002475 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant99be8232010-09-03 18:39:25 +00002476
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002477 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002478 void wait() const {__state_->wait();}
2479 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002480 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002481 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:26 +00002485 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002486 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:50 +00002491template <class _Rp>
2492shared_future<_Rp&>::~shared_future()
Howard Hinnant99be8232010-09-03 18:39:25 +00002493{
2494 if (__state_)
2495 __state_->__release_shared();
2496}
2497
Howard Hinnant99968442011-11-29 18:15:50 +00002498template <class _Rp>
2499shared_future<_Rp&>&
2500shared_future<_Rp&>::operator=(const shared_future& __rhs)
Howard Hinnant99be8232010-09-03 18:39:25 +00002501{
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:19 +00002511class _LIBCPP_TYPE_VIS shared_future<void>
Howard Hinnant99be8232010-09-03 18:39:25 +00002512{
2513 __assoc_sub_state* __state_;
2514
2515public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002516 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002517 shared_future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002518 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002519 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2520 {if (__state_) __state_->__add_shared();}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002521#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002522 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002523 shared_future(future<void>&& __f) _NOEXCEPT : __state_(__f.__state_)
Howard Hinnant99be8232010-09-03 18:39:25 +00002524 {__f.__state_ = nullptr;}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002525 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002526 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
Howard Hinnant99be8232010-09-03 18:39:25 +00002527 {__rhs.__state_ = nullptr;}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002528#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:25 +00002529 ~shared_future();
2530 shared_future& operator=(const shared_future& __rhs);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002531#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002532 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002533 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:25 +00002534 {
2535 shared_future(std::move(__rhs)).swap(*this);
2536 return *this;
2537 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002538#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:25 +00002539
2540 // retrieving the value
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002541 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002542 void get() const {__state_->copy();}
2543
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002544 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002545 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant99be8232010-09-03 18:39:25 +00002546
2547 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002548 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002549 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant99be8232010-09-03 18:39:25 +00002550
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002551 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002552 void wait() const {__state_->wait();}
2553 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002554 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002555 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:26 +00002559 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002560 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:50 +00002565template <class _Rp>
Howard Hinnant99be8232010-09-03 18:39:25 +00002566inline _LIBCPP_INLINE_VISIBILITY
2567void
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002568swap(shared_future<_Rp>& __x, shared_future<_Rp>& __y) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:25 +00002569{
2570 __x.swap(__y);
2571}
2572
Howard Hinnant99968442011-11-29 18:15:50 +00002573template <class _Rp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00002574inline
Howard Hinnant99968442011-11-29 18:15:50 +00002575shared_future<_Rp>
2576future<_Rp>::share()
Howard Hinnante6e4d012010-09-03 21:46:37 +00002577{
Howard Hinnant99968442011-11-29 18:15:50 +00002578 return shared_future<_Rp>(_VSTD::move(*this));
Howard Hinnante6e4d012010-09-03 21:46:37 +00002579}
2580
Howard Hinnant99968442011-11-29 18:15:50 +00002581template <class _Rp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00002582inline
Howard Hinnant99968442011-11-29 18:15:50 +00002583shared_future<_Rp&>
2584future<_Rp&>::share()
Howard Hinnante6e4d012010-09-03 21:46:37 +00002585{
Howard Hinnant99968442011-11-29 18:15:50 +00002586 return shared_future<_Rp&>(_VSTD::move(*this));
Howard Hinnant7de47902010-11-30 20:23:32 +00002587}
2588
Howard Hinnanta4451512010-12-02 16:45:21 +00002589#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2590
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00002591inline
Howard Hinnant7de47902010-11-30 20:23:32 +00002592shared_future<void>
2593future<void>::share()
2594{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002595 return shared_future<void>(_VSTD::move(*this));
Howard Hinnante6e4d012010-09-03 21:46:37 +00002596}
2597
Howard Hinnanta4451512010-12-02 16:45:21 +00002598#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2599
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002600_LIBCPP_END_NAMESPACE_STD
2601
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +00002602#endif // !_LIBCPP_HAS_NO_THREADS
2603
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002604#endif // _LIBCPP_FUTURE