future 66 KB

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