blob: 9f3780bc711065454e4f227456c9b175168d7370 [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_);
588 if (this->__has_value())
589 throw future_error(make_error_code(future_errc::promise_already_satisfied));
Howard Hinnant0949eed2011-06-30 21:18:19 +0000590 ::new(&__value_) _R(_VSTD::forward<_Arg>(__arg));
Howard Hinnant47499b12010-08-27 20:10:19 +0000591 this->__state_ |= base::__constructed | base::ready;
592 __lk.unlock();
593 __cv_.notify_all();
594}
595
596template <class _R>
597template <class _Arg>
598void
Howard Hinnant73d21a42010-09-04 23:28:19 +0000599#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +0000600__assoc_state<_R>::set_value_at_thread_exit(_Arg&& __arg)
601#else
602__assoc_state<_R>::set_value_at_thread_exit(_Arg& __arg)
603#endif
604{
605 unique_lock<mutex> __lk(this->__mut_);
606 if (this->__has_value())
607 throw future_error(make_error_code(future_errc::promise_already_satisfied));
Howard Hinnant0949eed2011-06-30 21:18:19 +0000608 ::new(&__value_) _R(_VSTD::forward<_Arg>(__arg));
Howard Hinnant47499b12010-08-27 20:10:19 +0000609 this->__state_ |= base::__constructed;
Howard Hinnant5306d682010-10-14 19:18:04 +0000610 __thread_local_data()->__make_ready_at_thread_exit(this);
Howard Hinnant47499b12010-08-27 20:10:19 +0000611 __lk.unlock();
612}
613
614template <class _R>
615_R
616__assoc_state<_R>::move()
617{
618 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant54da3382010-08-30 18:46:21 +0000619 this->__sub_wait(__lk);
Howard Hinnant47499b12010-08-27 20:10:19 +0000620 if (this->__exception_ != nullptr)
621 rethrow_exception(this->__exception_);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000622 return _VSTD::move(*reinterpret_cast<_R*>(&__value_));
Howard Hinnant47499b12010-08-27 20:10:19 +0000623}
624
625template <class _R>
626typename add_lvalue_reference<_R>::type
627__assoc_state<_R>::copy()
628{
629 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant54da3382010-08-30 18:46:21 +0000630 this->__sub_wait(__lk);
Howard Hinnant47499b12010-08-27 20:10:19 +0000631 if (this->__exception_ != nullptr)
632 rethrow_exception(this->__exception_);
633 return *reinterpret_cast<_R*>(&__value_);
634}
635
Howard Hinnantf39daa82010-08-28 21:01:06 +0000636template <class _R>
637class __assoc_state<_R&>
638 : public __assoc_sub_state
639{
640 typedef __assoc_sub_state base;
641 typedef _R* _U;
642protected:
643 _U __value_;
644
Howard Hinnant1694d232011-05-28 14:41:13 +0000645 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnantf39daa82010-08-28 21:01:06 +0000646public:
647
648 void set_value(_R& __arg);
649 void set_value_at_thread_exit(_R& __arg);
650
651 _R& copy();
652};
653
654template <class _R>
655void
Howard Hinnant1694d232011-05-28 14:41:13 +0000656__assoc_state<_R&>::__on_zero_shared() _NOEXCEPT
Howard Hinnantf39daa82010-08-28 21:01:06 +0000657{
658 delete this;
659}
660
661template <class _R>
662void
663__assoc_state<_R&>::set_value(_R& __arg)
664{
665 unique_lock<mutex> __lk(this->__mut_);
666 if (this->__has_value())
667 throw future_error(make_error_code(future_errc::promise_already_satisfied));
668 __value_ = &__arg;
669 this->__state_ |= base::__constructed | base::ready;
670 __lk.unlock();
671 __cv_.notify_all();
672}
673
674template <class _R>
675void
676__assoc_state<_R&>::set_value_at_thread_exit(_R& __arg)
677{
678 unique_lock<mutex> __lk(this->__mut_);
679 if (this->__has_value())
680 throw future_error(make_error_code(future_errc::promise_already_satisfied));
681 __value_ = &__arg;
682 this->__state_ |= base::__constructed;
Howard Hinnant5306d682010-10-14 19:18:04 +0000683 __thread_local_data()->__make_ready_at_thread_exit(this);
Howard Hinnantf39daa82010-08-28 21:01:06 +0000684 __lk.unlock();
685}
686
687template <class _R>
688_R&
689__assoc_state<_R&>::copy()
690{
691 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant54da3382010-08-30 18:46:21 +0000692 this->__sub_wait(__lk);
Howard Hinnantf39daa82010-08-28 21:01:06 +0000693 if (this->__exception_ != nullptr)
694 rethrow_exception(this->__exception_);
695 return *__value_;
696}
697
Howard Hinnant47499b12010-08-27 20:10:19 +0000698template <class _R, class _Alloc>
699class __assoc_state_alloc
700 : public __assoc_state<_R>
701{
702 typedef __assoc_state<_R> base;
703 _Alloc __alloc_;
704
Howard Hinnant1694d232011-05-28 14:41:13 +0000705 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant47499b12010-08-27 20:10:19 +0000706public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000707 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000708 explicit __assoc_state_alloc(const _Alloc& __a)
709 : __alloc_(__a) {}
710};
711
712template <class _R, class _Alloc>
713void
Howard Hinnant1694d232011-05-28 14:41:13 +0000714__assoc_state_alloc<_R, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +0000715{
716 if (this->__state_ & base::__constructed)
717 reinterpret_cast<_R*>(&this->__value_)->~_R();
718 typename _Alloc::template rebind<__assoc_state_alloc>::other __a(__alloc_);
719 this->~__assoc_state_alloc();
720 __a.deallocate(this, 1);
721}
722
Howard Hinnantf39daa82010-08-28 21:01:06 +0000723template <class _R, class _Alloc>
724class __assoc_state_alloc<_R&, _Alloc>
725 : public __assoc_state<_R&>
726{
727 typedef __assoc_state<_R&> base;
728 _Alloc __alloc_;
729
Howard Hinnant1694d232011-05-28 14:41:13 +0000730 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnantf39daa82010-08-28 21:01:06 +0000731public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000732 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39daa82010-08-28 21:01:06 +0000733 explicit __assoc_state_alloc(const _Alloc& __a)
734 : __alloc_(__a) {}
735};
736
737template <class _R, class _Alloc>
738void
Howard Hinnant1694d232011-05-28 14:41:13 +0000739__assoc_state_alloc<_R&, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantf39daa82010-08-28 21:01:06 +0000740{
741 typename _Alloc::template rebind<__assoc_state_alloc>::other __a(__alloc_);
742 this->~__assoc_state_alloc();
743 __a.deallocate(this, 1);
744}
745
Howard Hinnant47499b12010-08-27 20:10:19 +0000746template <class _Alloc>
747class __assoc_sub_state_alloc
748 : public __assoc_sub_state
749{
750 typedef __assoc_sub_state base;
751 _Alloc __alloc_;
752
Howard Hinnant1694d232011-05-28 14:41:13 +0000753 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant47499b12010-08-27 20:10:19 +0000754public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000755 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000756 explicit __assoc_sub_state_alloc(const _Alloc& __a)
757 : __alloc_(__a) {}
758};
759
760template <class _Alloc>
761void
Howard Hinnant1694d232011-05-28 14:41:13 +0000762__assoc_sub_state_alloc<_Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +0000763{
764 this->~base();
Howard Hinnantf39daa82010-08-28 21:01:06 +0000765 typename _Alloc::template rebind<__assoc_sub_state_alloc>::other __a(__alloc_);
Howard Hinnant47499b12010-08-27 20:10:19 +0000766 this->~__assoc_sub_state_alloc();
767 __a.deallocate(this, 1);
768}
769
Howard Hinnant54da3382010-08-30 18:46:21 +0000770template <class _R, class _F>
771class __deferred_assoc_state
772 : public __assoc_state<_R>
773{
774 typedef __assoc_state<_R> base;
775
776 _F __func_;
777
778public:
Howard Hinnant73d21a42010-09-04 23:28:19 +0000779#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21 +0000780 explicit __deferred_assoc_state(_F&& __f);
781#endif
782
783 virtual void __execute();
784};
785
Howard Hinnant73d21a42010-09-04 23:28:19 +0000786#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21 +0000787
788template <class _R, class _F>
789inline _LIBCPP_INLINE_VISIBILITY
790__deferred_assoc_state<_R, _F>::__deferred_assoc_state(_F&& __f)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000791 : __func_(_VSTD::forward<_F>(__f))
Howard Hinnant54da3382010-08-30 18:46:21 +0000792{
793 this->__set_deferred();
794}
795
Howard Hinnant73d21a42010-09-04 23:28:19 +0000796#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21 +0000797
798template <class _R, class _F>
799void
800__deferred_assoc_state<_R, _F>::__execute()
801{
802#ifndef _LIBCPP_NO_EXCEPTIONS
803 try
804 {
805#endif // _LIBCPP_NO_EXCEPTIONS
806 this->set_value(__func_());
807#ifndef _LIBCPP_NO_EXCEPTIONS
808 }
809 catch (...)
810 {
811 this->set_exception(current_exception());
812 }
813#endif // _LIBCPP_NO_EXCEPTIONS
814}
815
816template <class _F>
817class __deferred_assoc_state<void, _F>
818 : public __assoc_sub_state
819{
820 typedef __assoc_sub_state base;
821
822 _F __func_;
823
824public:
Howard Hinnant73d21a42010-09-04 23:28:19 +0000825#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21 +0000826 explicit __deferred_assoc_state(_F&& __f);
827#endif
828
829 virtual void __execute();
830};
831
Howard Hinnant73d21a42010-09-04 23:28:19 +0000832#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21 +0000833
834template <class _F>
835inline _LIBCPP_INLINE_VISIBILITY
836__deferred_assoc_state<void, _F>::__deferred_assoc_state(_F&& __f)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000837 : __func_(_VSTD::forward<_F>(__f))
Howard Hinnant54da3382010-08-30 18:46:21 +0000838{
839 this->__set_deferred();
840}
841
Howard Hinnant73d21a42010-09-04 23:28:19 +0000842#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21 +0000843
844template <class _F>
845void
846__deferred_assoc_state<void, _F>::__execute()
847{
848#ifndef _LIBCPP_NO_EXCEPTIONS
849 try
850 {
851#endif // _LIBCPP_NO_EXCEPTIONS
852 __func_();
853 this->set_value();
854#ifndef _LIBCPP_NO_EXCEPTIONS
855 }
856 catch (...)
857 {
858 this->set_exception(current_exception());
859 }
860#endif // _LIBCPP_NO_EXCEPTIONS
861}
862
Howard Hinnant57cff292011-05-19 15:05:04 +0000863template <class _R, class _F>
864class __async_assoc_state
865 : public __assoc_state<_R>
866{
867 typedef __assoc_state<_R> base;
868
869 _F __func_;
870
Howard Hinnant1694d232011-05-28 14:41:13 +0000871 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant57cff292011-05-19 15:05:04 +0000872public:
873#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
874 explicit __async_assoc_state(_F&& __f);
875#endif
876
877 virtual void __execute();
878};
879
880#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
881
882template <class _R, class _F>
883inline _LIBCPP_INLINE_VISIBILITY
884__async_assoc_state<_R, _F>::__async_assoc_state(_F&& __f)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000885 : __func_(_VSTD::forward<_F>(__f))
Howard Hinnant57cff292011-05-19 15:05:04 +0000886{
887}
888
889#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
890
891template <class _R, class _F>
892void
893__async_assoc_state<_R, _F>::__execute()
894{
895#ifndef _LIBCPP_NO_EXCEPTIONS
896 try
897 {
898#endif // _LIBCPP_NO_EXCEPTIONS
899 this->set_value(__func_());
900#ifndef _LIBCPP_NO_EXCEPTIONS
901 }
902 catch (...)
903 {
904 this->set_exception(current_exception());
905 }
906#endif // _LIBCPP_NO_EXCEPTIONS
907}
908
909template <class _R, class _F>
910void
Howard Hinnant1694d232011-05-28 14:41:13 +0000911__async_assoc_state<_R, _F>::__on_zero_shared() _NOEXCEPT
Howard Hinnant57cff292011-05-19 15:05:04 +0000912{
913 this->wait();
914 base::__on_zero_shared();
915}
916
917template <class _F>
918class __async_assoc_state<void, _F>
919 : public __assoc_sub_state
920{
921 typedef __assoc_sub_state base;
922
923 _F __func_;
924
Howard Hinnant1694d232011-05-28 14:41:13 +0000925 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant57cff292011-05-19 15:05:04 +0000926public:
927#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
928 explicit __async_assoc_state(_F&& __f);
929#endif
930
931 virtual void __execute();
932};
933
934#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
935
936template <class _F>
937inline _LIBCPP_INLINE_VISIBILITY
938__async_assoc_state<void, _F>::__async_assoc_state(_F&& __f)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000939 : __func_(_VSTD::forward<_F>(__f))
Howard Hinnant57cff292011-05-19 15:05:04 +0000940{
941}
942
943#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
944
945template <class _F>
946void
947__async_assoc_state<void, _F>::__execute()
948{
949#ifndef _LIBCPP_NO_EXCEPTIONS
950 try
951 {
952#endif // _LIBCPP_NO_EXCEPTIONS
953 __func_();
954 this->set_value();
955#ifndef _LIBCPP_NO_EXCEPTIONS
956 }
957 catch (...)
958 {
959 this->set_exception(current_exception());
960 }
961#endif // _LIBCPP_NO_EXCEPTIONS
962}
963
964template <class _F>
965void
Howard Hinnant1694d232011-05-28 14:41:13 +0000966__async_assoc_state<void, _F>::__on_zero_shared() _NOEXCEPT
Howard Hinnant57cff292011-05-19 15:05:04 +0000967{
968 this->wait();
969 base::__on_zero_shared();
970}
971
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000972template <class _R> class promise;
973template <class _R> class shared_future;
Howard Hinnant47499b12010-08-27 20:10:19 +0000974
975// future
976
Howard Hinnant54da3382010-08-30 18:46:21 +0000977template <class _R> class future;
978
979template <class _R, class _F>
980future<_R>
Howard Hinnant73d21a42010-09-04 23:28:19 +0000981#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21 +0000982__make_deferred_assoc_state(_F&& __f);
983#else
984__make_deferred_assoc_state(_F __f);
985#endif
986
Howard Hinnant57cff292011-05-19 15:05:04 +0000987template <class _R, class _F>
988future<_R>
989#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
990__make_async_assoc_state(_F&& __f);
991#else
992__make_async_assoc_state(_F __f);
993#endif
994
Howard Hinnant47499b12010-08-27 20:10:19 +0000995template <class _R>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000996class _LIBCPP_VISIBLE future
Howard Hinnant47499b12010-08-27 20:10:19 +0000997{
998 __assoc_state<_R>* __state_;
999
1000 explicit future(__assoc_state<_R>* __state);
1001
1002 template <class> friend class promise;
Howard Hinnant99be8232010-09-03 18:39:25 +00001003 template <class> friend class shared_future;
Howard Hinnant54da3382010-08-30 18:46:21 +00001004
Howard Hinnant73d21a42010-09-04 23:28:19 +00001005#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57cff292011-05-19 15:05:04 +00001006 template <class _R1, class _F>
Howard Hinnant54da3382010-08-30 18:46:21 +00001007 friend future<_R1> __make_deferred_assoc_state(_F&& __f);
Howard Hinnant57cff292011-05-19 15:05:04 +00001008 template <class _R1, class _F>
1009 friend future<_R1> __make_async_assoc_state(_F&& __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001010#else
Howard Hinnant57cff292011-05-19 15:05:04 +00001011 template <class _R1, class _F>
Howard Hinnant54da3382010-08-30 18:46:21 +00001012 friend future<_R1> __make_deferred_assoc_state(_F __f);
Howard Hinnant57cff292011-05-19 15:05:04 +00001013 template <class _R1, class _F>
1014 friend future<_R1> __make_async_assoc_state(_F __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001015#endif
1016
Howard Hinnant47499b12010-08-27 20:10:19 +00001017public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001018 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001019 future() : __state_(nullptr) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001020#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001021 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001022 future(future&& __rhs)
1023 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1024 future(const future&) = delete;
1025 future& operator=(const future&) = delete;
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001026 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001027 future& operator=(future&& __rhs)
1028 {
1029 future(std::move(__rhs)).swap(*this);
1030 return *this;
1031 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001032#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001033private:
1034 future(const future&);
1035 future& operator=(const future&);
1036public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001037#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001038 ~future();
Howard Hinnant7de47902010-11-30 20:23:32 +00001039 shared_future<_R> share();
Howard Hinnant47499b12010-08-27 20:10:19 +00001040
1041 // retrieving the value
1042 _R get();
1043
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001044 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0949eed2011-06-30 21:18:19 +00001045 void swap(future& __rhs) {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:19 +00001046
1047 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001048 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001049 bool valid() const {return __state_ != nullptr;}
1050
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001051 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001052 void wait() const {__state_->wait();}
1053 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001054 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001055 future_status
1056 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1057 {return __state_->wait_for(__rel_time);}
1058 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001059 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001060 future_status
1061 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1062 {return __state_->wait_until(__abs_time);}
1063};
1064
1065template <class _R>
1066future<_R>::future(__assoc_state<_R>* __state)
1067 : __state_(__state)
1068{
1069 if (__state_->__has_future_attached())
1070 throw future_error(make_error_code(future_errc::future_already_retrieved));
1071 __state_->__add_shared();
Howard Hinnant54da3382010-08-30 18:46:21 +00001072 __state_->__set_future_attached();
Howard Hinnant47499b12010-08-27 20:10:19 +00001073}
1074
Howard Hinnant54da3382010-08-30 18:46:21 +00001075struct __release_shared_count
1076{
1077 void operator()(__shared_count* p) {p->__release_shared();}
1078};
1079
Howard Hinnant47499b12010-08-27 20:10:19 +00001080template <class _R>
1081future<_R>::~future()
1082{
1083 if (__state_)
1084 __state_->__release_shared();
1085}
1086
1087template <class _R>
1088_R
1089future<_R>::get()
1090{
Howard Hinnant54da3382010-08-30 18:46:21 +00001091 unique_ptr<__shared_count, __release_shared_count> __(__state_);
Howard Hinnant47499b12010-08-27 20:10:19 +00001092 __assoc_state<_R>* __s = __state_;
1093 __state_ = nullptr;
1094 return __s->move();
1095}
1096
1097template <class _R>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001098class _LIBCPP_VISIBLE future<_R&>
Howard Hinnant47499b12010-08-27 20:10:19 +00001099{
1100 __assoc_state<_R&>* __state_;
1101
1102 explicit future(__assoc_state<_R&>* __state);
1103
1104 template <class> friend class promise;
Howard Hinnant99be8232010-09-03 18:39:25 +00001105 template <class> friend class shared_future;
Howard Hinnant54da3382010-08-30 18:46:21 +00001106
Howard Hinnant73d21a42010-09-04 23:28:19 +00001107#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57cff292011-05-19 15:05:04 +00001108 template <class _R1, class _F>
Howard Hinnant54da3382010-08-30 18:46:21 +00001109 friend future<_R1> __make_deferred_assoc_state(_F&& __f);
Howard Hinnant57cff292011-05-19 15:05:04 +00001110 template <class _R1, class _F>
1111 friend future<_R1> __make_async_assoc_state(_F&& __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001112#else
Howard Hinnant57cff292011-05-19 15:05:04 +00001113 template <class _R1, class _F>
Howard Hinnant54da3382010-08-30 18:46:21 +00001114 friend future<_R1> __make_deferred_assoc_state(_F __f);
Howard Hinnant57cff292011-05-19 15:05:04 +00001115 template <class _R1, class _F>
1116 friend future<_R1> __make_async_assoc_state(_F __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001117#endif
1118
Howard Hinnant47499b12010-08-27 20:10:19 +00001119public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001120 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001121 future() : __state_(nullptr) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001122#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001123 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001124 future(future&& __rhs)
1125 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1126 future(const future&) = delete;
1127 future& operator=(const future&) = delete;
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001128 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001129 future& operator=(future&& __rhs)
1130 {
1131 future(std::move(__rhs)).swap(*this);
1132 return *this;
1133 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001134#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001135private:
1136 future(const future&);
1137 future& operator=(const future&);
1138public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001139#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001140 ~future();
Howard Hinnant7de47902010-11-30 20:23:32 +00001141 shared_future<_R&> share();
Howard Hinnant47499b12010-08-27 20:10:19 +00001142
1143 // retrieving the value
1144 _R& get();
1145
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001146 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0949eed2011-06-30 21:18:19 +00001147 void swap(future& __rhs) {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:19 +00001148
1149 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001150 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001151 bool valid() const {return __state_ != nullptr;}
1152
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001153 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001154 void wait() const {__state_->wait();}
1155 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001156 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001157 future_status
1158 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1159 {return __state_->wait_for(__rel_time);}
1160 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001161 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001162 future_status
1163 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1164 {return __state_->wait_until(__abs_time);}
1165};
1166
1167template <class _R>
1168future<_R&>::future(__assoc_state<_R&>* __state)
1169 : __state_(__state)
1170{
1171 if (__state_->__has_future_attached())
1172 throw future_error(make_error_code(future_errc::future_already_retrieved));
1173 __state_->__add_shared();
Howard Hinnant54da3382010-08-30 18:46:21 +00001174 __state_->__set_future_attached();
Howard Hinnant47499b12010-08-27 20:10:19 +00001175}
1176
1177template <class _R>
1178future<_R&>::~future()
1179{
1180 if (__state_)
1181 __state_->__release_shared();
1182}
1183
1184template <class _R>
1185_R&
1186future<_R&>::get()
1187{
Howard Hinnant54da3382010-08-30 18:46:21 +00001188 unique_ptr<__shared_count, __release_shared_count> __(__state_);
Howard Hinnantf39daa82010-08-28 21:01:06 +00001189 __assoc_state<_R&>* __s = __state_;
Howard Hinnant47499b12010-08-27 20:10:19 +00001190 __state_ = nullptr;
1191 return __s->copy();
1192}
1193
1194template <>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001195class _LIBCPP_VISIBLE future<void>
Howard Hinnant47499b12010-08-27 20:10:19 +00001196{
1197 __assoc_sub_state* __state_;
1198
1199 explicit future(__assoc_sub_state* __state);
1200
1201 template <class> friend class promise;
Howard Hinnant99be8232010-09-03 18:39:25 +00001202 template <class> friend class shared_future;
Howard Hinnant54da3382010-08-30 18:46:21 +00001203
Howard Hinnant73d21a42010-09-04 23:28:19 +00001204#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57cff292011-05-19 15:05:04 +00001205 template <class _R1, class _F>
Howard Hinnant54da3382010-08-30 18:46:21 +00001206 friend future<_R1> __make_deferred_assoc_state(_F&& __f);
Howard Hinnant57cff292011-05-19 15:05:04 +00001207 template <class _R1, class _F>
1208 friend future<_R1> __make_async_assoc_state(_F&& __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001209#else
Howard Hinnant57cff292011-05-19 15:05:04 +00001210 template <class _R1, class _F>
Howard Hinnant54da3382010-08-30 18:46:21 +00001211 friend future<_R1> __make_deferred_assoc_state(_F __f);
Howard Hinnant57cff292011-05-19 15:05:04 +00001212 template <class _R1, class _F>
1213 friend future<_R1> __make_async_assoc_state(_F __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001214#endif
1215
Howard Hinnant47499b12010-08-27 20:10:19 +00001216public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001217 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001218 future() : __state_(nullptr) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001219#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001220 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001221 future(future&& __rhs)
1222 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1223 future(const future&) = delete;
1224 future& operator=(const future&) = delete;
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001225 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001226 future& operator=(future&& __rhs)
1227 {
1228 future(std::move(__rhs)).swap(*this);
1229 return *this;
1230 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001231#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001232private:
1233 future(const future&);
1234 future& operator=(const future&);
1235public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001236#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001237 ~future();
Howard Hinnant7de47902010-11-30 20:23:32 +00001238 shared_future<void> share();
Howard Hinnant47499b12010-08-27 20:10:19 +00001239
1240 // retrieving the value
1241 void get();
1242
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001243 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0949eed2011-06-30 21:18:19 +00001244 void swap(future& __rhs) {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:19 +00001245
1246 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001247 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001248 bool valid() const {return __state_ != nullptr;}
1249
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001250 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001251 void wait() const {__state_->wait();}
1252 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001253 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001254 future_status
1255 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1256 {return __state_->wait_for(__rel_time);}
1257 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001258 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001259 future_status
1260 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1261 {return __state_->wait_until(__abs_time);}
1262};
1263
Howard Hinnant99be8232010-09-03 18:39:25 +00001264template <class _R>
1265inline _LIBCPP_INLINE_VISIBILITY
1266void
1267swap(future<_R>& __x, future<_R>& __y)
1268{
1269 __x.swap(__y);
1270}
1271
Howard Hinnant47499b12010-08-27 20:10:19 +00001272// promise<R>
1273
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00001274template <class _Callable> class packaged_task;
Howard Hinnant54da3382010-08-30 18:46:21 +00001275
Howard Hinnant47499b12010-08-27 20:10:19 +00001276template <class _R>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001277class _LIBCPP_VISIBLE promise
Howard Hinnant47499b12010-08-27 20:10:19 +00001278{
1279 __assoc_state<_R>* __state_;
Howard Hinnant54da3382010-08-30 18:46:21 +00001280
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001281 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001282 explicit promise(nullptr_t) : __state_(nullptr) {}
1283
1284 template <class> friend class packaged_task;
Howard Hinnant47499b12010-08-27 20:10:19 +00001285public:
1286 promise();
1287 template <class _Alloc>
1288 promise(allocator_arg_t, const _Alloc& __a);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001289#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001290 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001291 promise(promise&& __rhs)
1292 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1293 promise(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +00001294#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001295private:
1296 promise(const promise& __rhs);
1297public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001298#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001299 ~promise();
1300
1301 // assignment
Howard Hinnant73d21a42010-09-04 23:28:19 +00001302#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001303 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001304 promise& operator=(promise&& __rhs)
1305 {
1306 promise(std::move(__rhs)).swap(*this);
1307 return *this;
1308 }
1309 promise& operator=(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +00001310#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001311private:
1312 promise& operator=(const promise& __rhs);
1313public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001314#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001315 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0949eed2011-06-30 21:18:19 +00001316 void swap(promise& __rhs) {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:19 +00001317
1318 // retrieving the result
1319 future<_R> get_future();
1320
1321 // setting the result
1322 void set_value(const _R& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001323#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001324 void set_value(_R&& __r);
1325#endif
1326 void set_exception(exception_ptr __p);
1327
1328 // setting the result with deferred notification
1329 void set_value_at_thread_exit(const _R& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001330#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001331 void set_value_at_thread_exit(_R&& __r);
1332#endif
1333 void set_exception_at_thread_exit(exception_ptr __p);
1334};
1335
1336template <class _R>
1337promise<_R>::promise()
1338 : __state_(new __assoc_state<_R>)
1339{
1340}
1341
1342template <class _R>
1343template <class _Alloc>
1344promise<_R>::promise(allocator_arg_t, const _Alloc& __a0)
1345{
1346 typedef typename _Alloc::template rebind<__assoc_state_alloc<_R, _Alloc> >::other _A2;
1347 typedef __allocator_destructor<_A2> _D2;
1348 _A2 __a(__a0);
1349 unique_ptr<__assoc_state_alloc<_R, _Alloc>, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1350 ::new(__hold.get()) __assoc_state_alloc<_R, _Alloc>(__a0);
1351 __state_ = __hold.release();
1352}
1353
1354template <class _R>
1355promise<_R>::~promise()
1356{
1357 if (__state_)
1358 {
1359 if (!__state_->__has_value() && __state_->use_count() > 1)
1360 __state_->set_exception(make_exception_ptr(
1361 future_error(make_error_code(future_errc::broken_promise))
1362 ));
1363 __state_->__release_shared();
1364 }
1365}
1366
1367template <class _R>
1368future<_R>
1369promise<_R>::get_future()
1370{
1371 if (__state_ == nullptr)
1372 throw future_error(make_error_code(future_errc::no_state));
1373 return future<_R>(__state_);
1374}
1375
1376template <class _R>
1377void
1378promise<_R>::set_value(const _R& __r)
1379{
1380 if (__state_ == nullptr)
1381 throw future_error(make_error_code(future_errc::no_state));
1382 __state_->set_value(__r);
1383}
1384
Howard Hinnant73d21a42010-09-04 23:28:19 +00001385#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001386
1387template <class _R>
1388void
1389promise<_R>::set_value(_R&& __r)
1390{
1391 if (__state_ == nullptr)
1392 throw future_error(make_error_code(future_errc::no_state));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001393 __state_->set_value(_VSTD::move(__r));
Howard Hinnant47499b12010-08-27 20:10:19 +00001394}
1395
Howard Hinnant73d21a42010-09-04 23:28:19 +00001396#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001397
1398template <class _R>
1399void
1400promise<_R>::set_exception(exception_ptr __p)
1401{
1402 if (__state_ == nullptr)
1403 throw future_error(make_error_code(future_errc::no_state));
1404 __state_->set_exception(__p);
1405}
1406
1407template <class _R>
1408void
1409promise<_R>::set_value_at_thread_exit(const _R& __r)
1410{
1411 if (__state_ == nullptr)
1412 throw future_error(make_error_code(future_errc::no_state));
1413 __state_->set_value_at_thread_exit(__r);
1414}
1415
Howard Hinnant73d21a42010-09-04 23:28:19 +00001416#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001417
1418template <class _R>
1419void
1420promise<_R>::set_value_at_thread_exit(_R&& __r)
1421{
1422 if (__state_ == nullptr)
1423 throw future_error(make_error_code(future_errc::no_state));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001424 __state_->set_value_at_thread_exit(_VSTD::move(__r));
Howard Hinnant47499b12010-08-27 20:10:19 +00001425}
1426
Howard Hinnant73d21a42010-09-04 23:28:19 +00001427#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001428
1429template <class _R>
1430void
1431promise<_R>::set_exception_at_thread_exit(exception_ptr __p)
1432{
1433 if (__state_ == nullptr)
1434 throw future_error(make_error_code(future_errc::no_state));
1435 __state_->set_exception_at_thread_exit(__p);
1436}
1437
1438// promise<R&>
1439
1440template <class _R>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001441class _LIBCPP_VISIBLE promise<_R&>
Howard Hinnant47499b12010-08-27 20:10:19 +00001442{
1443 __assoc_state<_R&>* __state_;
Howard Hinnant54da3382010-08-30 18:46:21 +00001444
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001445 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001446 explicit promise(nullptr_t) : __state_(nullptr) {}
1447
1448 template <class> friend class packaged_task;
1449
Howard Hinnant47499b12010-08-27 20:10:19 +00001450public:
1451 promise();
1452 template <class _Allocator>
1453 promise(allocator_arg_t, const _Allocator& __a);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001454#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001455 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001456 promise(promise&& __rhs)
1457 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1458 promise(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +00001459#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001460private:
1461 promise(const promise& __rhs);
1462public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001463#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001464 ~promise();
1465
1466 // assignment
Howard Hinnant73d21a42010-09-04 23:28:19 +00001467#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001468 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001469 promise& operator=(promise&& __rhs)
1470 {
1471 promise(std::move(__rhs)).swap(*this);
1472 return *this;
1473 }
1474 promise& operator=(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +00001475#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001476private:
1477 promise& operator=(const promise& __rhs);
1478public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001479#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001480 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0949eed2011-06-30 21:18:19 +00001481 void swap(promise& __rhs) {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:19 +00001482
1483 // retrieving the result
1484 future<_R&> get_future();
1485
1486 // setting the result
1487 void set_value(_R& __r);
1488 void set_exception(exception_ptr __p);
1489
1490 // setting the result with deferred notification
1491 void set_value_at_thread_exit(_R&);
1492 void set_exception_at_thread_exit(exception_ptr __p);
1493};
1494
1495template <class _R>
1496promise<_R&>::promise()
1497 : __state_(new __assoc_state<_R&>)
1498{
1499}
1500
1501template <class _R>
1502template <class _Alloc>
1503promise<_R&>::promise(allocator_arg_t, const _Alloc& __a0)
1504{
1505 typedef typename _Alloc::template rebind<__assoc_state_alloc<_R&, _Alloc> >::other _A2;
1506 typedef __allocator_destructor<_A2> _D2;
1507 _A2 __a(__a0);
1508 unique_ptr<__assoc_state_alloc<_R&, _Alloc>, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1509 ::new(__hold.get()) __assoc_state_alloc<_R&, _Alloc>(__a0);
1510 __state_ = __hold.release();
1511}
1512
1513template <class _R>
1514promise<_R&>::~promise()
1515{
1516 if (__state_)
1517 {
1518 if (!__state_->__has_value() && __state_->use_count() > 1)
1519 __state_->set_exception(make_exception_ptr(
1520 future_error(make_error_code(future_errc::broken_promise))
1521 ));
1522 __state_->__release_shared();
1523 }
1524}
1525
1526template <class _R>
1527future<_R&>
1528promise<_R&>::get_future()
1529{
1530 if (__state_ == nullptr)
1531 throw future_error(make_error_code(future_errc::no_state));
1532 return future<_R&>(__state_);
1533}
1534
1535template <class _R>
1536void
1537promise<_R&>::set_value(_R& __r)
1538{
1539 if (__state_ == nullptr)
1540 throw future_error(make_error_code(future_errc::no_state));
1541 __state_->set_value(__r);
1542}
1543
1544template <class _R>
1545void
1546promise<_R&>::set_exception(exception_ptr __p)
1547{
1548 if (__state_ == nullptr)
1549 throw future_error(make_error_code(future_errc::no_state));
1550 __state_->set_exception(__p);
1551}
1552
1553template <class _R>
1554void
1555promise<_R&>::set_value_at_thread_exit(_R& __r)
1556{
1557 if (__state_ == nullptr)
1558 throw future_error(make_error_code(future_errc::no_state));
1559 __state_->set_value_at_thread_exit(__r);
1560}
1561
1562template <class _R>
1563void
1564promise<_R&>::set_exception_at_thread_exit(exception_ptr __p)
1565{
1566 if (__state_ == nullptr)
1567 throw future_error(make_error_code(future_errc::no_state));
1568 __state_->set_exception_at_thread_exit(__p);
1569}
1570
1571// promise<void>
1572
1573template <>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001574class _LIBCPP_VISIBLE promise<void>
Howard Hinnant47499b12010-08-27 20:10:19 +00001575{
1576 __assoc_sub_state* __state_;
Howard Hinnant54da3382010-08-30 18:46:21 +00001577
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001578 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001579 explicit promise(nullptr_t) : __state_(nullptr) {}
1580
1581 template <class> friend class packaged_task;
1582
Howard Hinnant47499b12010-08-27 20:10:19 +00001583public:
1584 promise();
1585 template <class _Allocator>
1586 promise(allocator_arg_t, const _Allocator& __a);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001587#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001588 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001589 promise(promise&& __rhs)
1590 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1591 promise(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +00001592#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001593private:
1594 promise(const promise& __rhs);
1595public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001596#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001597 ~promise();
1598
1599 // assignment
Howard Hinnant73d21a42010-09-04 23:28:19 +00001600#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001601 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001602 promise& operator=(promise&& __rhs)
1603 {
1604 promise(std::move(__rhs)).swap(*this);
1605 return *this;
1606 }
1607 promise& operator=(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +00001608#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001609private:
1610 promise& operator=(const promise& __rhs);
1611public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001612#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001613 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0949eed2011-06-30 21:18:19 +00001614 void swap(promise& __rhs) {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:19 +00001615
1616 // retrieving the result
1617 future<void> get_future();
1618
1619 // setting the result
1620 void set_value();
1621 void set_exception(exception_ptr __p);
1622
1623 // setting the result with deferred notification
1624 void set_value_at_thread_exit();
1625 void set_exception_at_thread_exit(exception_ptr __p);
1626};
1627
1628template <class _Alloc>
1629promise<void>::promise(allocator_arg_t, const _Alloc& __a0)
1630{
1631 typedef typename _Alloc::template rebind<__assoc_sub_state_alloc<_Alloc> >::other _A2;
1632 typedef __allocator_destructor<_A2> _D2;
1633 _A2 __a(__a0);
1634 unique_ptr<__assoc_sub_state_alloc<_Alloc>, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1635 ::new(__hold.get()) __assoc_sub_state_alloc<_Alloc>(__a0);
1636 __state_ = __hold.release();
1637}
1638
1639template <class _R>
1640inline _LIBCPP_INLINE_VISIBILITY
1641void
1642swap(promise<_R>& __x, promise<_R>& __y)
1643{
1644 __x.swap(__y);
1645}
1646
1647template <class _R, class _Alloc>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001648 struct _LIBCPP_VISIBLE uses_allocator<promise<_R>, _Alloc>
1649 : public true_type {};
Howard Hinnant47499b12010-08-27 20:10:19 +00001650
Howard Hinnant54da3382010-08-30 18:46:21 +00001651#ifndef _LIBCPP_HAS_NO_VARIADICS
1652
1653// packaged_task
1654
1655template<class _Fp> class __packaged_task_base;
1656
1657template<class _R, class ..._ArgTypes>
1658class __packaged_task_base<_R(_ArgTypes...)>
1659{
1660 __packaged_task_base(const __packaged_task_base&);
1661 __packaged_task_base& operator=(const __packaged_task_base&);
1662public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001663 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001664 __packaged_task_base() {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001665 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001666 virtual ~__packaged_task_base() {}
1667 virtual void __move_to(__packaged_task_base*) = 0;
1668 virtual void destroy() = 0;
1669 virtual void destroy_deallocate() = 0;
1670 virtual _R operator()(_ArgTypes&& ...) = 0;
1671};
1672
1673template<class _FD, class _Alloc, class _FB> class __packaged_task_func;
1674
1675template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1676class __packaged_task_func<_F, _Alloc, _R(_ArgTypes...)>
1677 : public __packaged_task_base<_R(_ArgTypes...)>
1678{
1679 __compressed_pair<_F, _Alloc> __f_;
1680public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001681 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001682 explicit __packaged_task_func(const _F& __f) : __f_(__f) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001683 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0949eed2011-06-30 21:18:19 +00001684 explicit __packaged_task_func(_F&& __f) : __f_(_VSTD::move(__f)) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001685 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001686 __packaged_task_func(const _F& __f, const _Alloc& __a)
1687 : __f_(__f, __a) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001688 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001689 __packaged_task_func(_F&& __f, const _Alloc& __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001690 : __f_(_VSTD::move(__f), __a) {}
Howard Hinnant54da3382010-08-30 18:46:21 +00001691 virtual void __move_to(__packaged_task_base<_R(_ArgTypes...)>*);
1692 virtual void destroy();
1693 virtual void destroy_deallocate();
1694 virtual _R operator()(_ArgTypes&& ... __args);
1695};
1696
1697template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1698void
1699__packaged_task_func<_F, _Alloc, _R(_ArgTypes...)>::__move_to(
1700 __packaged_task_base<_R(_ArgTypes...)>* __p)
1701{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001702 ::new (__p) __packaged_task_func(_VSTD::move(__f_.first()), _VSTD::move(__f_.second()));
Howard Hinnant54da3382010-08-30 18:46:21 +00001703}
1704
1705template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1706void
1707__packaged_task_func<_F, _Alloc, _R(_ArgTypes...)>::destroy()
1708{
1709 __f_.~__compressed_pair<_F, _Alloc>();
1710}
1711
1712template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1713void
1714__packaged_task_func<_F, _Alloc, _R(_ArgTypes...)>::destroy_deallocate()
1715{
1716 typedef typename _Alloc::template rebind<__packaged_task_func>::other _A;
1717 _A __a(__f_.second());
1718 __f_.~__compressed_pair<_F, _Alloc>();
1719 __a.deallocate(this, 1);
1720}
1721
1722template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1723_R
1724__packaged_task_func<_F, _Alloc, _R(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
1725{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001726 return __invoke(__f_.first(), _VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnant54da3382010-08-30 18:46:21 +00001727}
1728
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00001729template <class _Callable> class __packaged_task_function;
Howard Hinnant54da3382010-08-30 18:46:21 +00001730
1731template<class _R, class ..._ArgTypes>
1732class __packaged_task_function<_R(_ArgTypes...)>
1733{
1734 typedef __packaged_task_base<_R(_ArgTypes...)> __base;
1735 aligned_storage<3*sizeof(void*)>::type __buf_;
1736 __base* __f_;
1737
1738public:
1739 typedef _R result_type;
1740
1741 // construct/copy/destroy:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001742 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001743 __packaged_task_function() : __f_(nullptr) {}
1744 template<class _F>
1745 __packaged_task_function(_F&& __f);
1746 template<class _F, class _Alloc>
1747 __packaged_task_function(allocator_arg_t, const _Alloc& __a, _F&& __f);
1748
1749 __packaged_task_function(__packaged_task_function&&);
1750 __packaged_task_function& operator=(__packaged_task_function&&);
1751
1752 __packaged_task_function(const __packaged_task_function&) = delete;
1753 __packaged_task_function& operator=(const __packaged_task_function&) = delete;
1754
1755 ~__packaged_task_function();
1756
1757 void swap(__packaged_task_function&);
1758
1759 _R operator()(_ArgTypes...) const;
1760};
1761
1762template<class _R, class ..._ArgTypes>
1763__packaged_task_function<_R(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f)
1764{
1765 if (__f.__f_ == nullptr)
1766 __f_ = nullptr;
1767 else if (__f.__f_ == (__base*)&__f.__buf_)
1768 {
1769 __f_ = (__base*)&__buf_;
1770 __f.__f_->__move_to(__f_);
1771 }
1772 else
1773 {
1774 __f_ = __f.__f_;
1775 __f.__f_ = nullptr;
1776 }
1777}
1778
1779template<class _R, class ..._ArgTypes>
1780template <class _F>
1781__packaged_task_function<_R(_ArgTypes...)>::__packaged_task_function(_F&& __f)
1782 : __f_(nullptr)
1783{
1784 typedef typename remove_reference<_F>::type _FR;
1785 typedef __packaged_task_func<_FR, allocator<_FR>, _R(_ArgTypes...)> _FF;
1786 if (sizeof(_FF) <= sizeof(__buf_))
1787 {
1788 __f_ = (__base*)&__buf_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001789 ::new (__f_) _FF(_VSTD::forward<_F>(__f));
Howard Hinnant54da3382010-08-30 18:46:21 +00001790 }
1791 else
1792 {
1793 typedef allocator<_FF> _A;
1794 _A __a;
1795 typedef __allocator_destructor<_A> _D;
1796 unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001797 ::new (__hold.get()) _FF(_VSTD::forward<_F>(__f), allocator<_FR>(__a));
Howard Hinnant54da3382010-08-30 18:46:21 +00001798 __f_ = __hold.release();
1799 }
1800}
1801
1802template<class _R, class ..._ArgTypes>
1803template <class _F, class _Alloc>
1804__packaged_task_function<_R(_ArgTypes...)>::__packaged_task_function(
1805 allocator_arg_t, const _Alloc& __a0, _F&& __f)
1806 : __f_(nullptr)
1807{
1808 typedef allocator_traits<_Alloc> __alloc_traits;
1809 typedef typename remove_reference<_F>::type _FR;
1810 typedef __packaged_task_func<_FR, _Alloc, _R(_ArgTypes...)> _FF;
1811 if (sizeof(_FF) <= sizeof(__buf_))
1812 {
1813 __f_ = (__base*)&__buf_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001814 ::new (__f_) _FF(_VSTD::forward<_F>(__f));
Howard Hinnant54da3382010-08-30 18:46:21 +00001815 }
1816 else
1817 {
1818 typedef typename __alloc_traits::template
1819#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1820 rebind_alloc<_FF>
1821#else
1822 rebind_alloc<_FF>::other
1823#endif
1824 _A;
1825 _A __a(__a0);
1826 typedef __allocator_destructor<_A> _D;
1827 unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001828 ::new (__hold.get()) _FF(_VSTD::forward<_F>(__f), _Alloc(__a));
Howard Hinnant54da3382010-08-30 18:46:21 +00001829 __f_ = __hold.release();
1830 }
1831}
1832
1833template<class _R, class ..._ArgTypes>
1834__packaged_task_function<_R(_ArgTypes...)>&
1835__packaged_task_function<_R(_ArgTypes...)>::operator=(__packaged_task_function&& __f)
1836{
1837 if (__f_ == (__base*)&__buf_)
1838 __f_->destroy();
1839 else if (__f_)
1840 __f_->destroy_deallocate();
1841 __f_ = nullptr;
1842 if (__f.__f_ == nullptr)
1843 __f_ = nullptr;
1844 else if (__f.__f_ == (__base*)&__f.__buf_)
1845 {
1846 __f_ = (__base*)&__buf_;
1847 __f.__f_->__move_to(__f_);
1848 }
1849 else
1850 {
1851 __f_ = __f.__f_;
1852 __f.__f_ = nullptr;
1853 }
1854}
1855
1856template<class _R, class ..._ArgTypes>
1857__packaged_task_function<_R(_ArgTypes...)>::~__packaged_task_function()
1858{
1859 if (__f_ == (__base*)&__buf_)
1860 __f_->destroy();
1861 else if (__f_)
1862 __f_->destroy_deallocate();
1863}
1864
1865template<class _R, class ..._ArgTypes>
1866void
1867__packaged_task_function<_R(_ArgTypes...)>::swap(__packaged_task_function& __f)
1868{
1869 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1870 {
1871 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1872 __base* __t = (__base*)&__tempbuf;
1873 __f_->__move_to(__t);
1874 __f_->destroy();
1875 __f_ = nullptr;
1876 __f.__f_->__move_to((__base*)&__buf_);
1877 __f.__f_->destroy();
1878 __f.__f_ = nullptr;
1879 __f_ = (__base*)&__buf_;
1880 __t->__move_to((__base*)&__f.__buf_);
1881 __t->destroy();
1882 __f.__f_ = (__base*)&__f.__buf_;
1883 }
1884 else if (__f_ == (__base*)&__buf_)
1885 {
1886 __f_->__move_to((__base*)&__f.__buf_);
1887 __f_->destroy();
1888 __f_ = __f.__f_;
1889 __f.__f_ = (__base*)&__f.__buf_;
1890 }
1891 else if (__f.__f_ == (__base*)&__f.__buf_)
1892 {
1893 __f.__f_->__move_to((__base*)&__buf_);
1894 __f.__f_->destroy();
1895 __f.__f_ = __f_;
1896 __f_ = (__base*)&__buf_;
1897 }
1898 else
Howard Hinnant0949eed2011-06-30 21:18:19 +00001899 _VSTD::swap(__f_, __f.__f_);
Howard Hinnant54da3382010-08-30 18:46:21 +00001900}
1901
1902template<class _R, class ..._ArgTypes>
1903inline _LIBCPP_INLINE_VISIBILITY
1904_R
1905__packaged_task_function<_R(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
1906{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001907 return (*__f_)(_VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnant54da3382010-08-30 18:46:21 +00001908}
1909
1910template<class _R, class ..._ArgTypes>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001911class _LIBCPP_VISIBLE packaged_task<_R(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:21 +00001912{
1913public:
1914 typedef _R result_type;
1915
1916private:
1917 __packaged_task_function<result_type(_ArgTypes...)> __f_;
1918 promise<result_type> __p_;
1919
1920public:
1921 // construction and destruction
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001922 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001923 packaged_task() : __p_(nullptr) {}
1924 template <class _F>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001925 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0949eed2011-06-30 21:18:19 +00001926 explicit packaged_task(_F&& __f) : __f_(_VSTD::forward<_F>(__f)) {}
Howard Hinnant54da3382010-08-30 18:46:21 +00001927 template <class _F, class _Allocator>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001928 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001929 explicit packaged_task(allocator_arg_t, const _Allocator& __a, _F&& __f)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001930 : __f_(allocator_arg, __a, _VSTD::forward<_F>(__f)),
Howard Hinnant54da3382010-08-30 18:46:21 +00001931 __p_(allocator_arg, __a) {}
1932 // ~packaged_task() = default;
1933
1934 // no copy
1935 packaged_task(packaged_task&) = delete;
1936 packaged_task& operator=(packaged_task&) = delete;
1937
1938 // move support
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001939 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001940 packaged_task(packaged_task&& __other)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001941 : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001942 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001943 packaged_task& operator=(packaged_task&& __other)
1944 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001945 __f_ = _VSTD::move(__other.__f_);
1946 __p_ = _VSTD::move(__other.__p_);
Howard Hinnant54da3382010-08-30 18:46:21 +00001947 return *this;
1948 }
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001949 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001950 void swap(packaged_task& __other)
1951 {
1952 __f_.swap(__other.__f_);
1953 __p_.swap(__other.__p_);
1954 }
1955
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001956 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7de47902010-11-30 20:23:32 +00001957 bool valid() const {return __p_.__state_ != nullptr;}
Howard Hinnant54da3382010-08-30 18:46:21 +00001958
1959 // result retrieval
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001960 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001961 future<result_type> get_future() {return __p_.get_future();}
1962
1963 // execution
1964 void operator()(_ArgTypes... __args);
1965 void make_ready_at_thread_exit(_ArgTypes... __args);
1966
1967 void reset();
1968};
1969
1970template<class _R, class ..._ArgTypes>
1971void
1972packaged_task<_R(_ArgTypes...)>::operator()(_ArgTypes... __args)
1973{
1974#ifndef _LIBCPP_NO_EXCEPTIONS
1975 if (__p_.__state_ == nullptr)
1976 throw future_error(make_error_code(future_errc::no_state));
1977 if (__p_.__state_->__has_value())
1978 throw future_error(make_error_code(future_errc::promise_already_satisfied));
1979 try
1980 {
1981#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:19 +00001982 __p_.set_value(__f_(_VSTD::forward<_ArgTypes>(__args)...));
Howard Hinnant54da3382010-08-30 18:46:21 +00001983#ifndef _LIBCPP_NO_EXCEPTIONS
1984 }
1985 catch (...)
1986 {
1987 __p_.set_exception(current_exception());
1988 }
1989#endif // _LIBCPP_NO_EXCEPTIONS
1990}
1991
1992template<class _R, class ..._ArgTypes>
1993void
1994packaged_task<_R(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
1995{
1996#ifndef _LIBCPP_NO_EXCEPTIONS
1997 if (__p_.__state_ == nullptr)
1998 throw future_error(make_error_code(future_errc::no_state));
1999 if (__p_.__state_->__has_value())
2000 throw future_error(make_error_code(future_errc::promise_already_satisfied));
2001 try
2002 {
2003#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:19 +00002004 __p_.set_value_at_thread_exit(__f_(_VSTD::forward<_ArgTypes>(__args)...));
Howard Hinnant54da3382010-08-30 18:46:21 +00002005#ifndef _LIBCPP_NO_EXCEPTIONS
2006 }
2007 catch (...)
2008 {
2009 __p_.set_exception_at_thread_exit(current_exception());
2010 }
2011#endif // _LIBCPP_NO_EXCEPTIONS
2012}
2013
2014template<class _R, class ..._ArgTypes>
2015void
2016packaged_task<_R(_ArgTypes...)>::reset()
2017{
2018#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant7de47902010-11-30 20:23:32 +00002019 if (!valid())
Howard Hinnant54da3382010-08-30 18:46:21 +00002020 throw future_error(make_error_code(future_errc::no_state));
2021#endif // _LIBCPP_NO_EXCEPTIONS
2022 __p_ = promise<result_type>();
2023}
2024
2025template<class ..._ArgTypes>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002026class _LIBCPP_VISIBLE packaged_task<void(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:21 +00002027{
2028public:
2029 typedef void result_type;
2030
2031private:
2032 __packaged_task_function<result_type(_ArgTypes...)> __f_;
2033 promise<result_type> __p_;
2034
2035public:
2036 // construction and destruction
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002037 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00002038 packaged_task() : __p_(nullptr) {}
2039 template <class _F>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002040 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0949eed2011-06-30 21:18:19 +00002041 explicit packaged_task(_F&& __f) : __f_(_VSTD::forward<_F>(__f)) {}
Howard Hinnant54da3382010-08-30 18:46:21 +00002042 template <class _F, class _Allocator>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002043 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00002044 explicit packaged_task(allocator_arg_t, const _Allocator& __a, _F&& __f)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002045 : __f_(allocator_arg, __a, _VSTD::forward<_F>(__f)),
Howard Hinnant54da3382010-08-30 18:46:21 +00002046 __p_(allocator_arg, __a) {}
2047 // ~packaged_task() = default;
2048
2049 // no copy
2050 packaged_task(packaged_task&) = delete;
2051 packaged_task& operator=(packaged_task&) = delete;
2052
2053 // move support
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002054 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00002055 packaged_task(packaged_task&& __other)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002056 : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002057 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00002058 packaged_task& operator=(packaged_task&& __other)
2059 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002060 __f_ = _VSTD::move(__other.__f_);
2061 __p_ = _VSTD::move(__other.__p_);
Howard Hinnant54da3382010-08-30 18:46:21 +00002062 return *this;
2063 }
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002064 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00002065 void swap(packaged_task& __other)
2066 {
2067 __f_.swap(__other.__f_);
2068 __p_.swap(__other.__p_);
2069 }
2070
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002071 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7de47902010-11-30 20:23:32 +00002072 bool valid() const {return __p_.__state_ != nullptr;}
Howard Hinnant54da3382010-08-30 18:46:21 +00002073
2074 // result retrieval
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002075 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00002076 future<result_type> get_future() {return __p_.get_future();}
2077
2078 // execution
2079 void operator()(_ArgTypes... __args);
2080 void make_ready_at_thread_exit(_ArgTypes... __args);
2081
2082 void reset();
2083};
2084
2085template<class ..._ArgTypes>
2086void
2087packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args)
2088{
2089#ifndef _LIBCPP_NO_EXCEPTIONS
2090 if (__p_.__state_ == nullptr)
2091 throw future_error(make_error_code(future_errc::no_state));
2092 if (__p_.__state_->__has_value())
2093 throw future_error(make_error_code(future_errc::promise_already_satisfied));
2094 try
2095 {
2096#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:19 +00002097 __f_(_VSTD::forward<_ArgTypes>(__args)...);
Howard Hinnant54da3382010-08-30 18:46:21 +00002098 __p_.set_value();
2099#ifndef _LIBCPP_NO_EXCEPTIONS
2100 }
2101 catch (...)
2102 {
2103 __p_.set_exception(current_exception());
2104 }
2105#endif // _LIBCPP_NO_EXCEPTIONS
2106}
2107
2108template<class ..._ArgTypes>
2109void
2110packaged_task<void(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
2111{
2112#ifndef _LIBCPP_NO_EXCEPTIONS
2113 if (__p_.__state_ == nullptr)
2114 throw future_error(make_error_code(future_errc::no_state));
2115 if (__p_.__state_->__has_value())
2116 throw future_error(make_error_code(future_errc::promise_already_satisfied));
2117 try
2118 {
2119#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:19 +00002120 __f_(_VSTD::forward<_ArgTypes>(__args)...);
Howard Hinnant54da3382010-08-30 18:46:21 +00002121 __p_.set_value_at_thread_exit();
2122#ifndef _LIBCPP_NO_EXCEPTIONS
2123 }
2124 catch (...)
2125 {
2126 __p_.set_exception_at_thread_exit(current_exception());
2127 }
2128#endif // _LIBCPP_NO_EXCEPTIONS
2129}
2130
2131template<class ..._ArgTypes>
2132void
2133packaged_task<void(_ArgTypes...)>::reset()
2134{
2135#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant7de47902010-11-30 20:23:32 +00002136 if (!valid())
Howard Hinnant54da3382010-08-30 18:46:21 +00002137 throw future_error(make_error_code(future_errc::no_state));
2138#endif // _LIBCPP_NO_EXCEPTIONS
2139 __p_ = promise<result_type>();
2140}
2141
2142template <class _Callable>
2143inline _LIBCPP_INLINE_VISIBILITY
2144void
2145swap(packaged_task<_Callable>& __x, packaged_task<_Callable>& __y)
2146{
2147 __x.swap(__y);
2148}
2149
2150template <class _Callable, class _Alloc>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002151struct _LIBCPP_VISIBLE uses_allocator<packaged_task<_Callable>, _Alloc>
2152 : public true_type {};
Howard Hinnant54da3382010-08-30 18:46:21 +00002153
2154template <class _R, class _F>
2155future<_R>
Howard Hinnant73d21a42010-09-04 23:28:19 +00002156#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21 +00002157__make_deferred_assoc_state(_F&& __f)
2158#else
2159__make_deferred_assoc_state(_F __f)
2160#endif
2161{
2162 unique_ptr<__deferred_assoc_state<_R, _F>, __release_shared_count>
Howard Hinnant0949eed2011-06-30 21:18:19 +00002163 __h(new __deferred_assoc_state<_R, _F>(_VSTD::forward<_F>(__f)));
Howard Hinnant54da3382010-08-30 18:46:21 +00002164 return future<_R>(__h.get());
2165}
2166
Howard Hinnant57cff292011-05-19 15:05:04 +00002167template <class _R, class _F>
2168future<_R>
2169#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2170__make_async_assoc_state(_F&& __f)
2171#else
2172__make_async_assoc_state(_F __f)
2173#endif
2174{
2175 unique_ptr<__async_assoc_state<_R, _F>, __release_shared_count>
Howard Hinnant0949eed2011-06-30 21:18:19 +00002176 __h(new __async_assoc_state<_R, _F>(_VSTD::forward<_F>(__f)));
2177 _VSTD::thread(&__async_assoc_state<_R, _F>::__execute, __h.get()).detach();
Howard Hinnant57cff292011-05-19 15:05:04 +00002178 return future<_R>(__h.get());
2179}
2180
Howard Hinnant54da3382010-08-30 18:46:21 +00002181template <class _F, class... _Args>
Howard Hinnant57cff292011-05-19 15:05:04 +00002182class __async_func
2183{
2184 tuple<_F, _Args...> __f_;
2185
2186public:
2187 typedef typename __invoke_of<_F, _Args...>::type _R;
2188
2189 _LIBCPP_INLINE_VISIBILITY
2190 explicit __async_func(_F&& __f, _Args&&... __args)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002191 : __f_(_VSTD::move(__f), _VSTD::move(__args)...) {}
Howard Hinnant57cff292011-05-19 15:05:04 +00002192
2193 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0949eed2011-06-30 21:18:19 +00002194 __async_func(__async_func&& __f) : __f_(_VSTD::move(__f.__f_)) {}
Howard Hinnant57cff292011-05-19 15:05:04 +00002195
2196 _R operator()()
2197 {
2198 typedef typename __make_tuple_indices<1+sizeof...(_Args), 1>::type _Index;
2199 return __execute(_Index());
2200 }
2201private:
2202 template <size_t ..._Indices>
2203 _R
2204 __execute(__tuple_indices<_Indices...>)
2205 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002206 return __invoke(_VSTD::move(_VSTD::get<0>(__f_)), _VSTD::move(_VSTD::get<_Indices>(__f_))...);
Howard Hinnant57cff292011-05-19 15:05:04 +00002207 }
2208};
2209
2210template <class _F, class... _Args>
2211future<typename __invoke_of<typename decay<_F>::type, typename decay<_Args>::type...>::type>
Howard Hinnant54da3382010-08-30 18:46:21 +00002212async(launch __policy, _F&& __f, _Args&&... __args)
2213{
Howard Hinnant57cff292011-05-19 15:05:04 +00002214 typedef __async_func<typename decay<_F>::type, typename decay<_Args>::type...> _BF;
2215 typedef typename _BF::_R _R;
Howard Hinnant54da3382010-08-30 18:46:21 +00002216 future<_R> __r;
Howard Hinnant66895642010-11-23 18:33:54 +00002217 if (__policy & launch::async)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002218 __r = _VSTD::__make_async_assoc_state<_R>(_BF(__decay_copy(_VSTD::forward<_F>(__f)),
2219 __decay_copy(_VSTD::forward<_Args>(__args))...));
Howard Hinnant66895642010-11-23 18:33:54 +00002220 else if (__policy & launch::deferred)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002221 __r = _VSTD::__make_deferred_assoc_state<_R>(_BF(__decay_copy(_VSTD::forward<_F>(__f)),
2222 __decay_copy(_VSTD::forward<_Args>(__args))...));
Howard Hinnant54da3382010-08-30 18:46:21 +00002223 return __r;
2224}
2225
2226template <class _F, class... _Args>
2227inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57cff292011-05-19 15:05:04 +00002228future<typename __invoke_of<typename decay<_F>::type, typename decay<_Args>::type...>::type>
Howard Hinnant54da3382010-08-30 18:46:21 +00002229async(_F&& __f, _Args&&... __args)
2230{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002231 return _VSTD::async(launch::any, _VSTD::forward<_F>(__f),
2232 _VSTD::forward<_Args>(__args)...);
Howard Hinnant54da3382010-08-30 18:46:21 +00002233}
2234
2235#endif // _LIBCPP_HAS_NO_VARIADICS
2236
Howard Hinnante6e4d012010-09-03 21:46:37 +00002237// shared_future
2238
Howard Hinnant99be8232010-09-03 18:39:25 +00002239template <class _R>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002240class _LIBCPP_VISIBLE shared_future
Howard Hinnant99be8232010-09-03 18:39:25 +00002241{
2242 __assoc_state<_R>* __state_;
2243
2244public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002245 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002246 shared_future() : __state_(nullptr) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002247 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002248 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2249 {if (__state_) __state_->__add_shared();}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002250#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002251 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002252 shared_future(future<_R>&& __f) : __state_(__f.__state_)
2253 {__f.__state_ = nullptr;}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002254 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002255 shared_future(shared_future&& __rhs) : __state_(__rhs.__state_)
2256 {__rhs.__state_ = nullptr;}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002257#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:25 +00002258 ~shared_future();
2259 shared_future& operator=(const shared_future& __rhs);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002260#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002261 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002262 shared_future& operator=(shared_future&& __rhs)
2263 {
2264 shared_future(std::move(__rhs)).swap(*this);
2265 return *this;
2266 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002267#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:25 +00002268
2269 // retrieving the value
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002270 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002271 const _R& get() const {return __state_->copy();}
2272
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002273 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0949eed2011-06-30 21:18:19 +00002274 void swap(shared_future& __rhs) {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant99be8232010-09-03 18:39:25 +00002275
2276 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002277 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002278 bool valid() const {return __state_ != nullptr;}
2279
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002280 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002281 void wait() const {__state_->wait();}
2282 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002283 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002284 future_status
2285 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2286 {return __state_->wait_for(__rel_time);}
2287 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002288 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002289 future_status
2290 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2291 {return __state_->wait_until(__abs_time);}
2292};
2293
2294template <class _R>
2295shared_future<_R>::~shared_future()
2296{
2297 if (__state_)
2298 __state_->__release_shared();
2299}
2300
2301template <class _R>
2302shared_future<_R>&
2303shared_future<_R>::operator=(const shared_future& __rhs)
2304{
2305 if (__rhs.__state_)
2306 __rhs.__state_->__add_shared();
2307 if (__state_)
2308 __state_->__release_shared();
2309 __state_ = __rhs.__state_;
2310 return *this;
2311}
2312
2313template <class _R>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002314class _LIBCPP_VISIBLE shared_future<_R&>
Howard Hinnant99be8232010-09-03 18:39:25 +00002315{
2316 __assoc_state<_R&>* __state_;
2317
2318public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002319 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002320 shared_future() : __state_(nullptr) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002321 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002322 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2323 {if (__state_) __state_->__add_shared();}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002324#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002325 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002326 shared_future(future<_R&>&& __f) : __state_(__f.__state_)
2327 {__f.__state_ = nullptr;}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002328 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002329 shared_future(shared_future&& __rhs) : __state_(__rhs.__state_)
2330 {__rhs.__state_ = nullptr;}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002331#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:25 +00002332 ~shared_future();
2333 shared_future& operator=(const shared_future& __rhs);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002334#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002335 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002336 shared_future& operator=(shared_future&& __rhs)
2337 {
2338 shared_future(std::move(__rhs)).swap(*this);
2339 return *this;
2340 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002341#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:25 +00002342
2343 // retrieving the value
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002344 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002345 _R& get() const {return __state_->copy();}
2346
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002347 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0949eed2011-06-30 21:18:19 +00002348 void swap(shared_future& __rhs) {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant99be8232010-09-03 18:39:25 +00002349
2350 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002351 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002352 bool valid() const {return __state_ != nullptr;}
2353
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002354 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002355 void wait() const {__state_->wait();}
2356 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002357 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002358 future_status
2359 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2360 {return __state_->wait_for(__rel_time);}
2361 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002362 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002363 future_status
2364 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2365 {return __state_->wait_until(__abs_time);}
2366};
2367
2368template <class _R>
2369shared_future<_R&>::~shared_future()
2370{
2371 if (__state_)
2372 __state_->__release_shared();
2373}
2374
2375template <class _R>
2376shared_future<_R&>&
2377shared_future<_R&>::operator=(const shared_future& __rhs)
2378{
2379 if (__rhs.__state_)
2380 __rhs.__state_->__add_shared();
2381 if (__state_)
2382 __state_->__release_shared();
2383 __state_ = __rhs.__state_;
2384 return *this;
2385}
2386
2387template <>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002388class _LIBCPP_VISIBLE shared_future<void>
Howard Hinnant99be8232010-09-03 18:39:25 +00002389{
2390 __assoc_sub_state* __state_;
2391
2392public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002393 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002394 shared_future() : __state_(nullptr) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002395 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002396 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2397 {if (__state_) __state_->__add_shared();}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002398#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002399 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002400 shared_future(future<void>&& __f) : __state_(__f.__state_)
2401 {__f.__state_ = nullptr;}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002402 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002403 shared_future(shared_future&& __rhs) : __state_(__rhs.__state_)
2404 {__rhs.__state_ = nullptr;}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002405#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:25 +00002406 ~shared_future();
2407 shared_future& operator=(const shared_future& __rhs);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002408#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002409 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002410 shared_future& operator=(shared_future&& __rhs)
2411 {
2412 shared_future(std::move(__rhs)).swap(*this);
2413 return *this;
2414 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002415#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:25 +00002416
2417 // retrieving the value
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002418 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002419 void get() const {__state_->copy();}
2420
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002421 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0949eed2011-06-30 21:18:19 +00002422 void swap(shared_future& __rhs) {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant99be8232010-09-03 18:39:25 +00002423
2424 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002425 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002426 bool valid() const {return __state_ != nullptr;}
2427
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002428 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002429 void wait() const {__state_->wait();}
2430 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002431 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002432 future_status
2433 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2434 {return __state_->wait_for(__rel_time);}
2435 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002436 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002437 future_status
2438 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2439 {return __state_->wait_until(__abs_time);}
2440};
2441
2442template <class _R>
2443inline _LIBCPP_INLINE_VISIBILITY
2444void
2445swap(shared_future<_R>& __x, shared_future<_R>& __y)
2446{
2447 __x.swap(__y);
2448}
2449
Howard Hinnante6e4d012010-09-03 21:46:37 +00002450template <class _R>
Howard Hinnant7de47902010-11-30 20:23:32 +00002451inline _LIBCPP_INLINE_VISIBILITY
2452shared_future<_R>
2453future<_R>::share()
Howard Hinnante6e4d012010-09-03 21:46:37 +00002454{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002455 return shared_future<_R>(_VSTD::move(*this));
Howard Hinnante6e4d012010-09-03 21:46:37 +00002456}
2457
2458template <class _R>
Howard Hinnante6e4d012010-09-03 21:46:37 +00002459inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7de47902010-11-30 20:23:32 +00002460shared_future<_R&>
2461future<_R&>::share()
Howard Hinnante6e4d012010-09-03 21:46:37 +00002462{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002463 return shared_future<_R&>(_VSTD::move(*this));
Howard Hinnant7de47902010-11-30 20:23:32 +00002464}
2465
Howard Hinnanta4451512010-12-02 16:45:21 +00002466#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2467
Howard Hinnant7de47902010-11-30 20:23:32 +00002468inline _LIBCPP_INLINE_VISIBILITY
2469shared_future<void>
2470future<void>::share()
2471{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002472 return shared_future<void>(_VSTD::move(*this));
Howard Hinnante6e4d012010-09-03 21:46:37 +00002473}
2474
Howard Hinnanta4451512010-12-02 16:45:21 +00002475#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2476
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002477_LIBCPP_END_NAMESPACE_STD
2478
2479#endif // _LIBCPP_FUTURE