future 70 KB

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