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