future 69 KB

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