future 67 KB

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