OSDN Git Service

2009-06-24 Jonathan Wakely <jwakely.gcc@gmail.com>
[pf3gnuchains/gcc-fork.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   // Forward declarations.
96   template<typename _Result>
97     class unique_future;
98
99   template<typename _Result>
100     class shared_future;
101
102   template<typename> 
103     class packaged_task;
104
105   template<typename _Result>
106     class promise;
107
108   // Holds the result of a future
109   struct _Future_result_base
110   {
111     _Future_result_base() = default;
112     _Future_result_base(const _Future_result_base&) = delete;
113     _Future_result_base& operator=(const _Future_result_base&) = delete;
114
115     exception_ptr _M_error;
116
117     // _M_destroy() allows derived classes to control deallocation,
118     // which will be needed when allocator support is added to promise.
119     // See http://gcc.gnu.org/ml/libstdc++/2009-06/msg00032.html
120     virtual void _M_destroy() = 0;
121     struct _Deleter
122     {
123       void operator()(_Future_result_base* __fr) const { __fr->_M_destroy(); }
124     };
125
126   protected:
127     ~_Future_result_base() = default;
128   };
129
130   // TODO: use template alias when available
131   /*
132    template<typename _Res>
133      using _Future_ptr = unique_ptr<_Res, _Future_result_base::_Deleter>;
134    */
135   template<typename _Res>
136     struct _Future_ptr
137     {
138       typedef unique_ptr<_Res, _Future_result_base::_Deleter> type;
139     };
140
141   // State shared between a promise and one or more associated futures.
142   class _Future_state
143   {
144     typedef _Future_ptr<_Future_result_base>::type _Future_ptr_type;
145
146   public:
147     _Future_state() : _M_result(), _M_retrieved(false) { }
148
149     _Future_state(const _Future_state&) = delete;
150     _Future_state& operator=(const _Future_state&) = delete;
151
152     bool
153     is_ready()
154     { return _M_get() != 0; }
155
156     bool
157     has_exception()
158     {
159       _Future_result_base* const __res = _M_get();
160       return __res && !(__res->_M_error == 0);
161     }
162
163     bool
164     has_value()
165     {
166       _Future_result_base* const __res = _M_get();
167       return __res && (__res->_M_error == 0);
168     }
169
170     _Future_result_base&
171     wait()
172     {
173       unique_lock<mutex> __lock(_M_mutex);
174       if (!_M_ready())
175         _M_cond.wait(__lock, std::bind(&_Future_state::_M_ready, this));
176       return *_M_result;
177     }
178
179     template<typename _Rep, typename _Period>
180       bool
181       wait_for(const chrono::duration<_Rep, _Period>& __rel)
182       {
183         unique_lock<mutex> __lock(_M_mutex);
184         return _M_ready() || _M_cond.wait_for(__lock, __rel,
185             std::bind(&_Future_state::_M_ready, this));
186       }
187
188     template<typename _Clock, typename _Duration>
189       bool
190       wait_until(const chrono::time_point<_Clock, _Duration>& __abs)
191       {
192         unique_lock<mutex> __lock(_M_mutex);
193         return _M_ready() || _M_cond.wait_until(__lock, __abs,
194             std::bind(&_Future_state::_M_ready, this));
195       }
196
197     void
198     _M_set_result(_Future_ptr_type __res)
199     {
200       {
201         lock_guard<mutex> __lock(_M_mutex);
202         if (_M_ready())
203           throw future_error(future_errc::promise_already_satisfied);
204         _M_result.swap(__res);
205       }
206       _M_cond.notify_all();
207     }
208
209     void
210     _M_break_promise(_Future_ptr_type __res)
211     {
212       if (static_cast<bool>(__res))
213       {
214         __res->_M_error
215           = std::copy_exception(future_error(future_errc::broken_promise));
216         {
217           lock_guard<mutex> __lock(_M_mutex);
218           _M_result.swap(__res);
219         }
220         _M_cond.notify_all();
221       }
222     }
223
224     // called when this object is passed to a unique_future
225     void
226     _M_set_retrieved_flag()
227     {
228       if (_M_retrieved.test_and_set())
229         throw future_error(future_errc::future_already_retrieved);
230     }
231
232   private:
233     _Future_result_base*
234     _M_get()
235     {
236       lock_guard<mutex> __lock(_M_mutex);
237       return _M_result.get();
238     }
239
240     bool _M_ready() const { return static_cast<bool>(_M_result); }
241
242     _Future_ptr_type    _M_result;
243     mutex               _M_mutex;
244     condition_variable  _M_cond;
245     atomic_flag         _M_retrieved;
246   };
247
248   // workaround for CWG issue 664 and c++/34022
249   template<typename _Result, bool = is_scalar<_Result>::value>
250     struct _Move_future_result
251     {
252       typedef _Result&& __rval_type;
253       static _Result&& _S_move(_Result& __res) { return std::move(__res); }
254     };
255
256   // specialization for scalar types returns rvalue not rvalue-reference
257   template<typename _Result>
258     struct _Move_future_result<_Result, true>
259     {
260       typedef _Result __rval_type;
261       static _Result _S_move(_Result __res) { return __res; }
262     };
263
264   template<typename _Result>
265     struct _Future_result : _Future_result_base
266     {
267       _Future_result() : _M_initialized() { }
268
269       ~_Future_result()
270       {
271         if (_M_initialized)
272           _M_value().~_Result();
273       }
274
275       // return lvalue, future will add const or rvalue-reference
276       _Result& _M_value()
277       { return *static_cast<_Result*>(_M_addr()); }
278
279       void
280       _M_set(const _Result& __res)
281       {
282         ::new (_M_addr()) _Result(__res);
283         _M_initialized = true;
284       }
285
286       void
287       _M_set(_Result&& __res)
288       {
289         typedef _Move_future_result<_Result> _Mover;
290         ::new (_M_addr()) _Result(_Mover::_S_move(__res));
291         _M_initialized = true;
292       }
293
294     private:
295       void _M_destroy() { delete this; }
296
297       void* _M_addr() { return static_cast<void*>(&_M_storage); }
298
299       typename aligned_storage<sizeof(_Result),
300                alignment_of<_Result>::value>::type _M_storage;
301       bool _M_initialized;
302     };
303
304   template<typename _Result>
305     struct _Future_result<_Result&> : _Future_result_base
306     {
307       _Future_result() : _M_value_ptr() { }
308
309       _Result* _M_value_ptr;
310
311       void _M_destroy() { delete this; }
312     };
313
314   template<>
315     struct _Future_result<void> : _Future_result_base
316     {
317       void _M_destroy() { delete this; }
318     };
319
320   // common implementation for unique_future and shared_future
321   template<typename _Result>
322     class _Future_impl
323     {
324     public:
325       // disable copying
326       _Future_impl(const _Future_impl&) = delete;
327       _Future_impl& operator=(const _Future_impl&) = delete;
328
329       // functions to check state and wait for ready
330       bool is_ready() const { return this->_M_state->is_ready(); }
331
332       bool has_exception() const { return this->_M_state->has_exception(); }
333
334       bool has_value() const { return this->_M_state->has_value(); }
335
336       void wait() const { this->_M_state->wait(); }
337
338       template<typename _Rep, typename _Period>
339         bool
340         wait_for(const chrono::duration<_Rep, _Period>& __rel) const
341         { return this->_M_state->wait_for(__rel); }
342
343       template<typename _Clock, typename _Duration>
344         bool
345         wait_until(const chrono::time_point<_Clock, _Duration>& __abs) const
346         { return this->_M_state->wait_until(__abs); }
347
348     protected:
349       // wait for the state to be ready and rethrow any stored exception
350       _Future_result<_Result>&
351       _M_get_result()
352       {
353         _Future_result_base& __res = this->_M_state->wait();
354         if (!(__res._M_error == 0))
355           rethrow_exception(__res._M_error);
356         return static_cast<_Future_result<_Result>&>(__res);
357       }
358
359       typedef shared_ptr<_Future_state> _State_ptr;
360
361       // construction of a unique_future by promise::get_future()
362       explicit
363       _Future_impl(const _State_ptr& __state)
364       : _M_state(__state)
365       {
366         if (static_cast<bool>(this->_M_state))
367           this->_M_state->_M_set_retrieved_flag();
368         else
369           throw future_error(future_errc::future_already_retrieved);
370       }
371
372       // copy construction from a shared_future
373       explicit
374       _Future_impl(const shared_future<_Result>&);
375
376       // move construction from a unique_future
377       explicit
378       _Future_impl(unique_future<_Result>&&);
379
380       _State_ptr _M_state;
381     };
382
383   /// primary template for unique_future
384   template<typename _Result>
385     class unique_future : public _Future_impl<_Result>
386     {
387       typedef _Move_future_result<_Result> _Mover;
388
389     public:
390       /// Move constructor
391       unique_future(unique_future&& __uf) : _Base_type(std::move(__uf)) { }
392
393       // disable copying
394       unique_future(const unique_future&) = delete;
395       unique_future& operator=(const unique_future&) = delete;
396
397       // retrieving the value
398       typename _Mover::__rval_type
399       get()
400       { return _Mover::_S_move(this->_M_get_result()._M_value()); }
401
402     private:
403       typedef _Future_impl<_Result> _Base_type;
404       typedef typename _Base_type::_State_ptr _State_ptr;
405
406       friend class promise<_Result>;
407
408       explicit
409       unique_future(const _State_ptr& __state) : _Base_type(__state) { }
410     };
411  
412   // partial specialization for unique_future<R&>
413   template<typename _Result>
414     class unique_future<_Result&> : public _Future_impl<_Result&>
415     {
416     public:
417       /// Move constructor
418       unique_future(unique_future&& __uf) : _Base_type(std::move(__uf)) { }
419
420       // disable copying
421       unique_future(const unique_future&) = delete;
422       unique_future& operator=(const unique_future&) = delete;
423
424       // retrieving the value
425       _Result& get() { return *this->_M_get_result()._M_value_ptr; }
426
427     private:
428       typedef _Future_impl<_Result&>           _Base_type;
429       typedef typename _Base_type::_State_ptr _State_ptr;
430
431       friend class promise<_Result&>;
432
433       explicit
434       unique_future(const _State_ptr& __state) : _Base_type(__state) { }
435     };
436
437   // specialization for unique_future<void>
438   template<>
439     class unique_future<void> : public _Future_impl<void>
440     {
441     public:
442       /// Move constructor
443       unique_future(unique_future&& __uf) : _Base_type(std::move(__uf)) { }
444
445       // disable copying
446       unique_future(const unique_future&) = delete;
447       unique_future& operator=(const unique_future&) = delete;
448
449       // retrieving the value
450       void get() { this->_M_get_result(); }
451
452     private:
453       typedef _Future_impl<void> _Base_type;
454       typedef _Base_type::_State_ptr _State_ptr;
455
456       friend class promise<void>;
457
458       explicit
459       unique_future(const _State_ptr& __state) : _Base_type(__state) { }
460     };
461
462   /// primary template for shared_future
463   template<typename _Result>
464     class shared_future : public _Future_impl<_Result>
465     {
466     public:
467       /// Copy constructor
468       shared_future(const shared_future& __sf) : _Base_type(__sf) { }
469
470       /// Construct from a unique_future rvalue
471       shared_future(unique_future<_Result>&& __uf)
472       : _Base_type(std::move(__uf))
473       { }
474
475       shared_future& operator=(const shared_future&) = delete;
476
477       // retrieving the value
478       const _Result&
479       get()
480       { return this->_M_get_result()._M_value(); }
481
482     private:
483       typedef _Future_impl<_Result> _Base_type;
484     };
485  
486   // partial specialization for shared_future<R&>
487   template<typename _Result>
488     class shared_future<_Result&> : public _Future_impl<_Result&>
489     {
490     public:
491       /// Copy constructor
492       shared_future(const shared_future& __sf) : _Base_type(__sf) { }
493
494       /// Construct from a unique_future rvalue
495       shared_future(unique_future<_Result&>&& __uf)
496       : _Base_type(std::move(__uf))
497       { }
498
499       shared_future& operator=(const shared_future&) = delete;
500
501       // retrieving the value
502       _Result& get() { return *this->_M_get_result()._M_value_ptr; }
503
504     private:
505       typedef _Future_impl<_Result&>           _Base_type;
506     };
507
508   // specialization for shared_future<void>
509   template<>
510     class shared_future<void> : public _Future_impl<void>
511     {
512     public:
513       /// Copy constructor
514       shared_future(const shared_future& __sf) : _Base_type(__sf) { }
515
516       /// Construct from a unique_future rvalue
517       shared_future(unique_future<void>&& __uf)
518       : _Base_type(std::move(__uf))
519       { }
520
521       shared_future& operator=(const shared_future&) = delete;
522
523       // retrieving the value
524       void get() { this->_M_get_result(); }
525
526     private:
527       typedef _Future_impl<void> _Base_type;
528     };
529
530   // now we can define the protected _Future_impl constructors
531
532   template<typename _Result>
533     _Future_impl<_Result>::_Future_impl(const shared_future<_Result>& __sf)
534     : _M_state(__sf._M_state)
535     { }
536
537   template<typename _Result>
538     _Future_impl<_Result>::_Future_impl(unique_future<_Result>&& __uf)
539     : _M_state(std::move(__uf._M_state))
540     { }
541
542   /// primary template for promise
543   template<typename _Result>
544     class promise
545     {
546     public:
547       promise()
548       : _M_future(std::make_shared<_Future_state>()),
549       _M_storage(new _Future_result<_Result>())
550       { }
551
552       promise(promise&& __rhs)
553       : _M_future(std::move(__rhs._M_future)),
554       _M_storage(std::move(__rhs._M_storage))
555       { }
556
557       // TODO: requires allocator concepts
558       /*
559       template<typename _Allocator>
560         promise(allocator_arg_t, const _Allocator& __a);
561
562       template<typename _Allocator>
563         promise(allocator_arg_t, const _Allocator&, promise&& __rhs);
564        */
565
566       promise(const promise&) = delete;
567
568       ~promise()
569       {
570         if (static_cast<bool>(_M_future) && !_M_future.unique())
571           _M_future->_M_break_promise(std::move(_M_storage));
572       }
573
574       // assignment
575       promise&
576       operator=(promise&& __rhs)
577       {
578         promise(std::move(__rhs)).swap(*this);
579         return *this;
580       }
581
582       promise& operator=(const promise&) = delete;
583
584       void
585       swap(promise& __rhs)
586       {
587         _M_future.swap(__rhs._M_future);
588         _M_storage.swap(__rhs._M_storage);
589       }
590
591       // retrieving the result
592       unique_future<_Result>
593       get_future()
594       { return unique_future<_Result>(_M_future); }
595
596       // setting the result
597       void
598       set_value(const _Result& __r)
599       {
600         if (!_M_satisfied())
601           _M_storage->_M_set(__r);
602         _M_future->_M_set_result(std::move(_M_storage));
603       }
604
605       void
606       set_value(_Result&& __r)
607       {
608         if (!_M_satisfied())
609           _M_storage->_M_set(_Mover::_S_move(__r));
610         _M_future->_M_set_result(std::move(_M_storage));
611       }
612
613       void
614       set_exception(exception_ptr __p)
615       {
616         if (!_M_satisfied())
617           _M_storage->_M_error = __p;
618         _M_future->_M_set_result(std::move(_M_storage));
619       }
620
621     private:
622       template<typename> friend class packaged_task;
623       typedef _Move_future_result<_Result> _Mover;
624       bool _M_satisfied() { return !static_cast<bool>(_M_storage); }
625       shared_ptr<_Future_state>                           _M_future;
626       typename _Future_ptr<_Future_result<_Result>>::type _M_storage;
627     };
628
629   // partial specialization for promise<R&>
630   template<typename _Result>
631     class promise<_Result&>
632     {
633     public:
634       promise()
635       : _M_future(std::make_shared<_Future_state>()),
636       _M_storage(new _Future_result<_Result&>())
637       { }
638
639       promise(promise&& __rhs)
640       : _M_future(std::move(__rhs._M_future)),
641       _M_storage(std::move(__rhs._M_storage))
642       { }
643
644       // TODO: requires allocator concepts
645       /*
646       template<typename _Allocator>
647         promise(allocator_arg_t, const _Allocator& __a);
648
649       template<typename _Allocator>
650         promise(allocator_arg_t, const _Allocator&, promise&& __rhs);
651        */
652
653       promise(const promise&) = delete;
654
655       ~promise()
656       {
657         if (static_cast<bool>(_M_future) && !_M_future.unique())
658           _M_future->_M_break_promise(std::move(_M_storage));
659       }
660
661       // assignment
662       promise&
663       operator=(promise&& __rhs)
664       {
665         promise(std::move(__rhs)).swap(*this);
666         return *this;
667       }
668
669       promise& operator=(const promise&) = delete;
670
671       void
672       swap(promise& __rhs)
673       {
674         _M_future.swap(__rhs._M_future);
675         _M_storage.swap(__rhs._M_storage);
676       }
677
678       // retrieving the result
679       unique_future<_Result&>
680       get_future()
681       { return unique_future<_Result&>(_M_future); }
682
683       // setting the result
684       void
685       set_value(_Result& __r)
686       {
687         if (!_M_satisfied())
688           _M_storage->_M_value_ptr = &__r;
689         _M_future->_M_set_result(std::move(_M_storage));
690       }
691
692       void
693       set_exception(exception_ptr __p)
694       {
695         if (!_M_satisfied())
696           _M_storage->_M_error = __p;
697         _M_future->_M_set_result(std::move(_M_storage));
698       }
699
700     private:
701       template<typename> friend class packaged_task;
702       bool _M_satisfied() { return !static_cast<bool>(_M_storage); }
703       shared_ptr<_Future_state>                             _M_future;
704       typename _Future_ptr<_Future_result<_Result&>>::type  _M_storage;
705     };
706
707   // specialization for promise<void>
708   template<>
709     class promise<void>
710     {
711     public:
712       promise()
713       : _M_future(std::make_shared<_Future_state>()),
714       _M_storage(new _Future_result<void>())
715       { }
716
717       promise(promise&& __rhs)
718       : _M_future(std::move(__rhs._M_future)),
719       _M_storage(std::move(__rhs._M_storage))
720       { }
721
722       // TODO: requires allocator concepts
723       /*
724       template<typename _Allocator>
725         promise(allocator_arg_t, const _Allocator& __a);
726
727       template<typename _Allocator>
728         promise(allocator_arg_t, const _Allocator&, promise&& __rhs);
729        */
730
731       promise(const promise&) = delete;
732
733       ~promise()
734       {
735         if (static_cast<bool>(_M_future) && !_M_future.unique())
736           _M_future->_M_break_promise(std::move(_M_storage));
737       }
738
739       // assignment
740       promise&
741       operator=(promise&& __rhs)
742       {
743         promise(std::move(__rhs)).swap(*this);
744         return *this;
745       }
746
747       promise& operator=(const promise&) = delete;
748
749       void
750       swap(promise& __rhs)
751       {
752         _M_future.swap(__rhs._M_future);
753         _M_storage.swap(__rhs._M_storage);
754       }
755
756       // retrieving the result
757       unique_future<void>
758       get_future()
759       { return unique_future<void>(_M_future); }
760
761       // setting the result
762       void
763       set_value()
764       {
765         _M_future->_M_set_result(std::move(_M_storage));
766       }
767
768       void
769       set_exception(exception_ptr __p)
770       {
771         if (!_M_satisfied())
772           _M_storage->_M_error = __p;
773         _M_future->_M_set_result(std::move(_M_storage));
774       }
775
776     private:
777       template<typename> friend class packaged_task;
778       bool _M_satisfied() { return !static_cast<bool>(_M_storage); }
779       shared_ptr<_Future_state>                 _M_future;
780       _Future_ptr<_Future_result<void>>::type   _M_storage;
781     };
782
783   // TODO: requires allocator concepts
784   /*
785   template<typename _Result, class Alloc>
786     concept_map UsesAllocator<promise<_Result>, Alloc>
787     {
788       typedef Alloc allocator_type;
789     }
790    */
791
792   template<typename _Result, typename... _ArgTypes>
793     struct _Run_task
794     {
795       static void
796       _S_run(promise<_Result>& __p, function<_Result(_ArgTypes...)>& __f,
797           _ArgTypes... __args)
798       {
799         __p.set_value(__f(std::forward<_ArgTypes>(__args)...));
800       }
801     };
802
803   // specialization used by packaged_task<void(...)>
804   template<typename... _ArgTypes>
805     struct _Run_task<void, _ArgTypes...>
806     {
807       static void
808       _S_run(promise<void>& __p, function<void(_ArgTypes...)>& __f,
809           _ArgTypes... __args)
810       {
811         __f(std::forward<_ArgTypes>(__args)...);
812         __p.set_value();
813       }
814     };
815
816   /// packaged_task
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