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