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(const _Result_base&) = delete; 00164 _Result_base& operator=(const _Result_base&) = delete; 00165 00166 // _M_destroy() allows derived classes to control deallocation 00167 virtual void _M_destroy() = 0; 00168 00169 struct _Deleter 00170 { 00171 void operator()(_Result_base* __fr) const { __fr->_M_destroy(); } 00172 }; 00173 00174 protected: 00175 _Result_base(); 00176 virtual ~_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 /// Base class for state between a promise and one or more 00277 /// associated futures. 00278 class _State_base 00279 { 00280 typedef _Ptr<_Result_base>::type _Ptr_type; 00281 00282 _Ptr_type _M_result; 00283 mutex _M_mutex; 00284 condition_variable _M_cond; 00285 atomic_flag _M_retrieved; 00286 once_flag _M_once; 00287 00288 public: 00289 _State_base() : _M_result(), _M_retrieved(ATOMIC_FLAG_INIT) { } 00290 _State_base(const _State_base&) = delete; 00291 _State_base& operator=(const _State_base&) = delete; 00292 virtual ~_State_base(); 00293 00294 _Result_base& 00295 wait() 00296 { 00297 _M_run_deferred(); 00298 unique_lock<mutex> __lock(_M_mutex); 00299 if (!_M_ready()) 00300 _M_cond.wait(__lock, std::bind<bool>(&_State_base::_M_ready, this)); 00301 return *_M_result; 00302 } 00303 00304 template<typename _Rep, typename _Period> 00305 bool 00306 wait_for(const chrono::duration<_Rep, _Period>& __rel) 00307 { 00308 unique_lock<mutex> __lock(_M_mutex); 00309 auto __bound = std::bind<bool>(&_State_base::_M_ready, this); 00310 return _M_ready() || _M_cond.wait_for(__lock, __rel, __bound); 00311 } 00312 00313 template<typename _Clock, typename _Duration> 00314 bool 00315 wait_until(const chrono::time_point<_Clock, _Duration>& __abs) 00316 { 00317 unique_lock<mutex> __lock(_M_mutex); 00318 auto __bound = std::bind<bool>(&_State_base::_M_ready, this); 00319 return _M_ready() || _M_cond.wait_until(__lock, __abs, __bound); 00320 } 00321 00322 void 00323 _M_set_result(function<_Ptr_type()> __res, bool __ignore_failure = false) 00324 { 00325 bool __set = __ignore_failure; 00326 // all calls to this function are serialized, 00327 // side-effects of invoking __res only happen once 00328 call_once(_M_once, &_State_base::_M_do_set, this, ref(__res), 00329 ref(__set)); 00330 if (!__set) 00331 __throw_future_error(int(future_errc::promise_already_satisfied)); 00332 } 00333 00334 void 00335 _M_break_promise(_Ptr_type __res) 00336 { 00337 if (static_cast<bool>(__res)) 00338 { 00339 error_code __ec(make_error_code(future_errc::broken_promise)); 00340 __res->_M_error = copy_exception(future_error(__ec)); 00341 { 00342 lock_guard<mutex> __lock(_M_mutex); 00343 _M_result.swap(__res); 00344 } 00345 _M_cond.notify_all(); 00346 } 00347 } 00348 00349 // Called when this object is passed to a future. 00350 void 00351 _M_set_retrieved_flag() 00352 { 00353 if (_M_retrieved.test_and_set()) 00354 __throw_future_error(int(future_errc::future_already_retrieved)); 00355 } 00356 00357 template<typename _Res, typename _Arg> 00358 struct _Setter; 00359 00360 // set lvalues 00361 template<typename _Res, typename _Arg> 00362 struct _Setter<_Res, _Arg&> 00363 { 00364 // check this is only used by promise<R>::set_value(const R&) 00365 // or promise<R>::set_value(R&) 00366 static_assert(is_same<_Res, _Arg&>::value // promise<R&> 00367 || is_same<const _Res, _Arg>::value, // promise<R> 00368 "Invalid specialisation"); 00369 00370 typename promise<_Res>::_Ptr_type operator()() 00371 { 00372 _State_base::_S_check(_M_promise->_M_future); 00373 _M_promise->_M_storage->_M_set(_M_arg); 00374 return std::move(_M_promise->_M_storage); 00375 } 00376 promise<_Res>* _M_promise; 00377 _Arg& _M_arg; 00378 }; 00379 00380 // set rvalues 00381 template<typename _Res> 00382 struct _Setter<_Res, _Res&&> 00383 { 00384 typename promise<_Res>::_Ptr_type operator()() 00385 { 00386 _State_base::_S_check(_M_promise->_M_future); 00387 _M_promise->_M_storage->_M_set(std::move(_M_arg)); 00388 return std::move(_M_promise->_M_storage); 00389 } 00390 promise<_Res>* _M_promise; 00391 _Res& _M_arg; 00392 }; 00393 00394 struct __exception_ptr_tag { }; 00395 00396 // set exceptions 00397 template<typename _Res> 00398 struct _Setter<_Res, __exception_ptr_tag> 00399 { 00400 typename promise<_Res>::_Ptr_type operator()() 00401 { 00402 _State_base::_S_check(_M_promise->_M_future); 00403 _M_promise->_M_storage->_M_error = _M_ex; 00404 return std::move(_M_promise->_M_storage); 00405 } 00406 00407 promise<_Res>* _M_promise; 00408 exception_ptr& _M_ex; 00409 }; 00410 00411 template<typename _Res, typename _Arg> 00412 static _Setter<_Res, _Arg&&> 00413 __setter(promise<_Res>* __prom, _Arg&& __arg) 00414 { 00415 return _Setter<_Res, _Arg&&>{ __prom, __arg }; 00416 } 00417 00418 template<typename _Res> 00419 static _Setter<_Res, __exception_ptr_tag> 00420 __setter(exception_ptr& __ex, promise<_Res>* __prom) 00421 { 00422 return _Setter<_Res, __exception_ptr_tag>{ __prom, __ex }; 00423 } 00424 00425 static _Setter<void, void> 00426 __setter(promise<void>* __prom); 00427 00428 template<typename _Tp> 00429 static bool 00430 _S_check(const shared_ptr<_Tp>& __p) 00431 { 00432 if (!static_cast<bool>(__p)) 00433 __throw_future_error((int)future_errc::no_state); 00434 } 00435 00436 private: 00437 void 00438 _M_do_set(function<_Ptr_type()>& __f, bool& __set) 00439 { 00440 _Ptr_type __res = __f(); 00441 { 00442 lock_guard<mutex> __lock(_M_mutex); 00443 _M_result.swap(__res); 00444 } 00445 _M_cond.notify_all(); 00446 __set = true; 00447 } 00448 00449 bool _M_ready() const { return static_cast<bool>(_M_result); } 00450 00451 virtual void _M_run_deferred() { } 00452 }; 00453 00454 template<typename _Res> 00455 class _Deferred_state; 00456 00457 template<typename _Res> 00458 class _Async_state; 00459 00460 template<typename _Signature> 00461 class _Task_state; 00462 00463 template<typename _StateT, typename _Res = typename _StateT::_Res_type> 00464 struct _Task_setter; 00465 }; 00466 00467 /// Partial specialization for reference types. 00468 template<typename _Res> 00469 struct __future_base::_Result<_Res&> : __future_base::_Result_base 00470 { 00471 _Result() : _M_value_ptr() { } 00472 00473 void _M_set(_Res& __res) { _M_value_ptr = &__res; } 00474 00475 _Res& _M_get() { return *_M_value_ptr; } 00476 00477 private: 00478 _Res* _M_value_ptr; 00479 00480 void _M_destroy() { delete this; } 00481 }; 00482 00483 /// Explicit specialization for void. 00484 template<> 00485 struct __future_base::_Result<void> : __future_base::_Result_base 00486 { 00487 private: 00488 void _M_destroy() { delete this; } 00489 }; 00490 00491 00492 /// Common implementation for future and shared_future. 00493 template<typename _Res> 00494 class __basic_future : public __future_base 00495 { 00496 protected: 00497 typedef shared_ptr<_State_base> __state_type; 00498 typedef __future_base::_Result<_Res>& __result_type; 00499 00500 private: 00501 __state_type _M_state; 00502 00503 public: 00504 // Disable copying. 00505 __basic_future(const __basic_future&) = delete; 00506 __basic_future& operator=(const __basic_future&) = delete; 00507 00508 bool 00509 valid() const { return static_cast<bool>(_M_state); } 00510 00511 void 00512 wait() const 00513 { 00514 _State_base::_S_check(_M_state); 00515 _M_state->wait(); 00516 } 00517 00518 template<typename _Rep, typename _Period> 00519 bool 00520 wait_for(const chrono::duration<_Rep, _Period>& __rel) const 00521 { 00522 _State_base::_S_check(_M_state); 00523 return _M_state->wait_for(__rel); 00524 } 00525 00526 template<typename _Clock, typename _Duration> 00527 bool 00528 wait_until(const chrono::time_point<_Clock, _Duration>& __abs) const 00529 { 00530 _State_base::_S_check(_M_state); 00531 return _M_state->wait_until(__abs); 00532 } 00533 00534 protected: 00535 /// Wait for the state to be ready and rethrow any stored exception 00536 __result_type 00537 _M_get_result() 00538 { 00539 _State_base::_S_check(_M_state); 00540 _Result_base& __res = _M_state->wait(); 00541 if (!(__res._M_error == 0)) 00542 rethrow_exception(__res._M_error); 00543 return static_cast<__result_type>(__res); 00544 } 00545 00546 void _M_swap(__basic_future& __that) 00547 { 00548 _M_state.swap(__that._M_state); 00549 } 00550 00551 // Construction of a future by promise::get_future() 00552 explicit 00553 __basic_future(const __state_type& __state) : _M_state(__state) 00554 { 00555 _State_base::_S_check(_M_state); 00556 _M_state->_M_set_retrieved_flag(); 00557 } 00558 00559 // Copy construction from a shared_future 00560 explicit 00561 __basic_future(const shared_future<_Res>&); 00562 00563 // Move construction from a shared_future 00564 explicit 00565 __basic_future(shared_future<_Res>&&); 00566 00567 // Move construction from a future 00568 explicit 00569 __basic_future(future<_Res>&&); 00570 00571 constexpr __basic_future() : _M_state() { } 00572 00573 struct _Reset 00574 { 00575 explicit _Reset(__basic_future& __fut) : _M_fut(__fut) { } 00576 ~_Reset() { _M_fut._M_state.reset(); } 00577 __basic_future& _M_fut; 00578 }; 00579 }; 00580 00581 00582 /// Primary template for future. 00583 template<typename _Res> 00584 class future : public __basic_future<_Res> 00585 { 00586 friend class promise<_Res>; 00587 template<typename> friend class packaged_task; 00588 template<typename _Fn, typename... _Args> 00589 friend future<typename result_of<_Fn(_Args...)>::type> 00590 async(launch, _Fn&&, _Args&&...); 00591 00592 typedef __basic_future<_Res> _Base_type; 00593 typedef typename _Base_type::__state_type __state_type; 00594 00595 explicit 00596 future(const __state_type& __state) : _Base_type(__state) { } 00597 00598 public: 00599 constexpr future() : _Base_type() { } 00600 00601 /// Move constructor 00602 future(future&& __uf) : _Base_type(std::move(__uf)) { } 00603 00604 // Disable copying 00605 future(const future&) = delete; 00606 future& operator=(const future&) = delete; 00607 00608 future& operator=(future&& __fut) 00609 { 00610 future(std::move(__fut))._M_swap(*this); 00611 return *this; 00612 } 00613 00614 /// Retrieving the value 00615 _Res 00616 get() 00617 { 00618 typename _Base_type::_Reset __reset(*this); 00619 return std::move(this->_M_get_result()._M_value()); 00620 } 00621 }; 00622 00623 /// Partial specialization for future<R&> 00624 template<typename _Res> 00625 class future<_Res&> : public __basic_future<_Res&> 00626 { 00627 friend class promise<_Res&>; 00628 template<typename> friend class packaged_task; 00629 template<typename _Fn, typename... _Args> 00630 friend future<typename result_of<_Fn(_Args...)>::type> 00631 async(launch, _Fn&&, _Args&&...); 00632 00633 typedef __basic_future<_Res&> _Base_type; 00634 typedef typename _Base_type::__state_type __state_type; 00635 00636 explicit 00637 future(const __state_type& __state) : _Base_type(__state) { } 00638 00639 public: 00640 constexpr future() : _Base_type() { } 00641 00642 /// Move constructor 00643 future(future&& __uf) : _Base_type(std::move(__uf)) { } 00644 00645 // Disable copying 00646 future(const future&) = delete; 00647 future& operator=(const future&) = delete; 00648 00649 future& operator=(future&& __fut) 00650 { 00651 future(std::move(__fut))._M_swap(*this); 00652 return *this; 00653 } 00654 00655 /// Retrieving the value 00656 _Res& 00657 get() 00658 { 00659 typename _Base_type::_Reset __reset(*this); 00660 return this->_M_get_result()._M_get(); 00661 } 00662 }; 00663 00664 /// Explicit specialization for future<void> 00665 template<> 00666 class future<void> : public __basic_future<void> 00667 { 00668 friend class promise<void>; 00669 template<typename> friend class packaged_task; 00670 template<typename _Fn, typename... _Args> 00671 friend future<typename result_of<_Fn(_Args...)>::type> 00672 async(launch, _Fn&&, _Args&&...); 00673 00674 typedef __basic_future<void> _Base_type; 00675 typedef typename _Base_type::__state_type __state_type; 00676 00677 explicit 00678 future(const __state_type& __state) : _Base_type(__state) { } 00679 00680 public: 00681 constexpr future() : _Base_type() { } 00682 00683 /// Move constructor 00684 future(future&& __uf) : _Base_type(std::move(__uf)) { } 00685 00686 // Disable copying 00687 future(const future&) = delete; 00688 future& operator=(const future&) = delete; 00689 00690 future& operator=(future&& __fut) 00691 { 00692 future(std::move(__fut))._M_swap(*this); 00693 return *this; 00694 } 00695 00696 /// Retrieving the value 00697 void 00698 get() 00699 { 00700 typename _Base_type::_Reset __reset(*this); 00701 this->_M_get_result(); 00702 } 00703 }; 00704 00705 00706 /// Primary template for shared_future. 00707 template<typename _Res> 00708 class shared_future : public __basic_future<_Res> 00709 { 00710 typedef __basic_future<_Res> _Base_type; 00711 00712 public: 00713 constexpr shared_future() : _Base_type() { } 00714 00715 /// Copy constructor 00716 shared_future(const shared_future& __sf) : _Base_type(__sf) { } 00717 00718 /// Construct from a future rvalue 00719 shared_future(future<_Res>&& __uf) 00720 : _Base_type(std::move(__uf)) 00721 { } 00722 00723 /// Construct from a shared_future rvalue 00724 shared_future(shared_future&& __sf) 00725 : _Base_type(std::move(__sf)) 00726 { } 00727 00728 shared_future& operator=(const shared_future& __sf) 00729 { 00730 shared_future(__sf)._M_swap(*this); 00731 return *this; 00732 } 00733 00734 shared_future& operator=(shared_future&& __sf) 00735 { 00736 shared_future(std::move(__sf))._M_swap(*this); 00737 return *this; 00738 } 00739 00740 /// Retrieving the value 00741 const _Res& 00742 get() 00743 { 00744 typename _Base_type::__result_type __r = this->_M_get_result(); 00745 _Res& __rs(__r._M_value()); 00746 return __rs; 00747 } 00748 }; 00749 00750 /// Partial specialization for shared_future<R&> 00751 template<typename _Res> 00752 class shared_future<_Res&> : public __basic_future<_Res&> 00753 { 00754 typedef __basic_future<_Res&> _Base_type; 00755 00756 public: 00757 constexpr shared_future() : _Base_type() { } 00758 00759 /// Copy constructor 00760 shared_future(const shared_future& __sf) : _Base_type(__sf) { } 00761 00762 /// Construct from a future rvalue 00763 shared_future(future<_Res&>&& __uf) 00764 : _Base_type(std::move(__uf)) 00765 { } 00766 00767 /// Construct from a shared_future rvalue 00768 shared_future(shared_future&& __sf) 00769 : _Base_type(std::move(__sf)) 00770 { } 00771 00772 shared_future& operator=(const shared_future& __sf) 00773 { 00774 shared_future(__sf)._M_swap(*this); 00775 return *this; 00776 } 00777 00778 shared_future& operator=(shared_future&& __sf) 00779 { 00780 shared_future(std::move(__sf))._M_swap(*this); 00781 return *this; 00782 } 00783 00784 /// Retrieving the value 00785 _Res& 00786 get() { return this->_M_get_result()._M_get(); } 00787 }; 00788 00789 /// Explicit specialization for shared_future<void> 00790 template<> 00791 class shared_future<void> : public __basic_future<void> 00792 { 00793 typedef __basic_future<void> _Base_type; 00794 00795 public: 00796 constexpr shared_future() : _Base_type() { } 00797 00798 /// Copy constructor 00799 shared_future(const shared_future& __sf) : _Base_type(__sf) { } 00800 00801 /// Construct from a future rvalue 00802 shared_future(future<void>&& __uf) 00803 : _Base_type(std::move(__uf)) 00804 { } 00805 00806 /// Construct from a shared_future rvalue 00807 shared_future(shared_future&& __sf) 00808 : _Base_type(std::move(__sf)) 00809 { } 00810 00811 shared_future& operator=(const shared_future& __sf) 00812 { 00813 shared_future(__sf)._M_swap(*this); 00814 return *this; 00815 } 00816 00817 shared_future& operator=(shared_future&& __sf) 00818 { 00819 shared_future(std::move(__sf))._M_swap(*this); 00820 return *this; 00821 } 00822 00823 // Retrieving the value 00824 void 00825 get() { this->_M_get_result(); } 00826 }; 00827 00828 // Now we can define the protected __basic_future constructors. 00829 template<typename _Res> 00830 inline __basic_future<_Res>:: 00831 __basic_future(const shared_future<_Res>& __sf) 00832 : _M_state(__sf._M_state) 00833 { } 00834 00835 template<typename _Res> 00836 inline __basic_future<_Res>:: 00837 __basic_future(shared_future<_Res>&& __sf) 00838 : _M_state(std::move(__sf._M_state)) 00839 { } 00840 00841 template<typename _Res> 00842 inline __basic_future<_Res>:: 00843 __basic_future(future<_Res>&& __uf) 00844 : _M_state(std::move(__uf._M_state)) 00845 { } 00846 00847 00848 /// Primary template for promise 00849 template<typename _Res> 00850 class promise 00851 { 00852 typedef __future_base::_State_base _State; 00853 typedef __future_base::_Result<_Res> _Res_type; 00854 typedef typename __future_base::_Ptr<_Res_type>::type _Ptr_type; 00855 template<typename, typename> friend class _State::_Setter; 00856 00857 shared_ptr<_State> _M_future; 00858 _Ptr_type _M_storage; 00859 00860 public: 00861 promise() 00862 : _M_future(std::make_shared<_State>()), 00863 _M_storage(new _Res_type()) 00864 { } 00865 00866 promise(promise&& __rhs) 00867 : _M_future(std::move(__rhs._M_future)), 00868 _M_storage(std::move(__rhs._M_storage)) 00869 { } 00870 00871 template<typename _Allocator> 00872 promise(allocator_arg_t, const _Allocator& __a) 00873 : _M_future(std::allocate_shared<_State>(__a)), 00874 _M_storage(__future_base::_S_allocate_result<_Res>(__a)) 00875 { } 00876 00877 promise(const promise&) = delete; 00878 00879 ~promise() 00880 { 00881 if (static_cast<bool>(_M_future) && !_M_future.unique()) 00882 _M_future->_M_break_promise(std::move(_M_storage)); 00883 } 00884 00885 // Assignment 00886 promise& 00887 operator=(promise&& __rhs) 00888 { 00889 promise(std::move(__rhs)).swap(*this); 00890 return *this; 00891 } 00892 00893 promise& operator=(const promise&) = delete; 00894 00895 void 00896 swap(promise& __rhs) 00897 { 00898 _M_future.swap(__rhs._M_future); 00899 _M_storage.swap(__rhs._M_storage); 00900 } 00901 00902 // Retrieving the result 00903 future<_Res> 00904 get_future() 00905 { return future<_Res>(_M_future); } 00906 00907 // Setting the result 00908 void 00909 set_value(const _Res& __r) 00910 { 00911 auto __setter = _State::__setter(this, __r); 00912 _M_future->_M_set_result(std::move(__setter)); 00913 } 00914 00915 void 00916 set_value(_Res&& __r) 00917 { 00918 auto __setter = _State::__setter(this, std::move(__r)); 00919 _M_future->_M_set_result(std::move(__setter)); 00920 } 00921 00922 void 00923 set_exception(exception_ptr __p) 00924 { 00925 auto __setter = _State::__setter(__p, this); 00926 _M_future->_M_set_result(std::move(__setter)); 00927 } 00928 }; 00929 00930 template<typename _Res> 00931 inline void 00932 swap(promise<_Res>& __x, promise<_Res>& __y) 00933 { __x.swap(__y); } 00934 00935 template<typename _Res, typename _Alloc> 00936 struct uses_allocator<promise<_Res>, _Alloc> 00937 : public true_type { }; 00938 00939 00940 /// Partial specialization for promise<R&> 00941 template<typename _Res> 00942 class promise<_Res&> 00943 { 00944 typedef __future_base::_State_base _State; 00945 typedef __future_base::_Result<_Res&> _Res_type; 00946 typedef typename __future_base::_Ptr<_Res_type>::type _Ptr_type; 00947 template<typename, typename> friend class _State::_Setter; 00948 00949 shared_ptr<_State> _M_future; 00950 _Ptr_type _M_storage; 00951 00952 public: 00953 promise() 00954 : _M_future(std::make_shared<_State>()), 00955 _M_storage(new _Res_type()) 00956 { } 00957 00958 promise(promise&& __rhs) 00959 : _M_future(std::move(__rhs._M_future)), 00960 _M_storage(std::move(__rhs._M_storage)) 00961 { } 00962 00963 template<typename _Allocator> 00964 promise(allocator_arg_t, const _Allocator& __a) 00965 : _M_future(std::allocate_shared<_State>(__a)), 00966 _M_storage(__future_base::_S_allocate_result<_Res&>(__a)) 00967 { } 00968 00969 promise(const promise&) = delete; 00970 00971 ~promise() 00972 { 00973 if (static_cast<bool>(_M_future) && !_M_future.unique()) 00974 _M_future->_M_break_promise(std::move(_M_storage)); 00975 } 00976 00977 // Assignment 00978 promise& 00979 operator=(promise&& __rhs) 00980 { 00981 promise(std::move(__rhs)).swap(*this); 00982 return *this; 00983 } 00984 00985 promise& operator=(const promise&) = delete; 00986 00987 void 00988 swap(promise& __rhs) 00989 { 00990 _M_future.swap(__rhs._M_future); 00991 _M_storage.swap(__rhs._M_storage); 00992 } 00993 00994 // Retrieving the result 00995 future<_Res&> 00996 get_future() 00997 { return future<_Res&>(_M_future); } 00998 00999 // Setting the result 01000 void 01001 set_value(_Res& __r) 01002 { 01003 auto __setter = _State::__setter(this, __r); 01004 _M_future->_M_set_result(std::move(__setter)); 01005 } 01006 01007 void 01008 set_exception(exception_ptr __p) 01009 { 01010 auto __setter = _State::__setter(__p, this); 01011 _M_future->_M_set_result(std::move(__setter)); 01012 } 01013 }; 01014 01015 /// Explicit specialization for promise<void> 01016 template<> 01017 class promise<void> 01018 { 01019 typedef __future_base::_State_base _State; 01020 typedef __future_base::_Result<void> _Res_type; 01021 typedef typename __future_base::_Ptr<_Res_type>::type _Ptr_type; 01022 template<typename, typename> friend class _State::_Setter; 01023 01024 shared_ptr<_State> _M_future; 01025 _Ptr_type _M_storage; 01026 01027 public: 01028 promise() 01029 : _M_future(std::make_shared<_State>()), 01030 _M_storage(new _Res_type()) 01031 { } 01032 01033 promise(promise&& __rhs) 01034 : _M_future(std::move(__rhs._M_future)), 01035 _M_storage(std::move(__rhs._M_storage)) 01036 { } 01037 01038 template<typename _Allocator> 01039 promise(allocator_arg_t, const _Allocator& __a) 01040 : _M_future(std::allocate_shared<_State>(__a)), 01041 _M_storage(__future_base::_S_allocate_result<void>(__a)) 01042 { } 01043 01044 promise(const promise&) = delete; 01045 01046 ~promise() 01047 { 01048 if (static_cast<bool>(_M_future) && !_M_future.unique()) 01049 _M_future->_M_break_promise(std::move(_M_storage)); 01050 } 01051 01052 // Assignment 01053 promise& 01054 operator=(promise&& __rhs) 01055 { 01056 promise(std::move(__rhs)).swap(*this); 01057 return *this; 01058 } 01059 01060 promise& operator=(const promise&) = delete; 01061 01062 void 01063 swap(promise& __rhs) 01064 { 01065 _M_future.swap(__rhs._M_future); 01066 _M_storage.swap(__rhs._M_storage); 01067 } 01068 01069 // Retrieving the result 01070 future<void> 01071 get_future() 01072 { return future<void>(_M_future); } 01073 01074 // Setting the result 01075 void set_value(); 01076 01077 void 01078 set_exception(exception_ptr __p) 01079 { 01080 auto __setter = _State::__setter(__p, this); 01081 _M_future->_M_set_result(std::move(__setter)); 01082 } 01083 }; 01084 01085 // set void 01086 template<> 01087 struct __future_base::_State_base::_Setter<void, void> 01088 { 01089 promise<void>::_Ptr_type operator()() 01090 { 01091 _State_base::_S_check(_M_promise->_M_future); 01092 return std::move(_M_promise->_M_storage); 01093 } 01094 01095 promise<void>* _M_promise; 01096 }; 01097 01098 inline __future_base::_State_base::_Setter<void, void> 01099 __future_base::_State_base::__setter(promise<void>* __prom) 01100 { 01101 return _Setter<void, void>{ __prom }; 01102 } 01103 01104 inline void 01105 promise<void>::set_value() 01106 { 01107 auto __setter = _State::__setter(this); 01108 _M_future->_M_set_result(std::move(__setter)); 01109 } 01110 01111 01112 template<typename _StateT, typename _Res> 01113 struct __future_base::_Task_setter 01114 { 01115 typename _StateT::_Ptr_type operator()() 01116 { 01117 __try 01118 { 01119 _M_state->_M_result->_M_set(_M_fn()); 01120 } 01121 __catch(...) 01122 { 01123 _M_state->_M_result->_M_error = current_exception(); 01124 } 01125 return std::move(_M_state->_M_result); 01126 } 01127 _StateT* _M_state; 01128 std::function<_Res()> _M_fn; 01129 }; 01130 01131 template<typename _StateT> 01132 struct __future_base::_Task_setter<_StateT, void> 01133 { 01134 typename _StateT::_Ptr_type operator()() 01135 { 01136 __try 01137 { 01138 _M_fn(); 01139 } 01140 __catch(...) 01141 { 01142 _M_state->_M_result->_M_error = current_exception(); 01143 } 01144 return std::move(_M_state->_M_result); 01145 } 01146 _StateT* _M_state; 01147 std::function<void()> _M_fn; 01148 }; 01149 01150 template<typename _Res, typename... _Args> 01151 struct __future_base::_Task_state<_Res(_Args...)> 01152 : __future_base::_State_base 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_base::_S_check(_M_state); 01267 _M_state->_M_run(std::forward<_ArgTypes>(__args)...); 01268 } 01269 01270 void 01271 reset() 01272 { 01273 __future_base::_State_base::_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_base 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_base 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_base> __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