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