blob: 62529d97c75c4a654fb7b66e0991fb1e890a65c3 [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{
22 broken_promise,
23 future_already_retrieved,
24 promise_already_satisfied,
25 no_state
26};
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 { };
43error_code make_error_code(future_errc e);
44error_condition make_error_condition(future_errc e);
45
46const error_category& future_category();
47
48class future_error
49 : public logic_error
50{
51public:
52 future_error(error_code ec); // exposition only
53
54 const error_code& code() const throw();
55 const char* what() const throw();
56};
57
58template <class R>
59class promise
60{
61public:
62 promise();
63 template <class Allocator>
64 promise(allocator_arg_t, const Allocator& a);
65 promise(promise&& rhs);
66 promise(const promise& rhs) = delete;
67 ~promise();
68
69 // assignment
70 promise& operator=(promise&& rhs);
71 promise& operator=(const promise& rhs) = delete;
72 void swap(promise& other);
73
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);
95 promise(promise&& rhs);
96 promise(const promise& rhs) = delete;
97 ~promise();
98
99 // assignment
100 promise& operator=(promise&& rhs);
101 promise& operator=(const promise& rhs) = delete;
102 void swap(promise& other);
103
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);
123 promise(promise&& rhs);
124 promise(const promise& rhs) = delete;
125 ~promise();
126
127 // assignment
128 promise& operator=(promise&& rhs);
129 promise& operator=(const promise& rhs) = delete;
130 void swap(promise& other);
131
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
144template <class R> void swap(promise<R>& x, promise<R>& y);
145
146template <class R, class Alloc>
147 struct uses_allocator<promise<R>, Alloc> : public true_type {};
148
149template <class R>
150class future
151{
152public:
153 future();
154 future(future&&);
155 future(const future& rhs) = delete;
156 ~future();
157 future& operator=(const future& rhs) = delete;
158 future& operator=(future&&);
Howard Hinnant7de47902010-11-30 20:23:32 +0000159 shared_future<R> share() &&;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000160
161 // retrieving the value
162 R get();
163
164 // functions to check state
165 bool valid() const;
166
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:
180 future();
181 future(future&&);
182 future(const future& rhs) = delete;
183 ~future();
184 future& operator=(const future& rhs) = delete;
185 future& operator=(future&&);
Howard Hinnant7de47902010-11-30 20:23:32 +0000186 shared_future<R&> share() &&;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000187
188 // retrieving the value
189 R& get();
190
191 // functions to check state
192 bool valid() const;
193
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:
207 future();
208 future(future&&);
209 future(const future& rhs) = delete;
210 ~future();
211 future& operator=(const future& rhs) = delete;
212 future& operator=(future&&);
Howard Hinnant7de47902010-11-30 20:23:32 +0000213 shared_future<void> share() &&;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000214
215 // retrieving the value
216 void get();
217
218 // functions to check state
219 bool valid() const;
220
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:
234 shared_future();
235 shared_future(const shared_future& rhs);
236 shared_future(future<R>&&);
237 shared_future(shared_future&& rhs);
238 ~shared_future();
239 shared_future& operator=(const shared_future& rhs);
240 shared_future& operator=(shared_future&& rhs);
241
242 // retrieving the value
243 const R& get() const;
244
245 // functions to check state
246 bool valid() const;
247
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:
261 shared_future();
262 shared_future(const shared_future& rhs);
Howard Hinnant99be8232010-09-03 18:39:25 +0000263 shared_future(future<R&>&&);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000264 shared_future(shared_future&& rhs);
265 ~shared_future();
266 shared_future& operator=(const shared_future& rhs);
267 shared_future& operator=(shared_future&& rhs);
268
269 // retrieving the value
270 R& get() const;
271
272 // functions to check state
273 bool valid() const;
274
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:
288 shared_future();
289 shared_future(const shared_future& rhs);
Howard Hinnant99be8232010-09-03 18:39:25 +0000290 shared_future(future<void>&&);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000291 shared_future(shared_future&& rhs);
292 ~shared_future();
293 shared_future& operator=(const shared_future& rhs);
294 shared_future& operator=(shared_future&& rhs);
295
296 // retrieving the value
297 void get() const;
298
299 // functions to check state
300 bool valid() const;
301
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>
312 future<typename result_of<F(Args...)>::type>
313 async(F&& f, Args&&... args);
314
315template <class F, class... Args>
316 future<typename result_of<F(Args...)>::type>
317 async(launch policy, F&& f, Args&&... args);
318
Howard Hinnantf5256e12010-05-11 21:36:01 +0000319template <class> class packaged_task; // undefined
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000320
321template <class R, class... ArgTypes>
322class packaged_task<R(ArgTypes...)>
323{
324public:
325 typedef R result_type;
326
327 // construction and destruction
328 packaged_task();
329 template <class F>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000330 explicit packaged_task(F&& f);
331 template <class F, class Allocator>
332 explicit packaged_task(allocator_arg_t, const Allocator& a, F&& f);
333 ~packaged_task();
334
335 // no copy
336 packaged_task(packaged_task&) = delete;
337 packaged_task& operator=(packaged_task&) = delete;
338
339 // move support
340 packaged_task(packaged_task&& other);
341 packaged_task& operator=(packaged_task&& other);
342 void swap(packaged_task& other);
343
Howard Hinnant7de47902010-11-30 20:23:32 +0000344 bool valid() const;
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>
357 void swap(packaged_task<R(ArgTypes...)&, packaged_task<R(ArgTypes...)>&);
358
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
373#pragma GCC system_header
374
375_LIBCPP_BEGIN_NAMESPACE_STD
376
377//enum class future_errc
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000378struct _LIBCPP_VISIBLE future_errc
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000379{
380enum _ {
381 broken_promise,
382 future_already_retrieved,
383 promise_already_satisfied,
384 no_state
385};
386
387 _ __v_;
388
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000389 _LIBCPP_INLINE_VISIBILITY future_errc(_ __v) : __v_(__v) {}
390 _LIBCPP_INLINE_VISIBILITY operator int() const {return __v_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000391
392};
393
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000394template <>
395struct _LIBCPP_VISIBLE is_error_code_enum<future_errc> : public true_type {};
Howard Hinnanta6521722010-08-25 17:32:05 +0000396
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000397//enum class launch
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000398struct _LIBCPP_VISIBLE launch
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000399{
400enum _ {
Howard Hinnant66895642010-11-23 18:33:54 +0000401 async = 1,
402 deferred = 2,
403 any = async | deferred
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000404};
405
406 _ __v_;
407
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000408 _LIBCPP_INLINE_VISIBILITY launch(_ __v) : __v_(__v) {}
409 _LIBCPP_INLINE_VISIBILITY operator int() const {return __v_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000410
411};
412
413//enum class future_status
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000414struct _LIBCPP_VISIBLE future_status
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000415{
416enum _ {
417 ready,
418 timeout,
419 deferred
420};
421
422 _ __v_;
423
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000424 _LIBCPP_INLINE_VISIBILITY future_status(_ __v) : __v_(__v) {}
425 _LIBCPP_INLINE_VISIBILITY operator int() const {return __v_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000426
427};
428
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000429_LIBCPP_VISIBLE
Howard Hinnanta6521722010-08-25 17:32:05 +0000430const error_category& future_category();
431
432inline _LIBCPP_INLINE_VISIBILITY
433error_code
434make_error_code(future_errc __e)
435{
436 return error_code(static_cast<int>(__e), future_category());
437}
438
439inline _LIBCPP_INLINE_VISIBILITY
440error_condition
441make_error_condition(future_errc __e)
442{
443 return error_condition(static_cast<int>(__e), future_category());
444}
445
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000446class _LIBCPP_EXCEPTION_ABI future_error
Howard Hinnanta6521722010-08-25 17:32:05 +0000447 : public logic_error
448{
449 error_code __ec_;
450public:
451 future_error(error_code __ec);
452
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000453 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6521722010-08-25 17:32:05 +0000454 const error_code& code() const throw() {return __ec_;}
Howard Hinnantac6de542011-07-07 21:03:52 +0000455
456 virtual ~future_error() _NOEXCEPT;
Howard Hinnanta6521722010-08-25 17:32:05 +0000457};
458
Howard Hinnant47499b12010-08-27 20:10:19 +0000459class __assoc_sub_state
460 : public __shared_count
461{
462protected:
463 exception_ptr __exception_;
464 mutable mutex __mut_;
465 mutable condition_variable __cv_;
466 unsigned __state_;
467
Howard Hinnant1694d232011-05-28 14:41:13 +0000468 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant54da3382010-08-30 18:46:21 +0000469 void __sub_wait(unique_lock<mutex>& __lk);
Howard Hinnant47499b12010-08-27 20:10:19 +0000470public:
471 enum
472 {
473 __constructed = 1,
474 __future_attached = 2,
475 ready = 4,
476 deferred = 8
477 };
478
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000479 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000480 __assoc_sub_state() : __state_(0) {}
481
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000482 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000483 bool __has_value() const
484 {return (__state_ & __constructed) || (__exception_ != nullptr);}
485
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000486 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000487 void __set_future_attached() {__state_ |= __future_attached;}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000488 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000489 bool __has_future_attached() const {return __state_ & __future_attached;}
490
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000491 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +0000492 void __set_deferred() {__state_ |= deferred;}
493
Howard Hinnant47499b12010-08-27 20:10:19 +0000494 void __make_ready();
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000495 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000496 bool __is_ready() const {return __state_ & ready;}
497
498 void set_value();
499 void set_value_at_thread_exit();
500
501 void set_exception(exception_ptr __p);
502 void set_exception_at_thread_exit(exception_ptr __p);
503
504 void copy();
505
Howard Hinnant54da3382010-08-30 18:46:21 +0000506 void wait();
Howard Hinnant47499b12010-08-27 20:10:19 +0000507 template <class _Rep, class _Period>
508 future_status
509 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const;
510 template <class _Clock, class _Duration>
511 future_status
512 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const;
Howard Hinnant54da3382010-08-30 18:46:21 +0000513
514 virtual void __execute();
Howard Hinnant47499b12010-08-27 20:10:19 +0000515};
516
Howard Hinnantf39daa82010-08-28 21:01:06 +0000517template <class _Clock, class _Duration>
518future_status
519__assoc_sub_state::wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
520{
521 unique_lock<mutex> __lk(__mut_);
Howard Hinnant54da3382010-08-30 18:46:21 +0000522 if (__state_ & deferred)
523 return future_status::deferred;
524 while (!(__state_ & ready) && _Clock::now() < __abs_time)
Howard Hinnantf39daa82010-08-28 21:01:06 +0000525 __cv_.wait_until(__lk, __abs_time);
526 if (__state_ & ready)
527 return future_status::ready;
Howard Hinnantf39daa82010-08-28 21:01:06 +0000528 return future_status::timeout;
529}
530
531template <class _Rep, class _Period>
532inline _LIBCPP_INLINE_VISIBILITY
533future_status
534__assoc_sub_state::wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
535{
Howard Hinnantf8f85212010-11-20 19:16:30 +0000536 return wait_until(chrono::steady_clock::now() + __rel_time);
Howard Hinnantf39daa82010-08-28 21:01:06 +0000537}
538
Howard Hinnant47499b12010-08-27 20:10:19 +0000539template <class _R>
540class __assoc_state
541 : public __assoc_sub_state
542{
543 typedef __assoc_sub_state base;
544 typedef typename aligned_storage<sizeof(_R), alignment_of<_R>::value>::type _U;
545protected:
546 _U __value_;
547
Howard Hinnant1694d232011-05-28 14:41:13 +0000548 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant47499b12010-08-27 20:10:19 +0000549public:
550
551 template <class _Arg>
Howard Hinnant73d21a42010-09-04 23:28:19 +0000552#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +0000553 void set_value(_Arg&& __arg);
554#else
555 void set_value(_Arg& __arg);
556#endif
557
558 template <class _Arg>
Howard Hinnant73d21a42010-09-04 23:28:19 +0000559#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +0000560 void set_value_at_thread_exit(_Arg&& __arg);
561#else
562 void set_value_at_thread_exit(_Arg& __arg);
563#endif
564
565 _R move();
566 typename add_lvalue_reference<_R>::type copy();
567};
568
569template <class _R>
570void
Howard Hinnant1694d232011-05-28 14:41:13 +0000571__assoc_state<_R>::__on_zero_shared() _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +0000572{
573 if (this->__state_ & base::__constructed)
574 reinterpret_cast<_R*>(&__value_)->~_R();
575 delete this;
576}
577
578template <class _R>
579template <class _Arg>
580void
Howard Hinnant73d21a42010-09-04 23:28:19 +0000581#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +0000582__assoc_state<_R>::set_value(_Arg&& __arg)
583#else
584__assoc_state<_R>::set_value(_Arg& __arg)
585#endif
586{
587 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant22ba71b2011-07-13 16:00:50 +0000588#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:19 +0000589 if (this->__has_value())
590 throw future_error(make_error_code(future_errc::promise_already_satisfied));
Howard Hinnant22ba71b2011-07-13 16:00:50 +0000591#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +0000592 ::new(&__value_) _R(_VSTD::forward<_Arg>(__arg));
Howard Hinnant47499b12010-08-27 20:10:19 +0000593 this->__state_ |= base::__constructed | base::ready;
594 __lk.unlock();
595 __cv_.notify_all();
596}
597
598template <class _R>
599template <class _Arg>
600void
Howard Hinnant73d21a42010-09-04 23:28:19 +0000601#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +0000602__assoc_state<_R>::set_value_at_thread_exit(_Arg&& __arg)
603#else
604__assoc_state<_R>::set_value_at_thread_exit(_Arg& __arg)
605#endif
606{
607 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant22ba71b2011-07-13 16:00:50 +0000608#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:19 +0000609 if (this->__has_value())
610 throw future_error(make_error_code(future_errc::promise_already_satisfied));
Howard Hinnant22ba71b2011-07-13 16:00:50 +0000611#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +0000612 ::new(&__value_) _R(_VSTD::forward<_Arg>(__arg));
Howard Hinnant47499b12010-08-27 20:10:19 +0000613 this->__state_ |= base::__constructed;
Howard Hinnant5306d682010-10-14 19:18:04 +0000614 __thread_local_data()->__make_ready_at_thread_exit(this);
Howard Hinnant47499b12010-08-27 20:10:19 +0000615 __lk.unlock();
616}
617
618template <class _R>
619_R
620__assoc_state<_R>::move()
621{
622 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant54da3382010-08-30 18:46:21 +0000623 this->__sub_wait(__lk);
Howard Hinnant47499b12010-08-27 20:10:19 +0000624 if (this->__exception_ != nullptr)
625 rethrow_exception(this->__exception_);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000626 return _VSTD::move(*reinterpret_cast<_R*>(&__value_));
Howard Hinnant47499b12010-08-27 20:10:19 +0000627}
628
629template <class _R>
630typename add_lvalue_reference<_R>::type
631__assoc_state<_R>::copy()
632{
633 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant54da3382010-08-30 18:46:21 +0000634 this->__sub_wait(__lk);
Howard Hinnant47499b12010-08-27 20:10:19 +0000635 if (this->__exception_ != nullptr)
636 rethrow_exception(this->__exception_);
637 return *reinterpret_cast<_R*>(&__value_);
638}
639
Howard Hinnantf39daa82010-08-28 21:01:06 +0000640template <class _R>
641class __assoc_state<_R&>
642 : public __assoc_sub_state
643{
644 typedef __assoc_sub_state base;
645 typedef _R* _U;
646protected:
647 _U __value_;
648
Howard Hinnant1694d232011-05-28 14:41:13 +0000649 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnantf39daa82010-08-28 21:01:06 +0000650public:
651
652 void set_value(_R& __arg);
653 void set_value_at_thread_exit(_R& __arg);
654
655 _R& copy();
656};
657
658template <class _R>
659void
Howard Hinnant1694d232011-05-28 14:41:13 +0000660__assoc_state<_R&>::__on_zero_shared() _NOEXCEPT
Howard Hinnantf39daa82010-08-28 21:01:06 +0000661{
662 delete this;
663}
664
665template <class _R>
666void
667__assoc_state<_R&>::set_value(_R& __arg)
668{
669 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant22ba71b2011-07-13 16:00:50 +0000670#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantf39daa82010-08-28 21:01:06 +0000671 if (this->__has_value())
672 throw future_error(make_error_code(future_errc::promise_already_satisfied));
Howard Hinnant22ba71b2011-07-13 16:00:50 +0000673#endif
Howard Hinnantf39daa82010-08-28 21:01:06 +0000674 __value_ = &__arg;
675 this->__state_ |= base::__constructed | base::ready;
676 __lk.unlock();
677 __cv_.notify_all();
678}
679
680template <class _R>
681void
682__assoc_state<_R&>::set_value_at_thread_exit(_R& __arg)
683{
684 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant22ba71b2011-07-13 16:00:50 +0000685#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantf39daa82010-08-28 21:01:06 +0000686 if (this->__has_value())
687 throw future_error(make_error_code(future_errc::promise_already_satisfied));
Howard Hinnant22ba71b2011-07-13 16:00:50 +0000688#endif
Howard Hinnantf39daa82010-08-28 21:01:06 +0000689 __value_ = &__arg;
690 this->__state_ |= base::__constructed;
Howard Hinnant5306d682010-10-14 19:18:04 +0000691 __thread_local_data()->__make_ready_at_thread_exit(this);
Howard Hinnantf39daa82010-08-28 21:01:06 +0000692 __lk.unlock();
693}
694
695template <class _R>
696_R&
697__assoc_state<_R&>::copy()
698{
699 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant54da3382010-08-30 18:46:21 +0000700 this->__sub_wait(__lk);
Howard Hinnantf39daa82010-08-28 21:01:06 +0000701 if (this->__exception_ != nullptr)
702 rethrow_exception(this->__exception_);
703 return *__value_;
704}
705
Howard Hinnant47499b12010-08-27 20:10:19 +0000706template <class _R, class _Alloc>
707class __assoc_state_alloc
708 : public __assoc_state<_R>
709{
710 typedef __assoc_state<_R> base;
711 _Alloc __alloc_;
712
Howard Hinnant1694d232011-05-28 14:41:13 +0000713 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant47499b12010-08-27 20:10:19 +0000714public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000715 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000716 explicit __assoc_state_alloc(const _Alloc& __a)
717 : __alloc_(__a) {}
718};
719
720template <class _R, class _Alloc>
721void
Howard Hinnant1694d232011-05-28 14:41:13 +0000722__assoc_state_alloc<_R, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +0000723{
724 if (this->__state_ & base::__constructed)
725 reinterpret_cast<_R*>(&this->__value_)->~_R();
726 typename _Alloc::template rebind<__assoc_state_alloc>::other __a(__alloc_);
727 this->~__assoc_state_alloc();
728 __a.deallocate(this, 1);
729}
730
Howard Hinnantf39daa82010-08-28 21:01:06 +0000731template <class _R, class _Alloc>
732class __assoc_state_alloc<_R&, _Alloc>
733 : public __assoc_state<_R&>
734{
735 typedef __assoc_state<_R&> base;
736 _Alloc __alloc_;
737
Howard Hinnant1694d232011-05-28 14:41:13 +0000738 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnantf39daa82010-08-28 21:01:06 +0000739public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000740 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39daa82010-08-28 21:01:06 +0000741 explicit __assoc_state_alloc(const _Alloc& __a)
742 : __alloc_(__a) {}
743};
744
745template <class _R, class _Alloc>
746void
Howard Hinnant1694d232011-05-28 14:41:13 +0000747__assoc_state_alloc<_R&, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantf39daa82010-08-28 21:01:06 +0000748{
749 typename _Alloc::template rebind<__assoc_state_alloc>::other __a(__alloc_);
750 this->~__assoc_state_alloc();
751 __a.deallocate(this, 1);
752}
753
Howard Hinnant47499b12010-08-27 20:10:19 +0000754template <class _Alloc>
755class __assoc_sub_state_alloc
756 : public __assoc_sub_state
757{
758 typedef __assoc_sub_state base;
759 _Alloc __alloc_;
760
Howard Hinnant1694d232011-05-28 14:41:13 +0000761 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant47499b12010-08-27 20:10:19 +0000762public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000763 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000764 explicit __assoc_sub_state_alloc(const _Alloc& __a)
765 : __alloc_(__a) {}
766};
767
768template <class _Alloc>
769void
Howard Hinnant1694d232011-05-28 14:41:13 +0000770__assoc_sub_state_alloc<_Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +0000771{
772 this->~base();
Howard Hinnantf39daa82010-08-28 21:01:06 +0000773 typename _Alloc::template rebind<__assoc_sub_state_alloc>::other __a(__alloc_);
Howard Hinnant47499b12010-08-27 20:10:19 +0000774 this->~__assoc_sub_state_alloc();
775 __a.deallocate(this, 1);
776}
777
Howard Hinnant54da3382010-08-30 18:46:21 +0000778template <class _R, class _F>
779class __deferred_assoc_state
780 : public __assoc_state<_R>
781{
782 typedef __assoc_state<_R> base;
783
784 _F __func_;
785
786public:
Howard Hinnant73d21a42010-09-04 23:28:19 +0000787#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21 +0000788 explicit __deferred_assoc_state(_F&& __f);
789#endif
790
791 virtual void __execute();
792};
793
Howard Hinnant73d21a42010-09-04 23:28:19 +0000794#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21 +0000795
796template <class _R, class _F>
797inline _LIBCPP_INLINE_VISIBILITY
798__deferred_assoc_state<_R, _F>::__deferred_assoc_state(_F&& __f)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000799 : __func_(_VSTD::forward<_F>(__f))
Howard Hinnant54da3382010-08-30 18:46:21 +0000800{
801 this->__set_deferred();
802}
803
Howard Hinnant73d21a42010-09-04 23:28:19 +0000804#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21 +0000805
806template <class _R, class _F>
807void
808__deferred_assoc_state<_R, _F>::__execute()
809{
810#ifndef _LIBCPP_NO_EXCEPTIONS
811 try
812 {
813#endif // _LIBCPP_NO_EXCEPTIONS
814 this->set_value(__func_());
815#ifndef _LIBCPP_NO_EXCEPTIONS
816 }
817 catch (...)
818 {
819 this->set_exception(current_exception());
820 }
821#endif // _LIBCPP_NO_EXCEPTIONS
822}
823
824template <class _F>
825class __deferred_assoc_state<void, _F>
826 : public __assoc_sub_state
827{
828 typedef __assoc_sub_state base;
829
830 _F __func_;
831
832public:
Howard Hinnant73d21a42010-09-04 23:28:19 +0000833#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21 +0000834 explicit __deferred_assoc_state(_F&& __f);
835#endif
836
837 virtual void __execute();
838};
839
Howard Hinnant73d21a42010-09-04 23:28:19 +0000840#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21 +0000841
842template <class _F>
843inline _LIBCPP_INLINE_VISIBILITY
844__deferred_assoc_state<void, _F>::__deferred_assoc_state(_F&& __f)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000845 : __func_(_VSTD::forward<_F>(__f))
Howard Hinnant54da3382010-08-30 18:46:21 +0000846{
847 this->__set_deferred();
848}
849
Howard Hinnant73d21a42010-09-04 23:28:19 +0000850#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21 +0000851
852template <class _F>
853void
854__deferred_assoc_state<void, _F>::__execute()
855{
856#ifndef _LIBCPP_NO_EXCEPTIONS
857 try
858 {
859#endif // _LIBCPP_NO_EXCEPTIONS
860 __func_();
861 this->set_value();
862#ifndef _LIBCPP_NO_EXCEPTIONS
863 }
864 catch (...)
865 {
866 this->set_exception(current_exception());
867 }
868#endif // _LIBCPP_NO_EXCEPTIONS
869}
870
Howard Hinnant57cff292011-05-19 15:05:04 +0000871template <class _R, class _F>
872class __async_assoc_state
873 : public __assoc_state<_R>
874{
875 typedef __assoc_state<_R> base;
876
877 _F __func_;
878
Howard Hinnant1694d232011-05-28 14:41:13 +0000879 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant57cff292011-05-19 15:05:04 +0000880public:
881#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
882 explicit __async_assoc_state(_F&& __f);
883#endif
884
885 virtual void __execute();
886};
887
888#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
889
890template <class _R, class _F>
891inline _LIBCPP_INLINE_VISIBILITY
892__async_assoc_state<_R, _F>::__async_assoc_state(_F&& __f)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000893 : __func_(_VSTD::forward<_F>(__f))
Howard Hinnant57cff292011-05-19 15:05:04 +0000894{
895}
896
897#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
898
899template <class _R, class _F>
900void
901__async_assoc_state<_R, _F>::__execute()
902{
903#ifndef _LIBCPP_NO_EXCEPTIONS
904 try
905 {
906#endif // _LIBCPP_NO_EXCEPTIONS
907 this->set_value(__func_());
908#ifndef _LIBCPP_NO_EXCEPTIONS
909 }
910 catch (...)
911 {
912 this->set_exception(current_exception());
913 }
914#endif // _LIBCPP_NO_EXCEPTIONS
915}
916
917template <class _R, class _F>
918void
Howard Hinnant1694d232011-05-28 14:41:13 +0000919__async_assoc_state<_R, _F>::__on_zero_shared() _NOEXCEPT
Howard Hinnant57cff292011-05-19 15:05:04 +0000920{
921 this->wait();
922 base::__on_zero_shared();
923}
924
925template <class _F>
926class __async_assoc_state<void, _F>
927 : public __assoc_sub_state
928{
929 typedef __assoc_sub_state base;
930
931 _F __func_;
932
Howard Hinnant1694d232011-05-28 14:41:13 +0000933 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant57cff292011-05-19 15:05:04 +0000934public:
935#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
936 explicit __async_assoc_state(_F&& __f);
937#endif
938
939 virtual void __execute();
940};
941
942#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
943
944template <class _F>
945inline _LIBCPP_INLINE_VISIBILITY
946__async_assoc_state<void, _F>::__async_assoc_state(_F&& __f)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000947 : __func_(_VSTD::forward<_F>(__f))
Howard Hinnant57cff292011-05-19 15:05:04 +0000948{
949}
950
951#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
952
953template <class _F>
954void
955__async_assoc_state<void, _F>::__execute()
956{
957#ifndef _LIBCPP_NO_EXCEPTIONS
958 try
959 {
960#endif // _LIBCPP_NO_EXCEPTIONS
961 __func_();
962 this->set_value();
963#ifndef _LIBCPP_NO_EXCEPTIONS
964 }
965 catch (...)
966 {
967 this->set_exception(current_exception());
968 }
969#endif // _LIBCPP_NO_EXCEPTIONS
970}
971
972template <class _F>
973void
Howard Hinnant1694d232011-05-28 14:41:13 +0000974__async_assoc_state<void, _F>::__on_zero_shared() _NOEXCEPT
Howard Hinnant57cff292011-05-19 15:05:04 +0000975{
976 this->wait();
977 base::__on_zero_shared();
978}
979
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000980template <class _R> class promise;
981template <class _R> class shared_future;
Howard Hinnant47499b12010-08-27 20:10:19 +0000982
983// future
984
Howard Hinnant54da3382010-08-30 18:46:21 +0000985template <class _R> class future;
986
987template <class _R, class _F>
988future<_R>
Howard Hinnant73d21a42010-09-04 23:28:19 +0000989#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21 +0000990__make_deferred_assoc_state(_F&& __f);
991#else
992__make_deferred_assoc_state(_F __f);
993#endif
994
Howard Hinnant57cff292011-05-19 15:05:04 +0000995template <class _R, class _F>
996future<_R>
997#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
998__make_async_assoc_state(_F&& __f);
999#else
1000__make_async_assoc_state(_F __f);
1001#endif
1002
Howard Hinnant47499b12010-08-27 20:10:19 +00001003template <class _R>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001004class _LIBCPP_VISIBLE future
Howard Hinnant47499b12010-08-27 20:10:19 +00001005{
1006 __assoc_state<_R>* __state_;
1007
1008 explicit future(__assoc_state<_R>* __state);
1009
1010 template <class> friend class promise;
Howard Hinnant99be8232010-09-03 18:39:25 +00001011 template <class> friend class shared_future;
Howard Hinnant54da3382010-08-30 18:46:21 +00001012
Howard Hinnant73d21a42010-09-04 23:28:19 +00001013#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57cff292011-05-19 15:05:04 +00001014 template <class _R1, class _F>
Howard Hinnant54da3382010-08-30 18:46:21 +00001015 friend future<_R1> __make_deferred_assoc_state(_F&& __f);
Howard Hinnant57cff292011-05-19 15:05:04 +00001016 template <class _R1, class _F>
1017 friend future<_R1> __make_async_assoc_state(_F&& __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001018#else
Howard Hinnant57cff292011-05-19 15:05:04 +00001019 template <class _R1, class _F>
Howard Hinnant54da3382010-08-30 18:46:21 +00001020 friend future<_R1> __make_deferred_assoc_state(_F __f);
Howard Hinnant57cff292011-05-19 15:05:04 +00001021 template <class _R1, class _F>
1022 friend future<_R1> __make_async_assoc_state(_F __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001023#endif
1024
Howard Hinnant47499b12010-08-27 20:10:19 +00001025public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001026 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001027 future() : __state_(nullptr) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001028#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001029 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001030 future(future&& __rhs)
1031 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1032 future(const future&) = delete;
1033 future& operator=(const future&) = delete;
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001034 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001035 future& operator=(future&& __rhs)
1036 {
1037 future(std::move(__rhs)).swap(*this);
1038 return *this;
1039 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001040#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001041private:
1042 future(const future&);
1043 future& operator=(const future&);
1044public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001045#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001046 ~future();
Howard Hinnant7de47902010-11-30 20:23:32 +00001047 shared_future<_R> share();
Howard Hinnant47499b12010-08-27 20:10:19 +00001048
1049 // retrieving the value
1050 _R get();
1051
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001052 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0949eed2011-06-30 21:18:19 +00001053 void swap(future& __rhs) {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:19 +00001054
1055 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001056 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001057 bool valid() const {return __state_ != nullptr;}
1058
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001059 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001060 void wait() const {__state_->wait();}
1061 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001062 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001063 future_status
1064 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1065 {return __state_->wait_for(__rel_time);}
1066 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001067 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001068 future_status
1069 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1070 {return __state_->wait_until(__abs_time);}
1071};
1072
1073template <class _R>
1074future<_R>::future(__assoc_state<_R>* __state)
1075 : __state_(__state)
1076{
Howard Hinnant22ba71b2011-07-13 16:00:50 +00001077#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:19 +00001078 if (__state_->__has_future_attached())
1079 throw future_error(make_error_code(future_errc::future_already_retrieved));
Howard Hinnant22ba71b2011-07-13 16:00:50 +00001080#endif
Howard Hinnant47499b12010-08-27 20:10:19 +00001081 __state_->__add_shared();
Howard Hinnant54da3382010-08-30 18:46:21 +00001082 __state_->__set_future_attached();
Howard Hinnant47499b12010-08-27 20:10:19 +00001083}
1084
Howard Hinnant54da3382010-08-30 18:46:21 +00001085struct __release_shared_count
1086{
1087 void operator()(__shared_count* p) {p->__release_shared();}
1088};
1089
Howard Hinnant47499b12010-08-27 20:10:19 +00001090template <class _R>
1091future<_R>::~future()
1092{
1093 if (__state_)
1094 __state_->__release_shared();
1095}
1096
1097template <class _R>
1098_R
1099future<_R>::get()
1100{
Howard Hinnant54da3382010-08-30 18:46:21 +00001101 unique_ptr<__shared_count, __release_shared_count> __(__state_);
Howard Hinnant47499b12010-08-27 20:10:19 +00001102 __assoc_state<_R>* __s = __state_;
1103 __state_ = nullptr;
1104 return __s->move();
1105}
1106
1107template <class _R>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001108class _LIBCPP_VISIBLE future<_R&>
Howard Hinnant47499b12010-08-27 20:10:19 +00001109{
1110 __assoc_state<_R&>* __state_;
1111
1112 explicit future(__assoc_state<_R&>* __state);
1113
1114 template <class> friend class promise;
Howard Hinnant99be8232010-09-03 18:39:25 +00001115 template <class> friend class shared_future;
Howard Hinnant54da3382010-08-30 18:46:21 +00001116
Howard Hinnant73d21a42010-09-04 23:28:19 +00001117#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57cff292011-05-19 15:05:04 +00001118 template <class _R1, class _F>
Howard Hinnant54da3382010-08-30 18:46:21 +00001119 friend future<_R1> __make_deferred_assoc_state(_F&& __f);
Howard Hinnant57cff292011-05-19 15:05:04 +00001120 template <class _R1, class _F>
1121 friend future<_R1> __make_async_assoc_state(_F&& __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001122#else
Howard Hinnant57cff292011-05-19 15:05:04 +00001123 template <class _R1, class _F>
Howard Hinnant54da3382010-08-30 18:46:21 +00001124 friend future<_R1> __make_deferred_assoc_state(_F __f);
Howard Hinnant57cff292011-05-19 15:05:04 +00001125 template <class _R1, class _F>
1126 friend future<_R1> __make_async_assoc_state(_F __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001127#endif
1128
Howard Hinnant47499b12010-08-27 20:10:19 +00001129public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001130 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001131 future() : __state_(nullptr) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001132#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001133 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001134 future(future&& __rhs)
1135 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1136 future(const future&) = delete;
1137 future& operator=(const future&) = delete;
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001138 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001139 future& operator=(future&& __rhs)
1140 {
1141 future(std::move(__rhs)).swap(*this);
1142 return *this;
1143 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001144#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001145private:
1146 future(const future&);
1147 future& operator=(const future&);
1148public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001149#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001150 ~future();
Howard Hinnant7de47902010-11-30 20:23:32 +00001151 shared_future<_R&> share();
Howard Hinnant47499b12010-08-27 20:10:19 +00001152
1153 // retrieving the value
1154 _R& get();
1155
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001156 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0949eed2011-06-30 21:18:19 +00001157 void swap(future& __rhs) {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:19 +00001158
1159 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001160 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001161 bool valid() const {return __state_ != nullptr;}
1162
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001163 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001164 void wait() const {__state_->wait();}
1165 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001166 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001167 future_status
1168 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1169 {return __state_->wait_for(__rel_time);}
1170 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001171 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001172 future_status
1173 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1174 {return __state_->wait_until(__abs_time);}
1175};
1176
1177template <class _R>
1178future<_R&>::future(__assoc_state<_R&>* __state)
1179 : __state_(__state)
1180{
Howard Hinnant22ba71b2011-07-13 16:00:50 +00001181#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:19 +00001182 if (__state_->__has_future_attached())
1183 throw future_error(make_error_code(future_errc::future_already_retrieved));
Howard Hinnant22ba71b2011-07-13 16:00:50 +00001184#endif
Howard Hinnant47499b12010-08-27 20:10:19 +00001185 __state_->__add_shared();
Howard Hinnant54da3382010-08-30 18:46:21 +00001186 __state_->__set_future_attached();
Howard Hinnant47499b12010-08-27 20:10:19 +00001187}
1188
1189template <class _R>
1190future<_R&>::~future()
1191{
1192 if (__state_)
1193 __state_->__release_shared();
1194}
1195
1196template <class _R>
1197_R&
1198future<_R&>::get()
1199{
Howard Hinnant54da3382010-08-30 18:46:21 +00001200 unique_ptr<__shared_count, __release_shared_count> __(__state_);
Howard Hinnantf39daa82010-08-28 21:01:06 +00001201 __assoc_state<_R&>* __s = __state_;
Howard Hinnant47499b12010-08-27 20:10:19 +00001202 __state_ = nullptr;
1203 return __s->copy();
1204}
1205
1206template <>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001207class _LIBCPP_VISIBLE future<void>
Howard Hinnant47499b12010-08-27 20:10:19 +00001208{
1209 __assoc_sub_state* __state_;
1210
1211 explicit future(__assoc_sub_state* __state);
1212
1213 template <class> friend class promise;
Howard Hinnant99be8232010-09-03 18:39:25 +00001214 template <class> friend class shared_future;
Howard Hinnant54da3382010-08-30 18:46:21 +00001215
Howard Hinnant73d21a42010-09-04 23:28:19 +00001216#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57cff292011-05-19 15:05:04 +00001217 template <class _R1, class _F>
Howard Hinnant54da3382010-08-30 18:46:21 +00001218 friend future<_R1> __make_deferred_assoc_state(_F&& __f);
Howard Hinnant57cff292011-05-19 15:05:04 +00001219 template <class _R1, class _F>
1220 friend future<_R1> __make_async_assoc_state(_F&& __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001221#else
Howard Hinnant57cff292011-05-19 15:05:04 +00001222 template <class _R1, class _F>
Howard Hinnant54da3382010-08-30 18:46:21 +00001223 friend future<_R1> __make_deferred_assoc_state(_F __f);
Howard Hinnant57cff292011-05-19 15:05:04 +00001224 template <class _R1, class _F>
1225 friend future<_R1> __make_async_assoc_state(_F __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001226#endif
1227
Howard Hinnant47499b12010-08-27 20:10:19 +00001228public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001229 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001230 future() : __state_(nullptr) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001231#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001232 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001233 future(future&& __rhs)
1234 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1235 future(const future&) = delete;
1236 future& operator=(const future&) = delete;
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001237 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001238 future& operator=(future&& __rhs)
1239 {
1240 future(std::move(__rhs)).swap(*this);
1241 return *this;
1242 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001243#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001244private:
1245 future(const future&);
1246 future& operator=(const future&);
1247public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001248#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001249 ~future();
Howard Hinnant7de47902010-11-30 20:23:32 +00001250 shared_future<void> share();
Howard Hinnant47499b12010-08-27 20:10:19 +00001251
1252 // retrieving the value
1253 void get();
1254
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001255 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0949eed2011-06-30 21:18:19 +00001256 void swap(future& __rhs) {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:19 +00001257
1258 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001259 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001260 bool valid() const {return __state_ != nullptr;}
1261
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001262 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001263 void wait() const {__state_->wait();}
1264 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001265 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001266 future_status
1267 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1268 {return __state_->wait_for(__rel_time);}
1269 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001270 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001271 future_status
1272 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1273 {return __state_->wait_until(__abs_time);}
1274};
1275
Howard Hinnant99be8232010-09-03 18:39:25 +00001276template <class _R>
1277inline _LIBCPP_INLINE_VISIBILITY
1278void
1279swap(future<_R>& __x, future<_R>& __y)
1280{
1281 __x.swap(__y);
1282}
1283
Howard Hinnant47499b12010-08-27 20:10:19 +00001284// promise<R>
1285
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00001286template <class _Callable> class packaged_task;
Howard Hinnant54da3382010-08-30 18:46:21 +00001287
Howard Hinnant47499b12010-08-27 20:10:19 +00001288template <class _R>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001289class _LIBCPP_VISIBLE promise
Howard Hinnant47499b12010-08-27 20:10:19 +00001290{
1291 __assoc_state<_R>* __state_;
Howard Hinnant54da3382010-08-30 18:46:21 +00001292
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001293 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001294 explicit promise(nullptr_t) : __state_(nullptr) {}
1295
1296 template <class> friend class packaged_task;
Howard Hinnant47499b12010-08-27 20:10:19 +00001297public:
1298 promise();
1299 template <class _Alloc>
1300 promise(allocator_arg_t, const _Alloc& __a);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001301#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001302 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001303 promise(promise&& __rhs)
1304 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1305 promise(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +00001306#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001307private:
1308 promise(const promise& __rhs);
1309public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001310#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001311 ~promise();
1312
1313 // assignment
Howard Hinnant73d21a42010-09-04 23:28:19 +00001314#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001315 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001316 promise& operator=(promise&& __rhs)
1317 {
1318 promise(std::move(__rhs)).swap(*this);
1319 return *this;
1320 }
1321 promise& operator=(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +00001322#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001323private:
1324 promise& operator=(const promise& __rhs);
1325public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001326#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001327 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0949eed2011-06-30 21:18:19 +00001328 void swap(promise& __rhs) {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:19 +00001329
1330 // retrieving the result
1331 future<_R> get_future();
1332
1333 // setting the result
1334 void set_value(const _R& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001335#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001336 void set_value(_R&& __r);
1337#endif
1338 void set_exception(exception_ptr __p);
1339
1340 // setting the result with deferred notification
1341 void set_value_at_thread_exit(const _R& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001342#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001343 void set_value_at_thread_exit(_R&& __r);
1344#endif
1345 void set_exception_at_thread_exit(exception_ptr __p);
1346};
1347
1348template <class _R>
1349promise<_R>::promise()
1350 : __state_(new __assoc_state<_R>)
1351{
1352}
1353
1354template <class _R>
1355template <class _Alloc>
1356promise<_R>::promise(allocator_arg_t, const _Alloc& __a0)
1357{
1358 typedef typename _Alloc::template rebind<__assoc_state_alloc<_R, _Alloc> >::other _A2;
1359 typedef __allocator_destructor<_A2> _D2;
1360 _A2 __a(__a0);
1361 unique_ptr<__assoc_state_alloc<_R, _Alloc>, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1362 ::new(__hold.get()) __assoc_state_alloc<_R, _Alloc>(__a0);
1363 __state_ = __hold.release();
1364}
1365
1366template <class _R>
1367promise<_R>::~promise()
1368{
1369 if (__state_)
1370 {
1371 if (!__state_->__has_value() && __state_->use_count() > 1)
1372 __state_->set_exception(make_exception_ptr(
1373 future_error(make_error_code(future_errc::broken_promise))
1374 ));
1375 __state_->__release_shared();
1376 }
1377}
1378
1379template <class _R>
1380future<_R>
1381promise<_R>::get_future()
1382{
Howard Hinnant22ba71b2011-07-13 16:00:50 +00001383#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:19 +00001384 if (__state_ == nullptr)
1385 throw future_error(make_error_code(future_errc::no_state));
Howard Hinnant22ba71b2011-07-13 16:00:50 +00001386#endif
Howard Hinnant47499b12010-08-27 20:10:19 +00001387 return future<_R>(__state_);
1388}
1389
1390template <class _R>
1391void
1392promise<_R>::set_value(const _R& __r)
1393{
Howard Hinnant22ba71b2011-07-13 16:00:50 +00001394#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:19 +00001395 if (__state_ == nullptr)
1396 throw future_error(make_error_code(future_errc::no_state));
Howard Hinnant22ba71b2011-07-13 16:00:50 +00001397#endif
Howard Hinnant47499b12010-08-27 20:10:19 +00001398 __state_->set_value(__r);
1399}
1400
Howard Hinnant73d21a42010-09-04 23:28:19 +00001401#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001402
1403template <class _R>
1404void
1405promise<_R>::set_value(_R&& __r)
1406{
Howard Hinnant22ba71b2011-07-13 16:00:50 +00001407#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:19 +00001408 if (__state_ == nullptr)
1409 throw future_error(make_error_code(future_errc::no_state));
Howard Hinnant22ba71b2011-07-13 16:00:50 +00001410#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001411 __state_->set_value(_VSTD::move(__r));
Howard Hinnant47499b12010-08-27 20:10:19 +00001412}
1413
Howard Hinnant73d21a42010-09-04 23:28:19 +00001414#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001415
1416template <class _R>
1417void
1418promise<_R>::set_exception(exception_ptr __p)
1419{
Howard Hinnant22ba71b2011-07-13 16:00:50 +00001420#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:19 +00001421 if (__state_ == nullptr)
1422 throw future_error(make_error_code(future_errc::no_state));
Howard Hinnant22ba71b2011-07-13 16:00:50 +00001423#endif
Howard Hinnant47499b12010-08-27 20:10:19 +00001424 __state_->set_exception(__p);
1425}
1426
1427template <class _R>
1428void
1429promise<_R>::set_value_at_thread_exit(const _R& __r)
1430{
Howard Hinnant22ba71b2011-07-13 16:00:50 +00001431#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:19 +00001432 if (__state_ == nullptr)
1433 throw future_error(make_error_code(future_errc::no_state));
Howard Hinnant22ba71b2011-07-13 16:00:50 +00001434#endif
Howard Hinnant47499b12010-08-27 20:10:19 +00001435 __state_->set_value_at_thread_exit(__r);
1436}
1437
Howard Hinnant73d21a42010-09-04 23:28:19 +00001438#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001439
1440template <class _R>
1441void
1442promise<_R>::set_value_at_thread_exit(_R&& __r)
1443{
Howard Hinnant22ba71b2011-07-13 16:00:50 +00001444#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:19 +00001445 if (__state_ == nullptr)
1446 throw future_error(make_error_code(future_errc::no_state));
Howard Hinnant22ba71b2011-07-13 16:00:50 +00001447#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001448 __state_->set_value_at_thread_exit(_VSTD::move(__r));
Howard Hinnant47499b12010-08-27 20:10:19 +00001449}
1450
Howard Hinnant73d21a42010-09-04 23:28:19 +00001451#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001452
1453template <class _R>
1454void
1455promise<_R>::set_exception_at_thread_exit(exception_ptr __p)
1456{
Howard Hinnant22ba71b2011-07-13 16:00:50 +00001457#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:19 +00001458 if (__state_ == nullptr)
1459 throw future_error(make_error_code(future_errc::no_state));
Howard Hinnant22ba71b2011-07-13 16:00:50 +00001460#endif
Howard Hinnant47499b12010-08-27 20:10:19 +00001461 __state_->set_exception_at_thread_exit(__p);
1462}
1463
1464// promise<R&>
1465
1466template <class _R>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001467class _LIBCPP_VISIBLE promise<_R&>
Howard Hinnant47499b12010-08-27 20:10:19 +00001468{
1469 __assoc_state<_R&>* __state_;
Howard Hinnant54da3382010-08-30 18:46:21 +00001470
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001471 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001472 explicit promise(nullptr_t) : __state_(nullptr) {}
1473
1474 template <class> friend class packaged_task;
1475
Howard Hinnant47499b12010-08-27 20:10:19 +00001476public:
1477 promise();
1478 template <class _Allocator>
1479 promise(allocator_arg_t, const _Allocator& __a);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001480#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001481 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001482 promise(promise&& __rhs)
1483 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1484 promise(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +00001485#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001486private:
1487 promise(const promise& __rhs);
1488public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001489#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001490 ~promise();
1491
1492 // assignment
Howard Hinnant73d21a42010-09-04 23:28:19 +00001493#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001494 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001495 promise& operator=(promise&& __rhs)
1496 {
1497 promise(std::move(__rhs)).swap(*this);
1498 return *this;
1499 }
1500 promise& operator=(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +00001501#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001502private:
1503 promise& operator=(const promise& __rhs);
1504public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001505#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001506 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0949eed2011-06-30 21:18:19 +00001507 void swap(promise& __rhs) {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:19 +00001508
1509 // retrieving the result
1510 future<_R&> get_future();
1511
1512 // setting the result
1513 void set_value(_R& __r);
1514 void set_exception(exception_ptr __p);
1515
1516 // setting the result with deferred notification
1517 void set_value_at_thread_exit(_R&);
1518 void set_exception_at_thread_exit(exception_ptr __p);
1519};
1520
1521template <class _R>
1522promise<_R&>::promise()
1523 : __state_(new __assoc_state<_R&>)
1524{
1525}
1526
1527template <class _R>
1528template <class _Alloc>
1529promise<_R&>::promise(allocator_arg_t, const _Alloc& __a0)
1530{
1531 typedef typename _Alloc::template rebind<__assoc_state_alloc<_R&, _Alloc> >::other _A2;
1532 typedef __allocator_destructor<_A2> _D2;
1533 _A2 __a(__a0);
1534 unique_ptr<__assoc_state_alloc<_R&, _Alloc>, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1535 ::new(__hold.get()) __assoc_state_alloc<_R&, _Alloc>(__a0);
1536 __state_ = __hold.release();
1537}
1538
1539template <class _R>
1540promise<_R&>::~promise()
1541{
1542 if (__state_)
1543 {
1544 if (!__state_->__has_value() && __state_->use_count() > 1)
1545 __state_->set_exception(make_exception_ptr(
1546 future_error(make_error_code(future_errc::broken_promise))
1547 ));
1548 __state_->__release_shared();
1549 }
1550}
1551
1552template <class _R>
1553future<_R&>
1554promise<_R&>::get_future()
1555{
Howard Hinnant22ba71b2011-07-13 16:00:50 +00001556#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:19 +00001557 if (__state_ == nullptr)
1558 throw future_error(make_error_code(future_errc::no_state));
Howard Hinnant22ba71b2011-07-13 16:00:50 +00001559#endif
Howard Hinnant47499b12010-08-27 20:10:19 +00001560 return future<_R&>(__state_);
1561}
1562
1563template <class _R>
1564void
1565promise<_R&>::set_value(_R& __r)
1566{
Howard Hinnant22ba71b2011-07-13 16:00:50 +00001567#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:19 +00001568 if (__state_ == nullptr)
1569 throw future_error(make_error_code(future_errc::no_state));
Howard Hinnant22ba71b2011-07-13 16:00:50 +00001570#endif
Howard Hinnant47499b12010-08-27 20:10:19 +00001571 __state_->set_value(__r);
1572}
1573
1574template <class _R>
1575void
1576promise<_R&>::set_exception(exception_ptr __p)
1577{
Howard Hinnant22ba71b2011-07-13 16:00:50 +00001578#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:19 +00001579 if (__state_ == nullptr)
1580 throw future_error(make_error_code(future_errc::no_state));
Howard Hinnant22ba71b2011-07-13 16:00:50 +00001581#endif
Howard Hinnant47499b12010-08-27 20:10:19 +00001582 __state_->set_exception(__p);
1583}
1584
1585template <class _R>
1586void
1587promise<_R&>::set_value_at_thread_exit(_R& __r)
1588{
Howard Hinnant22ba71b2011-07-13 16:00:50 +00001589#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:19 +00001590 if (__state_ == nullptr)
1591 throw future_error(make_error_code(future_errc::no_state));
Howard Hinnant22ba71b2011-07-13 16:00:50 +00001592#endif
Howard Hinnant47499b12010-08-27 20:10:19 +00001593 __state_->set_value_at_thread_exit(__r);
1594}
1595
1596template <class _R>
1597void
1598promise<_R&>::set_exception_at_thread_exit(exception_ptr __p)
1599{
Howard Hinnant22ba71b2011-07-13 16:00:50 +00001600#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant47499b12010-08-27 20:10:19 +00001601 if (__state_ == nullptr)
1602 throw future_error(make_error_code(future_errc::no_state));
Howard Hinnant22ba71b2011-07-13 16:00:50 +00001603#endif
Howard Hinnant47499b12010-08-27 20:10:19 +00001604 __state_->set_exception_at_thread_exit(__p);
1605}
1606
1607// promise<void>
1608
1609template <>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001610class _LIBCPP_VISIBLE promise<void>
Howard Hinnant47499b12010-08-27 20:10:19 +00001611{
1612 __assoc_sub_state* __state_;
Howard Hinnant54da3382010-08-30 18:46:21 +00001613
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001614 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001615 explicit promise(nullptr_t) : __state_(nullptr) {}
1616
1617 template <class> friend class packaged_task;
1618
Howard Hinnant47499b12010-08-27 20:10:19 +00001619public:
1620 promise();
1621 template <class _Allocator>
1622 promise(allocator_arg_t, const _Allocator& __a);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001623#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001624 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001625 promise(promise&& __rhs)
1626 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1627 promise(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +00001628#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001629private:
1630 promise(const promise& __rhs);
1631public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001632#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001633 ~promise();
1634
1635 // assignment
Howard Hinnant73d21a42010-09-04 23:28:19 +00001636#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001637 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001638 promise& operator=(promise&& __rhs)
1639 {
1640 promise(std::move(__rhs)).swap(*this);
1641 return *this;
1642 }
1643 promise& operator=(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +00001644#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001645private:
1646 promise& operator=(const promise& __rhs);
1647public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001648#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001649 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0949eed2011-06-30 21:18:19 +00001650 void swap(promise& __rhs) {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:19 +00001651
1652 // retrieving the result
1653 future<void> get_future();
1654
1655 // setting the result
1656 void set_value();
1657 void set_exception(exception_ptr __p);
1658
1659 // setting the result with deferred notification
1660 void set_value_at_thread_exit();
1661 void set_exception_at_thread_exit(exception_ptr __p);
1662};
1663
1664template <class _Alloc>
1665promise<void>::promise(allocator_arg_t, const _Alloc& __a0)
1666{
1667 typedef typename _Alloc::template rebind<__assoc_sub_state_alloc<_Alloc> >::other _A2;
1668 typedef __allocator_destructor<_A2> _D2;
1669 _A2 __a(__a0);
1670 unique_ptr<__assoc_sub_state_alloc<_Alloc>, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1671 ::new(__hold.get()) __assoc_sub_state_alloc<_Alloc>(__a0);
1672 __state_ = __hold.release();
1673}
1674
1675template <class _R>
1676inline _LIBCPP_INLINE_VISIBILITY
1677void
1678swap(promise<_R>& __x, promise<_R>& __y)
1679{
1680 __x.swap(__y);
1681}
1682
1683template <class _R, class _Alloc>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001684 struct _LIBCPP_VISIBLE uses_allocator<promise<_R>, _Alloc>
1685 : public true_type {};
Howard Hinnant47499b12010-08-27 20:10:19 +00001686
Howard Hinnant54da3382010-08-30 18:46:21 +00001687#ifndef _LIBCPP_HAS_NO_VARIADICS
1688
1689// packaged_task
1690
1691template<class _Fp> class __packaged_task_base;
1692
1693template<class _R, class ..._ArgTypes>
1694class __packaged_task_base<_R(_ArgTypes...)>
1695{
1696 __packaged_task_base(const __packaged_task_base&);
1697 __packaged_task_base& operator=(const __packaged_task_base&);
1698public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001699 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001700 __packaged_task_base() {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001701 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001702 virtual ~__packaged_task_base() {}
1703 virtual void __move_to(__packaged_task_base*) = 0;
1704 virtual void destroy() = 0;
1705 virtual void destroy_deallocate() = 0;
1706 virtual _R operator()(_ArgTypes&& ...) = 0;
1707};
1708
1709template<class _FD, class _Alloc, class _FB> class __packaged_task_func;
1710
1711template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1712class __packaged_task_func<_F, _Alloc, _R(_ArgTypes...)>
1713 : public __packaged_task_base<_R(_ArgTypes...)>
1714{
1715 __compressed_pair<_F, _Alloc> __f_;
1716public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001717 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001718 explicit __packaged_task_func(const _F& __f) : __f_(__f) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001719 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0949eed2011-06-30 21:18:19 +00001720 explicit __packaged_task_func(_F&& __f) : __f_(_VSTD::move(__f)) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001721 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001722 __packaged_task_func(const _F& __f, const _Alloc& __a)
1723 : __f_(__f, __a) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001724 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001725 __packaged_task_func(_F&& __f, const _Alloc& __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001726 : __f_(_VSTD::move(__f), __a) {}
Howard Hinnant54da3382010-08-30 18:46:21 +00001727 virtual void __move_to(__packaged_task_base<_R(_ArgTypes...)>*);
1728 virtual void destroy();
1729 virtual void destroy_deallocate();
1730 virtual _R operator()(_ArgTypes&& ... __args);
1731};
1732
1733template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1734void
1735__packaged_task_func<_F, _Alloc, _R(_ArgTypes...)>::__move_to(
1736 __packaged_task_base<_R(_ArgTypes...)>* __p)
1737{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001738 ::new (__p) __packaged_task_func(_VSTD::move(__f_.first()), _VSTD::move(__f_.second()));
Howard Hinnant54da3382010-08-30 18:46:21 +00001739}
1740
1741template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1742void
1743__packaged_task_func<_F, _Alloc, _R(_ArgTypes...)>::destroy()
1744{
1745 __f_.~__compressed_pair<_F, _Alloc>();
1746}
1747
1748template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1749void
1750__packaged_task_func<_F, _Alloc, _R(_ArgTypes...)>::destroy_deallocate()
1751{
1752 typedef typename _Alloc::template rebind<__packaged_task_func>::other _A;
1753 _A __a(__f_.second());
1754 __f_.~__compressed_pair<_F, _Alloc>();
1755 __a.deallocate(this, 1);
1756}
1757
1758template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1759_R
1760__packaged_task_func<_F, _Alloc, _R(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
1761{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001762 return __invoke(__f_.first(), _VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnant54da3382010-08-30 18:46:21 +00001763}
1764
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00001765template <class _Callable> class __packaged_task_function;
Howard Hinnant54da3382010-08-30 18:46:21 +00001766
1767template<class _R, class ..._ArgTypes>
1768class __packaged_task_function<_R(_ArgTypes...)>
1769{
1770 typedef __packaged_task_base<_R(_ArgTypes...)> __base;
1771 aligned_storage<3*sizeof(void*)>::type __buf_;
1772 __base* __f_;
1773
1774public:
1775 typedef _R result_type;
1776
1777 // construct/copy/destroy:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001778 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001779 __packaged_task_function() : __f_(nullptr) {}
1780 template<class _F>
1781 __packaged_task_function(_F&& __f);
1782 template<class _F, class _Alloc>
1783 __packaged_task_function(allocator_arg_t, const _Alloc& __a, _F&& __f);
1784
1785 __packaged_task_function(__packaged_task_function&&);
1786 __packaged_task_function& operator=(__packaged_task_function&&);
1787
1788 __packaged_task_function(const __packaged_task_function&) = delete;
1789 __packaged_task_function& operator=(const __packaged_task_function&) = delete;
1790
1791 ~__packaged_task_function();
1792
1793 void swap(__packaged_task_function&);
1794
1795 _R operator()(_ArgTypes...) const;
1796};
1797
1798template<class _R, class ..._ArgTypes>
1799__packaged_task_function<_R(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f)
1800{
1801 if (__f.__f_ == nullptr)
1802 __f_ = nullptr;
1803 else if (__f.__f_ == (__base*)&__f.__buf_)
1804 {
1805 __f_ = (__base*)&__buf_;
1806 __f.__f_->__move_to(__f_);
1807 }
1808 else
1809 {
1810 __f_ = __f.__f_;
1811 __f.__f_ = nullptr;
1812 }
1813}
1814
1815template<class _R, class ..._ArgTypes>
1816template <class _F>
1817__packaged_task_function<_R(_ArgTypes...)>::__packaged_task_function(_F&& __f)
1818 : __f_(nullptr)
1819{
1820 typedef typename remove_reference<_F>::type _FR;
1821 typedef __packaged_task_func<_FR, allocator<_FR>, _R(_ArgTypes...)> _FF;
1822 if (sizeof(_FF) <= sizeof(__buf_))
1823 {
1824 __f_ = (__base*)&__buf_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001825 ::new (__f_) _FF(_VSTD::forward<_F>(__f));
Howard Hinnant54da3382010-08-30 18:46:21 +00001826 }
1827 else
1828 {
1829 typedef allocator<_FF> _A;
1830 _A __a;
1831 typedef __allocator_destructor<_A> _D;
1832 unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001833 ::new (__hold.get()) _FF(_VSTD::forward<_F>(__f), allocator<_FR>(__a));
Howard Hinnant54da3382010-08-30 18:46:21 +00001834 __f_ = __hold.release();
1835 }
1836}
1837
1838template<class _R, class ..._ArgTypes>
1839template <class _F, class _Alloc>
1840__packaged_task_function<_R(_ArgTypes...)>::__packaged_task_function(
1841 allocator_arg_t, const _Alloc& __a0, _F&& __f)
1842 : __f_(nullptr)
1843{
1844 typedef allocator_traits<_Alloc> __alloc_traits;
1845 typedef typename remove_reference<_F>::type _FR;
1846 typedef __packaged_task_func<_FR, _Alloc, _R(_ArgTypes...)> _FF;
1847 if (sizeof(_FF) <= sizeof(__buf_))
1848 {
1849 __f_ = (__base*)&__buf_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001850 ::new (__f_) _FF(_VSTD::forward<_F>(__f));
Howard Hinnant54da3382010-08-30 18:46:21 +00001851 }
1852 else
1853 {
1854 typedef typename __alloc_traits::template
1855#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1856 rebind_alloc<_FF>
1857#else
1858 rebind_alloc<_FF>::other
1859#endif
1860 _A;
1861 _A __a(__a0);
1862 typedef __allocator_destructor<_A> _D;
1863 unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001864 ::new (__hold.get()) _FF(_VSTD::forward<_F>(__f), _Alloc(__a));
Howard Hinnant54da3382010-08-30 18:46:21 +00001865 __f_ = __hold.release();
1866 }
1867}
1868
1869template<class _R, class ..._ArgTypes>
1870__packaged_task_function<_R(_ArgTypes...)>&
1871__packaged_task_function<_R(_ArgTypes...)>::operator=(__packaged_task_function&& __f)
1872{
1873 if (__f_ == (__base*)&__buf_)
1874 __f_->destroy();
1875 else if (__f_)
1876 __f_->destroy_deallocate();
1877 __f_ = nullptr;
1878 if (__f.__f_ == nullptr)
1879 __f_ = nullptr;
1880 else if (__f.__f_ == (__base*)&__f.__buf_)
1881 {
1882 __f_ = (__base*)&__buf_;
1883 __f.__f_->__move_to(__f_);
1884 }
1885 else
1886 {
1887 __f_ = __f.__f_;
1888 __f.__f_ = nullptr;
1889 }
1890}
1891
1892template<class _R, class ..._ArgTypes>
1893__packaged_task_function<_R(_ArgTypes...)>::~__packaged_task_function()
1894{
1895 if (__f_ == (__base*)&__buf_)
1896 __f_->destroy();
1897 else if (__f_)
1898 __f_->destroy_deallocate();
1899}
1900
1901template<class _R, class ..._ArgTypes>
1902void
1903__packaged_task_function<_R(_ArgTypes...)>::swap(__packaged_task_function& __f)
1904{
1905 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1906 {
1907 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1908 __base* __t = (__base*)&__tempbuf;
1909 __f_->__move_to(__t);
1910 __f_->destroy();
1911 __f_ = nullptr;
1912 __f.__f_->__move_to((__base*)&__buf_);
1913 __f.__f_->destroy();
1914 __f.__f_ = nullptr;
1915 __f_ = (__base*)&__buf_;
1916 __t->__move_to((__base*)&__f.__buf_);
1917 __t->destroy();
1918 __f.__f_ = (__base*)&__f.__buf_;
1919 }
1920 else if (__f_ == (__base*)&__buf_)
1921 {
1922 __f_->__move_to((__base*)&__f.__buf_);
1923 __f_->destroy();
1924 __f_ = __f.__f_;
1925 __f.__f_ = (__base*)&__f.__buf_;
1926 }
1927 else if (__f.__f_ == (__base*)&__f.__buf_)
1928 {
1929 __f.__f_->__move_to((__base*)&__buf_);
1930 __f.__f_->destroy();
1931 __f.__f_ = __f_;
1932 __f_ = (__base*)&__buf_;
1933 }
1934 else
Howard Hinnant0949eed2011-06-30 21:18:19 +00001935 _VSTD::swap(__f_, __f.__f_);
Howard Hinnant54da3382010-08-30 18:46:21 +00001936}
1937
1938template<class _R, class ..._ArgTypes>
1939inline _LIBCPP_INLINE_VISIBILITY
1940_R
1941__packaged_task_function<_R(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
1942{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001943 return (*__f_)(_VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnant54da3382010-08-30 18:46:21 +00001944}
1945
1946template<class _R, class ..._ArgTypes>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001947class _LIBCPP_VISIBLE packaged_task<_R(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:21 +00001948{
1949public:
1950 typedef _R result_type;
1951
1952private:
1953 __packaged_task_function<result_type(_ArgTypes...)> __f_;
1954 promise<result_type> __p_;
1955
1956public:
1957 // construction and destruction
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001958 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001959 packaged_task() : __p_(nullptr) {}
1960 template <class _F>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001961 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0949eed2011-06-30 21:18:19 +00001962 explicit packaged_task(_F&& __f) : __f_(_VSTD::forward<_F>(__f)) {}
Howard Hinnant54da3382010-08-30 18:46:21 +00001963 template <class _F, class _Allocator>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001964 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001965 explicit packaged_task(allocator_arg_t, const _Allocator& __a, _F&& __f)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001966 : __f_(allocator_arg, __a, _VSTD::forward<_F>(__f)),
Howard Hinnant54da3382010-08-30 18:46:21 +00001967 __p_(allocator_arg, __a) {}
1968 // ~packaged_task() = default;
1969
1970 // no copy
1971 packaged_task(packaged_task&) = delete;
1972 packaged_task& operator=(packaged_task&) = delete;
1973
1974 // move support
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001975 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001976 packaged_task(packaged_task&& __other)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001977 : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001978 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001979 packaged_task& operator=(packaged_task&& __other)
1980 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001981 __f_ = _VSTD::move(__other.__f_);
1982 __p_ = _VSTD::move(__other.__p_);
Howard Hinnant54da3382010-08-30 18:46:21 +00001983 return *this;
1984 }
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001985 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001986 void swap(packaged_task& __other)
1987 {
1988 __f_.swap(__other.__f_);
1989 __p_.swap(__other.__p_);
1990 }
1991
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001992 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7de47902010-11-30 20:23:32 +00001993 bool valid() const {return __p_.__state_ != nullptr;}
Howard Hinnant54da3382010-08-30 18:46:21 +00001994
1995 // result retrieval
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001996 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001997 future<result_type> get_future() {return __p_.get_future();}
1998
1999 // execution
2000 void operator()(_ArgTypes... __args);
2001 void make_ready_at_thread_exit(_ArgTypes... __args);
2002
2003 void reset();
2004};
2005
2006template<class _R, class ..._ArgTypes>
2007void
2008packaged_task<_R(_ArgTypes...)>::operator()(_ArgTypes... __args)
2009{
2010#ifndef _LIBCPP_NO_EXCEPTIONS
2011 if (__p_.__state_ == nullptr)
2012 throw future_error(make_error_code(future_errc::no_state));
2013 if (__p_.__state_->__has_value())
2014 throw future_error(make_error_code(future_errc::promise_already_satisfied));
2015 try
2016 {
2017#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:19 +00002018 __p_.set_value(__f_(_VSTD::forward<_ArgTypes>(__args)...));
Howard Hinnant54da3382010-08-30 18:46:21 +00002019#ifndef _LIBCPP_NO_EXCEPTIONS
2020 }
2021 catch (...)
2022 {
2023 __p_.set_exception(current_exception());
2024 }
2025#endif // _LIBCPP_NO_EXCEPTIONS
2026}
2027
2028template<class _R, class ..._ArgTypes>
2029void
2030packaged_task<_R(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
2031{
2032#ifndef _LIBCPP_NO_EXCEPTIONS
2033 if (__p_.__state_ == nullptr)
2034 throw future_error(make_error_code(future_errc::no_state));
2035 if (__p_.__state_->__has_value())
2036 throw future_error(make_error_code(future_errc::promise_already_satisfied));
2037 try
2038 {
2039#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:19 +00002040 __p_.set_value_at_thread_exit(__f_(_VSTD::forward<_ArgTypes>(__args)...));
Howard Hinnant54da3382010-08-30 18:46:21 +00002041#ifndef _LIBCPP_NO_EXCEPTIONS
2042 }
2043 catch (...)
2044 {
2045 __p_.set_exception_at_thread_exit(current_exception());
2046 }
2047#endif // _LIBCPP_NO_EXCEPTIONS
2048}
2049
2050template<class _R, class ..._ArgTypes>
2051void
2052packaged_task<_R(_ArgTypes...)>::reset()
2053{
2054#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant7de47902010-11-30 20:23:32 +00002055 if (!valid())
Howard Hinnant54da3382010-08-30 18:46:21 +00002056 throw future_error(make_error_code(future_errc::no_state));
2057#endif // _LIBCPP_NO_EXCEPTIONS
2058 __p_ = promise<result_type>();
2059}
2060
2061template<class ..._ArgTypes>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002062class _LIBCPP_VISIBLE packaged_task<void(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:21 +00002063{
2064public:
2065 typedef void result_type;
2066
2067private:
2068 __packaged_task_function<result_type(_ArgTypes...)> __f_;
2069 promise<result_type> __p_;
2070
2071public:
2072 // construction and destruction
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002073 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00002074 packaged_task() : __p_(nullptr) {}
2075 template <class _F>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002076 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0949eed2011-06-30 21:18:19 +00002077 explicit packaged_task(_F&& __f) : __f_(_VSTD::forward<_F>(__f)) {}
Howard Hinnant54da3382010-08-30 18:46:21 +00002078 template <class _F, class _Allocator>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002079 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00002080 explicit packaged_task(allocator_arg_t, const _Allocator& __a, _F&& __f)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002081 : __f_(allocator_arg, __a, _VSTD::forward<_F>(__f)),
Howard Hinnant54da3382010-08-30 18:46:21 +00002082 __p_(allocator_arg, __a) {}
2083 // ~packaged_task() = default;
2084
2085 // no copy
2086 packaged_task(packaged_task&) = delete;
2087 packaged_task& operator=(packaged_task&) = delete;
2088
2089 // move support
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002090 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00002091 packaged_task(packaged_task&& __other)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002092 : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002093 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00002094 packaged_task& operator=(packaged_task&& __other)
2095 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002096 __f_ = _VSTD::move(__other.__f_);
2097 __p_ = _VSTD::move(__other.__p_);
Howard Hinnant54da3382010-08-30 18:46:21 +00002098 return *this;
2099 }
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002100 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00002101 void swap(packaged_task& __other)
2102 {
2103 __f_.swap(__other.__f_);
2104 __p_.swap(__other.__p_);
2105 }
2106
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002107 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7de47902010-11-30 20:23:32 +00002108 bool valid() const {return __p_.__state_ != nullptr;}
Howard Hinnant54da3382010-08-30 18:46:21 +00002109
2110 // result retrieval
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002111 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00002112 future<result_type> get_future() {return __p_.get_future();}
2113
2114 // execution
2115 void operator()(_ArgTypes... __args);
2116 void make_ready_at_thread_exit(_ArgTypes... __args);
2117
2118 void reset();
2119};
2120
2121template<class ..._ArgTypes>
2122void
2123packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args)
2124{
2125#ifndef _LIBCPP_NO_EXCEPTIONS
2126 if (__p_.__state_ == nullptr)
2127 throw future_error(make_error_code(future_errc::no_state));
2128 if (__p_.__state_->__has_value())
2129 throw future_error(make_error_code(future_errc::promise_already_satisfied));
2130 try
2131 {
2132#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:19 +00002133 __f_(_VSTD::forward<_ArgTypes>(__args)...);
Howard Hinnant54da3382010-08-30 18:46:21 +00002134 __p_.set_value();
2135#ifndef _LIBCPP_NO_EXCEPTIONS
2136 }
2137 catch (...)
2138 {
2139 __p_.set_exception(current_exception());
2140 }
2141#endif // _LIBCPP_NO_EXCEPTIONS
2142}
2143
2144template<class ..._ArgTypes>
2145void
2146packaged_task<void(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
2147{
2148#ifndef _LIBCPP_NO_EXCEPTIONS
2149 if (__p_.__state_ == nullptr)
2150 throw future_error(make_error_code(future_errc::no_state));
2151 if (__p_.__state_->__has_value())
2152 throw future_error(make_error_code(future_errc::promise_already_satisfied));
2153 try
2154 {
2155#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:19 +00002156 __f_(_VSTD::forward<_ArgTypes>(__args)...);
Howard Hinnant54da3382010-08-30 18:46:21 +00002157 __p_.set_value_at_thread_exit();
2158#ifndef _LIBCPP_NO_EXCEPTIONS
2159 }
2160 catch (...)
2161 {
2162 __p_.set_exception_at_thread_exit(current_exception());
2163 }
2164#endif // _LIBCPP_NO_EXCEPTIONS
2165}
2166
2167template<class ..._ArgTypes>
2168void
2169packaged_task<void(_ArgTypes...)>::reset()
2170{
2171#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant7de47902010-11-30 20:23:32 +00002172 if (!valid())
Howard Hinnant54da3382010-08-30 18:46:21 +00002173 throw future_error(make_error_code(future_errc::no_state));
2174#endif // _LIBCPP_NO_EXCEPTIONS
2175 __p_ = promise<result_type>();
2176}
2177
2178template <class _Callable>
2179inline _LIBCPP_INLINE_VISIBILITY
2180void
2181swap(packaged_task<_Callable>& __x, packaged_task<_Callable>& __y)
2182{
2183 __x.swap(__y);
2184}
2185
2186template <class _Callable, class _Alloc>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002187struct _LIBCPP_VISIBLE uses_allocator<packaged_task<_Callable>, _Alloc>
2188 : public true_type {};
Howard Hinnant54da3382010-08-30 18:46:21 +00002189
2190template <class _R, class _F>
2191future<_R>
Howard Hinnant73d21a42010-09-04 23:28:19 +00002192#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21 +00002193__make_deferred_assoc_state(_F&& __f)
2194#else
2195__make_deferred_assoc_state(_F __f)
2196#endif
2197{
2198 unique_ptr<__deferred_assoc_state<_R, _F>, __release_shared_count>
Howard Hinnant0949eed2011-06-30 21:18:19 +00002199 __h(new __deferred_assoc_state<_R, _F>(_VSTD::forward<_F>(__f)));
Howard Hinnant54da3382010-08-30 18:46:21 +00002200 return future<_R>(__h.get());
2201}
2202
Howard Hinnant57cff292011-05-19 15:05:04 +00002203template <class _R, class _F>
2204future<_R>
2205#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2206__make_async_assoc_state(_F&& __f)
2207#else
2208__make_async_assoc_state(_F __f)
2209#endif
2210{
2211 unique_ptr<__async_assoc_state<_R, _F>, __release_shared_count>
Howard Hinnant0949eed2011-06-30 21:18:19 +00002212 __h(new __async_assoc_state<_R, _F>(_VSTD::forward<_F>(__f)));
2213 _VSTD::thread(&__async_assoc_state<_R, _F>::__execute, __h.get()).detach();
Howard Hinnant57cff292011-05-19 15:05:04 +00002214 return future<_R>(__h.get());
2215}
2216
Howard Hinnant54da3382010-08-30 18:46:21 +00002217template <class _F, class... _Args>
Howard Hinnant57cff292011-05-19 15:05:04 +00002218class __async_func
2219{
2220 tuple<_F, _Args...> __f_;
2221
2222public:
2223 typedef typename __invoke_of<_F, _Args...>::type _R;
2224
2225 _LIBCPP_INLINE_VISIBILITY
2226 explicit __async_func(_F&& __f, _Args&&... __args)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002227 : __f_(_VSTD::move(__f), _VSTD::move(__args)...) {}
Howard Hinnant57cff292011-05-19 15:05:04 +00002228
2229 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0949eed2011-06-30 21:18:19 +00002230 __async_func(__async_func&& __f) : __f_(_VSTD::move(__f.__f_)) {}
Howard Hinnant57cff292011-05-19 15:05:04 +00002231
2232 _R operator()()
2233 {
2234 typedef typename __make_tuple_indices<1+sizeof...(_Args), 1>::type _Index;
2235 return __execute(_Index());
2236 }
2237private:
2238 template <size_t ..._Indices>
2239 _R
2240 __execute(__tuple_indices<_Indices...>)
2241 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002242 return __invoke(_VSTD::move(_VSTD::get<0>(__f_)), _VSTD::move(_VSTD::get<_Indices>(__f_))...);
Howard Hinnant57cff292011-05-19 15:05:04 +00002243 }
2244};
2245
2246template <class _F, class... _Args>
2247future<typename __invoke_of<typename decay<_F>::type, typename decay<_Args>::type...>::type>
Howard Hinnant54da3382010-08-30 18:46:21 +00002248async(launch __policy, _F&& __f, _Args&&... __args)
2249{
Howard Hinnant57cff292011-05-19 15:05:04 +00002250 typedef __async_func<typename decay<_F>::type, typename decay<_Args>::type...> _BF;
2251 typedef typename _BF::_R _R;
Howard Hinnant54da3382010-08-30 18:46:21 +00002252 future<_R> __r;
Howard Hinnant66895642010-11-23 18:33:54 +00002253 if (__policy & launch::async)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002254 __r = _VSTD::__make_async_assoc_state<_R>(_BF(__decay_copy(_VSTD::forward<_F>(__f)),
2255 __decay_copy(_VSTD::forward<_Args>(__args))...));
Howard Hinnant66895642010-11-23 18:33:54 +00002256 else if (__policy & launch::deferred)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002257 __r = _VSTD::__make_deferred_assoc_state<_R>(_BF(__decay_copy(_VSTD::forward<_F>(__f)),
2258 __decay_copy(_VSTD::forward<_Args>(__args))...));
Howard Hinnant54da3382010-08-30 18:46:21 +00002259 return __r;
2260}
2261
2262template <class _F, class... _Args>
2263inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57cff292011-05-19 15:05:04 +00002264future<typename __invoke_of<typename decay<_F>::type, typename decay<_Args>::type...>::type>
Howard Hinnant54da3382010-08-30 18:46:21 +00002265async(_F&& __f, _Args&&... __args)
2266{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002267 return _VSTD::async(launch::any, _VSTD::forward<_F>(__f),
2268 _VSTD::forward<_Args>(__args)...);
Howard Hinnant54da3382010-08-30 18:46:21 +00002269}
2270
2271#endif // _LIBCPP_HAS_NO_VARIADICS
2272
Howard Hinnante6e4d012010-09-03 21:46:37 +00002273// shared_future
2274
Howard Hinnant99be8232010-09-03 18:39:25 +00002275template <class _R>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002276class _LIBCPP_VISIBLE shared_future
Howard Hinnant99be8232010-09-03 18:39:25 +00002277{
2278 __assoc_state<_R>* __state_;
2279
2280public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002281 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002282 shared_future() : __state_(nullptr) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002283 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002284 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2285 {if (__state_) __state_->__add_shared();}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002286#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002287 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002288 shared_future(future<_R>&& __f) : __state_(__f.__state_)
2289 {__f.__state_ = nullptr;}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002290 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002291 shared_future(shared_future&& __rhs) : __state_(__rhs.__state_)
2292 {__rhs.__state_ = nullptr;}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002293#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:25 +00002294 ~shared_future();
2295 shared_future& operator=(const shared_future& __rhs);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002296#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002297 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002298 shared_future& operator=(shared_future&& __rhs)
2299 {
2300 shared_future(std::move(__rhs)).swap(*this);
2301 return *this;
2302 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002303#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:25 +00002304
2305 // retrieving the value
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002306 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002307 const _R& get() const {return __state_->copy();}
2308
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002309 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0949eed2011-06-30 21:18:19 +00002310 void swap(shared_future& __rhs) {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant99be8232010-09-03 18:39:25 +00002311
2312 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002313 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002314 bool valid() const {return __state_ != nullptr;}
2315
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002316 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002317 void wait() const {__state_->wait();}
2318 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002319 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002320 future_status
2321 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2322 {return __state_->wait_for(__rel_time);}
2323 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002324 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002325 future_status
2326 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2327 {return __state_->wait_until(__abs_time);}
2328};
2329
2330template <class _R>
2331shared_future<_R>::~shared_future()
2332{
2333 if (__state_)
2334 __state_->__release_shared();
2335}
2336
2337template <class _R>
2338shared_future<_R>&
2339shared_future<_R>::operator=(const shared_future& __rhs)
2340{
2341 if (__rhs.__state_)
2342 __rhs.__state_->__add_shared();
2343 if (__state_)
2344 __state_->__release_shared();
2345 __state_ = __rhs.__state_;
2346 return *this;
2347}
2348
2349template <class _R>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002350class _LIBCPP_VISIBLE shared_future<_R&>
Howard Hinnant99be8232010-09-03 18:39:25 +00002351{
2352 __assoc_state<_R&>* __state_;
2353
2354public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002355 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002356 shared_future() : __state_(nullptr) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002357 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002358 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2359 {if (__state_) __state_->__add_shared();}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002360#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002361 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002362 shared_future(future<_R&>&& __f) : __state_(__f.__state_)
2363 {__f.__state_ = nullptr;}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002364 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002365 shared_future(shared_future&& __rhs) : __state_(__rhs.__state_)
2366 {__rhs.__state_ = nullptr;}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002367#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:25 +00002368 ~shared_future();
2369 shared_future& operator=(const shared_future& __rhs);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002370#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002371 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002372 shared_future& operator=(shared_future&& __rhs)
2373 {
2374 shared_future(std::move(__rhs)).swap(*this);
2375 return *this;
2376 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002377#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:25 +00002378
2379 // retrieving the value
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002380 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002381 _R& get() const {return __state_->copy();}
2382
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002383 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0949eed2011-06-30 21:18:19 +00002384 void swap(shared_future& __rhs) {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant99be8232010-09-03 18:39:25 +00002385
2386 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002387 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002388 bool valid() const {return __state_ != nullptr;}
2389
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002390 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002391 void wait() const {__state_->wait();}
2392 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002393 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002394 future_status
2395 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2396 {return __state_->wait_for(__rel_time);}
2397 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002398 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002399 future_status
2400 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2401 {return __state_->wait_until(__abs_time);}
2402};
2403
2404template <class _R>
2405shared_future<_R&>::~shared_future()
2406{
2407 if (__state_)
2408 __state_->__release_shared();
2409}
2410
2411template <class _R>
2412shared_future<_R&>&
2413shared_future<_R&>::operator=(const shared_future& __rhs)
2414{
2415 if (__rhs.__state_)
2416 __rhs.__state_->__add_shared();
2417 if (__state_)
2418 __state_->__release_shared();
2419 __state_ = __rhs.__state_;
2420 return *this;
2421}
2422
2423template <>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002424class _LIBCPP_VISIBLE shared_future<void>
Howard Hinnant99be8232010-09-03 18:39:25 +00002425{
2426 __assoc_sub_state* __state_;
2427
2428public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002429 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002430 shared_future() : __state_(nullptr) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002431 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002432 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2433 {if (__state_) __state_->__add_shared();}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002434#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002435 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002436 shared_future(future<void>&& __f) : __state_(__f.__state_)
2437 {__f.__state_ = nullptr;}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002438 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002439 shared_future(shared_future&& __rhs) : __state_(__rhs.__state_)
2440 {__rhs.__state_ = nullptr;}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002441#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:25 +00002442 ~shared_future();
2443 shared_future& operator=(const shared_future& __rhs);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002444#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002445 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002446 shared_future& operator=(shared_future&& __rhs)
2447 {
2448 shared_future(std::move(__rhs)).swap(*this);
2449 return *this;
2450 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002451#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:25 +00002452
2453 // retrieving the value
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002454 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002455 void get() const {__state_->copy();}
2456
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002457 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0949eed2011-06-30 21:18:19 +00002458 void swap(shared_future& __rhs) {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant99be8232010-09-03 18:39:25 +00002459
2460 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002461 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002462 bool valid() const {return __state_ != nullptr;}
2463
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002464 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002465 void wait() const {__state_->wait();}
2466 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002467 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002468 future_status
2469 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2470 {return __state_->wait_for(__rel_time);}
2471 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002472 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002473 future_status
2474 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2475 {return __state_->wait_until(__abs_time);}
2476};
2477
2478template <class _R>
2479inline _LIBCPP_INLINE_VISIBILITY
2480void
2481swap(shared_future<_R>& __x, shared_future<_R>& __y)
2482{
2483 __x.swap(__y);
2484}
2485
Howard Hinnante6e4d012010-09-03 21:46:37 +00002486template <class _R>
Howard Hinnant7de47902010-11-30 20:23:32 +00002487inline _LIBCPP_INLINE_VISIBILITY
2488shared_future<_R>
2489future<_R>::share()
Howard Hinnante6e4d012010-09-03 21:46:37 +00002490{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002491 return shared_future<_R>(_VSTD::move(*this));
Howard Hinnante6e4d012010-09-03 21:46:37 +00002492}
2493
2494template <class _R>
Howard Hinnante6e4d012010-09-03 21:46:37 +00002495inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7de47902010-11-30 20:23:32 +00002496shared_future<_R&>
2497future<_R&>::share()
Howard Hinnante6e4d012010-09-03 21:46:37 +00002498{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002499 return shared_future<_R&>(_VSTD::move(*this));
Howard Hinnant7de47902010-11-30 20:23:32 +00002500}
2501
Howard Hinnanta4451512010-12-02 16:45:21 +00002502#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2503
Howard Hinnant7de47902010-11-30 20:23:32 +00002504inline _LIBCPP_INLINE_VISIBILITY
2505shared_future<void>
2506future<void>::share()
2507{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002508 return shared_future<void>(_VSTD::move(*this));
Howard Hinnante6e4d012010-09-03 21:46:37 +00002509}
2510
Howard Hinnanta4451512010-12-02 16:45:21 +00002511#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2512
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002513_LIBCPP_END_NAMESPACE_STD
2514
2515#endif // _LIBCPP_FUTURE