future 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065
  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 <__config>
  298. #ifdef _LIBCPP_HAS_NO_THREADS
  299. # error "<future> is not supported since libc++ has been configured without support for threads."
  300. #endif
  301. #include <__assert>
  302. #include <__chrono/duration.h>
  303. #include <__chrono/time_point.h>
  304. #include <__exception/exception_ptr.h>
  305. #include <__memory/addressof.h>
  306. #include <__memory/allocator.h>
  307. #include <__memory/allocator_arg_t.h>
  308. #include <__memory/allocator_destructor.h>
  309. #include <__memory/allocator_traits.h>
  310. #include <__memory/compressed_pair.h>
  311. #include <__memory/pointer_traits.h>
  312. #include <__memory/shared_ptr.h>
  313. #include <__memory/unique_ptr.h>
  314. #include <__memory/uses_allocator.h>
  315. #include <__system_error/error_category.h>
  316. #include <__system_error/error_code.h>
  317. #include <__system_error/error_condition.h>
  318. #include <__type_traits/aligned_storage.h>
  319. #include <__type_traits/strip_signature.h>
  320. #include <__utility/auto_cast.h>
  321. #include <__utility/forward.h>
  322. #include <__utility/move.h>
  323. #include <mutex>
  324. #include <new>
  325. #include <stdexcept>
  326. #include <thread>
  327. #include <version>
  328. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  329. # pragma GCC system_header
  330. #endif
  331. _LIBCPP_PUSH_MACROS
  332. #include <__undef_macros>
  333. _LIBCPP_BEGIN_NAMESPACE_STD
  334. // enum class future_errc
  335. _LIBCPP_DECLARE_STRONG_ENUM(future_errc){
  336. future_already_retrieved = 1, promise_already_satisfied, no_state, broken_promise};
  337. _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_errc)
  338. template <>
  339. struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<future_errc> : public true_type {};
  340. #ifdef _LIBCPP_CXX03_LANG
  341. template <>
  342. struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<future_errc::__lx> : public true_type {};
  343. #endif
  344. // enum class launch
  345. _LIBCPP_DECLARE_STRONG_ENUM(launch){async = 1, deferred = 2, any = async | deferred};
  346. _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(launch)
  347. #ifndef _LIBCPP_CXX03_LANG
  348. typedef underlying_type<launch>::type __launch_underlying_type;
  349. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR launch operator&(launch __x, launch __y) {
  350. return static_cast<launch>(static_cast<__launch_underlying_type>(__x) & static_cast<__launch_underlying_type>(__y));
  351. }
  352. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR launch operator|(launch __x, launch __y) {
  353. return static_cast<launch>(static_cast<__launch_underlying_type>(__x) | static_cast<__launch_underlying_type>(__y));
  354. }
  355. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR launch operator^(launch __x, launch __y) {
  356. return static_cast<launch>(static_cast<__launch_underlying_type>(__x) ^ static_cast<__launch_underlying_type>(__y));
  357. }
  358. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR launch operator~(launch __x) {
  359. return static_cast<launch>(~static_cast<__launch_underlying_type>(__x) & 3);
  360. }
  361. inline _LIBCPP_HIDE_FROM_ABI launch& operator&=(launch& __x, launch __y) {
  362. __x = __x & __y;
  363. return __x;
  364. }
  365. inline _LIBCPP_HIDE_FROM_ABI launch& operator|=(launch& __x, launch __y) {
  366. __x = __x | __y;
  367. return __x;
  368. }
  369. inline _LIBCPP_HIDE_FROM_ABI launch& operator^=(launch& __x, launch __y) {
  370. __x = __x ^ __y;
  371. return __x;
  372. }
  373. #endif // !_LIBCPP_CXX03_LANG
  374. // enum class future_status
  375. _LIBCPP_DECLARE_STRONG_ENUM(future_status){ready, timeout, deferred};
  376. _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_status)
  377. _LIBCPP_EXPORTED_FROM_ABI const error_category& future_category() _NOEXCEPT;
  378. inline _LIBCPP_HIDE_FROM_ABI error_code make_error_code(future_errc __e) _NOEXCEPT {
  379. return error_code(static_cast<int>(__e), future_category());
  380. }
  381. inline _LIBCPP_HIDE_FROM_ABI error_condition make_error_condition(future_errc __e) _NOEXCEPT {
  382. return error_condition(static_cast<int>(__e), future_category());
  383. }
  384. _LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_future_error(future_errc __ev);
  385. class _LIBCPP_EXPORTED_FROM_ABI future_error : public logic_error {
  386. error_code __ec_;
  387. future_error(error_code);
  388. friend void __throw_future_error(future_errc);
  389. template <class>
  390. friend class promise;
  391. public:
  392. #if _LIBCPP_STD_VER >= 17
  393. _LIBCPP_HIDE_FROM_ABI explicit future_error(future_errc __ec) : future_error(std::make_error_code(__ec)) {}
  394. #endif
  395. _LIBCPP_HIDE_FROM_ABI const error_code& code() const _NOEXCEPT { return __ec_; }
  396. _LIBCPP_HIDE_FROM_ABI future_error(const future_error&) _NOEXCEPT = default;
  397. ~future_error() _NOEXCEPT override;
  398. };
  399. // Declared above std::future_error
  400. void __throw_future_error(future_errc __ev) {
  401. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  402. throw future_error(make_error_code(__ev));
  403. #else
  404. (void)__ev;
  405. _LIBCPP_VERBOSE_ABORT("future_error was thrown in -fno-exceptions mode");
  406. #endif
  407. }
  408. class _LIBCPP_EXPORTED_FROM_ABI __assoc_sub_state : public __shared_count {
  409. protected:
  410. exception_ptr __exception_;
  411. mutable mutex __mut_;
  412. mutable condition_variable __cv_;
  413. unsigned __state_;
  414. void __on_zero_shared() _NOEXCEPT override;
  415. void __sub_wait(unique_lock<mutex>& __lk);
  416. public:
  417. enum { __constructed = 1, __future_attached = 2, ready = 4, deferred = 8 };
  418. _LIBCPP_HIDE_FROM_ABI __assoc_sub_state() : __state_(0) {}
  419. _LIBCPP_HIDE_FROM_ABI bool __has_value() const { return (__state_ & __constructed) || (__exception_ != nullptr); }
  420. _LIBCPP_HIDE_FROM_ABI void __attach_future() {
  421. lock_guard<mutex> __lk(__mut_);
  422. bool __has_future_attached = (__state_ & __future_attached) != 0;
  423. if (__has_future_attached)
  424. __throw_future_error(future_errc::future_already_retrieved);
  425. this->__add_shared();
  426. __state_ |= __future_attached;
  427. }
  428. _LIBCPP_HIDE_FROM_ABI void __set_deferred() { __state_ |= deferred; }
  429. void __make_ready();
  430. _LIBCPP_HIDE_FROM_ABI bool __is_ready() const { return (__state_ & ready) != 0; }
  431. void set_value();
  432. void set_value_at_thread_exit();
  433. void set_exception(exception_ptr __p);
  434. void set_exception_at_thread_exit(exception_ptr __p);
  435. void copy();
  436. void wait();
  437. template <class _Rep, class _Period>
  438. future_status _LIBCPP_HIDE_FROM_ABI wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const;
  439. template <class _Clock, class _Duration>
  440. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS future_status
  441. wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const;
  442. virtual void __execute();
  443. };
  444. template <class _Clock, class _Duration>
  445. future_status __assoc_sub_state::wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
  446. unique_lock<mutex> __lk(__mut_);
  447. if (__state_ & deferred)
  448. return future_status::deferred;
  449. while (!(__state_ & ready) && _Clock::now() < __abs_time)
  450. __cv_.wait_until(__lk, __abs_time);
  451. if (__state_ & ready)
  452. return future_status::ready;
  453. return future_status::timeout;
  454. }
  455. template <class _Rep, class _Period>
  456. inline future_status __assoc_sub_state::wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
  457. return wait_until(chrono::steady_clock::now() + __rel_time);
  458. }
  459. template <class _Rp>
  460. class _LIBCPP_HIDDEN __assoc_state : public __assoc_sub_state {
  461. typedef __assoc_sub_state base;
  462. _LIBCPP_SUPPRESS_DEPRECATED_PUSH
  463. typedef typename aligned_storage<sizeof(_Rp), _LIBCPP_ALIGNOF(_Rp)>::type _Up;
  464. _LIBCPP_SUPPRESS_DEPRECATED_POP
  465. protected:
  466. _Up __value_;
  467. _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
  468. public:
  469. template <class _Arg>
  470. _LIBCPP_HIDE_FROM_ABI void set_value(_Arg&& __arg);
  471. template <class _Arg>
  472. _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Arg&& __arg);
  473. _LIBCPP_HIDE_FROM_ABI _Rp move();
  474. _LIBCPP_HIDE_FROM_ABI __add_lvalue_reference_t<_Rp> copy();
  475. };
  476. template <class _Rp>
  477. void __assoc_state<_Rp>::__on_zero_shared() _NOEXCEPT {
  478. if (this->__state_ & base::__constructed)
  479. reinterpret_cast<_Rp*>(&__value_)->~_Rp();
  480. delete this;
  481. }
  482. template <class _Rp>
  483. template <class _Arg>
  484. void __assoc_state<_Rp>::set_value(_Arg&& __arg) {
  485. unique_lock<mutex> __lk(this->__mut_);
  486. if (this->__has_value())
  487. __throw_future_error(future_errc::promise_already_satisfied);
  488. ::new ((void*)&__value_) _Rp(std::forward<_Arg>(__arg));
  489. this->__state_ |= base::__constructed | base::ready;
  490. __cv_.notify_all();
  491. }
  492. template <class _Rp>
  493. template <class _Arg>
  494. void __assoc_state<_Rp>::set_value_at_thread_exit(_Arg&& __arg) {
  495. unique_lock<mutex> __lk(this->__mut_);
  496. if (this->__has_value())
  497. __throw_future_error(future_errc::promise_already_satisfied);
  498. ::new ((void*)&__value_) _Rp(std::forward<_Arg>(__arg));
  499. this->__state_ |= base::__constructed;
  500. __thread_local_data()->__make_ready_at_thread_exit(this);
  501. }
  502. template <class _Rp>
  503. _Rp __assoc_state<_Rp>::move() {
  504. unique_lock<mutex> __lk(this->__mut_);
  505. this->__sub_wait(__lk);
  506. if (this->__exception_ != nullptr)
  507. std::rethrow_exception(this->__exception_);
  508. return std::move(*reinterpret_cast<_Rp*>(&__value_));
  509. }
  510. template <class _Rp>
  511. __add_lvalue_reference_t<_Rp> __assoc_state<_Rp>::copy() {
  512. unique_lock<mutex> __lk(this->__mut_);
  513. this->__sub_wait(__lk);
  514. if (this->__exception_ != nullptr)
  515. std::rethrow_exception(this->__exception_);
  516. return *reinterpret_cast<_Rp*>(&__value_);
  517. }
  518. template <class _Rp>
  519. class __assoc_state<_Rp&> : public __assoc_sub_state {
  520. typedef __assoc_sub_state base;
  521. typedef _Rp* _Up;
  522. protected:
  523. _Up __value_;
  524. _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
  525. public:
  526. _LIBCPP_HIDE_FROM_ABI void set_value(_Rp& __arg);
  527. _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp& __arg);
  528. _LIBCPP_HIDE_FROM_ABI _Rp& copy();
  529. };
  530. template <class _Rp>
  531. void __assoc_state<_Rp&>::__on_zero_shared() _NOEXCEPT {
  532. delete this;
  533. }
  534. template <class _Rp>
  535. void __assoc_state<_Rp&>::set_value(_Rp& __arg) {
  536. unique_lock<mutex> __lk(this->__mut_);
  537. if (this->__has_value())
  538. __throw_future_error(future_errc::promise_already_satisfied);
  539. __value_ = std::addressof(__arg);
  540. this->__state_ |= base::__constructed | base::ready;
  541. __cv_.notify_all();
  542. }
  543. template <class _Rp>
  544. void __assoc_state<_Rp&>::set_value_at_thread_exit(_Rp& __arg) {
  545. unique_lock<mutex> __lk(this->__mut_);
  546. if (this->__has_value())
  547. __throw_future_error(future_errc::promise_already_satisfied);
  548. __value_ = std::addressof(__arg);
  549. this->__state_ |= base::__constructed;
  550. __thread_local_data()->__make_ready_at_thread_exit(this);
  551. }
  552. template <class _Rp>
  553. _Rp& __assoc_state<_Rp&>::copy() {
  554. unique_lock<mutex> __lk(this->__mut_);
  555. this->__sub_wait(__lk);
  556. if (this->__exception_ != nullptr)
  557. std::rethrow_exception(this->__exception_);
  558. return *__value_;
  559. }
  560. template <class _Rp, class _Alloc>
  561. class __assoc_state_alloc : public __assoc_state<_Rp> {
  562. typedef __assoc_state<_Rp> base;
  563. _Alloc __alloc_;
  564. _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __on_zero_shared() _NOEXCEPT;
  565. public:
  566. _LIBCPP_HIDE_FROM_ABI explicit __assoc_state_alloc(const _Alloc& __a) : __alloc_(__a) {}
  567. };
  568. template <class _Rp, class _Alloc>
  569. void __assoc_state_alloc<_Rp, _Alloc>::__on_zero_shared() _NOEXCEPT {
  570. if (this->__state_ & base::__constructed)
  571. reinterpret_cast<_Rp*>(std::addressof(this->__value_))->~_Rp();
  572. typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
  573. typedef allocator_traits<_Al> _ATraits;
  574. typedef pointer_traits<typename _ATraits::pointer> _PTraits;
  575. _Al __a(__alloc_);
  576. this->~__assoc_state_alloc();
  577. __a.deallocate(_PTraits::pointer_to(*this), 1);
  578. }
  579. template <class _Rp, class _Alloc>
  580. class __assoc_state_alloc<_Rp&, _Alloc> : public __assoc_state<_Rp&> {
  581. typedef __assoc_state<_Rp&> base;
  582. _Alloc __alloc_;
  583. _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __on_zero_shared() _NOEXCEPT;
  584. public:
  585. _LIBCPP_HIDE_FROM_ABI explicit __assoc_state_alloc(const _Alloc& __a) : __alloc_(__a) {}
  586. };
  587. template <class _Rp, class _Alloc>
  588. void __assoc_state_alloc<_Rp&, _Alloc>::__on_zero_shared() _NOEXCEPT {
  589. typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
  590. typedef allocator_traits<_Al> _ATraits;
  591. typedef pointer_traits<typename _ATraits::pointer> _PTraits;
  592. _Al __a(__alloc_);
  593. this->~__assoc_state_alloc();
  594. __a.deallocate(_PTraits::pointer_to(*this), 1);
  595. }
  596. template <class _Alloc>
  597. class __assoc_sub_state_alloc : public __assoc_sub_state {
  598. typedef __assoc_sub_state base;
  599. _Alloc __alloc_;
  600. _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
  601. public:
  602. _LIBCPP_HIDE_FROM_ABI explicit __assoc_sub_state_alloc(const _Alloc& __a) : __alloc_(__a) {}
  603. };
  604. template <class _Alloc>
  605. void __assoc_sub_state_alloc<_Alloc>::__on_zero_shared() _NOEXCEPT {
  606. typedef typename __allocator_traits_rebind<_Alloc, __assoc_sub_state_alloc>::type _Al;
  607. typedef allocator_traits<_Al> _ATraits;
  608. typedef pointer_traits<typename _ATraits::pointer> _PTraits;
  609. _Al __a(__alloc_);
  610. this->~__assoc_sub_state_alloc();
  611. __a.deallocate(_PTraits::pointer_to(*this), 1);
  612. }
  613. template <class _Rp, class _Fp>
  614. class __deferred_assoc_state : public __assoc_state<_Rp> {
  615. typedef __assoc_state<_Rp> base;
  616. _Fp __func_;
  617. public:
  618. _LIBCPP_HIDE_FROM_ABI explicit __deferred_assoc_state(_Fp&& __f);
  619. _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __execute();
  620. };
  621. template <class _Rp, class _Fp>
  622. inline __deferred_assoc_state<_Rp, _Fp>::__deferred_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {
  623. this->__set_deferred();
  624. }
  625. template <class _Rp, class _Fp>
  626. void __deferred_assoc_state<_Rp, _Fp>::__execute() {
  627. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  628. try {
  629. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  630. this->set_value(__func_());
  631. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  632. } catch (...) {
  633. this->set_exception(current_exception());
  634. }
  635. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  636. }
  637. template <class _Fp>
  638. class __deferred_assoc_state<void, _Fp> : public __assoc_sub_state {
  639. typedef __assoc_sub_state base;
  640. _Fp __func_;
  641. public:
  642. _LIBCPP_HIDE_FROM_ABI explicit __deferred_assoc_state(_Fp&& __f);
  643. _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __execute() override;
  644. };
  645. template <class _Fp>
  646. inline __deferred_assoc_state<void, _Fp>::__deferred_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {
  647. this->__set_deferred();
  648. }
  649. template <class _Fp>
  650. void __deferred_assoc_state<void, _Fp>::__execute() {
  651. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  652. try {
  653. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  654. __func_();
  655. this->set_value();
  656. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  657. } catch (...) {
  658. this->set_exception(current_exception());
  659. }
  660. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  661. }
  662. template <class _Rp, class _Fp>
  663. class __async_assoc_state : public __assoc_state<_Rp> {
  664. typedef __assoc_state<_Rp> base;
  665. _Fp __func_;
  666. _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __on_zero_shared() _NOEXCEPT;
  667. public:
  668. _LIBCPP_HIDE_FROM_ABI explicit __async_assoc_state(_Fp&& __f);
  669. _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __execute();
  670. };
  671. template <class _Rp, class _Fp>
  672. inline __async_assoc_state<_Rp, _Fp>::__async_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {}
  673. template <class _Rp, class _Fp>
  674. void __async_assoc_state<_Rp, _Fp>::__execute() {
  675. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  676. try {
  677. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  678. this->set_value(__func_());
  679. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  680. } catch (...) {
  681. this->set_exception(current_exception());
  682. }
  683. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  684. }
  685. template <class _Rp, class _Fp>
  686. void __async_assoc_state<_Rp, _Fp>::__on_zero_shared() _NOEXCEPT {
  687. this->wait();
  688. base::__on_zero_shared();
  689. }
  690. template <class _Fp>
  691. class __async_assoc_state<void, _Fp> : public __assoc_sub_state {
  692. typedef __assoc_sub_state base;
  693. _Fp __func_;
  694. _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
  695. public:
  696. _LIBCPP_HIDE_FROM_ABI explicit __async_assoc_state(_Fp&& __f);
  697. _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __execute() override;
  698. };
  699. template <class _Fp>
  700. inline __async_assoc_state<void, _Fp>::__async_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {}
  701. template <class _Fp>
  702. void __async_assoc_state<void, _Fp>::__execute() {
  703. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  704. try {
  705. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  706. __func_();
  707. this->set_value();
  708. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  709. } catch (...) {
  710. this->set_exception(current_exception());
  711. }
  712. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  713. }
  714. template <class _Fp>
  715. void __async_assoc_state<void, _Fp>::__on_zero_shared() _NOEXCEPT {
  716. this->wait();
  717. base::__on_zero_shared();
  718. }
  719. template <class _Rp>
  720. class _LIBCPP_TEMPLATE_VIS promise;
  721. template <class _Rp>
  722. class _LIBCPP_TEMPLATE_VIS shared_future;
  723. // future
  724. template <class _Rp>
  725. class _LIBCPP_TEMPLATE_VIS future;
  726. template <class _Rp, class _Fp>
  727. _LIBCPP_HIDE_FROM_ABI future<_Rp> __make_deferred_assoc_state(_Fp&& __f);
  728. template <class _Rp, class _Fp>
  729. _LIBCPP_HIDE_FROM_ABI future<_Rp> __make_async_assoc_state(_Fp&& __f);
  730. template <class _Rp>
  731. class _LIBCPP_TEMPLATE_VIS future {
  732. __assoc_state<_Rp>* __state_;
  733. explicit _LIBCPP_HIDE_FROM_ABI future(__assoc_state<_Rp>* __state);
  734. template <class>
  735. friend class promise;
  736. template <class>
  737. friend class shared_future;
  738. template <class _R1, class _Fp>
  739. friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
  740. template <class _R1, class _Fp>
  741. friend future<_R1> __make_async_assoc_state(_Fp&& __f);
  742. public:
  743. _LIBCPP_HIDE_FROM_ABI future() _NOEXCEPT : __state_(nullptr) {}
  744. _LIBCPP_HIDE_FROM_ABI future(future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
  745. future(const future&) = delete;
  746. future& operator=(const future&) = delete;
  747. _LIBCPP_HIDE_FROM_ABI future& operator=(future&& __rhs) _NOEXCEPT {
  748. future(std::move(__rhs)).swap(*this);
  749. return *this;
  750. }
  751. _LIBCPP_HIDE_FROM_ABI ~future();
  752. _LIBCPP_HIDE_FROM_ABI shared_future<_Rp> share() _NOEXCEPT;
  753. // retrieving the value
  754. _LIBCPP_HIDE_FROM_ABI _Rp get();
  755. _LIBCPP_HIDE_FROM_ABI void swap(future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
  756. // functions to check state
  757. _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
  758. _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
  759. template <class _Rep, class _Period>
  760. _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
  761. return __state_->wait_for(__rel_time);
  762. }
  763. template <class _Clock, class _Duration>
  764. _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
  765. return __state_->wait_until(__abs_time);
  766. }
  767. };
  768. template <class _Rp>
  769. future<_Rp>::future(__assoc_state<_Rp>* __state) : __state_(__state) {
  770. __state_->__attach_future();
  771. }
  772. struct __release_shared_count {
  773. _LIBCPP_HIDE_FROM_ABI void operator()(__shared_count* __p) { __p->__release_shared(); }
  774. };
  775. template <class _Rp>
  776. future<_Rp>::~future() {
  777. if (__state_)
  778. __state_->__release_shared();
  779. }
  780. template <class _Rp>
  781. _Rp future<_Rp>::get() {
  782. unique_ptr<__shared_count, __release_shared_count> __guard(__state_);
  783. __assoc_state<_Rp>* __s = __state_;
  784. __state_ = nullptr;
  785. return __s->move();
  786. }
  787. template <class _Rp>
  788. class _LIBCPP_TEMPLATE_VIS future<_Rp&> {
  789. __assoc_state<_Rp&>* __state_;
  790. explicit _LIBCPP_HIDE_FROM_ABI future(__assoc_state<_Rp&>* __state);
  791. template <class>
  792. friend class promise;
  793. template <class>
  794. friend class shared_future;
  795. template <class _R1, class _Fp>
  796. friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
  797. template <class _R1, class _Fp>
  798. friend future<_R1> __make_async_assoc_state(_Fp&& __f);
  799. public:
  800. _LIBCPP_HIDE_FROM_ABI future() _NOEXCEPT : __state_(nullptr) {}
  801. _LIBCPP_HIDE_FROM_ABI future(future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
  802. future(const future&) = delete;
  803. future& operator=(const future&) = delete;
  804. _LIBCPP_HIDE_FROM_ABI future& operator=(future&& __rhs) _NOEXCEPT {
  805. future(std::move(__rhs)).swap(*this);
  806. return *this;
  807. }
  808. _LIBCPP_HIDE_FROM_ABI ~future();
  809. _LIBCPP_HIDE_FROM_ABI shared_future<_Rp&> share() _NOEXCEPT;
  810. // retrieving the value
  811. _LIBCPP_HIDE_FROM_ABI _Rp& get();
  812. _LIBCPP_HIDE_FROM_ABI void swap(future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
  813. // functions to check state
  814. _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
  815. _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
  816. template <class _Rep, class _Period>
  817. _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
  818. return __state_->wait_for(__rel_time);
  819. }
  820. template <class _Clock, class _Duration>
  821. _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
  822. return __state_->wait_until(__abs_time);
  823. }
  824. };
  825. template <class _Rp>
  826. future<_Rp&>::future(__assoc_state<_Rp&>* __state) : __state_(__state) {
  827. __state_->__attach_future();
  828. }
  829. template <class _Rp>
  830. future<_Rp&>::~future() {
  831. if (__state_)
  832. __state_->__release_shared();
  833. }
  834. template <class _Rp>
  835. _Rp& future<_Rp&>::get() {
  836. unique_ptr<__shared_count, __release_shared_count> __guard(__state_);
  837. __assoc_state<_Rp&>* __s = __state_;
  838. __state_ = nullptr;
  839. return __s->copy();
  840. }
  841. template <>
  842. class _LIBCPP_EXPORTED_FROM_ABI future<void> {
  843. __assoc_sub_state* __state_;
  844. explicit future(__assoc_sub_state* __state);
  845. template <class>
  846. friend class promise;
  847. template <class>
  848. friend class shared_future;
  849. template <class _R1, class _Fp>
  850. friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
  851. template <class _R1, class _Fp>
  852. friend future<_R1> __make_async_assoc_state(_Fp&& __f);
  853. public:
  854. _LIBCPP_HIDE_FROM_ABI future() _NOEXCEPT : __state_(nullptr) {}
  855. _LIBCPP_HIDE_FROM_ABI future(future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
  856. future(const future&) = delete;
  857. future& operator=(const future&) = delete;
  858. _LIBCPP_HIDE_FROM_ABI future& operator=(future&& __rhs) _NOEXCEPT {
  859. future(std::move(__rhs)).swap(*this);
  860. return *this;
  861. }
  862. ~future();
  863. _LIBCPP_HIDE_FROM_ABI shared_future<void> share() _NOEXCEPT;
  864. // retrieving the value
  865. void get();
  866. _LIBCPP_HIDE_FROM_ABI void swap(future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
  867. // functions to check state
  868. _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
  869. _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
  870. template <class _Rep, class _Period>
  871. _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
  872. return __state_->wait_for(__rel_time);
  873. }
  874. template <class _Clock, class _Duration>
  875. _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
  876. return __state_->wait_until(__abs_time);
  877. }
  878. };
  879. template <class _Rp>
  880. inline _LIBCPP_HIDE_FROM_ABI void swap(future<_Rp>& __x, future<_Rp>& __y) _NOEXCEPT {
  881. __x.swap(__y);
  882. }
  883. // promise<R>
  884. template <class _Callable>
  885. class packaged_task;
  886. template <class _Rp>
  887. class _LIBCPP_TEMPLATE_VIS promise {
  888. __assoc_state<_Rp>* __state_;
  889. _LIBCPP_HIDE_FROM_ABI explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
  890. template <class>
  891. friend class packaged_task;
  892. public:
  893. _LIBCPP_HIDE_FROM_ABI promise();
  894. template <class _Alloc>
  895. _LIBCPP_HIDE_FROM_ABI promise(allocator_arg_t, const _Alloc& __a);
  896. _LIBCPP_HIDE_FROM_ABI promise(promise&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
  897. promise(const promise& __rhs) = delete;
  898. _LIBCPP_HIDE_FROM_ABI ~promise();
  899. // assignment
  900. _LIBCPP_HIDE_FROM_ABI promise& operator=(promise&& __rhs) _NOEXCEPT {
  901. promise(std::move(__rhs)).swap(*this);
  902. return *this;
  903. }
  904. promise& operator=(const promise& __rhs) = delete;
  905. _LIBCPP_HIDE_FROM_ABI void swap(promise& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
  906. // retrieving the result
  907. _LIBCPP_HIDE_FROM_ABI future<_Rp> get_future();
  908. // setting the result
  909. _LIBCPP_HIDE_FROM_ABI void set_value(const _Rp& __r);
  910. _LIBCPP_HIDE_FROM_ABI void set_value(_Rp&& __r);
  911. _LIBCPP_HIDE_FROM_ABI void set_exception(exception_ptr __p);
  912. // setting the result with deferred notification
  913. _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(const _Rp& __r);
  914. _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp&& __r);
  915. _LIBCPP_HIDE_FROM_ABI void set_exception_at_thread_exit(exception_ptr __p);
  916. };
  917. template <class _Rp>
  918. promise<_Rp>::promise() : __state_(new __assoc_state<_Rp>) {}
  919. template <class _Rp>
  920. template <class _Alloc>
  921. promise<_Rp>::promise(allocator_arg_t, const _Alloc& __a0) {
  922. typedef __assoc_state_alloc<_Rp, _Alloc> _State;
  923. typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
  924. typedef __allocator_destructor<_A2> _D2;
  925. _A2 __a(__a0);
  926. unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
  927. ::new ((void*)std::addressof(*__hold.get())) _State(__a0);
  928. __state_ = std::addressof(*__hold.release());
  929. }
  930. template <class _Rp>
  931. promise<_Rp>::~promise() {
  932. if (__state_) {
  933. if (!__state_->__has_value() && __state_->use_count() > 1)
  934. __state_->set_exception(make_exception_ptr(future_error(make_error_code(future_errc::broken_promise))));
  935. __state_->__release_shared();
  936. }
  937. }
  938. template <class _Rp>
  939. future<_Rp> promise<_Rp>::get_future() {
  940. if (__state_ == nullptr)
  941. __throw_future_error(future_errc::no_state);
  942. return future<_Rp>(__state_);
  943. }
  944. template <class _Rp>
  945. void promise<_Rp>::set_value(const _Rp& __r) {
  946. if (__state_ == nullptr)
  947. __throw_future_error(future_errc::no_state);
  948. __state_->set_value(__r);
  949. }
  950. template <class _Rp>
  951. void promise<_Rp>::set_value(_Rp&& __r) {
  952. if (__state_ == nullptr)
  953. __throw_future_error(future_errc::no_state);
  954. __state_->set_value(std::move(__r));
  955. }
  956. template <class _Rp>
  957. void promise<_Rp>::set_exception(exception_ptr __p) {
  958. _LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception: received nullptr");
  959. if (__state_ == nullptr)
  960. __throw_future_error(future_errc::no_state);
  961. __state_->set_exception(__p);
  962. }
  963. template <class _Rp>
  964. void promise<_Rp>::set_value_at_thread_exit(const _Rp& __r) {
  965. if (__state_ == nullptr)
  966. __throw_future_error(future_errc::no_state);
  967. __state_->set_value_at_thread_exit(__r);
  968. }
  969. template <class _Rp>
  970. void promise<_Rp>::set_value_at_thread_exit(_Rp&& __r) {
  971. if (__state_ == nullptr)
  972. __throw_future_error(future_errc::no_state);
  973. __state_->set_value_at_thread_exit(std::move(__r));
  974. }
  975. template <class _Rp>
  976. void promise<_Rp>::set_exception_at_thread_exit(exception_ptr __p) {
  977. _LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception_at_thread_exit: received nullptr");
  978. if (__state_ == nullptr)
  979. __throw_future_error(future_errc::no_state);
  980. __state_->set_exception_at_thread_exit(__p);
  981. }
  982. // promise<R&>
  983. template <class _Rp>
  984. class _LIBCPP_TEMPLATE_VIS promise<_Rp&> {
  985. __assoc_state<_Rp&>* __state_;
  986. _LIBCPP_HIDE_FROM_ABI explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
  987. template <class>
  988. friend class packaged_task;
  989. public:
  990. _LIBCPP_HIDE_FROM_ABI promise();
  991. template <class _Allocator>
  992. _LIBCPP_HIDE_FROM_ABI promise(allocator_arg_t, const _Allocator& __a);
  993. _LIBCPP_HIDE_FROM_ABI promise(promise&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
  994. promise(const promise& __rhs) = delete;
  995. _LIBCPP_HIDE_FROM_ABI ~promise();
  996. // assignment
  997. _LIBCPP_HIDE_FROM_ABI promise& operator=(promise&& __rhs) _NOEXCEPT {
  998. promise(std::move(__rhs)).swap(*this);
  999. return *this;
  1000. }
  1001. promise& operator=(const promise& __rhs) = delete;
  1002. _LIBCPP_HIDE_FROM_ABI void swap(promise& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
  1003. // retrieving the result
  1004. _LIBCPP_HIDE_FROM_ABI future<_Rp&> get_future();
  1005. // setting the result
  1006. _LIBCPP_HIDE_FROM_ABI void set_value(_Rp& __r);
  1007. _LIBCPP_HIDE_FROM_ABI void set_exception(exception_ptr __p);
  1008. // setting the result with deferred notification
  1009. _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp&);
  1010. _LIBCPP_HIDE_FROM_ABI void set_exception_at_thread_exit(exception_ptr __p);
  1011. };
  1012. template <class _Rp>
  1013. promise<_Rp&>::promise() : __state_(new __assoc_state<_Rp&>) {}
  1014. template <class _Rp>
  1015. template <class _Alloc>
  1016. promise<_Rp&>::promise(allocator_arg_t, const _Alloc& __a0) {
  1017. typedef __assoc_state_alloc<_Rp&, _Alloc> _State;
  1018. typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
  1019. typedef __allocator_destructor<_A2> _D2;
  1020. _A2 __a(__a0);
  1021. unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
  1022. ::new ((void*)std::addressof(*__hold.get())) _State(__a0);
  1023. __state_ = std::addressof(*__hold.release());
  1024. }
  1025. template <class _Rp>
  1026. promise<_Rp&>::~promise() {
  1027. if (__state_) {
  1028. if (!__state_->__has_value() && __state_->use_count() > 1)
  1029. __state_->set_exception(make_exception_ptr(future_error(make_error_code(future_errc::broken_promise))));
  1030. __state_->__release_shared();
  1031. }
  1032. }
  1033. template <class _Rp>
  1034. future<_Rp&> promise<_Rp&>::get_future() {
  1035. if (__state_ == nullptr)
  1036. __throw_future_error(future_errc::no_state);
  1037. return future<_Rp&>(__state_);
  1038. }
  1039. template <class _Rp>
  1040. void promise<_Rp&>::set_value(_Rp& __r) {
  1041. if (__state_ == nullptr)
  1042. __throw_future_error(future_errc::no_state);
  1043. __state_->set_value(__r);
  1044. }
  1045. template <class _Rp>
  1046. void promise<_Rp&>::set_exception(exception_ptr __p) {
  1047. _LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception: received nullptr");
  1048. if (__state_ == nullptr)
  1049. __throw_future_error(future_errc::no_state);
  1050. __state_->set_exception(__p);
  1051. }
  1052. template <class _Rp>
  1053. void promise<_Rp&>::set_value_at_thread_exit(_Rp& __r) {
  1054. if (__state_ == nullptr)
  1055. __throw_future_error(future_errc::no_state);
  1056. __state_->set_value_at_thread_exit(__r);
  1057. }
  1058. template <class _Rp>
  1059. void promise<_Rp&>::set_exception_at_thread_exit(exception_ptr __p) {
  1060. _LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception_at_thread_exit: received nullptr");
  1061. if (__state_ == nullptr)
  1062. __throw_future_error(future_errc::no_state);
  1063. __state_->set_exception_at_thread_exit(__p);
  1064. }
  1065. // promise<void>
  1066. template <>
  1067. class _LIBCPP_EXPORTED_FROM_ABI promise<void> {
  1068. __assoc_sub_state* __state_;
  1069. _LIBCPP_HIDE_FROM_ABI explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
  1070. template <class>
  1071. friend class packaged_task;
  1072. public:
  1073. promise();
  1074. template <class _Allocator>
  1075. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS promise(allocator_arg_t, const _Allocator& __a);
  1076. _LIBCPP_HIDE_FROM_ABI promise(promise&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
  1077. promise(const promise& __rhs) = delete;
  1078. ~promise();
  1079. // assignment
  1080. _LIBCPP_HIDE_FROM_ABI promise& operator=(promise&& __rhs) _NOEXCEPT {
  1081. promise(std::move(__rhs)).swap(*this);
  1082. return *this;
  1083. }
  1084. promise& operator=(const promise& __rhs) = delete;
  1085. _LIBCPP_HIDE_FROM_ABI void swap(promise& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
  1086. // retrieving the result
  1087. future<void> get_future();
  1088. // setting the result
  1089. void set_value();
  1090. void set_exception(exception_ptr __p);
  1091. // setting the result with deferred notification
  1092. void set_value_at_thread_exit();
  1093. void set_exception_at_thread_exit(exception_ptr __p);
  1094. };
  1095. template <class _Alloc>
  1096. promise<void>::promise(allocator_arg_t, const _Alloc& __a0) {
  1097. typedef __assoc_sub_state_alloc<_Alloc> _State;
  1098. typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
  1099. typedef __allocator_destructor<_A2> _D2;
  1100. _A2 __a(__a0);
  1101. unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
  1102. ::new ((void*)std::addressof(*__hold.get())) _State(__a0);
  1103. __state_ = std::addressof(*__hold.release());
  1104. }
  1105. template <class _Rp>
  1106. inline _LIBCPP_HIDE_FROM_ABI void swap(promise<_Rp>& __x, promise<_Rp>& __y) _NOEXCEPT {
  1107. __x.swap(__y);
  1108. }
  1109. template <class _Rp, class _Alloc>
  1110. struct _LIBCPP_TEMPLATE_VIS uses_allocator<promise<_Rp>, _Alloc> : public true_type {};
  1111. // packaged_task
  1112. template <class _Fp>
  1113. class __packaged_task_base;
  1114. template <class _Rp, class... _ArgTypes>
  1115. class __packaged_task_base<_Rp(_ArgTypes...)> {
  1116. __packaged_task_base(const __packaged_task_base&);
  1117. __packaged_task_base& operator=(const __packaged_task_base&);
  1118. public:
  1119. _LIBCPP_HIDE_FROM_ABI __packaged_task_base() {}
  1120. _LIBCPP_HIDE_FROM_ABI_VIRTUAL
  1121. virtual ~__packaged_task_base() {}
  1122. virtual void __move_to(__packaged_task_base*) _NOEXCEPT = 0;
  1123. virtual void destroy() = 0;
  1124. virtual void destroy_deallocate() = 0;
  1125. virtual _Rp operator()(_ArgTypes&&...) = 0;
  1126. };
  1127. template <class _FD, class _Alloc, class _FB>
  1128. class __packaged_task_func;
  1129. template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
  1130. class __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)> : public __packaged_task_base<_Rp(_ArgTypes...)> {
  1131. __compressed_pair<_Fp, _Alloc> __f_;
  1132. public:
  1133. _LIBCPP_HIDE_FROM_ABI explicit __packaged_task_func(const _Fp& __f) : __f_(__f, __default_init_tag()) {}
  1134. _LIBCPP_HIDE_FROM_ABI explicit __packaged_task_func(_Fp&& __f) : __f_(std::move(__f), __default_init_tag()) {}
  1135. _LIBCPP_HIDE_FROM_ABI __packaged_task_func(const _Fp& __f, const _Alloc& __a) : __f_(__f, __a) {}
  1136. _LIBCPP_HIDE_FROM_ABI __packaged_task_func(_Fp&& __f, const _Alloc& __a) : __f_(std::move(__f), __a) {}
  1137. _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __move_to(__packaged_task_base<_Rp(_ArgTypes...)>*) _NOEXCEPT;
  1138. _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy();
  1139. _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy_deallocate();
  1140. _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual _Rp operator()(_ArgTypes&&... __args);
  1141. };
  1142. template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
  1143. void __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__move_to(
  1144. __packaged_task_base<_Rp(_ArgTypes...)>* __p) _NOEXCEPT {
  1145. ::new ((void*)__p) __packaged_task_func(std::move(__f_.first()), std::move(__f_.second()));
  1146. }
  1147. template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
  1148. void __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() {
  1149. __f_.~__compressed_pair<_Fp, _Alloc>();
  1150. }
  1151. template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
  1152. void __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() {
  1153. typedef typename __allocator_traits_rebind<_Alloc, __packaged_task_func>::type _Ap;
  1154. typedef allocator_traits<_Ap> _ATraits;
  1155. typedef pointer_traits<typename _ATraits::pointer> _PTraits;
  1156. _Ap __a(__f_.second());
  1157. __f_.~__compressed_pair<_Fp, _Alloc>();
  1158. __a.deallocate(_PTraits::pointer_to(*this), 1);
  1159. }
  1160. template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
  1161. _Rp __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&&... __arg) {
  1162. return std::__invoke(__f_.first(), std::forward<_ArgTypes>(__arg)...);
  1163. }
  1164. template <class _Callable>
  1165. class __packaged_task_function;
  1166. template <class _Rp, class... _ArgTypes>
  1167. class __packaged_task_function<_Rp(_ArgTypes...)> {
  1168. typedef __packaged_task_base<_Rp(_ArgTypes...)> __base;
  1169. _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_CFI __base* __get_buf() { return (__base*)&__buf_; }
  1170. _LIBCPP_SUPPRESS_DEPRECATED_PUSH
  1171. typename aligned_storage<3 * sizeof(void*)>::type __buf_;
  1172. _LIBCPP_SUPPRESS_DEPRECATED_POP
  1173. __base* __f_;
  1174. public:
  1175. typedef _Rp result_type;
  1176. // construct/copy/destroy:
  1177. _LIBCPP_HIDE_FROM_ABI __packaged_task_function() _NOEXCEPT : __f_(nullptr) {}
  1178. template <class _Fp>
  1179. _LIBCPP_HIDE_FROM_ABI __packaged_task_function(_Fp&& __f);
  1180. template <class _Fp, class _Alloc>
  1181. _LIBCPP_HIDE_FROM_ABI __packaged_task_function(allocator_arg_t, const _Alloc& __a, _Fp&& __f);
  1182. _LIBCPP_HIDE_FROM_ABI __packaged_task_function(__packaged_task_function&&) _NOEXCEPT;
  1183. _LIBCPP_HIDE_FROM_ABI __packaged_task_function& operator=(__packaged_task_function&&) _NOEXCEPT;
  1184. __packaged_task_function(const __packaged_task_function&) = delete;
  1185. __packaged_task_function& operator=(const __packaged_task_function&) = delete;
  1186. _LIBCPP_HIDE_FROM_ABI ~__packaged_task_function();
  1187. _LIBCPP_HIDE_FROM_ABI void swap(__packaged_task_function&) _NOEXCEPT;
  1188. _LIBCPP_HIDE_FROM_ABI _LIBCPP_HIDE_FROM_ABI _Rp operator()(_ArgTypes...) const;
  1189. };
  1190. template <class _Rp, class... _ArgTypes>
  1191. __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f) _NOEXCEPT {
  1192. if (__f.__f_ == nullptr)
  1193. __f_ = nullptr;
  1194. else if (__f.__f_ == __f.__get_buf()) {
  1195. __f.__f_->__move_to(__get_buf());
  1196. __f_ = (__base*)&__buf_;
  1197. } else {
  1198. __f_ = __f.__f_;
  1199. __f.__f_ = nullptr;
  1200. }
  1201. }
  1202. template <class _Rp, class... _ArgTypes>
  1203. template <class _Fp>
  1204. __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(_Fp&& __f) : __f_(nullptr) {
  1205. typedef __libcpp_remove_reference_t<__decay_t<_Fp> > _FR;
  1206. typedef __packaged_task_func<_FR, allocator<_FR>, _Rp(_ArgTypes...)> _FF;
  1207. if (sizeof(_FF) <= sizeof(__buf_)) {
  1208. ::new ((void*)&__buf_) _FF(std::forward<_Fp>(__f));
  1209. __f_ = (__base*)&__buf_;
  1210. } else {
  1211. typedef allocator<_FF> _Ap;
  1212. _Ap __a;
  1213. typedef __allocator_destructor<_Ap> _Dp;
  1214. unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
  1215. ::new ((void*)__hold.get()) _FF(std::forward<_Fp>(__f), allocator<_FR>(__a));
  1216. __f_ = __hold.release();
  1217. }
  1218. }
  1219. template <class _Rp, class... _ArgTypes>
  1220. template <class _Fp, class _Alloc>
  1221. __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(allocator_arg_t, const _Alloc& __a0, _Fp&& __f)
  1222. : __f_(nullptr) {
  1223. typedef __libcpp_remove_reference_t<__decay_t<_Fp> > _FR;
  1224. typedef __packaged_task_func<_FR, _Alloc, _Rp(_ArgTypes...)> _FF;
  1225. if (sizeof(_FF) <= sizeof(__buf_)) {
  1226. __f_ = (__base*)&__buf_;
  1227. ::new ((void*)__f_) _FF(std::forward<_Fp>(__f));
  1228. } else {
  1229. typedef typename __allocator_traits_rebind<_Alloc, _FF>::type _Ap;
  1230. _Ap __a(__a0);
  1231. typedef __allocator_destructor<_Ap> _Dp;
  1232. unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
  1233. ::new ((void*)std::addressof(*__hold.get())) _FF(std::forward<_Fp>(__f), _Alloc(__a));
  1234. __f_ = std::addressof(*__hold.release());
  1235. }
  1236. }
  1237. template <class _Rp, class... _ArgTypes>
  1238. __packaged_task_function<_Rp(_ArgTypes...)>&
  1239. __packaged_task_function<_Rp(_ArgTypes...)>::operator=(__packaged_task_function&& __f) _NOEXCEPT {
  1240. if (__f_ == __get_buf())
  1241. __f_->destroy();
  1242. else if (__f_)
  1243. __f_->destroy_deallocate();
  1244. __f_ = nullptr;
  1245. if (__f.__f_ == nullptr)
  1246. __f_ = nullptr;
  1247. else if (__f.__f_ == __f.__get_buf()) {
  1248. __f.__f_->__move_to(__get_buf());
  1249. __f_ = __get_buf();
  1250. } else {
  1251. __f_ = __f.__f_;
  1252. __f.__f_ = nullptr;
  1253. }
  1254. return *this;
  1255. }
  1256. template <class _Rp, class... _ArgTypes>
  1257. __packaged_task_function<_Rp(_ArgTypes...)>::~__packaged_task_function() {
  1258. if (__f_ == __get_buf())
  1259. __f_->destroy();
  1260. else if (__f_)
  1261. __f_->destroy_deallocate();
  1262. }
  1263. template <class _Rp, class... _ArgTypes>
  1264. _LIBCPP_NO_CFI void __packaged_task_function<_Rp(_ArgTypes...)>::swap(__packaged_task_function& __f) _NOEXCEPT {
  1265. if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_) {
  1266. _LIBCPP_SUPPRESS_DEPRECATED_PUSH
  1267. typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
  1268. _LIBCPP_SUPPRESS_DEPRECATED_POP
  1269. __base* __t = (__base*)&__tempbuf;
  1270. __f_->__move_to(__t);
  1271. __f_->destroy();
  1272. __f_ = nullptr;
  1273. __f.__f_->__move_to((__base*)&__buf_);
  1274. __f.__f_->destroy();
  1275. __f.__f_ = nullptr;
  1276. __f_ = (__base*)&__buf_;
  1277. __t->__move_to((__base*)&__f.__buf_);
  1278. __t->destroy();
  1279. __f.__f_ = (__base*)&__f.__buf_;
  1280. } else if (__f_ == (__base*)&__buf_) {
  1281. __f_->__move_to((__base*)&__f.__buf_);
  1282. __f_->destroy();
  1283. __f_ = __f.__f_;
  1284. __f.__f_ = (__base*)&__f.__buf_;
  1285. } else if (__f.__f_ == (__base*)&__f.__buf_) {
  1286. __f.__f_->__move_to((__base*)&__buf_);
  1287. __f.__f_->destroy();
  1288. __f.__f_ = __f_;
  1289. __f_ = (__base*)&__buf_;
  1290. } else
  1291. std::swap(__f_, __f.__f_);
  1292. }
  1293. template <class _Rp, class... _ArgTypes>
  1294. inline _Rp __packaged_task_function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const {
  1295. return (*__f_)(std::forward<_ArgTypes>(__arg)...);
  1296. }
  1297. template <class _Rp, class... _ArgTypes>
  1298. class _LIBCPP_TEMPLATE_VIS packaged_task<_Rp(_ArgTypes...)> {
  1299. public:
  1300. typedef _Rp result_type; // extension
  1301. private:
  1302. __packaged_task_function<result_type(_ArgTypes...)> __f_;
  1303. promise<result_type> __p_;
  1304. public:
  1305. // construction and destruction
  1306. _LIBCPP_HIDE_FROM_ABI packaged_task() _NOEXCEPT : __p_(nullptr) {}
  1307. template <class _Fp, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value, int> = 0>
  1308. _LIBCPP_HIDE_FROM_ABI explicit packaged_task(_Fp&& __f) : __f_(std::forward<_Fp>(__f)) {}
  1309. template <class _Fp, class _Allocator, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value, int> = 0>
  1310. _LIBCPP_HIDE_FROM_ABI packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
  1311. : __f_(allocator_arg_t(), __a, std::forward<_Fp>(__f)), __p_(allocator_arg_t(), __a) {}
  1312. // ~packaged_task() = default;
  1313. // no copy
  1314. packaged_task(const packaged_task&) = delete;
  1315. packaged_task& operator=(const packaged_task&) = delete;
  1316. // move support
  1317. _LIBCPP_HIDE_FROM_ABI packaged_task(packaged_task&& __other) _NOEXCEPT
  1318. : __f_(std::move(__other.__f_)),
  1319. __p_(std::move(__other.__p_)) {}
  1320. _LIBCPP_HIDE_FROM_ABI packaged_task& operator=(packaged_task&& __other) _NOEXCEPT {
  1321. __f_ = std::move(__other.__f_);
  1322. __p_ = std::move(__other.__p_);
  1323. return *this;
  1324. }
  1325. _LIBCPP_HIDE_FROM_ABI void swap(packaged_task& __other) _NOEXCEPT {
  1326. __f_.swap(__other.__f_);
  1327. __p_.swap(__other.__p_);
  1328. }
  1329. _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __p_.__state_ != nullptr; }
  1330. // result retrieval
  1331. _LIBCPP_HIDE_FROM_ABI future<result_type> get_future() { return __p_.get_future(); }
  1332. // execution
  1333. _LIBCPP_HIDE_FROM_ABI void operator()(_ArgTypes... __args);
  1334. _LIBCPP_HIDE_FROM_ABI void make_ready_at_thread_exit(_ArgTypes... __args);
  1335. _LIBCPP_HIDE_FROM_ABI void reset();
  1336. };
  1337. template <class _Rp, class... _ArgTypes>
  1338. void packaged_task<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __args) {
  1339. if (__p_.__state_ == nullptr)
  1340. __throw_future_error(future_errc::no_state);
  1341. if (__p_.__state_->__has_value())
  1342. __throw_future_error(future_errc::promise_already_satisfied);
  1343. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  1344. try {
  1345. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  1346. __p_.set_value(__f_(std::forward<_ArgTypes>(__args)...));
  1347. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  1348. } catch (...) {
  1349. __p_.set_exception(current_exception());
  1350. }
  1351. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  1352. }
  1353. template <class _Rp, class... _ArgTypes>
  1354. void packaged_task<_Rp(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args) {
  1355. if (__p_.__state_ == nullptr)
  1356. __throw_future_error(future_errc::no_state);
  1357. if (__p_.__state_->__has_value())
  1358. __throw_future_error(future_errc::promise_already_satisfied);
  1359. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  1360. try {
  1361. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  1362. __p_.set_value_at_thread_exit(__f_(std::forward<_ArgTypes>(__args)...));
  1363. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  1364. } catch (...) {
  1365. __p_.set_exception_at_thread_exit(current_exception());
  1366. }
  1367. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  1368. }
  1369. template <class _Rp, class... _ArgTypes>
  1370. void packaged_task<_Rp(_ArgTypes...)>::reset() {
  1371. if (!valid())
  1372. __throw_future_error(future_errc::no_state);
  1373. __p_ = promise<result_type>();
  1374. }
  1375. template <class... _ArgTypes>
  1376. class _LIBCPP_TEMPLATE_VIS packaged_task<void(_ArgTypes...)> {
  1377. public:
  1378. typedef void result_type; // extension
  1379. private:
  1380. __packaged_task_function<result_type(_ArgTypes...)> __f_;
  1381. promise<result_type> __p_;
  1382. public:
  1383. // construction and destruction
  1384. _LIBCPP_HIDE_FROM_ABI packaged_task() _NOEXCEPT : __p_(nullptr) {}
  1385. template <class _Fp, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value, int> = 0>
  1386. _LIBCPP_HIDE_FROM_ABI explicit packaged_task(_Fp&& __f) : __f_(std::forward<_Fp>(__f)) {}
  1387. template <class _Fp, class _Allocator, __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value, int> = 0>
  1388. _LIBCPP_HIDE_FROM_ABI packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
  1389. : __f_(allocator_arg_t(), __a, std::forward<_Fp>(__f)), __p_(allocator_arg_t(), __a) {}
  1390. // ~packaged_task() = default;
  1391. // no copy
  1392. packaged_task(const packaged_task&) = delete;
  1393. packaged_task& operator=(const packaged_task&) = delete;
  1394. // move support
  1395. _LIBCPP_HIDE_FROM_ABI packaged_task(packaged_task&& __other) _NOEXCEPT
  1396. : __f_(std::move(__other.__f_)),
  1397. __p_(std::move(__other.__p_)) {}
  1398. _LIBCPP_HIDE_FROM_ABI packaged_task& operator=(packaged_task&& __other) _NOEXCEPT {
  1399. __f_ = std::move(__other.__f_);
  1400. __p_ = std::move(__other.__p_);
  1401. return *this;
  1402. }
  1403. _LIBCPP_HIDE_FROM_ABI void swap(packaged_task& __other) _NOEXCEPT {
  1404. __f_.swap(__other.__f_);
  1405. __p_.swap(__other.__p_);
  1406. }
  1407. _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __p_.__state_ != nullptr; }
  1408. // result retrieval
  1409. _LIBCPP_HIDE_FROM_ABI future<result_type> get_future() { return __p_.get_future(); }
  1410. // execution
  1411. _LIBCPP_HIDE_FROM_ABI void operator()(_ArgTypes... __args);
  1412. _LIBCPP_HIDE_FROM_ABI void make_ready_at_thread_exit(_ArgTypes... __args);
  1413. _LIBCPP_HIDE_FROM_ABI void reset();
  1414. };
  1415. #if _LIBCPP_STD_VER >= 17
  1416. template <class _Rp, class... _Args>
  1417. packaged_task(_Rp (*)(_Args...)) -> packaged_task<_Rp(_Args...)>;
  1418. template <class _Fp, class _Stripped = typename __strip_signature<decltype(&_Fp::operator())>::type>
  1419. packaged_task(_Fp) -> packaged_task<_Stripped>;
  1420. #endif
  1421. template <class... _ArgTypes>
  1422. void packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args) {
  1423. if (__p_.__state_ == nullptr)
  1424. __throw_future_error(future_errc::no_state);
  1425. if (__p_.__state_->__has_value())
  1426. __throw_future_error(future_errc::promise_already_satisfied);
  1427. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  1428. try {
  1429. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  1430. __f_(std::forward<_ArgTypes>(__args)...);
  1431. __p_.set_value();
  1432. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  1433. } catch (...) {
  1434. __p_.set_exception(current_exception());
  1435. }
  1436. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  1437. }
  1438. template <class... _ArgTypes>
  1439. void packaged_task<void(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args) {
  1440. if (__p_.__state_ == nullptr)
  1441. __throw_future_error(future_errc::no_state);
  1442. if (__p_.__state_->__has_value())
  1443. __throw_future_error(future_errc::promise_already_satisfied);
  1444. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  1445. try {
  1446. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  1447. __f_(std::forward<_ArgTypes>(__args)...);
  1448. __p_.set_value_at_thread_exit();
  1449. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  1450. } catch (...) {
  1451. __p_.set_exception_at_thread_exit(current_exception());
  1452. }
  1453. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  1454. }
  1455. template <class... _ArgTypes>
  1456. void packaged_task<void(_ArgTypes...)>::reset() {
  1457. if (!valid())
  1458. __throw_future_error(future_errc::no_state);
  1459. __p_ = promise<result_type>();
  1460. }
  1461. template <class _Rp, class... _ArgTypes>
  1462. inline _LIBCPP_HIDE_FROM_ABI void
  1463. swap(packaged_task<_Rp(_ArgTypes...)>& __x, packaged_task<_Rp(_ArgTypes...)>& __y) _NOEXCEPT {
  1464. __x.swap(__y);
  1465. }
  1466. template <class _Callable, class _Alloc>
  1467. struct _LIBCPP_TEMPLATE_VIS uses_allocator<packaged_task<_Callable>, _Alloc> : public true_type {};
  1468. template <class _Rp, class _Fp>
  1469. _LIBCPP_HIDE_FROM_ABI future<_Rp> __make_deferred_assoc_state(_Fp&& __f) {
  1470. unique_ptr<__deferred_assoc_state<_Rp, _Fp>, __release_shared_count> __h(
  1471. new __deferred_assoc_state<_Rp, _Fp>(std::forward<_Fp>(__f)));
  1472. return future<_Rp>(__h.get());
  1473. }
  1474. template <class _Rp, class _Fp>
  1475. _LIBCPP_HIDE_FROM_ABI future<_Rp> __make_async_assoc_state(_Fp&& __f) {
  1476. unique_ptr<__async_assoc_state<_Rp, _Fp>, __release_shared_count> __h(
  1477. new __async_assoc_state<_Rp, _Fp>(std::forward<_Fp>(__f)));
  1478. std::thread(&__async_assoc_state<_Rp, _Fp>::__execute, __h.get()).detach();
  1479. return future<_Rp>(__h.get());
  1480. }
  1481. #ifndef _LIBCPP_CXX03_LANG
  1482. template <class _Fp, class... _Args>
  1483. class _LIBCPP_HIDDEN __async_func {
  1484. tuple<_Fp, _Args...> __f_;
  1485. public:
  1486. typedef typename __invoke_of<_Fp, _Args...>::type _Rp;
  1487. _LIBCPP_HIDE_FROM_ABI explicit __async_func(_Fp&& __f, _Args&&... __args)
  1488. : __f_(std::move(__f), std::move(__args)...) {}
  1489. _LIBCPP_HIDE_FROM_ABI __async_func(__async_func&& __f) : __f_(std::move(__f.__f_)) {}
  1490. _LIBCPP_HIDE_FROM_ABI _Rp operator()() {
  1491. typedef typename __make_tuple_indices<1 + sizeof...(_Args), 1>::type _Index;
  1492. return __execute(_Index());
  1493. }
  1494. private:
  1495. template <size_t... _Indices>
  1496. _LIBCPP_HIDE_FROM_ABI _Rp __execute(__tuple_indices<_Indices...>) {
  1497. return std::__invoke(std::move(std::get<0>(__f_)), std::move(std::get<_Indices>(__f_))...);
  1498. }
  1499. };
  1500. inline _LIBCPP_HIDE_FROM_ABI bool __does_policy_contain(launch __policy, launch __value) {
  1501. return (int(__policy) & int(__value)) != 0;
  1502. }
  1503. template <class _Fp, class... _Args>
  1504. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
  1505. future<typename __invoke_of<__decay_t<_Fp>, __decay_t<_Args>...>::type>
  1506. async(launch __policy, _Fp&& __f, _Args&&... __args) {
  1507. typedef __async_func<__decay_t<_Fp>, __decay_t<_Args>...> _BF;
  1508. typedef typename _BF::_Rp _Rp;
  1509. # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  1510. try {
  1511. # endif
  1512. if (__does_policy_contain(__policy, launch::async))
  1513. return std::__make_async_assoc_state<_Rp>(
  1514. _BF(_LIBCPP_AUTO_CAST(std::forward<_Fp>(__f)), _LIBCPP_AUTO_CAST(std::forward<_Args>(__args))...));
  1515. # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  1516. } catch (...) {
  1517. if (__policy == launch::async)
  1518. throw;
  1519. }
  1520. # endif
  1521. if (__does_policy_contain(__policy, launch::deferred))
  1522. return std::__make_deferred_assoc_state<_Rp>(
  1523. _BF(_LIBCPP_AUTO_CAST(std::forward<_Fp>(__f)), _LIBCPP_AUTO_CAST(std::forward<_Args>(__args))...));
  1524. return future<_Rp>{};
  1525. }
  1526. template <class _Fp, class... _Args>
  1527. _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_HIDE_FROM_ABI
  1528. future<typename __invoke_of<__decay_t<_Fp>, __decay_t<_Args>...>::type>
  1529. async(_Fp&& __f, _Args&&... __args) {
  1530. return std::async(launch::any, std::forward<_Fp>(__f), std::forward<_Args>(__args)...);
  1531. }
  1532. #endif // C++03
  1533. // shared_future
  1534. template <class _Rp>
  1535. class _LIBCPP_TEMPLATE_VIS shared_future {
  1536. __assoc_state<_Rp>* __state_;
  1537. public:
  1538. _LIBCPP_HIDE_FROM_ABI shared_future() _NOEXCEPT : __state_(nullptr) {}
  1539. _LIBCPP_HIDE_FROM_ABI shared_future(const shared_future& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {
  1540. if (__state_)
  1541. __state_->__add_shared();
  1542. }
  1543. _LIBCPP_HIDE_FROM_ABI shared_future(future<_Rp>&& __f) _NOEXCEPT : __state_(__f.__state_) { __f.__state_ = nullptr; }
  1544. _LIBCPP_HIDE_FROM_ABI shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {
  1545. __rhs.__state_ = nullptr;
  1546. }
  1547. _LIBCPP_HIDE_FROM_ABI ~shared_future();
  1548. _LIBCPP_HIDE_FROM_ABI shared_future& operator=(const shared_future& __rhs) _NOEXCEPT;
  1549. _LIBCPP_HIDE_FROM_ABI shared_future& operator=(shared_future&& __rhs) _NOEXCEPT {
  1550. shared_future(std::move(__rhs)).swap(*this);
  1551. return *this;
  1552. }
  1553. // retrieving the value
  1554. _LIBCPP_HIDE_FROM_ABI const _Rp& get() const { return __state_->copy(); }
  1555. _LIBCPP_HIDE_FROM_ABI void swap(shared_future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
  1556. // functions to check state
  1557. _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
  1558. _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
  1559. template <class _Rep, class _Period>
  1560. _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
  1561. return __state_->wait_for(__rel_time);
  1562. }
  1563. template <class _Clock, class _Duration>
  1564. _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
  1565. return __state_->wait_until(__abs_time);
  1566. }
  1567. };
  1568. template <class _Rp>
  1569. shared_future<_Rp>::~shared_future() {
  1570. if (__state_)
  1571. __state_->__release_shared();
  1572. }
  1573. template <class _Rp>
  1574. shared_future<_Rp>& shared_future<_Rp>::operator=(const shared_future& __rhs) _NOEXCEPT {
  1575. if (__rhs.__state_)
  1576. __rhs.__state_->__add_shared();
  1577. if (__state_)
  1578. __state_->__release_shared();
  1579. __state_ = __rhs.__state_;
  1580. return *this;
  1581. }
  1582. template <class _Rp>
  1583. class _LIBCPP_TEMPLATE_VIS shared_future<_Rp&> {
  1584. __assoc_state<_Rp&>* __state_;
  1585. public:
  1586. _LIBCPP_HIDE_FROM_ABI shared_future() _NOEXCEPT : __state_(nullptr) {}
  1587. _LIBCPP_HIDE_FROM_ABI shared_future(const shared_future& __rhs) : __state_(__rhs.__state_) {
  1588. if (__state_)
  1589. __state_->__add_shared();
  1590. }
  1591. _LIBCPP_HIDE_FROM_ABI shared_future(future<_Rp&>&& __f) _NOEXCEPT : __state_(__f.__state_) { __f.__state_ = nullptr; }
  1592. _LIBCPP_HIDE_FROM_ABI shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {
  1593. __rhs.__state_ = nullptr;
  1594. }
  1595. _LIBCPP_HIDE_FROM_ABI ~shared_future();
  1596. _LIBCPP_HIDE_FROM_ABI shared_future& operator=(const shared_future& __rhs);
  1597. _LIBCPP_HIDE_FROM_ABI shared_future& operator=(shared_future&& __rhs) _NOEXCEPT {
  1598. shared_future(std::move(__rhs)).swap(*this);
  1599. return *this;
  1600. }
  1601. // retrieving the value
  1602. _LIBCPP_HIDE_FROM_ABI _Rp& get() const { return __state_->copy(); }
  1603. _LIBCPP_HIDE_FROM_ABI void swap(shared_future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
  1604. // functions to check state
  1605. _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
  1606. _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
  1607. template <class _Rep, class _Period>
  1608. _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
  1609. return __state_->wait_for(__rel_time);
  1610. }
  1611. template <class _Clock, class _Duration>
  1612. _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
  1613. return __state_->wait_until(__abs_time);
  1614. }
  1615. };
  1616. template <class _Rp>
  1617. shared_future<_Rp&>::~shared_future() {
  1618. if (__state_)
  1619. __state_->__release_shared();
  1620. }
  1621. template <class _Rp>
  1622. shared_future<_Rp&>& shared_future<_Rp&>::operator=(const shared_future& __rhs) {
  1623. if (__rhs.__state_)
  1624. __rhs.__state_->__add_shared();
  1625. if (__state_)
  1626. __state_->__release_shared();
  1627. __state_ = __rhs.__state_;
  1628. return *this;
  1629. }
  1630. template <>
  1631. class _LIBCPP_EXPORTED_FROM_ABI shared_future<void> {
  1632. __assoc_sub_state* __state_;
  1633. public:
  1634. _LIBCPP_HIDE_FROM_ABI shared_future() _NOEXCEPT : __state_(nullptr) {}
  1635. _LIBCPP_HIDE_FROM_ABI shared_future(const shared_future& __rhs) : __state_(__rhs.__state_) {
  1636. if (__state_)
  1637. __state_->__add_shared();
  1638. }
  1639. _LIBCPP_HIDE_FROM_ABI shared_future(future<void>&& __f) _NOEXCEPT : __state_(__f.__state_) { __f.__state_ = nullptr; }
  1640. _LIBCPP_HIDE_FROM_ABI shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {
  1641. __rhs.__state_ = nullptr;
  1642. }
  1643. ~shared_future();
  1644. shared_future& operator=(const shared_future& __rhs);
  1645. _LIBCPP_HIDE_FROM_ABI shared_future& operator=(shared_future&& __rhs) _NOEXCEPT {
  1646. shared_future(std::move(__rhs)).swap(*this);
  1647. return *this;
  1648. }
  1649. // retrieving the value
  1650. _LIBCPP_HIDE_FROM_ABI void get() const { __state_->copy(); }
  1651. _LIBCPP_HIDE_FROM_ABI void swap(shared_future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
  1652. // functions to check state
  1653. _LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
  1654. _LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
  1655. template <class _Rep, class _Period>
  1656. _LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
  1657. return __state_->wait_for(__rel_time);
  1658. }
  1659. template <class _Clock, class _Duration>
  1660. _LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
  1661. return __state_->wait_until(__abs_time);
  1662. }
  1663. };
  1664. template <class _Rp>
  1665. inline _LIBCPP_HIDE_FROM_ABI void swap(shared_future<_Rp>& __x, shared_future<_Rp>& __y) _NOEXCEPT {
  1666. __x.swap(__y);
  1667. }
  1668. template <class _Rp>
  1669. inline shared_future<_Rp> future<_Rp>::share() _NOEXCEPT {
  1670. return shared_future<_Rp>(std::move(*this));
  1671. }
  1672. template <class _Rp>
  1673. inline shared_future<_Rp&> future<_Rp&>::share() _NOEXCEPT {
  1674. return shared_future<_Rp&>(std::move(*this));
  1675. }
  1676. inline shared_future<void> future<void>::share() _NOEXCEPT { return shared_future<void>(std::move(*this)); }
  1677. _LIBCPP_END_NAMESPACE_STD
  1678. _LIBCPP_POP_MACROS
  1679. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 17
  1680. # include <chrono>
  1681. #endif
  1682. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  1683. # include <atomic>
  1684. # include <cstdlib>
  1685. # include <exception>
  1686. # include <iosfwd>
  1687. # include <system_error>
  1688. #endif
  1689. #endif // _LIBCPP_FUTURE