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