blob: e39ae4cf8a49038aeda7111112d44744ea8d2523 [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_;}
455};
456
Howard Hinnant47499b12010-08-27 20:10:19 +0000457class __assoc_sub_state
458 : public __shared_count
459{
460protected:
461 exception_ptr __exception_;
462 mutable mutex __mut_;
463 mutable condition_variable __cv_;
464 unsigned __state_;
465
Howard Hinnant1694d232011-05-28 14:41:13 +0000466 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant54da3382010-08-30 18:46:21 +0000467 void __sub_wait(unique_lock<mutex>& __lk);
Howard Hinnant47499b12010-08-27 20:10:19 +0000468public:
469 enum
470 {
471 __constructed = 1,
472 __future_attached = 2,
473 ready = 4,
474 deferred = 8
475 };
476
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000477 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000478 __assoc_sub_state() : __state_(0) {}
479
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000480 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000481 bool __has_value() const
482 {return (__state_ & __constructed) || (__exception_ != nullptr);}
483
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000484 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000485 void __set_future_attached() {__state_ |= __future_attached;}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000486 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000487 bool __has_future_attached() const {return __state_ & __future_attached;}
488
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000489 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +0000490 void __set_deferred() {__state_ |= deferred;}
491
Howard Hinnant47499b12010-08-27 20:10:19 +0000492 void __make_ready();
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000493 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000494 bool __is_ready() const {return __state_ & ready;}
495
496 void set_value();
497 void set_value_at_thread_exit();
498
499 void set_exception(exception_ptr __p);
500 void set_exception_at_thread_exit(exception_ptr __p);
501
502 void copy();
503
Howard Hinnant54da3382010-08-30 18:46:21 +0000504 void wait();
Howard Hinnant47499b12010-08-27 20:10:19 +0000505 template <class _Rep, class _Period>
506 future_status
507 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const;
508 template <class _Clock, class _Duration>
509 future_status
510 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const;
Howard Hinnant54da3382010-08-30 18:46:21 +0000511
512 virtual void __execute();
Howard Hinnant47499b12010-08-27 20:10:19 +0000513};
514
Howard Hinnantf39daa82010-08-28 21:01:06 +0000515template <class _Clock, class _Duration>
516future_status
517__assoc_sub_state::wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
518{
519 unique_lock<mutex> __lk(__mut_);
Howard Hinnant54da3382010-08-30 18:46:21 +0000520 if (__state_ & deferred)
521 return future_status::deferred;
522 while (!(__state_ & ready) && _Clock::now() < __abs_time)
Howard Hinnantf39daa82010-08-28 21:01:06 +0000523 __cv_.wait_until(__lk, __abs_time);
524 if (__state_ & ready)
525 return future_status::ready;
Howard Hinnantf39daa82010-08-28 21:01:06 +0000526 return future_status::timeout;
527}
528
529template <class _Rep, class _Period>
530inline _LIBCPP_INLINE_VISIBILITY
531future_status
532__assoc_sub_state::wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
533{
Howard Hinnantf8f85212010-11-20 19:16:30 +0000534 return wait_until(chrono::steady_clock::now() + __rel_time);
Howard Hinnantf39daa82010-08-28 21:01:06 +0000535}
536
Howard Hinnant47499b12010-08-27 20:10:19 +0000537template <class _R>
538class __assoc_state
539 : public __assoc_sub_state
540{
541 typedef __assoc_sub_state base;
542 typedef typename aligned_storage<sizeof(_R), alignment_of<_R>::value>::type _U;
543protected:
544 _U __value_;
545
Howard Hinnant1694d232011-05-28 14:41:13 +0000546 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant47499b12010-08-27 20:10:19 +0000547public:
548
549 template <class _Arg>
Howard Hinnant73d21a42010-09-04 23:28:19 +0000550#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +0000551 void set_value(_Arg&& __arg);
552#else
553 void set_value(_Arg& __arg);
554#endif
555
556 template <class _Arg>
Howard Hinnant73d21a42010-09-04 23:28:19 +0000557#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +0000558 void set_value_at_thread_exit(_Arg&& __arg);
559#else
560 void set_value_at_thread_exit(_Arg& __arg);
561#endif
562
563 _R move();
564 typename add_lvalue_reference<_R>::type copy();
565};
566
567template <class _R>
568void
Howard Hinnant1694d232011-05-28 14:41:13 +0000569__assoc_state<_R>::__on_zero_shared() _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +0000570{
571 if (this->__state_ & base::__constructed)
572 reinterpret_cast<_R*>(&__value_)->~_R();
573 delete this;
574}
575
576template <class _R>
577template <class _Arg>
578void
Howard Hinnant73d21a42010-09-04 23:28:19 +0000579#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +0000580__assoc_state<_R>::set_value(_Arg&& __arg)
581#else
582__assoc_state<_R>::set_value(_Arg& __arg)
583#endif
584{
585 unique_lock<mutex> __lk(this->__mut_);
586 if (this->__has_value())
587 throw future_error(make_error_code(future_errc::promise_already_satisfied));
588 ::new(&__value_) _R(_STD::forward<_Arg>(__arg));
589 this->__state_ |= base::__constructed | base::ready;
590 __lk.unlock();
591 __cv_.notify_all();
592}
593
594template <class _R>
595template <class _Arg>
596void
Howard Hinnant73d21a42010-09-04 23:28:19 +0000597#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +0000598__assoc_state<_R>::set_value_at_thread_exit(_Arg&& __arg)
599#else
600__assoc_state<_R>::set_value_at_thread_exit(_Arg& __arg)
601#endif
602{
603 unique_lock<mutex> __lk(this->__mut_);
604 if (this->__has_value())
605 throw future_error(make_error_code(future_errc::promise_already_satisfied));
606 ::new(&__value_) _R(_STD::forward<_Arg>(__arg));
607 this->__state_ |= base::__constructed;
Howard Hinnant5306d682010-10-14 19:18:04 +0000608 __thread_local_data()->__make_ready_at_thread_exit(this);
Howard Hinnant47499b12010-08-27 20:10:19 +0000609 __lk.unlock();
610}
611
612template <class _R>
613_R
614__assoc_state<_R>::move()
615{
616 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant54da3382010-08-30 18:46:21 +0000617 this->__sub_wait(__lk);
Howard Hinnant47499b12010-08-27 20:10:19 +0000618 if (this->__exception_ != nullptr)
619 rethrow_exception(this->__exception_);
620 return _STD::move(*reinterpret_cast<_R*>(&__value_));
621}
622
623template <class _R>
624typename add_lvalue_reference<_R>::type
625__assoc_state<_R>::copy()
626{
627 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant54da3382010-08-30 18:46:21 +0000628 this->__sub_wait(__lk);
Howard Hinnant47499b12010-08-27 20:10:19 +0000629 if (this->__exception_ != nullptr)
630 rethrow_exception(this->__exception_);
631 return *reinterpret_cast<_R*>(&__value_);
632}
633
Howard Hinnantf39daa82010-08-28 21:01:06 +0000634template <class _R>
635class __assoc_state<_R&>
636 : public __assoc_sub_state
637{
638 typedef __assoc_sub_state base;
639 typedef _R* _U;
640protected:
641 _U __value_;
642
Howard Hinnant1694d232011-05-28 14:41:13 +0000643 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnantf39daa82010-08-28 21:01:06 +0000644public:
645
646 void set_value(_R& __arg);
647 void set_value_at_thread_exit(_R& __arg);
648
649 _R& copy();
650};
651
652template <class _R>
653void
Howard Hinnant1694d232011-05-28 14:41:13 +0000654__assoc_state<_R&>::__on_zero_shared() _NOEXCEPT
Howard Hinnantf39daa82010-08-28 21:01:06 +0000655{
656 delete this;
657}
658
659template <class _R>
660void
661__assoc_state<_R&>::set_value(_R& __arg)
662{
663 unique_lock<mutex> __lk(this->__mut_);
664 if (this->__has_value())
665 throw future_error(make_error_code(future_errc::promise_already_satisfied));
666 __value_ = &__arg;
667 this->__state_ |= base::__constructed | base::ready;
668 __lk.unlock();
669 __cv_.notify_all();
670}
671
672template <class _R>
673void
674__assoc_state<_R&>::set_value_at_thread_exit(_R& __arg)
675{
676 unique_lock<mutex> __lk(this->__mut_);
677 if (this->__has_value())
678 throw future_error(make_error_code(future_errc::promise_already_satisfied));
679 __value_ = &__arg;
680 this->__state_ |= base::__constructed;
Howard Hinnant5306d682010-10-14 19:18:04 +0000681 __thread_local_data()->__make_ready_at_thread_exit(this);
Howard Hinnantf39daa82010-08-28 21:01:06 +0000682 __lk.unlock();
683}
684
685template <class _R>
686_R&
687__assoc_state<_R&>::copy()
688{
689 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant54da3382010-08-30 18:46:21 +0000690 this->__sub_wait(__lk);
Howard Hinnantf39daa82010-08-28 21:01:06 +0000691 if (this->__exception_ != nullptr)
692 rethrow_exception(this->__exception_);
693 return *__value_;
694}
695
Howard Hinnant47499b12010-08-27 20:10:19 +0000696template <class _R, class _Alloc>
697class __assoc_state_alloc
698 : public __assoc_state<_R>
699{
700 typedef __assoc_state<_R> base;
701 _Alloc __alloc_;
702
Howard Hinnant1694d232011-05-28 14:41:13 +0000703 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant47499b12010-08-27 20:10:19 +0000704public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000705 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000706 explicit __assoc_state_alloc(const _Alloc& __a)
707 : __alloc_(__a) {}
708};
709
710template <class _R, class _Alloc>
711void
Howard Hinnant1694d232011-05-28 14:41:13 +0000712__assoc_state_alloc<_R, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +0000713{
714 if (this->__state_ & base::__constructed)
715 reinterpret_cast<_R*>(&this->__value_)->~_R();
716 typename _Alloc::template rebind<__assoc_state_alloc>::other __a(__alloc_);
717 this->~__assoc_state_alloc();
718 __a.deallocate(this, 1);
719}
720
Howard Hinnantf39daa82010-08-28 21:01:06 +0000721template <class _R, class _Alloc>
722class __assoc_state_alloc<_R&, _Alloc>
723 : public __assoc_state<_R&>
724{
725 typedef __assoc_state<_R&> base;
726 _Alloc __alloc_;
727
Howard Hinnant1694d232011-05-28 14:41:13 +0000728 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnantf39daa82010-08-28 21:01:06 +0000729public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000730 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39daa82010-08-28 21:01:06 +0000731 explicit __assoc_state_alloc(const _Alloc& __a)
732 : __alloc_(__a) {}
733};
734
735template <class _R, class _Alloc>
736void
Howard Hinnant1694d232011-05-28 14:41:13 +0000737__assoc_state_alloc<_R&, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantf39daa82010-08-28 21:01:06 +0000738{
739 typename _Alloc::template rebind<__assoc_state_alloc>::other __a(__alloc_);
740 this->~__assoc_state_alloc();
741 __a.deallocate(this, 1);
742}
743
Howard Hinnant47499b12010-08-27 20:10:19 +0000744template <class _Alloc>
745class __assoc_sub_state_alloc
746 : public __assoc_sub_state
747{
748 typedef __assoc_sub_state base;
749 _Alloc __alloc_;
750
Howard Hinnant1694d232011-05-28 14:41:13 +0000751 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant47499b12010-08-27 20:10:19 +0000752public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000753 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +0000754 explicit __assoc_sub_state_alloc(const _Alloc& __a)
755 : __alloc_(__a) {}
756};
757
758template <class _Alloc>
759void
Howard Hinnant1694d232011-05-28 14:41:13 +0000760__assoc_sub_state_alloc<_Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19 +0000761{
762 this->~base();
Howard Hinnantf39daa82010-08-28 21:01:06 +0000763 typename _Alloc::template rebind<__assoc_sub_state_alloc>::other __a(__alloc_);
Howard Hinnant47499b12010-08-27 20:10:19 +0000764 this->~__assoc_sub_state_alloc();
765 __a.deallocate(this, 1);
766}
767
Howard Hinnant54da3382010-08-30 18:46:21 +0000768template <class _R, class _F>
769class __deferred_assoc_state
770 : public __assoc_state<_R>
771{
772 typedef __assoc_state<_R> base;
773
774 _F __func_;
775
776public:
Howard Hinnant73d21a42010-09-04 23:28:19 +0000777#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21 +0000778 explicit __deferred_assoc_state(_F&& __f);
779#endif
780
781 virtual void __execute();
782};
783
Howard Hinnant73d21a42010-09-04 23:28:19 +0000784#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21 +0000785
786template <class _R, class _F>
787inline _LIBCPP_INLINE_VISIBILITY
788__deferred_assoc_state<_R, _F>::__deferred_assoc_state(_F&& __f)
789 : __func_(_STD::forward<_F>(__f))
790{
791 this->__set_deferred();
792}
793
Howard Hinnant73d21a42010-09-04 23:28:19 +0000794#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21 +0000795
796template <class _R, class _F>
797void
798__deferred_assoc_state<_R, _F>::__execute()
799{
800#ifndef _LIBCPP_NO_EXCEPTIONS
801 try
802 {
803#endif // _LIBCPP_NO_EXCEPTIONS
804 this->set_value(__func_());
805#ifndef _LIBCPP_NO_EXCEPTIONS
806 }
807 catch (...)
808 {
809 this->set_exception(current_exception());
810 }
811#endif // _LIBCPP_NO_EXCEPTIONS
812}
813
814template <class _F>
815class __deferred_assoc_state<void, _F>
816 : public __assoc_sub_state
817{
818 typedef __assoc_sub_state base;
819
820 _F __func_;
821
822public:
Howard Hinnant73d21a42010-09-04 23:28:19 +0000823#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21 +0000824 explicit __deferred_assoc_state(_F&& __f);
825#endif
826
827 virtual void __execute();
828};
829
Howard Hinnant73d21a42010-09-04 23:28:19 +0000830#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21 +0000831
832template <class _F>
833inline _LIBCPP_INLINE_VISIBILITY
834__deferred_assoc_state<void, _F>::__deferred_assoc_state(_F&& __f)
835 : __func_(_STD::forward<_F>(__f))
836{
837 this->__set_deferred();
838}
839
Howard Hinnant73d21a42010-09-04 23:28:19 +0000840#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21 +0000841
842template <class _F>
843void
844__deferred_assoc_state<void, _F>::__execute()
845{
846#ifndef _LIBCPP_NO_EXCEPTIONS
847 try
848 {
849#endif // _LIBCPP_NO_EXCEPTIONS
850 __func_();
851 this->set_value();
852#ifndef _LIBCPP_NO_EXCEPTIONS
853 }
854 catch (...)
855 {
856 this->set_exception(current_exception());
857 }
858#endif // _LIBCPP_NO_EXCEPTIONS
859}
860
Howard Hinnant57cff292011-05-19 15:05:04 +0000861template <class _R, class _F>
862class __async_assoc_state
863 : public __assoc_state<_R>
864{
865 typedef __assoc_state<_R> base;
866
867 _F __func_;
868
Howard Hinnant1694d232011-05-28 14:41:13 +0000869 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant57cff292011-05-19 15:05:04 +0000870public:
871#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
872 explicit __async_assoc_state(_F&& __f);
873#endif
874
875 virtual void __execute();
876};
877
878#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
879
880template <class _R, class _F>
881inline _LIBCPP_INLINE_VISIBILITY
882__async_assoc_state<_R, _F>::__async_assoc_state(_F&& __f)
883 : __func_(_STD::forward<_F>(__f))
884{
885}
886
887#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
888
889template <class _R, class _F>
890void
891__async_assoc_state<_R, _F>::__execute()
892{
893#ifndef _LIBCPP_NO_EXCEPTIONS
894 try
895 {
896#endif // _LIBCPP_NO_EXCEPTIONS
897 this->set_value(__func_());
898#ifndef _LIBCPP_NO_EXCEPTIONS
899 }
900 catch (...)
901 {
902 this->set_exception(current_exception());
903 }
904#endif // _LIBCPP_NO_EXCEPTIONS
905}
906
907template <class _R, class _F>
908void
Howard Hinnant1694d232011-05-28 14:41:13 +0000909__async_assoc_state<_R, _F>::__on_zero_shared() _NOEXCEPT
Howard Hinnant57cff292011-05-19 15:05:04 +0000910{
911 this->wait();
912 base::__on_zero_shared();
913}
914
915template <class _F>
916class __async_assoc_state<void, _F>
917 : public __assoc_sub_state
918{
919 typedef __assoc_sub_state base;
920
921 _F __func_;
922
Howard Hinnant1694d232011-05-28 14:41:13 +0000923 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant57cff292011-05-19 15:05:04 +0000924public:
925#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
926 explicit __async_assoc_state(_F&& __f);
927#endif
928
929 virtual void __execute();
930};
931
932#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
933
934template <class _F>
935inline _LIBCPP_INLINE_VISIBILITY
936__async_assoc_state<void, _F>::__async_assoc_state(_F&& __f)
937 : __func_(_STD::forward<_F>(__f))
938{
939}
940
941#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
942
943template <class _F>
944void
945__async_assoc_state<void, _F>::__execute()
946{
947#ifndef _LIBCPP_NO_EXCEPTIONS
948 try
949 {
950#endif // _LIBCPP_NO_EXCEPTIONS
951 __func_();
952 this->set_value();
953#ifndef _LIBCPP_NO_EXCEPTIONS
954 }
955 catch (...)
956 {
957 this->set_exception(current_exception());
958 }
959#endif // _LIBCPP_NO_EXCEPTIONS
960}
961
962template <class _F>
963void
Howard Hinnant1694d232011-05-28 14:41:13 +0000964__async_assoc_state<void, _F>::__on_zero_shared() _NOEXCEPT
Howard Hinnant57cff292011-05-19 15:05:04 +0000965{
966 this->wait();
967 base::__on_zero_shared();
968}
969
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000970template <class _R> class promise;
971template <class _R> class shared_future;
Howard Hinnant47499b12010-08-27 20:10:19 +0000972
973// future
974
Howard Hinnant54da3382010-08-30 18:46:21 +0000975template <class _R> class future;
976
977template <class _R, class _F>
978future<_R>
Howard Hinnant73d21a42010-09-04 23:28:19 +0000979#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21 +0000980__make_deferred_assoc_state(_F&& __f);
981#else
982__make_deferred_assoc_state(_F __f);
983#endif
984
Howard Hinnant57cff292011-05-19 15:05:04 +0000985template <class _R, class _F>
986future<_R>
987#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
988__make_async_assoc_state(_F&& __f);
989#else
990__make_async_assoc_state(_F __f);
991#endif
992
Howard Hinnant47499b12010-08-27 20:10:19 +0000993template <class _R>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +0000994class _LIBCPP_VISIBLE future
Howard Hinnant47499b12010-08-27 20:10:19 +0000995{
996 __assoc_state<_R>* __state_;
997
998 explicit future(__assoc_state<_R>* __state);
999
1000 template <class> friend class promise;
Howard Hinnant99be8232010-09-03 18:39:25 +00001001 template <class> friend class shared_future;
Howard Hinnant54da3382010-08-30 18:46:21 +00001002
Howard Hinnant73d21a42010-09-04 23:28:19 +00001003#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57cff292011-05-19 15:05:04 +00001004 template <class _R1, class _F>
Howard Hinnant54da3382010-08-30 18:46:21 +00001005 friend future<_R1> __make_deferred_assoc_state(_F&& __f);
Howard Hinnant57cff292011-05-19 15:05:04 +00001006 template <class _R1, class _F>
1007 friend future<_R1> __make_async_assoc_state(_F&& __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001008#else
Howard Hinnant57cff292011-05-19 15:05:04 +00001009 template <class _R1, class _F>
Howard Hinnant54da3382010-08-30 18:46:21 +00001010 friend future<_R1> __make_deferred_assoc_state(_F __f);
Howard Hinnant57cff292011-05-19 15:05:04 +00001011 template <class _R1, class _F>
1012 friend future<_R1> __make_async_assoc_state(_F __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001013#endif
1014
Howard Hinnant47499b12010-08-27 20:10:19 +00001015public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001016 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001017 future() : __state_(nullptr) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001018#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001019 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001020 future(future&& __rhs)
1021 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1022 future(const future&) = delete;
1023 future& operator=(const future&) = delete;
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001024 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001025 future& operator=(future&& __rhs)
1026 {
1027 future(std::move(__rhs)).swap(*this);
1028 return *this;
1029 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001030#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001031private:
1032 future(const future&);
1033 future& operator=(const future&);
1034public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001035#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001036 ~future();
Howard Hinnant7de47902010-11-30 20:23:32 +00001037 shared_future<_R> share();
Howard Hinnant47499b12010-08-27 20:10:19 +00001038
1039 // retrieving the value
1040 _R get();
1041
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001042 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001043 void swap(future& __rhs) {_STD::swap(__state_, __rhs.__state_);}
1044
1045 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001046 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001047 bool valid() const {return __state_ != nullptr;}
1048
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001049 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001050 void wait() const {__state_->wait();}
1051 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001052 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001053 future_status
1054 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1055 {return __state_->wait_for(__rel_time);}
1056 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001057 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001058 future_status
1059 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1060 {return __state_->wait_until(__abs_time);}
1061};
1062
1063template <class _R>
1064future<_R>::future(__assoc_state<_R>* __state)
1065 : __state_(__state)
1066{
1067 if (__state_->__has_future_attached())
1068 throw future_error(make_error_code(future_errc::future_already_retrieved));
1069 __state_->__add_shared();
Howard Hinnant54da3382010-08-30 18:46:21 +00001070 __state_->__set_future_attached();
Howard Hinnant47499b12010-08-27 20:10:19 +00001071}
1072
Howard Hinnant54da3382010-08-30 18:46:21 +00001073struct __release_shared_count
1074{
1075 void operator()(__shared_count* p) {p->__release_shared();}
1076};
1077
Howard Hinnant47499b12010-08-27 20:10:19 +00001078template <class _R>
1079future<_R>::~future()
1080{
1081 if (__state_)
1082 __state_->__release_shared();
1083}
1084
1085template <class _R>
1086_R
1087future<_R>::get()
1088{
Howard Hinnant54da3382010-08-30 18:46:21 +00001089 unique_ptr<__shared_count, __release_shared_count> __(__state_);
Howard Hinnant47499b12010-08-27 20:10:19 +00001090 __assoc_state<_R>* __s = __state_;
1091 __state_ = nullptr;
1092 return __s->move();
1093}
1094
1095template <class _R>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001096class _LIBCPP_VISIBLE future<_R&>
Howard Hinnant47499b12010-08-27 20:10:19 +00001097{
1098 __assoc_state<_R&>* __state_;
1099
1100 explicit future(__assoc_state<_R&>* __state);
1101
1102 template <class> friend class promise;
Howard Hinnant99be8232010-09-03 18:39:25 +00001103 template <class> friend class shared_future;
Howard Hinnant54da3382010-08-30 18:46:21 +00001104
Howard Hinnant73d21a42010-09-04 23:28:19 +00001105#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57cff292011-05-19 15:05:04 +00001106 template <class _R1, class _F>
Howard Hinnant54da3382010-08-30 18:46:21 +00001107 friend future<_R1> __make_deferred_assoc_state(_F&& __f);
Howard Hinnant57cff292011-05-19 15:05:04 +00001108 template <class _R1, class _F>
1109 friend future<_R1> __make_async_assoc_state(_F&& __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001110#else
Howard Hinnant57cff292011-05-19 15:05:04 +00001111 template <class _R1, class _F>
Howard Hinnant54da3382010-08-30 18:46:21 +00001112 friend future<_R1> __make_deferred_assoc_state(_F __f);
Howard Hinnant57cff292011-05-19 15:05:04 +00001113 template <class _R1, class _F>
1114 friend future<_R1> __make_async_assoc_state(_F __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001115#endif
1116
Howard Hinnant47499b12010-08-27 20:10:19 +00001117public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001118 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001119 future() : __state_(nullptr) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001120#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001121 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001122 future(future&& __rhs)
1123 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1124 future(const future&) = delete;
1125 future& operator=(const future&) = delete;
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001126 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001127 future& operator=(future&& __rhs)
1128 {
1129 future(std::move(__rhs)).swap(*this);
1130 return *this;
1131 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001132#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001133private:
1134 future(const future&);
1135 future& operator=(const future&);
1136public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001137#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001138 ~future();
Howard Hinnant7de47902010-11-30 20:23:32 +00001139 shared_future<_R&> share();
Howard Hinnant47499b12010-08-27 20:10:19 +00001140
1141 // retrieving the value
1142 _R& get();
1143
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001144 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001145 void swap(future& __rhs) {_STD::swap(__state_, __rhs.__state_);}
1146
1147 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001148 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001149 bool valid() const {return __state_ != nullptr;}
1150
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001151 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001152 void wait() const {__state_->wait();}
1153 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001154 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001155 future_status
1156 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1157 {return __state_->wait_for(__rel_time);}
1158 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001159 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001160 future_status
1161 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1162 {return __state_->wait_until(__abs_time);}
1163};
1164
1165template <class _R>
1166future<_R&>::future(__assoc_state<_R&>* __state)
1167 : __state_(__state)
1168{
1169 if (__state_->__has_future_attached())
1170 throw future_error(make_error_code(future_errc::future_already_retrieved));
1171 __state_->__add_shared();
Howard Hinnant54da3382010-08-30 18:46:21 +00001172 __state_->__set_future_attached();
Howard Hinnant47499b12010-08-27 20:10:19 +00001173}
1174
1175template <class _R>
1176future<_R&>::~future()
1177{
1178 if (__state_)
1179 __state_->__release_shared();
1180}
1181
1182template <class _R>
1183_R&
1184future<_R&>::get()
1185{
Howard Hinnant54da3382010-08-30 18:46:21 +00001186 unique_ptr<__shared_count, __release_shared_count> __(__state_);
Howard Hinnantf39daa82010-08-28 21:01:06 +00001187 __assoc_state<_R&>* __s = __state_;
Howard Hinnant47499b12010-08-27 20:10:19 +00001188 __state_ = nullptr;
1189 return __s->copy();
1190}
1191
1192template <>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001193class _LIBCPP_VISIBLE future<void>
Howard Hinnant47499b12010-08-27 20:10:19 +00001194{
1195 __assoc_sub_state* __state_;
1196
1197 explicit future(__assoc_sub_state* __state);
1198
1199 template <class> friend class promise;
Howard Hinnant99be8232010-09-03 18:39:25 +00001200 template <class> friend class shared_future;
Howard Hinnant54da3382010-08-30 18:46:21 +00001201
Howard Hinnant73d21a42010-09-04 23:28:19 +00001202#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57cff292011-05-19 15:05:04 +00001203 template <class _R1, class _F>
Howard Hinnant54da3382010-08-30 18:46:21 +00001204 friend future<_R1> __make_deferred_assoc_state(_F&& __f);
Howard Hinnant57cff292011-05-19 15:05:04 +00001205 template <class _R1, class _F>
1206 friend future<_R1> __make_async_assoc_state(_F&& __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001207#else
Howard Hinnant57cff292011-05-19 15:05:04 +00001208 template <class _R1, class _F>
Howard Hinnant54da3382010-08-30 18:46:21 +00001209 friend future<_R1> __make_deferred_assoc_state(_F __f);
Howard Hinnant57cff292011-05-19 15:05:04 +00001210 template <class _R1, class _F>
1211 friend future<_R1> __make_async_assoc_state(_F __f);
Howard Hinnant54da3382010-08-30 18:46:21 +00001212#endif
1213
Howard Hinnant47499b12010-08-27 20:10:19 +00001214public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001215 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001216 future() : __state_(nullptr) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001217#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001218 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001219 future(future&& __rhs)
1220 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1221 future(const future&) = delete;
1222 future& operator=(const future&) = delete;
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001223 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001224 future& operator=(future&& __rhs)
1225 {
1226 future(std::move(__rhs)).swap(*this);
1227 return *this;
1228 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001229#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001230private:
1231 future(const future&);
1232 future& operator=(const future&);
1233public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001234#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001235 ~future();
Howard Hinnant7de47902010-11-30 20:23:32 +00001236 shared_future<void> share();
Howard Hinnant47499b12010-08-27 20:10:19 +00001237
1238 // retrieving the value
1239 void get();
1240
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001241 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001242 void swap(future& __rhs) {_STD::swap(__state_, __rhs.__state_);}
1243
1244 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001245 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001246 bool valid() const {return __state_ != nullptr;}
1247
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001248 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001249 void wait() const {__state_->wait();}
1250 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001251 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001252 future_status
1253 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1254 {return __state_->wait_for(__rel_time);}
1255 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001256 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001257 future_status
1258 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1259 {return __state_->wait_until(__abs_time);}
1260};
1261
Howard Hinnant99be8232010-09-03 18:39:25 +00001262template <class _R>
1263inline _LIBCPP_INLINE_VISIBILITY
1264void
1265swap(future<_R>& __x, future<_R>& __y)
1266{
1267 __x.swap(__y);
1268}
1269
Howard Hinnant47499b12010-08-27 20:10:19 +00001270// promise<R>
1271
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00001272template <class _Callable> class packaged_task;
Howard Hinnant54da3382010-08-30 18:46:21 +00001273
Howard Hinnant47499b12010-08-27 20:10:19 +00001274template <class _R>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001275class _LIBCPP_VISIBLE promise
Howard Hinnant47499b12010-08-27 20:10:19 +00001276{
1277 __assoc_state<_R>* __state_;
Howard Hinnant54da3382010-08-30 18:46:21 +00001278
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001279 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001280 explicit promise(nullptr_t) : __state_(nullptr) {}
1281
1282 template <class> friend class packaged_task;
Howard Hinnant47499b12010-08-27 20:10:19 +00001283public:
1284 promise();
1285 template <class _Alloc>
1286 promise(allocator_arg_t, const _Alloc& __a);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001287#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001288 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001289 promise(promise&& __rhs)
1290 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1291 promise(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +00001292#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001293private:
1294 promise(const promise& __rhs);
1295public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001296#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001297 ~promise();
1298
1299 // assignment
Howard Hinnant73d21a42010-09-04 23:28:19 +00001300#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001301 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001302 promise& operator=(promise&& __rhs)
1303 {
1304 promise(std::move(__rhs)).swap(*this);
1305 return *this;
1306 }
1307 promise& operator=(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +00001308#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001309private:
1310 promise& operator=(const promise& __rhs);
1311public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001312#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001313 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001314 void swap(promise& __rhs) {_STD::swap(__state_, __rhs.__state_);}
1315
1316 // retrieving the result
1317 future<_R> get_future();
1318
1319 // setting the result
1320 void set_value(const _R& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001321#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001322 void set_value(_R&& __r);
1323#endif
1324 void set_exception(exception_ptr __p);
1325
1326 // setting the result with deferred notification
1327 void set_value_at_thread_exit(const _R& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001328#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001329 void set_value_at_thread_exit(_R&& __r);
1330#endif
1331 void set_exception_at_thread_exit(exception_ptr __p);
1332};
1333
1334template <class _R>
1335promise<_R>::promise()
1336 : __state_(new __assoc_state<_R>)
1337{
1338}
1339
1340template <class _R>
1341template <class _Alloc>
1342promise<_R>::promise(allocator_arg_t, const _Alloc& __a0)
1343{
1344 typedef typename _Alloc::template rebind<__assoc_state_alloc<_R, _Alloc> >::other _A2;
1345 typedef __allocator_destructor<_A2> _D2;
1346 _A2 __a(__a0);
1347 unique_ptr<__assoc_state_alloc<_R, _Alloc>, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1348 ::new(__hold.get()) __assoc_state_alloc<_R, _Alloc>(__a0);
1349 __state_ = __hold.release();
1350}
1351
1352template <class _R>
1353promise<_R>::~promise()
1354{
1355 if (__state_)
1356 {
1357 if (!__state_->__has_value() && __state_->use_count() > 1)
1358 __state_->set_exception(make_exception_ptr(
1359 future_error(make_error_code(future_errc::broken_promise))
1360 ));
1361 __state_->__release_shared();
1362 }
1363}
1364
1365template <class _R>
1366future<_R>
1367promise<_R>::get_future()
1368{
1369 if (__state_ == nullptr)
1370 throw future_error(make_error_code(future_errc::no_state));
1371 return future<_R>(__state_);
1372}
1373
1374template <class _R>
1375void
1376promise<_R>::set_value(const _R& __r)
1377{
1378 if (__state_ == nullptr)
1379 throw future_error(make_error_code(future_errc::no_state));
1380 __state_->set_value(__r);
1381}
1382
Howard Hinnant73d21a42010-09-04 23:28:19 +00001383#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001384
1385template <class _R>
1386void
1387promise<_R>::set_value(_R&& __r)
1388{
1389 if (__state_ == nullptr)
1390 throw future_error(make_error_code(future_errc::no_state));
1391 __state_->set_value(_STD::move(__r));
1392}
1393
Howard Hinnant73d21a42010-09-04 23:28:19 +00001394#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001395
1396template <class _R>
1397void
1398promise<_R>::set_exception(exception_ptr __p)
1399{
1400 if (__state_ == nullptr)
1401 throw future_error(make_error_code(future_errc::no_state));
1402 __state_->set_exception(__p);
1403}
1404
1405template <class _R>
1406void
1407promise<_R>::set_value_at_thread_exit(const _R& __r)
1408{
1409 if (__state_ == nullptr)
1410 throw future_error(make_error_code(future_errc::no_state));
1411 __state_->set_value_at_thread_exit(__r);
1412}
1413
Howard Hinnant73d21a42010-09-04 23:28:19 +00001414#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001415
1416template <class _R>
1417void
1418promise<_R>::set_value_at_thread_exit(_R&& __r)
1419{
1420 if (__state_ == nullptr)
1421 throw future_error(make_error_code(future_errc::no_state));
1422 __state_->set_value_at_thread_exit(_STD::move(__r));
1423}
1424
Howard Hinnant73d21a42010-09-04 23:28:19 +00001425#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001426
1427template <class _R>
1428void
1429promise<_R>::set_exception_at_thread_exit(exception_ptr __p)
1430{
1431 if (__state_ == nullptr)
1432 throw future_error(make_error_code(future_errc::no_state));
1433 __state_->set_exception_at_thread_exit(__p);
1434}
1435
1436// promise<R&>
1437
1438template <class _R>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001439class _LIBCPP_VISIBLE promise<_R&>
Howard Hinnant47499b12010-08-27 20:10:19 +00001440{
1441 __assoc_state<_R&>* __state_;
Howard Hinnant54da3382010-08-30 18:46:21 +00001442
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001443 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001444 explicit promise(nullptr_t) : __state_(nullptr) {}
1445
1446 template <class> friend class packaged_task;
1447
Howard Hinnant47499b12010-08-27 20:10:19 +00001448public:
1449 promise();
1450 template <class _Allocator>
1451 promise(allocator_arg_t, const _Allocator& __a);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001452#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001453 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001454 promise(promise&& __rhs)
1455 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1456 promise(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +00001457#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001458private:
1459 promise(const promise& __rhs);
1460public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001461#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001462 ~promise();
1463
1464 // assignment
Howard Hinnant73d21a42010-09-04 23:28:19 +00001465#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001466 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001467 promise& operator=(promise&& __rhs)
1468 {
1469 promise(std::move(__rhs)).swap(*this);
1470 return *this;
1471 }
1472 promise& operator=(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +00001473#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001474private:
1475 promise& operator=(const promise& __rhs);
1476public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001477#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001478 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001479 void swap(promise& __rhs) {_STD::swap(__state_, __rhs.__state_);}
1480
1481 // retrieving the result
1482 future<_R&> get_future();
1483
1484 // setting the result
1485 void set_value(_R& __r);
1486 void set_exception(exception_ptr __p);
1487
1488 // setting the result with deferred notification
1489 void set_value_at_thread_exit(_R&);
1490 void set_exception_at_thread_exit(exception_ptr __p);
1491};
1492
1493template <class _R>
1494promise<_R&>::promise()
1495 : __state_(new __assoc_state<_R&>)
1496{
1497}
1498
1499template <class _R>
1500template <class _Alloc>
1501promise<_R&>::promise(allocator_arg_t, const _Alloc& __a0)
1502{
1503 typedef typename _Alloc::template rebind<__assoc_state_alloc<_R&, _Alloc> >::other _A2;
1504 typedef __allocator_destructor<_A2> _D2;
1505 _A2 __a(__a0);
1506 unique_ptr<__assoc_state_alloc<_R&, _Alloc>, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1507 ::new(__hold.get()) __assoc_state_alloc<_R&, _Alloc>(__a0);
1508 __state_ = __hold.release();
1509}
1510
1511template <class _R>
1512promise<_R&>::~promise()
1513{
1514 if (__state_)
1515 {
1516 if (!__state_->__has_value() && __state_->use_count() > 1)
1517 __state_->set_exception(make_exception_ptr(
1518 future_error(make_error_code(future_errc::broken_promise))
1519 ));
1520 __state_->__release_shared();
1521 }
1522}
1523
1524template <class _R>
1525future<_R&>
1526promise<_R&>::get_future()
1527{
1528 if (__state_ == nullptr)
1529 throw future_error(make_error_code(future_errc::no_state));
1530 return future<_R&>(__state_);
1531}
1532
1533template <class _R>
1534void
1535promise<_R&>::set_value(_R& __r)
1536{
1537 if (__state_ == nullptr)
1538 throw future_error(make_error_code(future_errc::no_state));
1539 __state_->set_value(__r);
1540}
1541
1542template <class _R>
1543void
1544promise<_R&>::set_exception(exception_ptr __p)
1545{
1546 if (__state_ == nullptr)
1547 throw future_error(make_error_code(future_errc::no_state));
1548 __state_->set_exception(__p);
1549}
1550
1551template <class _R>
1552void
1553promise<_R&>::set_value_at_thread_exit(_R& __r)
1554{
1555 if (__state_ == nullptr)
1556 throw future_error(make_error_code(future_errc::no_state));
1557 __state_->set_value_at_thread_exit(__r);
1558}
1559
1560template <class _R>
1561void
1562promise<_R&>::set_exception_at_thread_exit(exception_ptr __p)
1563{
1564 if (__state_ == nullptr)
1565 throw future_error(make_error_code(future_errc::no_state));
1566 __state_->set_exception_at_thread_exit(__p);
1567}
1568
1569// promise<void>
1570
1571template <>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001572class _LIBCPP_VISIBLE promise<void>
Howard Hinnant47499b12010-08-27 20:10:19 +00001573{
1574 __assoc_sub_state* __state_;
Howard Hinnant54da3382010-08-30 18:46:21 +00001575
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001576 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001577 explicit promise(nullptr_t) : __state_(nullptr) {}
1578
1579 template <class> friend class packaged_task;
1580
Howard Hinnant47499b12010-08-27 20:10:19 +00001581public:
1582 promise();
1583 template <class _Allocator>
1584 promise(allocator_arg_t, const _Allocator& __a);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001585#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001586 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001587 promise(promise&& __rhs)
1588 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1589 promise(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +00001590#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001591private:
1592 promise(const promise& __rhs);
1593public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001594#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001595 ~promise();
1596
1597 // assignment
Howard Hinnant73d21a42010-09-04 23:28:19 +00001598#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001599 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001600 promise& operator=(promise&& __rhs)
1601 {
1602 promise(std::move(__rhs)).swap(*this);
1603 return *this;
1604 }
1605 promise& operator=(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +00001606#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19 +00001607private:
1608 promise& operator=(const promise& __rhs);
1609public:
Howard Hinnant73d21a42010-09-04 23:28:19 +00001610#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001611 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19 +00001612 void swap(promise& __rhs) {_STD::swap(__state_, __rhs.__state_);}
1613
1614 // retrieving the result
1615 future<void> get_future();
1616
1617 // setting the result
1618 void set_value();
1619 void set_exception(exception_ptr __p);
1620
1621 // setting the result with deferred notification
1622 void set_value_at_thread_exit();
1623 void set_exception_at_thread_exit(exception_ptr __p);
1624};
1625
1626template <class _Alloc>
1627promise<void>::promise(allocator_arg_t, const _Alloc& __a0)
1628{
1629 typedef typename _Alloc::template rebind<__assoc_sub_state_alloc<_Alloc> >::other _A2;
1630 typedef __allocator_destructor<_A2> _D2;
1631 _A2 __a(__a0);
1632 unique_ptr<__assoc_sub_state_alloc<_Alloc>, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1633 ::new(__hold.get()) __assoc_sub_state_alloc<_Alloc>(__a0);
1634 __state_ = __hold.release();
1635}
1636
1637template <class _R>
1638inline _LIBCPP_INLINE_VISIBILITY
1639void
1640swap(promise<_R>& __x, promise<_R>& __y)
1641{
1642 __x.swap(__y);
1643}
1644
1645template <class _R, class _Alloc>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001646 struct _LIBCPP_VISIBLE uses_allocator<promise<_R>, _Alloc>
1647 : public true_type {};
Howard Hinnant47499b12010-08-27 20:10:19 +00001648
Howard Hinnant54da3382010-08-30 18:46:21 +00001649#ifndef _LIBCPP_HAS_NO_VARIADICS
1650
1651// packaged_task
1652
1653template<class _Fp> class __packaged_task_base;
1654
1655template<class _R, class ..._ArgTypes>
1656class __packaged_task_base<_R(_ArgTypes...)>
1657{
1658 __packaged_task_base(const __packaged_task_base&);
1659 __packaged_task_base& operator=(const __packaged_task_base&);
1660public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001661 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001662 __packaged_task_base() {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001663 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001664 virtual ~__packaged_task_base() {}
1665 virtual void __move_to(__packaged_task_base*) = 0;
1666 virtual void destroy() = 0;
1667 virtual void destroy_deallocate() = 0;
1668 virtual _R operator()(_ArgTypes&& ...) = 0;
1669};
1670
1671template<class _FD, class _Alloc, class _FB> class __packaged_task_func;
1672
1673template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1674class __packaged_task_func<_F, _Alloc, _R(_ArgTypes...)>
1675 : public __packaged_task_base<_R(_ArgTypes...)>
1676{
1677 __compressed_pair<_F, _Alloc> __f_;
1678public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001679 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001680 explicit __packaged_task_func(const _F& __f) : __f_(__f) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001681 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001682 explicit __packaged_task_func(_F&& __f) : __f_(_STD::move(__f)) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001683 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001684 __packaged_task_func(const _F& __f, const _Alloc& __a)
1685 : __f_(__f, __a) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001686 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001687 __packaged_task_func(_F&& __f, const _Alloc& __a)
1688 : __f_(_STD::move(__f), __a) {}
1689 virtual void __move_to(__packaged_task_base<_R(_ArgTypes...)>*);
1690 virtual void destroy();
1691 virtual void destroy_deallocate();
1692 virtual _R operator()(_ArgTypes&& ... __args);
1693};
1694
1695template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1696void
1697__packaged_task_func<_F, _Alloc, _R(_ArgTypes...)>::__move_to(
1698 __packaged_task_base<_R(_ArgTypes...)>* __p)
1699{
1700 ::new (__p) __packaged_task_func(_STD::move(__f_.first()), _STD::move(__f_.second()));
1701}
1702
1703template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1704void
1705__packaged_task_func<_F, _Alloc, _R(_ArgTypes...)>::destroy()
1706{
1707 __f_.~__compressed_pair<_F, _Alloc>();
1708}
1709
1710template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1711void
1712__packaged_task_func<_F, _Alloc, _R(_ArgTypes...)>::destroy_deallocate()
1713{
1714 typedef typename _Alloc::template rebind<__packaged_task_func>::other _A;
1715 _A __a(__f_.second());
1716 __f_.~__compressed_pair<_F, _Alloc>();
1717 __a.deallocate(this, 1);
1718}
1719
1720template<class _F, class _Alloc, class _R, class ..._ArgTypes>
1721_R
1722__packaged_task_func<_F, _Alloc, _R(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
1723{
1724 return __invoke(__f_.first(), _STD::forward<_ArgTypes>(__arg)...);
1725}
1726
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00001727template <class _Callable> class __packaged_task_function;
Howard Hinnant54da3382010-08-30 18:46:21 +00001728
1729template<class _R, class ..._ArgTypes>
1730class __packaged_task_function<_R(_ArgTypes...)>
1731{
1732 typedef __packaged_task_base<_R(_ArgTypes...)> __base;
1733 aligned_storage<3*sizeof(void*)>::type __buf_;
1734 __base* __f_;
1735
1736public:
1737 typedef _R result_type;
1738
1739 // construct/copy/destroy:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001740 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001741 __packaged_task_function() : __f_(nullptr) {}
1742 template<class _F>
1743 __packaged_task_function(_F&& __f);
1744 template<class _F, class _Alloc>
1745 __packaged_task_function(allocator_arg_t, const _Alloc& __a, _F&& __f);
1746
1747 __packaged_task_function(__packaged_task_function&&);
1748 __packaged_task_function& operator=(__packaged_task_function&&);
1749
1750 __packaged_task_function(const __packaged_task_function&) = delete;
1751 __packaged_task_function& operator=(const __packaged_task_function&) = delete;
1752
1753 ~__packaged_task_function();
1754
1755 void swap(__packaged_task_function&);
1756
1757 _R operator()(_ArgTypes...) const;
1758};
1759
1760template<class _R, class ..._ArgTypes>
1761__packaged_task_function<_R(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f)
1762{
1763 if (__f.__f_ == nullptr)
1764 __f_ = nullptr;
1765 else if (__f.__f_ == (__base*)&__f.__buf_)
1766 {
1767 __f_ = (__base*)&__buf_;
1768 __f.__f_->__move_to(__f_);
1769 }
1770 else
1771 {
1772 __f_ = __f.__f_;
1773 __f.__f_ = nullptr;
1774 }
1775}
1776
1777template<class _R, class ..._ArgTypes>
1778template <class _F>
1779__packaged_task_function<_R(_ArgTypes...)>::__packaged_task_function(_F&& __f)
1780 : __f_(nullptr)
1781{
1782 typedef typename remove_reference<_F>::type _FR;
1783 typedef __packaged_task_func<_FR, allocator<_FR>, _R(_ArgTypes...)> _FF;
1784 if (sizeof(_FF) <= sizeof(__buf_))
1785 {
1786 __f_ = (__base*)&__buf_;
1787 ::new (__f_) _FF(_STD::forward<_F>(__f));
1788 }
1789 else
1790 {
1791 typedef allocator<_FF> _A;
1792 _A __a;
1793 typedef __allocator_destructor<_A> _D;
1794 unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
1795 ::new (__hold.get()) _FF(_STD::forward<_F>(__f), allocator<_FR>(__a));
1796 __f_ = __hold.release();
1797 }
1798}
1799
1800template<class _R, class ..._ArgTypes>
1801template <class _F, class _Alloc>
1802__packaged_task_function<_R(_ArgTypes...)>::__packaged_task_function(
1803 allocator_arg_t, const _Alloc& __a0, _F&& __f)
1804 : __f_(nullptr)
1805{
1806 typedef allocator_traits<_Alloc> __alloc_traits;
1807 typedef typename remove_reference<_F>::type _FR;
1808 typedef __packaged_task_func<_FR, _Alloc, _R(_ArgTypes...)> _FF;
1809 if (sizeof(_FF) <= sizeof(__buf_))
1810 {
1811 __f_ = (__base*)&__buf_;
1812 ::new (__f_) _FF(_STD::forward<_F>(__f));
1813 }
1814 else
1815 {
1816 typedef typename __alloc_traits::template
1817#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1818 rebind_alloc<_FF>
1819#else
1820 rebind_alloc<_FF>::other
1821#endif
1822 _A;
1823 _A __a(__a0);
1824 typedef __allocator_destructor<_A> _D;
1825 unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
1826 ::new (__hold.get()) _FF(_STD::forward<_F>(__f), _Alloc(__a));
1827 __f_ = __hold.release();
1828 }
1829}
1830
1831template<class _R, class ..._ArgTypes>
1832__packaged_task_function<_R(_ArgTypes...)>&
1833__packaged_task_function<_R(_ArgTypes...)>::operator=(__packaged_task_function&& __f)
1834{
1835 if (__f_ == (__base*)&__buf_)
1836 __f_->destroy();
1837 else if (__f_)
1838 __f_->destroy_deallocate();
1839 __f_ = nullptr;
1840 if (__f.__f_ == nullptr)
1841 __f_ = nullptr;
1842 else if (__f.__f_ == (__base*)&__f.__buf_)
1843 {
1844 __f_ = (__base*)&__buf_;
1845 __f.__f_->__move_to(__f_);
1846 }
1847 else
1848 {
1849 __f_ = __f.__f_;
1850 __f.__f_ = nullptr;
1851 }
1852}
1853
1854template<class _R, class ..._ArgTypes>
1855__packaged_task_function<_R(_ArgTypes...)>::~__packaged_task_function()
1856{
1857 if (__f_ == (__base*)&__buf_)
1858 __f_->destroy();
1859 else if (__f_)
1860 __f_->destroy_deallocate();
1861}
1862
1863template<class _R, class ..._ArgTypes>
1864void
1865__packaged_task_function<_R(_ArgTypes...)>::swap(__packaged_task_function& __f)
1866{
1867 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1868 {
1869 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1870 __base* __t = (__base*)&__tempbuf;
1871 __f_->__move_to(__t);
1872 __f_->destroy();
1873 __f_ = nullptr;
1874 __f.__f_->__move_to((__base*)&__buf_);
1875 __f.__f_->destroy();
1876 __f.__f_ = nullptr;
1877 __f_ = (__base*)&__buf_;
1878 __t->__move_to((__base*)&__f.__buf_);
1879 __t->destroy();
1880 __f.__f_ = (__base*)&__f.__buf_;
1881 }
1882 else if (__f_ == (__base*)&__buf_)
1883 {
1884 __f_->__move_to((__base*)&__f.__buf_);
1885 __f_->destroy();
1886 __f_ = __f.__f_;
1887 __f.__f_ = (__base*)&__f.__buf_;
1888 }
1889 else if (__f.__f_ == (__base*)&__f.__buf_)
1890 {
1891 __f.__f_->__move_to((__base*)&__buf_);
1892 __f.__f_->destroy();
1893 __f.__f_ = __f_;
1894 __f_ = (__base*)&__buf_;
1895 }
1896 else
1897 _STD::swap(__f_, __f.__f_);
1898}
1899
1900template<class _R, class ..._ArgTypes>
1901inline _LIBCPP_INLINE_VISIBILITY
1902_R
1903__packaged_task_function<_R(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
1904{
1905 return (*__f_)(_STD::forward<_ArgTypes>(__arg)...);
1906}
1907
1908template<class _R, class ..._ArgTypes>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001909class _LIBCPP_VISIBLE packaged_task<_R(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:21 +00001910{
1911public:
1912 typedef _R result_type;
1913
1914private:
1915 __packaged_task_function<result_type(_ArgTypes...)> __f_;
1916 promise<result_type> __p_;
1917
1918public:
1919 // construction and destruction
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001920 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001921 packaged_task() : __p_(nullptr) {}
1922 template <class _F>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001923 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001924 explicit packaged_task(_F&& __f) : __f_(_STD::forward<_F>(__f)) {}
1925 template <class _F, class _Allocator>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001926 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001927 explicit packaged_task(allocator_arg_t, const _Allocator& __a, _F&& __f)
1928 : __f_(allocator_arg, __a, _STD::forward<_F>(__f)),
1929 __p_(allocator_arg, __a) {}
1930 // ~packaged_task() = default;
1931
1932 // no copy
1933 packaged_task(packaged_task&) = delete;
1934 packaged_task& operator=(packaged_task&) = delete;
1935
1936 // move support
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001937 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001938 packaged_task(packaged_task&& __other)
1939 : __f_(_STD::move(__other.__f_)), __p_(_STD::move(__other.__p_)) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001940 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001941 packaged_task& operator=(packaged_task&& __other)
1942 {
1943 __f_ = _STD::move(__other.__f_);
1944 __p_ = _STD::move(__other.__p_);
1945 return *this;
1946 }
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001947 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001948 void swap(packaged_task& __other)
1949 {
1950 __f_.swap(__other.__f_);
1951 __p_.swap(__other.__p_);
1952 }
1953
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001954 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7de47902010-11-30 20:23:32 +00001955 bool valid() const {return __p_.__state_ != nullptr;}
Howard Hinnant54da3382010-08-30 18:46:21 +00001956
1957 // result retrieval
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00001958 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00001959 future<result_type> get_future() {return __p_.get_future();}
1960
1961 // execution
1962 void operator()(_ArgTypes... __args);
1963 void make_ready_at_thread_exit(_ArgTypes... __args);
1964
1965 void reset();
1966};
1967
1968template<class _R, class ..._ArgTypes>
1969void
1970packaged_task<_R(_ArgTypes...)>::operator()(_ArgTypes... __args)
1971{
1972#ifndef _LIBCPP_NO_EXCEPTIONS
1973 if (__p_.__state_ == nullptr)
1974 throw future_error(make_error_code(future_errc::no_state));
1975 if (__p_.__state_->__has_value())
1976 throw future_error(make_error_code(future_errc::promise_already_satisfied));
1977 try
1978 {
1979#endif // _LIBCPP_NO_EXCEPTIONS
1980 __p_.set_value(__f_(_STD::forward<_ArgTypes>(__args)...));
1981#ifndef _LIBCPP_NO_EXCEPTIONS
1982 }
1983 catch (...)
1984 {
1985 __p_.set_exception(current_exception());
1986 }
1987#endif // _LIBCPP_NO_EXCEPTIONS
1988}
1989
1990template<class _R, class ..._ArgTypes>
1991void
1992packaged_task<_R(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
1993{
1994#ifndef _LIBCPP_NO_EXCEPTIONS
1995 if (__p_.__state_ == nullptr)
1996 throw future_error(make_error_code(future_errc::no_state));
1997 if (__p_.__state_->__has_value())
1998 throw future_error(make_error_code(future_errc::promise_already_satisfied));
1999 try
2000 {
2001#endif // _LIBCPP_NO_EXCEPTIONS
2002 __p_.set_value_at_thread_exit(__f_(_STD::forward<_ArgTypes>(__args)...));
2003#ifndef _LIBCPP_NO_EXCEPTIONS
2004 }
2005 catch (...)
2006 {
2007 __p_.set_exception_at_thread_exit(current_exception());
2008 }
2009#endif // _LIBCPP_NO_EXCEPTIONS
2010}
2011
2012template<class _R, class ..._ArgTypes>
2013void
2014packaged_task<_R(_ArgTypes...)>::reset()
2015{
2016#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant7de47902010-11-30 20:23:32 +00002017 if (!valid())
Howard Hinnant54da3382010-08-30 18:46:21 +00002018 throw future_error(make_error_code(future_errc::no_state));
2019#endif // _LIBCPP_NO_EXCEPTIONS
2020 __p_ = promise<result_type>();
2021}
2022
2023template<class ..._ArgTypes>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002024class _LIBCPP_VISIBLE packaged_task<void(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:21 +00002025{
2026public:
2027 typedef void result_type;
2028
2029private:
2030 __packaged_task_function<result_type(_ArgTypes...)> __f_;
2031 promise<result_type> __p_;
2032
2033public:
2034 // construction and destruction
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002035 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00002036 packaged_task() : __p_(nullptr) {}
2037 template <class _F>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002038 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00002039 explicit packaged_task(_F&& __f) : __f_(_STD::forward<_F>(__f)) {}
2040 template <class _F, class _Allocator>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002041 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00002042 explicit packaged_task(allocator_arg_t, const _Allocator& __a, _F&& __f)
2043 : __f_(allocator_arg, __a, _STD::forward<_F>(__f)),
2044 __p_(allocator_arg, __a) {}
2045 // ~packaged_task() = default;
2046
2047 // no copy
2048 packaged_task(packaged_task&) = delete;
2049 packaged_task& operator=(packaged_task&) = delete;
2050
2051 // move support
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002052 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00002053 packaged_task(packaged_task&& __other)
2054 : __f_(_STD::move(__other.__f_)), __p_(_STD::move(__other.__p_)) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002055 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00002056 packaged_task& operator=(packaged_task&& __other)
2057 {
2058 __f_ = _STD::move(__other.__f_);
2059 __p_ = _STD::move(__other.__p_);
2060 return *this;
2061 }
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002062 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00002063 void swap(packaged_task& __other)
2064 {
2065 __f_.swap(__other.__f_);
2066 __p_.swap(__other.__p_);
2067 }
2068
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002069 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7de47902010-11-30 20:23:32 +00002070 bool valid() const {return __p_.__state_ != nullptr;}
Howard Hinnant54da3382010-08-30 18:46:21 +00002071
2072 // result retrieval
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002073 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21 +00002074 future<result_type> get_future() {return __p_.get_future();}
2075
2076 // execution
2077 void operator()(_ArgTypes... __args);
2078 void make_ready_at_thread_exit(_ArgTypes... __args);
2079
2080 void reset();
2081};
2082
2083template<class ..._ArgTypes>
2084void
2085packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args)
2086{
2087#ifndef _LIBCPP_NO_EXCEPTIONS
2088 if (__p_.__state_ == nullptr)
2089 throw future_error(make_error_code(future_errc::no_state));
2090 if (__p_.__state_->__has_value())
2091 throw future_error(make_error_code(future_errc::promise_already_satisfied));
2092 try
2093 {
2094#endif // _LIBCPP_NO_EXCEPTIONS
2095 __f_(_STD::forward<_ArgTypes>(__args)...);
2096 __p_.set_value();
2097#ifndef _LIBCPP_NO_EXCEPTIONS
2098 }
2099 catch (...)
2100 {
2101 __p_.set_exception(current_exception());
2102 }
2103#endif // _LIBCPP_NO_EXCEPTIONS
2104}
2105
2106template<class ..._ArgTypes>
2107void
2108packaged_task<void(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
2109{
2110#ifndef _LIBCPP_NO_EXCEPTIONS
2111 if (__p_.__state_ == nullptr)
2112 throw future_error(make_error_code(future_errc::no_state));
2113 if (__p_.__state_->__has_value())
2114 throw future_error(make_error_code(future_errc::promise_already_satisfied));
2115 try
2116 {
2117#endif // _LIBCPP_NO_EXCEPTIONS
2118 __f_(_STD::forward<_ArgTypes>(__args)...);
2119 __p_.set_value_at_thread_exit();
2120#ifndef _LIBCPP_NO_EXCEPTIONS
2121 }
2122 catch (...)
2123 {
2124 __p_.set_exception_at_thread_exit(current_exception());
2125 }
2126#endif // _LIBCPP_NO_EXCEPTIONS
2127}
2128
2129template<class ..._ArgTypes>
2130void
2131packaged_task<void(_ArgTypes...)>::reset()
2132{
2133#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant7de47902010-11-30 20:23:32 +00002134 if (!valid())
Howard Hinnant54da3382010-08-30 18:46:21 +00002135 throw future_error(make_error_code(future_errc::no_state));
2136#endif // _LIBCPP_NO_EXCEPTIONS
2137 __p_ = promise<result_type>();
2138}
2139
2140template <class _Callable>
2141inline _LIBCPP_INLINE_VISIBILITY
2142void
2143swap(packaged_task<_Callable>& __x, packaged_task<_Callable>& __y)
2144{
2145 __x.swap(__y);
2146}
2147
2148template <class _Callable, class _Alloc>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002149struct _LIBCPP_VISIBLE uses_allocator<packaged_task<_Callable>, _Alloc>
2150 : public true_type {};
Howard Hinnant54da3382010-08-30 18:46:21 +00002151
2152template <class _R, class _F>
2153future<_R>
Howard Hinnant73d21a42010-09-04 23:28:19 +00002154#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21 +00002155__make_deferred_assoc_state(_F&& __f)
2156#else
2157__make_deferred_assoc_state(_F __f)
2158#endif
2159{
2160 unique_ptr<__deferred_assoc_state<_R, _F>, __release_shared_count>
2161 __h(new __deferred_assoc_state<_R, _F>(_STD::forward<_F>(__f)));
2162 return future<_R>(__h.get());
2163}
2164
Howard Hinnant57cff292011-05-19 15:05:04 +00002165template <class _R, class _F>
2166future<_R>
2167#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2168__make_async_assoc_state(_F&& __f)
2169#else
2170__make_async_assoc_state(_F __f)
2171#endif
2172{
2173 unique_ptr<__async_assoc_state<_R, _F>, __release_shared_count>
2174 __h(new __async_assoc_state<_R, _F>(_STD::forward<_F>(__f)));
2175 _STD::thread(&__async_assoc_state<_R, _F>::__execute, __h.get()).detach();
2176 return future<_R>(__h.get());
2177}
2178
Howard Hinnant54da3382010-08-30 18:46:21 +00002179template <class _F, class... _Args>
Howard Hinnant57cff292011-05-19 15:05:04 +00002180class __async_func
2181{
2182 tuple<_F, _Args...> __f_;
2183
2184public:
2185 typedef typename __invoke_of<_F, _Args...>::type _R;
2186
2187 _LIBCPP_INLINE_VISIBILITY
2188 explicit __async_func(_F&& __f, _Args&&... __args)
2189 : __f_(_STD::move(__f), _STD::move(__args)...) {}
2190
2191 _LIBCPP_INLINE_VISIBILITY
2192 __async_func(__async_func&& __f) : __f_(_STD::move(__f.__f_)) {}
2193
2194 _R operator()()
2195 {
2196 typedef typename __make_tuple_indices<1+sizeof...(_Args), 1>::type _Index;
2197 return __execute(_Index());
2198 }
2199private:
2200 template <size_t ..._Indices>
2201 _R
2202 __execute(__tuple_indices<_Indices...>)
2203 {
2204 return __invoke(_STD::move(_STD::get<0>(__f_)), _STD::move(_STD::get<_Indices>(__f_))...);
2205 }
2206};
2207
2208template <class _F, class... _Args>
2209future<typename __invoke_of<typename decay<_F>::type, typename decay<_Args>::type...>::type>
Howard Hinnant54da3382010-08-30 18:46:21 +00002210async(launch __policy, _F&& __f, _Args&&... __args)
2211{
Howard Hinnant57cff292011-05-19 15:05:04 +00002212 typedef __async_func<typename decay<_F>::type, typename decay<_Args>::type...> _BF;
2213 typedef typename _BF::_R _R;
Howard Hinnant54da3382010-08-30 18:46:21 +00002214 future<_R> __r;
Howard Hinnant66895642010-11-23 18:33:54 +00002215 if (__policy & launch::async)
Howard Hinnant57cff292011-05-19 15:05:04 +00002216 __r = _STD::__make_async_assoc_state<_R>(_BF(__decay_copy(_STD::forward<_F>(__f)),
2217 __decay_copy(_STD::forward<_Args>(__args))...));
Howard Hinnant66895642010-11-23 18:33:54 +00002218 else if (__policy & launch::deferred)
Howard Hinnant57cff292011-05-19 15:05:04 +00002219 __r = _STD::__make_deferred_assoc_state<_R>(_BF(__decay_copy(_STD::forward<_F>(__f)),
2220 __decay_copy(_STD::forward<_Args>(__args))...));
Howard Hinnant54da3382010-08-30 18:46:21 +00002221 return __r;
2222}
2223
2224template <class _F, class... _Args>
2225inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57cff292011-05-19 15:05:04 +00002226future<typename __invoke_of<typename decay<_F>::type, typename decay<_Args>::type...>::type>
Howard Hinnant54da3382010-08-30 18:46:21 +00002227async(_F&& __f, _Args&&... __args)
2228{
Howard Hinnanted22f562011-05-16 16:20:59 +00002229 return _STD::async(launch::any, _STD::forward<_F>(__f),
2230 _STD::forward<_Args>(__args)...);
Howard Hinnant54da3382010-08-30 18:46:21 +00002231}
2232
2233#endif // _LIBCPP_HAS_NO_VARIADICS
2234
Howard Hinnante6e4d012010-09-03 21:46:37 +00002235// shared_future
2236
Howard Hinnant99be8232010-09-03 18:39:25 +00002237template <class _R>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002238class _LIBCPP_VISIBLE shared_future
Howard Hinnant99be8232010-09-03 18:39:25 +00002239{
2240 __assoc_state<_R>* __state_;
2241
2242public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002243 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002244 shared_future() : __state_(nullptr) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002245 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002246 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2247 {if (__state_) __state_->__add_shared();}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002248#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002249 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002250 shared_future(future<_R>&& __f) : __state_(__f.__state_)
2251 {__f.__state_ = nullptr;}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002252 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002253 shared_future(shared_future&& __rhs) : __state_(__rhs.__state_)
2254 {__rhs.__state_ = nullptr;}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002255#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:25 +00002256 ~shared_future();
2257 shared_future& operator=(const shared_future& __rhs);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002258#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002259 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002260 shared_future& operator=(shared_future&& __rhs)
2261 {
2262 shared_future(std::move(__rhs)).swap(*this);
2263 return *this;
2264 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002265#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:25 +00002266
2267 // retrieving the value
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002268 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002269 const _R& get() const {return __state_->copy();}
2270
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002271 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002272 void swap(shared_future& __rhs) {_STD::swap(__state_, __rhs.__state_);}
2273
2274 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002275 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002276 bool valid() const {return __state_ != nullptr;}
2277
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002278 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002279 void wait() const {__state_->wait();}
2280 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002281 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002282 future_status
2283 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2284 {return __state_->wait_for(__rel_time);}
2285 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002286 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002287 future_status
2288 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2289 {return __state_->wait_until(__abs_time);}
2290};
2291
2292template <class _R>
2293shared_future<_R>::~shared_future()
2294{
2295 if (__state_)
2296 __state_->__release_shared();
2297}
2298
2299template <class _R>
2300shared_future<_R>&
2301shared_future<_R>::operator=(const shared_future& __rhs)
2302{
2303 if (__rhs.__state_)
2304 __rhs.__state_->__add_shared();
2305 if (__state_)
2306 __state_->__release_shared();
2307 __state_ = __rhs.__state_;
2308 return *this;
2309}
2310
2311template <class _R>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002312class _LIBCPP_VISIBLE shared_future<_R&>
Howard Hinnant99be8232010-09-03 18:39:25 +00002313{
2314 __assoc_state<_R&>* __state_;
2315
2316public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002317 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002318 shared_future() : __state_(nullptr) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002319 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002320 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2321 {if (__state_) __state_->__add_shared();}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002322#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002323 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002324 shared_future(future<_R&>&& __f) : __state_(__f.__state_)
2325 {__f.__state_ = nullptr;}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002326 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002327 shared_future(shared_future&& __rhs) : __state_(__rhs.__state_)
2328 {__rhs.__state_ = nullptr;}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002329#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:25 +00002330 ~shared_future();
2331 shared_future& operator=(const shared_future& __rhs);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002332#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002333 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002334 shared_future& operator=(shared_future&& __rhs)
2335 {
2336 shared_future(std::move(__rhs)).swap(*this);
2337 return *this;
2338 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002339#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:25 +00002340
2341 // retrieving the value
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002342 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002343 _R& get() const {return __state_->copy();}
2344
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002345 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002346 void swap(shared_future& __rhs) {_STD::swap(__state_, __rhs.__state_);}
2347
2348 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002349 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002350 bool valid() const {return __state_ != nullptr;}
2351
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002352 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002353 void wait() const {__state_->wait();}
2354 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002355 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002356 future_status
2357 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2358 {return __state_->wait_for(__rel_time);}
2359 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002360 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002361 future_status
2362 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2363 {return __state_->wait_until(__abs_time);}
2364};
2365
2366template <class _R>
2367shared_future<_R&>::~shared_future()
2368{
2369 if (__state_)
2370 __state_->__release_shared();
2371}
2372
2373template <class _R>
2374shared_future<_R&>&
2375shared_future<_R&>::operator=(const shared_future& __rhs)
2376{
2377 if (__rhs.__state_)
2378 __rhs.__state_->__add_shared();
2379 if (__state_)
2380 __state_->__release_shared();
2381 __state_ = __rhs.__state_;
2382 return *this;
2383}
2384
2385template <>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002386class _LIBCPP_VISIBLE shared_future<void>
Howard Hinnant99be8232010-09-03 18:39:25 +00002387{
2388 __assoc_sub_state* __state_;
2389
2390public:
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002391 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002392 shared_future() : __state_(nullptr) {}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002393 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002394 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2395 {if (__state_) __state_->__add_shared();}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002396#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002397 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002398 shared_future(future<void>&& __f) : __state_(__f.__state_)
2399 {__f.__state_ = nullptr;}
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002400 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002401 shared_future(shared_future&& __rhs) : __state_(__rhs.__state_)
2402 {__rhs.__state_ = nullptr;}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002403#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:25 +00002404 ~shared_future();
2405 shared_future& operator=(const shared_future& __rhs);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002406#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002407 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002408 shared_future& operator=(shared_future&& __rhs)
2409 {
2410 shared_future(std::move(__rhs)).swap(*this);
2411 return *this;
2412 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002413#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:25 +00002414
2415 // retrieving the value
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002416 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002417 void get() const {__state_->copy();}
2418
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002419 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002420 void swap(shared_future& __rhs) {_STD::swap(__state_, __rhs.__state_);}
2421
2422 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002423 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002424 bool valid() const {return __state_ != nullptr;}
2425
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002426 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002427 void wait() const {__state_->wait();}
2428 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002429 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002430 future_status
2431 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2432 {return __state_->wait_for(__rel_time);}
2433 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:26 +00002434 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:25 +00002435 future_status
2436 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2437 {return __state_->wait_until(__abs_time);}
2438};
2439
2440template <class _R>
2441inline _LIBCPP_INLINE_VISIBILITY
2442void
2443swap(shared_future<_R>& __x, shared_future<_R>& __y)
2444{
2445 __x.swap(__y);
2446}
2447
Howard Hinnante6e4d012010-09-03 21:46:37 +00002448template <class _R>
Howard Hinnant7de47902010-11-30 20:23:32 +00002449inline _LIBCPP_INLINE_VISIBILITY
2450shared_future<_R>
2451future<_R>::share()
Howard Hinnante6e4d012010-09-03 21:46:37 +00002452{
Howard Hinnant7de47902010-11-30 20:23:32 +00002453 return shared_future<_R>(_STD::move(*this));
Howard Hinnante6e4d012010-09-03 21:46:37 +00002454}
2455
2456template <class _R>
Howard Hinnante6e4d012010-09-03 21:46:37 +00002457inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7de47902010-11-30 20:23:32 +00002458shared_future<_R&>
2459future<_R&>::share()
Howard Hinnante6e4d012010-09-03 21:46:37 +00002460{
Howard Hinnant7de47902010-11-30 20:23:32 +00002461 return shared_future<_R&>(_STD::move(*this));
2462}
2463
Howard Hinnanta4451512010-12-02 16:45:21 +00002464#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2465
Howard Hinnant7de47902010-11-30 20:23:32 +00002466inline _LIBCPP_INLINE_VISIBILITY
2467shared_future<void>
2468future<void>::share()
2469{
2470 return shared_future<void>(_STD::move(*this));
Howard Hinnante6e4d012010-09-03 21:46:37 +00002471}
2472
Howard Hinnanta4451512010-12-02 16:45:21 +00002473#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2474
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002475_LIBCPP_END_NAMESPACE_STD
2476
2477#endif // _LIBCPP_FUTURE