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