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