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