1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482 |
- // -*- 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:
- explicit future_error(future_errc e); // since C++17
- const error_code& code() const noexcept;
- const char* what() const noexcept;
- private:
- error_code ec_; // exposition only
- };
- 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 <__exception/exception_ptr.h>
- #include <__memory/addressof.h>
- #include <__memory/allocator.h>
- #include <__memory/allocator_arg_t.h>
- #include <__memory/allocator_destructor.h>
- #include <__memory/allocator_traits.h>
- #include <__memory/compressed_pair.h>
- #include <__memory/pointer_traits.h>
- #include <__memory/shared_ptr.h>
- #include <__memory/unique_ptr.h>
- #include <__memory/uses_allocator.h>
- #include <__system_error/error_category.h>
- #include <__system_error/error_code.h>
- #include <__system_error/error_condition.h>
- #include <__type_traits/aligned_storage.h>
- #include <__type_traits/alignment_of.h>
- #include <__type_traits/strip_signature.h>
- #include <__utility/auto_cast.h>
- #include <__utility/forward.h>
- #include <__utility/move.h>
- #include <mutex>
- #include <new>
- #include <stdexcept>
- #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_EXPORTED_FROM_ABI 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());
- }
- _LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI
- #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
- _LIBCPP_AVAILABILITY_FUTURE_ERROR
- #endif
- void __throw_future_error(future_errc __ev);
- class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_AVAILABILITY_FUTURE_ERROR future_error
- : public logic_error
- {
- error_code __ec_;
- future_error(error_code);
- friend void __throw_future_error(future_errc);
- template <class> friend class promise;
- public:
- #if _LIBCPP_STD_VER >= 17
- _LIBCPP_HIDE_FROM_ABI explicit future_error(future_errc __ec) : future_error(std::make_error_code(__ec)) {}
- #endif
- _LIBCPP_INLINE_VISIBILITY
- const error_code& code() const _NOEXCEPT {return __ec_;}
- _LIBCPP_HIDE_FROM_ABI future_error(const future_error&) _NOEXCEPT = default;
- ~future_error() _NOEXCEPT override;
- };
- // Declared above std::future_error
- void __throw_future_error(future_errc __ev)
- {
- #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
- throw future_error(make_error_code(__ev));
- #else
- (void)__ev;
- _LIBCPP_VERBOSE_ABORT("future_error was thrown in -fno-exceptions mode");
- #endif
- }
- class _LIBCPP_EXPORTED_FROM_ABI _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_;
- _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
- public:
- template <class _Arg>
- _LIBCPP_HIDE_FROM_ABI void set_value(_Arg&& __arg);
- template <class _Arg>
- _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Arg&& __arg);
- _LIBCPP_HIDE_FROM_ABI _Rp move();
- _LIBCPP_HIDE_FROM_ABI __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_;
- _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
- public:
- _LIBCPP_HIDE_FROM_ABI void set_value(_Rp& __arg);
- _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp& __arg);
- _LIBCPP_HIDE_FROM_ABI _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_;
- _LIBCPP_HIDE_FROM_ABI_VIRTUAL 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_;
- _LIBCPP_HIDE_FROM_ABI_VIRTUAL 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_;
- _LIBCPP_HIDE_FROM_ABI_VIRTUAL 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);
- _LIBCPP_HIDE_FROM_ABI_VIRTUAL 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_HAS_NO_EXCEPTIONS
- try
- {
- #endif // _LIBCPP_HAS_NO_EXCEPTIONS
- this->set_value(__func_());
- #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
- }
- catch (...)
- {
- this->set_exception(current_exception());
- }
- #endif // _LIBCPP_HAS_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);
- _LIBCPP_HIDE_FROM_ABI_VIRTUAL 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_HAS_NO_EXCEPTIONS
- try
- {
- #endif // _LIBCPP_HAS_NO_EXCEPTIONS
- __func_();
- this->set_value();
- #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
- }
- catch (...)
- {
- this->set_exception(current_exception());
- }
- #endif // _LIBCPP_HAS_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_;
- _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __on_zero_shared() _NOEXCEPT;
- public:
- _LIBCPP_INLINE_VISIBILITY
- explicit __async_assoc_state(_Fp&& __f);
- _LIBCPP_HIDE_FROM_ABI_VIRTUAL 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_HAS_NO_EXCEPTIONS
- try
- {
- #endif // _LIBCPP_HAS_NO_EXCEPTIONS
- this->set_value(__func_());
- #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
- }
- catch (...)
- {
- this->set_exception(current_exception());
- }
- #endif // _LIBCPP_HAS_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_;
- _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
- public:
- _LIBCPP_INLINE_VISIBILITY
- explicit __async_assoc_state(_Fp&& __f);
- _LIBCPP_HIDE_FROM_ABI_VIRTUAL 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_HAS_NO_EXCEPTIONS
- try
- {
- #endif // _LIBCPP_HAS_NO_EXCEPTIONS
- __func_();
- this->set_value();
- #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
- }
- catch (...)
- {
- this->set_exception(current_exception());
- }
- #endif // _LIBCPP_HAS_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 _LIBCPP_HIDE_FROM_ABI 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;
- }
- _LIBCPP_HIDE_FROM_ABI ~future();
- _LIBCPP_INLINE_VISIBILITY
- shared_future<_Rp> share() _NOEXCEPT;
- // retrieving the value
- _LIBCPP_HIDE_FROM_ABI _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
- {
- _LIBCPP_HIDE_FROM_ABI 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> __guard(__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 _LIBCPP_HIDE_FROM_ABI 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;
- }
- _LIBCPP_HIDE_FROM_ABI ~future();
- _LIBCPP_INLINE_VISIBILITY
- shared_future<_Rp&> share() _NOEXCEPT;
- // retrieving the value
- _LIBCPP_HIDE_FROM_ABI _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> __guard(__state_);
- __assoc_state<_Rp&>* __s = __state_;
- __state_ = nullptr;
- return __s->copy();
- }
- template <>
- class _LIBCPP_EXPORTED_FROM_ABI _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:
- _LIBCPP_HIDE_FROM_ABI promise();
- template <class _Alloc>
- _LIBCPP_HIDE_FROM_ABI 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;
- _LIBCPP_HIDE_FROM_ABI ~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
- _LIBCPP_HIDE_FROM_ABI future<_Rp> get_future();
- // setting the result
- _LIBCPP_HIDE_FROM_ABI void set_value(const _Rp& __r);
- _LIBCPP_HIDE_FROM_ABI void set_value(_Rp&& __r);
- _LIBCPP_HIDE_FROM_ABI void set_exception(exception_ptr __p);
- // setting the result with deferred notification
- _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(const _Rp& __r);
- _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp&& __r);
- _LIBCPP_HIDE_FROM_ABI 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_UNCATEGORIZED( __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_UNCATEGORIZED( __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:
- _LIBCPP_HIDE_FROM_ABI promise();
- template <class _Allocator>
- _LIBCPP_HIDE_FROM_ABI 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;
- _LIBCPP_HIDE_FROM_ABI ~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
- _LIBCPP_HIDE_FROM_ABI future<_Rp&> get_future();
- // setting the result
- _LIBCPP_HIDE_FROM_ABI void set_value(_Rp& __r);
- _LIBCPP_HIDE_FROM_ABI void set_exception(exception_ptr __p);
- // setting the result with deferred notification
- _LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp&);
- _LIBCPP_HIDE_FROM_ABI 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_UNCATEGORIZED( __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_UNCATEGORIZED( __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_EXPORTED_FROM_ABI _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) {}
- _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __move_to(__packaged_task_base<_Rp(_ArgTypes...)>*) _NOEXCEPT;
- _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy();
- _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy_deallocate();
- _LIBCPP_HIDE_FROM_ABI_VIRTUAL 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>
- _LIBCPP_HIDE_FROM_ABI __packaged_task_function(_Fp&& __f);
- template<class _Fp, class _Alloc>
- _LIBCPP_HIDE_FROM_ABI __packaged_task_function(allocator_arg_t, const _Alloc& __a, _Fp&& __f);
- _LIBCPP_HIDE_FROM_ABI __packaged_task_function(__packaged_task_function&&) _NOEXCEPT;
- _LIBCPP_HIDE_FROM_ABI __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;
- _LIBCPP_HIDE_FROM_ABI ~__packaged_task_function();
- _LIBCPP_HIDE_FROM_ABI void swap(__packaged_task_function&) _NOEXCEPT;
- _LIBCPP_INLINE_VISIBILITY
- _LIBCPP_HIDE_FROM_ABI _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<__decay_t<_Fp> > _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<__decay_t<_Fp> > _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_t(), __a, _VSTD::forward<_Fp>(__f)),
- __p_(allocator_arg_t(), __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
- _LIBCPP_HIDE_FROM_ABI void operator()(_ArgTypes... __args);
- _LIBCPP_HIDE_FROM_ABI void make_ready_at_thread_exit(_ArgTypes... __args);
- _LIBCPP_HIDE_FROM_ABI 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_HAS_NO_EXCEPTIONS
- try
- {
- #endif // _LIBCPP_HAS_NO_EXCEPTIONS
- __p_.set_value(__f_(_VSTD::forward<_ArgTypes>(__args)...));
- #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
- }
- catch (...)
- {
- __p_.set_exception(current_exception());
- }
- #endif // _LIBCPP_HAS_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_HAS_NO_EXCEPTIONS
- try
- {
- #endif // _LIBCPP_HAS_NO_EXCEPTIONS
- __p_.set_value_at_thread_exit(__f_(_VSTD::forward<_ArgTypes>(__args)...));
- #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
- }
- catch (...)
- {
- __p_.set_exception_at_thread_exit(current_exception());
- }
- #endif // _LIBCPP_HAS_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_t(), __a, _VSTD::forward<_Fp>(__f)),
- __p_(allocator_arg_t(), __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
- _LIBCPP_HIDE_FROM_ABI void operator()(_ArgTypes... __args);
- _LIBCPP_HIDE_FROM_ABI void make_ready_at_thread_exit(_ArgTypes... __args);
- _LIBCPP_HIDE_FROM_ABI 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_HAS_NO_EXCEPTIONS
- try
- {
- #endif // _LIBCPP_HAS_NO_EXCEPTIONS
- __f_(_VSTD::forward<_ArgTypes>(__args)...);
- __p_.set_value();
- #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
- }
- catch (...)
- {
- __p_.set_exception(current_exception());
- }
- #endif // _LIBCPP_HAS_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_HAS_NO_EXCEPTIONS
- try
- {
- #endif // _LIBCPP_HAS_NO_EXCEPTIONS
- __f_(_VSTD::forward<_ArgTypes>(__args)...);
- __p_.set_value_at_thread_exit();
- #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
- }
- catch (...)
- {
- __p_.set_exception_at_thread_exit(current_exception());
- }
- #endif // _LIBCPP_HAS_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_)) {}
- _LIBCPP_HIDE_FROM_ABI _Rp operator()()
- {
- typedef typename __make_tuple_indices<1+sizeof...(_Args), 1>::type _Index;
- return __execute(_Index());
- }
- private:
- template <size_t ..._Indices>
- _LIBCPP_HIDE_FROM_ABI _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<__decay_t<_Fp>, __decay_t<_Args>...>::type>
- async(launch __policy, _Fp&& __f, _Args&&... __args)
- {
- typedef __async_func<__decay_t<_Fp>, __decay_t<_Args>...> _BF;
- typedef typename _BF::_Rp _Rp;
- #ifndef _LIBCPP_HAS_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_HAS_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<__decay_t<_Fp>, __decay_t<_Args>...>::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;}
- _LIBCPP_HIDE_FROM_ABI ~shared_future();
- _LIBCPP_HIDE_FROM_ABI 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;}
- _LIBCPP_HIDE_FROM_ABI ~shared_future();
- _LIBCPP_HIDE_FROM_ABI 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_EXPORTED_FROM_ABI _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
- #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
- # include <atomic>
- # include <cstdlib>
- # include <exception>
- # include <system_error>
- #endif
- #endif // _LIBCPP_FUTURE
|