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