future 66 KB

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