]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/std/future
*: Use headername alias to associate private includes to public includes.
[thirdparty/gcc.git] / libstdc++-v3 / include / std / future
1 // <future> -*- C++ -*-
2
3 // Copyright (C) 2009, 2010 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24
25 /** @file include/future
26 * This is a Standard C++ Library header.
27 */
28
29 #ifndef _GLIBCXX_FUTURE
30 #define _GLIBCXX_FUTURE 1
31
32 #pragma GCC system_header
33
34 #ifndef __GXX_EXPERIMENTAL_CXX0X__
35 # include <bits/c++0x_warning.h>
36 #else
37
38 #include <functional>
39 #include <memory>
40 #include <mutex>
41 #include <thread>
42 #include <condition_variable>
43 #include <system_error>
44 #include <exception>
45 #include <atomic>
46 #include <bits/functexcept.h>
47
48 _GLIBCXX_BEGIN_NAMESPACE(std)
49
50 /**
51 * @defgroup futures Futures
52 * @ingroup concurrency
53 *
54 * Classes for futures support.
55 * @{
56 */
57
58 /// Error code for futures
59 enum class future_errc
60 {
61 broken_promise,
62 future_already_retrieved,
63 promise_already_satisfied,
64 no_state
65 };
66
67 /// Specialization.
68 template<>
69 struct is_error_code_enum<future_errc> : public true_type { };
70
71 /// Points to a statically-allocated object derived from error_category.
72 const error_category&
73 future_category();
74
75 /// Overload for make_error_code.
76 inline error_code
77 make_error_code(future_errc __errc)
78 { return error_code(static_cast<int>(__errc), future_category()); }
79
80 /// Overload for make_error_condition.
81 inline error_condition
82 make_error_condition(future_errc __errc)
83 { return error_condition(static_cast<int>(__errc), future_category()); }
84
85 /**
86 * @brief Exception type thrown by futures.
87 * @ingroup exceptions
88 */
89 class future_error : public logic_error
90 {
91 error_code _M_code;
92
93 public:
94 explicit future_error(error_code __ec)
95 : logic_error("std::future_error"), _M_code(__ec)
96 { }
97
98 virtual ~future_error() throw();
99
100 virtual const char*
101 what() const throw();
102
103 const error_code&
104 code() const throw() { return _M_code; }
105 };
106
107 // Forward declarations.
108 template<typename _Res>
109 class future;
110
111 template<typename _Res>
112 class shared_future;
113
114 template<typename _Res>
115 class atomic_future;
116
117 template<typename _Signature>
118 class packaged_task;
119
120 template<typename _Res>
121 class promise;
122
123 /// Launch code for futures
124 enum class launch
125 {
126 any,
127 async,
128 sync
129 };
130
131 /// Status code for futures
132 enum class future_status
133 {
134 ready,
135 timeout,
136 deferred
137 };
138
139 template<typename _Fn, typename... _Args>
140 future<typename result_of<_Fn(_Args...)>::type>
141 async(launch __policy, _Fn&& __fn, _Args&&... __args);
142
143 template<typename _Fn, typename... _Args>
144 typename
145 enable_if<!is_same<typename decay<_Fn>::type, launch>::value,
146 future<decltype(std::declval<_Fn>()(std::declval<_Args>()...))>
147 >::type
148 async(_Fn&& __fn, _Args&&... __args);
149
150 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1) \
151 && defined(_GLIBCXX_ATOMIC_BUILTINS_4)
152
153 /// Base class and enclosing scope.
154 struct __future_base
155 {
156 /// Base class for results.
157 struct _Result_base
158 {
159 exception_ptr _M_error;
160
161 _Result_base() = default;
162 _Result_base(const _Result_base&) = delete;
163 _Result_base& operator=(const _Result_base&) = delete;
164
165 // _M_destroy() allows derived classes to control deallocation
166 virtual void _M_destroy() = 0;
167
168 struct _Deleter
169 {
170 void operator()(_Result_base* __fr) const { __fr->_M_destroy(); }
171 };
172
173 protected:
174 ~_Result_base();
175 };
176
177 /// Result.
178 template<typename _Res>
179 struct _Result : _Result_base
180 {
181 private:
182 typedef alignment_of<_Res> __a_of;
183 typedef aligned_storage<sizeof(_Res), __a_of::value> __align_storage;
184 typedef typename __align_storage::type __align_type;
185
186 __align_type _M_storage;
187 bool _M_initialized;
188
189 public:
190 _Result() : _M_initialized() { }
191
192 ~_Result()
193 {
194 if (_M_initialized)
195 _M_value().~_Res();
196 }
197
198 // Return lvalue, future will add const or rvalue-reference
199 _Res&
200 _M_value() { return *static_cast<_Res*>(_M_addr()); }
201
202 void
203 _M_set(const _Res& __res)
204 {
205 ::new (_M_addr()) _Res(__res);
206 _M_initialized = true;
207 }
208
209 void
210 _M_set(_Res&& __res)
211 {
212 ::new (_M_addr()) _Res(std::move(__res));
213 _M_initialized = true;
214 }
215
216 private:
217 void _M_destroy() { delete this; }
218
219 void* _M_addr() { return static_cast<void*>(&_M_storage); }
220 };
221
222 // TODO: use template alias when available
223 /*
224 template<typename _Res>
225 using _Ptr = unique_ptr<_Res, _Result_base::_Deleter>;
226 */
227 /// A unique_ptr based on the instantiating type.
228 template<typename _Res>
229 struct _Ptr
230 {
231 typedef unique_ptr<_Res, _Result_base::_Deleter> type;
232 };
233
234 /// Result_alloc.
235 template<typename _Res, typename _Alloc>
236 struct _Result_alloc : _Result<_Res>, _Alloc
237 {
238 typedef typename _Alloc::template rebind<_Result_alloc>::other
239 __allocator_type;
240
241 explicit
242 _Result_alloc(const _Alloc& __a) : _Result<_Res>(), _Alloc(__a)
243 { }
244
245 private:
246 void _M_destroy()
247 {
248 __allocator_type __a(*this);
249 __a.destroy(this);
250 __a.deallocate(this, 1);
251 }
252 };
253
254 template<typename _Res, typename _Allocator>
255 static typename _Ptr<_Result_alloc<_Res, _Allocator>>::type
256 _S_allocate_result(const _Allocator& __a)
257 {
258 typedef _Result_alloc<_Res, _Allocator> __result_type;
259 typename __result_type::__allocator_type __a2(__a);
260 __result_type* __p = __a2.allocate(1);
261 __try
262 {
263 __a2.construct(__p, __a);
264 }
265 __catch(...)
266 {
267 __a2.deallocate(__p, 1);
268 __throw_exception_again;
269 }
270 return typename _Ptr<__result_type>::type(__p);
271 }
272
273
274 /// Shared state between a promise and one or more associated futures.
275 class _State
276 {
277 typedef _Ptr<_Result_base>::type _Ptr_type;
278
279 _Ptr_type _M_result;
280 mutex _M_mutex;
281 condition_variable _M_cond;
282 atomic_flag _M_retrieved;
283 once_flag _M_once;
284
285 public:
286 _State() : _M_result(), _M_retrieved(ATOMIC_FLAG_INIT) { }
287
288 _State(const _State&) = delete;
289 _State& operator=(const _State&) = delete;
290
291 _Result_base&
292 wait()
293 {
294 _M_run_deferred();
295 unique_lock<mutex> __lock(_M_mutex);
296 if (!_M_ready())
297 _M_cond.wait(__lock, std::bind<bool>(&_State::_M_ready, this));
298 return *_M_result;
299 }
300
301 template<typename _Rep, typename _Period>
302 bool
303 wait_for(const chrono::duration<_Rep, _Period>& __rel)
304 {
305 unique_lock<mutex> __lock(_M_mutex);
306 auto __bound = std::bind<bool>(&_State::_M_ready, this);
307 return _M_ready() || _M_cond.wait_for(__lock, __rel, __bound);
308 }
309
310 template<typename _Clock, typename _Duration>
311 bool
312 wait_until(const chrono::time_point<_Clock, _Duration>& __abs)
313 {
314 unique_lock<mutex> __lock(_M_mutex);
315 auto __bound = std::bind<bool>(&_State::_M_ready, this);
316 return _M_ready() || _M_cond.wait_until(__lock, __abs, __bound);
317 }
318
319 void
320 _M_set_result(function<_Ptr_type()> __res, bool __ignore_failure = false)
321 {
322 bool __set = __ignore_failure;
323 // all calls to this function are serialized,
324 // side-effects of invoking __res only happen once
325 call_once(_M_once, &_State::_M_do_set, this, ref(__res),
326 ref(__set));
327 if (!__set)
328 __throw_future_error(int(future_errc::promise_already_satisfied));
329 }
330
331 void
332 _M_break_promise(_Ptr_type __res)
333 {
334 if (static_cast<bool>(__res))
335 {
336 error_code __ec(make_error_code(future_errc::broken_promise));
337 __res->_M_error = copy_exception(future_error(__ec));
338 {
339 lock_guard<mutex> __lock(_M_mutex);
340 _M_result.swap(__res);
341 }
342 _M_cond.notify_all();
343 }
344 }
345
346 // Called when this object is passed to a future.
347 void
348 _M_set_retrieved_flag()
349 {
350 if (_M_retrieved.test_and_set())
351 __throw_future_error(int(future_errc::future_already_retrieved));
352 }
353
354 template<typename _Res, typename _Arg>
355 struct _Setter;
356
357 // set lvalues
358 template<typename _Res, typename _Arg>
359 struct _Setter<_Res, _Arg&>
360 {
361 // check this is only used by promise<R>::set_value(const R&)
362 // or promise<R>::set_value(R&)
363 static_assert(is_same<_Res, _Arg&>::value // promise<R&>
364 || is_same<const _Res, _Arg>::value, // promise<R>
365 "Invalid specialisation");
366
367 typename promise<_Res>::_Ptr_type operator()()
368 {
369 _State::_S_check(_M_promise->_M_future);
370 _M_promise->_M_storage->_M_set(_M_arg);
371 return std::move(_M_promise->_M_storage);
372 }
373 promise<_Res>* _M_promise;
374 _Arg& _M_arg;
375 };
376
377 // set rvalues
378 template<typename _Res>
379 struct _Setter<_Res, _Res&&>
380 {
381 typename promise<_Res>::_Ptr_type operator()()
382 {
383 _State::_S_check(_M_promise->_M_future);
384 _M_promise->_M_storage->_M_set(std::move(_M_arg));
385 return std::move(_M_promise->_M_storage);
386 }
387 promise<_Res>* _M_promise;
388 _Res& _M_arg;
389 };
390
391 struct __exception_ptr_tag { };
392
393 // set exceptions
394 template<typename _Res>
395 struct _Setter<_Res, __exception_ptr_tag>
396 {
397 typename promise<_Res>::_Ptr_type operator()()
398 {
399 _State::_S_check(_M_promise->_M_future);
400 _M_promise->_M_storage->_M_error = _M_ex;
401 return std::move(_M_promise->_M_storage);
402 }
403
404 promise<_Res>* _M_promise;
405 exception_ptr& _M_ex;
406 };
407
408 template<typename _Res, typename _Arg>
409 static _Setter<_Res, _Arg&&>
410 __setter(promise<_Res>* __prom, _Arg&& __arg)
411 {
412 return _Setter<_Res, _Arg&&>{ __prom, __arg };
413 }
414
415 template<typename _Res>
416 static _Setter<_Res, __exception_ptr_tag>
417 __setter(exception_ptr& __ex, promise<_Res>* __prom)
418 {
419 return _Setter<_Res, __exception_ptr_tag>{ __prom, __ex };
420 }
421
422 static _Setter<void, void>
423 __setter(promise<void>* __prom);
424
425 template<typename _Tp>
426 static bool
427 _S_check(const shared_ptr<_Tp>& __p)
428 {
429 if (!static_cast<bool>(__p))
430 __throw_future_error((int)future_errc::no_state);
431 }
432
433 private:
434 void
435 _M_do_set(function<_Ptr_type()>& __f, bool& __set)
436 {
437 _Ptr_type __res = __f();
438 {
439 lock_guard<mutex> __lock(_M_mutex);
440 _M_result.swap(__res);
441 }
442 _M_cond.notify_all();
443 __set = true;
444 }
445
446 bool _M_ready() const { return static_cast<bool>(_M_result); }
447
448 virtual void _M_run_deferred() { }
449 };
450
451 template<typename _Res>
452 class _Deferred_state;
453
454 template<typename _Res>
455 class _Async_state;
456
457 template<typename _Signature>
458 class _Task_state;
459
460 template<typename _StateT, typename _Res = typename _StateT::_Res_type>
461 struct _Task_setter;
462 };
463
464 inline __future_base::_Result_base::~_Result_base() = default;
465
466 /// Partial specialization for reference types.
467 template<typename _Res>
468 struct __future_base::_Result<_Res&> : __future_base::_Result_base
469 {
470 _Result() : _M_value_ptr() { }
471
472 void _M_set(_Res& __res) { _M_value_ptr = &__res; }
473
474 _Res& _M_get() { return *_M_value_ptr; }
475
476 private:
477 _Res* _M_value_ptr;
478
479 void _M_destroy() { delete this; }
480 };
481
482 /// Explicit specialization for void.
483 template<>
484 struct __future_base::_Result<void> : __future_base::_Result_base
485 {
486 private:
487 void _M_destroy() { delete this; }
488 };
489
490
491 /// Common implementation for future and shared_future.
492 template<typename _Res>
493 class __basic_future : public __future_base
494 {
495 protected:
496 typedef shared_ptr<_State> __state_type;
497 typedef __future_base::_Result<_Res>& __result_type;
498
499 private:
500 __state_type _M_state;
501
502 public:
503 // Disable copying.
504 __basic_future(const __basic_future&) = delete;
505 __basic_future& operator=(const __basic_future&) = delete;
506
507 bool
508 valid() const { return static_cast<bool>(_M_state); }
509
510 void
511 wait() const
512 {
513 _State::_S_check(_M_state);
514 _M_state->wait();
515 }
516
517 template<typename _Rep, typename _Period>
518 bool
519 wait_for(const chrono::duration<_Rep, _Period>& __rel) const
520 {
521 _State::_S_check(_M_state);
522 return _M_state->wait_for(__rel);
523 }
524
525 template<typename _Clock, typename _Duration>
526 bool
527 wait_until(const chrono::time_point<_Clock, _Duration>& __abs) const
528 {
529 _State::_S_check(_M_state);
530 return _M_state->wait_until(__abs);
531 }
532
533 protected:
534 /// Wait for the state to be ready and rethrow any stored exception
535 __result_type
536 _M_get_result()
537 {
538 _State::_S_check(_M_state);
539 _Result_base& __res = _M_state->wait();
540 if (!(__res._M_error == 0))
541 rethrow_exception(__res._M_error);
542 return static_cast<__result_type>(__res);
543 }
544
545 void _M_swap(__basic_future& __that)
546 {
547 _M_state.swap(__that._M_state);
548 }
549
550 // Construction of a future by promise::get_future()
551 explicit
552 __basic_future(const __state_type& __state) : _M_state(__state)
553 {
554 _State::_S_check(_M_state);
555 _M_state->_M_set_retrieved_flag();
556 }
557
558 // Copy construction from a shared_future
559 explicit
560 __basic_future(const shared_future<_Res>&);
561
562 // Move construction from a shared_future
563 explicit
564 __basic_future(shared_future<_Res>&&);
565
566 // Move construction from a future
567 explicit
568 __basic_future(future<_Res>&&);
569
570 constexpr __basic_future() : _M_state() { }
571
572 struct _Reset
573 {
574 explicit _Reset(__basic_future& __fut) : _M_fut(__fut) { }
575 ~_Reset() { _M_fut._M_state.reset(); }
576 __basic_future& _M_fut;
577 };
578 };
579
580
581 /// Primary template for future.
582 template<typename _Res>
583 class future : public __basic_future<_Res>
584 {
585 friend class promise<_Res>;
586 template<typename> friend class packaged_task;
587 template<typename _Fn, typename... _Args>
588 friend future<typename result_of<_Fn(_Args...)>::type>
589 async(launch, _Fn&&, _Args&&...);
590
591 typedef __basic_future<_Res> _Base_type;
592 typedef typename _Base_type::__state_type __state_type;
593
594 explicit
595 future(const __state_type& __state) : _Base_type(__state) { }
596
597 public:
598 constexpr future() : _Base_type() { }
599
600 /// Move constructor
601 future(future&& __uf) : _Base_type(std::move(__uf)) { }
602
603 // Disable copying
604 future(const future&) = delete;
605 future& operator=(const future&) = delete;
606
607 future& operator=(future&& __fut)
608 {
609 future(std::move(__fut))._M_swap(*this);
610 return *this;
611 }
612
613 /// Retrieving the value
614 _Res
615 get()
616 {
617 typename _Base_type::_Reset __reset(*this);
618 return std::move(this->_M_get_result()._M_value());
619 }
620 };
621
622 /// Partial specialization for future<R&>
623 template<typename _Res>
624 class future<_Res&> : public __basic_future<_Res&>
625 {
626 friend class promise<_Res&>;
627 template<typename> friend class packaged_task;
628 template<typename _Fn, typename... _Args>
629 friend future<typename result_of<_Fn(_Args...)>::type>
630 async(launch, _Fn&&, _Args&&...);
631
632 typedef __basic_future<_Res&> _Base_type;
633 typedef typename _Base_type::__state_type __state_type;
634
635 explicit
636 future(const __state_type& __state) : _Base_type(__state) { }
637
638 public:
639 constexpr future() : _Base_type() { }
640
641 /// Move constructor
642 future(future&& __uf) : _Base_type(std::move(__uf)) { }
643
644 // Disable copying
645 future(const future&) = delete;
646 future& operator=(const future&) = delete;
647
648 future& operator=(future&& __fut)
649 {
650 future(std::move(__fut))._M_swap(*this);
651 return *this;
652 }
653
654 /// Retrieving the value
655 _Res&
656 get()
657 {
658 typename _Base_type::_Reset __reset(*this);
659 return this->_M_get_result()._M_get();
660 }
661 };
662
663 /// Explicit specialization for future<void>
664 template<>
665 class future<void> : public __basic_future<void>
666 {
667 friend class promise<void>;
668 template<typename> friend class packaged_task;
669 template<typename _Fn, typename... _Args>
670 friend future<typename result_of<_Fn(_Args...)>::type>
671 async(launch, _Fn&&, _Args&&...);
672
673 typedef __basic_future<void> _Base_type;
674 typedef typename _Base_type::__state_type __state_type;
675
676 explicit
677 future(const __state_type& __state) : _Base_type(__state) { }
678
679 public:
680 constexpr future() : _Base_type() { }
681
682 /// Move constructor
683 future(future&& __uf) : _Base_type(std::move(__uf)) { }
684
685 // Disable copying
686 future(const future&) = delete;
687 future& operator=(const future&) = delete;
688
689 future& operator=(future&& __fut)
690 {
691 future(std::move(__fut))._M_swap(*this);
692 return *this;
693 }
694
695 /// Retrieving the value
696 void
697 get()
698 {
699 typename _Base_type::_Reset __reset(*this);
700 this->_M_get_result();
701 }
702 };
703
704
705 /// Primary template for shared_future.
706 template<typename _Res>
707 class shared_future : public __basic_future<_Res>
708 {
709 typedef __basic_future<_Res> _Base_type;
710
711 public:
712 constexpr shared_future() : _Base_type() { }
713
714 /// Copy constructor
715 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
716
717 /// Construct from a future rvalue
718 shared_future(future<_Res>&& __uf)
719 : _Base_type(std::move(__uf))
720 { }
721
722 /// Construct from a shared_future rvalue
723 shared_future(shared_future&& __sf)
724 : _Base_type(std::move(__sf))
725 { }
726
727 shared_future& operator=(const shared_future& __sf)
728 {
729 shared_future(__sf)._M_swap(*this);
730 return *this;
731 }
732
733 shared_future& operator=(shared_future&& __sf)
734 {
735 shared_future(std::move(__sf))._M_swap(*this);
736 return *this;
737 }
738
739 /// Retrieving the value
740 const _Res&
741 get()
742 {
743 typename _Base_type::__result_type __r = this->_M_get_result();
744 _Res& __rs(__r._M_value());
745 return __rs;
746 }
747 };
748
749 /// Partial specialization for shared_future<R&>
750 template<typename _Res>
751 class shared_future<_Res&> : public __basic_future<_Res&>
752 {
753 typedef __basic_future<_Res&> _Base_type;
754
755 public:
756 constexpr shared_future() : _Base_type() { }
757
758 /// Copy constructor
759 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
760
761 /// Construct from a future rvalue
762 shared_future(future<_Res&>&& __uf)
763 : _Base_type(std::move(__uf))
764 { }
765
766 /// Construct from a shared_future rvalue
767 shared_future(shared_future&& __sf)
768 : _Base_type(std::move(__sf))
769 { }
770
771 shared_future& operator=(const shared_future& __sf)
772 {
773 shared_future(__sf)._M_swap(*this);
774 return *this;
775 }
776
777 shared_future& operator=(shared_future&& __sf)
778 {
779 shared_future(std::move(__sf))._M_swap(*this);
780 return *this;
781 }
782
783 /// Retrieving the value
784 _Res&
785 get() { return this->_M_get_result()._M_get(); }
786 };
787
788 /// Explicit specialization for shared_future<void>
789 template<>
790 class shared_future<void> : public __basic_future<void>
791 {
792 typedef __basic_future<void> _Base_type;
793
794 public:
795 constexpr shared_future() : _Base_type() { }
796
797 /// Copy constructor
798 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
799
800 /// Construct from a future rvalue
801 shared_future(future<void>&& __uf)
802 : _Base_type(std::move(__uf))
803 { }
804
805 /// Construct from a shared_future rvalue
806 shared_future(shared_future&& __sf)
807 : _Base_type(std::move(__sf))
808 { }
809
810 shared_future& operator=(const shared_future& __sf)
811 {
812 shared_future(__sf)._M_swap(*this);
813 return *this;
814 }
815
816 shared_future& operator=(shared_future&& __sf)
817 {
818 shared_future(std::move(__sf))._M_swap(*this);
819 return *this;
820 }
821
822 // Retrieving the value
823 void
824 get() { this->_M_get_result(); }
825 };
826
827 // Now we can define the protected __basic_future constructors.
828 template<typename _Res>
829 inline __basic_future<_Res>::
830 __basic_future(const shared_future<_Res>& __sf)
831 : _M_state(__sf._M_state)
832 { }
833
834 template<typename _Res>
835 inline __basic_future<_Res>::
836 __basic_future(shared_future<_Res>&& __sf)
837 : _M_state(std::move(__sf._M_state))
838 { }
839
840 template<typename _Res>
841 inline __basic_future<_Res>::
842 __basic_future(future<_Res>&& __uf)
843 : _M_state(std::move(__uf._M_state))
844 { }
845
846
847 /// Primary template for promise
848 template<typename _Res>
849 class promise
850 {
851 typedef __future_base::_State _State;
852 typedef __future_base::_Result<_Res> _Res_type;
853 typedef typename __future_base::_Ptr<_Res_type>::type _Ptr_type;
854 template<typename, typename> friend class _State::_Setter;
855
856 shared_ptr<_State> _M_future;
857 _Ptr_type _M_storage;
858
859 public:
860 promise()
861 : _M_future(std::make_shared<_State>()),
862 _M_storage(new _Res_type())
863 { }
864
865 promise(promise&& __rhs)
866 : _M_future(std::move(__rhs._M_future)),
867 _M_storage(std::move(__rhs._M_storage))
868 { }
869
870 template<typename _Allocator>
871 promise(allocator_arg_t, const _Allocator& __a)
872 : _M_future(std::allocate_shared<_State>(__a)),
873 _M_storage(__future_base::_S_allocate_result<_Res>(__a))
874 { }
875
876 promise(const promise&) = delete;
877
878 ~promise()
879 {
880 if (static_cast<bool>(_M_future) && !_M_future.unique())
881 _M_future->_M_break_promise(std::move(_M_storage));
882 }
883
884 // Assignment
885 promise&
886 operator=(promise&& __rhs)
887 {
888 promise(std::move(__rhs)).swap(*this);
889 return *this;
890 }
891
892 promise& operator=(const promise&) = delete;
893
894 void
895 swap(promise& __rhs)
896 {
897 _M_future.swap(__rhs._M_future);
898 _M_storage.swap(__rhs._M_storage);
899 }
900
901 // Retrieving the result
902 future<_Res>
903 get_future()
904 { return future<_Res>(_M_future); }
905
906 // Setting the result
907 void
908 set_value(const _Res& __r)
909 {
910 auto __setter = _State::__setter(this, __r);
911 _M_future->_M_set_result(std::move(__setter));
912 }
913
914 void
915 set_value(_Res&& __r)
916 {
917 auto __setter = _State::__setter(this, std::move(__r));
918 _M_future->_M_set_result(std::move(__setter));
919 }
920
921 void
922 set_exception(exception_ptr __p)
923 {
924 auto __setter = _State::__setter(__p, this);
925 _M_future->_M_set_result(std::move(__setter));
926 }
927 };
928
929 template<typename _Res>
930 inline void
931 swap(promise<_Res>& __x, promise<_Res>& __y)
932 { __x.swap(__y); }
933
934 template<typename _Res, typename _Alloc>
935 struct uses_allocator<promise<_Res>, _Alloc>
936 : public true_type { };
937
938
939 /// Partial specialization for promise<R&>
940 template<typename _Res>
941 class promise<_Res&>
942 {
943 typedef __future_base::_State _State;
944 typedef __future_base::_Result<_Res&> _Res_type;
945 typedef typename __future_base::_Ptr<_Res_type>::type _Ptr_type;
946 template<typename, typename> friend class _State::_Setter;
947
948 shared_ptr<_State> _M_future;
949 _Ptr_type _M_storage;
950
951 public:
952 promise()
953 : _M_future(std::make_shared<_State>()),
954 _M_storage(new _Res_type())
955 { }
956
957 promise(promise&& __rhs)
958 : _M_future(std::move(__rhs._M_future)),
959 _M_storage(std::move(__rhs._M_storage))
960 { }
961
962 template<typename _Allocator>
963 promise(allocator_arg_t, const _Allocator& __a)
964 : _M_future(std::allocate_shared<_State>(__a)),
965 _M_storage(__future_base::_S_allocate_result<_Res&>(__a))
966 { }
967
968 promise(const promise&) = delete;
969
970 ~promise()
971 {
972 if (static_cast<bool>(_M_future) && !_M_future.unique())
973 _M_future->_M_break_promise(std::move(_M_storage));
974 }
975
976 // Assignment
977 promise&
978 operator=(promise&& __rhs)
979 {
980 promise(std::move(__rhs)).swap(*this);
981 return *this;
982 }
983
984 promise& operator=(const promise&) = delete;
985
986 void
987 swap(promise& __rhs)
988 {
989 _M_future.swap(__rhs._M_future);
990 _M_storage.swap(__rhs._M_storage);
991 }
992
993 // Retrieving the result
994 future<_Res&>
995 get_future()
996 { return future<_Res&>(_M_future); }
997
998 // Setting the result
999 void
1000 set_value(_Res& __r)
1001 {
1002 auto __setter = _State::__setter(this, __r);
1003 _M_future->_M_set_result(std::move(__setter));
1004 }
1005
1006 void
1007 set_exception(exception_ptr __p)
1008 {
1009 auto __setter = _State::__setter(__p, this);
1010 _M_future->_M_set_result(std::move(__setter));
1011 }
1012 };
1013
1014 /// Explicit specialization for promise<void>
1015 template<>
1016 class promise<void>
1017 {
1018 typedef __future_base::_State _State;
1019 typedef __future_base::_Result<void> _Res_type;
1020 typedef typename __future_base::_Ptr<_Res_type>::type _Ptr_type;
1021 template<typename, typename> friend class _State::_Setter;
1022
1023 shared_ptr<_State> _M_future;
1024 _Ptr_type _M_storage;
1025
1026 public:
1027 promise()
1028 : _M_future(std::make_shared<_State>()),
1029 _M_storage(new _Res_type())
1030 { }
1031
1032 promise(promise&& __rhs)
1033 : _M_future(std::move(__rhs._M_future)),
1034 _M_storage(std::move(__rhs._M_storage))
1035 { }
1036
1037 template<typename _Allocator>
1038 promise(allocator_arg_t, const _Allocator& __a)
1039 : _M_future(std::allocate_shared<_State>(__a)),
1040 _M_storage(__future_base::_S_allocate_result<void>(__a))
1041 { }
1042
1043 promise(const promise&) = delete;
1044
1045 ~promise()
1046 {
1047 if (static_cast<bool>(_M_future) && !_M_future.unique())
1048 _M_future->_M_break_promise(std::move(_M_storage));
1049 }
1050
1051 // Assignment
1052 promise&
1053 operator=(promise&& __rhs)
1054 {
1055 promise(std::move(__rhs)).swap(*this);
1056 return *this;
1057 }
1058
1059 promise& operator=(const promise&) = delete;
1060
1061 void
1062 swap(promise& __rhs)
1063 {
1064 _M_future.swap(__rhs._M_future);
1065 _M_storage.swap(__rhs._M_storage);
1066 }
1067
1068 // Retrieving the result
1069 future<void>
1070 get_future()
1071 { return future<void>(_M_future); }
1072
1073 // Setting the result
1074 void set_value();
1075
1076 void
1077 set_exception(exception_ptr __p)
1078 {
1079 auto __setter = _State::__setter(__p, this);
1080 _M_future->_M_set_result(std::move(__setter));
1081 }
1082 };
1083
1084 // set void
1085 template<>
1086 struct __future_base::_State::_Setter<void, void>
1087 {
1088 promise<void>::_Ptr_type operator()()
1089 {
1090 _State::_S_check(_M_promise->_M_future);
1091 return std::move(_M_promise->_M_storage);
1092 }
1093
1094 promise<void>* _M_promise;
1095 };
1096
1097 inline __future_base::_State::_Setter<void, void>
1098 __future_base::_State::__setter(promise<void>* __prom)
1099 {
1100 return _Setter<void, void>{ __prom };
1101 }
1102
1103 inline void
1104 promise<void>::set_value()
1105 {
1106 auto __setter = _State::__setter(this);
1107 _M_future->_M_set_result(std::move(__setter));
1108 }
1109
1110
1111 template<typename _StateT, typename _Res>
1112 struct __future_base::_Task_setter
1113 {
1114 typename _StateT::_Ptr_type operator()()
1115 {
1116 __try
1117 {
1118 _M_state->_M_result->_M_set(_M_fn());
1119 }
1120 __catch(...)
1121 {
1122 _M_state->_M_result->_M_error = current_exception();
1123 }
1124 return std::move(_M_state->_M_result);
1125 }
1126 _StateT* _M_state;
1127 std::function<_Res()> _M_fn;
1128 };
1129
1130 template<typename _StateT>
1131 struct __future_base::_Task_setter<_StateT, void>
1132 {
1133 typename _StateT::_Ptr_type operator()()
1134 {
1135 __try
1136 {
1137 _M_fn();
1138 }
1139 __catch(...)
1140 {
1141 _M_state->_M_result->_M_error = current_exception();
1142 }
1143 return std::move(_M_state->_M_result);
1144 }
1145 _StateT* _M_state;
1146 std::function<void()> _M_fn;
1147 };
1148
1149 template<typename _Res, typename... _Args>
1150 struct __future_base::_Task_state<_Res(_Args...)> : __future_base::_State
1151 {
1152 typedef _Res _Res_type;
1153
1154 _Task_state(std::function<_Res(_Args...)> __task)
1155 : _M_result(new _Result<_Res>()), _M_task(std::move(__task))
1156 { }
1157
1158 template<typename _Func, typename _Alloc>
1159 _Task_state(_Func&& __task, const _Alloc& __a)
1160 : _M_result(_S_allocate_result<_Res>(__a)),
1161 _M_task(allocator_arg, __a, std::move(__task))
1162 { }
1163
1164 void
1165 _M_run(_Args... __args)
1166 {
1167 // bound arguments decay so wrap lvalue references
1168 auto __bound = std::bind<_Res>(std::ref(_M_task),
1169 _S_maybe_wrap_ref(std::forward<_Args>(__args))...);
1170 _Task_setter<_Task_state> __setter{ this, std::move(__bound) };
1171 _M_set_result(std::move(__setter));
1172 }
1173
1174 template<typename, typename> friend class _Task_setter;
1175 typedef typename __future_base::_Ptr<_Result<_Res>>::type _Ptr_type;
1176 _Ptr_type _M_result;
1177 std::function<_Res(_Args...)> _M_task;
1178
1179 template<typename _Tp>
1180 static reference_wrapper<_Tp>
1181 _S_maybe_wrap_ref(_Tp& __t)
1182 { return std::ref(__t); }
1183
1184 template<typename _Tp>
1185 static typename enable_if<!is_lvalue_reference<_Tp>::value,
1186 _Tp>::type&&
1187 _S_maybe_wrap_ref(_Tp&& __t)
1188 { return std::forward<_Tp>(__t); }
1189 };
1190
1191 /// packaged_task
1192 template<typename _Res, typename... _ArgTypes>
1193 class packaged_task<_Res(_ArgTypes...)>
1194 {
1195 typedef __future_base::_Task_state<_Res(_ArgTypes...)> _State_type;
1196 shared_ptr<_State_type> _M_state;
1197
1198 public:
1199 typedef _Res result_type;
1200
1201 // Construction and destruction
1202 packaged_task() { }
1203
1204 template<typename _Fn>
1205 explicit
1206 packaged_task(const _Fn& __fn)
1207 : _M_state(std::make_shared<_State_type>(__fn))
1208 { }
1209
1210 template<typename _Fn>
1211 explicit
1212 packaged_task(_Fn&& __fn)
1213 : _M_state(std::make_shared<_State_type>(std::move(__fn)))
1214 { }
1215
1216 explicit
1217 packaged_task(_Res(*__fn)(_ArgTypes...))
1218 : _M_state(std::make_shared<_State_type>(__fn))
1219 { }
1220
1221 template<typename _Fn, typename _Allocator>
1222 explicit
1223 packaged_task(allocator_arg_t __tag, const _Allocator& __a, _Fn __fn)
1224 : _M_state(std::allocate_shared<_State_type>(__a, std::move(__fn)))
1225 { }
1226
1227 ~packaged_task()
1228 {
1229 if (static_cast<bool>(_M_state) && !_M_state.unique())
1230 _M_state->_M_break_promise(std::move(_M_state->_M_result));
1231 }
1232
1233 // No copy
1234 packaged_task(packaged_task&) = delete;
1235 packaged_task& operator=(packaged_task&) = delete;
1236
1237 // Move support
1238 packaged_task(packaged_task&& __other)
1239 { this->swap(__other); }
1240
1241 packaged_task& operator=(packaged_task&& __other)
1242 {
1243 packaged_task(std::move(__other)).swap(*this);
1244 return *this;
1245 }
1246
1247 void
1248 swap(packaged_task& __other)
1249 { _M_state.swap(__other._M_state); }
1250
1251 explicit operator bool() const { return static_cast<bool>(_M_state); }
1252
1253 // Result retrieval
1254 future<_Res>
1255 get_future()
1256 { return future<_Res>(_M_state); }
1257
1258 // Execution
1259 void
1260 operator()(_ArgTypes... __args)
1261 {
1262 __future_base::_State::_S_check(_M_state);
1263 _M_state->_M_run(std::forward<_ArgTypes>(__args)...);
1264 }
1265
1266 void
1267 reset()
1268 {
1269 __future_base::_State::_S_check(_M_state);
1270 packaged_task(std::move(_M_state->_M_task)).swap(*this);
1271 }
1272 };
1273
1274 /// swap
1275 template<typename _Res, typename... _ArgTypes>
1276 inline void
1277 swap(packaged_task<_Res(_ArgTypes...)>& __x,
1278 packaged_task<_Res(_ArgTypes...)>& __y)
1279 { __x.swap(__y); }
1280
1281 template<typename _Res, typename _Alloc>
1282 struct uses_allocator<packaged_task<_Res>, _Alloc>
1283 : public true_type { };
1284
1285
1286 template<typename _Res>
1287 class __future_base::_Deferred_state : public __future_base::_State
1288 {
1289 public:
1290 typedef _Res _Res_type;
1291
1292 explicit
1293 _Deferred_state(std::function<_Res()>&& __fn)
1294 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
1295 { }
1296
1297 private:
1298 template<typename, typename> friend class _Task_setter;
1299 typedef typename __future_base::_Ptr<_Result<_Res>>::type _Ptr_type;
1300 _Ptr_type _M_result;
1301 std::function<_Res()> _M_fn;
1302
1303 virtual void
1304 _M_run_deferred()
1305 {
1306 _Task_setter<_Deferred_state> __setter{ this, _M_fn };
1307 // safe to call multiple times so ignore failure
1308 _M_set_result(std::move(__setter), true);
1309 }
1310 };
1311
1312 template<typename _Res>
1313 class __future_base::_Async_state : public __future_base::_State
1314 {
1315 public:
1316 typedef _Res _Res_type;
1317
1318 explicit
1319 _Async_state(std::function<_Res()>&& __fn)
1320 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn)),
1321 _M_thread(mem_fn(&_Async_state::_M_do_run), this)
1322 { }
1323
1324 ~_Async_state() { _M_thread.join(); }
1325
1326 private:
1327 void _M_do_run()
1328 {
1329 _Task_setter<_Async_state> __setter{ this, std::move(_M_fn) };
1330 _M_set_result(std::move(__setter));
1331 }
1332
1333 template<typename, typename> friend class _Task_setter;
1334 typedef typename __future_base::_Ptr<_Result<_Res>>::type _Ptr_type;
1335 _Ptr_type _M_result;
1336 std::function<_Res()> _M_fn;
1337 thread _M_thread;
1338 };
1339
1340 /// async
1341 template<typename _Fn, typename... _Args>
1342 future<typename result_of<_Fn(_Args...)>::type>
1343 async(launch __policy, _Fn&& __fn, _Args&&... __args)
1344 {
1345 typedef typename result_of<_Fn(_Args...)>::type result_type;
1346 std::shared_ptr<__future_base::_State> __state;
1347 if (__policy == launch::async)
1348 {
1349 typedef typename __future_base::_Async_state<result_type> _State;
1350 __state = std::make_shared<_State>(std::bind<result_type>(
1351 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1352 }
1353 else
1354 {
1355 typedef typename __future_base::_Deferred_state<result_type> _State;
1356 __state = std::make_shared<_State>(std::bind<result_type>(
1357 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1358 }
1359 return future<result_type>(__state);
1360 }
1361
1362 /// async, potential overload
1363 template<typename _Fn, typename... _Args>
1364 inline typename
1365 enable_if<!is_same<typename decay<_Fn>::type, launch>::value,
1366 future<decltype(std::declval<_Fn>()(std::declval<_Args>()...))>
1367 >::type
1368 async(_Fn&& __fn, _Args&&... __args)
1369 {
1370 return async(launch::any, std::forward<_Fn>(__fn),
1371 std::forward<_Args>(__args)...);
1372 }
1373
1374 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
1375 // && _GLIBCXX_ATOMIC_BUILTINS_4
1376
1377 // @} group futures
1378 _GLIBCXX_END_NAMESPACE
1379
1380 #endif // __GXX_EXPERIMENTAL_CXX0X__
1381
1382 #endif // _GLIBCXX_FUTURE