future 64 KB

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