blob: e38876758e1381528fecff2ccaf50d71a2dc269b [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
Marshall Clow5ec20df2016-11-14 18:56:24 +000053 explicit future_error(future_errc); // C++17
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;
Marshall Clow19cd3fd2017-01-25 20:14:03 +0000159 shared_future<R> share() noexcept;
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;
Marshall Clow9bb0cca2017-01-24 23:28:25 +0000186 shared_future<R&> share() noexcept;
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;
Marshall Clow9bb0cca2017-01-24 23:28:25 +0000213 shared_future<void> share() noexcept;
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:
Eric Fiselier68db6cd2016-06-01 21:05:53 +0000325 typedef R result_type; // extension
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000326
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 <>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000394struct _LIBCPP_TEMPLATE_VIS 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 <>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000398struct _LIBCPP_TEMPLATE_VIS 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
Mehdi Amini907c1192017-05-04 17:08:54 +0000502class _LIBCPP_EXCEPTION_ABI _LIBCPP_AVAILABILITY_FUTURE_ERROR 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);
Marshall Clow5ec20df2016-11-14 18:56:24 +0000508#if _LIBCPP_STD_VERS > 14
509 explicit future_error(future_errc _Ev) : logic_error(), __ec_(make_error_code(_Ev)) {}
510#endif
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000511 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +0000512 const error_code& code() const _NOEXCEPT {return __ec_;}
Howard Hinnantac6de542011-07-07 21:03:52 +0000513
514 virtual ~future_error() _NOEXCEPT;
Howard Hinnanta6521722010-08-25 17:32:05 +0000515};
516
Marshall Clow14c09a22016-08-25 15:09:01 +0000517_LIBCPP_NORETURN inline _LIBCPP_ALWAYS_INLINE
Mehdi Amini907c1192017-05-04 17:08:54 +0000518#ifndef _LIBCPP_NO_EXCEPTIONS
519_LIBCPP_AVAILABILITY_FUTURE_ERROR
520#endif
Eric Fiselier423ca202015-10-02 21:25:15 +0000521void __throw_future_error(future_errc _Ev)
Marshall Clowa1899742015-09-03 15:11:32 +0000522{
523#ifndef _LIBCPP_NO_EXCEPTIONS
524 throw future_error(make_error_code(_Ev));
525#else
Eric Fiselier2ab8f622016-12-24 01:56:25 +0000526 ((void)_Ev);
Marshall Clow14c09a22016-08-25 15:09:01 +0000527 _VSTD::abort();
Marshall Clowa1899742015-09-03 15:11:32 +0000528#endif
529}
530
Mehdi Amini907c1192017-05-04 17:08:54 +0000531class _LIBCPP_TYPE_VIS _LIBCPP_AVAILABILITY_FUTURE __assoc_sub_state
Howard Hinnant47499b12010-08-27 20:10:19 +0000532 : public __shared_count
533{
534protected:
535 exception_ptr __exception_;
536 mutable mutex __mut_;
537 mutable condition_variable __cv_;
538 unsigned __state_;
539
Howard Hinnant1694d232011-05-28 14:41:13 +0000540 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant54da3382010-08-30 18:46:21 +0000541 void __sub_wait(unique_lock<mutex>& __lk);
Howard Hinnant47499b12010-08-27 20:10:19 +0000542public:
543 enum
544 {
545 __constructed = 1,
546 __future_attached = 2,
547 ready = 4,
548 deferred = 8
549 };
550
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000551 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000552 __assoc_sub_state() : __state_(0) {}
553
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000554 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000555 bool __has_value() const
556 {return (__state_ & __constructed) || (__exception_ != nullptr);}
557
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000558 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1b031c92013-01-14 20:01:24 +0000559 void __set_future_attached()
560 {
561 lock_guard<mutex> __lk(__mut_);
562 __state_ |= __future_attached;
563 }
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000564 _LIBCPP_INLINE_VISIBILITY
Marshall Clow9de3d4c2013-10-13 01:02:45 +0000565 bool __has_future_attached() const {return (__state_ & __future_attached) != 0;}
Howard Hinnant47499b12010-08-27 20:10:19 +0000566
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000567 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +0000568 void __set_deferred() {__state_ |= deferred;}
569
Howard Hinnant47499b12010-08-27 20:10:19 +0000570 void __make_ready();
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000571 _LIBCPP_INLINE_VISIBILITY
Marshall Clow9de3d4c2013-10-13 01:02:45 +0000572 bool __is_ready() const {return (__state_ & ready) != 0;}
Howard Hinnant47499b12010-08-27 20:10:19 +0000573
574 void set_value();
575 void set_value_at_thread_exit();
576
577 void set_exception(exception_ptr __p);
578 void set_exception_at_thread_exit(exception_ptr __p);
579
580 void copy();
581
Howard Hinnant54da3382010-08-30 18:46:21 +0000582 void wait();
Howard Hinnant47499b12010-08-27 20:10:19 +0000583 template <class _Rep, class _Period>
584 future_status
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000585 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000586 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const;
587 template <class _Clock, class _Duration>
Shoaib Meenai6b734922017-03-02 03:22:18 +0000588 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Howard Hinnant47499b12010-08-27 20:10:19 +0000589 future_status
590 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const;
Howard Hinnant54da3382010-08-30 18:46:21 +0000591
592 virtual void __execute();
Howard Hinnant47499b12010-08-27 20:10:19 +0000593};
594
Howard Hinnantf39daa82010-08-28 21:01:06 +0000595template <class _Clock, class _Duration>
596future_status
597__assoc_sub_state::wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
598{
599 unique_lock<mutex> __lk(__mut_);
Howard Hinnant54da3382010-08-30 18:46:21 +0000600 if (__state_ & deferred)
601 return future_status::deferred;
602 while (!(__state_ & ready) && _Clock::now() < __abs_time)
Howard Hinnantf39daa82010-08-28 21:01:06 +0000603 __cv_.wait_until(__lk, __abs_time);
604 if (__state_ & ready)
605 return future_status::ready;
Howard Hinnantf39daa82010-08-28 21:01:06 +0000606 return future_status::timeout;
607}
608
609template <class _Rep, class _Period>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000610inline
Howard Hinnantf39daa82010-08-28 21:01:06 +0000611future_status
612__assoc_sub_state::wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
613{
Howard Hinnantf8f85212010-11-20 19:16:30 +0000614 return wait_until(chrono::steady_clock::now() + __rel_time);
Howard Hinnantf39daa82010-08-28 21:01:06 +0000615}
616
Howard Hinnant99968442011-11-29 18:15:50 +0000617template <class _Rp>
Mehdi Amini907c1192017-05-04 17:08:54 +0000618class _LIBCPP_AVAILABILITY_FUTURE __assoc_state
Howard Hinnant47499b12010-08-27 20:10:19 +0000619 : public __assoc_sub_state
620{
621 typedef __assoc_sub_state base;
Howard Hinnant99968442011-11-29 18:15:50 +0000622 typedef typename aligned_storage<sizeof(_Rp), alignment_of<_Rp>::value>::type _Up;
Howard Hinnant47499b12010-08-27 20:10:19 +0000623protected:
Howard Hinnant99968442011-11-29 18:15:50 +0000624 _Up __value_;
Howard Hinnant47499b12010-08-27 20:10:19 +0000625
Howard Hinnant1694d232011-05-28 14:41:13 +0000626 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant47499b12010-08-27 20:10:19 +0000627public:
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(_Arg&& __arg);
632#else
633 void set_value(_Arg& __arg);
634#endif
635
636 template <class _Arg>
Howard Hinnant73d21a42010-09-04 23:28:19 +0000637#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +0000638 void set_value_at_thread_exit(_Arg&& __arg);
639#else
640 void set_value_at_thread_exit(_Arg& __arg);
641#endif
642
Howard Hinnant99968442011-11-29 18:15:50 +0000643 _Rp move();
644 typename add_lvalue_reference<_Rp>::type copy();
Howard Hinnant47499b12010-08-27 20:10:19 +0000645};
646
Howard Hinnant99968442011-11-29 18:15:50 +0000647template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +0000648void
Howard Hinnant99968442011-11-29 18:15:50 +0000649__assoc_state<_Rp>::__on_zero_shared() _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +0000650{
651 if (this->__state_ & base::__constructed)
Howard Hinnant99968442011-11-29 18:15:50 +0000652 reinterpret_cast<_Rp*>(&__value_)->~_Rp();
Howard Hinnant47499b12010-08-27 20:10:19 +0000653 delete this;
654}
655
Howard Hinnant99968442011-11-29 18:15:50 +0000656template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +0000657template <class _Arg>
Mehdi Amini907c1192017-05-04 17:08:54 +0000658_LIBCPP_AVAILABILITY_FUTURE
Howard Hinnant47499b12010-08-27 20:10:19 +0000659void
Howard Hinnant73d21a42010-09-04 23:28:19 +0000660#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +0000661__assoc_state<_Rp>::set_value(_Arg&& __arg)
Howard Hinnant47499b12010-08-27 20:10:19 +0000662#else
Howard Hinnant99968442011-11-29 18:15:50 +0000663__assoc_state<_Rp>::set_value(_Arg& __arg)
Howard Hinnant47499b12010-08-27 20:10:19 +0000664#endif
665{
666 unique_lock<mutex> __lk(this->__mut_);
667 if (this->__has_value())
Eric Fiselier423ca202015-10-02 21:25:15 +0000668 __throw_future_error(future_errc::promise_already_satisfied);
Howard Hinnant99968442011-11-29 18:15:50 +0000669 ::new(&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
Howard Hinnant47499b12010-08-27 20:10:19 +0000670 this->__state_ |= base::__constructed | base::ready;
Howard Hinnant47499b12010-08-27 20:10:19 +0000671 __cv_.notify_all();
672}
673
Howard Hinnant99968442011-11-29 18:15:50 +0000674template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +0000675template <class _Arg>
676void
Howard Hinnant73d21a42010-09-04 23:28:19 +0000677#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +0000678__assoc_state<_Rp>::set_value_at_thread_exit(_Arg&& __arg)
Howard Hinnant47499b12010-08-27 20:10:19 +0000679#else
Howard Hinnant99968442011-11-29 18:15:50 +0000680__assoc_state<_Rp>::set_value_at_thread_exit(_Arg& __arg)
Howard Hinnant47499b12010-08-27 20:10:19 +0000681#endif
682{
683 unique_lock<mutex> __lk(this->__mut_);
684 if (this->__has_value())
Eric Fiselier423ca202015-10-02 21:25:15 +0000685 __throw_future_error(future_errc::promise_already_satisfied);
Howard Hinnant99968442011-11-29 18:15:50 +0000686 ::new(&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
Howard Hinnant47499b12010-08-27 20:10:19 +0000687 this->__state_ |= base::__constructed;
Howard Hinnant5306d682010-10-14 19:18:04 +0000688 __thread_local_data()->__make_ready_at_thread_exit(this);
Howard Hinnant47499b12010-08-27 20:10:19 +0000689}
690
Howard Hinnant99968442011-11-29 18:15:50 +0000691template <class _Rp>
692_Rp
693__assoc_state<_Rp>::move()
Howard Hinnant47499b12010-08-27 20:10:19 +0000694{
695 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant54da3382010-08-30 18:46:21 +0000696 this->__sub_wait(__lk);
Howard Hinnant47499b12010-08-27 20:10:19 +0000697 if (this->__exception_ != nullptr)
698 rethrow_exception(this->__exception_);
Howard Hinnant99968442011-11-29 18:15:50 +0000699 return _VSTD::move(*reinterpret_cast<_Rp*>(&__value_));
Howard Hinnant47499b12010-08-27 20:10:19 +0000700}
701
Howard Hinnant99968442011-11-29 18:15:50 +0000702template <class _Rp>
703typename add_lvalue_reference<_Rp>::type
704__assoc_state<_Rp>::copy()
Howard Hinnant47499b12010-08-27 20:10:19 +0000705{
706 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant54da3382010-08-30 18:46:21 +0000707 this->__sub_wait(__lk);
Howard Hinnant47499b12010-08-27 20:10:19 +0000708 if (this->__exception_ != nullptr)
709 rethrow_exception(this->__exception_);
Howard Hinnant99968442011-11-29 18:15:50 +0000710 return *reinterpret_cast<_Rp*>(&__value_);
Howard Hinnant47499b12010-08-27 20:10:19 +0000711}
712
Howard Hinnant99968442011-11-29 18:15:50 +0000713template <class _Rp>
Mehdi Amini907c1192017-05-04 17:08:54 +0000714class _LIBCPP_AVAILABILITY_FUTURE __assoc_state<_Rp&>
Howard Hinnantf39daa82010-08-28 21:01:06 +0000715 : public __assoc_sub_state
716{
717 typedef __assoc_sub_state base;
Howard Hinnant99968442011-11-29 18:15:50 +0000718 typedef _Rp* _Up;
Howard Hinnantf39daa82010-08-28 21:01:06 +0000719protected:
Howard Hinnant99968442011-11-29 18:15:50 +0000720 _Up __value_;
Howard Hinnantf39daa82010-08-28 21:01:06 +0000721
Howard Hinnant1694d232011-05-28 14:41:13 +0000722 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnantf39daa82010-08-28 21:01:06 +0000723public:
724
Howard Hinnant99968442011-11-29 18:15:50 +0000725 void set_value(_Rp& __arg);
726 void set_value_at_thread_exit(_Rp& __arg);
Howard Hinnantf39daa82010-08-28 21:01:06 +0000727
Howard Hinnant99968442011-11-29 18:15:50 +0000728 _Rp& copy();
Howard Hinnantf39daa82010-08-28 21:01:06 +0000729};
730
Howard Hinnant99968442011-11-29 18:15:50 +0000731template <class _Rp>
Howard Hinnantf39daa82010-08-28 21:01:06 +0000732void
Howard Hinnant99968442011-11-29 18:15:50 +0000733__assoc_state<_Rp&>::__on_zero_shared() _NOEXCEPT
Howard Hinnantf39daa82010-08-28 21:01:06 +0000734{
735 delete this;
736}
737
Howard Hinnant99968442011-11-29 18:15:50 +0000738template <class _Rp>
Howard Hinnantf39daa82010-08-28 21:01:06 +0000739void
Howard Hinnant99968442011-11-29 18:15:50 +0000740__assoc_state<_Rp&>::set_value(_Rp& __arg)
Howard Hinnantf39daa82010-08-28 21:01:06 +0000741{
742 unique_lock<mutex> __lk(this->__mut_);
743 if (this->__has_value())
Eric Fiselier423ca202015-10-02 21:25:15 +0000744 __throw_future_error(future_errc::promise_already_satisfied);
Howard Hinnanta4e87ab2013-08-08 18:38:55 +0000745 __value_ = _VSTD::addressof(__arg);
Howard Hinnantf39daa82010-08-28 21:01:06 +0000746 this->__state_ |= base::__constructed | base::ready;
Howard Hinnantf39daa82010-08-28 21:01:06 +0000747 __cv_.notify_all();
748}
749
Howard Hinnant99968442011-11-29 18:15:50 +0000750template <class _Rp>
Howard Hinnantf39daa82010-08-28 21:01:06 +0000751void
Howard Hinnant99968442011-11-29 18:15:50 +0000752__assoc_state<_Rp&>::set_value_at_thread_exit(_Rp& __arg)
Howard Hinnantf39daa82010-08-28 21:01:06 +0000753{
754 unique_lock<mutex> __lk(this->__mut_);
755 if (this->__has_value())
Eric Fiselier423ca202015-10-02 21:25:15 +0000756 __throw_future_error(future_errc::promise_already_satisfied);
Howard Hinnanta4e87ab2013-08-08 18:38:55 +0000757 __value_ = _VSTD::addressof(__arg);
Howard Hinnantf39daa82010-08-28 21:01:06 +0000758 this->__state_ |= base::__constructed;
Howard Hinnant5306d682010-10-14 19:18:04 +0000759 __thread_local_data()->__make_ready_at_thread_exit(this);
Howard Hinnantf39daa82010-08-28 21:01:06 +0000760}
761
Howard Hinnant99968442011-11-29 18:15:50 +0000762template <class _Rp>
763_Rp&
764__assoc_state<_Rp&>::copy()
Howard Hinnantf39daa82010-08-28 21:01:06 +0000765{
766 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant54da3382010-08-30 18:46:21 +0000767 this->__sub_wait(__lk);
Howard Hinnantf39daa82010-08-28 21:01:06 +0000768 if (this->__exception_ != nullptr)
769 rethrow_exception(this->__exception_);
770 return *__value_;
771}
772
Howard Hinnant99968442011-11-29 18:15:50 +0000773template <class _Rp, class _Alloc>
Mehdi Amini907c1192017-05-04 17:08:54 +0000774class _LIBCPP_AVAILABILITY_FUTURE __assoc_state_alloc
Howard Hinnant99968442011-11-29 18:15:50 +0000775 : public __assoc_state<_Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +0000776{
Howard Hinnant99968442011-11-29 18:15:50 +0000777 typedef __assoc_state<_Rp> base;
Howard Hinnant47499b12010-08-27 20:10:19 +0000778 _Alloc __alloc_;
779
Howard Hinnant1694d232011-05-28 14:41:13 +0000780 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant47499b12010-08-27 20:10:19 +0000781public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000782 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000783 explicit __assoc_state_alloc(const _Alloc& __a)
784 : __alloc_(__a) {}
785};
786
Howard Hinnant99968442011-11-29 18:15:50 +0000787template <class _Rp, class _Alloc>
Howard Hinnant47499b12010-08-27 20:10:19 +0000788void
Howard Hinnant99968442011-11-29 18:15:50 +0000789__assoc_state_alloc<_Rp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +0000790{
791 if (this->__state_ & base::__constructed)
Howard Hinnanta4e87ab2013-08-08 18:38:55 +0000792 reinterpret_cast<_Rp*>(_VSTD::addressof(this->__value_))->~_Rp();
Eric Fiselier8492cd82015-02-05 23:01:40 +0000793 typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
794 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4d2413c2014-10-23 06:24:45 +0000795 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselier8492cd82015-02-05 23:01:40 +0000796 _Al __a(__alloc_);
Howard Hinnant47499b12010-08-27 20:10:19 +0000797 this->~__assoc_state_alloc();
Eric Fiselier4d2413c2014-10-23 06:24:45 +0000798 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnant47499b12010-08-27 20:10:19 +0000799}
800
Howard Hinnant99968442011-11-29 18:15:50 +0000801template <class _Rp, class _Alloc>
Mehdi Amini907c1192017-05-04 17:08:54 +0000802class _LIBCPP_AVAILABILITY_FUTURE __assoc_state_alloc<_Rp&, _Alloc>
Howard Hinnant99968442011-11-29 18:15:50 +0000803 : public __assoc_state<_Rp&>
Howard Hinnantf39daa82010-08-28 21:01:06 +0000804{
Howard Hinnant99968442011-11-29 18:15:50 +0000805 typedef __assoc_state<_Rp&> base;
Howard Hinnantf39daa82010-08-28 21:01:06 +0000806 _Alloc __alloc_;
807
Howard Hinnant1694d232011-05-28 14:41:13 +0000808 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnantf39daa82010-08-28 21:01:06 +0000809public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000810 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39daa82010-08-28 21:01:06 +0000811 explicit __assoc_state_alloc(const _Alloc& __a)
812 : __alloc_(__a) {}
813};
814
Howard Hinnant99968442011-11-29 18:15:50 +0000815template <class _Rp, class _Alloc>
Howard Hinnantf39daa82010-08-28 21:01:06 +0000816void
Howard Hinnant99968442011-11-29 18:15:50 +0000817__assoc_state_alloc<_Rp&, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantf39daa82010-08-28 21:01:06 +0000818{
Eric Fiselier8492cd82015-02-05 23:01:40 +0000819 typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
820 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4d2413c2014-10-23 06:24:45 +0000821 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselier8492cd82015-02-05 23:01:40 +0000822 _Al __a(__alloc_);
Howard Hinnantf39daa82010-08-28 21:01:06 +0000823 this->~__assoc_state_alloc();
Eric Fiselier4d2413c2014-10-23 06:24:45 +0000824 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantf39daa82010-08-28 21:01:06 +0000825}
826
Howard Hinnant47499b12010-08-27 20:10:19 +0000827template <class _Alloc>
Mehdi Amini907c1192017-05-04 17:08:54 +0000828class _LIBCPP_AVAILABILITY_FUTURE __assoc_sub_state_alloc
Howard Hinnant47499b12010-08-27 20:10:19 +0000829 : public __assoc_sub_state
830{
831 typedef __assoc_sub_state base;
832 _Alloc __alloc_;
833
Howard Hinnant1694d232011-05-28 14:41:13 +0000834 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant47499b12010-08-27 20:10:19 +0000835public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000836 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000837 explicit __assoc_sub_state_alloc(const _Alloc& __a)
838 : __alloc_(__a) {}
839};
840
841template <class _Alloc>
842void
Howard Hinnant1694d232011-05-28 14:41:13 +0000843__assoc_sub_state_alloc<_Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +0000844{
Eric Fiselier8492cd82015-02-05 23:01:40 +0000845 typedef typename __allocator_traits_rebind<_Alloc, __assoc_sub_state_alloc>::type _Al;
846 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4d2413c2014-10-23 06:24:45 +0000847 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselier8492cd82015-02-05 23:01:40 +0000848 _Al __a(__alloc_);
Howard Hinnant47499b12010-08-27 20:10:19 +0000849 this->~__assoc_sub_state_alloc();
Eric Fiselier4d2413c2014-10-23 06:24:45 +0000850 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnant47499b12010-08-27 20:10:19 +0000851}
852
Howard Hinnant99968442011-11-29 18:15:50 +0000853template <class _Rp, class _Fp>
Mehdi Amini907c1192017-05-04 17:08:54 +0000854class _LIBCPP_AVAILABILITY_FUTURE __deferred_assoc_state
Howard Hinnant99968442011-11-29 18:15:50 +0000855 : public __assoc_state<_Rp>
Howard Hinnant54da3382010-08-30 18:46:21 +0000856{
Howard Hinnant99968442011-11-29 18:15:50 +0000857 typedef __assoc_state<_Rp> base;
Howard Hinnant54da3382010-08-30 18:46:21 +0000858
Howard Hinnant99968442011-11-29 18:15:50 +0000859 _Fp __func_;
Howard Hinnant54da3382010-08-30 18:46:21 +0000860
861public:
Howard Hinnant73d21a42010-09-04 23:28:19 +0000862#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000863 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000864 explicit __deferred_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:21 +0000865#endif
866
867 virtual void __execute();
868};
869
Howard Hinnant73d21a42010-09-04 23:28:19 +0000870#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21 +0000871
Howard Hinnant99968442011-11-29 18:15:50 +0000872template <class _Rp, class _Fp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000873inline
Howard Hinnant99968442011-11-29 18:15:50 +0000874__deferred_assoc_state<_Rp, _Fp>::__deferred_assoc_state(_Fp&& __f)
875 : __func_(_VSTD::forward<_Fp>(__f))
Howard Hinnant54da3382010-08-30 18:46:21 +0000876{
877 this->__set_deferred();
878}
879
Howard Hinnant73d21a42010-09-04 23:28:19 +0000880#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21 +0000881
Howard Hinnant99968442011-11-29 18:15:50 +0000882template <class _Rp, class _Fp>
Howard Hinnant54da3382010-08-30 18:46:21 +0000883void
Howard Hinnant99968442011-11-29 18:15:50 +0000884__deferred_assoc_state<_Rp, _Fp>::__execute()
Howard Hinnant54da3382010-08-30 18:46:21 +0000885{
886#ifndef _LIBCPP_NO_EXCEPTIONS
887 try
888 {
889#endif // _LIBCPP_NO_EXCEPTIONS
890 this->set_value(__func_());
891#ifndef _LIBCPP_NO_EXCEPTIONS
892 }
893 catch (...)
894 {
895 this->set_exception(current_exception());
896 }
897#endif // _LIBCPP_NO_EXCEPTIONS
898}
899
Howard Hinnant99968442011-11-29 18:15:50 +0000900template <class _Fp>
Mehdi Amini907c1192017-05-04 17:08:54 +0000901class _LIBCPP_AVAILABILITY_FUTURE __deferred_assoc_state<void, _Fp>
Howard Hinnant54da3382010-08-30 18:46:21 +0000902 : public __assoc_sub_state
903{
904 typedef __assoc_sub_state base;
905
Howard Hinnant99968442011-11-29 18:15:50 +0000906 _Fp __func_;
Howard Hinnant54da3382010-08-30 18:46:21 +0000907
908public:
Howard Hinnant73d21a42010-09-04 23:28:19 +0000909#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000910 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000911 explicit __deferred_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:21 +0000912#endif
913
914 virtual void __execute();
915};
916
Howard Hinnant73d21a42010-09-04 23:28:19 +0000917#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21 +0000918
Howard Hinnant99968442011-11-29 18:15:50 +0000919template <class _Fp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000920inline
Howard Hinnant99968442011-11-29 18:15:50 +0000921__deferred_assoc_state<void, _Fp>::__deferred_assoc_state(_Fp&& __f)
922 : __func_(_VSTD::forward<_Fp>(__f))
Howard Hinnant54da3382010-08-30 18:46:21 +0000923{
924 this->__set_deferred();
925}
926
Howard Hinnant73d21a42010-09-04 23:28:19 +0000927#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21 +0000928
Howard Hinnant99968442011-11-29 18:15:50 +0000929template <class _Fp>
Howard Hinnant54da3382010-08-30 18:46:21 +0000930void
Howard Hinnant99968442011-11-29 18:15:50 +0000931__deferred_assoc_state<void, _Fp>::__execute()
Howard Hinnant54da3382010-08-30 18:46:21 +0000932{
933#ifndef _LIBCPP_NO_EXCEPTIONS
934 try
935 {
936#endif // _LIBCPP_NO_EXCEPTIONS
937 __func_();
938 this->set_value();
939#ifndef _LIBCPP_NO_EXCEPTIONS
940 }
941 catch (...)
942 {
943 this->set_exception(current_exception());
944 }
945#endif // _LIBCPP_NO_EXCEPTIONS
946}
947
Howard Hinnant99968442011-11-29 18:15:50 +0000948template <class _Rp, class _Fp>
Mehdi Amini907c1192017-05-04 17:08:54 +0000949class _LIBCPP_AVAILABILITY_FUTURE __async_assoc_state
Howard Hinnant99968442011-11-29 18:15:50 +0000950 : public __assoc_state<_Rp>
Howard Hinnant57cff292011-05-19 15:05:04 +0000951{
Howard Hinnant99968442011-11-29 18:15:50 +0000952 typedef __assoc_state<_Rp> base;
Howard Hinnant57cff292011-05-19 15:05:04 +0000953
Howard Hinnant99968442011-11-29 18:15:50 +0000954 _Fp __func_;
Howard Hinnant57cff292011-05-19 15:05:04 +0000955
Howard Hinnant1694d232011-05-28 14:41:13 +0000956 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant57cff292011-05-19 15:05:04 +0000957public:
958#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000959 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000960 explicit __async_assoc_state(_Fp&& __f);
Howard Hinnant57cff292011-05-19 15:05:04 +0000961#endif
962
963 virtual void __execute();
964};
965
966#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
967
Howard Hinnant99968442011-11-29 18:15:50 +0000968template <class _Rp, class _Fp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000969inline
Howard Hinnant99968442011-11-29 18:15:50 +0000970__async_assoc_state<_Rp, _Fp>::__async_assoc_state(_Fp&& __f)
971 : __func_(_VSTD::forward<_Fp>(__f))
Howard Hinnant57cff292011-05-19 15:05:04 +0000972{
973}
974
975#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
976
Howard Hinnant99968442011-11-29 18:15:50 +0000977template <class _Rp, class _Fp>
Howard Hinnant57cff292011-05-19 15:05:04 +0000978void
Howard Hinnant99968442011-11-29 18:15:50 +0000979__async_assoc_state<_Rp, _Fp>::__execute()
Howard Hinnant57cff292011-05-19 15:05:04 +0000980{
981#ifndef _LIBCPP_NO_EXCEPTIONS
982 try
983 {
984#endif // _LIBCPP_NO_EXCEPTIONS
985 this->set_value(__func_());
986#ifndef _LIBCPP_NO_EXCEPTIONS
987 }
988 catch (...)
989 {
990 this->set_exception(current_exception());
991 }
992#endif // _LIBCPP_NO_EXCEPTIONS
993}
994
Howard Hinnant99968442011-11-29 18:15:50 +0000995template <class _Rp, class _Fp>
Howard Hinnant57cff292011-05-19 15:05:04 +0000996void
Howard Hinnant99968442011-11-29 18:15:50 +0000997__async_assoc_state<_Rp, _Fp>::__on_zero_shared() _NOEXCEPT
Howard Hinnant57cff292011-05-19 15:05:04 +0000998{
999 this->wait();
1000 base::__on_zero_shared();
1001}
1002
Howard Hinnant99968442011-11-29 18:15:50 +00001003template <class _Fp>
Mehdi Amini907c1192017-05-04 17:08:54 +00001004class _LIBCPP_AVAILABILITY_FUTURE __async_assoc_state<void, _Fp>
Howard Hinnant57cff292011-05-19 15:05:04 +00001005 : public __assoc_sub_state
1006{
1007 typedef __assoc_sub_state base;
1008
Howard Hinnant99968442011-11-29 18:15:50 +00001009 _Fp __func_;
Howard Hinnant57cff292011-05-19 15:05:04 +00001010
Howard Hinnant1694d232011-05-28 14:41:13 +00001011 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant57cff292011-05-19 15:05:04 +00001012public:
1013#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001014 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001015 explicit __async_assoc_state(_Fp&& __f);
Howard Hinnant57cff292011-05-19 15:05:04 +00001016#endif
1017
1018 virtual void __execute();
1019};
1020
1021#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1022
Howard Hinnant99968442011-11-29 18:15:50 +00001023template <class _Fp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001024inline
Howard Hinnant99968442011-11-29 18:15:50 +00001025__async_assoc_state<void, _Fp>::__async_assoc_state(_Fp&& __f)
1026 : __func_(_VSTD::forward<_Fp>(__f))
Howard Hinnant57cff292011-05-19 15:05:04 +00001027{
1028}
1029
1030#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1031
Howard Hinnant99968442011-11-29 18:15:50 +00001032template <class _Fp>
Howard Hinnant57cff292011-05-19 15:05:04 +00001033void
Howard Hinnant99968442011-11-29 18:15:50 +00001034__async_assoc_state<void, _Fp>::__execute()
Howard Hinnant57cff292011-05-19 15:05:04 +00001035{
1036#ifndef _LIBCPP_NO_EXCEPTIONS
1037 try
1038 {
1039#endif // _LIBCPP_NO_EXCEPTIONS
1040 __func_();
1041 this->set_value();
1042#ifndef _LIBCPP_NO_EXCEPTIONS
1043 }
1044 catch (...)
1045 {
1046 this->set_exception(current_exception());
1047 }
1048#endif // _LIBCPP_NO_EXCEPTIONS
1049}
1050
Howard Hinnant99968442011-11-29 18:15:50 +00001051template <class _Fp>
Howard Hinnant57cff292011-05-19 15:05:04 +00001052void
Howard Hinnant99968442011-11-29 18:15:50 +00001053__async_assoc_state<void, _Fp>::__on_zero_shared() _NOEXCEPT
Howard Hinnant57cff292011-05-19 15:05:04 +00001054{
1055 this->wait();
1056 base::__on_zero_shared();
1057}
1058
Eric Fiselierc3589a82017-01-04 23:56:00 +00001059template <class _Rp> class _LIBCPP_TEMPLATE_VIS promise;
1060template <class _Rp> class _LIBCPP_TEMPLATE_VIS shared_future;
Howard Hinnant47499b12010-08-27 20:10:19 +00001061
1062// future
1063
Eric Fiselierc3589a82017-01-04 23:56:00 +00001064template <class _Rp> class _LIBCPP_TEMPLATE_VIS future;
Howard Hinnant54da3382010-08-30 18:46:21 +00001065
Howard Hinnant99968442011-11-29 18:15:50 +00001066template <class _Rp, class _Fp>
1067future<_Rp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00001068#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00001069__make_deferred_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001070#else
Howard Hinnant99968442011-11-29 18:15:50 +00001071__make_deferred_assoc_state(_Fp __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001072#endif
1073
Howard Hinnant99968442011-11-29 18:15:50 +00001074template <class _Rp, class _Fp>
1075future<_Rp>
Howard Hinnant57cff292011-05-19 15:05:04 +00001076#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00001077__make_async_assoc_state(_Fp&& __f);
Howard Hinnant57cff292011-05-19 15:05:04 +00001078#else
Howard Hinnant99968442011-11-29 18:15:50 +00001079__make_async_assoc_state(_Fp __f);
Howard Hinnant57cff292011-05-19 15:05:04 +00001080#endif
1081
Howard Hinnant99968442011-11-29 18:15:50 +00001082template <class _Rp>
Mehdi Amini907c1192017-05-04 17:08:54 +00001083class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE future
Howard Hinnant47499b12010-08-27 20:10:19 +00001084{
Howard Hinnant99968442011-11-29 18:15:50 +00001085 __assoc_state<_Rp>* __state_;
Howard Hinnant47499b12010-08-27 20:10:19 +00001086
Howard Hinnant99968442011-11-29 18:15:50 +00001087 explicit future(__assoc_state<_Rp>* __state);
Howard Hinnant47499b12010-08-27 20:10:19 +00001088
1089 template <class> friend class promise;
Howard Hinnant99be8232010-09-03 18:39:25 +00001090 template <class> friend class shared_future;
Howard Hinnant54da3382010-08-30 18:46:21 +00001091
Howard Hinnant73d21a42010-09-04 23:28:19 +00001092#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00001093 template <class _R1, class _Fp>
1094 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1095 template <class _R1, class _Fp>
1096 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001097#else
Howard Hinnant99968442011-11-29 18:15:50 +00001098 template <class _R1, class _Fp>
1099 friend future<_R1> __make_deferred_assoc_state(_Fp __f);
1100 template <class _R1, class _Fp>
1101 friend future<_R1> __make_async_assoc_state(_Fp __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001102#endif
1103
Howard Hinnant47499b12010-08-27 20:10:19 +00001104public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001105 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001106 future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001107#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001108 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001109 future(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +00001110 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1111 future(const future&) = delete;
1112 future& operator=(const future&) = delete;
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001113 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001114 future& operator=(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +00001115 {
1116 future(std::move(__rhs)).swap(*this);
1117 return *this;
1118 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001119#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001120private:
1121 future(const future&);
1122 future& operator=(const future&);
1123public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001124#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001125 ~future();
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001126 _LIBCPP_INLINE_VISIBILITY
Marshall Clow9bb0cca2017-01-24 23:28:25 +00001127 shared_future<_Rp> share() _NOEXCEPT;
Howard Hinnant47499b12010-08-27 20:10:19 +00001128
1129 // retrieving the value
Howard Hinnant99968442011-11-29 18:15:50 +00001130 _Rp get();
Howard Hinnant47499b12010-08-27 20:10:19 +00001131
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001132 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001133 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:19 +00001134
1135 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001136 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001137 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant47499b12010-08-27 20:10:19 +00001138
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001139 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001140 void wait() const {__state_->wait();}
1141 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001142 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001143 future_status
1144 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1145 {return __state_->wait_for(__rel_time);}
1146 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001147 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001148 future_status
1149 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1150 {return __state_->wait_until(__abs_time);}
1151};
1152
Howard Hinnant99968442011-11-29 18:15:50 +00001153template <class _Rp>
1154future<_Rp>::future(__assoc_state<_Rp>* __state)
Howard Hinnant47499b12010-08-27 20:10:19 +00001155 : __state_(__state)
1156{
1157 if (__state_->__has_future_attached())
Eric Fiselier423ca202015-10-02 21:25:15 +00001158 __throw_future_error(future_errc::future_already_retrieved);
Howard Hinnant47499b12010-08-27 20:10:19 +00001159 __state_->__add_shared();
Howard Hinnant54da3382010-08-30 18:46:21 +00001160 __state_->__set_future_attached();
Howard Hinnant47499b12010-08-27 20:10:19 +00001161}
1162
Howard Hinnant54da3382010-08-30 18:46:21 +00001163struct __release_shared_count
1164{
1165 void operator()(__shared_count* p) {p->__release_shared();}
1166};
1167
Howard Hinnant99968442011-11-29 18:15:50 +00001168template <class _Rp>
1169future<_Rp>::~future()
Howard Hinnant47499b12010-08-27 20:10:19 +00001170{
1171 if (__state_)
1172 __state_->__release_shared();
1173}
1174
Howard Hinnant99968442011-11-29 18:15:50 +00001175template <class _Rp>
1176_Rp
1177future<_Rp>::get()
Howard Hinnant47499b12010-08-27 20:10:19 +00001178{
Howard Hinnant54da3382010-08-30 18:46:21 +00001179 unique_ptr<__shared_count, __release_shared_count> __(__state_);
Howard Hinnant99968442011-11-29 18:15:50 +00001180 __assoc_state<_Rp>* __s = __state_;
Howard Hinnant47499b12010-08-27 20:10:19 +00001181 __state_ = nullptr;
1182 return __s->move();
1183}
1184
Howard Hinnant99968442011-11-29 18:15:50 +00001185template <class _Rp>
Mehdi Amini907c1192017-05-04 17:08:54 +00001186class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE future<_Rp&>
Howard Hinnant47499b12010-08-27 20:10:19 +00001187{
Howard Hinnant99968442011-11-29 18:15:50 +00001188 __assoc_state<_Rp&>* __state_;
Howard Hinnant47499b12010-08-27 20:10:19 +00001189
Howard Hinnant99968442011-11-29 18:15:50 +00001190 explicit future(__assoc_state<_Rp&>* __state);
Howard Hinnant47499b12010-08-27 20:10:19 +00001191
1192 template <class> friend class promise;
Howard Hinnant99be8232010-09-03 18:39:25 +00001193 template <class> friend class shared_future;
Howard Hinnant54da3382010-08-30 18:46:21 +00001194
Howard Hinnant73d21a42010-09-04 23:28:19 +00001195#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00001196 template <class _R1, class _Fp>
1197 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1198 template <class _R1, class _Fp>
1199 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001200#else
Howard Hinnant99968442011-11-29 18:15:50 +00001201 template <class _R1, class _Fp>
1202 friend future<_R1> __make_deferred_assoc_state(_Fp __f);
1203 template <class _R1, class _Fp>
1204 friend future<_R1> __make_async_assoc_state(_Fp __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001205#endif
1206
Howard Hinnant47499b12010-08-27 20:10:19 +00001207public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001208 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001209 future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001210#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001211 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001212 future(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +00001213 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1214 future(const future&) = delete;
1215 future& operator=(const future&) = delete;
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001216 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001217 future& operator=(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +00001218 {
1219 future(std::move(__rhs)).swap(*this);
1220 return *this;
1221 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001222#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001223private:
1224 future(const future&);
1225 future& operator=(const future&);
1226public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001227#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001228 ~future();
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001229 _LIBCPP_INLINE_VISIBILITY
Marshall Clow9bb0cca2017-01-24 23:28:25 +00001230 shared_future<_Rp&> share() _NOEXCEPT;
Howard Hinnant47499b12010-08-27 20:10:19 +00001231
1232 // retrieving the value
Howard Hinnant99968442011-11-29 18:15:50 +00001233 _Rp& get();
Howard Hinnant47499b12010-08-27 20:10:19 +00001234
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001235 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001236 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:19 +00001237
1238 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001239 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001240 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant47499b12010-08-27 20:10:19 +00001241
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001242 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001243 void wait() const {__state_->wait();}
1244 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001245 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001246 future_status
1247 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1248 {return __state_->wait_for(__rel_time);}
1249 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001250 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001251 future_status
1252 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1253 {return __state_->wait_until(__abs_time);}
1254};
1255
Howard Hinnant99968442011-11-29 18:15:50 +00001256template <class _Rp>
1257future<_Rp&>::future(__assoc_state<_Rp&>* __state)
Howard Hinnant47499b12010-08-27 20:10:19 +00001258 : __state_(__state)
1259{
1260 if (__state_->__has_future_attached())
Eric Fiselier423ca202015-10-02 21:25:15 +00001261 __throw_future_error(future_errc::future_already_retrieved);
Howard Hinnant47499b12010-08-27 20:10:19 +00001262 __state_->__add_shared();
Howard Hinnant54da3382010-08-30 18:46:21 +00001263 __state_->__set_future_attached();
Howard Hinnant47499b12010-08-27 20:10:19 +00001264}
1265
Howard Hinnant99968442011-11-29 18:15:50 +00001266template <class _Rp>
1267future<_Rp&>::~future()
Howard Hinnant47499b12010-08-27 20:10:19 +00001268{
1269 if (__state_)
1270 __state_->__release_shared();
1271}
1272
Howard Hinnant99968442011-11-29 18:15:50 +00001273template <class _Rp>
1274_Rp&
1275future<_Rp&>::get()
Howard Hinnant47499b12010-08-27 20:10:19 +00001276{
Howard Hinnant54da3382010-08-30 18:46:21 +00001277 unique_ptr<__shared_count, __release_shared_count> __(__state_);
Howard Hinnant99968442011-11-29 18:15:50 +00001278 __assoc_state<_Rp&>* __s = __state_;
Howard Hinnant47499b12010-08-27 20:10:19 +00001279 __state_ = nullptr;
1280 return __s->copy();
1281}
1282
1283template <>
Mehdi Amini907c1192017-05-04 17:08:54 +00001284class _LIBCPP_TYPE_VIS _LIBCPP_AVAILABILITY_FUTURE future<void>
Howard Hinnant47499b12010-08-27 20:10:19 +00001285{
1286 __assoc_sub_state* __state_;
1287
1288 explicit future(__assoc_sub_state* __state);
1289
1290 template <class> friend class promise;
Howard Hinnant99be8232010-09-03 18:39:25 +00001291 template <class> friend class shared_future;
Howard Hinnant54da3382010-08-30 18:46:21 +00001292
Howard Hinnant73d21a42010-09-04 23:28:19 +00001293#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00001294 template <class _R1, class _Fp>
1295 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1296 template <class _R1, class _Fp>
1297 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001298#else
Howard Hinnant99968442011-11-29 18:15:50 +00001299 template <class _R1, class _Fp>
1300 friend future<_R1> __make_deferred_assoc_state(_Fp __f);
1301 template <class _R1, class _Fp>
1302 friend future<_R1> __make_async_assoc_state(_Fp __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001303#endif
1304
Howard Hinnant47499b12010-08-27 20:10:19 +00001305public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001306 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001307 future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001308#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001309 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001310 future(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +00001311 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1312 future(const future&) = delete;
1313 future& operator=(const future&) = delete;
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001314 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001315 future& operator=(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +00001316 {
1317 future(std::move(__rhs)).swap(*this);
1318 return *this;
1319 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001320#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001321private:
1322 future(const future&);
1323 future& operator=(const future&);
1324public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001325#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001326 ~future();
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001327 _LIBCPP_INLINE_VISIBILITY
Marshall Clow9bb0cca2017-01-24 23:28:25 +00001328 shared_future<void> share() _NOEXCEPT;
Howard Hinnant47499b12010-08-27 20:10:19 +00001329
1330 // retrieving the value
1331 void get();
1332
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001333 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001334 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:19 +00001335
1336 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001337 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001338 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant47499b12010-08-27 20:10:19 +00001339
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001340 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001341 void wait() const {__state_->wait();}
1342 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001343 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001344 future_status
1345 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1346 {return __state_->wait_for(__rel_time);}
1347 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001348 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001349 future_status
1350 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1351 {return __state_->wait_until(__abs_time);}
1352};
1353
Howard Hinnant99968442011-11-29 18:15:50 +00001354template <class _Rp>
Howard Hinnant99be8232010-09-03 18:39:25 +00001355inline _LIBCPP_INLINE_VISIBILITY
1356void
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001357swap(future<_Rp>& __x, future<_Rp>& __y) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:25 +00001358{
1359 __x.swap(__y);
1360}
1361
Howard Hinnant47499b12010-08-27 20:10:19 +00001362// promise<R>
1363
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00001364template <class _Callable> class packaged_task;
Howard Hinnant54da3382010-08-30 18:46:21 +00001365
Howard Hinnant99968442011-11-29 18:15:50 +00001366template <class _Rp>
Mehdi Amini907c1192017-05-04 17:08:54 +00001367class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE promise
Howard Hinnant47499b12010-08-27 20:10:19 +00001368{
Howard Hinnant99968442011-11-29 18:15:50 +00001369 __assoc_state<_Rp>* __state_;
Howard Hinnant54da3382010-08-30 18:46:21 +00001370
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001371 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001372 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant54da3382010-08-30 18:46:21 +00001373
1374 template <class> friend class packaged_task;
Howard Hinnant47499b12010-08-27 20:10:19 +00001375public:
1376 promise();
1377 template <class _Alloc>
1378 promise(allocator_arg_t, const _Alloc& __a);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001379#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001380 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001381 promise(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +00001382 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1383 promise(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +00001384#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001385private:
1386 promise(const promise& __rhs);
1387public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001388#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001389 ~promise();
1390
1391 // assignment
Howard Hinnant73d21a42010-09-04 23:28:19 +00001392#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001393 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001394 promise& operator=(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +00001395 {
1396 promise(std::move(__rhs)).swap(*this);
1397 return *this;
1398 }
1399 promise& operator=(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +00001400#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001401private:
1402 promise& operator=(const promise& __rhs);
1403public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001404#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001405 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001406 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:19 +00001407
1408 // retrieving the result
Howard Hinnant99968442011-11-29 18:15:50 +00001409 future<_Rp> get_future();
Howard Hinnant47499b12010-08-27 20:10:19 +00001410
1411 // setting the result
Howard Hinnant99968442011-11-29 18:15:50 +00001412 void set_value(const _Rp& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001413#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00001414 void set_value(_Rp&& __r);
Howard Hinnant47499b12010-08-27 20:10:19 +00001415#endif
1416 void set_exception(exception_ptr __p);
1417
1418 // setting the result with deferred notification
Howard Hinnant99968442011-11-29 18:15:50 +00001419 void set_value_at_thread_exit(const _Rp& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001420#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00001421 void set_value_at_thread_exit(_Rp&& __r);
Howard Hinnant47499b12010-08-27 20:10:19 +00001422#endif
1423 void set_exception_at_thread_exit(exception_ptr __p);
1424};
1425
Howard Hinnant99968442011-11-29 18:15:50 +00001426template <class _Rp>
1427promise<_Rp>::promise()
1428 : __state_(new __assoc_state<_Rp>)
Howard Hinnant47499b12010-08-27 20:10:19 +00001429{
1430}
1431
Howard Hinnant99968442011-11-29 18:15:50 +00001432template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +00001433template <class _Alloc>
Howard Hinnant99968442011-11-29 18:15:50 +00001434promise<_Rp>::promise(allocator_arg_t, const _Alloc& __a0)
Howard Hinnant47499b12010-08-27 20:10:19 +00001435{
Eric Fiselier4d2413c2014-10-23 06:24:45 +00001436 typedef __assoc_state_alloc<_Rp, _Alloc> _State;
1437 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
Howard Hinnant47499b12010-08-27 20:10:19 +00001438 typedef __allocator_destructor<_A2> _D2;
1439 _A2 __a(__a0);
Eric Fiselier4d2413c2014-10-23 06:24:45 +00001440 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1441 ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0);
1442 __state_ = _VSTD::addressof(*__hold.release());
Howard Hinnant47499b12010-08-27 20:10:19 +00001443}
1444
Howard Hinnant99968442011-11-29 18:15:50 +00001445template <class _Rp>
1446promise<_Rp>::~promise()
Howard Hinnant47499b12010-08-27 20:10:19 +00001447{
1448 if (__state_)
1449 {
1450 if (!__state_->__has_value() && __state_->use_count() > 1)
1451 __state_->set_exception(make_exception_ptr(
1452 future_error(make_error_code(future_errc::broken_promise))
1453 ));
1454 __state_->__release_shared();
1455 }
1456}
1457
Howard Hinnant99968442011-11-29 18:15:50 +00001458template <class _Rp>
1459future<_Rp>
1460promise<_Rp>::get_future()
Howard Hinnant47499b12010-08-27 20:10:19 +00001461{
1462 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00001463 __throw_future_error(future_errc::no_state);
Howard Hinnant99968442011-11-29 18:15:50 +00001464 return future<_Rp>(__state_);
Howard Hinnant47499b12010-08-27 20:10:19 +00001465}
1466
Howard Hinnant99968442011-11-29 18:15:50 +00001467template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +00001468void
Howard Hinnant99968442011-11-29 18:15:50 +00001469promise<_Rp>::set_value(const _Rp& __r)
Howard Hinnant47499b12010-08-27 20:10:19 +00001470{
1471 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00001472 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:19 +00001473 __state_->set_value(__r);
1474}
1475
Howard Hinnant73d21a42010-09-04 23:28:19 +00001476#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001477
Howard Hinnant99968442011-11-29 18:15:50 +00001478template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +00001479void
Howard Hinnant99968442011-11-29 18:15:50 +00001480promise<_Rp>::set_value(_Rp&& __r)
Howard Hinnant47499b12010-08-27 20:10:19 +00001481{
1482 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00001483 __throw_future_error(future_errc::no_state);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001484 __state_->set_value(_VSTD::move(__r));
Howard Hinnant47499b12010-08-27 20:10:19 +00001485}
1486
Howard Hinnant73d21a42010-09-04 23:28:19 +00001487#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001488
Howard Hinnant99968442011-11-29 18:15:50 +00001489template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +00001490void
Howard Hinnant99968442011-11-29 18:15:50 +00001491promise<_Rp>::set_exception(exception_ptr __p)
Howard Hinnant47499b12010-08-27 20:10:19 +00001492{
Marshall Cloweaba7bb2016-05-16 16:55:32 +00001493 _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception: received nullptr" );
Howard Hinnant47499b12010-08-27 20:10:19 +00001494 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_exception(__p);
1497}
1498
Howard Hinnant99968442011-11-29 18:15:50 +00001499template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +00001500void
Howard Hinnant99968442011-11-29 18:15:50 +00001501promise<_Rp>::set_value_at_thread_exit(const _Rp& __r)
Howard Hinnant47499b12010-08-27 20:10:19 +00001502{
1503 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00001504 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:19 +00001505 __state_->set_value_at_thread_exit(__r);
1506}
1507
Howard Hinnant73d21a42010-09-04 23:28:19 +00001508#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001509
Howard Hinnant99968442011-11-29 18:15:50 +00001510template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +00001511void
Howard Hinnant99968442011-11-29 18:15:50 +00001512promise<_Rp>::set_value_at_thread_exit(_Rp&& __r)
Howard Hinnant47499b12010-08-27 20:10:19 +00001513{
1514 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00001515 __throw_future_error(future_errc::no_state);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001516 __state_->set_value_at_thread_exit(_VSTD::move(__r));
Howard Hinnant47499b12010-08-27 20:10:19 +00001517}
1518
Howard Hinnant73d21a42010-09-04 23:28:19 +00001519#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001520
Howard Hinnant99968442011-11-29 18:15:50 +00001521template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +00001522void
Howard Hinnant99968442011-11-29 18:15:50 +00001523promise<_Rp>::set_exception_at_thread_exit(exception_ptr __p)
Howard Hinnant47499b12010-08-27 20:10:19 +00001524{
Eric Fiselierb169bb02016-05-31 01:50:55 +00001525 _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception_at_thread_exit: received nullptr" );
Howard Hinnant47499b12010-08-27 20:10:19 +00001526 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00001527 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:19 +00001528 __state_->set_exception_at_thread_exit(__p);
1529}
1530
1531// promise<R&>
1532
Howard Hinnant99968442011-11-29 18:15:50 +00001533template <class _Rp>
Mehdi Amini907c1192017-05-04 17:08:54 +00001534class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE promise<_Rp&>
Howard Hinnant47499b12010-08-27 20:10:19 +00001535{
Howard Hinnant99968442011-11-29 18:15:50 +00001536 __assoc_state<_Rp&>* __state_;
Howard Hinnant54da3382010-08-30 18:46:21 +00001537
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001538 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001539 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant54da3382010-08-30 18:46:21 +00001540
1541 template <class> friend class packaged_task;
1542
Howard Hinnant47499b12010-08-27 20:10:19 +00001543public:
1544 promise();
1545 template <class _Allocator>
1546 promise(allocator_arg_t, const _Allocator& __a);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001547#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001548 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001549 promise(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +00001550 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1551 promise(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +00001552#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001553private:
1554 promise(const promise& __rhs);
1555public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001556#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001557 ~promise();
1558
1559 // assignment
Howard Hinnant73d21a42010-09-04 23:28:19 +00001560#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001562 promise& operator=(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +00001563 {
1564 promise(std::move(__rhs)).swap(*this);
1565 return *this;
1566 }
1567 promise& operator=(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +00001568#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001569private:
1570 promise& operator=(const promise& __rhs);
1571public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001572#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001573 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001574 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:19 +00001575
1576 // retrieving the result
Howard Hinnant99968442011-11-29 18:15:50 +00001577 future<_Rp&> get_future();
Howard Hinnant47499b12010-08-27 20:10:19 +00001578
1579 // setting the result
Howard Hinnant99968442011-11-29 18:15:50 +00001580 void set_value(_Rp& __r);
Howard Hinnant47499b12010-08-27 20:10:19 +00001581 void set_exception(exception_ptr __p);
1582
1583 // setting the result with deferred notification
Howard Hinnant99968442011-11-29 18:15:50 +00001584 void set_value_at_thread_exit(_Rp&);
Howard Hinnant47499b12010-08-27 20:10:19 +00001585 void set_exception_at_thread_exit(exception_ptr __p);
1586};
1587
Howard Hinnant99968442011-11-29 18:15:50 +00001588template <class _Rp>
1589promise<_Rp&>::promise()
1590 : __state_(new __assoc_state<_Rp&>)
Howard Hinnant47499b12010-08-27 20:10:19 +00001591{
1592}
1593
Howard Hinnant99968442011-11-29 18:15:50 +00001594template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +00001595template <class _Alloc>
Howard Hinnant99968442011-11-29 18:15:50 +00001596promise<_Rp&>::promise(allocator_arg_t, const _Alloc& __a0)
Howard Hinnant47499b12010-08-27 20:10:19 +00001597{
Eric Fiselier4d2413c2014-10-23 06:24:45 +00001598 typedef __assoc_state_alloc<_Rp&, _Alloc> _State;
1599 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
Howard Hinnant47499b12010-08-27 20:10:19 +00001600 typedef __allocator_destructor<_A2> _D2;
1601 _A2 __a(__a0);
Eric Fiselier4d2413c2014-10-23 06:24:45 +00001602 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1603 ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0);
1604 __state_ = _VSTD::addressof(*__hold.release());
Howard Hinnant47499b12010-08-27 20:10:19 +00001605}
1606
Howard Hinnant99968442011-11-29 18:15:50 +00001607template <class _Rp>
1608promise<_Rp&>::~promise()
Howard Hinnant47499b12010-08-27 20:10:19 +00001609{
1610 if (__state_)
1611 {
1612 if (!__state_->__has_value() && __state_->use_count() > 1)
1613 __state_->set_exception(make_exception_ptr(
1614 future_error(make_error_code(future_errc::broken_promise))
1615 ));
1616 __state_->__release_shared();
1617 }
1618}
1619
Howard Hinnant99968442011-11-29 18:15:50 +00001620template <class _Rp>
1621future<_Rp&>
1622promise<_Rp&>::get_future()
Howard Hinnant47499b12010-08-27 20:10:19 +00001623{
1624 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00001625 __throw_future_error(future_errc::no_state);
Howard Hinnant99968442011-11-29 18:15:50 +00001626 return future<_Rp&>(__state_);
Howard Hinnant47499b12010-08-27 20:10:19 +00001627}
1628
Howard Hinnant99968442011-11-29 18:15:50 +00001629template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +00001630void
Howard Hinnant99968442011-11-29 18:15:50 +00001631promise<_Rp&>::set_value(_Rp& __r)
Howard Hinnant47499b12010-08-27 20:10:19 +00001632{
1633 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00001634 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:19 +00001635 __state_->set_value(__r);
1636}
1637
Howard Hinnant99968442011-11-29 18:15:50 +00001638template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +00001639void
Howard Hinnant99968442011-11-29 18:15:50 +00001640promise<_Rp&>::set_exception(exception_ptr __p)
Howard Hinnant47499b12010-08-27 20:10:19 +00001641{
Marshall Cloweaba7bb2016-05-16 16:55:32 +00001642 _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception: received nullptr" );
Howard Hinnant47499b12010-08-27 20:10:19 +00001643 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00001644 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:19 +00001645 __state_->set_exception(__p);
1646}
1647
Howard Hinnant99968442011-11-29 18:15:50 +00001648template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +00001649void
Howard Hinnant99968442011-11-29 18:15:50 +00001650promise<_Rp&>::set_value_at_thread_exit(_Rp& __r)
Howard Hinnant47499b12010-08-27 20:10:19 +00001651{
1652 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00001653 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:19 +00001654 __state_->set_value_at_thread_exit(__r);
1655}
1656
Howard Hinnant99968442011-11-29 18:15:50 +00001657template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +00001658void
Howard Hinnant99968442011-11-29 18:15:50 +00001659promise<_Rp&>::set_exception_at_thread_exit(exception_ptr __p)
Howard Hinnant47499b12010-08-27 20:10:19 +00001660{
Eric Fiselierb169bb02016-05-31 01:50:55 +00001661 _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception_at_thread_exit: received nullptr" );
Howard Hinnant47499b12010-08-27 20:10:19 +00001662 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00001663 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:19 +00001664 __state_->set_exception_at_thread_exit(__p);
1665}
1666
1667// promise<void>
1668
1669template <>
Mehdi Amini907c1192017-05-04 17:08:54 +00001670class _LIBCPP_TYPE_VIS _LIBCPP_AVAILABILITY_FUTURE promise<void>
Howard Hinnant47499b12010-08-27 20:10:19 +00001671{
1672 __assoc_sub_state* __state_;
Howard Hinnant54da3382010-08-30 18:46:21 +00001673
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001674 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001675 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant54da3382010-08-30 18:46:21 +00001676
1677 template <class> friend class packaged_task;
1678
Howard Hinnant47499b12010-08-27 20:10:19 +00001679public:
1680 promise();
1681 template <class _Allocator>
Shoaib Meenai6b734922017-03-02 03:22:18 +00001682 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Howard Hinnant47499b12010-08-27 20:10:19 +00001683 promise(allocator_arg_t, const _Allocator& __a);
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(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +00001687 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1688 promise(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +00001689#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001690private:
1691 promise(const promise& __rhs);
1692public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001693#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001694 ~promise();
1695
1696 // assignment
Howard Hinnant73d21a42010-09-04 23:28:19 +00001697#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001698 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001699 promise& operator=(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +00001700 {
1701 promise(std::move(__rhs)).swap(*this);
1702 return *this;
1703 }
1704 promise& operator=(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +00001705#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001706private:
1707 promise& operator=(const promise& __rhs);
1708public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001709#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001710 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001711 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:19 +00001712
1713 // retrieving the result
1714 future<void> get_future();
1715
1716 // setting the result
1717 void set_value();
1718 void set_exception(exception_ptr __p);
1719
1720 // setting the result with deferred notification
1721 void set_value_at_thread_exit();
1722 void set_exception_at_thread_exit(exception_ptr __p);
1723};
1724
1725template <class _Alloc>
1726promise<void>::promise(allocator_arg_t, const _Alloc& __a0)
1727{
Eric Fiselier4d2413c2014-10-23 06:24:45 +00001728 typedef __assoc_sub_state_alloc<_Alloc> _State;
1729 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
Howard Hinnant47499b12010-08-27 20:10:19 +00001730 typedef __allocator_destructor<_A2> _D2;
1731 _A2 __a(__a0);
Eric Fiselier4d2413c2014-10-23 06:24:45 +00001732 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1733 ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0);
1734 __state_ = _VSTD::addressof(*__hold.release());
Howard Hinnant47499b12010-08-27 20:10:19 +00001735}
1736
Howard Hinnant99968442011-11-29 18:15:50 +00001737template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19 +00001738inline _LIBCPP_INLINE_VISIBILITY
1739void
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001740swap(promise<_Rp>& __x, promise<_Rp>& __y) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +00001741{
1742 __x.swap(__y);
1743}
1744
Howard Hinnant99968442011-11-29 18:15:50 +00001745template <class _Rp, class _Alloc>
Eric Fiselierc3589a82017-01-04 23:56:00 +00001746 struct _LIBCPP_TEMPLATE_VIS uses_allocator<promise<_Rp>, _Alloc>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001747 : public true_type {};
Howard Hinnant47499b12010-08-27 20:10:19 +00001748
Howard Hinnant54da3382010-08-30 18:46:21 +00001749#ifndef _LIBCPP_HAS_NO_VARIADICS
1750
1751// packaged_task
1752
1753template<class _Fp> class __packaged_task_base;
1754
Howard Hinnant99968442011-11-29 18:15:50 +00001755template<class _Rp, class ..._ArgTypes>
Mehdi Amini907c1192017-05-04 17:08:54 +00001756class _LIBCPP_AVAILABILITY_FUTURE __packaged_task_base<_Rp(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:21 +00001757{
1758 __packaged_task_base(const __packaged_task_base&);
1759 __packaged_task_base& operator=(const __packaged_task_base&);
1760public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001761 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001762 __packaged_task_base() {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001763 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001764 virtual ~__packaged_task_base() {}
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001765 virtual void __move_to(__packaged_task_base*) _NOEXCEPT = 0;
Howard Hinnant54da3382010-08-30 18:46:21 +00001766 virtual void destroy() = 0;
1767 virtual void destroy_deallocate() = 0;
Howard Hinnant99968442011-11-29 18:15:50 +00001768 virtual _Rp operator()(_ArgTypes&& ...) = 0;
Howard Hinnant54da3382010-08-30 18:46:21 +00001769};
1770
1771template<class _FD, class _Alloc, class _FB> class __packaged_task_func;
1772
Howard Hinnant99968442011-11-29 18:15:50 +00001773template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Mehdi Amini907c1192017-05-04 17:08:54 +00001774class _LIBCPP_AVAILABILITY_FUTURE __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>
Howard Hinnant99968442011-11-29 18:15:50 +00001775 : public __packaged_task_base<_Rp(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:21 +00001776{
Howard Hinnant99968442011-11-29 18:15:50 +00001777 __compressed_pair<_Fp, _Alloc> __f_;
Howard Hinnant54da3382010-08-30 18:46:21 +00001778public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001779 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001780 explicit __packaged_task_func(const _Fp& __f) : __f_(__f) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001781 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001782 explicit __packaged_task_func(_Fp&& __f) : __f_(_VSTD::move(__f)) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001783 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001784 __packaged_task_func(const _Fp& __f, const _Alloc& __a)
Howard Hinnant54da3382010-08-30 18:46:21 +00001785 : __f_(__f, __a) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001786 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001787 __packaged_task_func(_Fp&& __f, const _Alloc& __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001788 : __f_(_VSTD::move(__f), __a) {}
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001789 virtual void __move_to(__packaged_task_base<_Rp(_ArgTypes...)>*) _NOEXCEPT;
Howard Hinnant54da3382010-08-30 18:46:21 +00001790 virtual void destroy();
1791 virtual void destroy_deallocate();
Howard Hinnant99968442011-11-29 18:15:50 +00001792 virtual _Rp operator()(_ArgTypes&& ... __args);
Howard Hinnant54da3382010-08-30 18:46:21 +00001793};
1794
Howard Hinnant99968442011-11-29 18:15:50 +00001795template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:21 +00001796void
Howard Hinnant99968442011-11-29 18:15:50 +00001797__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__move_to(
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001798 __packaged_task_base<_Rp(_ArgTypes...)>* __p) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:21 +00001799{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001800 ::new (__p) __packaged_task_func(_VSTD::move(__f_.first()), _VSTD::move(__f_.second()));
Howard Hinnant54da3382010-08-30 18:46:21 +00001801}
1802
Howard Hinnant99968442011-11-29 18:15:50 +00001803template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:21 +00001804void
Howard Hinnant99968442011-11-29 18:15:50 +00001805__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy()
Howard Hinnant54da3382010-08-30 18:46:21 +00001806{
Howard Hinnant99968442011-11-29 18:15:50 +00001807 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnant54da3382010-08-30 18:46:21 +00001808}
1809
Howard Hinnant99968442011-11-29 18:15:50 +00001810template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:21 +00001811void
Howard Hinnant99968442011-11-29 18:15:50 +00001812__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate()
Howard Hinnant54da3382010-08-30 18:46:21 +00001813{
Eric Fiselier4d2413c2014-10-23 06:24:45 +00001814 typedef typename __allocator_traits_rebind<_Alloc, __packaged_task_func>::type _Ap;
1815 typedef allocator_traits<_Ap> _ATraits;
1816 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Howard Hinnant99968442011-11-29 18:15:50 +00001817 _Ap __a(__f_.second());
1818 __f_.~__compressed_pair<_Fp, _Alloc>();
Eric Fiselier4d2413c2014-10-23 06:24:45 +00001819 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnant54da3382010-08-30 18:46:21 +00001820}
1821
Howard Hinnant99968442011-11-29 18:15:50 +00001822template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1823_Rp
1824__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
Howard Hinnant54da3382010-08-30 18:46:21 +00001825{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001826 return __invoke(__f_.first(), _VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnant54da3382010-08-30 18:46:21 +00001827}
1828
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00001829template <class _Callable> class __packaged_task_function;
Howard Hinnant54da3382010-08-30 18:46:21 +00001830
Howard Hinnant99968442011-11-29 18:15:50 +00001831template<class _Rp, class ..._ArgTypes>
Mehdi Amini907c1192017-05-04 17:08:54 +00001832class _LIBCPP_AVAILABILITY_FUTURE __packaged_task_function<_Rp(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:21 +00001833{
Howard Hinnant99968442011-11-29 18:15:50 +00001834 typedef __packaged_task_base<_Rp(_ArgTypes...)> __base;
Howard Hinnant78f0de22013-01-21 17:26:55 +00001835 typename aligned_storage<3*sizeof(void*)>::type __buf_;
Howard Hinnant54da3382010-08-30 18:46:21 +00001836 __base* __f_;
1837
1838public:
Howard Hinnant99968442011-11-29 18:15:50 +00001839 typedef _Rp result_type;
Howard Hinnant54da3382010-08-30 18:46:21 +00001840
1841 // construct/copy/destroy:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001842 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001843 __packaged_task_function() _NOEXCEPT : __f_(nullptr) {}
Howard Hinnant99968442011-11-29 18:15:50 +00001844 template<class _Fp>
1845 __packaged_task_function(_Fp&& __f);
1846 template<class _Fp, class _Alloc>
1847 __packaged_task_function(allocator_arg_t, const _Alloc& __a, _Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001848
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001849 __packaged_task_function(__packaged_task_function&&) _NOEXCEPT;
1850 __packaged_task_function& operator=(__packaged_task_function&&) _NOEXCEPT;
Howard Hinnant54da3382010-08-30 18:46:21 +00001851
1852 __packaged_task_function(const __packaged_task_function&) = delete;
1853 __packaged_task_function& operator=(const __packaged_task_function&) = delete;
1854
1855 ~__packaged_task_function();
1856
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001857 void swap(__packaged_task_function&) _NOEXCEPT;
Howard Hinnant54da3382010-08-30 18:46:21 +00001858
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001859 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001860 _Rp operator()(_ArgTypes...) const;
Howard Hinnant54da3382010-08-30 18:46:21 +00001861};
1862
Howard Hinnant99968442011-11-29 18:15:50 +00001863template<class _Rp, class ..._ArgTypes>
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001864__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:21 +00001865{
1866 if (__f.__f_ == nullptr)
1867 __f_ = nullptr;
1868 else if (__f.__f_ == (__base*)&__f.__buf_)
1869 {
1870 __f_ = (__base*)&__buf_;
1871 __f.__f_->__move_to(__f_);
1872 }
1873 else
1874 {
1875 __f_ = __f.__f_;
1876 __f.__f_ = nullptr;
1877 }
1878}
1879
Howard Hinnant99968442011-11-29 18:15:50 +00001880template<class _Rp, class ..._ArgTypes>
1881template <class _Fp>
1882__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(_Fp&& __f)
Howard Hinnant54da3382010-08-30 18:46:21 +00001883 : __f_(nullptr)
1884{
Marshall Clowf1264e72014-04-07 13:32:26 +00001885 typedef typename remove_reference<typename decay<_Fp>::type>::type _FR;
Howard Hinnant99968442011-11-29 18:15:50 +00001886 typedef __packaged_task_func<_FR, allocator<_FR>, _Rp(_ArgTypes...)> _FF;
Howard Hinnant54da3382010-08-30 18:46:21 +00001887 if (sizeof(_FF) <= sizeof(__buf_))
1888 {
1889 __f_ = (__base*)&__buf_;
Howard Hinnant99968442011-11-29 18:15:50 +00001890 ::new (__f_) _FF(_VSTD::forward<_Fp>(__f));
Howard Hinnant54da3382010-08-30 18:46:21 +00001891 }
1892 else
1893 {
Howard Hinnant99968442011-11-29 18:15:50 +00001894 typedef allocator<_FF> _Ap;
1895 _Ap __a;
1896 typedef __allocator_destructor<_Ap> _Dp;
1897 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1898 ::new (__hold.get()) _FF(_VSTD::forward<_Fp>(__f), allocator<_FR>(__a));
Howard Hinnant54da3382010-08-30 18:46:21 +00001899 __f_ = __hold.release();
1900 }
1901}
1902
Howard Hinnant99968442011-11-29 18:15:50 +00001903template<class _Rp, class ..._ArgTypes>
1904template <class _Fp, class _Alloc>
1905__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(
1906 allocator_arg_t, const _Alloc& __a0, _Fp&& __f)
Howard Hinnant54da3382010-08-30 18:46:21 +00001907 : __f_(nullptr)
1908{
Marshall Clowf1264e72014-04-07 13:32:26 +00001909 typedef typename remove_reference<typename decay<_Fp>::type>::type _FR;
Howard Hinnant99968442011-11-29 18:15:50 +00001910 typedef __packaged_task_func<_FR, _Alloc, _Rp(_ArgTypes...)> _FF;
Howard Hinnant54da3382010-08-30 18:46:21 +00001911 if (sizeof(_FF) <= sizeof(__buf_))
1912 {
1913 __f_ = (__base*)&__buf_;
Howard Hinnant99968442011-11-29 18:15:50 +00001914 ::new (__f_) _FF(_VSTD::forward<_Fp>(__f));
Howard Hinnant54da3382010-08-30 18:46:21 +00001915 }
1916 else
1917 {
Eric Fiselier4d2413c2014-10-23 06:24:45 +00001918 typedef typename __allocator_traits_rebind<_Alloc, _FF>::type _Ap;
Howard Hinnant99968442011-11-29 18:15:50 +00001919 _Ap __a(__a0);
1920 typedef __allocator_destructor<_Ap> _Dp;
1921 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Eric Fiselier4d2413c2014-10-23 06:24:45 +00001922 ::new (static_cast<void*>(_VSTD::addressof(*__hold.get())))
1923 _FF(_VSTD::forward<_Fp>(__f), _Alloc(__a));
1924 __f_ = _VSTD::addressof(*__hold.release());
Howard Hinnant54da3382010-08-30 18:46:21 +00001925 }
1926}
1927
Howard Hinnant99968442011-11-29 18:15:50 +00001928template<class _Rp, class ..._ArgTypes>
1929__packaged_task_function<_Rp(_ArgTypes...)>&
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001930__packaged_task_function<_Rp(_ArgTypes...)>::operator=(__packaged_task_function&& __f) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:21 +00001931{
1932 if (__f_ == (__base*)&__buf_)
1933 __f_->destroy();
1934 else if (__f_)
1935 __f_->destroy_deallocate();
1936 __f_ = nullptr;
1937 if (__f.__f_ == nullptr)
1938 __f_ = nullptr;
1939 else if (__f.__f_ == (__base*)&__f.__buf_)
1940 {
1941 __f_ = (__base*)&__buf_;
1942 __f.__f_->__move_to(__f_);
1943 }
1944 else
1945 {
1946 __f_ = __f.__f_;
1947 __f.__f_ = nullptr;
1948 }
Argyrios Kyrtzidis1dc6f7a2012-10-13 02:03:45 +00001949 return *this;
Howard Hinnant54da3382010-08-30 18:46:21 +00001950}
1951
Howard Hinnant99968442011-11-29 18:15:50 +00001952template<class _Rp, class ..._ArgTypes>
1953__packaged_task_function<_Rp(_ArgTypes...)>::~__packaged_task_function()
Howard Hinnant54da3382010-08-30 18:46:21 +00001954{
1955 if (__f_ == (__base*)&__buf_)
1956 __f_->destroy();
1957 else if (__f_)
1958 __f_->destroy_deallocate();
1959}
1960
Howard Hinnant99968442011-11-29 18:15:50 +00001961template<class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:21 +00001962void
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00001963__packaged_task_function<_Rp(_ArgTypes...)>::swap(__packaged_task_function& __f) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:21 +00001964{
1965 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1966 {
1967 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1968 __base* __t = (__base*)&__tempbuf;
1969 __f_->__move_to(__t);
1970 __f_->destroy();
1971 __f_ = nullptr;
1972 __f.__f_->__move_to((__base*)&__buf_);
1973 __f.__f_->destroy();
1974 __f.__f_ = nullptr;
1975 __f_ = (__base*)&__buf_;
1976 __t->__move_to((__base*)&__f.__buf_);
1977 __t->destroy();
1978 __f.__f_ = (__base*)&__f.__buf_;
1979 }
1980 else if (__f_ == (__base*)&__buf_)
1981 {
1982 __f_->__move_to((__base*)&__f.__buf_);
1983 __f_->destroy();
1984 __f_ = __f.__f_;
1985 __f.__f_ = (__base*)&__f.__buf_;
1986 }
1987 else if (__f.__f_ == (__base*)&__f.__buf_)
1988 {
1989 __f.__f_->__move_to((__base*)&__buf_);
1990 __f.__f_->destroy();
1991 __f.__f_ = __f_;
1992 __f_ = (__base*)&__buf_;
1993 }
1994 else
Howard Hinnant0949eed2011-06-30 21:18:19 +00001995 _VSTD::swap(__f_, __f.__f_);
Howard Hinnant54da3382010-08-30 18:46:21 +00001996}
1997
Howard Hinnant99968442011-11-29 18:15:50 +00001998template<class _Rp, class ..._ArgTypes>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001999inline
Howard Hinnant99968442011-11-29 18:15:50 +00002000_Rp
2001__packaged_task_function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
Howard Hinnant54da3382010-08-30 18:46:21 +00002002{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002003 return (*__f_)(_VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnant54da3382010-08-30 18:46:21 +00002004}
2005
Howard Hinnant99968442011-11-29 18:15:50 +00002006template<class _Rp, class ..._ArgTypes>
Mehdi Amini907c1192017-05-04 17:08:54 +00002007class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE packaged_task<_Rp(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:21 +00002008{
2009public:
Eric Fiselier68db6cd2016-06-01 21:05:53 +00002010 typedef _Rp result_type; // extension
Howard Hinnant54da3382010-08-30 18:46:21 +00002011
2012private:
2013 __packaged_task_function<result_type(_ArgTypes...)> __f_;
2014 promise<result_type> __p_;
2015
2016public:
2017 // construction and destruction
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002018 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002019 packaged_task() _NOEXCEPT : __p_(nullptr) {}
Marshall Clow5f2d5b92013-10-12 22:49:17 +00002020 template <class _Fp,
2021 class = typename enable_if
2022 <
2023 !is_same<
2024 typename decay<_Fp>::type,
2025 packaged_task
2026 >::value
2027 >::type
2028 >
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002029 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00002030 explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
Marshall Clow5f2d5b92013-10-12 22:49:17 +00002031 template <class _Fp, class _Allocator,
2032 class = typename enable_if
2033 <
2034 !is_same<
2035 typename decay<_Fp>::type,
2036 packaged_task
2037 >::value
2038 >::type
2039 >
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002040 _LIBCPP_INLINE_VISIBILITY
Marshall Clow07546f32015-06-30 14:16:49 +00002041 packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
Howard Hinnant99968442011-11-29 18:15:50 +00002042 : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)),
Howard Hinnant54da3382010-08-30 18:46:21 +00002043 __p_(allocator_arg, __a) {}
2044 // ~packaged_task() = default;
2045
2046 // no copy
Howard Hinnant8131a012012-07-21 19:34:12 +00002047 packaged_task(const packaged_task&) = delete;
2048 packaged_task& operator=(const packaged_task&) = delete;
Howard Hinnant54da3382010-08-30 18:46:21 +00002049
2050 // move support
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002051 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002052 packaged_task(packaged_task&& __other) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002053 : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002054 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002055 packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:21 +00002056 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002057 __f_ = _VSTD::move(__other.__f_);
2058 __p_ = _VSTD::move(__other.__p_);
Howard Hinnant54da3382010-08-30 18:46:21 +00002059 return *this;
2060 }
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002061 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002062 void swap(packaged_task& __other) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:21 +00002063 {
2064 __f_.swap(__other.__f_);
2065 __p_.swap(__other.__p_);
2066 }
2067
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002068 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002069 bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
Howard Hinnant54da3382010-08-30 18:46:21 +00002070
2071 // result retrieval
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002072 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00002073 future<result_type> get_future() {return __p_.get_future();}
2074
2075 // execution
2076 void operator()(_ArgTypes... __args);
2077 void make_ready_at_thread_exit(_ArgTypes... __args);
2078
2079 void reset();
2080};
2081
Howard Hinnant99968442011-11-29 18:15:50 +00002082template<class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:21 +00002083void
Howard Hinnant99968442011-11-29 18:15:50 +00002084packaged_task<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __args)
Howard Hinnant54da3382010-08-30 18:46:21 +00002085{
Howard Hinnant54da3382010-08-30 18:46:21 +00002086 if (__p_.__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00002087 __throw_future_error(future_errc::no_state);
Howard Hinnant54da3382010-08-30 18:46:21 +00002088 if (__p_.__state_->__has_value())
Eric Fiselier423ca202015-10-02 21:25:15 +00002089 __throw_future_error(future_errc::promise_already_satisfied);
Marshall Clowa1899742015-09-03 15:11:32 +00002090#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant54da3382010-08-30 18:46:21 +00002091 try
2092 {
2093#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:19 +00002094 __p_.set_value(__f_(_VSTD::forward<_ArgTypes>(__args)...));
Howard Hinnant54da3382010-08-30 18:46:21 +00002095#ifndef _LIBCPP_NO_EXCEPTIONS
2096 }
2097 catch (...)
2098 {
2099 __p_.set_exception(current_exception());
2100 }
2101#endif // _LIBCPP_NO_EXCEPTIONS
2102}
2103
Howard Hinnant99968442011-11-29 18:15:50 +00002104template<class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:21 +00002105void
Howard Hinnant99968442011-11-29 18:15:50 +00002106packaged_task<_Rp(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
Howard Hinnant54da3382010-08-30 18:46:21 +00002107{
Howard Hinnant54da3382010-08-30 18:46:21 +00002108 if (__p_.__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00002109 __throw_future_error(future_errc::no_state);
Howard Hinnant54da3382010-08-30 18:46:21 +00002110 if (__p_.__state_->__has_value())
Eric Fiselier423ca202015-10-02 21:25:15 +00002111 __throw_future_error(future_errc::promise_already_satisfied);
Marshall Clowa1899742015-09-03 15:11:32 +00002112#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant54da3382010-08-30 18:46:21 +00002113 try
2114 {
2115#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:19 +00002116 __p_.set_value_at_thread_exit(__f_(_VSTD::forward<_ArgTypes>(__args)...));
Howard Hinnant54da3382010-08-30 18:46:21 +00002117#ifndef _LIBCPP_NO_EXCEPTIONS
2118 }
2119 catch (...)
2120 {
2121 __p_.set_exception_at_thread_exit(current_exception());
2122 }
2123#endif // _LIBCPP_NO_EXCEPTIONS
2124}
2125
Howard Hinnant99968442011-11-29 18:15:50 +00002126template<class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:21 +00002127void
Howard Hinnant99968442011-11-29 18:15:50 +00002128packaged_task<_Rp(_ArgTypes...)>::reset()
Howard Hinnant54da3382010-08-30 18:46:21 +00002129{
Howard Hinnant7de47902010-11-30 20:23:32 +00002130 if (!valid())
Eric Fiselier423ca202015-10-02 21:25:15 +00002131 __throw_future_error(future_errc::no_state);
Howard Hinnant54da3382010-08-30 18:46:21 +00002132 __p_ = promise<result_type>();
2133}
2134
2135template<class ..._ArgTypes>
Mehdi Amini907c1192017-05-04 17:08:54 +00002136class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE packaged_task<void(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:21 +00002137{
2138public:
Eric Fiselier68db6cd2016-06-01 21:05:53 +00002139 typedef void result_type; // extension
Howard Hinnant54da3382010-08-30 18:46:21 +00002140
2141private:
2142 __packaged_task_function<result_type(_ArgTypes...)> __f_;
2143 promise<result_type> __p_;
2144
2145public:
2146 // construction and destruction
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002147 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002148 packaged_task() _NOEXCEPT : __p_(nullptr) {}
Marshall Clow5f2d5b92013-10-12 22:49:17 +00002149 template <class _Fp,
2150 class = typename enable_if
2151 <
2152 !is_same<
2153 typename decay<_Fp>::type,
2154 packaged_task
2155 >::value
2156 >::type
2157 >
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002158 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00002159 explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
Marshall Clow5f2d5b92013-10-12 22:49:17 +00002160 template <class _Fp, class _Allocator,
2161 class = typename enable_if
2162 <
2163 !is_same<
2164 typename decay<_Fp>::type,
2165 packaged_task
2166 >::value
2167 >::type
2168 >
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002169 _LIBCPP_INLINE_VISIBILITY
Marshall Clow5706c372015-06-30 18:28:35 +00002170 packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
Howard Hinnant99968442011-11-29 18:15:50 +00002171 : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)),
Howard Hinnant54da3382010-08-30 18:46:21 +00002172 __p_(allocator_arg, __a) {}
2173 // ~packaged_task() = default;
2174
2175 // no copy
Howard Hinnant8131a012012-07-21 19:34:12 +00002176 packaged_task(const packaged_task&) = delete;
2177 packaged_task& operator=(const packaged_task&) = delete;
Howard Hinnant54da3382010-08-30 18:46:21 +00002178
2179 // move support
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002180 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002181 packaged_task(packaged_task&& __other) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002182 : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002183 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002184 packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:21 +00002185 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002186 __f_ = _VSTD::move(__other.__f_);
2187 __p_ = _VSTD::move(__other.__p_);
Howard Hinnant54da3382010-08-30 18:46:21 +00002188 return *this;
2189 }
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002190 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002191 void swap(packaged_task& __other) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:21 +00002192 {
2193 __f_.swap(__other.__f_);
2194 __p_.swap(__other.__p_);
2195 }
2196
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002197 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002198 bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
Howard Hinnant54da3382010-08-30 18:46:21 +00002199
2200 // result retrieval
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002201 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00002202 future<result_type> get_future() {return __p_.get_future();}
2203
2204 // execution
2205 void operator()(_ArgTypes... __args);
2206 void make_ready_at_thread_exit(_ArgTypes... __args);
2207
2208 void reset();
2209};
2210
2211template<class ..._ArgTypes>
2212void
2213packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args)
2214{
Howard Hinnant54da3382010-08-30 18:46:21 +00002215 if (__p_.__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00002216 __throw_future_error(future_errc::no_state);
Howard Hinnant54da3382010-08-30 18:46:21 +00002217 if (__p_.__state_->__has_value())
Eric Fiselier423ca202015-10-02 21:25:15 +00002218 __throw_future_error(future_errc::promise_already_satisfied);
Marshall Clowa1899742015-09-03 15:11:32 +00002219#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant54da3382010-08-30 18:46:21 +00002220 try
2221 {
2222#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:19 +00002223 __f_(_VSTD::forward<_ArgTypes>(__args)...);
Howard Hinnant54da3382010-08-30 18:46:21 +00002224 __p_.set_value();
2225#ifndef _LIBCPP_NO_EXCEPTIONS
2226 }
2227 catch (...)
2228 {
2229 __p_.set_exception(current_exception());
2230 }
2231#endif // _LIBCPP_NO_EXCEPTIONS
2232}
2233
2234template<class ..._ArgTypes>
2235void
2236packaged_task<void(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
2237{
Howard Hinnant54da3382010-08-30 18:46:21 +00002238 if (__p_.__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:15 +00002239 __throw_future_error(future_errc::no_state);
Howard Hinnant54da3382010-08-30 18:46:21 +00002240 if (__p_.__state_->__has_value())
Eric Fiselier423ca202015-10-02 21:25:15 +00002241 __throw_future_error(future_errc::promise_already_satisfied);
Marshall Clowa1899742015-09-03 15:11:32 +00002242#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant54da3382010-08-30 18:46:21 +00002243 try
2244 {
2245#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:19 +00002246 __f_(_VSTD::forward<_ArgTypes>(__args)...);
Howard Hinnant54da3382010-08-30 18:46:21 +00002247 __p_.set_value_at_thread_exit();
2248#ifndef _LIBCPP_NO_EXCEPTIONS
2249 }
2250 catch (...)
2251 {
2252 __p_.set_exception_at_thread_exit(current_exception());
2253 }
2254#endif // _LIBCPP_NO_EXCEPTIONS
2255}
2256
2257template<class ..._ArgTypes>
2258void
2259packaged_task<void(_ArgTypes...)>::reset()
2260{
Howard Hinnant7de47902010-11-30 20:23:32 +00002261 if (!valid())
Eric Fiselier423ca202015-10-02 21:25:15 +00002262 __throw_future_error(future_errc::no_state);
Howard Hinnant54da3382010-08-30 18:46:21 +00002263 __p_ = promise<result_type>();
2264}
2265
2266template <class _Callable>
2267inline _LIBCPP_INLINE_VISIBILITY
2268void
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002269swap(packaged_task<_Callable>& __x, packaged_task<_Callable>& __y) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:21 +00002270{
2271 __x.swap(__y);
2272}
2273
2274template <class _Callable, class _Alloc>
Eric Fiselierc3589a82017-01-04 23:56:00 +00002275struct _LIBCPP_TEMPLATE_VIS uses_allocator<packaged_task<_Callable>, _Alloc>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002276 : public true_type {};
Howard Hinnant54da3382010-08-30 18:46:21 +00002277
Howard Hinnant99968442011-11-29 18:15:50 +00002278template <class _Rp, class _Fp>
2279future<_Rp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00002280#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00002281__make_deferred_assoc_state(_Fp&& __f)
Howard Hinnant54da3382010-08-30 18:46:21 +00002282#else
Howard Hinnant99968442011-11-29 18:15:50 +00002283__make_deferred_assoc_state(_Fp __f)
Howard Hinnant54da3382010-08-30 18:46:21 +00002284#endif
2285{
Howard Hinnant99968442011-11-29 18:15:50 +00002286 unique_ptr<__deferred_assoc_state<_Rp, _Fp>, __release_shared_count>
2287 __h(new __deferred_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
2288 return future<_Rp>(__h.get());
Howard Hinnant54da3382010-08-30 18:46:21 +00002289}
2290
Howard Hinnant99968442011-11-29 18:15:50 +00002291template <class _Rp, class _Fp>
2292future<_Rp>
Howard Hinnant57cff292011-05-19 15:05:04 +00002293#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00002294__make_async_assoc_state(_Fp&& __f)
Howard Hinnant57cff292011-05-19 15:05:04 +00002295#else
Howard Hinnant99968442011-11-29 18:15:50 +00002296__make_async_assoc_state(_Fp __f)
Howard Hinnant57cff292011-05-19 15:05:04 +00002297#endif
2298{
Howard Hinnant99968442011-11-29 18:15:50 +00002299 unique_ptr<__async_assoc_state<_Rp, _Fp>, __release_shared_count>
2300 __h(new __async_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
2301 _VSTD::thread(&__async_assoc_state<_Rp, _Fp>::__execute, __h.get()).detach();
2302 return future<_Rp>(__h.get());
Howard Hinnant57cff292011-05-19 15:05:04 +00002303}
2304
Howard Hinnant99968442011-11-29 18:15:50 +00002305template <class _Fp, class... _Args>
Howard Hinnant57cff292011-05-19 15:05:04 +00002306class __async_func
2307{
Howard Hinnant99968442011-11-29 18:15:50 +00002308 tuple<_Fp, _Args...> __f_;
Howard Hinnant57cff292011-05-19 15:05:04 +00002309
2310public:
Howard Hinnant99968442011-11-29 18:15:50 +00002311 typedef typename __invoke_of<_Fp, _Args...>::type _Rp;
Howard Hinnant57cff292011-05-19 15:05:04 +00002312
2313 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00002314 explicit __async_func(_Fp&& __f, _Args&&... __args)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002315 : __f_(_VSTD::move(__f), _VSTD::move(__args)...) {}
Howard Hinnant57cff292011-05-19 15:05:04 +00002316
2317 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0949eed2011-06-30 21:18:19 +00002318 __async_func(__async_func&& __f) : __f_(_VSTD::move(__f.__f_)) {}
Howard Hinnant57cff292011-05-19 15:05:04 +00002319
Howard Hinnant99968442011-11-29 18:15:50 +00002320 _Rp operator()()
Howard Hinnant57cff292011-05-19 15:05:04 +00002321 {
2322 typedef typename __make_tuple_indices<1+sizeof...(_Args), 1>::type _Index;
2323 return __execute(_Index());
2324 }
2325private:
2326 template <size_t ..._Indices>
Howard Hinnant99968442011-11-29 18:15:50 +00002327 _Rp
Howard Hinnant57cff292011-05-19 15:05:04 +00002328 __execute(__tuple_indices<_Indices...>)
2329 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002330 return __invoke(_VSTD::move(_VSTD::get<0>(__f_)), _VSTD::move(_VSTD::get<_Indices>(__f_))...);
Howard Hinnant57cff292011-05-19 15:05:04 +00002331 }
2332};
2333
Marshall Clow3b3108e2013-11-03 22:06:53 +00002334inline _LIBCPP_INLINE_VISIBILITY bool __does_policy_contain(launch __policy, launch __value )
Marshall Clowad2a6002013-11-03 15:43:35 +00002335{ return (int(__policy) & int(__value)) != 0; }
2336
Howard Hinnant99968442011-11-29 18:15:50 +00002337template <class _Fp, class... _Args>
2338future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
2339async(launch __policy, _Fp&& __f, _Args&&... __args)
Howard Hinnant54da3382010-08-30 18:46:21 +00002340{
Howard Hinnant99968442011-11-29 18:15:50 +00002341 typedef __async_func<typename decay<_Fp>::type, typename decay<_Args>::type...> _BF;
2342 typedef typename _BF::_Rp _Rp;
Marshall Clowad2a6002013-11-03 15:43:35 +00002343
2344#ifndef _LIBCPP_NO_EXCEPTIONS
2345 try
2346 {
2347#endif
2348 if (__does_policy_contain(__policy, launch::async))
2349 return _VSTD::__make_async_assoc_state<_Rp>(_BF(__decay_copy(_VSTD::forward<_Fp>(__f)),
Howard Hinnant0949eed2011-06-30 21:18:19 +00002350 __decay_copy(_VSTD::forward<_Args>(__args))...));
Marshall Clowad2a6002013-11-03 15:43:35 +00002351#ifndef _LIBCPP_NO_EXCEPTIONS
2352 }
2353 catch ( ... ) { if (__policy == launch::async) throw ; }
2354#endif
2355
2356 if (__does_policy_contain(__policy, launch::deferred))
2357 return _VSTD::__make_deferred_assoc_state<_Rp>(_BF(__decay_copy(_VSTD::forward<_Fp>(__f)),
Howard Hinnant0949eed2011-06-30 21:18:19 +00002358 __decay_copy(_VSTD::forward<_Args>(__args))...));
Marshall Clowad2a6002013-11-03 15:43:35 +00002359 return future<_Rp>{};
Howard Hinnant54da3382010-08-30 18:46:21 +00002360}
2361
Howard Hinnant99968442011-11-29 18:15:50 +00002362template <class _Fp, class... _Args>
Howard Hinnant54da3382010-08-30 18:46:21 +00002363inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00002364future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
2365async(_Fp&& __f, _Args&&... __args)
Howard Hinnant54da3382010-08-30 18:46:21 +00002366{
Howard Hinnant99968442011-11-29 18:15:50 +00002367 return _VSTD::async(launch::any, _VSTD::forward<_Fp>(__f),
Howard Hinnant0949eed2011-06-30 21:18:19 +00002368 _VSTD::forward<_Args>(__args)...);
Howard Hinnant54da3382010-08-30 18:46:21 +00002369}
2370
2371#endif // _LIBCPP_HAS_NO_VARIADICS
2372
Howard Hinnante6e4d012010-09-03 21:46:37 +00002373// shared_future
2374
Howard Hinnant99968442011-11-29 18:15:50 +00002375template <class _Rp>
Eric Fiselierc3589a82017-01-04 23:56:00 +00002376class _LIBCPP_TEMPLATE_VIS shared_future
Howard Hinnant99be8232010-09-03 18:39:25 +00002377{
Howard Hinnant99968442011-11-29 18:15:50 +00002378 __assoc_state<_Rp>* __state_;
Howard Hinnant99be8232010-09-03 18:39:25 +00002379
2380public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002381 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002382 shared_future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002383 _LIBCPP_INLINE_VISIBILITY
Marshall Clow3d7c49b2016-11-14 19:58:05 +00002384 shared_future(const shared_future& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
Howard Hinnant99be8232010-09-03 18:39:25 +00002385 {if (__state_) __state_->__add_shared();}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002386#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002387 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002388 shared_future(future<_Rp>&& __f) _NOEXCEPT : __state_(__f.__state_)
Howard Hinnant99be8232010-09-03 18:39:25 +00002389 {__f.__state_ = nullptr;}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002390 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002391 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
Howard Hinnant99be8232010-09-03 18:39:25 +00002392 {__rhs.__state_ = nullptr;}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002393#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:25 +00002394 ~shared_future();
Marshall Clow3d7c49b2016-11-14 19:58:05 +00002395 shared_future& operator=(const shared_future& __rhs) _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00002396#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002397 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002398 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:25 +00002399 {
2400 shared_future(std::move(__rhs)).swap(*this);
2401 return *this;
2402 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002403#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:25 +00002404
2405 // retrieving the value
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002406 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00002407 const _Rp& get() const {return __state_->copy();}
Howard Hinnant99be8232010-09-03 18:39:25 +00002408
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002409 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002410 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant99be8232010-09-03 18:39:25 +00002411
2412 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002413 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002414 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant99be8232010-09-03 18:39:25 +00002415
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002416 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002417 void wait() const {__state_->wait();}
2418 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002419 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002420 future_status
2421 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2422 {return __state_->wait_for(__rel_time);}
2423 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002424 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002425 future_status
2426 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2427 {return __state_->wait_until(__abs_time);}
2428};
2429
Howard Hinnant99968442011-11-29 18:15:50 +00002430template <class _Rp>
2431shared_future<_Rp>::~shared_future()
Howard Hinnant99be8232010-09-03 18:39:25 +00002432{
2433 if (__state_)
2434 __state_->__release_shared();
2435}
2436
Howard Hinnant99968442011-11-29 18:15:50 +00002437template <class _Rp>
2438shared_future<_Rp>&
Marshall Clow3d7c49b2016-11-14 19:58:05 +00002439shared_future<_Rp>::operator=(const shared_future& __rhs) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:25 +00002440{
2441 if (__rhs.__state_)
2442 __rhs.__state_->__add_shared();
2443 if (__state_)
2444 __state_->__release_shared();
2445 __state_ = __rhs.__state_;
2446 return *this;
2447}
2448
Howard Hinnant99968442011-11-29 18:15:50 +00002449template <class _Rp>
Eric Fiselierc3589a82017-01-04 23:56:00 +00002450class _LIBCPP_TEMPLATE_VIS shared_future<_Rp&>
Howard Hinnant99be8232010-09-03 18:39:25 +00002451{
Howard Hinnant99968442011-11-29 18:15:50 +00002452 __assoc_state<_Rp&>* __state_;
Howard Hinnant99be8232010-09-03 18:39:25 +00002453
2454public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002455 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002456 shared_future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002457 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002458 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2459 {if (__state_) __state_->__add_shared();}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002460#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002461 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002462 shared_future(future<_Rp&>&& __f) _NOEXCEPT : __state_(__f.__state_)
Howard Hinnant99be8232010-09-03 18:39:25 +00002463 {__f.__state_ = nullptr;}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002464 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002465 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
Howard Hinnant99be8232010-09-03 18:39:25 +00002466 {__rhs.__state_ = nullptr;}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002467#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:25 +00002468 ~shared_future();
2469 shared_future& operator=(const shared_future& __rhs);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002470#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002471 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002472 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:25 +00002473 {
2474 shared_future(std::move(__rhs)).swap(*this);
2475 return *this;
2476 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002477#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:25 +00002478
2479 // retrieving the value
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002480 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00002481 _Rp& get() const {return __state_->copy();}
Howard Hinnant99be8232010-09-03 18:39:25 +00002482
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002483 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002484 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant99be8232010-09-03 18:39:25 +00002485
2486 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002487 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002488 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant99be8232010-09-03 18:39:25 +00002489
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002490 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002491 void wait() const {__state_->wait();}
2492 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002493 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002494 future_status
2495 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2496 {return __state_->wait_for(__rel_time);}
2497 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002498 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002499 future_status
2500 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2501 {return __state_->wait_until(__abs_time);}
2502};
2503
Howard Hinnant99968442011-11-29 18:15:50 +00002504template <class _Rp>
2505shared_future<_Rp&>::~shared_future()
Howard Hinnant99be8232010-09-03 18:39:25 +00002506{
2507 if (__state_)
2508 __state_->__release_shared();
2509}
2510
Howard Hinnant99968442011-11-29 18:15:50 +00002511template <class _Rp>
2512shared_future<_Rp&>&
2513shared_future<_Rp&>::operator=(const shared_future& __rhs)
Howard Hinnant99be8232010-09-03 18:39:25 +00002514{
2515 if (__rhs.__state_)
2516 __rhs.__state_->__add_shared();
2517 if (__state_)
2518 __state_->__release_shared();
2519 __state_ = __rhs.__state_;
2520 return *this;
2521}
2522
2523template <>
Mehdi Amini907c1192017-05-04 17:08:54 +00002524class _LIBCPP_TYPE_VIS _LIBCPP_AVAILABILITY_FUTURE shared_future<void>
Howard Hinnant99be8232010-09-03 18:39:25 +00002525{
2526 __assoc_sub_state* __state_;
2527
2528public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002529 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002530 shared_future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002531 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002532 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2533 {if (__state_) __state_->__add_shared();}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002534#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002535 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002536 shared_future(future<void>&& __f) _NOEXCEPT : __state_(__f.__state_)
Howard Hinnant99be8232010-09-03 18:39:25 +00002537 {__f.__state_ = nullptr;}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002538 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002539 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
Howard Hinnant99be8232010-09-03 18:39:25 +00002540 {__rhs.__state_ = nullptr;}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002541#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:25 +00002542 ~shared_future();
2543 shared_future& operator=(const shared_future& __rhs);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002544#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002545 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002546 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:25 +00002547 {
2548 shared_future(std::move(__rhs)).swap(*this);
2549 return *this;
2550 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002551#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:25 +00002552
2553 // retrieving the value
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002554 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002555 void get() const {__state_->copy();}
2556
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002557 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002558 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant99be8232010-09-03 18:39:25 +00002559
2560 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002562 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant99be8232010-09-03 18:39:25 +00002563
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002564 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002565 void wait() const {__state_->wait();}
2566 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002567 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002568 future_status
2569 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2570 {return __state_->wait_for(__rel_time);}
2571 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002572 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002573 future_status
2574 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2575 {return __state_->wait_until(__abs_time);}
2576};
2577
Howard Hinnant99968442011-11-29 18:15:50 +00002578template <class _Rp>
Howard Hinnant99be8232010-09-03 18:39:25 +00002579inline _LIBCPP_INLINE_VISIBILITY
2580void
Howard Hinnant8bf01dd2012-07-21 17:46:55 +00002581swap(shared_future<_Rp>& __x, shared_future<_Rp>& __y) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:25 +00002582{
2583 __x.swap(__y);
2584}
2585
Howard Hinnant99968442011-11-29 18:15:50 +00002586template <class _Rp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00002587inline
Howard Hinnant99968442011-11-29 18:15:50 +00002588shared_future<_Rp>
Marshall Clow9bb0cca2017-01-24 23:28:25 +00002589future<_Rp>::share() _NOEXCEPT
Howard Hinnante6e4d012010-09-03 21:46:37 +00002590{
Howard Hinnant99968442011-11-29 18:15:50 +00002591 return shared_future<_Rp>(_VSTD::move(*this));
Howard Hinnante6e4d012010-09-03 21:46:37 +00002592}
2593
Howard Hinnant99968442011-11-29 18:15:50 +00002594template <class _Rp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00002595inline
Howard Hinnant99968442011-11-29 18:15:50 +00002596shared_future<_Rp&>
Marshall Clow9bb0cca2017-01-24 23:28:25 +00002597future<_Rp&>::share() _NOEXCEPT
Howard Hinnante6e4d012010-09-03 21:46:37 +00002598{
Howard Hinnant99968442011-11-29 18:15:50 +00002599 return shared_future<_Rp&>(_VSTD::move(*this));
Howard Hinnant7de47902010-11-30 20:23:32 +00002600}
2601
Howard Hinnanta4451512010-12-02 16:45:21 +00002602#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2603
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00002604inline
Howard Hinnant7de47902010-11-30 20:23:32 +00002605shared_future<void>
Marshall Clow9bb0cca2017-01-24 23:28:25 +00002606future<void>::share() _NOEXCEPT
Howard Hinnant7de47902010-11-30 20:23:32 +00002607{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002608 return shared_future<void>(_VSTD::move(*this));
Howard Hinnante6e4d012010-09-03 21:46:37 +00002609}
2610
Howard Hinnanta4451512010-12-02 16:45:21 +00002611#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2612
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002613_LIBCPP_END_NAMESPACE_STD
2614
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +00002615#endif // !_LIBCPP_HAS_NO_THREADS
2616
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002617#endif // _LIBCPP_FUTURE