future 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608
  1. // -*- C++ -*-
  2. //===--------------------------- future -----------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP_FUTURE
  10. #define _LIBCPP_FUTURE
  11. /*
  12. future synopsis
  13. namespace std
  14. {
  15. enum class future_errc
  16. {
  17. future_already_retrieved = 1,
  18. promise_already_satisfied,
  19. no_state,
  20. broken_promise
  21. };
  22. enum class launch
  23. {
  24. async = 1,
  25. deferred = 2,
  26. any = async | deferred
  27. };
  28. enum class future_status
  29. {
  30. ready,
  31. timeout,
  32. deferred
  33. };
  34. template <> struct is_error_code_enum<future_errc> : public true_type { };
  35. error_code make_error_code(future_errc e) noexcept;
  36. error_condition make_error_condition(future_errc e) noexcept;
  37. const error_category& future_category() noexcept;
  38. class future_error
  39. : public logic_error
  40. {
  41. public:
  42. future_error(error_code ec); // exposition only
  43. explicit future_error(future_errc); // C++17
  44. const error_code& code() const noexcept;
  45. const char* what() const noexcept;
  46. };
  47. template <class R>
  48. class promise
  49. {
  50. public:
  51. promise();
  52. template <class Allocator>
  53. promise(allocator_arg_t, const Allocator& a);
  54. promise(promise&& rhs) noexcept;
  55. promise(const promise& rhs) = delete;
  56. ~promise();
  57. // assignment
  58. promise& operator=(promise&& rhs) noexcept;
  59. promise& operator=(const promise& rhs) = delete;
  60. void swap(promise& other) noexcept;
  61. // retrieving the result
  62. future<R> get_future();
  63. // setting the result
  64. void set_value(const R& r);
  65. void set_value(R&& r);
  66. void set_exception(exception_ptr p);
  67. // setting the result with deferred notification
  68. void set_value_at_thread_exit(const R& r);
  69. void set_value_at_thread_exit(R&& r);
  70. void set_exception_at_thread_exit(exception_ptr p);
  71. };
  72. template <class R>
  73. class promise<R&>
  74. {
  75. public:
  76. promise();
  77. template <class Allocator>
  78. promise(allocator_arg_t, const Allocator& a);
  79. promise(promise&& rhs) noexcept;
  80. promise(const promise& rhs) = delete;
  81. ~promise();
  82. // assignment
  83. promise& operator=(promise&& rhs) noexcept;
  84. promise& operator=(const promise& rhs) = delete;
  85. void swap(promise& other) noexcept;
  86. // retrieving the result
  87. future<R&> get_future();
  88. // setting the result
  89. void set_value(R& r);
  90. void set_exception(exception_ptr p);
  91. // setting the result with deferred notification
  92. void set_value_at_thread_exit(R&);
  93. void set_exception_at_thread_exit(exception_ptr p);
  94. };
  95. template <>
  96. class promise<void>
  97. {
  98. public:
  99. promise();
  100. template <class Allocator>
  101. promise(allocator_arg_t, const Allocator& a);
  102. promise(promise&& rhs) noexcept;
  103. promise(const promise& rhs) = delete;
  104. ~promise();
  105. // assignment
  106. promise& operator=(promise&& rhs) noexcept;
  107. promise& operator=(const promise& rhs) = delete;
  108. void swap(promise& other) noexcept;
  109. // retrieving the result
  110. future<void> get_future();
  111. // setting the result
  112. void set_value();
  113. void set_exception(exception_ptr p);
  114. // setting the result with deferred notification
  115. void set_value_at_thread_exit();
  116. void set_exception_at_thread_exit(exception_ptr p);
  117. };
  118. template <class R> void swap(promise<R>& x, promise<R>& y) noexcept;
  119. template <class R, class Alloc>
  120. struct uses_allocator<promise<R>, Alloc> : public true_type {};
  121. template <class R>
  122. class future
  123. {
  124. public:
  125. future() noexcept;
  126. future(future&&) noexcept;
  127. future(const future& rhs) = delete;
  128. ~future();
  129. future& operator=(const future& rhs) = delete;
  130. future& operator=(future&&) noexcept;
  131. shared_future<R> share() noexcept;
  132. // retrieving the value
  133. R get();
  134. // functions to check state
  135. bool valid() const noexcept;
  136. void wait() const;
  137. template <class Rep, class Period>
  138. future_status
  139. wait_for(const chrono::duration<Rep, Period>& rel_time) const;
  140. template <class Clock, class Duration>
  141. future_status
  142. wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
  143. };
  144. template <class R>
  145. class future<R&>
  146. {
  147. public:
  148. future() noexcept;
  149. future(future&&) noexcept;
  150. future(const future& rhs) = delete;
  151. ~future();
  152. future& operator=(const future& rhs) = delete;
  153. future& operator=(future&&) noexcept;
  154. shared_future<R&> share() noexcept;
  155. // retrieving the value
  156. R& get();
  157. // functions to check state
  158. bool valid() const noexcept;
  159. void wait() const;
  160. template <class Rep, class Period>
  161. future_status
  162. wait_for(const chrono::duration<Rep, Period>& rel_time) const;
  163. template <class Clock, class Duration>
  164. future_status
  165. wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
  166. };
  167. template <>
  168. class future<void>
  169. {
  170. public:
  171. future() noexcept;
  172. future(future&&) noexcept;
  173. future(const future& rhs) = delete;
  174. ~future();
  175. future& operator=(const future& rhs) = delete;
  176. future& operator=(future&&) noexcept;
  177. shared_future<void> share() noexcept;
  178. // retrieving the value
  179. void get();
  180. // functions to check state
  181. bool valid() const noexcept;
  182. void wait() const;
  183. template <class Rep, class Period>
  184. future_status
  185. wait_for(const chrono::duration<Rep, Period>& rel_time) const;
  186. template <class Clock, class Duration>
  187. future_status
  188. wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
  189. };
  190. template <class R>
  191. class shared_future
  192. {
  193. public:
  194. shared_future() noexcept;
  195. shared_future(const shared_future& rhs);
  196. shared_future(future<R>&&) noexcept;
  197. shared_future(shared_future&& rhs) noexcept;
  198. ~shared_future();
  199. shared_future& operator=(const shared_future& rhs);
  200. shared_future& operator=(shared_future&& rhs) noexcept;
  201. // retrieving the value
  202. const R& get() const;
  203. // functions to check state
  204. bool valid() const noexcept;
  205. void wait() const;
  206. template <class Rep, class Period>
  207. future_status
  208. wait_for(const chrono::duration<Rep, Period>& rel_time) const;
  209. template <class Clock, class Duration>
  210. future_status
  211. wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
  212. };
  213. template <class R>
  214. class shared_future<R&>
  215. {
  216. public:
  217. shared_future() noexcept;
  218. shared_future(const shared_future& rhs);
  219. shared_future(future<R&>&&) noexcept;
  220. shared_future(shared_future&& rhs) noexcept;
  221. ~shared_future();
  222. shared_future& operator=(const shared_future& rhs);
  223. shared_future& operator=(shared_future&& rhs) noexcept;
  224. // retrieving the value
  225. R& get() const;
  226. // functions to check state
  227. bool valid() const noexcept;
  228. void wait() const;
  229. template <class Rep, class Period>
  230. future_status
  231. wait_for(const chrono::duration<Rep, Period>& rel_time) const;
  232. template <class Clock, class Duration>
  233. future_status
  234. wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
  235. };
  236. template <>
  237. class shared_future<void>
  238. {
  239. public:
  240. shared_future() noexcept;
  241. shared_future(const shared_future& rhs);
  242. shared_future(future<void>&&) noexcept;
  243. shared_future(shared_future&& rhs) noexcept;
  244. ~shared_future();
  245. shared_future& operator=(const shared_future& rhs);
  246. shared_future& operator=(shared_future&& rhs) noexcept;
  247. // retrieving the value
  248. void get() const;
  249. // functions to check state
  250. bool valid() const noexcept;
  251. void wait() const;
  252. template <class Rep, class Period>
  253. future_status
  254. wait_for(const chrono::duration<Rep, Period>& rel_time) const;
  255. template <class Clock, class Duration>
  256. future_status
  257. wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
  258. };
  259. template <class F, class... Args>
  260. future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
  261. async(F&& f, Args&&... args);
  262. template <class F, class... Args>
  263. future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
  264. async(launch policy, F&& f, Args&&... args);
  265. template <class> class packaged_task; // undefined
  266. template <class R, class... ArgTypes>
  267. class packaged_task<R(ArgTypes...)>
  268. {
  269. public:
  270. typedef R result_type; // extension
  271. // construction and destruction
  272. packaged_task() noexcept;
  273. template <class F>
  274. explicit packaged_task(F&& f);
  275. template <class F, class Allocator>
  276. packaged_task(allocator_arg_t, const Allocator& a, F&& f);
  277. ~packaged_task();
  278. // no copy
  279. packaged_task(const packaged_task&) = delete;
  280. packaged_task& operator=(const packaged_task&) = delete;
  281. // move support
  282. packaged_task(packaged_task&& other) noexcept;
  283. packaged_task& operator=(packaged_task&& other) noexcept;
  284. void swap(packaged_task& other) noexcept;
  285. bool valid() const noexcept;
  286. // result retrieval
  287. future<R> get_future();
  288. // execution
  289. void operator()(ArgTypes... );
  290. void make_ready_at_thread_exit(ArgTypes...);
  291. void reset();
  292. };
  293. template <class R>
  294. void swap(packaged_task<R(ArgTypes...)&, packaged_task<R(ArgTypes...)>&) noexcept;
  295. template <class R, class Alloc> struct uses_allocator<packaged_task<R>, Alloc>;
  296. } // std
  297. */
  298. #include <__config>
  299. #include <system_error>
  300. #include <memory>
  301. #include <chrono>
  302. #include <exception>
  303. #include <mutex>
  304. #include <thread>
  305. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  306. #pragma GCC system_header
  307. #endif
  308. #ifdef _LIBCPP_HAS_NO_THREADS
  309. #error <future> is not supported on this single threaded system
  310. #else // !_LIBCPP_HAS_NO_THREADS
  311. _LIBCPP_BEGIN_NAMESPACE_STD
  312. //enum class future_errc
  313. _LIBCPP_DECLARE_STRONG_ENUM(future_errc)
  314. {
  315. future_already_retrieved = 1,
  316. promise_already_satisfied,
  317. no_state,
  318. broken_promise
  319. };
  320. _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_errc)
  321. template <>
  322. struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<future_errc> : public true_type {};
  323. #ifdef _LIBCPP_HAS_NO_STRONG_ENUMS
  324. template <>
  325. struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<future_errc::__lx> : public true_type { };
  326. #endif
  327. //enum class launch
  328. _LIBCPP_DECLARE_STRONG_ENUM(launch)
  329. {
  330. async = 1,
  331. deferred = 2,
  332. any = async | deferred
  333. };
  334. _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(launch)
  335. #ifndef _LIBCPP_HAS_NO_STRONG_ENUMS
  336. typedef underlying_type<launch>::type __launch_underlying_type;
  337. inline _LIBCPP_INLINE_VISIBILITY
  338. _LIBCPP_CONSTEXPR
  339. launch
  340. operator&(launch __x, launch __y)
  341. {
  342. return static_cast<launch>(static_cast<__launch_underlying_type>(__x) &
  343. static_cast<__launch_underlying_type>(__y));
  344. }
  345. inline _LIBCPP_INLINE_VISIBILITY
  346. _LIBCPP_CONSTEXPR
  347. launch
  348. operator|(launch __x, launch __y)
  349. {
  350. return static_cast<launch>(static_cast<__launch_underlying_type>(__x) |
  351. static_cast<__launch_underlying_type>(__y));
  352. }
  353. inline _LIBCPP_INLINE_VISIBILITY
  354. _LIBCPP_CONSTEXPR
  355. launch
  356. operator^(launch __x, launch __y)
  357. {
  358. return static_cast<launch>(static_cast<__launch_underlying_type>(__x) ^
  359. static_cast<__launch_underlying_type>(__y));
  360. }
  361. inline _LIBCPP_INLINE_VISIBILITY
  362. _LIBCPP_CONSTEXPR
  363. launch
  364. operator~(launch __x)
  365. {
  366. return static_cast<launch>(~static_cast<__launch_underlying_type>(__x) & 3);
  367. }
  368. inline _LIBCPP_INLINE_VISIBILITY
  369. launch&
  370. operator&=(launch& __x, launch __y)
  371. {
  372. __x = __x & __y; return __x;
  373. }
  374. inline _LIBCPP_INLINE_VISIBILITY
  375. launch&
  376. operator|=(launch& __x, launch __y)
  377. {
  378. __x = __x | __y; return __x;
  379. }
  380. inline _LIBCPP_INLINE_VISIBILITY
  381. launch&
  382. operator^=(launch& __x, launch __y)
  383. {
  384. __x = __x ^ __y; return __x;
  385. }
  386. #endif // !_LIBCPP_HAS_NO_STRONG_ENUMS
  387. //enum class future_status
  388. _LIBCPP_DECLARE_STRONG_ENUM(future_status)
  389. {
  390. ready,
  391. timeout,
  392. deferred
  393. };
  394. _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_status)
  395. _LIBCPP_FUNC_VIS
  396. const error_category& future_category() _NOEXCEPT;
  397. inline _LIBCPP_INLINE_VISIBILITY
  398. error_code
  399. make_error_code(future_errc __e) _NOEXCEPT
  400. {
  401. return error_code(static_cast<int>(__e), future_category());
  402. }
  403. inline _LIBCPP_INLINE_VISIBILITY
  404. error_condition
  405. make_error_condition(future_errc __e) _NOEXCEPT
  406. {
  407. return error_condition(static_cast<int>(__e), future_category());
  408. }
  409. class _LIBCPP_EXCEPTION_ABI _LIBCPP_AVAILABILITY_FUTURE_ERROR future_error
  410. : public logic_error
  411. {
  412. error_code __ec_;
  413. public:
  414. future_error(error_code __ec);
  415. #if _LIBCPP_STD_VERS > 14
  416. explicit future_error(future_errc _Ev) : logic_error(), __ec_(make_error_code(_Ev)) {}
  417. #endif
  418. _LIBCPP_INLINE_VISIBILITY
  419. const error_code& code() const _NOEXCEPT {return __ec_;}
  420. virtual ~future_error() _NOEXCEPT;
  421. };
  422. _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
  423. #ifndef _LIBCPP_NO_EXCEPTIONS
  424. _LIBCPP_AVAILABILITY_FUTURE_ERROR
  425. #endif
  426. void __throw_future_error(future_errc _Ev)
  427. {
  428. #ifndef _LIBCPP_NO_EXCEPTIONS
  429. throw future_error(make_error_code(_Ev));
  430. #else
  431. ((void)_Ev);
  432. _VSTD::abort();
  433. #endif
  434. }
  435. class _LIBCPP_TYPE_VIS _LIBCPP_AVAILABILITY_FUTURE __assoc_sub_state
  436. : public __shared_count
  437. {
  438. protected:
  439. exception_ptr __exception_;
  440. mutable mutex __mut_;
  441. mutable condition_variable __cv_;
  442. unsigned __state_;
  443. virtual void __on_zero_shared() _NOEXCEPT;
  444. void __sub_wait(unique_lock<mutex>& __lk);
  445. public:
  446. enum
  447. {
  448. __constructed = 1,
  449. __future_attached = 2,
  450. ready = 4,
  451. deferred = 8
  452. };
  453. _LIBCPP_INLINE_VISIBILITY
  454. __assoc_sub_state() : __state_(0) {}
  455. _LIBCPP_INLINE_VISIBILITY
  456. bool __has_value() const
  457. {return (__state_ & __constructed) || (__exception_ != nullptr);}
  458. _LIBCPP_INLINE_VISIBILITY
  459. void __attach_future() {
  460. lock_guard<mutex> __lk(__mut_);
  461. bool __has_future_attached = (__state_ & __future_attached) != 0;
  462. if (__has_future_attached)
  463. __throw_future_error(future_errc::future_already_retrieved);
  464. this->__add_shared();
  465. __state_ |= __future_attached;
  466. }
  467. _LIBCPP_INLINE_VISIBILITY
  468. void __set_deferred() {__state_ |= deferred;}
  469. void __make_ready();
  470. _LIBCPP_INLINE_VISIBILITY
  471. bool __is_ready() const {return (__state_ & ready) != 0;}
  472. void set_value();
  473. void set_value_at_thread_exit();
  474. void set_exception(exception_ptr __p);
  475. void set_exception_at_thread_exit(exception_ptr __p);
  476. void copy();
  477. void wait();
  478. template <class _Rep, class _Period>
  479. future_status
  480. _LIBCPP_INLINE_VISIBILITY
  481. wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const;
  482. template <class _Clock, class _Duration>
  483. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
  484. future_status
  485. wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const;
  486. virtual void __execute();
  487. };
  488. template <class _Clock, class _Duration>
  489. future_status
  490. __assoc_sub_state::wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
  491. {
  492. unique_lock<mutex> __lk(__mut_);
  493. if (__state_ & deferred)
  494. return future_status::deferred;
  495. while (!(__state_ & ready) && _Clock::now() < __abs_time)
  496. __cv_.wait_until(__lk, __abs_time);
  497. if (__state_ & ready)
  498. return future_status::ready;
  499. return future_status::timeout;
  500. }
  501. template <class _Rep, class _Period>
  502. inline
  503. future_status
  504. __assoc_sub_state::wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
  505. {
  506. return wait_until(chrono::steady_clock::now() + __rel_time);
  507. }
  508. template <class _Rp>
  509. class _LIBCPP_AVAILABILITY_FUTURE __assoc_state
  510. : public __assoc_sub_state
  511. {
  512. typedef __assoc_sub_state base;
  513. typedef typename aligned_storage<sizeof(_Rp), alignment_of<_Rp>::value>::type _Up;
  514. protected:
  515. _Up __value_;
  516. virtual void __on_zero_shared() _NOEXCEPT;
  517. public:
  518. template <class _Arg>
  519. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  520. void set_value(_Arg&& __arg);
  521. #else
  522. void set_value(_Arg& __arg);
  523. #endif
  524. template <class _Arg>
  525. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  526. void set_value_at_thread_exit(_Arg&& __arg);
  527. #else
  528. void set_value_at_thread_exit(_Arg& __arg);
  529. #endif
  530. _Rp move();
  531. typename add_lvalue_reference<_Rp>::type copy();
  532. };
  533. template <class _Rp>
  534. void
  535. __assoc_state<_Rp>::__on_zero_shared() _NOEXCEPT
  536. {
  537. if (this->__state_ & base::__constructed)
  538. reinterpret_cast<_Rp*>(&__value_)->~_Rp();
  539. delete this;
  540. }
  541. template <class _Rp>
  542. template <class _Arg>
  543. _LIBCPP_AVAILABILITY_FUTURE
  544. void
  545. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  546. __assoc_state<_Rp>::set_value(_Arg&& __arg)
  547. #else
  548. __assoc_state<_Rp>::set_value(_Arg& __arg)
  549. #endif
  550. {
  551. unique_lock<mutex> __lk(this->__mut_);
  552. if (this->__has_value())
  553. __throw_future_error(future_errc::promise_already_satisfied);
  554. ::new(&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
  555. this->__state_ |= base::__constructed | base::ready;
  556. __cv_.notify_all();
  557. }
  558. template <class _Rp>
  559. template <class _Arg>
  560. void
  561. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  562. __assoc_state<_Rp>::set_value_at_thread_exit(_Arg&& __arg)
  563. #else
  564. __assoc_state<_Rp>::set_value_at_thread_exit(_Arg& __arg)
  565. #endif
  566. {
  567. unique_lock<mutex> __lk(this->__mut_);
  568. if (this->__has_value())
  569. __throw_future_error(future_errc::promise_already_satisfied);
  570. ::new(&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
  571. this->__state_ |= base::__constructed;
  572. __thread_local_data()->__make_ready_at_thread_exit(this);
  573. }
  574. template <class _Rp>
  575. _Rp
  576. __assoc_state<_Rp>::move()
  577. {
  578. unique_lock<mutex> __lk(this->__mut_);
  579. this->__sub_wait(__lk);
  580. if (this->__exception_ != nullptr)
  581. rethrow_exception(this->__exception_);
  582. return _VSTD::move(*reinterpret_cast<_Rp*>(&__value_));
  583. }
  584. template <class _Rp>
  585. typename add_lvalue_reference<_Rp>::type
  586. __assoc_state<_Rp>::copy()
  587. {
  588. unique_lock<mutex> __lk(this->__mut_);
  589. this->__sub_wait(__lk);
  590. if (this->__exception_ != nullptr)
  591. rethrow_exception(this->__exception_);
  592. return *reinterpret_cast<_Rp*>(&__value_);
  593. }
  594. template <class _Rp>
  595. class _LIBCPP_AVAILABILITY_FUTURE __assoc_state<_Rp&>
  596. : public __assoc_sub_state
  597. {
  598. typedef __assoc_sub_state base;
  599. typedef _Rp* _Up;
  600. protected:
  601. _Up __value_;
  602. virtual void __on_zero_shared() _NOEXCEPT;
  603. public:
  604. void set_value(_Rp& __arg);
  605. void set_value_at_thread_exit(_Rp& __arg);
  606. _Rp& copy();
  607. };
  608. template <class _Rp>
  609. void
  610. __assoc_state<_Rp&>::__on_zero_shared() _NOEXCEPT
  611. {
  612. delete this;
  613. }
  614. template <class _Rp>
  615. void
  616. __assoc_state<_Rp&>::set_value(_Rp& __arg)
  617. {
  618. unique_lock<mutex> __lk(this->__mut_);
  619. if (this->__has_value())
  620. __throw_future_error(future_errc::promise_already_satisfied);
  621. __value_ = _VSTD::addressof(__arg);
  622. this->__state_ |= base::__constructed | base::ready;
  623. __cv_.notify_all();
  624. }
  625. template <class _Rp>
  626. void
  627. __assoc_state<_Rp&>::set_value_at_thread_exit(_Rp& __arg)
  628. {
  629. unique_lock<mutex> __lk(this->__mut_);
  630. if (this->__has_value())
  631. __throw_future_error(future_errc::promise_already_satisfied);
  632. __value_ = _VSTD::addressof(__arg);
  633. this->__state_ |= base::__constructed;
  634. __thread_local_data()->__make_ready_at_thread_exit(this);
  635. }
  636. template <class _Rp>
  637. _Rp&
  638. __assoc_state<_Rp&>::copy()
  639. {
  640. unique_lock<mutex> __lk(this->__mut_);
  641. this->__sub_wait(__lk);
  642. if (this->__exception_ != nullptr)
  643. rethrow_exception(this->__exception_);
  644. return *__value_;
  645. }
  646. template <class _Rp, class _Alloc>
  647. class _LIBCPP_AVAILABILITY_FUTURE __assoc_state_alloc
  648. : public __assoc_state<_Rp>
  649. {
  650. typedef __assoc_state<_Rp> base;
  651. _Alloc __alloc_;
  652. virtual void __on_zero_shared() _NOEXCEPT;
  653. public:
  654. _LIBCPP_INLINE_VISIBILITY
  655. explicit __assoc_state_alloc(const _Alloc& __a)
  656. : __alloc_(__a) {}
  657. };
  658. template <class _Rp, class _Alloc>
  659. void
  660. __assoc_state_alloc<_Rp, _Alloc>::__on_zero_shared() _NOEXCEPT
  661. {
  662. if (this->__state_ & base::__constructed)
  663. reinterpret_cast<_Rp*>(_VSTD::addressof(this->__value_))->~_Rp();
  664. typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
  665. typedef allocator_traits<_Al> _ATraits;
  666. typedef pointer_traits<typename _ATraits::pointer> _PTraits;
  667. _Al __a(__alloc_);
  668. this->~__assoc_state_alloc();
  669. __a.deallocate(_PTraits::pointer_to(*this), 1);
  670. }
  671. template <class _Rp, class _Alloc>
  672. class _LIBCPP_AVAILABILITY_FUTURE __assoc_state_alloc<_Rp&, _Alloc>
  673. : public __assoc_state<_Rp&>
  674. {
  675. typedef __assoc_state<_Rp&> base;
  676. _Alloc __alloc_;
  677. virtual void __on_zero_shared() _NOEXCEPT;
  678. public:
  679. _LIBCPP_INLINE_VISIBILITY
  680. explicit __assoc_state_alloc(const _Alloc& __a)
  681. : __alloc_(__a) {}
  682. };
  683. template <class _Rp, class _Alloc>
  684. void
  685. __assoc_state_alloc<_Rp&, _Alloc>::__on_zero_shared() _NOEXCEPT
  686. {
  687. typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
  688. typedef allocator_traits<_Al> _ATraits;
  689. typedef pointer_traits<typename _ATraits::pointer> _PTraits;
  690. _Al __a(__alloc_);
  691. this->~__assoc_state_alloc();
  692. __a.deallocate(_PTraits::pointer_to(*this), 1);
  693. }
  694. template <class _Alloc>
  695. class _LIBCPP_AVAILABILITY_FUTURE __assoc_sub_state_alloc
  696. : public __assoc_sub_state
  697. {
  698. typedef __assoc_sub_state base;
  699. _Alloc __alloc_;
  700. virtual void __on_zero_shared() _NOEXCEPT;
  701. public:
  702. _LIBCPP_INLINE_VISIBILITY
  703. explicit __assoc_sub_state_alloc(const _Alloc& __a)
  704. : __alloc_(__a) {}
  705. };
  706. template <class _Alloc>
  707. void
  708. __assoc_sub_state_alloc<_Alloc>::__on_zero_shared() _NOEXCEPT
  709. {
  710. typedef typename __allocator_traits_rebind<_Alloc, __assoc_sub_state_alloc>::type _Al;
  711. typedef allocator_traits<_Al> _ATraits;
  712. typedef pointer_traits<typename _ATraits::pointer> _PTraits;
  713. _Al __a(__alloc_);
  714. this->~__assoc_sub_state_alloc();
  715. __a.deallocate(_PTraits::pointer_to(*this), 1);
  716. }
  717. template <class _Rp, class _Fp>
  718. class _LIBCPP_AVAILABILITY_FUTURE __deferred_assoc_state
  719. : public __assoc_state<_Rp>
  720. {
  721. typedef __assoc_state<_Rp> base;
  722. _Fp __func_;
  723. public:
  724. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  725. _LIBCPP_INLINE_VISIBILITY
  726. explicit __deferred_assoc_state(_Fp&& __f);
  727. #endif
  728. virtual void __execute();
  729. };
  730. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  731. template <class _Rp, class _Fp>
  732. inline
  733. __deferred_assoc_state<_Rp, _Fp>::__deferred_assoc_state(_Fp&& __f)
  734. : __func_(_VSTD::forward<_Fp>(__f))
  735. {
  736. this->__set_deferred();
  737. }
  738. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  739. template <class _Rp, class _Fp>
  740. void
  741. __deferred_assoc_state<_Rp, _Fp>::__execute()
  742. {
  743. #ifndef _LIBCPP_NO_EXCEPTIONS
  744. try
  745. {
  746. #endif // _LIBCPP_NO_EXCEPTIONS
  747. this->set_value(__func_());
  748. #ifndef _LIBCPP_NO_EXCEPTIONS
  749. }
  750. catch (...)
  751. {
  752. this->set_exception(current_exception());
  753. }
  754. #endif // _LIBCPP_NO_EXCEPTIONS
  755. }
  756. template <class _Fp>
  757. class _LIBCPP_AVAILABILITY_FUTURE __deferred_assoc_state<void, _Fp>
  758. : public __assoc_sub_state
  759. {
  760. typedef __assoc_sub_state base;
  761. _Fp __func_;
  762. public:
  763. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  764. _LIBCPP_INLINE_VISIBILITY
  765. explicit __deferred_assoc_state(_Fp&& __f);
  766. #endif
  767. virtual void __execute();
  768. };
  769. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  770. template <class _Fp>
  771. inline
  772. __deferred_assoc_state<void, _Fp>::__deferred_assoc_state(_Fp&& __f)
  773. : __func_(_VSTD::forward<_Fp>(__f))
  774. {
  775. this->__set_deferred();
  776. }
  777. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  778. template <class _Fp>
  779. void
  780. __deferred_assoc_state<void, _Fp>::__execute()
  781. {
  782. #ifndef _LIBCPP_NO_EXCEPTIONS
  783. try
  784. {
  785. #endif // _LIBCPP_NO_EXCEPTIONS
  786. __func_();
  787. this->set_value();
  788. #ifndef _LIBCPP_NO_EXCEPTIONS
  789. }
  790. catch (...)
  791. {
  792. this->set_exception(current_exception());
  793. }
  794. #endif // _LIBCPP_NO_EXCEPTIONS
  795. }
  796. template <class _Rp, class _Fp>
  797. class _LIBCPP_AVAILABILITY_FUTURE __async_assoc_state
  798. : public __assoc_state<_Rp>
  799. {
  800. typedef __assoc_state<_Rp> base;
  801. _Fp __func_;
  802. virtual void __on_zero_shared() _NOEXCEPT;
  803. public:
  804. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  805. _LIBCPP_INLINE_VISIBILITY
  806. explicit __async_assoc_state(_Fp&& __f);
  807. #endif
  808. virtual void __execute();
  809. };
  810. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  811. template <class _Rp, class _Fp>
  812. inline
  813. __async_assoc_state<_Rp, _Fp>::__async_assoc_state(_Fp&& __f)
  814. : __func_(_VSTD::forward<_Fp>(__f))
  815. {
  816. }
  817. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  818. template <class _Rp, class _Fp>
  819. void
  820. __async_assoc_state<_Rp, _Fp>::__execute()
  821. {
  822. #ifndef _LIBCPP_NO_EXCEPTIONS
  823. try
  824. {
  825. #endif // _LIBCPP_NO_EXCEPTIONS
  826. this->set_value(__func_());
  827. #ifndef _LIBCPP_NO_EXCEPTIONS
  828. }
  829. catch (...)
  830. {
  831. this->set_exception(current_exception());
  832. }
  833. #endif // _LIBCPP_NO_EXCEPTIONS
  834. }
  835. template <class _Rp, class _Fp>
  836. void
  837. __async_assoc_state<_Rp, _Fp>::__on_zero_shared() _NOEXCEPT
  838. {
  839. this->wait();
  840. base::__on_zero_shared();
  841. }
  842. template <class _Fp>
  843. class _LIBCPP_AVAILABILITY_FUTURE __async_assoc_state<void, _Fp>
  844. : public __assoc_sub_state
  845. {
  846. typedef __assoc_sub_state base;
  847. _Fp __func_;
  848. virtual void __on_zero_shared() _NOEXCEPT;
  849. public:
  850. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  851. _LIBCPP_INLINE_VISIBILITY
  852. explicit __async_assoc_state(_Fp&& __f);
  853. #endif
  854. virtual void __execute();
  855. };
  856. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  857. template <class _Fp>
  858. inline
  859. __async_assoc_state<void, _Fp>::__async_assoc_state(_Fp&& __f)
  860. : __func_(_VSTD::forward<_Fp>(__f))
  861. {
  862. }
  863. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  864. template <class _Fp>
  865. void
  866. __async_assoc_state<void, _Fp>::__execute()
  867. {
  868. #ifndef _LIBCPP_NO_EXCEPTIONS
  869. try
  870. {
  871. #endif // _LIBCPP_NO_EXCEPTIONS
  872. __func_();
  873. this->set_value();
  874. #ifndef _LIBCPP_NO_EXCEPTIONS
  875. }
  876. catch (...)
  877. {
  878. this->set_exception(current_exception());
  879. }
  880. #endif // _LIBCPP_NO_EXCEPTIONS
  881. }
  882. template <class _Fp>
  883. void
  884. __async_assoc_state<void, _Fp>::__on_zero_shared() _NOEXCEPT
  885. {
  886. this->wait();
  887. base::__on_zero_shared();
  888. }
  889. template <class _Rp> class _LIBCPP_TEMPLATE_VIS promise;
  890. template <class _Rp> class _LIBCPP_TEMPLATE_VIS shared_future;
  891. // future
  892. template <class _Rp> class _LIBCPP_TEMPLATE_VIS future;
  893. template <class _Rp, class _Fp>
  894. future<_Rp>
  895. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  896. __make_deferred_assoc_state(_Fp&& __f);
  897. #else
  898. __make_deferred_assoc_state(_Fp __f);
  899. #endif
  900. template <class _Rp, class _Fp>
  901. future<_Rp>
  902. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  903. __make_async_assoc_state(_Fp&& __f);
  904. #else
  905. __make_async_assoc_state(_Fp __f);
  906. #endif
  907. template <class _Rp>
  908. class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE future
  909. {
  910. __assoc_state<_Rp>* __state_;
  911. explicit future(__assoc_state<_Rp>* __state);
  912. template <class> friend class promise;
  913. template <class> friend class shared_future;
  914. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  915. template <class _R1, class _Fp>
  916. friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
  917. template <class _R1, class _Fp>
  918. friend future<_R1> __make_async_assoc_state(_Fp&& __f);
  919. #else
  920. template <class _R1, class _Fp>
  921. friend future<_R1> __make_deferred_assoc_state(_Fp __f);
  922. template <class _R1, class _Fp>
  923. friend future<_R1> __make_async_assoc_state(_Fp __f);
  924. #endif
  925. public:
  926. _LIBCPP_INLINE_VISIBILITY
  927. future() _NOEXCEPT : __state_(nullptr) {}
  928. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  929. _LIBCPP_INLINE_VISIBILITY
  930. future(future&& __rhs) _NOEXCEPT
  931. : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
  932. future(const future&) = delete;
  933. future& operator=(const future&) = delete;
  934. _LIBCPP_INLINE_VISIBILITY
  935. future& operator=(future&& __rhs) _NOEXCEPT
  936. {
  937. future(std::move(__rhs)).swap(*this);
  938. return *this;
  939. }
  940. #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  941. private:
  942. future(const future&);
  943. future& operator=(const future&);
  944. public:
  945. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  946. ~future();
  947. _LIBCPP_INLINE_VISIBILITY
  948. shared_future<_Rp> share() _NOEXCEPT;
  949. // retrieving the value
  950. _Rp get();
  951. _LIBCPP_INLINE_VISIBILITY
  952. void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
  953. // functions to check state
  954. _LIBCPP_INLINE_VISIBILITY
  955. bool valid() const _NOEXCEPT {return __state_ != nullptr;}
  956. _LIBCPP_INLINE_VISIBILITY
  957. void wait() const {__state_->wait();}
  958. template <class _Rep, class _Period>
  959. _LIBCPP_INLINE_VISIBILITY
  960. future_status
  961. wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
  962. {return __state_->wait_for(__rel_time);}
  963. template <class _Clock, class _Duration>
  964. _LIBCPP_INLINE_VISIBILITY
  965. future_status
  966. wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
  967. {return __state_->wait_until(__abs_time);}
  968. };
  969. template <class _Rp>
  970. future<_Rp>::future(__assoc_state<_Rp>* __state)
  971. : __state_(__state)
  972. {
  973. __state_->__attach_future();
  974. }
  975. struct __release_shared_count
  976. {
  977. void operator()(__shared_count* p) {p->__release_shared();}
  978. };
  979. template <class _Rp>
  980. future<_Rp>::~future()
  981. {
  982. if (__state_)
  983. __state_->__release_shared();
  984. }
  985. template <class _Rp>
  986. _Rp
  987. future<_Rp>::get()
  988. {
  989. unique_ptr<__shared_count, __release_shared_count> __(__state_);
  990. __assoc_state<_Rp>* __s = __state_;
  991. __state_ = nullptr;
  992. return __s->move();
  993. }
  994. template <class _Rp>
  995. class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE future<_Rp&>
  996. {
  997. __assoc_state<_Rp&>* __state_;
  998. explicit future(__assoc_state<_Rp&>* __state);
  999. template <class> friend class promise;
  1000. template <class> friend class shared_future;
  1001. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1002. template <class _R1, class _Fp>
  1003. friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
  1004. template <class _R1, class _Fp>
  1005. friend future<_R1> __make_async_assoc_state(_Fp&& __f);
  1006. #else
  1007. template <class _R1, class _Fp>
  1008. friend future<_R1> __make_deferred_assoc_state(_Fp __f);
  1009. template <class _R1, class _Fp>
  1010. friend future<_R1> __make_async_assoc_state(_Fp __f);
  1011. #endif
  1012. public:
  1013. _LIBCPP_INLINE_VISIBILITY
  1014. future() _NOEXCEPT : __state_(nullptr) {}
  1015. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1016. _LIBCPP_INLINE_VISIBILITY
  1017. future(future&& __rhs) _NOEXCEPT
  1018. : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
  1019. future(const future&) = delete;
  1020. future& operator=(const future&) = delete;
  1021. _LIBCPP_INLINE_VISIBILITY
  1022. future& operator=(future&& __rhs) _NOEXCEPT
  1023. {
  1024. future(std::move(__rhs)).swap(*this);
  1025. return *this;
  1026. }
  1027. #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1028. private:
  1029. future(const future&);
  1030. future& operator=(const future&);
  1031. public:
  1032. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1033. ~future();
  1034. _LIBCPP_INLINE_VISIBILITY
  1035. shared_future<_Rp&> share() _NOEXCEPT;
  1036. // retrieving the value
  1037. _Rp& get();
  1038. _LIBCPP_INLINE_VISIBILITY
  1039. void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
  1040. // functions to check state
  1041. _LIBCPP_INLINE_VISIBILITY
  1042. bool valid() const _NOEXCEPT {return __state_ != nullptr;}
  1043. _LIBCPP_INLINE_VISIBILITY
  1044. void wait() const {__state_->wait();}
  1045. template <class _Rep, class _Period>
  1046. _LIBCPP_INLINE_VISIBILITY
  1047. future_status
  1048. wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
  1049. {return __state_->wait_for(__rel_time);}
  1050. template <class _Clock, class _Duration>
  1051. _LIBCPP_INLINE_VISIBILITY
  1052. future_status
  1053. wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
  1054. {return __state_->wait_until(__abs_time);}
  1055. };
  1056. template <class _Rp>
  1057. future<_Rp&>::future(__assoc_state<_Rp&>* __state)
  1058. : __state_(__state)
  1059. {
  1060. __state_->__attach_future();
  1061. }
  1062. template <class _Rp>
  1063. future<_Rp&>::~future()
  1064. {
  1065. if (__state_)
  1066. __state_->__release_shared();
  1067. }
  1068. template <class _Rp>
  1069. _Rp&
  1070. future<_Rp&>::get()
  1071. {
  1072. unique_ptr<__shared_count, __release_shared_count> __(__state_);
  1073. __assoc_state<_Rp&>* __s = __state_;
  1074. __state_ = nullptr;
  1075. return __s->copy();
  1076. }
  1077. template <>
  1078. class _LIBCPP_TYPE_VIS _LIBCPP_AVAILABILITY_FUTURE future<void>
  1079. {
  1080. __assoc_sub_state* __state_;
  1081. explicit future(__assoc_sub_state* __state);
  1082. template <class> friend class promise;
  1083. template <class> friend class shared_future;
  1084. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1085. template <class _R1, class _Fp>
  1086. friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
  1087. template <class _R1, class _Fp>
  1088. friend future<_R1> __make_async_assoc_state(_Fp&& __f);
  1089. #else
  1090. template <class _R1, class _Fp>
  1091. friend future<_R1> __make_deferred_assoc_state(_Fp __f);
  1092. template <class _R1, class _Fp>
  1093. friend future<_R1> __make_async_assoc_state(_Fp __f);
  1094. #endif
  1095. public:
  1096. _LIBCPP_INLINE_VISIBILITY
  1097. future() _NOEXCEPT : __state_(nullptr) {}
  1098. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1099. _LIBCPP_INLINE_VISIBILITY
  1100. future(future&& __rhs) _NOEXCEPT
  1101. : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
  1102. future(const future&) = delete;
  1103. future& operator=(const future&) = delete;
  1104. _LIBCPP_INLINE_VISIBILITY
  1105. future& operator=(future&& __rhs) _NOEXCEPT
  1106. {
  1107. future(std::move(__rhs)).swap(*this);
  1108. return *this;
  1109. }
  1110. #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1111. private:
  1112. future(const future&);
  1113. future& operator=(const future&);
  1114. public:
  1115. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1116. ~future();
  1117. _LIBCPP_INLINE_VISIBILITY
  1118. shared_future<void> share() _NOEXCEPT;
  1119. // retrieving the value
  1120. void get();
  1121. _LIBCPP_INLINE_VISIBILITY
  1122. void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
  1123. // functions to check state
  1124. _LIBCPP_INLINE_VISIBILITY
  1125. bool valid() const _NOEXCEPT {return __state_ != nullptr;}
  1126. _LIBCPP_INLINE_VISIBILITY
  1127. void wait() const {__state_->wait();}
  1128. template <class _Rep, class _Period>
  1129. _LIBCPP_INLINE_VISIBILITY
  1130. future_status
  1131. wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
  1132. {return __state_->wait_for(__rel_time);}
  1133. template <class _Clock, class _Duration>
  1134. _LIBCPP_INLINE_VISIBILITY
  1135. future_status
  1136. wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
  1137. {return __state_->wait_until(__abs_time);}
  1138. };
  1139. template <class _Rp>
  1140. inline _LIBCPP_INLINE_VISIBILITY
  1141. void
  1142. swap(future<_Rp>& __x, future<_Rp>& __y) _NOEXCEPT
  1143. {
  1144. __x.swap(__y);
  1145. }
  1146. // promise<R>
  1147. template <class _Callable> class packaged_task;
  1148. template <class _Rp>
  1149. class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE promise
  1150. {
  1151. __assoc_state<_Rp>* __state_;
  1152. _LIBCPP_INLINE_VISIBILITY
  1153. explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
  1154. template <class> friend class packaged_task;
  1155. public:
  1156. promise();
  1157. template <class _Alloc>
  1158. promise(allocator_arg_t, const _Alloc& __a);
  1159. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1160. _LIBCPP_INLINE_VISIBILITY
  1161. promise(promise&& __rhs) _NOEXCEPT
  1162. : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
  1163. promise(const promise& __rhs) = delete;
  1164. #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1165. private:
  1166. promise(const promise& __rhs);
  1167. public:
  1168. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1169. ~promise();
  1170. // assignment
  1171. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1172. _LIBCPP_INLINE_VISIBILITY
  1173. promise& operator=(promise&& __rhs) _NOEXCEPT
  1174. {
  1175. promise(std::move(__rhs)).swap(*this);
  1176. return *this;
  1177. }
  1178. promise& operator=(const promise& __rhs) = delete;
  1179. #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1180. private:
  1181. promise& operator=(const promise& __rhs);
  1182. public:
  1183. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1184. _LIBCPP_INLINE_VISIBILITY
  1185. void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
  1186. // retrieving the result
  1187. future<_Rp> get_future();
  1188. // setting the result
  1189. void set_value(const _Rp& __r);
  1190. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1191. void set_value(_Rp&& __r);
  1192. #endif
  1193. void set_exception(exception_ptr __p);
  1194. // setting the result with deferred notification
  1195. void set_value_at_thread_exit(const _Rp& __r);
  1196. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1197. void set_value_at_thread_exit(_Rp&& __r);
  1198. #endif
  1199. void set_exception_at_thread_exit(exception_ptr __p);
  1200. };
  1201. template <class _Rp>
  1202. promise<_Rp>::promise()
  1203. : __state_(new __assoc_state<_Rp>)
  1204. {
  1205. }
  1206. template <class _Rp>
  1207. template <class _Alloc>
  1208. promise<_Rp>::promise(allocator_arg_t, const _Alloc& __a0)
  1209. {
  1210. typedef __assoc_state_alloc<_Rp, _Alloc> _State;
  1211. typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
  1212. typedef __allocator_destructor<_A2> _D2;
  1213. _A2 __a(__a0);
  1214. unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
  1215. ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0);
  1216. __state_ = _VSTD::addressof(*__hold.release());
  1217. }
  1218. template <class _Rp>
  1219. promise<_Rp>::~promise()
  1220. {
  1221. if (__state_)
  1222. {
  1223. if (!__state_->__has_value() && __state_->use_count() > 1)
  1224. __state_->set_exception(make_exception_ptr(
  1225. future_error(make_error_code(future_errc::broken_promise))
  1226. ));
  1227. __state_->__release_shared();
  1228. }
  1229. }
  1230. template <class _Rp>
  1231. future<_Rp>
  1232. promise<_Rp>::get_future()
  1233. {
  1234. if (__state_ == nullptr)
  1235. __throw_future_error(future_errc::no_state);
  1236. return future<_Rp>(__state_);
  1237. }
  1238. template <class _Rp>
  1239. void
  1240. promise<_Rp>::set_value(const _Rp& __r)
  1241. {
  1242. if (__state_ == nullptr)
  1243. __throw_future_error(future_errc::no_state);
  1244. __state_->set_value(__r);
  1245. }
  1246. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1247. template <class _Rp>
  1248. void
  1249. promise<_Rp>::set_value(_Rp&& __r)
  1250. {
  1251. if (__state_ == nullptr)
  1252. __throw_future_error(future_errc::no_state);
  1253. __state_->set_value(_VSTD::move(__r));
  1254. }
  1255. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1256. template <class _Rp>
  1257. void
  1258. promise<_Rp>::set_exception(exception_ptr __p)
  1259. {
  1260. _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception: received nullptr" );
  1261. if (__state_ == nullptr)
  1262. __throw_future_error(future_errc::no_state);
  1263. __state_->set_exception(__p);
  1264. }
  1265. template <class _Rp>
  1266. void
  1267. promise<_Rp>::set_value_at_thread_exit(const _Rp& __r)
  1268. {
  1269. if (__state_ == nullptr)
  1270. __throw_future_error(future_errc::no_state);
  1271. __state_->set_value_at_thread_exit(__r);
  1272. }
  1273. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1274. template <class _Rp>
  1275. void
  1276. promise<_Rp>::set_value_at_thread_exit(_Rp&& __r)
  1277. {
  1278. if (__state_ == nullptr)
  1279. __throw_future_error(future_errc::no_state);
  1280. __state_->set_value_at_thread_exit(_VSTD::move(__r));
  1281. }
  1282. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1283. template <class _Rp>
  1284. void
  1285. promise<_Rp>::set_exception_at_thread_exit(exception_ptr __p)
  1286. {
  1287. _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception_at_thread_exit: received nullptr" );
  1288. if (__state_ == nullptr)
  1289. __throw_future_error(future_errc::no_state);
  1290. __state_->set_exception_at_thread_exit(__p);
  1291. }
  1292. // promise<R&>
  1293. template <class _Rp>
  1294. class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE promise<_Rp&>
  1295. {
  1296. __assoc_state<_Rp&>* __state_;
  1297. _LIBCPP_INLINE_VISIBILITY
  1298. explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
  1299. template <class> friend class packaged_task;
  1300. public:
  1301. promise();
  1302. template <class _Allocator>
  1303. promise(allocator_arg_t, const _Allocator& __a);
  1304. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1305. _LIBCPP_INLINE_VISIBILITY
  1306. promise(promise&& __rhs) _NOEXCEPT
  1307. : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
  1308. promise(const promise& __rhs) = delete;
  1309. #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1310. private:
  1311. promise(const promise& __rhs);
  1312. public:
  1313. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1314. ~promise();
  1315. // assignment
  1316. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1317. _LIBCPP_INLINE_VISIBILITY
  1318. promise& operator=(promise&& __rhs) _NOEXCEPT
  1319. {
  1320. promise(std::move(__rhs)).swap(*this);
  1321. return *this;
  1322. }
  1323. promise& operator=(const promise& __rhs) = delete;
  1324. #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1325. private:
  1326. promise& operator=(const promise& __rhs);
  1327. public:
  1328. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1329. _LIBCPP_INLINE_VISIBILITY
  1330. void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
  1331. // retrieving the result
  1332. future<_Rp&> get_future();
  1333. // setting the result
  1334. void set_value(_Rp& __r);
  1335. void set_exception(exception_ptr __p);
  1336. // setting the result with deferred notification
  1337. void set_value_at_thread_exit(_Rp&);
  1338. void set_exception_at_thread_exit(exception_ptr __p);
  1339. };
  1340. template <class _Rp>
  1341. promise<_Rp&>::promise()
  1342. : __state_(new __assoc_state<_Rp&>)
  1343. {
  1344. }
  1345. template <class _Rp>
  1346. template <class _Alloc>
  1347. promise<_Rp&>::promise(allocator_arg_t, const _Alloc& __a0)
  1348. {
  1349. typedef __assoc_state_alloc<_Rp&, _Alloc> _State;
  1350. typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
  1351. typedef __allocator_destructor<_A2> _D2;
  1352. _A2 __a(__a0);
  1353. unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
  1354. ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0);
  1355. __state_ = _VSTD::addressof(*__hold.release());
  1356. }
  1357. template <class _Rp>
  1358. promise<_Rp&>::~promise()
  1359. {
  1360. if (__state_)
  1361. {
  1362. if (!__state_->__has_value() && __state_->use_count() > 1)
  1363. __state_->set_exception(make_exception_ptr(
  1364. future_error(make_error_code(future_errc::broken_promise))
  1365. ));
  1366. __state_->__release_shared();
  1367. }
  1368. }
  1369. template <class _Rp>
  1370. future<_Rp&>
  1371. promise<_Rp&>::get_future()
  1372. {
  1373. if (__state_ == nullptr)
  1374. __throw_future_error(future_errc::no_state);
  1375. return future<_Rp&>(__state_);
  1376. }
  1377. template <class _Rp>
  1378. void
  1379. promise<_Rp&>::set_value(_Rp& __r)
  1380. {
  1381. if (__state_ == nullptr)
  1382. __throw_future_error(future_errc::no_state);
  1383. __state_->set_value(__r);
  1384. }
  1385. template <class _Rp>
  1386. void
  1387. promise<_Rp&>::set_exception(exception_ptr __p)
  1388. {
  1389. _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception: received nullptr" );
  1390. if (__state_ == nullptr)
  1391. __throw_future_error(future_errc::no_state);
  1392. __state_->set_exception(__p);
  1393. }
  1394. template <class _Rp>
  1395. void
  1396. promise<_Rp&>::set_value_at_thread_exit(_Rp& __r)
  1397. {
  1398. if (__state_ == nullptr)
  1399. __throw_future_error(future_errc::no_state);
  1400. __state_->set_value_at_thread_exit(__r);
  1401. }
  1402. template <class _Rp>
  1403. void
  1404. promise<_Rp&>::set_exception_at_thread_exit(exception_ptr __p)
  1405. {
  1406. _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception_at_thread_exit: received nullptr" );
  1407. if (__state_ == nullptr)
  1408. __throw_future_error(future_errc::no_state);
  1409. __state_->set_exception_at_thread_exit(__p);
  1410. }
  1411. // promise<void>
  1412. template <>
  1413. class _LIBCPP_TYPE_VIS _LIBCPP_AVAILABILITY_FUTURE promise<void>
  1414. {
  1415. __assoc_sub_state* __state_;
  1416. _LIBCPP_INLINE_VISIBILITY
  1417. explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
  1418. template <class> friend class packaged_task;
  1419. public:
  1420. promise();
  1421. template <class _Allocator>
  1422. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
  1423. promise(allocator_arg_t, const _Allocator& __a);
  1424. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1425. _LIBCPP_INLINE_VISIBILITY
  1426. promise(promise&& __rhs) _NOEXCEPT
  1427. : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
  1428. promise(const promise& __rhs) = delete;
  1429. #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1430. private:
  1431. promise(const promise& __rhs);
  1432. public:
  1433. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1434. ~promise();
  1435. // assignment
  1436. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1437. _LIBCPP_INLINE_VISIBILITY
  1438. promise& operator=(promise&& __rhs) _NOEXCEPT
  1439. {
  1440. promise(std::move(__rhs)).swap(*this);
  1441. return *this;
  1442. }
  1443. promise& operator=(const promise& __rhs) = delete;
  1444. #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1445. private:
  1446. promise& operator=(const promise& __rhs);
  1447. public:
  1448. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1449. _LIBCPP_INLINE_VISIBILITY
  1450. void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
  1451. // retrieving the result
  1452. future<void> get_future();
  1453. // setting the result
  1454. void set_value();
  1455. void set_exception(exception_ptr __p);
  1456. // setting the result with deferred notification
  1457. void set_value_at_thread_exit();
  1458. void set_exception_at_thread_exit(exception_ptr __p);
  1459. };
  1460. template <class _Alloc>
  1461. promise<void>::promise(allocator_arg_t, const _Alloc& __a0)
  1462. {
  1463. typedef __assoc_sub_state_alloc<_Alloc> _State;
  1464. typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
  1465. typedef __allocator_destructor<_A2> _D2;
  1466. _A2 __a(__a0);
  1467. unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
  1468. ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0);
  1469. __state_ = _VSTD::addressof(*__hold.release());
  1470. }
  1471. template <class _Rp>
  1472. inline _LIBCPP_INLINE_VISIBILITY
  1473. void
  1474. swap(promise<_Rp>& __x, promise<_Rp>& __y) _NOEXCEPT
  1475. {
  1476. __x.swap(__y);
  1477. }
  1478. template <class _Rp, class _Alloc>
  1479. struct _LIBCPP_TEMPLATE_VIS uses_allocator<promise<_Rp>, _Alloc>
  1480. : public true_type {};
  1481. #ifndef _LIBCPP_HAS_NO_VARIADICS
  1482. // packaged_task
  1483. template<class _Fp> class __packaged_task_base;
  1484. template<class _Rp, class ..._ArgTypes>
  1485. class _LIBCPP_AVAILABILITY_FUTURE __packaged_task_base<_Rp(_ArgTypes...)>
  1486. {
  1487. __packaged_task_base(const __packaged_task_base&);
  1488. __packaged_task_base& operator=(const __packaged_task_base&);
  1489. public:
  1490. _LIBCPP_INLINE_VISIBILITY
  1491. __packaged_task_base() {}
  1492. _LIBCPP_INLINE_VISIBILITY
  1493. virtual ~__packaged_task_base() {}
  1494. virtual void __move_to(__packaged_task_base*) _NOEXCEPT = 0;
  1495. virtual void destroy() = 0;
  1496. virtual void destroy_deallocate() = 0;
  1497. virtual _Rp operator()(_ArgTypes&& ...) = 0;
  1498. };
  1499. template<class _FD, class _Alloc, class _FB> class __packaged_task_func;
  1500. template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
  1501. class _LIBCPP_AVAILABILITY_FUTURE __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>
  1502. : public __packaged_task_base<_Rp(_ArgTypes...)>
  1503. {
  1504. __compressed_pair<_Fp, _Alloc> __f_;
  1505. public:
  1506. _LIBCPP_INLINE_VISIBILITY
  1507. explicit __packaged_task_func(const _Fp& __f) : __f_(__f) {}
  1508. _LIBCPP_INLINE_VISIBILITY
  1509. explicit __packaged_task_func(_Fp&& __f) : __f_(_VSTD::move(__f)) {}
  1510. _LIBCPP_INLINE_VISIBILITY
  1511. __packaged_task_func(const _Fp& __f, const _Alloc& __a)
  1512. : __f_(__f, __a) {}
  1513. _LIBCPP_INLINE_VISIBILITY
  1514. __packaged_task_func(_Fp&& __f, const _Alloc& __a)
  1515. : __f_(_VSTD::move(__f), __a) {}
  1516. virtual void __move_to(__packaged_task_base<_Rp(_ArgTypes...)>*) _NOEXCEPT;
  1517. virtual void destroy();
  1518. virtual void destroy_deallocate();
  1519. virtual _Rp operator()(_ArgTypes&& ... __args);
  1520. };
  1521. template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
  1522. void
  1523. __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__move_to(
  1524. __packaged_task_base<_Rp(_ArgTypes...)>* __p) _NOEXCEPT
  1525. {
  1526. ::new (__p) __packaged_task_func(_VSTD::move(__f_.first()), _VSTD::move(__f_.second()));
  1527. }
  1528. template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
  1529. void
  1530. __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy()
  1531. {
  1532. __f_.~__compressed_pair<_Fp, _Alloc>();
  1533. }
  1534. template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
  1535. void
  1536. __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate()
  1537. {
  1538. typedef typename __allocator_traits_rebind<_Alloc, __packaged_task_func>::type _Ap;
  1539. typedef allocator_traits<_Ap> _ATraits;
  1540. typedef pointer_traits<typename _ATraits::pointer> _PTraits;
  1541. _Ap __a(__f_.second());
  1542. __f_.~__compressed_pair<_Fp, _Alloc>();
  1543. __a.deallocate(_PTraits::pointer_to(*this), 1);
  1544. }
  1545. template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
  1546. _Rp
  1547. __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
  1548. {
  1549. return __invoke(__f_.first(), _VSTD::forward<_ArgTypes>(__arg)...);
  1550. }
  1551. template <class _Callable> class __packaged_task_function;
  1552. template<class _Rp, class ..._ArgTypes>
  1553. class _LIBCPP_AVAILABILITY_FUTURE __packaged_task_function<_Rp(_ArgTypes...)>
  1554. {
  1555. typedef __packaged_task_base<_Rp(_ArgTypes...)> __base;
  1556. typename aligned_storage<3*sizeof(void*)>::type __buf_;
  1557. __base* __f_;
  1558. public:
  1559. typedef _Rp result_type;
  1560. // construct/copy/destroy:
  1561. _LIBCPP_INLINE_VISIBILITY
  1562. __packaged_task_function() _NOEXCEPT : __f_(nullptr) {}
  1563. template<class _Fp>
  1564. __packaged_task_function(_Fp&& __f);
  1565. template<class _Fp, class _Alloc>
  1566. __packaged_task_function(allocator_arg_t, const _Alloc& __a, _Fp&& __f);
  1567. __packaged_task_function(__packaged_task_function&&) _NOEXCEPT;
  1568. __packaged_task_function& operator=(__packaged_task_function&&) _NOEXCEPT;
  1569. __packaged_task_function(const __packaged_task_function&) = delete;
  1570. __packaged_task_function& operator=(const __packaged_task_function&) = delete;
  1571. ~__packaged_task_function();
  1572. void swap(__packaged_task_function&) _NOEXCEPT;
  1573. _LIBCPP_INLINE_VISIBILITY
  1574. _Rp operator()(_ArgTypes...) const;
  1575. };
  1576. template<class _Rp, class ..._ArgTypes>
  1577. __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f) _NOEXCEPT
  1578. {
  1579. if (__f.__f_ == nullptr)
  1580. __f_ = nullptr;
  1581. else if (__f.__f_ == (__base*)&__f.__buf_)
  1582. {
  1583. __f_ = (__base*)&__buf_;
  1584. __f.__f_->__move_to(__f_);
  1585. }
  1586. else
  1587. {
  1588. __f_ = __f.__f_;
  1589. __f.__f_ = nullptr;
  1590. }
  1591. }
  1592. template<class _Rp, class ..._ArgTypes>
  1593. template <class _Fp>
  1594. __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(_Fp&& __f)
  1595. : __f_(nullptr)
  1596. {
  1597. typedef typename remove_reference<typename decay<_Fp>::type>::type _FR;
  1598. typedef __packaged_task_func<_FR, allocator<_FR>, _Rp(_ArgTypes...)> _FF;
  1599. if (sizeof(_FF) <= sizeof(__buf_))
  1600. {
  1601. __f_ = (__base*)&__buf_;
  1602. ::new (__f_) _FF(_VSTD::forward<_Fp>(__f));
  1603. }
  1604. else
  1605. {
  1606. typedef allocator<_FF> _Ap;
  1607. _Ap __a;
  1608. typedef __allocator_destructor<_Ap> _Dp;
  1609. unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
  1610. ::new (__hold.get()) _FF(_VSTD::forward<_Fp>(__f), allocator<_FR>(__a));
  1611. __f_ = __hold.release();
  1612. }
  1613. }
  1614. template<class _Rp, class ..._ArgTypes>
  1615. template <class _Fp, class _Alloc>
  1616. __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(
  1617. allocator_arg_t, const _Alloc& __a0, _Fp&& __f)
  1618. : __f_(nullptr)
  1619. {
  1620. typedef typename remove_reference<typename decay<_Fp>::type>::type _FR;
  1621. typedef __packaged_task_func<_FR, _Alloc, _Rp(_ArgTypes...)> _FF;
  1622. if (sizeof(_FF) <= sizeof(__buf_))
  1623. {
  1624. __f_ = (__base*)&__buf_;
  1625. ::new (__f_) _FF(_VSTD::forward<_Fp>(__f));
  1626. }
  1627. else
  1628. {
  1629. typedef typename __allocator_traits_rebind<_Alloc, _FF>::type _Ap;
  1630. _Ap __a(__a0);
  1631. typedef __allocator_destructor<_Ap> _Dp;
  1632. unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
  1633. ::new (static_cast<void*>(_VSTD::addressof(*__hold.get())))
  1634. _FF(_VSTD::forward<_Fp>(__f), _Alloc(__a));
  1635. __f_ = _VSTD::addressof(*__hold.release());
  1636. }
  1637. }
  1638. template<class _Rp, class ..._ArgTypes>
  1639. __packaged_task_function<_Rp(_ArgTypes...)>&
  1640. __packaged_task_function<_Rp(_ArgTypes...)>::operator=(__packaged_task_function&& __f) _NOEXCEPT
  1641. {
  1642. if (__f_ == (__base*)&__buf_)
  1643. __f_->destroy();
  1644. else if (__f_)
  1645. __f_->destroy_deallocate();
  1646. __f_ = nullptr;
  1647. if (__f.__f_ == nullptr)
  1648. __f_ = nullptr;
  1649. else if (__f.__f_ == (__base*)&__f.__buf_)
  1650. {
  1651. __f_ = (__base*)&__buf_;
  1652. __f.__f_->__move_to(__f_);
  1653. }
  1654. else
  1655. {
  1656. __f_ = __f.__f_;
  1657. __f.__f_ = nullptr;
  1658. }
  1659. return *this;
  1660. }
  1661. template<class _Rp, class ..._ArgTypes>
  1662. __packaged_task_function<_Rp(_ArgTypes...)>::~__packaged_task_function()
  1663. {
  1664. if (__f_ == (__base*)&__buf_)
  1665. __f_->destroy();
  1666. else if (__f_)
  1667. __f_->destroy_deallocate();
  1668. }
  1669. template<class _Rp, class ..._ArgTypes>
  1670. void
  1671. __packaged_task_function<_Rp(_ArgTypes...)>::swap(__packaged_task_function& __f) _NOEXCEPT
  1672. {
  1673. if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
  1674. {
  1675. typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
  1676. __base* __t = (__base*)&__tempbuf;
  1677. __f_->__move_to(__t);
  1678. __f_->destroy();
  1679. __f_ = nullptr;
  1680. __f.__f_->__move_to((__base*)&__buf_);
  1681. __f.__f_->destroy();
  1682. __f.__f_ = nullptr;
  1683. __f_ = (__base*)&__buf_;
  1684. __t->__move_to((__base*)&__f.__buf_);
  1685. __t->destroy();
  1686. __f.__f_ = (__base*)&__f.__buf_;
  1687. }
  1688. else if (__f_ == (__base*)&__buf_)
  1689. {
  1690. __f_->__move_to((__base*)&__f.__buf_);
  1691. __f_->destroy();
  1692. __f_ = __f.__f_;
  1693. __f.__f_ = (__base*)&__f.__buf_;
  1694. }
  1695. else if (__f.__f_ == (__base*)&__f.__buf_)
  1696. {
  1697. __f.__f_->__move_to((__base*)&__buf_);
  1698. __f.__f_->destroy();
  1699. __f.__f_ = __f_;
  1700. __f_ = (__base*)&__buf_;
  1701. }
  1702. else
  1703. _VSTD::swap(__f_, __f.__f_);
  1704. }
  1705. template<class _Rp, class ..._ArgTypes>
  1706. inline
  1707. _Rp
  1708. __packaged_task_function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
  1709. {
  1710. return (*__f_)(_VSTD::forward<_ArgTypes>(__arg)...);
  1711. }
  1712. template<class _Rp, class ..._ArgTypes>
  1713. class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE packaged_task<_Rp(_ArgTypes...)>
  1714. {
  1715. public:
  1716. typedef _Rp result_type; // extension
  1717. private:
  1718. __packaged_task_function<result_type(_ArgTypes...)> __f_;
  1719. promise<result_type> __p_;
  1720. public:
  1721. // construction and destruction
  1722. _LIBCPP_INLINE_VISIBILITY
  1723. packaged_task() _NOEXCEPT : __p_(nullptr) {}
  1724. template <class _Fp,
  1725. class = typename enable_if
  1726. <
  1727. !is_same<
  1728. typename __uncvref<_Fp>::type,
  1729. packaged_task
  1730. >::value
  1731. >::type
  1732. >
  1733. _LIBCPP_INLINE_VISIBILITY
  1734. explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
  1735. template <class _Fp, class _Allocator,
  1736. class = typename enable_if
  1737. <
  1738. !is_same<
  1739. typename __uncvref<_Fp>::type,
  1740. packaged_task
  1741. >::value
  1742. >::type
  1743. >
  1744. _LIBCPP_INLINE_VISIBILITY
  1745. packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
  1746. : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)),
  1747. __p_(allocator_arg, __a) {}
  1748. // ~packaged_task() = default;
  1749. // no copy
  1750. packaged_task(const packaged_task&) = delete;
  1751. packaged_task& operator=(const packaged_task&) = delete;
  1752. // move support
  1753. _LIBCPP_INLINE_VISIBILITY
  1754. packaged_task(packaged_task&& __other) _NOEXCEPT
  1755. : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
  1756. _LIBCPP_INLINE_VISIBILITY
  1757. packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
  1758. {
  1759. __f_ = _VSTD::move(__other.__f_);
  1760. __p_ = _VSTD::move(__other.__p_);
  1761. return *this;
  1762. }
  1763. _LIBCPP_INLINE_VISIBILITY
  1764. void swap(packaged_task& __other) _NOEXCEPT
  1765. {
  1766. __f_.swap(__other.__f_);
  1767. __p_.swap(__other.__p_);
  1768. }
  1769. _LIBCPP_INLINE_VISIBILITY
  1770. bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
  1771. // result retrieval
  1772. _LIBCPP_INLINE_VISIBILITY
  1773. future<result_type> get_future() {return __p_.get_future();}
  1774. // execution
  1775. void operator()(_ArgTypes... __args);
  1776. void make_ready_at_thread_exit(_ArgTypes... __args);
  1777. void reset();
  1778. };
  1779. template<class _Rp, class ..._ArgTypes>
  1780. void
  1781. packaged_task<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __args)
  1782. {
  1783. if (__p_.__state_ == nullptr)
  1784. __throw_future_error(future_errc::no_state);
  1785. if (__p_.__state_->__has_value())
  1786. __throw_future_error(future_errc::promise_already_satisfied);
  1787. #ifndef _LIBCPP_NO_EXCEPTIONS
  1788. try
  1789. {
  1790. #endif // _LIBCPP_NO_EXCEPTIONS
  1791. __p_.set_value(__f_(_VSTD::forward<_ArgTypes>(__args)...));
  1792. #ifndef _LIBCPP_NO_EXCEPTIONS
  1793. }
  1794. catch (...)
  1795. {
  1796. __p_.set_exception(current_exception());
  1797. }
  1798. #endif // _LIBCPP_NO_EXCEPTIONS
  1799. }
  1800. template<class _Rp, class ..._ArgTypes>
  1801. void
  1802. packaged_task<_Rp(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
  1803. {
  1804. if (__p_.__state_ == nullptr)
  1805. __throw_future_error(future_errc::no_state);
  1806. if (__p_.__state_->__has_value())
  1807. __throw_future_error(future_errc::promise_already_satisfied);
  1808. #ifndef _LIBCPP_NO_EXCEPTIONS
  1809. try
  1810. {
  1811. #endif // _LIBCPP_NO_EXCEPTIONS
  1812. __p_.set_value_at_thread_exit(__f_(_VSTD::forward<_ArgTypes>(__args)...));
  1813. #ifndef _LIBCPP_NO_EXCEPTIONS
  1814. }
  1815. catch (...)
  1816. {
  1817. __p_.set_exception_at_thread_exit(current_exception());
  1818. }
  1819. #endif // _LIBCPP_NO_EXCEPTIONS
  1820. }
  1821. template<class _Rp, class ..._ArgTypes>
  1822. void
  1823. packaged_task<_Rp(_ArgTypes...)>::reset()
  1824. {
  1825. if (!valid())
  1826. __throw_future_error(future_errc::no_state);
  1827. __p_ = promise<result_type>();
  1828. }
  1829. template<class ..._ArgTypes>
  1830. class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE packaged_task<void(_ArgTypes...)>
  1831. {
  1832. public:
  1833. typedef void result_type; // extension
  1834. private:
  1835. __packaged_task_function<result_type(_ArgTypes...)> __f_;
  1836. promise<result_type> __p_;
  1837. public:
  1838. // construction and destruction
  1839. _LIBCPP_INLINE_VISIBILITY
  1840. packaged_task() _NOEXCEPT : __p_(nullptr) {}
  1841. template <class _Fp,
  1842. class = typename enable_if
  1843. <
  1844. !is_same<
  1845. typename __uncvref<_Fp>::type,
  1846. packaged_task
  1847. >::value
  1848. >::type
  1849. >
  1850. _LIBCPP_INLINE_VISIBILITY
  1851. explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
  1852. template <class _Fp, class _Allocator,
  1853. class = typename enable_if
  1854. <
  1855. !is_same<
  1856. typename __uncvref<_Fp>::type,
  1857. packaged_task
  1858. >::value
  1859. >::type
  1860. >
  1861. _LIBCPP_INLINE_VISIBILITY
  1862. packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
  1863. : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)),
  1864. __p_(allocator_arg, __a) {}
  1865. // ~packaged_task() = default;
  1866. // no copy
  1867. packaged_task(const packaged_task&) = delete;
  1868. packaged_task& operator=(const packaged_task&) = delete;
  1869. // move support
  1870. _LIBCPP_INLINE_VISIBILITY
  1871. packaged_task(packaged_task&& __other) _NOEXCEPT
  1872. : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
  1873. _LIBCPP_INLINE_VISIBILITY
  1874. packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
  1875. {
  1876. __f_ = _VSTD::move(__other.__f_);
  1877. __p_ = _VSTD::move(__other.__p_);
  1878. return *this;
  1879. }
  1880. _LIBCPP_INLINE_VISIBILITY
  1881. void swap(packaged_task& __other) _NOEXCEPT
  1882. {
  1883. __f_.swap(__other.__f_);
  1884. __p_.swap(__other.__p_);
  1885. }
  1886. _LIBCPP_INLINE_VISIBILITY
  1887. bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
  1888. // result retrieval
  1889. _LIBCPP_INLINE_VISIBILITY
  1890. future<result_type> get_future() {return __p_.get_future();}
  1891. // execution
  1892. void operator()(_ArgTypes... __args);
  1893. void make_ready_at_thread_exit(_ArgTypes... __args);
  1894. void reset();
  1895. };
  1896. template<class ..._ArgTypes>
  1897. void
  1898. packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args)
  1899. {
  1900. if (__p_.__state_ == nullptr)
  1901. __throw_future_error(future_errc::no_state);
  1902. if (__p_.__state_->__has_value())
  1903. __throw_future_error(future_errc::promise_already_satisfied);
  1904. #ifndef _LIBCPP_NO_EXCEPTIONS
  1905. try
  1906. {
  1907. #endif // _LIBCPP_NO_EXCEPTIONS
  1908. __f_(_VSTD::forward<_ArgTypes>(__args)...);
  1909. __p_.set_value();
  1910. #ifndef _LIBCPP_NO_EXCEPTIONS
  1911. }
  1912. catch (...)
  1913. {
  1914. __p_.set_exception(current_exception());
  1915. }
  1916. #endif // _LIBCPP_NO_EXCEPTIONS
  1917. }
  1918. template<class ..._ArgTypes>
  1919. void
  1920. packaged_task<void(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
  1921. {
  1922. if (__p_.__state_ == nullptr)
  1923. __throw_future_error(future_errc::no_state);
  1924. if (__p_.__state_->__has_value())
  1925. __throw_future_error(future_errc::promise_already_satisfied);
  1926. #ifndef _LIBCPP_NO_EXCEPTIONS
  1927. try
  1928. {
  1929. #endif // _LIBCPP_NO_EXCEPTIONS
  1930. __f_(_VSTD::forward<_ArgTypes>(__args)...);
  1931. __p_.set_value_at_thread_exit();
  1932. #ifndef _LIBCPP_NO_EXCEPTIONS
  1933. }
  1934. catch (...)
  1935. {
  1936. __p_.set_exception_at_thread_exit(current_exception());
  1937. }
  1938. #endif // _LIBCPP_NO_EXCEPTIONS
  1939. }
  1940. template<class ..._ArgTypes>
  1941. void
  1942. packaged_task<void(_ArgTypes...)>::reset()
  1943. {
  1944. if (!valid())
  1945. __throw_future_error(future_errc::no_state);
  1946. __p_ = promise<result_type>();
  1947. }
  1948. template <class _Callable>
  1949. inline _LIBCPP_INLINE_VISIBILITY
  1950. void
  1951. swap(packaged_task<_Callable>& __x, packaged_task<_Callable>& __y) _NOEXCEPT
  1952. {
  1953. __x.swap(__y);
  1954. }
  1955. template <class _Callable, class _Alloc>
  1956. struct _LIBCPP_TEMPLATE_VIS uses_allocator<packaged_task<_Callable>, _Alloc>
  1957. : public true_type {};
  1958. template <class _Rp, class _Fp>
  1959. future<_Rp>
  1960. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1961. __make_deferred_assoc_state(_Fp&& __f)
  1962. #else
  1963. __make_deferred_assoc_state(_Fp __f)
  1964. #endif
  1965. {
  1966. unique_ptr<__deferred_assoc_state<_Rp, _Fp>, __release_shared_count>
  1967. __h(new __deferred_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
  1968. return future<_Rp>(__h.get());
  1969. }
  1970. template <class _Rp, class _Fp>
  1971. future<_Rp>
  1972. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1973. __make_async_assoc_state(_Fp&& __f)
  1974. #else
  1975. __make_async_assoc_state(_Fp __f)
  1976. #endif
  1977. {
  1978. unique_ptr<__async_assoc_state<_Rp, _Fp>, __release_shared_count>
  1979. __h(new __async_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
  1980. _VSTD::thread(&__async_assoc_state<_Rp, _Fp>::__execute, __h.get()).detach();
  1981. return future<_Rp>(__h.get());
  1982. }
  1983. template <class _Fp, class... _Args>
  1984. class __async_func
  1985. {
  1986. tuple<_Fp, _Args...> __f_;
  1987. public:
  1988. typedef typename __invoke_of<_Fp, _Args...>::type _Rp;
  1989. _LIBCPP_INLINE_VISIBILITY
  1990. explicit __async_func(_Fp&& __f, _Args&&... __args)
  1991. : __f_(_VSTD::move(__f), _VSTD::move(__args)...) {}
  1992. _LIBCPP_INLINE_VISIBILITY
  1993. __async_func(__async_func&& __f) : __f_(_VSTD::move(__f.__f_)) {}
  1994. _Rp operator()()
  1995. {
  1996. typedef typename __make_tuple_indices<1+sizeof...(_Args), 1>::type _Index;
  1997. return __execute(_Index());
  1998. }
  1999. private:
  2000. template <size_t ..._Indices>
  2001. _Rp
  2002. __execute(__tuple_indices<_Indices...>)
  2003. {
  2004. return __invoke(_VSTD::move(_VSTD::get<0>(__f_)), _VSTD::move(_VSTD::get<_Indices>(__f_))...);
  2005. }
  2006. };
  2007. inline _LIBCPP_INLINE_VISIBILITY bool __does_policy_contain(launch __policy, launch __value )
  2008. { return (int(__policy) & int(__value)) != 0; }
  2009. template <class _Fp, class... _Args>
  2010. _LIBCPP_NODISCARD_AFTER_CXX17
  2011. future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
  2012. async(launch __policy, _Fp&& __f, _Args&&... __args)
  2013. {
  2014. typedef __async_func<typename decay<_Fp>::type, typename decay<_Args>::type...> _BF;
  2015. typedef typename _BF::_Rp _Rp;
  2016. #ifndef _LIBCPP_NO_EXCEPTIONS
  2017. try
  2018. {
  2019. #endif
  2020. if (__does_policy_contain(__policy, launch::async))
  2021. return _VSTD::__make_async_assoc_state<_Rp>(_BF(__decay_copy(_VSTD::forward<_Fp>(__f)),
  2022. __decay_copy(_VSTD::forward<_Args>(__args))...));
  2023. #ifndef _LIBCPP_NO_EXCEPTIONS
  2024. }
  2025. catch ( ... ) { if (__policy == launch::async) throw ; }
  2026. #endif
  2027. if (__does_policy_contain(__policy, launch::deferred))
  2028. return _VSTD::__make_deferred_assoc_state<_Rp>(_BF(__decay_copy(_VSTD::forward<_Fp>(__f)),
  2029. __decay_copy(_VSTD::forward<_Args>(__args))...));
  2030. return future<_Rp>{};
  2031. }
  2032. template <class _Fp, class... _Args>
  2033. _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY
  2034. future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
  2035. async(_Fp&& __f, _Args&&... __args)
  2036. {
  2037. return _VSTD::async(launch::any, _VSTD::forward<_Fp>(__f),
  2038. _VSTD::forward<_Args>(__args)...);
  2039. }
  2040. #endif // _LIBCPP_HAS_NO_VARIADICS
  2041. // shared_future
  2042. template <class _Rp>
  2043. class _LIBCPP_TEMPLATE_VIS shared_future
  2044. {
  2045. __assoc_state<_Rp>* __state_;
  2046. public:
  2047. _LIBCPP_INLINE_VISIBILITY
  2048. shared_future() _NOEXCEPT : __state_(nullptr) {}
  2049. _LIBCPP_INLINE_VISIBILITY
  2050. shared_future(const shared_future& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
  2051. {if (__state_) __state_->__add_shared();}
  2052. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  2053. _LIBCPP_INLINE_VISIBILITY
  2054. shared_future(future<_Rp>&& __f) _NOEXCEPT : __state_(__f.__state_)
  2055. {__f.__state_ = nullptr;}
  2056. _LIBCPP_INLINE_VISIBILITY
  2057. shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
  2058. {__rhs.__state_ = nullptr;}
  2059. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  2060. ~shared_future();
  2061. shared_future& operator=(const shared_future& __rhs) _NOEXCEPT;
  2062. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  2063. _LIBCPP_INLINE_VISIBILITY
  2064. shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
  2065. {
  2066. shared_future(std::move(__rhs)).swap(*this);
  2067. return *this;
  2068. }
  2069. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  2070. // retrieving the value
  2071. _LIBCPP_INLINE_VISIBILITY
  2072. const _Rp& get() const {return __state_->copy();}
  2073. _LIBCPP_INLINE_VISIBILITY
  2074. void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
  2075. // functions to check state
  2076. _LIBCPP_INLINE_VISIBILITY
  2077. bool valid() const _NOEXCEPT {return __state_ != nullptr;}
  2078. _LIBCPP_INLINE_VISIBILITY
  2079. void wait() const {__state_->wait();}
  2080. template <class _Rep, class _Period>
  2081. _LIBCPP_INLINE_VISIBILITY
  2082. future_status
  2083. wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
  2084. {return __state_->wait_for(__rel_time);}
  2085. template <class _Clock, class _Duration>
  2086. _LIBCPP_INLINE_VISIBILITY
  2087. future_status
  2088. wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
  2089. {return __state_->wait_until(__abs_time);}
  2090. };
  2091. template <class _Rp>
  2092. shared_future<_Rp>::~shared_future()
  2093. {
  2094. if (__state_)
  2095. __state_->__release_shared();
  2096. }
  2097. template <class _Rp>
  2098. shared_future<_Rp>&
  2099. shared_future<_Rp>::operator=(const shared_future& __rhs) _NOEXCEPT
  2100. {
  2101. if (__rhs.__state_)
  2102. __rhs.__state_->__add_shared();
  2103. if (__state_)
  2104. __state_->__release_shared();
  2105. __state_ = __rhs.__state_;
  2106. return *this;
  2107. }
  2108. template <class _Rp>
  2109. class _LIBCPP_TEMPLATE_VIS shared_future<_Rp&>
  2110. {
  2111. __assoc_state<_Rp&>* __state_;
  2112. public:
  2113. _LIBCPP_INLINE_VISIBILITY
  2114. shared_future() _NOEXCEPT : __state_(nullptr) {}
  2115. _LIBCPP_INLINE_VISIBILITY
  2116. shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
  2117. {if (__state_) __state_->__add_shared();}
  2118. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  2119. _LIBCPP_INLINE_VISIBILITY
  2120. shared_future(future<_Rp&>&& __f) _NOEXCEPT : __state_(__f.__state_)
  2121. {__f.__state_ = nullptr;}
  2122. _LIBCPP_INLINE_VISIBILITY
  2123. shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
  2124. {__rhs.__state_ = nullptr;}
  2125. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  2126. ~shared_future();
  2127. shared_future& operator=(const shared_future& __rhs);
  2128. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  2129. _LIBCPP_INLINE_VISIBILITY
  2130. shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
  2131. {
  2132. shared_future(std::move(__rhs)).swap(*this);
  2133. return *this;
  2134. }
  2135. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  2136. // retrieving the value
  2137. _LIBCPP_INLINE_VISIBILITY
  2138. _Rp& get() const {return __state_->copy();}
  2139. _LIBCPP_INLINE_VISIBILITY
  2140. void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
  2141. // functions to check state
  2142. _LIBCPP_INLINE_VISIBILITY
  2143. bool valid() const _NOEXCEPT {return __state_ != nullptr;}
  2144. _LIBCPP_INLINE_VISIBILITY
  2145. void wait() const {__state_->wait();}
  2146. template <class _Rep, class _Period>
  2147. _LIBCPP_INLINE_VISIBILITY
  2148. future_status
  2149. wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
  2150. {return __state_->wait_for(__rel_time);}
  2151. template <class _Clock, class _Duration>
  2152. _LIBCPP_INLINE_VISIBILITY
  2153. future_status
  2154. wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
  2155. {return __state_->wait_until(__abs_time);}
  2156. };
  2157. template <class _Rp>
  2158. shared_future<_Rp&>::~shared_future()
  2159. {
  2160. if (__state_)
  2161. __state_->__release_shared();
  2162. }
  2163. template <class _Rp>
  2164. shared_future<_Rp&>&
  2165. shared_future<_Rp&>::operator=(const shared_future& __rhs)
  2166. {
  2167. if (__rhs.__state_)
  2168. __rhs.__state_->__add_shared();
  2169. if (__state_)
  2170. __state_->__release_shared();
  2171. __state_ = __rhs.__state_;
  2172. return *this;
  2173. }
  2174. template <>
  2175. class _LIBCPP_TYPE_VIS _LIBCPP_AVAILABILITY_FUTURE shared_future<void>
  2176. {
  2177. __assoc_sub_state* __state_;
  2178. public:
  2179. _LIBCPP_INLINE_VISIBILITY
  2180. shared_future() _NOEXCEPT : __state_(nullptr) {}
  2181. _LIBCPP_INLINE_VISIBILITY
  2182. shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
  2183. {if (__state_) __state_->__add_shared();}
  2184. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  2185. _LIBCPP_INLINE_VISIBILITY
  2186. shared_future(future<void>&& __f) _NOEXCEPT : __state_(__f.__state_)
  2187. {__f.__state_ = nullptr;}
  2188. _LIBCPP_INLINE_VISIBILITY
  2189. shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
  2190. {__rhs.__state_ = nullptr;}
  2191. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  2192. ~shared_future();
  2193. shared_future& operator=(const shared_future& __rhs);
  2194. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  2195. _LIBCPP_INLINE_VISIBILITY
  2196. shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
  2197. {
  2198. shared_future(std::move(__rhs)).swap(*this);
  2199. return *this;
  2200. }
  2201. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  2202. // retrieving the value
  2203. _LIBCPP_INLINE_VISIBILITY
  2204. void get() const {__state_->copy();}
  2205. _LIBCPP_INLINE_VISIBILITY
  2206. void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
  2207. // functions to check state
  2208. _LIBCPP_INLINE_VISIBILITY
  2209. bool valid() const _NOEXCEPT {return __state_ != nullptr;}
  2210. _LIBCPP_INLINE_VISIBILITY
  2211. void wait() const {__state_->wait();}
  2212. template <class _Rep, class _Period>
  2213. _LIBCPP_INLINE_VISIBILITY
  2214. future_status
  2215. wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
  2216. {return __state_->wait_for(__rel_time);}
  2217. template <class _Clock, class _Duration>
  2218. _LIBCPP_INLINE_VISIBILITY
  2219. future_status
  2220. wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
  2221. {return __state_->wait_until(__abs_time);}
  2222. };
  2223. template <class _Rp>
  2224. inline _LIBCPP_INLINE_VISIBILITY
  2225. void
  2226. swap(shared_future<_Rp>& __x, shared_future<_Rp>& __y) _NOEXCEPT
  2227. {
  2228. __x.swap(__y);
  2229. }
  2230. template <class _Rp>
  2231. inline
  2232. shared_future<_Rp>
  2233. future<_Rp>::share() _NOEXCEPT
  2234. {
  2235. return shared_future<_Rp>(_VSTD::move(*this));
  2236. }
  2237. template <class _Rp>
  2238. inline
  2239. shared_future<_Rp&>
  2240. future<_Rp&>::share() _NOEXCEPT
  2241. {
  2242. return shared_future<_Rp&>(_VSTD::move(*this));
  2243. }
  2244. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  2245. inline
  2246. shared_future<void>
  2247. future<void>::share() _NOEXCEPT
  2248. {
  2249. return shared_future<void>(_VSTD::move(*this));
  2250. }
  2251. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  2252. _LIBCPP_END_NAMESPACE_STD
  2253. #endif // !_LIBCPP_HAS_NO_THREADS
  2254. #endif // _LIBCPP_FUTURE