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