123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457 |
- // -*- C++ -*-
- //===----------------------------------------------------------------------===//
- //
- // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
- // See https://llvm.org/LICENSE.txt for license information.
- // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
- //
- //===----------------------------------------------------------------------===//
- #ifndef _LIBCPP_FUTURE
- #define _LIBCPP_FUTURE
- /*
- future synopsis
- namespace std
- {
- enum class future_errc
- {
- future_already_retrieved = 1,
- promise_already_satisfied,
- no_state,
- broken_promise
- };
- enum class launch
- {
- async = 1,
- deferred = 2,
- any = async | deferred
- };
- enum class future_status
- {
- ready,
- timeout,
- deferred
- };
- template <> struct is_error_code_enum<future_errc> : public true_type { };
- error_code make_error_code(future_errc e) noexcept;
- error_condition make_error_condition(future_errc e) noexcept;
- const error_category& future_category() noexcept;
- class future_error
- : public logic_error
- {
- public:
- future_error(error_code ec); // exposition only
- explicit future_error(future_errc); // C++17
- const error_code& code() const noexcept;
- const char* what() const noexcept;
- };
- template <class R>
- class promise
- {
- public:
- promise();
- template <class Allocator>
- promise(allocator_arg_t, const Allocator& a);
- promise(promise&& rhs) noexcept;
- promise(const promise& rhs) = delete;
- ~promise();
- // assignment
- promise& operator=(promise&& rhs) noexcept;
- promise& operator=(const promise& rhs) = delete;
- void swap(promise& other) noexcept;
- // retrieving the result
- future<R> get_future();
- // setting the result
- void set_value(const R& r);
- void set_value(R&& r);
- void set_exception(exception_ptr p);
- // setting the result with deferred notification
- void set_value_at_thread_exit(const R& r);
- void set_value_at_thread_exit(R&& r);
- void set_exception_at_thread_exit(exception_ptr p);
- };
- template <class R>
- class promise<R&>
- {
- public:
- promise();
- template <class Allocator>
- promise(allocator_arg_t, const Allocator& a);
- promise(promise&& rhs) noexcept;
- promise(const promise& rhs) = delete;
- ~promise();
- // assignment
- promise& operator=(promise&& rhs) noexcept;
- promise& operator=(const promise& rhs) = delete;
- void swap(promise& other) noexcept;
- // retrieving the result
- future<R&> get_future();
- // setting the result
- void set_value(R& r);
- void set_exception(exception_ptr p);
- // setting the result with deferred notification
- void set_value_at_thread_exit(R&);
- void set_exception_at_thread_exit(exception_ptr p);
- };
- template <>
- class promise<void>
- {
- public:
- promise();
- template <class Allocator>
- promise(allocator_arg_t, const Allocator& a);
- promise(promise&& rhs) noexcept;
- promise(const promise& rhs) = delete;
- ~promise();
- // assignment
- promise& operator=(promise&& rhs) noexcept;
- promise& operator=(const promise& rhs) = delete;
- void swap(promise& other) noexcept;
- // retrieving the result
- future<void> get_future();
- // setting the result
- void set_value();
- void set_exception(exception_ptr p);
- // setting the result with deferred notification
- void set_value_at_thread_exit();
- void set_exception_at_thread_exit(exception_ptr p);
- };
- template <class R> void swap(promise<R>& x, promise<R>& y) noexcept;
- template <class R, class Alloc>
- struct uses_allocator<promise<R>, Alloc> : public true_type {};
- template <class R>
- class future
- {
- public:
- future() noexcept;
- future(future&&) noexcept;
- future(const future& rhs) = delete;
- ~future();
- future& operator=(const future& rhs) = delete;
- future& operator=(future&&) noexcept;
- shared_future<R> share() noexcept;
- // retrieving the value
- R get();
- // functions to check state
- bool valid() const noexcept;
- void wait() const;
- template <class Rep, class Period>
- future_status
- wait_for(const chrono::duration<Rep, Period>& rel_time) const;
- template <class Clock, class Duration>
- future_status
- wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
- };
- template <class R>
- class future<R&>
- {
- public:
- future() noexcept;
- future(future&&) noexcept;
- future(const future& rhs) = delete;
- ~future();
- future& operator=(const future& rhs) = delete;
- future& operator=(future&&) noexcept;
- shared_future<R&> share() noexcept;
- // retrieving the value
- R& get();
- // functions to check state
- bool valid() const noexcept;
- void wait() const;
- template <class Rep, class Period>
- future_status
- wait_for(const chrono::duration<Rep, Period>& rel_time) const;
- template <class Clock, class Duration>
- future_status
- wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
- };
- template <>
- class future<void>
- {
- public:
- future() noexcept;
- future(future&&) noexcept;
- future(const future& rhs) = delete;
- ~future();
- future& operator=(const future& rhs) = delete;
- future& operator=(future&&) noexcept;
- shared_future<void> share() noexcept;
- // retrieving the value
- void get();
- // functions to check state
- bool valid() const noexcept;
- void wait() const;
- template <class Rep, class Period>
- future_status
- wait_for(const chrono::duration<Rep, Period>& rel_time) const;
- template <class Clock, class Duration>
- future_status
- wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
- };
- template <class R>
- class shared_future
- {
- public:
- shared_future() noexcept;
- shared_future(const shared_future& rhs);
- shared_future(future<R>&&) noexcept;
- shared_future(shared_future&& rhs) noexcept;
- ~shared_future();
- shared_future& operator=(const shared_future& rhs);
- shared_future& operator=(shared_future&& rhs) noexcept;
- // retrieving the value
- const R& get() const;
- // functions to check state
- bool valid() const noexcept;
- void wait() const;
- template <class Rep, class Period>
- future_status
- wait_for(const chrono::duration<Rep, Period>& rel_time) const;
- template <class Clock, class Duration>
- future_status
- wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
- };
- template <class R>
- class shared_future<R&>
- {
- public:
- shared_future() noexcept;
- shared_future(const shared_future& rhs);
- shared_future(future<R&>&&) noexcept;
- shared_future(shared_future&& rhs) noexcept;
- ~shared_future();
- shared_future& operator=(const shared_future& rhs);
- shared_future& operator=(shared_future&& rhs) noexcept;
- // retrieving the value
- R& get() const;
- // functions to check state
- bool valid() const noexcept;
- void wait() const;
- template <class Rep, class Period>
- future_status
- wait_for(const chrono::duration<Rep, Period>& rel_time) const;
- template <class Clock, class Duration>
- future_status
- wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
- };
- template <>
- class shared_future<void>
- {
- public:
- shared_future() noexcept;
- shared_future(const shared_future& rhs);
- shared_future(future<void>&&) noexcept;
- shared_future(shared_future&& rhs) noexcept;
- ~shared_future();
- shared_future& operator=(const shared_future& rhs);
- shared_future& operator=(shared_future&& rhs) noexcept;
- // retrieving the value
- void get() const;
- // functions to check state
- bool valid() const noexcept;
- void wait() const;
- template <class Rep, class Period>
- future_status
- wait_for(const chrono::duration<Rep, Period>& rel_time) const;
- template <class Clock, class Duration>
- future_status
- wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
- };
- template <class F, class... Args>
- future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
- async(F&& f, Args&&... args);
- template <class F, class... Args>
- future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
- async(launch policy, F&& f, Args&&... args);
- template <class> class packaged_task; // undefined
- template <class R, class... ArgTypes>
- class packaged_task<R(ArgTypes...)>
- {
- public:
- typedef R result_type; // extension
- // construction and destruction
- packaged_task() noexcept;
- template <class F>
- explicit packaged_task(F&& f);
- template <class F, class Allocator>
- packaged_task(allocator_arg_t, const Allocator& a, F&& f);
- ~packaged_task();
- // no copy
- packaged_task(const packaged_task&) = delete;
- packaged_task& operator=(const packaged_task&) = delete;
- // move support
- packaged_task(packaged_task&& other) noexcept;
- packaged_task& operator=(packaged_task&& other) noexcept;
- void swap(packaged_task& other) noexcept;
- bool valid() const noexcept;
- // result retrieval
- future<R> get_future();
- // execution
- void operator()(ArgTypes... );
- void make_ready_at_thread_exit(ArgTypes...);
- void reset();
- };
- template <class R>
- void swap(packaged_task<R(ArgTypes...)&, packaged_task<R(ArgTypes...)>&) noexcept;
- template <class R, class Alloc> struct uses_allocator<packaged_task<R>, Alloc>;
- } // std
- */
- #include <__assert> // all public C++ headers provide the assertion handler
- #include <__availability>
- #include <__chrono/duration.h>
- #include <__chrono/time_point.h>
- #include <__config>
- #include <__memory/allocator_arg_t.h>
- #include <__memory/allocator_destructor.h>
- #include <__memory/uses_allocator.h>
- #include <__type_traits/strip_signature.h>
- #include <__utility/auto_cast.h>
- #include <__utility/forward.h>
- #include <__utility/move.h>
- #include <exception>
- #include <mutex>
- #include <new>
- #include <system_error>
- #include <thread>
- #include <version>
- #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
- # pragma GCC system_header
- #endif
- #ifdef _LIBCPP_HAS_NO_THREADS
- # error "<future> is not supported since libc++ has been configured without support for threads."
- #endif
- _LIBCPP_BEGIN_NAMESPACE_STD
- //enum class future_errc
- _LIBCPP_DECLARE_STRONG_ENUM(future_errc)
- {
- future_already_retrieved = 1,
- promise_already_satisfied,
- no_state,
- broken_promise
- };
- _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_errc)
- template <>
- struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<future_errc> : public true_type {};
- #ifdef _LIBCPP_CXX03_LANG
- template <>
- struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<future_errc::__lx> : public true_type { };
- #endif
- //enum class launch
- _LIBCPP_DECLARE_STRONG_ENUM(launch)
- {
- async = 1,
- deferred = 2,
- any = async | deferred
- };
- _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(launch)
- #ifndef _LIBCPP_CXX03_LANG
- typedef underlying_type<launch>::type __launch_underlying_type;
- inline _LIBCPP_INLINE_VISIBILITY
- _LIBCPP_CONSTEXPR
- launch
- operator&(launch __x, launch __y)
- {
- return static_cast<launch>(static_cast<__launch_underlying_type>(__x) &
- static_cast<__launch_underlying_type>(__y));
- }
- inline _LIBCPP_INLINE_VISIBILITY
- _LIBCPP_CONSTEXPR
- launch
- operator|(launch __x, launch __y)
- {
- return static_cast<launch>(static_cast<__launch_underlying_type>(__x) |
- static_cast<__launch_underlying_type>(__y));
- }
- inline _LIBCPP_INLINE_VISIBILITY
- _LIBCPP_CONSTEXPR
- launch
- operator^(launch __x, launch __y)
- {
- return static_cast<launch>(static_cast<__launch_underlying_type>(__x) ^
- static_cast<__launch_underlying_type>(__y));
- }
- inline _LIBCPP_INLINE_VISIBILITY
- _LIBCPP_CONSTEXPR
- launch
- operator~(launch __x)
- {
- return static_cast<launch>(~static_cast<__launch_underlying_type>(__x) & 3);
- }
- inline _LIBCPP_INLINE_VISIBILITY
- launch&
- operator&=(launch& __x, launch __y)
- {
- __x = __x & __y; return __x;
- }
- inline _LIBCPP_INLINE_VISIBILITY
- launch&
- operator|=(launch& __x, launch __y)
- {
- __x = __x | __y; return __x;
- }
- inline _LIBCPP_INLINE_VISIBILITY
- launch&
- operator^=(launch& __x, launch __y)
- {
- __x = __x ^ __y; return __x;
- }
- #endif // !_LIBCPP_CXX03_LANG
- //enum class future_status
- _LIBCPP_DECLARE_STRONG_ENUM(future_status)
- {
- ready,
- timeout,
- deferred
- };
- _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_status)
- _LIBCPP_FUNC_VIS
- const error_category& future_category() _NOEXCEPT;
- inline _LIBCPP_INLINE_VISIBILITY
- error_code
- make_error_code(future_errc __e) _NOEXCEPT
- {
- return error_code(static_cast<int>(__e), future_category());
- }
- inline _LIBCPP_INLINE_VISIBILITY
- error_condition
- make_error_condition(future_errc __e) _NOEXCEPT
- {
- return error_condition(static_cast<int>(__e), future_category());
- }
- class _LIBCPP_EXCEPTION_ABI _LIBCPP_AVAILABILITY_FUTURE_ERROR future_error
- : public logic_error
- {
- error_code __ec_;
- public:
- future_error(error_code __ec);
- _LIBCPP_INLINE_VISIBILITY
- const error_code& code() const _NOEXCEPT {return __ec_;}
- future_error(const future_error&) _NOEXCEPT = default;
- ~future_error() _NOEXCEPT override;
- };
- _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
- #ifndef _LIBCPP_NO_EXCEPTIONS
- _LIBCPP_AVAILABILITY_FUTURE_ERROR
- #endif
- void __throw_future_error(future_errc __ev)
- {
- #ifndef _LIBCPP_NO_EXCEPTIONS
- throw future_error(make_error_code(__ev));
- #else
- ((void)__ev);
- _VSTD::abort();
- #endif
- }
- class _LIBCPP_TYPE_VIS _LIBCPP_AVAILABILITY_FUTURE __assoc_sub_state
- : public __shared_count
- {
- protected:
- exception_ptr __exception_;
- mutable mutex __mut_;
- mutable condition_variable __cv_;
- unsigned __state_;
- void __on_zero_shared() _NOEXCEPT override;
- void __sub_wait(unique_lock<mutex>& __lk);
- public:
- enum
- {
- __constructed = 1,
- __future_attached = 2,
- ready = 4,
- deferred = 8
- };
- _LIBCPP_INLINE_VISIBILITY
- __assoc_sub_state() : __state_(0) {}
- _LIBCPP_INLINE_VISIBILITY
- bool __has_value() const
- {return (__state_ & __constructed) || (__exception_ != nullptr);}
- _LIBCPP_INLINE_VISIBILITY
- void __attach_future() {
- lock_guard<mutex> __lk(__mut_);
- bool __has_future_attached = (__state_ & __future_attached) != 0;
- if (__has_future_attached)
- __throw_future_error(future_errc::future_already_retrieved);
- this->__add_shared();
- __state_ |= __future_attached;
- }
- _LIBCPP_INLINE_VISIBILITY
- void __set_deferred() {__state_ |= deferred;}
- void __make_ready();
- _LIBCPP_INLINE_VISIBILITY
- bool __is_ready() const {return (__state_ & ready) != 0;}
- void set_value();
- void set_value_at_thread_exit();
- void set_exception(exception_ptr __p);
- void set_exception_at_thread_exit(exception_ptr __p);
- void copy();
- void wait();
- template <class _Rep, class _Period>
- future_status
- _LIBCPP_INLINE_VISIBILITY
- wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const;
- template <class _Clock, class _Duration>
- _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
- future_status
- wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const;
- virtual void __execute();
- };
- template <class _Clock, class _Duration>
- future_status
- __assoc_sub_state::wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
- {
- unique_lock<mutex> __lk(__mut_);
- if (__state_ & deferred)
- return future_status::deferred;
- while (!(__state_ & ready) && _Clock::now() < __abs_time)
- __cv_.wait_until(__lk, __abs_time);
- if (__state_ & ready)
- return future_status::ready;
- return future_status::timeout;
- }
- template <class _Rep, class _Period>
- inline
- future_status
- __assoc_sub_state::wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
- {
- return wait_until(chrono::steady_clock::now() + __rel_time);
- }
- template <class _Rp>
- class _LIBCPP_AVAILABILITY_FUTURE _LIBCPP_HIDDEN __assoc_state
- : public __assoc_sub_state
- {
- typedef __assoc_sub_state base;
- _LIBCPP_SUPPRESS_DEPRECATED_PUSH
- typedef typename aligned_storage<sizeof(_Rp), alignment_of<_Rp>::value>::type _Up;
- _LIBCPP_SUPPRESS_DEPRECATED_POP
- protected:
- _Up __value_;
- void __on_zero_shared() _NOEXCEPT override;
- public:
- template <class _Arg>
- void set_value(_Arg&& __arg);
- template <class _Arg>
- void set_value_at_thread_exit(_Arg&& __arg);
- _Rp move();
- __add_lvalue_reference_t<_Rp> copy();
- };
- template <class _Rp>
- void
- __assoc_state<_Rp>::__on_zero_shared() _NOEXCEPT
- {
- if (this->__state_ & base::__constructed)
- reinterpret_cast<_Rp*>(&__value_)->~_Rp();
- delete this;
- }
- template <class _Rp>
- template <class _Arg>
- _LIBCPP_AVAILABILITY_FUTURE
- void
- __assoc_state<_Rp>::set_value(_Arg&& __arg)
- {
- unique_lock<mutex> __lk(this->__mut_);
- if (this->__has_value())
- __throw_future_error(future_errc::promise_already_satisfied);
- ::new ((void*)&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
- this->__state_ |= base::__constructed | base::ready;
- __cv_.notify_all();
- }
- template <class _Rp>
- template <class _Arg>
- void
- __assoc_state<_Rp>::set_value_at_thread_exit(_Arg&& __arg)
- {
- unique_lock<mutex> __lk(this->__mut_);
- if (this->__has_value())
- __throw_future_error(future_errc::promise_already_satisfied);
- ::new ((void*)&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
- this->__state_ |= base::__constructed;
- __thread_local_data()->__make_ready_at_thread_exit(this);
- }
- template <class _Rp>
- _Rp
- __assoc_state<_Rp>::move()
- {
- unique_lock<mutex> __lk(this->__mut_);
- this->__sub_wait(__lk);
- if (this->__exception_ != nullptr)
- std::rethrow_exception(this->__exception_);
- return _VSTD::move(*reinterpret_cast<_Rp*>(&__value_));
- }
- template <class _Rp>
- __add_lvalue_reference_t<_Rp>
- __assoc_state<_Rp>::copy()
- {
- unique_lock<mutex> __lk(this->__mut_);
- this->__sub_wait(__lk);
- if (this->__exception_ != nullptr)
- std::rethrow_exception(this->__exception_);
- return *reinterpret_cast<_Rp*>(&__value_);
- }
- template <class _Rp>
- class _LIBCPP_AVAILABILITY_FUTURE __assoc_state<_Rp&>
- : public __assoc_sub_state
- {
- typedef __assoc_sub_state base;
- typedef _Rp* _Up;
- protected:
- _Up __value_;
- void __on_zero_shared() _NOEXCEPT override;
- public:
- void set_value(_Rp& __arg);
- void set_value_at_thread_exit(_Rp& __arg);
- _Rp& copy();
- };
- template <class _Rp>
- void
- __assoc_state<_Rp&>::__on_zero_shared() _NOEXCEPT
- {
- delete this;
- }
- template <class _Rp>
- void
- __assoc_state<_Rp&>::set_value(_Rp& __arg)
- {
- unique_lock<mutex> __lk(this->__mut_);
- if (this->__has_value())
- __throw_future_error(future_errc::promise_already_satisfied);
- __value_ = _VSTD::addressof(__arg);
- this->__state_ |= base::__constructed | base::ready;
- __cv_.notify_all();
- }
- template <class _Rp>
- void
- __assoc_state<_Rp&>::set_value_at_thread_exit(_Rp& __arg)
- {
- unique_lock<mutex> __lk(this->__mut_);
- if (this->__has_value())
- __throw_future_error(future_errc::promise_already_satisfied);
- __value_ = _VSTD::addressof(__arg);
- this->__state_ |= base::__constructed;
- __thread_local_data()->__make_ready_at_thread_exit(this);
- }
- template <class _Rp>
- _Rp&
- __assoc_state<_Rp&>::copy()
- {
- unique_lock<mutex> __lk(this->__mut_);
- this->__sub_wait(__lk);
- if (this->__exception_ != nullptr)
- std::rethrow_exception(this->__exception_);
- return *__value_;
- }
- template <class _Rp, class _Alloc>
- class _LIBCPP_AVAILABILITY_FUTURE __assoc_state_alloc
- : public __assoc_state<_Rp>
- {
- typedef __assoc_state<_Rp> base;
- _Alloc __alloc_;
- virtual void __on_zero_shared() _NOEXCEPT;
- public:
- _LIBCPP_INLINE_VISIBILITY
- explicit __assoc_state_alloc(const _Alloc& __a)
- : __alloc_(__a) {}
- };
- template <class _Rp, class _Alloc>
- void
- __assoc_state_alloc<_Rp, _Alloc>::__on_zero_shared() _NOEXCEPT
- {
- if (this->__state_ & base::__constructed)
- reinterpret_cast<_Rp*>(_VSTD::addressof(this->__value_))->~_Rp();
- typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
- typedef allocator_traits<_Al> _ATraits;
- typedef pointer_traits<typename _ATraits::pointer> _PTraits;
- _Al __a(__alloc_);
- this->~__assoc_state_alloc();
- __a.deallocate(_PTraits::pointer_to(*this), 1);
- }
- template <class _Rp, class _Alloc>
- class _LIBCPP_AVAILABILITY_FUTURE __assoc_state_alloc<_Rp&, _Alloc>
- : public __assoc_state<_Rp&>
- {
- typedef __assoc_state<_Rp&> base;
- _Alloc __alloc_;
- virtual void __on_zero_shared() _NOEXCEPT;
- public:
- _LIBCPP_INLINE_VISIBILITY
- explicit __assoc_state_alloc(const _Alloc& __a)
- : __alloc_(__a) {}
- };
- template <class _Rp, class _Alloc>
- void
- __assoc_state_alloc<_Rp&, _Alloc>::__on_zero_shared() _NOEXCEPT
- {
- typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
- typedef allocator_traits<_Al> _ATraits;
- typedef pointer_traits<typename _ATraits::pointer> _PTraits;
- _Al __a(__alloc_);
- this->~__assoc_state_alloc();
- __a.deallocate(_PTraits::pointer_to(*this), 1);
- }
- template <class _Alloc>
- class _LIBCPP_AVAILABILITY_FUTURE __assoc_sub_state_alloc
- : public __assoc_sub_state
- {
- typedef __assoc_sub_state base;
- _Alloc __alloc_;
- void __on_zero_shared() _NOEXCEPT override;
- public:
- _LIBCPP_INLINE_VISIBILITY
- explicit __assoc_sub_state_alloc(const _Alloc& __a)
- : __alloc_(__a) {}
- };
- template <class _Alloc>
- void
- __assoc_sub_state_alloc<_Alloc>::__on_zero_shared() _NOEXCEPT
- {
- typedef typename __allocator_traits_rebind<_Alloc, __assoc_sub_state_alloc>::type _Al;
- typedef allocator_traits<_Al> _ATraits;
- typedef pointer_traits<typename _ATraits::pointer> _PTraits;
- _Al __a(__alloc_);
- this->~__assoc_sub_state_alloc();
- __a.deallocate(_PTraits::pointer_to(*this), 1);
- }
- template <class _Rp, class _Fp>
- class _LIBCPP_AVAILABILITY_FUTURE __deferred_assoc_state
- : public __assoc_state<_Rp>
- {
- typedef __assoc_state<_Rp> base;
- _Fp __func_;
- public:
- _LIBCPP_INLINE_VISIBILITY
- explicit __deferred_assoc_state(_Fp&& __f);
- virtual void __execute();
- };
- template <class _Rp, class _Fp>
- inline
- __deferred_assoc_state<_Rp, _Fp>::__deferred_assoc_state(_Fp&& __f)
- : __func_(_VSTD::forward<_Fp>(__f))
- {
- this->__set_deferred();
- }
- template <class _Rp, class _Fp>
- void
- __deferred_assoc_state<_Rp, _Fp>::__execute()
- {
- #ifndef _LIBCPP_NO_EXCEPTIONS
- try
- {
- #endif // _LIBCPP_NO_EXCEPTIONS
- this->set_value(__func_());
- #ifndef _LIBCPP_NO_EXCEPTIONS
- }
- catch (...)
- {
- this->set_exception(current_exception());
- }
- #endif // _LIBCPP_NO_EXCEPTIONS
- }
- template <class _Fp>
- class _LIBCPP_AVAILABILITY_FUTURE __deferred_assoc_state<void, _Fp>
- : public __assoc_sub_state
- {
- typedef __assoc_sub_state base;
- _Fp __func_;
- public:
- _LIBCPP_INLINE_VISIBILITY
- explicit __deferred_assoc_state(_Fp&& __f);
- void __execute() override;
- };
- template <class _Fp>
- inline
- __deferred_assoc_state<void, _Fp>::__deferred_assoc_state(_Fp&& __f)
- : __func_(_VSTD::forward<_Fp>(__f))
- {
- this->__set_deferred();
- }
- template <class _Fp>
- void
- __deferred_assoc_state<void, _Fp>::__execute()
- {
- #ifndef _LIBCPP_NO_EXCEPTIONS
- try
- {
- #endif // _LIBCPP_NO_EXCEPTIONS
- __func_();
- this->set_value();
- #ifndef _LIBCPP_NO_EXCEPTIONS
- }
- catch (...)
- {
- this->set_exception(current_exception());
- }
- #endif // _LIBCPP_NO_EXCEPTIONS
- }
- template <class _Rp, class _Fp>
- class _LIBCPP_AVAILABILITY_FUTURE __async_assoc_state
- : public __assoc_state<_Rp>
- {
- typedef __assoc_state<_Rp> base;
- _Fp __func_;
- virtual void __on_zero_shared() _NOEXCEPT;
- public:
- _LIBCPP_INLINE_VISIBILITY
- explicit __async_assoc_state(_Fp&& __f);
- virtual void __execute();
- };
- template <class _Rp, class _Fp>
- inline
- __async_assoc_state<_Rp, _Fp>::__async_assoc_state(_Fp&& __f)
- : __func_(_VSTD::forward<_Fp>(__f))
- {
- }
- template <class _Rp, class _Fp>
- void
- __async_assoc_state<_Rp, _Fp>::__execute()
- {
- #ifndef _LIBCPP_NO_EXCEPTIONS
- try
- {
- #endif // _LIBCPP_NO_EXCEPTIONS
- this->set_value(__func_());
- #ifndef _LIBCPP_NO_EXCEPTIONS
- }
- catch (...)
- {
- this->set_exception(current_exception());
- }
- #endif // _LIBCPP_NO_EXCEPTIONS
- }
- template <class _Rp, class _Fp>
- void
- __async_assoc_state<_Rp, _Fp>::__on_zero_shared() _NOEXCEPT
- {
- this->wait();
- base::__on_zero_shared();
- }
- template <class _Fp>
- class _LIBCPP_AVAILABILITY_FUTURE __async_assoc_state<void, _Fp>
- : public __assoc_sub_state
- {
- typedef __assoc_sub_state base;
- _Fp __func_;
- void __on_zero_shared() _NOEXCEPT override;
- public:
- _LIBCPP_INLINE_VISIBILITY
- explicit __async_assoc_state(_Fp&& __f);
- void __execute() override;
- };
- template <class _Fp>
- inline
- __async_assoc_state<void, _Fp>::__async_assoc_state(_Fp&& __f)
- : __func_(_VSTD::forward<_Fp>(__f))
- {
- }
- template <class _Fp>
- void
- __async_assoc_state<void, _Fp>::__execute()
- {
- #ifndef _LIBCPP_NO_EXCEPTIONS
- try
- {
- #endif // _LIBCPP_NO_EXCEPTIONS
- __func_();
- this->set_value();
- #ifndef _LIBCPP_NO_EXCEPTIONS
- }
- catch (...)
- {
- this->set_exception(current_exception());
- }
- #endif // _LIBCPP_NO_EXCEPTIONS
- }
- template <class _Fp>
- void
- __async_assoc_state<void, _Fp>::__on_zero_shared() _NOEXCEPT
- {
- this->wait();
- base::__on_zero_shared();
- }
- template <class _Rp> class _LIBCPP_TEMPLATE_VIS promise;
- template <class _Rp> class _LIBCPP_TEMPLATE_VIS shared_future;
- // future
- template <class _Rp> class _LIBCPP_TEMPLATE_VIS future;
- template <class _Rp, class _Fp>
- _LIBCPP_INLINE_VISIBILITY future<_Rp>
- __make_deferred_assoc_state(_Fp&& __f);
- template <class _Rp, class _Fp>
- _LIBCPP_INLINE_VISIBILITY future<_Rp>
- __make_async_assoc_state(_Fp&& __f);
- template <class _Rp>
- class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE future
- {
- __assoc_state<_Rp>* __state_;
- explicit future(__assoc_state<_Rp>* __state);
- template <class> friend class promise;
- template <class> friend class shared_future;
- template <class _R1, class _Fp>
- friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
- template <class _R1, class _Fp>
- friend future<_R1> __make_async_assoc_state(_Fp&& __f);
- public:
- _LIBCPP_INLINE_VISIBILITY
- future() _NOEXCEPT : __state_(nullptr) {}
- _LIBCPP_INLINE_VISIBILITY
- future(future&& __rhs) _NOEXCEPT
- : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
- future(const future&) = delete;
- future& operator=(const future&) = delete;
- _LIBCPP_INLINE_VISIBILITY
- future& operator=(future&& __rhs) _NOEXCEPT
- {
- future(_VSTD::move(__rhs)).swap(*this);
- return *this;
- }
- ~future();
- _LIBCPP_INLINE_VISIBILITY
- shared_future<_Rp> share() _NOEXCEPT;
- // retrieving the value
- _Rp get();
- _LIBCPP_INLINE_VISIBILITY
- void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
- // functions to check state
- _LIBCPP_INLINE_VISIBILITY
- bool valid() const _NOEXCEPT {return __state_ != nullptr;}
- _LIBCPP_INLINE_VISIBILITY
- void wait() const {__state_->wait();}
- template <class _Rep, class _Period>
- _LIBCPP_INLINE_VISIBILITY
- future_status
- wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
- {return __state_->wait_for(__rel_time);}
- template <class _Clock, class _Duration>
- _LIBCPP_INLINE_VISIBILITY
- future_status
- wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
- {return __state_->wait_until(__abs_time);}
- };
- template <class _Rp>
- future<_Rp>::future(__assoc_state<_Rp>* __state)
- : __state_(__state)
- {
- __state_->__attach_future();
- }
- struct __release_shared_count
- {
- void operator()(__shared_count* __p) {__p->__release_shared();}
- };
- template <class _Rp>
- future<_Rp>::~future()
- {
- if (__state_)
- __state_->__release_shared();
- }
- template <class _Rp>
- _Rp
- future<_Rp>::get()
- {
- unique_ptr<__shared_count, __release_shared_count> __(__state_);
- __assoc_state<_Rp>* __s = __state_;
- __state_ = nullptr;
- return __s->move();
- }
- template <class _Rp>
- class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE future<_Rp&>
- {
- __assoc_state<_Rp&>* __state_;
- explicit future(__assoc_state<_Rp&>* __state);
- template <class> friend class promise;
- template <class> friend class shared_future;
- template <class _R1, class _Fp>
- friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
- template <class _R1, class _Fp>
- friend future<_R1> __make_async_assoc_state(_Fp&& __f);
- public:
- _LIBCPP_INLINE_VISIBILITY
- future() _NOEXCEPT : __state_(nullptr) {}
- _LIBCPP_INLINE_VISIBILITY
- future(future&& __rhs) _NOEXCEPT
- : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
- future(const future&) = delete;
- future& operator=(const future&) = delete;
- _LIBCPP_INLINE_VISIBILITY
- future& operator=(future&& __rhs) _NOEXCEPT
- {
- future(_VSTD::move(__rhs)).swap(*this);
- return *this;
- }
- ~future();
- _LIBCPP_INLINE_VISIBILITY
- shared_future<_Rp&> share() _NOEXCEPT;
- // retrieving the value
- _Rp& get();
- _LIBCPP_INLINE_VISIBILITY
- void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
- // functions to check state
- _LIBCPP_INLINE_VISIBILITY
- bool valid() const _NOEXCEPT {return __state_ != nullptr;}
- _LIBCPP_INLINE_VISIBILITY
- void wait() const {__state_->wait();}
- template <class _Rep, class _Period>
- _LIBCPP_INLINE_VISIBILITY
- future_status
- wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
- {return __state_->wait_for(__rel_time);}
- template <class _Clock, class _Duration>
- _LIBCPP_INLINE_VISIBILITY
- future_status
- wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
- {return __state_->wait_until(__abs_time);}
- };
- template <class _Rp>
- future<_Rp&>::future(__assoc_state<_Rp&>* __state)
- : __state_(__state)
- {
- __state_->__attach_future();
- }
- template <class _Rp>
- future<_Rp&>::~future()
- {
- if (__state_)
- __state_->__release_shared();
- }
- template <class _Rp>
- _Rp&
- future<_Rp&>::get()
- {
- unique_ptr<__shared_count, __release_shared_count> __(__state_);
- __assoc_state<_Rp&>* __s = __state_;
- __state_ = nullptr;
- return __s->copy();
- }
- template <>
- class _LIBCPP_TYPE_VIS _LIBCPP_AVAILABILITY_FUTURE future<void>
- {
- __assoc_sub_state* __state_;
- explicit future(__assoc_sub_state* __state);
- template <class> friend class promise;
- template <class> friend class shared_future;
- template <class _R1, class _Fp>
- friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
- template <class _R1, class _Fp>
- friend future<_R1> __make_async_assoc_state(_Fp&& __f);
- public:
- _LIBCPP_INLINE_VISIBILITY
- future() _NOEXCEPT : __state_(nullptr) {}
- _LIBCPP_INLINE_VISIBILITY
- future(future&& __rhs) _NOEXCEPT
- : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
- future(const future&) = delete;
- future& operator=(const future&) = delete;
- _LIBCPP_INLINE_VISIBILITY
- future& operator=(future&& __rhs) _NOEXCEPT
- {
- future(_VSTD::move(__rhs)).swap(*this);
- return *this;
- }
- ~future();
- _LIBCPP_INLINE_VISIBILITY
- shared_future<void> share() _NOEXCEPT;
- // retrieving the value
- void get();
- _LIBCPP_INLINE_VISIBILITY
- void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
- // functions to check state
- _LIBCPP_INLINE_VISIBILITY
- bool valid() const _NOEXCEPT {return __state_ != nullptr;}
- _LIBCPP_INLINE_VISIBILITY
- void wait() const {__state_->wait();}
- template <class _Rep, class _Period>
- _LIBCPP_INLINE_VISIBILITY
- future_status
- wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
- {return __state_->wait_for(__rel_time);}
- template <class _Clock, class _Duration>
- _LIBCPP_INLINE_VISIBILITY
- future_status
- wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
- {return __state_->wait_until(__abs_time);}
- };
- template <class _Rp>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- swap(future<_Rp>& __x, future<_Rp>& __y) _NOEXCEPT
- {
- __x.swap(__y);
- }
- // promise<R>
- template <class _Callable> class packaged_task;
- template <class _Rp>
- class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE promise
- {
- __assoc_state<_Rp>* __state_;
- _LIBCPP_INLINE_VISIBILITY
- explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
- template <class> friend class packaged_task;
- public:
- promise();
- template <class _Alloc>
- promise(allocator_arg_t, const _Alloc& __a);
- _LIBCPP_INLINE_VISIBILITY
- promise(promise&& __rhs) _NOEXCEPT
- : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
- promise(const promise& __rhs) = delete;
- ~promise();
- // assignment
- _LIBCPP_INLINE_VISIBILITY
- promise& operator=(promise&& __rhs) _NOEXCEPT
- {
- promise(_VSTD::move(__rhs)).swap(*this);
- return *this;
- }
- promise& operator=(const promise& __rhs) = delete;
- _LIBCPP_INLINE_VISIBILITY
- void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
- // retrieving the result
- future<_Rp> get_future();
- // setting the result
- void set_value(const _Rp& __r);
- void set_value(_Rp&& __r);
- void set_exception(exception_ptr __p);
- // setting the result with deferred notification
- void set_value_at_thread_exit(const _Rp& __r);
- void set_value_at_thread_exit(_Rp&& __r);
- void set_exception_at_thread_exit(exception_ptr __p);
- };
- template <class _Rp>
- promise<_Rp>::promise()
- : __state_(new __assoc_state<_Rp>)
- {
- }
- template <class _Rp>
- template <class _Alloc>
- promise<_Rp>::promise(allocator_arg_t, const _Alloc& __a0)
- {
- typedef __assoc_state_alloc<_Rp, _Alloc> _State;
- typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
- typedef __allocator_destructor<_A2> _D2;
- _A2 __a(__a0);
- unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
- ::new ((void*)_VSTD::addressof(*__hold.get())) _State(__a0);
- __state_ = _VSTD::addressof(*__hold.release());
- }
- template <class _Rp>
- promise<_Rp>::~promise()
- {
- if (__state_)
- {
- if (!__state_->__has_value() && __state_->use_count() > 1)
- __state_->set_exception(make_exception_ptr(
- future_error(make_error_code(future_errc::broken_promise))
- ));
- __state_->__release_shared();
- }
- }
- template <class _Rp>
- future<_Rp>
- promise<_Rp>::get_future()
- {
- if (__state_ == nullptr)
- __throw_future_error(future_errc::no_state);
- return future<_Rp>(__state_);
- }
- template <class _Rp>
- void
- promise<_Rp>::set_value(const _Rp& __r)
- {
- if (__state_ == nullptr)
- __throw_future_error(future_errc::no_state);
- __state_->set_value(__r);
- }
- template <class _Rp>
- void
- promise<_Rp>::set_value(_Rp&& __r)
- {
- if (__state_ == nullptr)
- __throw_future_error(future_errc::no_state);
- __state_->set_value(_VSTD::move(__r));
- }
- template <class _Rp>
- void
- promise<_Rp>::set_exception(exception_ptr __p)
- {
- _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception: received nullptr" );
- if (__state_ == nullptr)
- __throw_future_error(future_errc::no_state);
- __state_->set_exception(__p);
- }
- template <class _Rp>
- void
- promise<_Rp>::set_value_at_thread_exit(const _Rp& __r)
- {
- if (__state_ == nullptr)
- __throw_future_error(future_errc::no_state);
- __state_->set_value_at_thread_exit(__r);
- }
- template <class _Rp>
- void
- promise<_Rp>::set_value_at_thread_exit(_Rp&& __r)
- {
- if (__state_ == nullptr)
- __throw_future_error(future_errc::no_state);
- __state_->set_value_at_thread_exit(_VSTD::move(__r));
- }
- template <class _Rp>
- void
- promise<_Rp>::set_exception_at_thread_exit(exception_ptr __p)
- {
- _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception_at_thread_exit: received nullptr" );
- if (__state_ == nullptr)
- __throw_future_error(future_errc::no_state);
- __state_->set_exception_at_thread_exit(__p);
- }
- // promise<R&>
- template <class _Rp>
- class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE promise<_Rp&>
- {
- __assoc_state<_Rp&>* __state_;
- _LIBCPP_INLINE_VISIBILITY
- explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
- template <class> friend class packaged_task;
- public:
- promise();
- template <class _Allocator>
- promise(allocator_arg_t, const _Allocator& __a);
- _LIBCPP_INLINE_VISIBILITY
- promise(promise&& __rhs) _NOEXCEPT
- : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
- promise(const promise& __rhs) = delete;
- ~promise();
- // assignment
- _LIBCPP_INLINE_VISIBILITY
- promise& operator=(promise&& __rhs) _NOEXCEPT
- {
- promise(_VSTD::move(__rhs)).swap(*this);
- return *this;
- }
- promise& operator=(const promise& __rhs) = delete;
- _LIBCPP_INLINE_VISIBILITY
- void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
- // retrieving the result
- future<_Rp&> get_future();
- // setting the result
- void set_value(_Rp& __r);
- void set_exception(exception_ptr __p);
- // setting the result with deferred notification
- void set_value_at_thread_exit(_Rp&);
- void set_exception_at_thread_exit(exception_ptr __p);
- };
- template <class _Rp>
- promise<_Rp&>::promise()
- : __state_(new __assoc_state<_Rp&>)
- {
- }
- template <class _Rp>
- template <class _Alloc>
- promise<_Rp&>::promise(allocator_arg_t, const _Alloc& __a0)
- {
- typedef __assoc_state_alloc<_Rp&, _Alloc> _State;
- typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
- typedef __allocator_destructor<_A2> _D2;
- _A2 __a(__a0);
- unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
- ::new ((void*)_VSTD::addressof(*__hold.get())) _State(__a0);
- __state_ = _VSTD::addressof(*__hold.release());
- }
- template <class _Rp>
- promise<_Rp&>::~promise()
- {
- if (__state_)
- {
- if (!__state_->__has_value() && __state_->use_count() > 1)
- __state_->set_exception(make_exception_ptr(
- future_error(make_error_code(future_errc::broken_promise))
- ));
- __state_->__release_shared();
- }
- }
- template <class _Rp>
- future<_Rp&>
- promise<_Rp&>::get_future()
- {
- if (__state_ == nullptr)
- __throw_future_error(future_errc::no_state);
- return future<_Rp&>(__state_);
- }
- template <class _Rp>
- void
- promise<_Rp&>::set_value(_Rp& __r)
- {
- if (__state_ == nullptr)
- __throw_future_error(future_errc::no_state);
- __state_->set_value(__r);
- }
- template <class _Rp>
- void
- promise<_Rp&>::set_exception(exception_ptr __p)
- {
- _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception: received nullptr" );
- if (__state_ == nullptr)
- __throw_future_error(future_errc::no_state);
- __state_->set_exception(__p);
- }
- template <class _Rp>
- void
- promise<_Rp&>::set_value_at_thread_exit(_Rp& __r)
- {
- if (__state_ == nullptr)
- __throw_future_error(future_errc::no_state);
- __state_->set_value_at_thread_exit(__r);
- }
- template <class _Rp>
- void
- promise<_Rp&>::set_exception_at_thread_exit(exception_ptr __p)
- {
- _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception_at_thread_exit: received nullptr" );
- if (__state_ == nullptr)
- __throw_future_error(future_errc::no_state);
- __state_->set_exception_at_thread_exit(__p);
- }
- // promise<void>
- template <>
- class _LIBCPP_TYPE_VIS _LIBCPP_AVAILABILITY_FUTURE promise<void>
- {
- __assoc_sub_state* __state_;
- _LIBCPP_INLINE_VISIBILITY
- explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
- template <class> friend class packaged_task;
- public:
- promise();
- template <class _Allocator>
- _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
- promise(allocator_arg_t, const _Allocator& __a);
- _LIBCPP_INLINE_VISIBILITY
- promise(promise&& __rhs) _NOEXCEPT
- : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
- promise(const promise& __rhs) = delete;
- ~promise();
- // assignment
- _LIBCPP_INLINE_VISIBILITY
- promise& operator=(promise&& __rhs) _NOEXCEPT
- {
- promise(_VSTD::move(__rhs)).swap(*this);
- return *this;
- }
- promise& operator=(const promise& __rhs) = delete;
- _LIBCPP_INLINE_VISIBILITY
- void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
- // retrieving the result
- future<void> get_future();
- // setting the result
- void set_value();
- void set_exception(exception_ptr __p);
- // setting the result with deferred notification
- void set_value_at_thread_exit();
- void set_exception_at_thread_exit(exception_ptr __p);
- };
- template <class _Alloc>
- promise<void>::promise(allocator_arg_t, const _Alloc& __a0)
- {
- typedef __assoc_sub_state_alloc<_Alloc> _State;
- typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
- typedef __allocator_destructor<_A2> _D2;
- _A2 __a(__a0);
- unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
- ::new ((void*)_VSTD::addressof(*__hold.get())) _State(__a0);
- __state_ = _VSTD::addressof(*__hold.release());
- }
- template <class _Rp>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- swap(promise<_Rp>& __x, promise<_Rp>& __y) _NOEXCEPT
- {
- __x.swap(__y);
- }
- template <class _Rp, class _Alloc>
- struct _LIBCPP_TEMPLATE_VIS uses_allocator<promise<_Rp>, _Alloc>
- : public true_type {};
- // packaged_task
- template<class _Fp> class __packaged_task_base;
- template<class _Rp, class ..._ArgTypes>
- class _LIBCPP_AVAILABILITY_FUTURE __packaged_task_base<_Rp(_ArgTypes...)>
- {
- __packaged_task_base(const __packaged_task_base&);
- __packaged_task_base& operator=(const __packaged_task_base&);
- public:
- _LIBCPP_INLINE_VISIBILITY
- __packaged_task_base() {}
- _LIBCPP_HIDE_FROM_ABI_VIRTUAL
- virtual ~__packaged_task_base() {}
- virtual void __move_to(__packaged_task_base*) _NOEXCEPT = 0;
- virtual void destroy() = 0;
- virtual void destroy_deallocate() = 0;
- virtual _Rp operator()(_ArgTypes&& ...) = 0;
- };
- template<class _FD, class _Alloc, class _FB> class __packaged_task_func;
- template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
- class _LIBCPP_AVAILABILITY_FUTURE __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>
- : public __packaged_task_base<_Rp(_ArgTypes...)>
- {
- __compressed_pair<_Fp, _Alloc> __f_;
- public:
- _LIBCPP_INLINE_VISIBILITY
- explicit __packaged_task_func(const _Fp& __f) : __f_(__f, __default_init_tag()) {}
- _LIBCPP_INLINE_VISIBILITY
- explicit __packaged_task_func(_Fp&& __f) : __f_(_VSTD::move(__f), __default_init_tag()) {}
- _LIBCPP_INLINE_VISIBILITY
- __packaged_task_func(const _Fp& __f, const _Alloc& __a)
- : __f_(__f, __a) {}
- _LIBCPP_INLINE_VISIBILITY
- __packaged_task_func(_Fp&& __f, const _Alloc& __a)
- : __f_(_VSTD::move(__f), __a) {}
- virtual void __move_to(__packaged_task_base<_Rp(_ArgTypes...)>*) _NOEXCEPT;
- virtual void destroy();
- virtual void destroy_deallocate();
- virtual _Rp operator()(_ArgTypes&& ... __args);
- };
- template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
- void
- __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__move_to(
- __packaged_task_base<_Rp(_ArgTypes...)>* __p) _NOEXCEPT
- {
- ::new ((void*)__p) __packaged_task_func(_VSTD::move(__f_.first()), _VSTD::move(__f_.second()));
- }
- template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
- void
- __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy()
- {
- __f_.~__compressed_pair<_Fp, _Alloc>();
- }
- template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
- void
- __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate()
- {
- typedef typename __allocator_traits_rebind<_Alloc, __packaged_task_func>::type _Ap;
- typedef allocator_traits<_Ap> _ATraits;
- typedef pointer_traits<typename _ATraits::pointer> _PTraits;
- _Ap __a(__f_.second());
- __f_.~__compressed_pair<_Fp, _Alloc>();
- __a.deallocate(_PTraits::pointer_to(*this), 1);
- }
- template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
- _Rp
- __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
- {
- return _VSTD::__invoke(__f_.first(), _VSTD::forward<_ArgTypes>(__arg)...);
- }
- template <class _Callable> class __packaged_task_function;
- template<class _Rp, class ..._ArgTypes>
- class _LIBCPP_AVAILABILITY_FUTURE __packaged_task_function<_Rp(_ArgTypes...)>
- {
- typedef __packaged_task_base<_Rp(_ArgTypes...)> __base;
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_NO_CFI
- __base* __get_buf() { return (__base*)&__buf_; }
- _LIBCPP_SUPPRESS_DEPRECATED_PUSH
- typename aligned_storage<3*sizeof(void*)>::type __buf_;
- _LIBCPP_SUPPRESS_DEPRECATED_POP
- __base* __f_;
- public:
- typedef _Rp result_type;
- // construct/copy/destroy:
- _LIBCPP_INLINE_VISIBILITY
- __packaged_task_function() _NOEXCEPT : __f_(nullptr) {}
- template<class _Fp>
- __packaged_task_function(_Fp&& __f);
- template<class _Fp, class _Alloc>
- __packaged_task_function(allocator_arg_t, const _Alloc& __a, _Fp&& __f);
- __packaged_task_function(__packaged_task_function&&) _NOEXCEPT;
- __packaged_task_function& operator=(__packaged_task_function&&) _NOEXCEPT;
- __packaged_task_function(const __packaged_task_function&) = delete;
- __packaged_task_function& operator=(const __packaged_task_function&) = delete;
- ~__packaged_task_function();
- void swap(__packaged_task_function&) _NOEXCEPT;
- _LIBCPP_INLINE_VISIBILITY
- _Rp operator()(_ArgTypes...) const;
- };
- template<class _Rp, class ..._ArgTypes>
- __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f) _NOEXCEPT
- {
- if (__f.__f_ == nullptr)
- __f_ = nullptr;
- else if (__f.__f_ == __f.__get_buf())
- {
- __f.__f_->__move_to(__get_buf());
- __f_ = (__base*)&__buf_;
- }
- else
- {
- __f_ = __f.__f_;
- __f.__f_ = nullptr;
- }
- }
- template<class _Rp, class ..._ArgTypes>
- template <class _Fp>
- __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(_Fp&& __f)
- : __f_(nullptr)
- {
- typedef __libcpp_remove_reference_t<typename decay<_Fp>::type> _FR;
- typedef __packaged_task_func<_FR, allocator<_FR>, _Rp(_ArgTypes...)> _FF;
- if (sizeof(_FF) <= sizeof(__buf_))
- {
- ::new ((void*)&__buf_) _FF(_VSTD::forward<_Fp>(__f));
- __f_ = (__base*)&__buf_;
- }
- else
- {
- typedef allocator<_FF> _Ap;
- _Ap __a;
- typedef __allocator_destructor<_Ap> _Dp;
- unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
- ::new ((void*)__hold.get()) _FF(_VSTD::forward<_Fp>(__f), allocator<_FR>(__a));
- __f_ = __hold.release();
- }
- }
- template<class _Rp, class ..._ArgTypes>
- template <class _Fp, class _Alloc>
- __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(
- allocator_arg_t, const _Alloc& __a0, _Fp&& __f)
- : __f_(nullptr)
- {
- typedef __libcpp_remove_reference_t<typename decay<_Fp>::type> _FR;
- typedef __packaged_task_func<_FR, _Alloc, _Rp(_ArgTypes...)> _FF;
- if (sizeof(_FF) <= sizeof(__buf_))
- {
- __f_ = (__base*)&__buf_;
- ::new ((void*)__f_) _FF(_VSTD::forward<_Fp>(__f));
- }
- else
- {
- typedef typename __allocator_traits_rebind<_Alloc, _FF>::type _Ap;
- _Ap __a(__a0);
- typedef __allocator_destructor<_Ap> _Dp;
- unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
- ::new ((void*)_VSTD::addressof(*__hold.get()))
- _FF(_VSTD::forward<_Fp>(__f), _Alloc(__a));
- __f_ = _VSTD::addressof(*__hold.release());
- }
- }
- template<class _Rp, class ..._ArgTypes>
- __packaged_task_function<_Rp(_ArgTypes...)>&
- __packaged_task_function<_Rp(_ArgTypes...)>::operator=(__packaged_task_function&& __f) _NOEXCEPT
- {
- if (__f_ == __get_buf())
- __f_->destroy();
- else if (__f_)
- __f_->destroy_deallocate();
- __f_ = nullptr;
- if (__f.__f_ == nullptr)
- __f_ = nullptr;
- else if (__f.__f_ == __f.__get_buf())
- {
- __f.__f_->__move_to(__get_buf());
- __f_ = __get_buf();
- }
- else
- {
- __f_ = __f.__f_;
- __f.__f_ = nullptr;
- }
- return *this;
- }
- template<class _Rp, class ..._ArgTypes>
- __packaged_task_function<_Rp(_ArgTypes...)>::~__packaged_task_function()
- {
- if (__f_ == __get_buf())
- __f_->destroy();
- else if (__f_)
- __f_->destroy_deallocate();
- }
- template<class _Rp, class ..._ArgTypes>
- _LIBCPP_NO_CFI
- void
- __packaged_task_function<_Rp(_ArgTypes...)>::swap(__packaged_task_function& __f) _NOEXCEPT
- {
- if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
- {
- _LIBCPP_SUPPRESS_DEPRECATED_PUSH
- typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
- _LIBCPP_SUPPRESS_DEPRECATED_POP
- __base* __t = (__base*)&__tempbuf;
- __f_->__move_to(__t);
- __f_->destroy();
- __f_ = nullptr;
- __f.__f_->__move_to((__base*)&__buf_);
- __f.__f_->destroy();
- __f.__f_ = nullptr;
- __f_ = (__base*)&__buf_;
- __t->__move_to((__base*)&__f.__buf_);
- __t->destroy();
- __f.__f_ = (__base*)&__f.__buf_;
- }
- else if (__f_ == (__base*)&__buf_)
- {
- __f_->__move_to((__base*)&__f.__buf_);
- __f_->destroy();
- __f_ = __f.__f_;
- __f.__f_ = (__base*)&__f.__buf_;
- }
- else if (__f.__f_ == (__base*)&__f.__buf_)
- {
- __f.__f_->__move_to((__base*)&__buf_);
- __f.__f_->destroy();
- __f.__f_ = __f_;
- __f_ = (__base*)&__buf_;
- }
- else
- _VSTD::swap(__f_, __f.__f_);
- }
- template<class _Rp, class ..._ArgTypes>
- inline
- _Rp
- __packaged_task_function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
- {
- return (*__f_)(_VSTD::forward<_ArgTypes>(__arg)...);
- }
- template<class _Rp, class ..._ArgTypes>
- class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE packaged_task<_Rp(_ArgTypes...)>
- {
- public:
- typedef _Rp result_type; // extension
- private:
- __packaged_task_function<result_type(_ArgTypes...)> __f_;
- promise<result_type> __p_;
- public:
- // construction and destruction
- _LIBCPP_INLINE_VISIBILITY
- packaged_task() _NOEXCEPT : __p_(nullptr) {}
- template <class _Fp,
- class = __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value> >
- _LIBCPP_INLINE_VISIBILITY
- explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
- template <class _Fp, class _Allocator,
- class = __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value> >
- _LIBCPP_INLINE_VISIBILITY
- packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
- : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)),
- __p_(allocator_arg, __a) {}
- // ~packaged_task() = default;
- // no copy
- packaged_task(const packaged_task&) = delete;
- packaged_task& operator=(const packaged_task&) = delete;
- // move support
- _LIBCPP_INLINE_VISIBILITY
- packaged_task(packaged_task&& __other) _NOEXCEPT
- : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
- _LIBCPP_INLINE_VISIBILITY
- packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
- {
- __f_ = _VSTD::move(__other.__f_);
- __p_ = _VSTD::move(__other.__p_);
- return *this;
- }
- _LIBCPP_INLINE_VISIBILITY
- void swap(packaged_task& __other) _NOEXCEPT
- {
- __f_.swap(__other.__f_);
- __p_.swap(__other.__p_);
- }
- _LIBCPP_INLINE_VISIBILITY
- bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
- // result retrieval
- _LIBCPP_INLINE_VISIBILITY
- future<result_type> get_future() {return __p_.get_future();}
- // execution
- void operator()(_ArgTypes... __args);
- void make_ready_at_thread_exit(_ArgTypes... __args);
- void reset();
- };
- template<class _Rp, class ..._ArgTypes>
- void
- packaged_task<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __args)
- {
- if (__p_.__state_ == nullptr)
- __throw_future_error(future_errc::no_state);
- if (__p_.__state_->__has_value())
- __throw_future_error(future_errc::promise_already_satisfied);
- #ifndef _LIBCPP_NO_EXCEPTIONS
- try
- {
- #endif // _LIBCPP_NO_EXCEPTIONS
- __p_.set_value(__f_(_VSTD::forward<_ArgTypes>(__args)...));
- #ifndef _LIBCPP_NO_EXCEPTIONS
- }
- catch (...)
- {
- __p_.set_exception(current_exception());
- }
- #endif // _LIBCPP_NO_EXCEPTIONS
- }
- template<class _Rp, class ..._ArgTypes>
- void
- packaged_task<_Rp(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
- {
- if (__p_.__state_ == nullptr)
- __throw_future_error(future_errc::no_state);
- if (__p_.__state_->__has_value())
- __throw_future_error(future_errc::promise_already_satisfied);
- #ifndef _LIBCPP_NO_EXCEPTIONS
- try
- {
- #endif // _LIBCPP_NO_EXCEPTIONS
- __p_.set_value_at_thread_exit(__f_(_VSTD::forward<_ArgTypes>(__args)...));
- #ifndef _LIBCPP_NO_EXCEPTIONS
- }
- catch (...)
- {
- __p_.set_exception_at_thread_exit(current_exception());
- }
- #endif // _LIBCPP_NO_EXCEPTIONS
- }
- template<class _Rp, class ..._ArgTypes>
- void
- packaged_task<_Rp(_ArgTypes...)>::reset()
- {
- if (!valid())
- __throw_future_error(future_errc::no_state);
- __p_ = promise<result_type>();
- }
- template<class ..._ArgTypes>
- class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE packaged_task<void(_ArgTypes...)>
- {
- public:
- typedef void result_type; // extension
- private:
- __packaged_task_function<result_type(_ArgTypes...)> __f_;
- promise<result_type> __p_;
- public:
- // construction and destruction
- _LIBCPP_INLINE_VISIBILITY
- packaged_task() _NOEXCEPT : __p_(nullptr) {}
- template <class _Fp,
- class = __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value> >
- _LIBCPP_INLINE_VISIBILITY
- explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
- template <class _Fp, class _Allocator,
- class = __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value> >
- _LIBCPP_INLINE_VISIBILITY
- packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
- : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)),
- __p_(allocator_arg, __a) {}
- // ~packaged_task() = default;
- // no copy
- packaged_task(const packaged_task&) = delete;
- packaged_task& operator=(const packaged_task&) = delete;
- // move support
- _LIBCPP_INLINE_VISIBILITY
- packaged_task(packaged_task&& __other) _NOEXCEPT
- : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
- _LIBCPP_INLINE_VISIBILITY
- packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
- {
- __f_ = _VSTD::move(__other.__f_);
- __p_ = _VSTD::move(__other.__p_);
- return *this;
- }
- _LIBCPP_INLINE_VISIBILITY
- void swap(packaged_task& __other) _NOEXCEPT
- {
- __f_.swap(__other.__f_);
- __p_.swap(__other.__p_);
- }
- _LIBCPP_INLINE_VISIBILITY
- bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
- // result retrieval
- _LIBCPP_INLINE_VISIBILITY
- future<result_type> get_future() {return __p_.get_future();}
- // execution
- void operator()(_ArgTypes... __args);
- void make_ready_at_thread_exit(_ArgTypes... __args);
- void reset();
- };
- #if _LIBCPP_STD_VER >= 17
- template <class _Rp, class... _Args>
- packaged_task(_Rp(*)(_Args...)) -> packaged_task<_Rp(_Args...)>;
- template <class _Fp, class _Stripped = typename __strip_signature<decltype(&_Fp::operator())>::type>
- packaged_task(_Fp) -> packaged_task<_Stripped>;
- #endif
- template<class ..._ArgTypes>
- void
- packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args)
- {
- if (__p_.__state_ == nullptr)
- __throw_future_error(future_errc::no_state);
- if (__p_.__state_->__has_value())
- __throw_future_error(future_errc::promise_already_satisfied);
- #ifndef _LIBCPP_NO_EXCEPTIONS
- try
- {
- #endif // _LIBCPP_NO_EXCEPTIONS
- __f_(_VSTD::forward<_ArgTypes>(__args)...);
- __p_.set_value();
- #ifndef _LIBCPP_NO_EXCEPTIONS
- }
- catch (...)
- {
- __p_.set_exception(current_exception());
- }
- #endif // _LIBCPP_NO_EXCEPTIONS
- }
- template<class ..._ArgTypes>
- void
- packaged_task<void(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
- {
- if (__p_.__state_ == nullptr)
- __throw_future_error(future_errc::no_state);
- if (__p_.__state_->__has_value())
- __throw_future_error(future_errc::promise_already_satisfied);
- #ifndef _LIBCPP_NO_EXCEPTIONS
- try
- {
- #endif // _LIBCPP_NO_EXCEPTIONS
- __f_(_VSTD::forward<_ArgTypes>(__args)...);
- __p_.set_value_at_thread_exit();
- #ifndef _LIBCPP_NO_EXCEPTIONS
- }
- catch (...)
- {
- __p_.set_exception_at_thread_exit(current_exception());
- }
- #endif // _LIBCPP_NO_EXCEPTIONS
- }
- template<class ..._ArgTypes>
- void
- packaged_task<void(_ArgTypes...)>::reset()
- {
- if (!valid())
- __throw_future_error(future_errc::no_state);
- __p_ = promise<result_type>();
- }
- template <class _Rp, class... _ArgTypes>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- swap(packaged_task<_Rp(_ArgTypes...)>& __x, packaged_task<_Rp(_ArgTypes...)>& __y) _NOEXCEPT
- {
- __x.swap(__y);
- }
- template <class _Callable, class _Alloc>
- struct _LIBCPP_TEMPLATE_VIS uses_allocator<packaged_task<_Callable>, _Alloc>
- : public true_type {};
- template <class _Rp, class _Fp>
- _LIBCPP_INLINE_VISIBILITY future<_Rp>
- __make_deferred_assoc_state(_Fp&& __f)
- {
- unique_ptr<__deferred_assoc_state<_Rp, _Fp>, __release_shared_count>
- __h(new __deferred_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
- return future<_Rp>(__h.get());
- }
- template <class _Rp, class _Fp>
- _LIBCPP_INLINE_VISIBILITY future<_Rp>
- __make_async_assoc_state(_Fp&& __f)
- {
- unique_ptr<__async_assoc_state<_Rp, _Fp>, __release_shared_count>
- __h(new __async_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
- _VSTD::thread(&__async_assoc_state<_Rp, _Fp>::__execute, __h.get()).detach();
- return future<_Rp>(__h.get());
- }
- #ifndef _LIBCPP_CXX03_LANG
- template <class _Fp, class... _Args>
- class _LIBCPP_HIDDEN __async_func
- {
- tuple<_Fp, _Args...> __f_;
- public:
- typedef typename __invoke_of<_Fp, _Args...>::type _Rp;
- _LIBCPP_INLINE_VISIBILITY
- explicit __async_func(_Fp&& __f, _Args&&... __args)
- : __f_(_VSTD::move(__f), _VSTD::move(__args)...) {}
- _LIBCPP_INLINE_VISIBILITY
- __async_func(__async_func&& __f) : __f_(_VSTD::move(__f.__f_)) {}
- _Rp operator()()
- {
- typedef typename __make_tuple_indices<1+sizeof...(_Args), 1>::type _Index;
- return __execute(_Index());
- }
- private:
- template <size_t ..._Indices>
- _Rp
- __execute(__tuple_indices<_Indices...>)
- {
- return _VSTD::__invoke(_VSTD::move(_VSTD::get<0>(__f_)), _VSTD::move(_VSTD::get<_Indices>(__f_))...);
- }
- };
- inline _LIBCPP_INLINE_VISIBILITY bool __does_policy_contain(launch __policy, launch __value )
- { return (int(__policy) & int(__value)) != 0; }
- template <class _Fp, class... _Args>
- _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
- future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
- async(launch __policy, _Fp&& __f, _Args&&... __args)
- {
- typedef __async_func<typename decay<_Fp>::type, typename decay<_Args>::type...> _BF;
- typedef typename _BF::_Rp _Rp;
- #ifndef _LIBCPP_NO_EXCEPTIONS
- try
- {
- #endif
- if (__does_policy_contain(__policy, launch::async))
- return _VSTD::__make_async_assoc_state<_Rp>(_BF(_LIBCPP_AUTO_CAST(_VSTD::forward<_Fp>(__f)),
- _LIBCPP_AUTO_CAST(_VSTD::forward<_Args>(__args))...));
- #ifndef _LIBCPP_NO_EXCEPTIONS
- }
- catch ( ... ) { if (__policy == launch::async) throw ; }
- #endif
- if (__does_policy_contain(__policy, launch::deferred))
- return _VSTD::__make_deferred_assoc_state<_Rp>(_BF(_LIBCPP_AUTO_CAST(_VSTD::forward<_Fp>(__f)),
- _LIBCPP_AUTO_CAST(_VSTD::forward<_Args>(__args))...));
- return future<_Rp>{};
- }
- template <class _Fp, class... _Args>
- _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY
- future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
- async(_Fp&& __f, _Args&&... __args)
- {
- return _VSTD::async(launch::any, _VSTD::forward<_Fp>(__f),
- _VSTD::forward<_Args>(__args)...);
- }
- #endif // C++03
- // shared_future
- template <class _Rp>
- class _LIBCPP_TEMPLATE_VIS shared_future
- {
- __assoc_state<_Rp>* __state_;
- public:
- _LIBCPP_INLINE_VISIBILITY
- shared_future() _NOEXCEPT : __state_(nullptr) {}
- _LIBCPP_INLINE_VISIBILITY
- shared_future(const shared_future& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
- {if (__state_) __state_->__add_shared();}
- _LIBCPP_INLINE_VISIBILITY
- shared_future(future<_Rp>&& __f) _NOEXCEPT : __state_(__f.__state_)
- {__f.__state_ = nullptr;}
- _LIBCPP_INLINE_VISIBILITY
- shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
- {__rhs.__state_ = nullptr;}
- ~shared_future();
- shared_future& operator=(const shared_future& __rhs) _NOEXCEPT;
- _LIBCPP_INLINE_VISIBILITY
- shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
- {
- shared_future(_VSTD::move(__rhs)).swap(*this);
- return *this;
- }
- // retrieving the value
- _LIBCPP_INLINE_VISIBILITY
- const _Rp& get() const {return __state_->copy();}
- _LIBCPP_INLINE_VISIBILITY
- void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
- // functions to check state
- _LIBCPP_INLINE_VISIBILITY
- bool valid() const _NOEXCEPT {return __state_ != nullptr;}
- _LIBCPP_INLINE_VISIBILITY
- void wait() const {__state_->wait();}
- template <class _Rep, class _Period>
- _LIBCPP_INLINE_VISIBILITY
- future_status
- wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
- {return __state_->wait_for(__rel_time);}
- template <class _Clock, class _Duration>
- _LIBCPP_INLINE_VISIBILITY
- future_status
- wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
- {return __state_->wait_until(__abs_time);}
- };
- template <class _Rp>
- shared_future<_Rp>::~shared_future()
- {
- if (__state_)
- __state_->__release_shared();
- }
- template <class _Rp>
- shared_future<_Rp>&
- shared_future<_Rp>::operator=(const shared_future& __rhs) _NOEXCEPT
- {
- if (__rhs.__state_)
- __rhs.__state_->__add_shared();
- if (__state_)
- __state_->__release_shared();
- __state_ = __rhs.__state_;
- return *this;
- }
- template <class _Rp>
- class _LIBCPP_TEMPLATE_VIS shared_future<_Rp&>
- {
- __assoc_state<_Rp&>* __state_;
- public:
- _LIBCPP_INLINE_VISIBILITY
- shared_future() _NOEXCEPT : __state_(nullptr) {}
- _LIBCPP_INLINE_VISIBILITY
- shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
- {if (__state_) __state_->__add_shared();}
- _LIBCPP_INLINE_VISIBILITY
- shared_future(future<_Rp&>&& __f) _NOEXCEPT : __state_(__f.__state_)
- {__f.__state_ = nullptr;}
- _LIBCPP_INLINE_VISIBILITY
- shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
- {__rhs.__state_ = nullptr;}
- ~shared_future();
- shared_future& operator=(const shared_future& __rhs);
- _LIBCPP_INLINE_VISIBILITY
- shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
- {
- shared_future(_VSTD::move(__rhs)).swap(*this);
- return *this;
- }
- // retrieving the value
- _LIBCPP_INLINE_VISIBILITY
- _Rp& get() const {return __state_->copy();}
- _LIBCPP_INLINE_VISIBILITY
- void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
- // functions to check state
- _LIBCPP_INLINE_VISIBILITY
- bool valid() const _NOEXCEPT {return __state_ != nullptr;}
- _LIBCPP_INLINE_VISIBILITY
- void wait() const {__state_->wait();}
- template <class _Rep, class _Period>
- _LIBCPP_INLINE_VISIBILITY
- future_status
- wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
- {return __state_->wait_for(__rel_time);}
- template <class _Clock, class _Duration>
- _LIBCPP_INLINE_VISIBILITY
- future_status
- wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
- {return __state_->wait_until(__abs_time);}
- };
- template <class _Rp>
- shared_future<_Rp&>::~shared_future()
- {
- if (__state_)
- __state_->__release_shared();
- }
- template <class _Rp>
- shared_future<_Rp&>&
- shared_future<_Rp&>::operator=(const shared_future& __rhs)
- {
- if (__rhs.__state_)
- __rhs.__state_->__add_shared();
- if (__state_)
- __state_->__release_shared();
- __state_ = __rhs.__state_;
- return *this;
- }
- template <>
- class _LIBCPP_TYPE_VIS _LIBCPP_AVAILABILITY_FUTURE shared_future<void>
- {
- __assoc_sub_state* __state_;
- public:
- _LIBCPP_INLINE_VISIBILITY
- shared_future() _NOEXCEPT : __state_(nullptr) {}
- _LIBCPP_INLINE_VISIBILITY
- shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
- {if (__state_) __state_->__add_shared();}
- _LIBCPP_INLINE_VISIBILITY
- shared_future(future<void>&& __f) _NOEXCEPT : __state_(__f.__state_)
- {__f.__state_ = nullptr;}
- _LIBCPP_INLINE_VISIBILITY
- shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
- {__rhs.__state_ = nullptr;}
- ~shared_future();
- shared_future& operator=(const shared_future& __rhs);
- _LIBCPP_INLINE_VISIBILITY
- shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
- {
- shared_future(_VSTD::move(__rhs)).swap(*this);
- return *this;
- }
- // retrieving the value
- _LIBCPP_INLINE_VISIBILITY
- void get() const {__state_->copy();}
- _LIBCPP_INLINE_VISIBILITY
- void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
- // functions to check state
- _LIBCPP_INLINE_VISIBILITY
- bool valid() const _NOEXCEPT {return __state_ != nullptr;}
- _LIBCPP_INLINE_VISIBILITY
- void wait() const {__state_->wait();}
- template <class _Rep, class _Period>
- _LIBCPP_INLINE_VISIBILITY
- future_status
- wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
- {return __state_->wait_for(__rel_time);}
- template <class _Clock, class _Duration>
- _LIBCPP_INLINE_VISIBILITY
- future_status
- wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
- {return __state_->wait_until(__abs_time);}
- };
- template <class _Rp>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- swap(shared_future<_Rp>& __x, shared_future<_Rp>& __y) _NOEXCEPT
- {
- __x.swap(__y);
- }
- template <class _Rp>
- inline
- shared_future<_Rp>
- future<_Rp>::share() _NOEXCEPT
- {
- return shared_future<_Rp>(_VSTD::move(*this));
- }
- template <class _Rp>
- inline
- shared_future<_Rp&>
- future<_Rp&>::share() _NOEXCEPT
- {
- return shared_future<_Rp&>(_VSTD::move(*this));
- }
- inline
- shared_future<void>
- future<void>::share() _NOEXCEPT
- {
- return shared_future<void>(_VSTD::move(*this));
- }
- _LIBCPP_END_NAMESPACE_STD
- #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 17
- # include <chrono>
- #endif
- #endif // _LIBCPP_FUTURE
|