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