libstdc++
|
00001 // <future> -*- C++ -*- 00002 00003 // Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /** @file include/future 00026 * This is a Standard C++ Library header. 00027 */ 00028 00029 #ifndef _GLIBCXX_FUTURE 00030 #define _GLIBCXX_FUTURE 1 00031 00032 #pragma GCC system_header 00033 00034 #ifndef __GXX_EXPERIMENTAL_CXX0X__ 00035 # include <bits/c++0x_warning.h> 00036 #else 00037 00038 #include <functional> 00039 #include <memory> 00040 #include <mutex> 00041 #include <thread> 00042 #include <condition_variable> 00043 #include <system_error> 00044 #include <exception> 00045 #include <atomic> 00046 #include <bits/functexcept.h> 00047 00048 namespace std _GLIBCXX_VISIBILITY(default) 00049 { 00050 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00051 00052 /** 00053 * @defgroup futures Futures 00054 * @ingroup concurrency 00055 * 00056 * Classes for futures support. 00057 * @{ 00058 */ 00059 00060 /// Error code for futures 00061 enum class future_errc 00062 { 00063 broken_promise, 00064 future_already_retrieved, 00065 promise_already_satisfied, 00066 no_state 00067 }; 00068 00069 /// Specialization. 00070 template<> 00071 struct is_error_code_enum<future_errc> : public true_type { }; 00072 00073 /// Points to a statically-allocated object derived from error_category. 00074 const error_category& 00075 future_category(); 00076 00077 /// Overload for make_error_code. 00078 inline error_code 00079 make_error_code(future_errc __errc) 00080 { return error_code(static_cast<int>(__errc), future_category()); } 00081 00082 /// Overload for make_error_condition. 00083 inline error_condition 00084 make_error_condition(future_errc __errc) 00085 { return error_condition(static_cast<int>(__errc), future_category()); } 00086 00087 /** 00088 * @brief Exception type thrown by futures. 00089 * @ingroup exceptions 00090 */ 00091 class future_error : public logic_error 00092 { 00093 error_code _M_code; 00094 00095 public: 00096 explicit future_error(error_code __ec) 00097 : logic_error("std::future_error"), _M_code(__ec) 00098 { } 00099 00100 virtual ~future_error() throw(); 00101 00102 virtual const char* 00103 what() const throw(); 00104 00105 const error_code& 00106 code() const throw() { return _M_code; } 00107 }; 00108 00109 // Forward declarations. 00110 template<typename _Res> 00111 class future; 00112 00113 template<typename _Res> 00114 class shared_future; 00115 00116 template<typename _Res> 00117 class atomic_future; 00118 00119 template<typename _Signature> 00120 class packaged_task; 00121 00122 template<typename _Res> 00123 class promise; 00124 00125 /// Launch code for futures 00126 enum class launch 00127 { 00128 any, 00129 async, 00130 sync 00131 }; 00132 00133 /// Status code for futures 00134 enum class future_status 00135 { 00136 ready, 00137 timeout, 00138 deferred 00139 }; 00140 00141 template<typename _Fn, typename... _Args> 00142 future<typename result_of<_Fn(_Args...)>::type> 00143 async(launch __policy, _Fn&& __fn, _Args&&... __args); 00144 00145 template<typename _FnCheck, typename _Fn, typename... _Args> 00146 struct __async_sfinae_helper 00147 { 00148 typedef future<typename result_of<_Fn(_Args...)>::type> type; 00149 }; 00150 00151 template<typename _Fn, typename... _Args> 00152 struct __async_sfinae_helper<launch, _Fn, _Args...> 00153 { }; 00154 00155 template<typename _Fn, typename... _Args> 00156 typename 00157 __async_sfinae_helper<typename decay<_Fn>::type, _Fn, _Args...>::type 00158 async(_Fn&& __fn, _Args&&... __args); 00159 00160 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1) \ 00161 && defined(_GLIBCXX_ATOMIC_BUILTINS_4) 00162 00163 /// Base class and enclosing scope. 00164 struct __future_base 00165 { 00166 /// Base class for results. 00167 struct _Result_base 00168 { 00169 exception_ptr _M_error; 00170 00171 _Result_base(const _Result_base&) = delete; 00172 _Result_base& operator=(const _Result_base&) = delete; 00173 00174 // _M_destroy() allows derived classes to control deallocation 00175 virtual void _M_destroy() = 0; 00176 00177 struct _Deleter 00178 { 00179 void operator()(_Result_base* __fr) const { __fr->_M_destroy(); } 00180 }; 00181 00182 protected: 00183 _Result_base(); 00184 virtual ~_Result_base(); 00185 }; 00186 00187 /// Result. 00188 template<typename _Res> 00189 struct _Result : _Result_base 00190 { 00191 private: 00192 typedef alignment_of<_Res> __a_of; 00193 typedef aligned_storage<sizeof(_Res), __a_of::value> __align_storage; 00194 typedef typename __align_storage::type __align_type; 00195 00196 __align_type _M_storage; 00197 bool _M_initialized; 00198 00199 public: 00200 _Result() : _M_initialized() { } 00201 00202 ~_Result() 00203 { 00204 if (_M_initialized) 00205 _M_value().~_Res(); 00206 } 00207 00208 // Return lvalue, future will add const or rvalue-reference 00209 _Res& 00210 _M_value() { return *static_cast<_Res*>(_M_addr()); } 00211 00212 void 00213 _M_set(const _Res& __res) 00214 { 00215 ::new (_M_addr()) _Res(__res); 00216 _M_initialized = true; 00217 } 00218 00219 void 00220 _M_set(_Res&& __res) 00221 { 00222 ::new (_M_addr()) _Res(std::move(__res)); 00223 _M_initialized = true; 00224 } 00225 00226 private: 00227 void _M_destroy() { delete this; } 00228 00229 void* _M_addr() { return static_cast<void*>(&_M_storage); } 00230 }; 00231 00232 // TODO: use template alias when available 00233 /* 00234 template<typename _Res> 00235 using _Ptr = unique_ptr<_Res, _Result_base::_Deleter>; 00236 */ 00237 /// A unique_ptr based on the instantiating type. 00238 template<typename _Res> 00239 struct _Ptr 00240 { 00241 typedef unique_ptr<_Res, _Result_base::_Deleter> type; 00242 }; 00243 00244 /// Result_alloc. 00245 template<typename _Res, typename _Alloc> 00246 struct _Result_alloc : _Result<_Res>, _Alloc 00247 { 00248 typedef typename _Alloc::template rebind<_Result_alloc>::other 00249 __allocator_type; 00250 00251 explicit 00252 _Result_alloc(const _Alloc& __a) : _Result<_Res>(), _Alloc(__a) 00253 { } 00254 00255 private: 00256 void _M_destroy() 00257 { 00258 __allocator_type __a(*this); 00259 __a.destroy(this); 00260 __a.deallocate(this, 1); 00261 } 00262 }; 00263 00264 template<typename _Res, typename _Allocator> 00265 static typename _Ptr<_Result_alloc<_Res, _Allocator>>::type 00266 _S_allocate_result(const _Allocator& __a) 00267 { 00268 typedef _Result_alloc<_Res, _Allocator> __result_type; 00269 typename __result_type::__allocator_type __a2(__a); 00270 __result_type* __p = __a2.allocate(1); 00271 __try 00272 { 00273 __a2.construct(__p, __a); 00274 } 00275 __catch(...) 00276 { 00277 __a2.deallocate(__p, 1); 00278 __throw_exception_again; 00279 } 00280 return typename _Ptr<__result_type>::type(__p); 00281 } 00282 00283 00284 /// Base class for state between a promise and one or more 00285 /// associated futures. 00286 class _State_base 00287 { 00288 typedef _Ptr<_Result_base>::type _Ptr_type; 00289 00290 _Ptr_type _M_result; 00291 mutex _M_mutex; 00292 condition_variable _M_cond; 00293 atomic_flag _M_retrieved; 00294 once_flag _M_once; 00295 00296 public: 00297 _State_base() : _M_result(), _M_retrieved(ATOMIC_FLAG_INIT) { } 00298 _State_base(const _State_base&) = delete; 00299 _State_base& operator=(const _State_base&) = delete; 00300 virtual ~_State_base(); 00301 00302 _Result_base& 00303 wait() 00304 { 00305 _M_run_deferred(); 00306 unique_lock<mutex> __lock(_M_mutex); 00307 if (!_M_ready()) 00308 _M_cond.wait(__lock, std::bind<bool>(&_State_base::_M_ready, this)); 00309 return *_M_result; 00310 } 00311 00312 template<typename _Rep, typename _Period> 00313 bool 00314 wait_for(const chrono::duration<_Rep, _Period>& __rel) 00315 { 00316 unique_lock<mutex> __lock(_M_mutex); 00317 auto __bound = std::bind<bool>(&_State_base::_M_ready, this); 00318 return _M_ready() || _M_cond.wait_for(__lock, __rel, __bound); 00319 } 00320 00321 template<typename _Clock, typename _Duration> 00322 bool 00323 wait_until(const chrono::time_point<_Clock, _Duration>& __abs) 00324 { 00325 unique_lock<mutex> __lock(_M_mutex); 00326 auto __bound = std::bind<bool>(&_State_base::_M_ready, this); 00327 return _M_ready() || _M_cond.wait_until(__lock, __abs, __bound); 00328 } 00329 00330 void 00331 _M_set_result(function<_Ptr_type()> __res, bool __ignore_failure = false) 00332 { 00333 bool __set = __ignore_failure; 00334 // all calls to this function are serialized, 00335 // side-effects of invoking __res only happen once 00336 call_once(_M_once, &_State_base::_M_do_set, this, ref(__res), 00337 ref(__set)); 00338 if (!__set) 00339 __throw_future_error(int(future_errc::promise_already_satisfied)); 00340 } 00341 00342 void 00343 _M_break_promise(_Ptr_type __res) 00344 { 00345 if (static_cast<bool>(__res)) 00346 { 00347 error_code __ec(make_error_code(future_errc::broken_promise)); 00348 __res->_M_error = copy_exception(future_error(__ec)); 00349 { 00350 lock_guard<mutex> __lock(_M_mutex); 00351 _M_result.swap(__res); 00352 } 00353 _M_cond.notify_all(); 00354 } 00355 } 00356 00357 // Called when this object is passed to a future. 00358 void 00359 _M_set_retrieved_flag() 00360 { 00361 if (_M_retrieved.test_and_set()) 00362 __throw_future_error(int(future_errc::future_already_retrieved)); 00363 } 00364 00365 template<typename _Res, typename _Arg> 00366 struct _Setter; 00367 00368 // set lvalues 00369 template<typename _Res, typename _Arg> 00370 struct _Setter<_Res, _Arg&> 00371 { 00372 // check this is only used by promise<R>::set_value(const R&) 00373 // or promise<R>::set_value(R&) 00374 static_assert(is_same<_Res, _Arg&>::value // promise<R&> 00375 || is_same<const _Res, _Arg>::value, // promise<R> 00376 "Invalid specialisation"); 00377 00378 typename promise<_Res>::_Ptr_type operator()() 00379 { 00380 _State_base::_S_check(_M_promise->_M_future); 00381 _M_promise->_M_storage->_M_set(_M_arg); 00382 return std::move(_M_promise->_M_storage); 00383 } 00384 promise<_Res>* _M_promise; 00385 _Arg& _M_arg; 00386 }; 00387 00388 // set rvalues 00389 template<typename _Res> 00390 struct _Setter<_Res, _Res&&> 00391 { 00392 typename promise<_Res>::_Ptr_type operator()() 00393 { 00394 _State_base::_S_check(_M_promise->_M_future); 00395 _M_promise->_M_storage->_M_set(std::move(_M_arg)); 00396 return std::move(_M_promise->_M_storage); 00397 } 00398 promise<_Res>* _M_promise; 00399 _Res& _M_arg; 00400 }; 00401 00402 struct __exception_ptr_tag { }; 00403 00404 // set exceptions 00405 template<typename _Res> 00406 struct _Setter<_Res, __exception_ptr_tag> 00407 { 00408 typename promise<_Res>::_Ptr_type operator()() 00409 { 00410 _State_base::_S_check(_M_promise->_M_future); 00411 _M_promise->_M_storage->_M_error = _M_ex; 00412 return std::move(_M_promise->_M_storage); 00413 } 00414 00415 promise<_Res>* _M_promise; 00416 exception_ptr& _M_ex; 00417 }; 00418 00419 template<typename _Res, typename _Arg> 00420 static _Setter<_Res, _Arg&&> 00421 __setter(promise<_Res>* __prom, _Arg&& __arg) 00422 { 00423 return _Setter<_Res, _Arg&&>{ __prom, __arg }; 00424 } 00425 00426 template<typename _Res> 00427 static _Setter<_Res, __exception_ptr_tag> 00428 __setter(exception_ptr& __ex, promise<_Res>* __prom) 00429 { 00430 return _Setter<_Res, __exception_ptr_tag>{ __prom, __ex }; 00431 } 00432 00433 static _Setter<void, void> 00434 __setter(promise<void>* __prom); 00435 00436 template<typename _Tp> 00437 static bool 00438 _S_check(const shared_ptr<_Tp>& __p) 00439 { 00440 if (!static_cast<bool>(__p)) 00441 __throw_future_error((int)future_errc::no_state); 00442 } 00443 00444 private: 00445 void 00446 _M_do_set(function<_Ptr_type()>& __f, bool& __set) 00447 { 00448 _Ptr_type __res = __f(); 00449 { 00450 lock_guard<mutex> __lock(_M_mutex); 00451 _M_result.swap(__res); 00452 } 00453 _M_cond.notify_all(); 00454 __set = true; 00455 } 00456 00457 bool _M_ready() const { return static_cast<bool>(_M_result); } 00458 00459 virtual void _M_run_deferred() { } 00460 }; 00461 00462 template<typename _Res> 00463 class _Deferred_state; 00464 00465 template<typename _Res> 00466 class _Async_state; 00467 00468 template<typename _Signature> 00469 class _Task_state; 00470 00471 template<typename _StateT, typename _Res = typename _StateT::_Res_type> 00472 struct _Task_setter; 00473 }; 00474 00475 /// Partial specialization for reference types. 00476 template<typename _Res> 00477 struct __future_base::_Result<_Res&> : __future_base::_Result_base 00478 { 00479 _Result() : _M_value_ptr() { } 00480 00481 void _M_set(_Res& __res) { _M_value_ptr = &__res; } 00482 00483 _Res& _M_get() { return *_M_value_ptr; } 00484 00485 private: 00486 _Res* _M_value_ptr; 00487 00488 void _M_destroy() { delete this; } 00489 }; 00490 00491 /// Explicit specialization for void. 00492 template<> 00493 struct __future_base::_Result<void> : __future_base::_Result_base 00494 { 00495 private: 00496 void _M_destroy() { delete this; } 00497 }; 00498 00499 00500 /// Common implementation for future and shared_future. 00501 template<typename _Res> 00502 class __basic_future : public __future_base 00503 { 00504 protected: 00505 typedef shared_ptr<_State_base> __state_type; 00506 typedef __future_base::_Result<_Res>& __result_type; 00507 00508 private: 00509 __state_type _M_state; 00510 00511 public: 00512 // Disable copying. 00513 __basic_future(const __basic_future&) = delete; 00514 __basic_future& operator=(const __basic_future&) = delete; 00515 00516 bool 00517 valid() const { return static_cast<bool>(_M_state); } 00518 00519 void 00520 wait() const 00521 { 00522 _State_base::_S_check(_M_state); 00523 _M_state->wait(); 00524 } 00525 00526 template<typename _Rep, typename _Period> 00527 bool 00528 wait_for(const chrono::duration<_Rep, _Period>& __rel) const 00529 { 00530 _State_base::_S_check(_M_state); 00531 return _M_state->wait_for(__rel); 00532 } 00533 00534 template<typename _Clock, typename _Duration> 00535 bool 00536 wait_until(const chrono::time_point<_Clock, _Duration>& __abs) const 00537 { 00538 _State_base::_S_check(_M_state); 00539 return _M_state->wait_until(__abs); 00540 } 00541 00542 protected: 00543 /// Wait for the state to be ready and rethrow any stored exception 00544 __result_type 00545 _M_get_result() 00546 { 00547 _State_base::_S_check(_M_state); 00548 _Result_base& __res = _M_state->wait(); 00549 if (!(__res._M_error == 0)) 00550 rethrow_exception(__res._M_error); 00551 return static_cast<__result_type>(__res); 00552 } 00553 00554 void _M_swap(__basic_future& __that) 00555 { 00556 _M_state.swap(__that._M_state); 00557 } 00558 00559 // Construction of a future by promise::get_future() 00560 explicit 00561 __basic_future(const __state_type& __state) : _M_state(__state) 00562 { 00563 _State_base::_S_check(_M_state); 00564 _M_state->_M_set_retrieved_flag(); 00565 } 00566 00567 // Copy construction from a shared_future 00568 explicit 00569 __basic_future(const shared_future<_Res>&); 00570 00571 // Move construction from a shared_future 00572 explicit 00573 __basic_future(shared_future<_Res>&&); 00574 00575 // Move construction from a future 00576 explicit 00577 __basic_future(future<_Res>&&); 00578 00579 constexpr __basic_future() : _M_state() { } 00580 00581 struct _Reset 00582 { 00583 explicit _Reset(__basic_future& __fut) : _M_fut(__fut) { } 00584 ~_Reset() { _M_fut._M_state.reset(); } 00585 __basic_future& _M_fut; 00586 }; 00587 }; 00588 00589 00590 /// Primary template for future. 00591 template<typename _Res> 00592 class future : public __basic_future<_Res> 00593 { 00594 friend class promise<_Res>; 00595 template<typename> friend class packaged_task; 00596 template<typename _Fn, typename... _Args> 00597 friend future<typename result_of<_Fn(_Args...)>::type> 00598 async(launch, _Fn&&, _Args&&...); 00599 00600 typedef __basic_future<_Res> _Base_type; 00601 typedef typename _Base_type::__state_type __state_type; 00602 00603 explicit 00604 future(const __state_type& __state) : _Base_type(__state) { } 00605 00606 public: 00607 constexpr future() : _Base_type() { } 00608 00609 /// Move constructor 00610 future(future&& __uf) : _Base_type(std::move(__uf)) { } 00611 00612 // Disable copying 00613 future(const future&) = delete; 00614 future& operator=(const future&) = delete; 00615 00616 future& operator=(future&& __fut) 00617 { 00618 future(std::move(__fut))._M_swap(*this); 00619 return *this; 00620 } 00621 00622 /// Retrieving the value 00623 _Res 00624 get() 00625 { 00626 typename _Base_type::_Reset __reset(*this); 00627 return std::move(this->_M_get_result()._M_value()); 00628 } 00629 }; 00630 00631 /// Partial specialization for future<R&> 00632 template<typename _Res> 00633 class future<_Res&> : public __basic_future<_Res&> 00634 { 00635 friend class promise<_Res&>; 00636 template<typename> friend class packaged_task; 00637 template<typename _Fn, typename... _Args> 00638 friend future<typename result_of<_Fn(_Args...)>::type> 00639 async(launch, _Fn&&, _Args&&...); 00640 00641 typedef __basic_future<_Res&> _Base_type; 00642 typedef typename _Base_type::__state_type __state_type; 00643 00644 explicit 00645 future(const __state_type& __state) : _Base_type(__state) { } 00646 00647 public: 00648 constexpr future() : _Base_type() { } 00649 00650 /// Move constructor 00651 future(future&& __uf) : _Base_type(std::move(__uf)) { } 00652 00653 // Disable copying 00654 future(const future&) = delete; 00655 future& operator=(const future&) = delete; 00656 00657 future& operator=(future&& __fut) 00658 { 00659 future(std::move(__fut))._M_swap(*this); 00660 return *this; 00661 } 00662 00663 /// Retrieving the value 00664 _Res& 00665 get() 00666 { 00667 typename _Base_type::_Reset __reset(*this); 00668 return this->_M_get_result()._M_get(); 00669 } 00670 }; 00671 00672 /// Explicit specialization for future<void> 00673 template<> 00674 class future<void> : public __basic_future<void> 00675 { 00676 friend class promise<void>; 00677 template<typename> friend class packaged_task; 00678 template<typename _Fn, typename... _Args> 00679 friend future<typename result_of<_Fn(_Args...)>::type> 00680 async(launch, _Fn&&, _Args&&...); 00681 00682 typedef __basic_future<void> _Base_type; 00683 typedef typename _Base_type::__state_type __state_type; 00684 00685 explicit 00686 future(const __state_type& __state) : _Base_type(__state) { } 00687 00688 public: 00689 constexpr future() : _Base_type() { } 00690 00691 /// Move constructor 00692 future(future&& __uf) : _Base_type(std::move(__uf)) { } 00693 00694 // Disable copying 00695 future(const future&) = delete; 00696 future& operator=(const future&) = delete; 00697 00698 future& operator=(future&& __fut) 00699 { 00700 future(std::move(__fut))._M_swap(*this); 00701 return *this; 00702 } 00703 00704 /// Retrieving the value 00705 void 00706 get() 00707 { 00708 typename _Base_type::_Reset __reset(*this); 00709 this->_M_get_result(); 00710 } 00711 }; 00712 00713 00714 /// Primary template for shared_future. 00715 template<typename _Res> 00716 class shared_future : public __basic_future<_Res> 00717 { 00718 typedef __basic_future<_Res> _Base_type; 00719 00720 public: 00721 constexpr shared_future() : _Base_type() { } 00722 00723 /// Copy constructor 00724 shared_future(const shared_future& __sf) : _Base_type(__sf) { } 00725 00726 /// Construct from a future rvalue 00727 shared_future(future<_Res>&& __uf) 00728 : _Base_type(std::move(__uf)) 00729 { } 00730 00731 /// Construct from a shared_future rvalue 00732 shared_future(shared_future&& __sf) 00733 : _Base_type(std::move(__sf)) 00734 { } 00735 00736 shared_future& operator=(const shared_future& __sf) 00737 { 00738 shared_future(__sf)._M_swap(*this); 00739 return *this; 00740 } 00741 00742 shared_future& operator=(shared_future&& __sf) 00743 { 00744 shared_future(std::move(__sf))._M_swap(*this); 00745 return *this; 00746 } 00747 00748 /// Retrieving the value 00749 const _Res& 00750 get() 00751 { 00752 typename _Base_type::__result_type __r = this->_M_get_result(); 00753 _Res& __rs(__r._M_value()); 00754 return __rs; 00755 } 00756 }; 00757 00758 /// Partial specialization for shared_future<R&> 00759 template<typename _Res> 00760 class shared_future<_Res&> : public __basic_future<_Res&> 00761 { 00762 typedef __basic_future<_Res&> _Base_type; 00763 00764 public: 00765 constexpr shared_future() : _Base_type() { } 00766 00767 /// Copy constructor 00768 shared_future(const shared_future& __sf) : _Base_type(__sf) { } 00769 00770 /// Construct from a future rvalue 00771 shared_future(future<_Res&>&& __uf) 00772 : _Base_type(std::move(__uf)) 00773 { } 00774 00775 /// Construct from a shared_future rvalue 00776 shared_future(shared_future&& __sf) 00777 : _Base_type(std::move(__sf)) 00778 { } 00779 00780 shared_future& operator=(const shared_future& __sf) 00781 { 00782 shared_future(__sf)._M_swap(*this); 00783 return *this; 00784 } 00785 00786 shared_future& operator=(shared_future&& __sf) 00787 { 00788 shared_future(std::move(__sf))._M_swap(*this); 00789 return *this; 00790 } 00791 00792 /// Retrieving the value 00793 _Res& 00794 get() { return this->_M_get_result()._M_get(); } 00795 }; 00796 00797 /// Explicit specialization for shared_future<void> 00798 template<> 00799 class shared_future<void> : public __basic_future<void> 00800 { 00801 typedef __basic_future<void> _Base_type; 00802 00803 public: 00804 constexpr shared_future() : _Base_type() { } 00805 00806 /// Copy constructor 00807 shared_future(const shared_future& __sf) : _Base_type(__sf) { } 00808 00809 /// Construct from a future rvalue 00810 shared_future(future<void>&& __uf) 00811 : _Base_type(std::move(__uf)) 00812 { } 00813 00814 /// Construct from a shared_future rvalue 00815 shared_future(shared_future&& __sf) 00816 : _Base_type(std::move(__sf)) 00817 { } 00818 00819 shared_future& operator=(const shared_future& __sf) 00820 { 00821 shared_future(__sf)._M_swap(*this); 00822 return *this; 00823 } 00824 00825 shared_future& operator=(shared_future&& __sf) 00826 { 00827 shared_future(std::move(__sf))._M_swap(*this); 00828 return *this; 00829 } 00830 00831 // Retrieving the value 00832 void 00833 get() { this->_M_get_result(); } 00834 }; 00835 00836 // Now we can define the protected __basic_future constructors. 00837 template<typename _Res> 00838 inline __basic_future<_Res>:: 00839 __basic_future(const shared_future<_Res>& __sf) 00840 : _M_state(__sf._M_state) 00841 { } 00842 00843 template<typename _Res> 00844 inline __basic_future<_Res>:: 00845 __basic_future(shared_future<_Res>&& __sf) 00846 : _M_state(std::move(__sf._M_state)) 00847 { } 00848 00849 template<typename _Res> 00850 inline __basic_future<_Res>:: 00851 __basic_future(future<_Res>&& __uf) 00852 : _M_state(std::move(__uf._M_state)) 00853 { } 00854 00855 00856 /// Primary template for promise 00857 template<typename _Res> 00858 class promise 00859 { 00860 typedef __future_base::_State_base _State; 00861 typedef __future_base::_Result<_Res> _Res_type; 00862 typedef typename __future_base::_Ptr<_Res_type>::type _Ptr_type; 00863 template<typename, typename> friend class _State::_Setter; 00864 00865 shared_ptr<_State> _M_future; 00866 _Ptr_type _M_storage; 00867 00868 public: 00869 promise() 00870 : _M_future(std::make_shared<_State>()), 00871 _M_storage(new _Res_type()) 00872 { } 00873 00874 promise(promise&& __rhs) 00875 : _M_future(std::move(__rhs._M_future)), 00876 _M_storage(std::move(__rhs._M_storage)) 00877 { } 00878 00879 template<typename _Allocator> 00880 promise(allocator_arg_t, const _Allocator& __a) 00881 : _M_future(std::allocate_shared<_State>(__a)), 00882 _M_storage(__future_base::_S_allocate_result<_Res>(__a)) 00883 { } 00884 00885 promise(const promise&) = delete; 00886 00887 ~promise() 00888 { 00889 if (static_cast<bool>(_M_future) && !_M_future.unique()) 00890 _M_future->_M_break_promise(std::move(_M_storage)); 00891 } 00892 00893 // Assignment 00894 promise& 00895 operator=(promise&& __rhs) 00896 { 00897 promise(std::move(__rhs)).swap(*this); 00898 return *this; 00899 } 00900 00901 promise& operator=(const promise&) = delete; 00902 00903 void 00904 swap(promise& __rhs) 00905 { 00906 _M_future.swap(__rhs._M_future); 00907 _M_storage.swap(__rhs._M_storage); 00908 } 00909 00910 // Retrieving the result 00911 future<_Res> 00912 get_future() 00913 { return future<_Res>(_M_future); } 00914 00915 // Setting the result 00916 void 00917 set_value(const _Res& __r) 00918 { 00919 auto __setter = _State::__setter(this, __r); 00920 _M_future->_M_set_result(std::move(__setter)); 00921 } 00922 00923 void 00924 set_value(_Res&& __r) 00925 { 00926 auto __setter = _State::__setter(this, std::move(__r)); 00927 _M_future->_M_set_result(std::move(__setter)); 00928 } 00929 00930 void 00931 set_exception(exception_ptr __p) 00932 { 00933 auto __setter = _State::__setter(__p, this); 00934 _M_future->_M_set_result(std::move(__setter)); 00935 } 00936 }; 00937 00938 template<typename _Res> 00939 inline void 00940 swap(promise<_Res>& __x, promise<_Res>& __y) 00941 { __x.swap(__y); } 00942 00943 template<typename _Res, typename _Alloc> 00944 struct uses_allocator<promise<_Res>, _Alloc> 00945 : public true_type { }; 00946 00947 00948 /// Partial specialization for promise<R&> 00949 template<typename _Res> 00950 class promise<_Res&> 00951 { 00952 typedef __future_base::_State_base _State; 00953 typedef __future_base::_Result<_Res&> _Res_type; 00954 typedef typename __future_base::_Ptr<_Res_type>::type _Ptr_type; 00955 template<typename, typename> friend class _State::_Setter; 00956 00957 shared_ptr<_State> _M_future; 00958 _Ptr_type _M_storage; 00959 00960 public: 00961 promise() 00962 : _M_future(std::make_shared<_State>()), 00963 _M_storage(new _Res_type()) 00964 { } 00965 00966 promise(promise&& __rhs) 00967 : _M_future(std::move(__rhs._M_future)), 00968 _M_storage(std::move(__rhs._M_storage)) 00969 { } 00970 00971 template<typename _Allocator> 00972 promise(allocator_arg_t, const _Allocator& __a) 00973 : _M_future(std::allocate_shared<_State>(__a)), 00974 _M_storage(__future_base::_S_allocate_result<_Res&>(__a)) 00975 { } 00976 00977 promise(const promise&) = delete; 00978 00979 ~promise() 00980 { 00981 if (static_cast<bool>(_M_future) && !_M_future.unique()) 00982 _M_future->_M_break_promise(std::move(_M_storage)); 00983 } 00984 00985 // Assignment 00986 promise& 00987 operator=(promise&& __rhs) 00988 { 00989 promise(std::move(__rhs)).swap(*this); 00990 return *this; 00991 } 00992 00993 promise& operator=(const promise&) = delete; 00994 00995 void 00996 swap(promise& __rhs) 00997 { 00998 _M_future.swap(__rhs._M_future); 00999 _M_storage.swap(__rhs._M_storage); 01000 } 01001 01002 // Retrieving the result 01003 future<_Res&> 01004 get_future() 01005 { return future<_Res&>(_M_future); } 01006 01007 // Setting the result 01008 void 01009 set_value(_Res& __r) 01010 { 01011 auto __setter = _State::__setter(this, __r); 01012 _M_future->_M_set_result(std::move(__setter)); 01013 } 01014 01015 void 01016 set_exception(exception_ptr __p) 01017 { 01018 auto __setter = _State::__setter(__p, this); 01019 _M_future->_M_set_result(std::move(__setter)); 01020 } 01021 }; 01022 01023 /// Explicit specialization for promise<void> 01024 template<> 01025 class promise<void> 01026 { 01027 typedef __future_base::_State_base _State; 01028 typedef __future_base::_Result<void> _Res_type; 01029 typedef typename __future_base::_Ptr<_Res_type>::type _Ptr_type; 01030 template<typename, typename> friend class _State::_Setter; 01031 01032 shared_ptr<_State> _M_future; 01033 _Ptr_type _M_storage; 01034 01035 public: 01036 promise() 01037 : _M_future(std::make_shared<_State>()), 01038 _M_storage(new _Res_type()) 01039 { } 01040 01041 promise(promise&& __rhs) 01042 : _M_future(std::move(__rhs._M_future)), 01043 _M_storage(std::move(__rhs._M_storage)) 01044 { } 01045 01046 template<typename _Allocator> 01047 promise(allocator_arg_t, const _Allocator& __a) 01048 : _M_future(std::allocate_shared<_State>(__a)), 01049 _M_storage(__future_base::_S_allocate_result<void>(__a)) 01050 { } 01051 01052 promise(const promise&) = delete; 01053 01054 ~promise() 01055 { 01056 if (static_cast<bool>(_M_future) && !_M_future.unique()) 01057 _M_future->_M_break_promise(std::move(_M_storage)); 01058 } 01059 01060 // Assignment 01061 promise& 01062 operator=(promise&& __rhs) 01063 { 01064 promise(std::move(__rhs)).swap(*this); 01065 return *this; 01066 } 01067 01068 promise& operator=(const promise&) = delete; 01069 01070 void 01071 swap(promise& __rhs) 01072 { 01073 _M_future.swap(__rhs._M_future); 01074 _M_storage.swap(__rhs._M_storage); 01075 } 01076 01077 // Retrieving the result 01078 future<void> 01079 get_future() 01080 { return future<void>(_M_future); } 01081 01082 // Setting the result 01083 void set_value(); 01084 01085 void 01086 set_exception(exception_ptr __p) 01087 { 01088 auto __setter = _State::__setter(__p, this); 01089 _M_future->_M_set_result(std::move(__setter)); 01090 } 01091 }; 01092 01093 // set void 01094 template<> 01095 struct __future_base::_State_base::_Setter<void, void> 01096 { 01097 promise<void>::_Ptr_type operator()() 01098 { 01099 _State_base::_S_check(_M_promise->_M_future); 01100 return std::move(_M_promise->_M_storage); 01101 } 01102 01103 promise<void>* _M_promise; 01104 }; 01105 01106 inline __future_base::_State_base::_Setter<void, void> 01107 __future_base::_State_base::__setter(promise<void>* __prom) 01108 { 01109 return _Setter<void, void>{ __prom }; 01110 } 01111 01112 inline void 01113 promise<void>::set_value() 01114 { 01115 auto __setter = _State::__setter(this); 01116 _M_future->_M_set_result(std::move(__setter)); 01117 } 01118 01119 01120 template<typename _StateT, typename _Res> 01121 struct __future_base::_Task_setter 01122 { 01123 typename _StateT::_Ptr_type operator()() 01124 { 01125 __try 01126 { 01127 _M_state->_M_result->_M_set(_M_fn()); 01128 } 01129 __catch(...) 01130 { 01131 _M_state->_M_result->_M_error = current_exception(); 01132 } 01133 return std::move(_M_state->_M_result); 01134 } 01135 _StateT* _M_state; 01136 std::function<_Res()> _M_fn; 01137 }; 01138 01139 template<typename _StateT> 01140 struct __future_base::_Task_setter<_StateT, void> 01141 { 01142 typename _StateT::_Ptr_type operator()() 01143 { 01144 __try 01145 { 01146 _M_fn(); 01147 } 01148 __catch(...) 01149 { 01150 _M_state->_M_result->_M_error = current_exception(); 01151 } 01152 return std::move(_M_state->_M_result); 01153 } 01154 _StateT* _M_state; 01155 std::function<void()> _M_fn; 01156 }; 01157 01158 template<typename _Res, typename... _Args> 01159 struct __future_base::_Task_state<_Res(_Args...)> 01160 : __future_base::_State_base 01161 { 01162 typedef _Res _Res_type; 01163 01164 _Task_state(std::function<_Res(_Args...)> __task) 01165 : _M_result(new _Result<_Res>()), _M_task(std::move(__task)) 01166 { } 01167 01168 template<typename _Func, typename _Alloc> 01169 _Task_state(_Func&& __task, const _Alloc& __a) 01170 : _M_result(_S_allocate_result<_Res>(__a)), 01171 _M_task(allocator_arg, __a, std::move(__task)) 01172 { } 01173 01174 void 01175 _M_run(_Args... __args) 01176 { 01177 // bound arguments decay so wrap lvalue references 01178 auto __bound = std::bind<_Res>(std::ref(_M_task), 01179 _S_maybe_wrap_ref(std::forward<_Args>(__args))...); 01180 _Task_setter<_Task_state> __setter{ this, std::move(__bound) }; 01181 _M_set_result(std::move(__setter)); 01182 } 01183 01184 template<typename, typename> friend class _Task_setter; 01185 typedef typename __future_base::_Ptr<_Result<_Res>>::type _Ptr_type; 01186 _Ptr_type _M_result; 01187 std::function<_Res(_Args...)> _M_task; 01188 01189 template<typename _Tp> 01190 static reference_wrapper<_Tp> 01191 _S_maybe_wrap_ref(_Tp& __t) 01192 { return std::ref(__t); } 01193 01194 template<typename _Tp> 01195 static typename enable_if<!is_lvalue_reference<_Tp>::value, 01196 _Tp>::type&& 01197 _S_maybe_wrap_ref(_Tp&& __t) 01198 { return std::forward<_Tp>(__t); } 01199 }; 01200 01201 /// packaged_task 01202 template<typename _Res, typename... _ArgTypes> 01203 class packaged_task<_Res(_ArgTypes...)> 01204 { 01205 typedef __future_base::_Task_state<_Res(_ArgTypes...)> _State_type; 01206 shared_ptr<_State_type> _M_state; 01207 01208 public: 01209 typedef _Res result_type; 01210 01211 // Construction and destruction 01212 packaged_task() { } 01213 01214 template<typename _Fn> 01215 explicit 01216 packaged_task(const _Fn& __fn) 01217 : _M_state(std::make_shared<_State_type>(__fn)) 01218 { } 01219 01220 template<typename _Fn> 01221 explicit 01222 packaged_task(_Fn&& __fn) 01223 : _M_state(std::make_shared<_State_type>(std::move(__fn))) 01224 { } 01225 01226 explicit 01227 packaged_task(_Res(*__fn)(_ArgTypes...)) 01228 : _M_state(std::make_shared<_State_type>(__fn)) 01229 { } 01230 01231 template<typename _Fn, typename _Allocator> 01232 explicit 01233 packaged_task(allocator_arg_t __tag, const _Allocator& __a, _Fn __fn) 01234 : _M_state(std::allocate_shared<_State_type>(__a, std::move(__fn))) 01235 { } 01236 01237 ~packaged_task() 01238 { 01239 if (static_cast<bool>(_M_state) && !_M_state.unique()) 01240 _M_state->_M_break_promise(std::move(_M_state->_M_result)); 01241 } 01242 01243 // No copy 01244 packaged_task(packaged_task&) = delete; 01245 packaged_task& operator=(packaged_task&) = delete; 01246 01247 // Move support 01248 packaged_task(packaged_task&& __other) 01249 { this->swap(__other); } 01250 01251 packaged_task& operator=(packaged_task&& __other) 01252 { 01253 packaged_task(std::move(__other)).swap(*this); 01254 return *this; 01255 } 01256 01257 void 01258 swap(packaged_task& __other) 01259 { _M_state.swap(__other._M_state); } 01260 01261 bool 01262 valid() const 01263 { return static_cast<bool>(_M_state); } 01264 01265 // Result retrieval 01266 future<_Res> 01267 get_future() 01268 { return future<_Res>(_M_state); } 01269 01270 // Execution 01271 void 01272 operator()(_ArgTypes... __args) 01273 { 01274 __future_base::_State_base::_S_check(_M_state); 01275 _M_state->_M_run(std::forward<_ArgTypes>(__args)...); 01276 } 01277 01278 void 01279 reset() 01280 { 01281 __future_base::_State_base::_S_check(_M_state); 01282 packaged_task(std::move(_M_state->_M_task)).swap(*this); 01283 } 01284 }; 01285 01286 /// swap 01287 template<typename _Res, typename... _ArgTypes> 01288 inline void 01289 swap(packaged_task<_Res(_ArgTypes...)>& __x, 01290 packaged_task<_Res(_ArgTypes...)>& __y) 01291 { __x.swap(__y); } 01292 01293 template<typename _Res, typename _Alloc> 01294 struct uses_allocator<packaged_task<_Res>, _Alloc> 01295 : public true_type { }; 01296 01297 01298 template<typename _Res> 01299 class __future_base::_Deferred_state : public __future_base::_State_base 01300 { 01301 public: 01302 typedef _Res _Res_type; 01303 01304 explicit 01305 _Deferred_state(std::function<_Res()>&& __fn) 01306 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn)) 01307 { } 01308 01309 private: 01310 template<typename, typename> friend class _Task_setter; 01311 typedef typename __future_base::_Ptr<_Result<_Res>>::type _Ptr_type; 01312 _Ptr_type _M_result; 01313 std::function<_Res()> _M_fn; 01314 01315 virtual void 01316 _M_run_deferred() 01317 { 01318 _Task_setter<_Deferred_state> __setter{ this, _M_fn }; 01319 // safe to call multiple times so ignore failure 01320 _M_set_result(std::move(__setter), true); 01321 } 01322 }; 01323 01324 template<typename _Res> 01325 class __future_base::_Async_state : public __future_base::_State_base 01326 { 01327 public: 01328 typedef _Res _Res_type; 01329 01330 explicit 01331 _Async_state(std::function<_Res()>&& __fn) 01332 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn)), 01333 _M_thread(mem_fn(&_Async_state::_M_do_run), this) 01334 { } 01335 01336 ~_Async_state() { _M_thread.join(); } 01337 01338 private: 01339 void _M_do_run() 01340 { 01341 _Task_setter<_Async_state> __setter{ this, std::move(_M_fn) }; 01342 _M_set_result(std::move(__setter)); 01343 } 01344 01345 template<typename, typename> friend class _Task_setter; 01346 typedef typename __future_base::_Ptr<_Result<_Res>>::type _Ptr_type; 01347 _Ptr_type _M_result; 01348 std::function<_Res()> _M_fn; 01349 thread _M_thread; 01350 }; 01351 01352 /// async 01353 template<typename _Fn, typename... _Args> 01354 future<typename result_of<_Fn(_Args...)>::type> 01355 async(launch __policy, _Fn&& __fn, _Args&&... __args) 01356 { 01357 typedef typename result_of<_Fn(_Args...)>::type result_type; 01358 std::shared_ptr<__future_base::_State_base> __state; 01359 if (__policy == launch::async) 01360 { 01361 typedef typename __future_base::_Async_state<result_type> _State; 01362 __state = std::make_shared<_State>(std::bind<result_type>( 01363 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...)); 01364 } 01365 else 01366 { 01367 typedef typename __future_base::_Deferred_state<result_type> _State; 01368 __state = std::make_shared<_State>(std::bind<result_type>( 01369 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...)); 01370 } 01371 return future<result_type>(__state); 01372 } 01373 01374 /// async, potential overload 01375 template<typename _Fn, typename... _Args> 01376 inline typename 01377 __async_sfinae_helper<typename decay<_Fn>::type, _Fn, _Args...>::type 01378 async(_Fn&& __fn, _Args&&... __args) 01379 { 01380 return async(launch::any, std::forward<_Fn>(__fn), 01381 std::forward<_Args>(__args)...); 01382 } 01383 01384 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1 01385 // && _GLIBCXX_ATOMIC_BUILTINS_4 01386 01387 // @} group futures 01388 _GLIBCXX_END_NAMESPACE_VERSION 01389 } // namespace 01390 01391 #endif // __GXX_EXPERIMENTAL_CXX0X__ 01392 01393 #endif // _GLIBCXX_FUTURE