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