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 _State_baseV2::_S_check(_M_promise->_M_future); 00466 _M_promise->_M_storage->_M_set(*_M_arg); 00467 return std::move(_M_promise->_M_storage); 00468 } 00469 promise<_Res>* _M_promise; 00470 _Arg* _M_arg; 00471 }; 00472 00473 // set rvalues 00474 template<typename _Res> 00475 struct _Setter<_Res, _Res&&> 00476 { 00477 // Used by std::promise to move construct the result. 00478 typename promise<_Res>::_Ptr_type operator()() const 00479 { 00480 _State_baseV2::_S_check(_M_promise->_M_future); 00481 _M_promise->_M_storage->_M_set(std::move(*_M_arg)); 00482 return std::move(_M_promise->_M_storage); 00483 } 00484 promise<_Res>* _M_promise; 00485 _Res* _M_arg; 00486 }; 00487 00488 struct __exception_ptr_tag { }; 00489 00490 // set exceptions 00491 template<typename _Res> 00492 struct _Setter<_Res, __exception_ptr_tag> 00493 { 00494 // Used by std::promise to store an exception as the result. 00495 typename promise<_Res>::_Ptr_type operator()() const 00496 { 00497 _State_baseV2::_S_check(_M_promise->_M_future); 00498 _M_promise->_M_storage->_M_error = *_M_ex; 00499 return std::move(_M_promise->_M_storage); 00500 } 00501 00502 promise<_Res>* _M_promise; 00503 exception_ptr* _M_ex; 00504 }; 00505 00506 template<typename _Res, typename _Arg> 00507 static _Setter<_Res, _Arg&&> 00508 __setter(promise<_Res>* __prom, _Arg&& __arg) 00509 { 00510 return _Setter<_Res, _Arg&&>{ __prom, std::__addressof(__arg) }; 00511 } 00512 00513 template<typename _Res> 00514 static _Setter<_Res, __exception_ptr_tag> 00515 __setter(exception_ptr& __ex, promise<_Res>* __prom) 00516 { 00517 return _Setter<_Res, __exception_ptr_tag>{ __prom, &__ex }; 00518 } 00519 00520 template<typename _Tp> 00521 static void 00522 _S_check(const shared_ptr<_Tp>& __p) 00523 { 00524 if (!static_cast<bool>(__p)) 00525 __throw_future_error((int)future_errc::no_state); 00526 } 00527 00528 private: 00529 // The function invoked with std::call_once(_M_once, ...). 00530 void 00531 _M_do_set(function<_Ptr_type()>* __f, bool* __did_set) 00532 { 00533 _Ptr_type __res = (*__f)(); 00534 // Notify the caller that we did try to set; if we do not throw an 00535 // exception, the caller will be aware that it did set (e.g., see 00536 // _M_set_result). 00537 *__did_set = true; 00538 _M_result.swap(__res); // nothrow 00539 } 00540 00541 // Wait for completion of async function. 00542 virtual void _M_complete_async() { } 00543 00544 // Return true if state corresponds to a deferred function. 00545 virtual bool _M_is_deferred_future() const { return false; } 00546 00547 struct _Make_ready final : __at_thread_exit_elt 00548 { 00549 weak_ptr<_State_baseV2> _M_shared_state; 00550 static void _S_run(void*); 00551 void _M_set(); 00552 }; 00553 }; 00554 00555 #ifdef _GLIBCXX_ASYNC_ABI_COMPAT 00556 class _State_base; 00557 class _Async_state_common; 00558 #else 00559 using _State_base = _State_baseV2; 00560 class _Async_state_commonV2; 00561 #endif 00562 00563 template<typename _BoundFn, typename = typename _BoundFn::result_type> 00564 class _Deferred_state; 00565 00566 template<typename _BoundFn, typename = typename _BoundFn::result_type> 00567 class _Async_state_impl; 00568 00569 template<typename _Signature> 00570 class _Task_state_base; 00571 00572 template<typename _Fn, typename _Alloc, typename _Signature> 00573 class _Task_state; 00574 00575 template<typename _BoundFn> 00576 static std::shared_ptr<_State_base> 00577 _S_make_deferred_state(_BoundFn&& __fn); 00578 00579 template<typename _BoundFn> 00580 static std::shared_ptr<_State_base> 00581 _S_make_async_state(_BoundFn&& __fn); 00582 00583 template<typename _Res_ptr, typename _Fn, 00584 typename _Res = typename _Res_ptr::element_type::result_type> 00585 struct _Task_setter; 00586 00587 template<typename _Res_ptr, typename _BoundFn> 00588 static _Task_setter<_Res_ptr, _BoundFn> 00589 _S_task_setter(_Res_ptr& __ptr, _BoundFn& __call) 00590 { 00591 return { std::__addressof(__ptr), std::__addressof(__call) }; 00592 } 00593 }; 00594 00595 /// Partial specialization for reference types. 00596 template<typename _Res> 00597 struct __future_base::_Result<_Res&> : __future_base::_Result_base 00598 { 00599 typedef _Res& result_type; 00600 00601 _Result() noexcept : _M_value_ptr() { } 00602 00603 void 00604 _M_set(_Res& __res) noexcept 00605 { _M_value_ptr = std::addressof(__res); } 00606 00607 _Res& _M_get() noexcept { return *_M_value_ptr; } 00608 00609 private: 00610 _Res* _M_value_ptr; 00611 00612 void _M_destroy() { delete this; } 00613 }; 00614 00615 /// Explicit specialization for void. 00616 template<> 00617 struct __future_base::_Result<void> : __future_base::_Result_base 00618 { 00619 typedef void result_type; 00620 00621 private: 00622 void _M_destroy() { delete this; } 00623 }; 00624 00625 #ifndef _GLIBCXX_ASYNC_ABI_COMPAT 00626 00627 // Allow _Setter objects to be stored locally in std::function 00628 template<typename _Res, typename _Arg> 00629 struct __is_location_invariant 00630 <__future_base::_State_base::_Setter<_Res, _Arg>> 00631 : true_type { }; 00632 00633 // Allow _Task_setter objects to be stored locally in std::function 00634 template<typename _Res_ptr, typename _Fn, typename _Res> 00635 struct __is_location_invariant 00636 <__future_base::_Task_setter<_Res_ptr, _Fn, _Res>> 00637 : true_type { }; 00638 00639 /// Common implementation for future and shared_future. 00640 template<typename _Res> 00641 class __basic_future : public __future_base 00642 { 00643 protected: 00644 typedef shared_ptr<_State_base> __state_type; 00645 typedef __future_base::_Result<_Res>& __result_type; 00646 00647 private: 00648 __state_type _M_state; 00649 00650 public: 00651 // Disable copying. 00652 __basic_future(const __basic_future&) = delete; 00653 __basic_future& operator=(const __basic_future&) = delete; 00654 00655 bool 00656 valid() const noexcept { return static_cast<bool>(_M_state); } 00657 00658 void 00659 wait() const 00660 { 00661 _State_base::_S_check(_M_state); 00662 _M_state->wait(); 00663 } 00664 00665 template<typename _Rep, typename _Period> 00666 future_status 00667 wait_for(const chrono::duration<_Rep, _Period>& __rel) const 00668 { 00669 _State_base::_S_check(_M_state); 00670 return _M_state->wait_for(__rel); 00671 } 00672 00673 template<typename _Clock, typename _Duration> 00674 future_status 00675 wait_until(const chrono::time_point<_Clock, _Duration>& __abs) const 00676 { 00677 _State_base::_S_check(_M_state); 00678 return _M_state->wait_until(__abs); 00679 } 00680 00681 protected: 00682 /// Wait for the state to be ready and rethrow any stored exception 00683 __result_type 00684 _M_get_result() const 00685 { 00686 _State_base::_S_check(_M_state); 00687 _Result_base& __res = _M_state->wait(); 00688 if (!(__res._M_error == 0)) 00689 rethrow_exception(__res._M_error); 00690 return static_cast<__result_type>(__res); 00691 } 00692 00693 void _M_swap(__basic_future& __that) noexcept 00694 { 00695 _M_state.swap(__that._M_state); 00696 } 00697 00698 // Construction of a future by promise::get_future() 00699 explicit 00700 __basic_future(const __state_type& __state) : _M_state(__state) 00701 { 00702 _State_base::_S_check(_M_state); 00703 _M_state->_M_set_retrieved_flag(); 00704 } 00705 00706 // Copy construction from a shared_future 00707 explicit 00708 __basic_future(const shared_future<_Res>&) noexcept; 00709 00710 // Move construction from a shared_future 00711 explicit 00712 __basic_future(shared_future<_Res>&&) noexcept; 00713 00714 // Move construction from a future 00715 explicit 00716 __basic_future(future<_Res>&&) noexcept; 00717 00718 constexpr __basic_future() noexcept : _M_state() { } 00719 00720 struct _Reset 00721 { 00722 explicit _Reset(__basic_future& __fut) noexcept : _M_fut(__fut) { } 00723 ~_Reset() { _M_fut._M_state.reset(); } 00724 __basic_future& _M_fut; 00725 }; 00726 }; 00727 00728 00729 /// Primary template for future. 00730 template<typename _Res> 00731 class future : public __basic_future<_Res> 00732 { 00733 friend class promise<_Res>; 00734 template<typename> friend class packaged_task; 00735 template<typename _Fn, typename... _Args> 00736 friend future<__async_result_of<_Fn, _Args...>> 00737 async(launch, _Fn&&, _Args&&...); 00738 00739 typedef __basic_future<_Res> _Base_type; 00740 typedef typename _Base_type::__state_type __state_type; 00741 00742 explicit 00743 future(const __state_type& __state) : _Base_type(__state) { } 00744 00745 public: 00746 constexpr future() noexcept : _Base_type() { } 00747 00748 /// Move constructor 00749 future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { } 00750 00751 // Disable copying 00752 future(const future&) = delete; 00753 future& operator=(const future&) = delete; 00754 00755 future& operator=(future&& __fut) noexcept 00756 { 00757 future(std::move(__fut))._M_swap(*this); 00758 return *this; 00759 } 00760 00761 /// Retrieving the value 00762 _Res 00763 get() 00764 { 00765 typename _Base_type::_Reset __reset(*this); 00766 return std::move(this->_M_get_result()._M_value()); 00767 } 00768 00769 shared_future<_Res> share(); 00770 }; 00771 00772 /// Partial specialization for future<R&> 00773 template<typename _Res> 00774 class future<_Res&> : public __basic_future<_Res&> 00775 { 00776 friend class promise<_Res&>; 00777 template<typename> friend class packaged_task; 00778 template<typename _Fn, typename... _Args> 00779 friend future<__async_result_of<_Fn, _Args...>> 00780 async(launch, _Fn&&, _Args&&...); 00781 00782 typedef __basic_future<_Res&> _Base_type; 00783 typedef typename _Base_type::__state_type __state_type; 00784 00785 explicit 00786 future(const __state_type& __state) : _Base_type(__state) { } 00787 00788 public: 00789 constexpr future() noexcept : _Base_type() { } 00790 00791 /// Move constructor 00792 future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { } 00793 00794 // Disable copying 00795 future(const future&) = delete; 00796 future& operator=(const future&) = delete; 00797 00798 future& operator=(future&& __fut) noexcept 00799 { 00800 future(std::move(__fut))._M_swap(*this); 00801 return *this; 00802 } 00803 00804 /// Retrieving the value 00805 _Res& 00806 get() 00807 { 00808 typename _Base_type::_Reset __reset(*this); 00809 return this->_M_get_result()._M_get(); 00810 } 00811 00812 shared_future<_Res&> share(); 00813 }; 00814 00815 /// Explicit specialization for future<void> 00816 template<> 00817 class future<void> : public __basic_future<void> 00818 { 00819 friend class promise<void>; 00820 template<typename> friend class packaged_task; 00821 template<typename _Fn, typename... _Args> 00822 friend future<__async_result_of<_Fn, _Args...>> 00823 async(launch, _Fn&&, _Args&&...); 00824 00825 typedef __basic_future<void> _Base_type; 00826 typedef typename _Base_type::__state_type __state_type; 00827 00828 explicit 00829 future(const __state_type& __state) : _Base_type(__state) { } 00830 00831 public: 00832 constexpr future() noexcept : _Base_type() { } 00833 00834 /// Move constructor 00835 future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { } 00836 00837 // Disable copying 00838 future(const future&) = delete; 00839 future& operator=(const future&) = delete; 00840 00841 future& operator=(future&& __fut) noexcept 00842 { 00843 future(std::move(__fut))._M_swap(*this); 00844 return *this; 00845 } 00846 00847 /// Retrieving the value 00848 void 00849 get() 00850 { 00851 typename _Base_type::_Reset __reset(*this); 00852 this->_M_get_result(); 00853 } 00854 00855 shared_future<void> share(); 00856 }; 00857 00858 00859 /// Primary template for shared_future. 00860 template<typename _Res> 00861 class shared_future : public __basic_future<_Res> 00862 { 00863 typedef __basic_future<_Res> _Base_type; 00864 00865 public: 00866 constexpr shared_future() noexcept : _Base_type() { } 00867 00868 /// Copy constructor 00869 shared_future(const shared_future& __sf) : _Base_type(__sf) { } 00870 00871 /// Construct from a future rvalue 00872 shared_future(future<_Res>&& __uf) noexcept 00873 : _Base_type(std::move(__uf)) 00874 { } 00875 00876 /// Construct from a shared_future rvalue 00877 shared_future(shared_future&& __sf) noexcept 00878 : _Base_type(std::move(__sf)) 00879 { } 00880 00881 shared_future& operator=(const shared_future& __sf) 00882 { 00883 shared_future(__sf)._M_swap(*this); 00884 return *this; 00885 } 00886 00887 shared_future& operator=(shared_future&& __sf) noexcept 00888 { 00889 shared_future(std::move(__sf))._M_swap(*this); 00890 return *this; 00891 } 00892 00893 /// Retrieving the value 00894 const _Res& 00895 get() const { return this->_M_get_result()._M_value(); } 00896 }; 00897 00898 /// Partial specialization for shared_future<R&> 00899 template<typename _Res> 00900 class shared_future<_Res&> : public __basic_future<_Res&> 00901 { 00902 typedef __basic_future<_Res&> _Base_type; 00903 00904 public: 00905 constexpr shared_future() noexcept : _Base_type() { } 00906 00907 /// Copy constructor 00908 shared_future(const shared_future& __sf) : _Base_type(__sf) { } 00909 00910 /// Construct from a future rvalue 00911 shared_future(future<_Res&>&& __uf) noexcept 00912 : _Base_type(std::move(__uf)) 00913 { } 00914 00915 /// Construct from a shared_future rvalue 00916 shared_future(shared_future&& __sf) noexcept 00917 : _Base_type(std::move(__sf)) 00918 { } 00919 00920 shared_future& operator=(const shared_future& __sf) 00921 { 00922 shared_future(__sf)._M_swap(*this); 00923 return *this; 00924 } 00925 00926 shared_future& operator=(shared_future&& __sf) noexcept 00927 { 00928 shared_future(std::move(__sf))._M_swap(*this); 00929 return *this; 00930 } 00931 00932 /// Retrieving the value 00933 _Res& 00934 get() const { return this->_M_get_result()._M_get(); } 00935 }; 00936 00937 /// Explicit specialization for shared_future<void> 00938 template<> 00939 class shared_future<void> : public __basic_future<void> 00940 { 00941 typedef __basic_future<void> _Base_type; 00942 00943 public: 00944 constexpr shared_future() noexcept : _Base_type() { } 00945 00946 /// Copy constructor 00947 shared_future(const shared_future& __sf) : _Base_type(__sf) { } 00948 00949 /// Construct from a future rvalue 00950 shared_future(future<void>&& __uf) noexcept 00951 : _Base_type(std::move(__uf)) 00952 { } 00953 00954 /// Construct from a shared_future rvalue 00955 shared_future(shared_future&& __sf) noexcept 00956 : _Base_type(std::move(__sf)) 00957 { } 00958 00959 shared_future& operator=(const shared_future& __sf) 00960 { 00961 shared_future(__sf)._M_swap(*this); 00962 return *this; 00963 } 00964 00965 shared_future& operator=(shared_future&& __sf) noexcept 00966 { 00967 shared_future(std::move(__sf))._M_swap(*this); 00968 return *this; 00969 } 00970 00971 // Retrieving the value 00972 void 00973 get() const { this->_M_get_result(); } 00974 }; 00975 00976 // Now we can define the protected __basic_future constructors. 00977 template<typename _Res> 00978 inline __basic_future<_Res>:: 00979 __basic_future(const shared_future<_Res>& __sf) noexcept 00980 : _M_state(__sf._M_state) 00981 { } 00982 00983 template<typename _Res> 00984 inline __basic_future<_Res>:: 00985 __basic_future(shared_future<_Res>&& __sf) noexcept 00986 : _M_state(std::move(__sf._M_state)) 00987 { } 00988 00989 template<typename _Res> 00990 inline __basic_future<_Res>:: 00991 __basic_future(future<_Res>&& __uf) noexcept 00992 : _M_state(std::move(__uf._M_state)) 00993 { } 00994 00995 template<typename _Res> 00996 inline shared_future<_Res> 00997 future<_Res>::share() 00998 { return shared_future<_Res>(std::move(*this)); } 00999 01000 template<typename _Res> 01001 inline shared_future<_Res&> 01002 future<_Res&>::share() 01003 { return shared_future<_Res&>(std::move(*this)); } 01004 01005 inline shared_future<void> 01006 future<void>::share() 01007 { return shared_future<void>(std::move(*this)); } 01008 01009 /// Primary template for promise 01010 template<typename _Res> 01011 class promise 01012 { 01013 typedef __future_base::_State_base _State; 01014 typedef __future_base::_Result<_Res> _Res_type; 01015 typedef __future_base::_Ptr<_Res_type> _Ptr_type; 01016 template<typename, typename> friend class _State::_Setter; 01017 01018 shared_ptr<_State> _M_future; 01019 _Ptr_type _M_storage; 01020 01021 public: 01022 promise() 01023 : _M_future(std::make_shared<_State>()), 01024 _M_storage(new _Res_type()) 01025 { } 01026 01027 promise(promise&& __rhs) noexcept 01028 : _M_future(std::move(__rhs._M_future)), 01029 _M_storage(std::move(__rhs._M_storage)) 01030 { } 01031 01032 template<typename _Allocator> 01033 promise(allocator_arg_t, const _Allocator& __a) 01034 : _M_future(std::allocate_shared<_State>(__a)), 01035 _M_storage(__future_base::_S_allocate_result<_Res>(__a)) 01036 { } 01037 01038 template<typename _Allocator> 01039 promise(allocator_arg_t, const _Allocator&, promise&& __rhs) 01040 : _M_future(std::move(__rhs._M_future)), 01041 _M_storage(std::move(__rhs._M_storage)) 01042 { } 01043 01044 promise(const promise&) = delete; 01045 01046 ~promise() 01047 { 01048 if (static_cast<bool>(_M_future) && !_M_future.unique()) 01049 _M_future->_M_break_promise(std::move(_M_storage)); 01050 } 01051 01052 // Assignment 01053 promise& 01054 operator=(promise&& __rhs) noexcept 01055 { 01056 promise(std::move(__rhs)).swap(*this); 01057 return *this; 01058 } 01059 01060 promise& operator=(const promise&) = delete; 01061 01062 void 01063 swap(promise& __rhs) noexcept 01064 { 01065 _M_future.swap(__rhs._M_future); 01066 _M_storage.swap(__rhs._M_storage); 01067 } 01068 01069 // Retrieving the result 01070 future<_Res> 01071 get_future() 01072 { return future<_Res>(_M_future); } 01073 01074 // Setting the result 01075 void 01076 set_value(const _Res& __r) 01077 { _M_future->_M_set_result(_State::__setter(this, __r)); } 01078 01079 void 01080 set_value(_Res&& __r) 01081 { _M_future->_M_set_result(_State::__setter(this, std::move(__r))); } 01082 01083 void 01084 set_exception(exception_ptr __p) 01085 { _M_future->_M_set_result(_State::__setter(__p, this)); } 01086 01087 void 01088 set_value_at_thread_exit(const _Res& __r) 01089 { 01090 _M_future->_M_set_delayed_result(_State::__setter(this, __r), 01091 _M_future); 01092 } 01093 01094 void 01095 set_value_at_thread_exit(_Res&& __r) 01096 { 01097 _M_future->_M_set_delayed_result( 01098 _State::__setter(this, std::move(__r)), _M_future); 01099 } 01100 01101 void 01102 set_exception_at_thread_exit(exception_ptr __p) 01103 { 01104 _M_future->_M_set_delayed_result(_State::__setter(__p, this), 01105 _M_future); 01106 } 01107 }; 01108 01109 template<typename _Res> 01110 inline void 01111 swap(promise<_Res>& __x, promise<_Res>& __y) noexcept 01112 { __x.swap(__y); } 01113 01114 template<typename _Res, typename _Alloc> 01115 struct uses_allocator<promise<_Res>, _Alloc> 01116 : public true_type { }; 01117 01118 01119 /// Partial specialization for promise<R&> 01120 template<typename _Res> 01121 class promise<_Res&> 01122 { 01123 typedef __future_base::_State_base _State; 01124 typedef __future_base::_Result<_Res&> _Res_type; 01125 typedef __future_base::_Ptr<_Res_type> _Ptr_type; 01126 template<typename, typename> friend class _State::_Setter; 01127 01128 shared_ptr<_State> _M_future; 01129 _Ptr_type _M_storage; 01130 01131 public: 01132 promise() 01133 : _M_future(std::make_shared<_State>()), 01134 _M_storage(new _Res_type()) 01135 { } 01136 01137 promise(promise&& __rhs) noexcept 01138 : _M_future(std::move(__rhs._M_future)), 01139 _M_storage(std::move(__rhs._M_storage)) 01140 { } 01141 01142 template<typename _Allocator> 01143 promise(allocator_arg_t, const _Allocator& __a) 01144 : _M_future(std::allocate_shared<_State>(__a)), 01145 _M_storage(__future_base::_S_allocate_result<_Res&>(__a)) 01146 { } 01147 01148 template<typename _Allocator> 01149 promise(allocator_arg_t, const _Allocator&, promise&& __rhs) 01150 : _M_future(std::move(__rhs._M_future)), 01151 _M_storage(std::move(__rhs._M_storage)) 01152 { } 01153 01154 promise(const promise&) = delete; 01155 01156 ~promise() 01157 { 01158 if (static_cast<bool>(_M_future) && !_M_future.unique()) 01159 _M_future->_M_break_promise(std::move(_M_storage)); 01160 } 01161 01162 // Assignment 01163 promise& 01164 operator=(promise&& __rhs) noexcept 01165 { 01166 promise(std::move(__rhs)).swap(*this); 01167 return *this; 01168 } 01169 01170 promise& operator=(const promise&) = delete; 01171 01172 void 01173 swap(promise& __rhs) noexcept 01174 { 01175 _M_future.swap(__rhs._M_future); 01176 _M_storage.swap(__rhs._M_storage); 01177 } 01178 01179 // Retrieving the result 01180 future<_Res&> 01181 get_future() 01182 { return future<_Res&>(_M_future); } 01183 01184 // Setting the result 01185 void 01186 set_value(_Res& __r) 01187 { _M_future->_M_set_result(_State::__setter(this, __r)); } 01188 01189 void 01190 set_exception(exception_ptr __p) 01191 { _M_future->_M_set_result(_State::__setter(__p, this)); } 01192 01193 void 01194 set_value_at_thread_exit(_Res& __r) 01195 { 01196 _M_future->_M_set_delayed_result(_State::__setter(this, __r), 01197 _M_future); 01198 } 01199 01200 void 01201 set_exception_at_thread_exit(exception_ptr __p) 01202 { 01203 _M_future->_M_set_delayed_result(_State::__setter(__p, this), 01204 _M_future); 01205 } 01206 }; 01207 01208 /// Explicit specialization for promise<void> 01209 template<> 01210 class promise<void> 01211 { 01212 typedef __future_base::_State_base _State; 01213 typedef __future_base::_Result<void> _Res_type; 01214 typedef __future_base::_Ptr<_Res_type> _Ptr_type; 01215 template<typename, typename> friend class _State::_Setter; 01216 01217 shared_ptr<_State> _M_future; 01218 _Ptr_type _M_storage; 01219 01220 public: 01221 promise() 01222 : _M_future(std::make_shared<_State>()), 01223 _M_storage(new _Res_type()) 01224 { } 01225 01226 promise(promise&& __rhs) noexcept 01227 : _M_future(std::move(__rhs._M_future)), 01228 _M_storage(std::move(__rhs._M_storage)) 01229 { } 01230 01231 template<typename _Allocator> 01232 promise(allocator_arg_t, const _Allocator& __a) 01233 : _M_future(std::allocate_shared<_State>(__a)), 01234 _M_storage(__future_base::_S_allocate_result<void>(__a)) 01235 { } 01236 01237 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01238 // 2095. missing constructors needed for uses-allocator construction 01239 template<typename _Allocator> 01240 promise(allocator_arg_t, const _Allocator&, promise&& __rhs) 01241 : _M_future(std::move(__rhs._M_future)), 01242 _M_storage(std::move(__rhs._M_storage)) 01243 { } 01244 01245 promise(const promise&) = delete; 01246 01247 ~promise() 01248 { 01249 if (static_cast<bool>(_M_future) && !_M_future.unique()) 01250 _M_future->_M_break_promise(std::move(_M_storage)); 01251 } 01252 01253 // Assignment 01254 promise& 01255 operator=(promise&& __rhs) noexcept 01256 { 01257 promise(std::move(__rhs)).swap(*this); 01258 return *this; 01259 } 01260 01261 promise& operator=(const promise&) = delete; 01262 01263 void 01264 swap(promise& __rhs) noexcept 01265 { 01266 _M_future.swap(__rhs._M_future); 01267 _M_storage.swap(__rhs._M_storage); 01268 } 01269 01270 // Retrieving the result 01271 future<void> 01272 get_future() 01273 { return future<void>(_M_future); } 01274 01275 // Setting the result 01276 void set_value(); 01277 01278 void 01279 set_exception(exception_ptr __p) 01280 { _M_future->_M_set_result(_State::__setter(__p, this)); } 01281 01282 void 01283 set_value_at_thread_exit(); 01284 01285 void 01286 set_exception_at_thread_exit(exception_ptr __p) 01287 { 01288 _M_future->_M_set_delayed_result(_State::__setter(__p, this), 01289 _M_future); 01290 } 01291 }; 01292 01293 // set void 01294 template<> 01295 struct __future_base::_State_base::_Setter<void, void> 01296 { 01297 promise<void>::_Ptr_type operator()() const 01298 { 01299 _State_base::_S_check(_M_promise->_M_future); 01300 return std::move(_M_promise->_M_storage); 01301 } 01302 01303 promise<void>* _M_promise; 01304 }; 01305 01306 inline void 01307 promise<void>::set_value() 01308 { _M_future->_M_set_result(_State::_Setter<void, void>{ this }); } 01309 01310 inline void 01311 promise<void>::set_value_at_thread_exit() 01312 { 01313 _M_future->_M_set_delayed_result(_State::_Setter<void, void>{this}, 01314 _M_future); 01315 } 01316 01317 template<typename _Ptr_type, typename _Fn, typename _Res> 01318 struct __future_base::_Task_setter 01319 { 01320 // Invoke the function and provide the result to the caller. 01321 _Ptr_type operator()() const 01322 { 01323 __try 01324 { 01325 (*_M_result)->_M_set((*_M_fn)()); 01326 } 01327 __catch(const __cxxabiv1::__forced_unwind&) 01328 { 01329 __throw_exception_again; // will cause broken_promise 01330 } 01331 __catch(...) 01332 { 01333 (*_M_result)->_M_error = current_exception(); 01334 } 01335 return std::move(*_M_result); 01336 } 01337 _Ptr_type* _M_result; 01338 _Fn* _M_fn; 01339 }; 01340 01341 template<typename _Ptr_type, typename _Fn> 01342 struct __future_base::_Task_setter<_Ptr_type, _Fn, void> 01343 { 01344 _Ptr_type operator()() const 01345 { 01346 __try 01347 { 01348 (*_M_fn)(); 01349 } 01350 __catch(const __cxxabiv1::__forced_unwind&) 01351 { 01352 __throw_exception_again; // will cause broken_promise 01353 } 01354 __catch(...) 01355 { 01356 (*_M_result)->_M_error = current_exception(); 01357 } 01358 return std::move(*_M_result); 01359 } 01360 _Ptr_type* _M_result; 01361 _Fn* _M_fn; 01362 }; 01363 01364 // Holds storage for a packaged_task's result. 01365 template<typename _Res, typename... _Args> 01366 struct __future_base::_Task_state_base<_Res(_Args...)> 01367 : __future_base::_State_base 01368 { 01369 typedef _Res _Res_type; 01370 01371 template<typename _Alloc> 01372 _Task_state_base(const _Alloc& __a) 01373 : _M_result(_S_allocate_result<_Res>(__a)) 01374 { } 01375 01376 // Invoke the stored task and make the state ready. 01377 virtual void 01378 _M_run(_Args&&... __args) = 0; 01379 01380 // Invoke the stored task and make the state ready at thread exit. 01381 virtual void 01382 _M_run_delayed(_Args&&... __args, weak_ptr<_State_base>) = 0; 01383 01384 virtual shared_ptr<_Task_state_base> 01385 _M_reset() = 0; 01386 01387 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type; 01388 _Ptr_type _M_result; 01389 }; 01390 01391 // Holds a packaged_task's stored task. 01392 template<typename _Fn, typename _Alloc, typename _Res, typename... _Args> 01393 struct __future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)> final 01394 : __future_base::_Task_state_base<_Res(_Args...)> 01395 { 01396 template<typename _Fn2> 01397 _Task_state(_Fn2&& __fn, const _Alloc& __a) 01398 : _Task_state_base<_Res(_Args...)>(__a), 01399 _M_impl(std::forward<_Fn2>(__fn), __a) 01400 { } 01401 01402 private: 01403 virtual void 01404 _M_run(_Args&&... __args) 01405 { 01406 // bound arguments decay so wrap lvalue references 01407 auto __boundfn = std::__bind_simple(std::ref(_M_impl._M_fn), 01408 _S_maybe_wrap_ref(std::forward<_Args>(__args))...); 01409 this->_M_set_result(_S_task_setter(this->_M_result, __boundfn)); 01410 } 01411 01412 virtual void 01413 _M_run_delayed(_Args&&... __args, weak_ptr<_State_base> __self) 01414 { 01415 // bound arguments decay so wrap lvalue references 01416 auto __boundfn = std::__bind_simple(std::ref(_M_impl._M_fn), 01417 _S_maybe_wrap_ref(std::forward<_Args>(__args))...); 01418 this->_M_set_delayed_result(_S_task_setter(this->_M_result, __boundfn), 01419 std::move(__self)); 01420 } 01421 01422 virtual shared_ptr<_Task_state_base<_Res(_Args...)>> 01423 _M_reset(); 01424 01425 template<typename _Tp> 01426 static reference_wrapper<_Tp> 01427 _S_maybe_wrap_ref(_Tp& __t) 01428 { return std::ref(__t); } 01429 01430 template<typename _Tp> 01431 static 01432 typename enable_if<!is_lvalue_reference<_Tp>::value, _Tp>::type&& 01433 _S_maybe_wrap_ref(_Tp&& __t) 01434 { return std::forward<_Tp>(__t); } 01435 01436 struct _Impl : _Alloc 01437 { 01438 template<typename _Fn2> 01439 _Impl(_Fn2&& __fn, const _Alloc& __a) 01440 : _Alloc(__a), _M_fn(std::forward<_Fn2>(__fn)) { } 01441 _Fn _M_fn; 01442 } _M_impl; 01443 }; 01444 01445 template<typename _Signature, typename _Fn, typename _Alloc> 01446 static shared_ptr<__future_base::_Task_state_base<_Signature>> 01447 __create_task_state(_Fn&& __fn, const _Alloc& __a) 01448 { 01449 typedef typename decay<_Fn>::type _Fn2; 01450 typedef __future_base::_Task_state<_Fn2, _Alloc, _Signature> _State; 01451 return std::allocate_shared<_State>(__a, std::forward<_Fn>(__fn), __a); 01452 } 01453 01454 template<typename _Fn, typename _Alloc, typename _Res, typename... _Args> 01455 shared_ptr<__future_base::_Task_state_base<_Res(_Args...)>> 01456 __future_base::_Task_state<_Fn, _Alloc, _Res(_Args...)>::_M_reset() 01457 { 01458 return __create_task_state<_Res(_Args...)>(std::move(_M_impl._M_fn), 01459 static_cast<_Alloc&>(_M_impl)); 01460 } 01461 01462 template<typename _Task, typename _Fn, bool 01463 = is_same<_Task, typename decay<_Fn>::type>::value> 01464 struct __constrain_pkgdtask 01465 { typedef void __type; }; 01466 01467 template<typename _Task, typename _Fn> 01468 struct __constrain_pkgdtask<_Task, _Fn, true> 01469 { }; 01470 01471 /// packaged_task 01472 template<typename _Res, typename... _ArgTypes> 01473 class packaged_task<_Res(_ArgTypes...)> 01474 { 01475 typedef __future_base::_Task_state_base<_Res(_ArgTypes...)> _State_type; 01476 shared_ptr<_State_type> _M_state; 01477 01478 public: 01479 // Construction and destruction 01480 packaged_task() noexcept { } 01481 01482 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01483 // 2095. missing constructors needed for uses-allocator construction 01484 template<typename _Allocator> 01485 packaged_task(allocator_arg_t, const _Allocator& __a) noexcept 01486 { } 01487 01488 template<typename _Fn, typename = typename 01489 __constrain_pkgdtask<packaged_task, _Fn>::__type> 01490 explicit 01491 packaged_task(_Fn&& __fn) 01492 : packaged_task(allocator_arg, std::allocator<int>(), 01493 std::forward<_Fn>(__fn)) 01494 { } 01495 01496 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01497 // 2097. packaged_task constructors should be constrained 01498 // 2407. [this constructor should not be] explicit 01499 template<typename _Fn, typename _Alloc, typename = typename 01500 __constrain_pkgdtask<packaged_task, _Fn>::__type> 01501 packaged_task(allocator_arg_t, const _Alloc& __a, _Fn&& __fn) 01502 : _M_state(__create_task_state<_Res(_ArgTypes...)>( 01503 std::forward<_Fn>(__fn), __a)) 01504 { } 01505 01506 ~packaged_task() 01507 { 01508 if (static_cast<bool>(_M_state) && !_M_state.unique()) 01509 _M_state->_M_break_promise(std::move(_M_state->_M_result)); 01510 } 01511 01512 // No copy 01513 packaged_task(const packaged_task&) = delete; 01514 packaged_task& operator=(const packaged_task&) = delete; 01515 01516 template<typename _Allocator> 01517 packaged_task(allocator_arg_t, const _Allocator&, 01518 const packaged_task&) = delete; 01519 01520 // Move support 01521 packaged_task(packaged_task&& __other) noexcept 01522 { this->swap(__other); } 01523 01524 template<typename _Allocator> 01525 packaged_task(allocator_arg_t, const _Allocator&, 01526 packaged_task&& __other) noexcept 01527 { this->swap(__other); } 01528 01529 packaged_task& operator=(packaged_task&& __other) noexcept 01530 { 01531 packaged_task(std::move(__other)).swap(*this); 01532 return *this; 01533 } 01534 01535 void 01536 swap(packaged_task& __other) noexcept 01537 { _M_state.swap(__other._M_state); } 01538 01539 bool 01540 valid() const noexcept 01541 { return static_cast<bool>(_M_state); } 01542 01543 // Result retrieval 01544 future<_Res> 01545 get_future() 01546 { return future<_Res>(_M_state); } 01547 01548 // Execution 01549 void 01550 operator()(_ArgTypes... __args) 01551 { 01552 __future_base::_State_base::_S_check(_M_state); 01553 _M_state->_M_run(std::forward<_ArgTypes>(__args)...); 01554 } 01555 01556 void 01557 make_ready_at_thread_exit(_ArgTypes... __args) 01558 { 01559 __future_base::_State_base::_S_check(_M_state); 01560 _M_state->_M_run_delayed(std::forward<_ArgTypes>(__args)..., _M_state); 01561 } 01562 01563 void 01564 reset() 01565 { 01566 __future_base::_State_base::_S_check(_M_state); 01567 packaged_task __tmp; 01568 __tmp._M_state = _M_state; 01569 _M_state = _M_state->_M_reset(); 01570 } 01571 }; 01572 01573 /// swap 01574 template<typename _Res, typename... _ArgTypes> 01575 inline void 01576 swap(packaged_task<_Res(_ArgTypes...)>& __x, 01577 packaged_task<_Res(_ArgTypes...)>& __y) noexcept 01578 { __x.swap(__y); } 01579 01580 template<typename _Res, typename _Alloc> 01581 struct uses_allocator<packaged_task<_Res>, _Alloc> 01582 : public true_type { }; 01583 01584 01585 // Shared state created by std::async(). 01586 // Holds a deferred function and storage for its result. 01587 template<typename _BoundFn, typename _Res> 01588 class __future_base::_Deferred_state final 01589 : public __future_base::_State_base 01590 { 01591 public: 01592 explicit 01593 _Deferred_state(_BoundFn&& __fn) 01594 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn)) 01595 { } 01596 01597 private: 01598 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type; 01599 _Ptr_type _M_result; 01600 _BoundFn _M_fn; 01601 01602 // Run the deferred function. 01603 virtual void 01604 _M_complete_async() 01605 { 01606 // Multiple threads can call a waiting function on the future and 01607 // reach this point at the same time. The call_once in _M_set_result 01608 // ensures only the first one run the deferred function, stores the 01609 // result in _M_result, swaps that with the base _M_result and makes 01610 // the state ready. Tell _M_set_result to ignore failure so all later 01611 // calls do nothing. 01612 _M_set_result(_S_task_setter(_M_result, _M_fn), true); 01613 } 01614 01615 // Caller should check whether the state is ready first, because this 01616 // function will return true even after the deferred function has run. 01617 virtual bool _M_is_deferred_future() const { return true; } 01618 }; 01619 01620 // Common functionality hoisted out of the _Async_state_impl template. 01621 class __future_base::_Async_state_commonV2 01622 : public __future_base::_State_base 01623 { 01624 protected: 01625 ~_Async_state_commonV2() = default; 01626 01627 // Make waiting functions block until the thread completes, as if joined. 01628 // 01629 // This function is used by wait() to satisfy the first requirement below 01630 // and by wait_for() / wait_until() to satisfy the second. 01631 // 01632 // [futures.async]: 01633 // 01634 // — a call to a waiting function on an asynchronous return object that 01635 // shares the shared state created by this async call shall block until 01636 // the associated thread has completed, as if joined, or else time out. 01637 // 01638 // — the associated thread completion synchronizes with the return from 01639 // the first function that successfully detects the ready status of the 01640 // shared state or with the return from the last function that releases 01641 // the shared state, whichever happens first. 01642 virtual void _M_complete_async() { _M_join(); } 01643 01644 void _M_join() { std::call_once(_M_once, &thread::join, &_M_thread); } 01645 01646 thread _M_thread; 01647 once_flag _M_once; 01648 }; 01649 01650 // Shared state created by std::async(). 01651 // Starts a new thread that runs a function and makes the shared state ready. 01652 template<typename _BoundFn, typename _Res> 01653 class __future_base::_Async_state_impl final 01654 : public __future_base::_Async_state_commonV2 01655 { 01656 public: 01657 explicit 01658 _Async_state_impl(_BoundFn&& __fn) 01659 : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn)) 01660 { 01661 _M_thread = std::thread{ [this] { 01662 __try 01663 { 01664 _M_set_result(_S_task_setter(_M_result, _M_fn)); 01665 } 01666 __catch (const __cxxabiv1::__forced_unwind&) 01667 { 01668 // make the shared state ready on thread cancellation 01669 if (static_cast<bool>(_M_result)) 01670 this->_M_break_promise(std::move(_M_result)); 01671 __throw_exception_again; 01672 } 01673 } }; 01674 } 01675 01676 // Must not destroy _M_result and _M_fn until the thread finishes. 01677 // Call join() directly rather than through _M_join() because no other 01678 // thread can be referring to this state if it is being destroyed. 01679 ~_Async_state_impl() { if (_M_thread.joinable()) _M_thread.join(); } 01680 01681 private: 01682 typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type; 01683 _Ptr_type _M_result; 01684 _BoundFn _M_fn; 01685 }; 01686 01687 template<typename _BoundFn> 01688 inline std::shared_ptr<__future_base::_State_base> 01689 __future_base::_S_make_deferred_state(_BoundFn&& __fn) 01690 { 01691 typedef typename remove_reference<_BoundFn>::type __fn_type; 01692 typedef _Deferred_state<__fn_type> __state_type; 01693 return std::make_shared<__state_type>(std::move(__fn)); 01694 } 01695 01696 template<typename _BoundFn> 01697 inline std::shared_ptr<__future_base::_State_base> 01698 __future_base::_S_make_async_state(_BoundFn&& __fn) 01699 { 01700 typedef typename remove_reference<_BoundFn>::type __fn_type; 01701 typedef _Async_state_impl<__fn_type> __state_type; 01702 return std::make_shared<__state_type>(std::move(__fn)); 01703 } 01704 01705 01706 /// async 01707 template<typename _Fn, typename... _Args> 01708 future<__async_result_of<_Fn, _Args...>> 01709 async(launch __policy, _Fn&& __fn, _Args&&... __args) 01710 { 01711 std::shared_ptr<__future_base::_State_base> __state; 01712 if ((__policy & launch::async) == launch::async) 01713 { 01714 __try 01715 { 01716 __state = __future_base::_S_make_async_state(std::__bind_simple( 01717 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...)); 01718 } 01719 #if __cpp_exceptions 01720 catch(const system_error& __e) 01721 { 01722 if (__e.code() != errc::resource_unavailable_try_again 01723 || (__policy & launch::deferred) != launch::deferred) 01724 throw; 01725 } 01726 #endif 01727 } 01728 if (!__state) 01729 { 01730 __state = __future_base::_S_make_deferred_state(std::__bind_simple( 01731 std::forward<_Fn>(__fn), std::forward<_Args>(__args)...)); 01732 } 01733 return future<__async_result_of<_Fn, _Args...>>(__state); 01734 } 01735 01736 /// async, potential overload 01737 template<typename _Fn, typename... _Args> 01738 inline future<__async_result_of<_Fn, _Args...>> 01739 async(_Fn&& __fn, _Args&&... __args) 01740 { 01741 return std::async(launch::async|launch::deferred, 01742 std::forward<_Fn>(__fn), 01743 std::forward<_Args>(__args)...); 01744 } 01745 01746 #endif // _GLIBCXX_ASYNC_ABI_COMPAT 01747 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1 01748 // && ATOMIC_INT_LOCK_FREE 01749 01750 // @} group futures 01751 _GLIBCXX_END_NAMESPACE_VERSION 01752 } // namespace 01753 01754 #endif // C++11 01755 01756 #endif // _GLIBCXX_FUTURE