future: New.
[gcc.git] / libstdc++-v3 / include / std / future
1 // <future> -*- C++ -*-
2
3 // Copyright (C) 2009 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 <c++0x_warning.h>
36 #else
37
38 #include <functional>
39 #include <memory>
40 #include <mutex>
41 #include <condition_variable>
42 #include <system_error>
43 #include <exception>
44 #include <cstdatomic>
45
46 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1) \
47 && defined(_GLIBCXX_ATOMIC_BUILTINS_4)
48
49 namespace std
50 {
51 /**
52 * @defgroup futures Futures
53 * @ingroup concurrency
54 *
55 * Classes for futures support.
56 * @{
57 */
58
59 /// Error code for futures
60 enum class future_errc
61 { broken_promise, future_already_retrieved, promise_already_satisfied };
62
63 // TODO: requires concepts
64 // concept_map ErrorCodeEnum<future_errc> { }
65 template<>
66 struct is_error_code_enum<future_errc> : public true_type { };
67
68 /// Points to a statically-allocated object derived from error_category.
69 extern const error_category* const future_category;
70
71 // TODO: requires constexpr
72 inline error_code make_error_code(future_errc __errc)
73 { return error_code(static_cast<int>(__errc), *future_category); }
74
75 // TODO: requires constexpr
76 inline error_condition make_error_condition(future_errc __errc)
77 { return error_condition(static_cast<int>(__errc), *future_category); }
78
79 /// Exception type thrown by futures.
80 class future_error : public logic_error
81 {
82 public:
83 explicit future_error(future_errc __ec)
84 : logic_error("std::future_error"), _M_code(make_error_code(__ec))
85 { }
86
87 const error_code& code() const throw() { return _M_code; }
88
89 const char* what() const throw() { return _M_code.message().c_str(); }
90
91 private:
92 error_code _M_code;
93 };
94
95 // Holds the result of a future
96 struct _Future_result_base
97 {
98 _Future_result_base() = default;
99 _Future_result_base(const _Future_result_base&) = delete;
100 _Future_result_base& operator=(const _Future_result_base&) = delete;
101
102 exception_ptr _M_error;
103
104 // _M_destroy() allows derived classes to control deallocation,
105 // which will be needed when allocator support is added to promise.
106 // See http://gcc.gnu.org/ml/libstdc++/2009-06/msg00032.html
107 virtual void _M_destroy() = 0;
108 struct _Deleter
109 {
110 void operator()(_Future_result_base* __fr) const { __fr->_M_destroy(); }
111 };
112
113 protected:
114 ~_Future_result_base() = default;
115 };
116
117 // TODO: use template alias when available
118 /*
119 template<typename _Res>
120 using _Future_ptr = unique_ptr<_Res, _Future_result_base::_Deleter>;
121 */
122 template<typename _Res>
123 struct _Future_ptr
124 {
125 typedef unique_ptr<_Res, _Future_result_base::_Deleter> type;
126 };
127
128 // State shared between a promise and one or more associated futures.
129 class _Future_state
130 {
131 typedef _Future_ptr<_Future_result_base>::type _Future_ptr_type;
132
133 public:
134 _Future_state() : _M_result(), _M_retrieved(false) { }
135
136 _Future_state(const _Future_state&) = delete;
137 _Future_state& operator=(const _Future_state&) = delete;
138
139 bool
140 is_ready()
141 { return _M_get() != 0; }
142
143 bool
144 has_exception()
145 {
146 _Future_result_base* const __res = _M_get();
147 return __res && !(__res->_M_error == 0);
148 }
149
150 bool
151 has_value()
152 {
153 _Future_result_base* const __res = _M_get();
154 return __res && (__res->_M_error == 0);
155 }
156
157 _Future_result_base&
158 wait()
159 {
160 unique_lock<mutex> __lock(_M_mutex);
161 if (!_M_ready())
162 _M_cond.wait(__lock, std::bind(&_Future_state::_M_ready, this));
163 return *_M_result;
164 }
165
166 template<typename _Rep, typename _Period>
167 bool
168 wait_for(const chrono::duration<_Rep, _Period>& __rel)
169 {
170 unique_lock<mutex> __lock(_M_mutex);
171 return _M_ready() || _M_cond.wait_for(__lock, __rel,
172 std::bind(&_Future_state::_M_ready, this));
173 }
174
175 template<typename _Clock, typename _Duration>
176 bool
177 wait_until(const chrono::time_point<_Clock, _Duration>& __abs)
178 {
179 unique_lock<mutex> __lock(_M_mutex);
180 return _M_ready() || _M_cond.wait_until(__lock, __abs,
181 std::bind(&_Future_state::_M_ready, this));
182 }
183
184 void
185 _M_set_result(_Future_ptr_type __res)
186 {
187 {
188 lock_guard<mutex> __lock(_M_mutex);
189 if (_M_ready())
190 throw future_error(future_errc::promise_already_satisfied);
191 _M_result.swap(__res);
192 }
193 _M_cond.notify_all();
194 }
195
196 void
197 _M_break_promise(_Future_ptr_type __res)
198 {
199 if (static_cast<bool>(__res))
200 {
201 __res->_M_error
202 = std::copy_exception(future_error(future_errc::broken_promise));
203 {
204 lock_guard<mutex> __lock(_M_mutex);
205 _M_result.swap(__res);
206 }
207 _M_cond.notify_all();
208 }
209 }
210
211 // called when this object is passed to a unique_future
212 void
213 _M_set_retrieved_flag()
214 {
215 if (_M_retrieved.test_and_set())
216 throw future_error(future_errc::future_already_retrieved);
217 }
218
219 private:
220 _Future_result_base*
221 _M_get()
222 {
223 lock_guard<mutex> __lock(_M_mutex);
224 return _M_result.get();
225 }
226
227 bool _M_ready() const { return static_cast<bool>(_M_result); }
228
229 _Future_ptr_type _M_result;
230 mutex _M_mutex;
231 condition_variable _M_cond;
232 atomic_flag _M_retrieved;
233 };
234
235 // workaround for CWG issue 664 and c++/34022
236 template<typename _Result, bool = is_scalar<_Result>::value>
237 struct _Move_future_result
238 {
239 typedef _Result&& __rval_type;
240 static _Result&& _S_move(_Result& __res) { return std::move(__res); }
241 };
242
243 // specialization for scalar types returns rvalue not rvalue-reference
244 template<typename _Result>
245 struct _Move_future_result<_Result, true>
246 {
247 typedef _Result __rval_type;
248 static _Result _S_move(_Result __res) { return __res; }
249 };
250
251 template<typename _Result>
252 struct _Future_result : _Future_result_base
253 {
254 _Future_result() : _M_initialized() { }
255
256 ~_Future_result()
257 {
258 if (_M_initialized)
259 _M_value().~_Result();
260 }
261
262 // return lvalue, future will add const or rvalue-reference
263 _Result& _M_value()
264 { return *static_cast<_Result*>(_M_addr()); }
265
266 void
267 _M_set(const _Result& __res)
268 {
269 ::new (_M_addr()) _Result(__res);
270 _M_initialized = true;
271 }
272
273 void
274 _M_set(_Result&& __res)
275 {
276 typedef _Move_future_result<_Result> _Mover;
277 ::new (_M_addr()) _Result(_Mover::_S_move(__res));
278 _M_initialized = true;
279 }
280
281 private:
282 void _M_destroy() { delete this; }
283
284 void* _M_addr() { return static_cast<void*>(&_M_storage); }
285
286 typename aligned_storage<sizeof(_Result),
287 alignment_of<_Result>::value>::type _M_storage;
288 bool _M_initialized;
289 };
290
291 template<typename _Result>
292 struct _Future_result<_Result&> : _Future_result_base
293 {
294 _Future_result() : _M_value_ptr() { }
295
296 _Result* _M_value_ptr;
297
298 void _M_destroy() { delete this; }
299 };
300
301 template<>
302 struct _Future_result<void> : _Future_result_base
303 {
304 void _M_destroy() { delete this; }
305 };
306
307 /// unique_future
308 template<typename _Result>
309 class unique_future;
310
311 /// shared_future
312 template<typename _Result>
313 class shared_future;
314
315 // common implementation for unique_future and shared_future
316 template<typename _Result>
317 class _Future_impl
318 {
319 public:
320 // disable copying
321 _Future_impl(const _Future_impl&) = delete;
322 _Future_impl& operator=(const _Future_impl&) = delete;
323
324 // functions to check state and wait for ready
325 bool is_ready() const { return this->_M_state->is_ready(); }
326
327 bool has_exception() const { return this->_M_state->has_exception(); }
328
329 bool has_value() const { return this->_M_state->has_value(); }
330
331 void wait() const { this->_M_state->wait(); }
332
333 template<typename _Rep, typename _Period>
334 bool
335 wait_for(const chrono::duration<_Rep, _Period>& __rel) const
336 { return this->_M_state->wait_for(__rel); }
337
338 template<typename _Clock, typename _Duration>
339 bool
340 wait_until(const chrono::time_point<_Clock, _Duration>& __abs) const
341 { return this->_M_state->wait_until(__abs); }
342
343 protected:
344 // wait for the state to be ready and rethrow any stored exception
345 _Future_result<_Result>&
346 _M_get_result()
347 {
348 _Future_result_base& __res = this->_M_state->wait();
349 if (!(__res._M_error == 0))
350 rethrow_exception(__res._M_error);
351 return static_cast<_Future_result<_Result>&>(__res);
352 }
353
354 typedef shared_ptr<_Future_state> _State_ptr;
355
356 // construction of a unique_future by promise::get_future()
357 explicit
358 _Future_impl(const _State_ptr& __state)
359 : _M_state(__state)
360 {
361 if (static_cast<bool>(this->_M_state))
362 this->_M_state->_M_set_retrieved_flag();
363 else
364 throw future_error(future_errc::future_already_retrieved);
365 }
366
367 // copy construction from a shared_future
368 explicit
369 _Future_impl(const shared_future<_Result>&);
370
371 // move construction from a unique_future
372 explicit
373 _Future_impl(unique_future<_Result>&&);
374
375 _State_ptr _M_state;
376 };
377
378 /// promise
379 template<typename _Result>
380 class promise;
381
382 // primary template for unique_future
383 template<typename _Result>
384 class unique_future : public _Future_impl<_Result>
385 {
386 typedef _Move_future_result<_Result> _Mover;
387
388 public:
389 /// Move constructor
390 unique_future(unique_future&& __uf) : _Base_type(std::move(__uf)) { }
391
392 // disable copying
393 unique_future(const unique_future&) = delete;
394 unique_future& operator=(const unique_future&) = delete;
395
396 // retrieving the value
397 typename _Mover::__rval_type
398 get()
399 { return _Mover::_S_move(this->_M_get_result()._M_value()); }
400
401 private:
402 typedef _Future_impl<_Result> _Base_type;
403 typedef typename _Base_type::_State_ptr _State_ptr;
404
405 friend class promise<_Result>;
406
407 explicit
408 unique_future(const _State_ptr& __state) : _Base_type(__state) { }
409 };
410
411 // partial specialization for unique_future<R&>
412 template<typename _Result>
413 class unique_future<_Result&> : public _Future_impl<_Result&>
414 {
415 public:
416 /// Move constructor
417 unique_future(unique_future&& __uf) : _Base_type(std::move(__uf)) { }
418
419 // disable copying
420 unique_future(const unique_future&) = delete;
421 unique_future& operator=(const unique_future&) = delete;
422
423 // retrieving the value
424 _Result& get() { return *this->_M_get_result()._M_value_ptr; }
425
426 private:
427 typedef _Future_impl<_Result&> _Base_type;
428 typedef typename _Base_type::_State_ptr _State_ptr;
429
430 friend class promise<_Result&>;
431
432 explicit
433 unique_future(const _State_ptr& __state) : _Base_type(__state) { }
434 };
435
436 // specialization for unique_future<void>
437 template<>
438 class unique_future<void> : public _Future_impl<void>
439 {
440 public:
441 /// Move constructor
442 unique_future(unique_future&& __uf) : _Base_type(std::move(__uf)) { }
443
444 // disable copying
445 unique_future(const unique_future&) = delete;
446 unique_future& operator=(const unique_future&) = delete;
447
448 // retrieving the value
449 void get() { this->_M_get_result(); }
450
451 private:
452 typedef _Future_impl<void> _Base_type;
453 typedef _Base_type::_State_ptr _State_ptr;
454
455 friend class promise<void>;
456
457 explicit
458 unique_future(const _State_ptr& __state) : _Base_type(__state) { }
459 };
460
461 // primary template for unique_future
462 template<typename _Result>
463 class shared_future : public _Future_impl<_Result>
464 {
465 public:
466 /// Copy constructor
467 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
468
469 /// Construct from a unique_future rvalue
470 shared_future(unique_future<_Result>&& __uf)
471 : _Base_type(std::move(__uf))
472 { }
473
474 shared_future& operator=(const shared_future&) = delete;
475
476 // retrieving the value
477 const _Result&
478 get()
479 { return this->_M_get_result()._M_value(); }
480
481 private:
482 typedef _Future_impl<_Result> _Base_type;
483 };
484
485 // partial specialization for shared_future<R&>
486 template<typename _Result>
487 class shared_future<_Result&> : public _Future_impl<_Result&>
488 {
489 public:
490 /// Copy constructor
491 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
492
493 /// Construct from a unique_future rvalue
494 shared_future(unique_future<_Result&>&& __uf)
495 : _Base_type(std::move(__uf))
496 { }
497
498 shared_future& operator=(const shared_future&) = delete;
499
500 // retrieving the value
501 _Result& get() { return *this->_M_get_result()._M_value_ptr; }
502
503 private:
504 typedef _Future_impl<_Result&> _Base_type;
505 };
506
507 // specialization for shared_future<void>
508 template<>
509 class shared_future<void> : public _Future_impl<void>
510 {
511 public:
512 /// Copy constructor
513 shared_future(const shared_future& __sf) : _Base_type(__sf) { }
514
515 /// Construct from a unique_future rvalue
516 shared_future(unique_future<void>&& __uf)
517 : _Base_type(std::move(__uf))
518 { }
519
520 shared_future& operator=(const shared_future&) = delete;
521
522 // retrieving the value
523 void get() { this->_M_get_result(); }
524
525 private:
526 typedef _Future_impl<void> _Base_type;
527 };
528
529 // now we can define the protected _Future_impl constructors
530
531 template<typename _Result>
532 _Future_impl<_Result>::_Future_impl(const shared_future<_Result>& __sf)
533 : _M_state(__sf._M_state)
534 { }
535
536 template<typename _Result>
537 _Future_impl<_Result>::_Future_impl(unique_future<_Result>&& __uf)
538 : _M_state(std::move(__uf._M_state))
539 { }
540
541 template<typename> class packaged_task; // undefined
542
543 // primary template for promise
544 template<typename _Result>
545 class promise
546 {
547 public:
548 promise()
549 : _M_future(std::make_shared<_Future_state>()),
550 _M_storage(new _Future_result<_Result>())
551 { }
552
553 promise(promise&& __rhs)
554 : _M_future(std::move(__rhs._M_future)),
555 _M_storage(std::move(__rhs._M_storage))
556 { }
557
558 // TODO: requires allocator concepts
559 /*
560 template<typename _Allocator>
561 promise(allocator_arg_t, const _Allocator& __a);
562
563 template<typename _Allocator>
564 promise(allocator_arg_t, const _Allocator&, promise&& __rhs);
565 */
566
567 promise(const promise&) = delete;
568
569 ~promise()
570 {
571 if (static_cast<bool>(_M_future) && !_M_future.unique())
572 _M_future->_M_break_promise(std::move(_M_storage));
573 }
574
575 // assignment
576 promise&
577 operator=(promise&& __rhs)
578 {
579 promise(std::move(__rhs)).swap(*this);
580 return *this;
581 }
582
583 promise& operator=(const promise&) = delete;
584
585 void
586 swap(promise& __rhs)
587 {
588 _M_future.swap(__rhs._M_future);
589 _M_storage.swap(__rhs._M_storage);
590 }
591
592 // retrieving the result
593 unique_future<_Result>
594 get_future()
595 { return unique_future<_Result>(_M_future); }
596
597 // setting the result
598 void
599 set_value(const _Result& __r)
600 {
601 if (!_M_satisfied())
602 _M_storage->_M_set(__r);
603 _M_future->_M_set_result(std::move(_M_storage));
604 }
605
606 void
607 set_value(_Result&& __r)
608 {
609 if (!_M_satisfied())
610 _M_storage->_M_set(_Mover::_S_move(__r));
611 _M_future->_M_set_result(std::move(_M_storage));
612 }
613
614 void
615 set_exception(exception_ptr __p)
616 {
617 if (!_M_satisfied())
618 _M_storage->_M_error = __p;
619 _M_future->_M_set_result(std::move(_M_storage));
620 }
621
622 private:
623 template<typename> friend class packaged_task;
624 typedef _Move_future_result<_Result> _Mover;
625 bool _M_satisfied() { return !static_cast<bool>(_M_storage); }
626 shared_ptr<_Future_state> _M_future;
627 typename _Future_ptr<_Future_result<_Result>>::type _M_storage;
628 };
629
630 // partial specialization for promise<R&>
631 template<typename _Result>
632 class promise<_Result&>
633 {
634 public:
635 promise()
636 : _M_future(std::make_shared<_Future_state>()),
637 _M_storage(new _Future_result<_Result&>())
638 { }
639
640 promise(promise&& __rhs)
641 : _M_future(std::move(__rhs._M_future)),
642 _M_storage(std::move(__rhs._M_storage))
643 { }
644
645 // TODO: requires allocator concepts
646 /*
647 template<typename _Allocator>
648 promise(allocator_arg_t, const _Allocator& __a);
649
650 template<typename _Allocator>
651 promise(allocator_arg_t, const _Allocator&, promise&& __rhs);
652 */
653
654 promise(const promise&) = delete;
655
656 ~promise()
657 {
658 if (static_cast<bool>(_M_future) && !_M_future.unique())
659 _M_future->_M_break_promise(std::move(_M_storage));
660 }
661
662 // assignment
663 promise&
664 operator=(promise&& __rhs)
665 {
666 promise(std::move(__rhs)).swap(*this);
667 return *this;
668 }
669
670 promise& operator=(const promise&) = delete;
671
672 void
673 swap(promise& __rhs)
674 {
675 _M_future.swap(__rhs._M_future);
676 _M_storage.swap(__rhs._M_storage);
677 }
678
679 // retrieving the result
680 unique_future<_Result&>
681 get_future()
682 { return unique_future<_Result&>(_M_future); }
683
684 // setting the result
685 void
686 set_value(_Result& __r)
687 {
688 if (!_M_satisfied())
689 _M_storage->_M_value_ptr = &__r;
690 _M_future->_M_set_result(std::move(_M_storage));
691 }
692
693 void
694 set_exception(exception_ptr __p)
695 {
696 if (!_M_satisfied())
697 _M_storage->_M_error = __p;
698 _M_future->_M_set_result(std::move(_M_storage));
699 }
700
701 private:
702 template<typename> friend class packaged_task;
703 bool _M_satisfied() { return !static_cast<bool>(_M_storage); }
704 shared_ptr<_Future_state> _M_future;
705 typename _Future_ptr<_Future_result<_Result&>>::type _M_storage;
706 };
707
708 // specialization for promise<void>
709 template<>
710 class promise<void>
711 {
712 public:
713 promise()
714 : _M_future(std::make_shared<_Future_state>()),
715 _M_storage(new _Future_result<void>())
716 { }
717
718 promise(promise&& __rhs)
719 : _M_future(std::move(__rhs._M_future)),
720 _M_storage(std::move(__rhs._M_storage))
721 { }
722
723 // TODO: requires allocator concepts
724 /*
725 template<typename _Allocator>
726 promise(allocator_arg_t, const _Allocator& __a);
727
728 template<typename _Allocator>
729 promise(allocator_arg_t, const _Allocator&, promise&& __rhs);
730 */
731
732 promise(const promise&) = delete;
733
734 ~promise()
735 {
736 if (static_cast<bool>(_M_future) && !_M_future.unique())
737 _M_future->_M_break_promise(std::move(_M_storage));
738 }
739
740 // assignment
741 promise&
742 operator=(promise&& __rhs)
743 {
744 promise(std::move(__rhs)).swap(*this);
745 return *this;
746 }
747
748 promise& operator=(const promise&) = delete;
749
750 void
751 swap(promise& __rhs)
752 {
753 _M_future.swap(__rhs._M_future);
754 _M_storage.swap(__rhs._M_storage);
755 }
756
757 // retrieving the result
758 unique_future<void>
759 get_future()
760 { return unique_future<void>(_M_future); }
761
762 // setting the result
763 void
764 set_value()
765 {
766 _M_future->_M_set_result(std::move(_M_storage));
767 }
768
769 void
770 set_exception(exception_ptr __p)
771 {
772 if (!_M_satisfied())
773 _M_storage->_M_error = __p;
774 _M_future->_M_set_result(std::move(_M_storage));
775 }
776
777 private:
778 template<typename> friend class packaged_task;
779 bool _M_satisfied() { return !static_cast<bool>(_M_storage); }
780 shared_ptr<_Future_state> _M_future;
781 _Future_ptr<_Future_result<void>>::type _M_storage;
782 };
783
784 // TODO: requires allocator concepts
785 /*
786 template<typename _Result, class Alloc>
787 concept_map UsesAllocator<promise<_Result>, Alloc>
788 {
789 typedef Alloc allocator_type;
790 }
791 */
792
793 template<typename _Result, typename... _ArgTypes>
794 struct _Run_task
795 {
796 static void
797 _S_run(promise<_Result>& __p, function<_Result(_ArgTypes...)>& __f,
798 _ArgTypes... __args)
799 {
800 __p.set_value(__f(std::forward<_ArgTypes>(__args)...));
801 }
802 };
803
804 // specialization used by packaged_task<void(...)>
805 template<typename... _ArgTypes>
806 struct _Run_task<void, _ArgTypes...>
807 {
808 static void
809 _S_run(promise<void>& __p, function<void(_ArgTypes...)>& __f,
810 _ArgTypes... __args)
811 {
812 __f(std::forward<_ArgTypes>(__args)...);
813 __p.set_value();
814 }
815 };
816
817 template<typename _Result, typename... _ArgTypes>
818 class packaged_task<_Result(_ArgTypes...)>
819 {
820 public:
821 typedef _Result result_type;
822
823 // construction and destruction
824 packaged_task() { }
825
826 template<typename _Fn>
827 explicit
828 packaged_task(const _Fn& __fn) : _M_task(__fn) { }
829
830 template<typename _Fn>
831 explicit
832 packaged_task(_Fn&& __fn) : _M_task(std::move(__fn)) { }
833
834 explicit
835 packaged_task(_Result(*__fn)(_ArgTypes...)) : _M_task(__fn) { }
836
837 // TODO: requires allocator concepts
838 /*
839 template<typename _Fn, typename _Allocator>
840 explicit
841 packaged_task(allocator_arg_t __tag, const _Allocator& __a, _Fn __fn)
842 : _M_task(__tag, __a, __fn), _M_promise(__tag, __a)
843 { }
844
845 template<typename _Fn, typename _Allocator>
846 explicit
847 packaged_task(allocator_arg_t __tag, const _Allocator& __a, _Fn&& __fn)
848 : _M_task(__tag, __a, std::move(__fn)), _M_promise(__tag, __a)
849 { }
850 */
851
852 ~packaged_task() = default;
853
854 // no copy
855 packaged_task(packaged_task&) = delete;
856 packaged_task& operator=(packaged_task&) = delete;
857
858 // move support
859 packaged_task(packaged_task&& __other)
860 { this->swap(__other); }
861
862 packaged_task& operator=(packaged_task&& __other)
863 {
864 packaged_task(std::move(__other)).swap(*this);
865 return *this;
866 }
867
868 void
869 swap(packaged_task& __other)
870 {
871 _M_task.swap(__other._M_task);
872 _M_promise.swap(__other._M_promise);
873 }
874
875 explicit operator bool() const { return static_cast<bool>(_M_task); }
876
877 // result retrieval
878 unique_future<_Result>
879 get_future()
880 {
881 try
882 {
883 return _M_promise.get_future();
884 }
885 catch (const future_error& __e)
886 {
887 if (__e.code() == future_errc::future_already_retrieved)
888 throw std::bad_function_call();
889 throw;
890 }
891 }
892
893 // execution
894 void
895 operator()(_ArgTypes... __args)
896 {
897 if (!static_cast<bool>(_M_task) || _M_promise._M_satisfied())
898 throw std::bad_function_call();
899 try
900 {
901 _Run_task<_Result, _ArgTypes...>::_S_run(_M_promise, _M_task,
902 std::forward<_ArgTypes>(__args)...);
903 }
904 catch (...)
905 {
906 _M_promise.set_exception(current_exception());
907 }
908 }
909
910 void reset() { promise<_Result>().swap(_M_promise); }
911
912 private:
913 function<_Result(_ArgTypes...)> _M_task;
914 promise<_Result> _M_promise;
915 };
916
917 // @} group futures
918 }
919
920 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
921 // && _GLIBCXX_ATOMIC_BUILTINS_4
922
923 #endif // __GXX_EXPERIMENTAL_CXX0X__
924
925 #endif // _GLIBCXX_FUTURE