OSDN Git Service

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