xref: /llvm-project-15.0.7/libcxx/include/thread (revision bfbd73f8)
13e519524SHoward Hinnant// -*- C++ -*-
23e519524SHoward Hinnant//===--------------------------- thread -----------------------------------===//
33e519524SHoward Hinnant//
457b08b09SChandler Carruth// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
557b08b09SChandler Carruth// See https://llvm.org/LICENSE.txt for license information.
657b08b09SChandler Carruth// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
73e519524SHoward Hinnant//
83e519524SHoward Hinnant//===----------------------------------------------------------------------===//
93e519524SHoward Hinnant
103e519524SHoward Hinnant#ifndef _LIBCPP_THREAD
113e519524SHoward Hinnant#define _LIBCPP_THREAD
123e519524SHoward Hinnant
133e519524SHoward Hinnant/*
143e519524SHoward Hinnant
153e519524SHoward Hinnant    thread synopsis
163e519524SHoward Hinnant
173e519524SHoward Hinnantnamespace std
183e519524SHoward Hinnant{
193e519524SHoward Hinnant
203e519524SHoward Hinnantclass thread
213e519524SHoward Hinnant{
223e519524SHoward Hinnantpublic:
233e519524SHoward Hinnant    class id;
243e519524SHoward Hinnant    typedef pthread_t native_handle_type;
253e519524SHoward Hinnant
2636101a5bSHoward Hinnant    thread() noexcept;
273e519524SHoward Hinnant    template <class F, class ...Args> explicit thread(F&& f, Args&&... args);
283e519524SHoward Hinnant    ~thread();
293e519524SHoward Hinnant
303e519524SHoward Hinnant    thread(const thread&) = delete;
3136101a5bSHoward Hinnant    thread(thread&& t) noexcept;
323e519524SHoward Hinnant
333e519524SHoward Hinnant    thread& operator=(const thread&) = delete;
3436101a5bSHoward Hinnant    thread& operator=(thread&& t) noexcept;
353e519524SHoward Hinnant
3636101a5bSHoward Hinnant    void swap(thread& t) noexcept;
373e519524SHoward Hinnant
3836101a5bSHoward Hinnant    bool joinable() const noexcept;
393e519524SHoward Hinnant    void join();
403e519524SHoward Hinnant    void detach();
4136101a5bSHoward Hinnant    id get_id() const noexcept;
423e519524SHoward Hinnant    native_handle_type native_handle();
433e519524SHoward Hinnant
4436101a5bSHoward Hinnant    static unsigned hardware_concurrency() noexcept;
453e519524SHoward Hinnant};
463e519524SHoward Hinnant
4736101a5bSHoward Hinnantvoid swap(thread& x, thread& y) noexcept;
483e519524SHoward Hinnant
493e519524SHoward Hinnantclass thread::id
503e519524SHoward Hinnant{
513e519524SHoward Hinnantpublic:
5236101a5bSHoward Hinnant    id() noexcept;
533e519524SHoward Hinnant};
543e519524SHoward Hinnant
5536101a5bSHoward Hinnantbool operator==(thread::id x, thread::id y) noexcept;
5636101a5bSHoward Hinnantbool operator!=(thread::id x, thread::id y) noexcept;
5736101a5bSHoward Hinnantbool operator< (thread::id x, thread::id y) noexcept;
5836101a5bSHoward Hinnantbool operator<=(thread::id x, thread::id y) noexcept;
5936101a5bSHoward Hinnantbool operator> (thread::id x, thread::id y) noexcept;
6036101a5bSHoward Hinnantbool operator>=(thread::id x, thread::id y) noexcept;
613e519524SHoward Hinnant
623e519524SHoward Hinnanttemplate<class charT, class traits>
633e519524SHoward Hinnantbasic_ostream<charT, traits>&
643e519524SHoward Hinnantoperator<<(basic_ostream<charT, traits>& out, thread::id id);
653e519524SHoward Hinnant
663e519524SHoward Hinnantnamespace this_thread
673e519524SHoward Hinnant{
683e519524SHoward Hinnant
6936101a5bSHoward Hinnantthread::id get_id() noexcept;
703e519524SHoward Hinnant
7136101a5bSHoward Hinnantvoid yield() noexcept;
723e519524SHoward Hinnant
733e519524SHoward Hinnanttemplate <class Clock, class Duration>
743e519524SHoward Hinnantvoid sleep_until(const chrono::time_point<Clock, Duration>& abs_time);
753e519524SHoward Hinnant
763e519524SHoward Hinnanttemplate <class Rep, class Period>
773e519524SHoward Hinnantvoid sleep_for(const chrono::duration<Rep, Period>& rel_time);
783e519524SHoward Hinnant
793e519524SHoward Hinnant}  // this_thread
803e519524SHoward Hinnant
813e519524SHoward Hinnant}  // std
823e519524SHoward Hinnant
833e519524SHoward Hinnant*/
843e519524SHoward Hinnant
853e519524SHoward Hinnant#include <__config>
86*bfbd73f8SArthur O'Dwyer#include <__debug>
873e519524SHoward Hinnant#include <__functional_base>
88*bfbd73f8SArthur O'Dwyer#include <__mutex_base>
89*bfbd73f8SArthur O'Dwyer#include <__threading_support>
90*bfbd73f8SArthur O'Dwyer#include <chrono>
913e519524SHoward Hinnant#include <cstddef>
923e519524SHoward Hinnant#include <functional>
93*bfbd73f8SArthur O'Dwyer#include <iosfwd>
943e519524SHoward Hinnant#include <memory>
953e519524SHoward Hinnant#include <system_error>
968df61ea8SHoward Hinnant#include <tuple>
97*bfbd73f8SArthur O'Dwyer#include <type_traits>
983e519524SHoward Hinnant
99073458b1SHoward Hinnant#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
1003e519524SHoward Hinnant#pragma GCC system_header
101073458b1SHoward Hinnant#endif
1023e519524SHoward Hinnant
103a016efb1SEric Fiselier_LIBCPP_PUSH_MACROS
104a016efb1SEric Fiselier#include <__undef_macros>
105a016efb1SEric Fiselier
106b3fcc67fSJonathan Roelofs#ifdef _LIBCPP_HAS_NO_THREADS
107b3fcc67fSJonathan Roelofs#error <thread> is not supported on this single threaded system
108b3fcc67fSJonathan Roelofs#else // !_LIBCPP_HAS_NO_THREADS
109b3fcc67fSJonathan Roelofs
1103e519524SHoward Hinnant_LIBCPP_BEGIN_NAMESPACE_STD
1113e519524SHoward Hinnant
1124504cf2cSEric Fiseliertemplate <class _Tp> class __thread_specific_ptr;
1134504cf2cSEric Fiselierclass _LIBCPP_TYPE_VIS __thread_struct;
1144504cf2cSEric Fiselierclass _LIBCPP_HIDDEN __thread_struct_imp;
1154504cf2cSEric Fiselierclass __assoc_sub_state;
1164504cf2cSEric Fiselier
1174504cf2cSEric Fiselier_LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data();
1184504cf2cSEric Fiselier
1194504cf2cSEric Fiselierclass _LIBCPP_TYPE_VIS __thread_struct
1204504cf2cSEric Fiselier{
1214504cf2cSEric Fiselier    __thread_struct_imp* __p_;
1224504cf2cSEric Fiselier
1234504cf2cSEric Fiselier    __thread_struct(const __thread_struct&);
1244504cf2cSEric Fiselier    __thread_struct& operator=(const __thread_struct&);
1254504cf2cSEric Fiselierpublic:
1264504cf2cSEric Fiselier    __thread_struct();
1274504cf2cSEric Fiselier    ~__thread_struct();
1284504cf2cSEric Fiselier
1294504cf2cSEric Fiselier    void notify_all_at_thread_exit(condition_variable*, mutex*);
1304504cf2cSEric Fiselier    void __make_ready_at_thread_exit(__assoc_sub_state*);
1314504cf2cSEric Fiselier};
1324504cf2cSEric Fiselier
133167fd108SHoward Hinnanttemplate <class _Tp>
134167fd108SHoward Hinnantclass __thread_specific_ptr
135167fd108SHoward Hinnant{
1368c2bf45dSAsiri Rathnayake    __libcpp_tls_key __key_;
137167fd108SHoward Hinnant
1384504cf2cSEric Fiselier     // Only __thread_local_data() may construct a __thread_specific_ptr
1394504cf2cSEric Fiselier     // and only with _Tp == __thread_struct.
1409a5e62bfSEric Fiselier    static_assert((is_same<_Tp, __thread_struct>::value), "");
1414504cf2cSEric Fiselier    __thread_specific_ptr();
1424504cf2cSEric Fiselier    friend _LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data();
1434504cf2cSEric Fiselier
144167fd108SHoward Hinnant    __thread_specific_ptr(const __thread_specific_ptr&);
145167fd108SHoward Hinnant    __thread_specific_ptr& operator=(const __thread_specific_ptr&);
146167fd108SHoward Hinnant
1479b3222f6SLouis Dionne    _LIBCPP_HIDDEN static void _LIBCPP_TLS_DESTRUCTOR_CC __at_thread_exit(void*);
1486d7d0becSSaleem Abdulrasool
149167fd108SHoward Hinnantpublic:
150167fd108SHoward Hinnant    typedef _Tp* pointer;
151167fd108SHoward Hinnant
152167fd108SHoward Hinnant    ~__thread_specific_ptr();
153167fd108SHoward Hinnant
154e0601335SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
1558c2bf45dSAsiri Rathnayake    pointer get() const {return static_cast<_Tp*>(__libcpp_tls_get(__key_));}
156e0601335SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
157167fd108SHoward Hinnant    pointer operator*() const {return *get();}
158e0601335SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
159167fd108SHoward Hinnant    pointer operator->() const {return get();}
160ff94d250SEric Fiselier    void set_pointer(pointer __p);
161167fd108SHoward Hinnant};
162167fd108SHoward Hinnant
163167fd108SHoward Hinnanttemplate <class _Tp>
1646d7d0becSSaleem Abdulrasoolvoid _LIBCPP_TLS_DESTRUCTOR_CC
165167fd108SHoward Hinnant__thread_specific_ptr<_Tp>::__at_thread_exit(void* __p)
166167fd108SHoward Hinnant{
167167fd108SHoward Hinnant    delete static_cast<pointer>(__p);
168167fd108SHoward Hinnant}
169167fd108SHoward Hinnant
170167fd108SHoward Hinnanttemplate <class _Tp>
171167fd108SHoward Hinnant__thread_specific_ptr<_Tp>::__thread_specific_ptr()
172167fd108SHoward Hinnant{
1736d7d0becSSaleem Abdulrasool  int __ec =
1746d7d0becSSaleem Abdulrasool      __libcpp_tls_create(&__key_, &__thread_specific_ptr::__at_thread_exit);
175167fd108SHoward Hinnant  if (__ec)
1766d7d0becSSaleem Abdulrasool    __throw_system_error(__ec, "__thread_specific_ptr construction failed");
177167fd108SHoward Hinnant}
178167fd108SHoward Hinnant
179167fd108SHoward Hinnanttemplate <class _Tp>
180167fd108SHoward Hinnant__thread_specific_ptr<_Tp>::~__thread_specific_ptr()
181167fd108SHoward Hinnant{
1824504cf2cSEric Fiselier    // __thread_specific_ptr is only created with a static storage duration
1834504cf2cSEric Fiselier    // so this destructor is only invoked during program termination. Invoking
1844504cf2cSEric Fiselier    // pthread_key_delete(__key_) may prevent other threads from deleting their
1854504cf2cSEric Fiselier    // thread local data. For this reason we leak the key.
186167fd108SHoward Hinnant}
187167fd108SHoward Hinnant
188167fd108SHoward Hinnanttemplate <class _Tp>
189167fd108SHoward Hinnantvoid
190ff94d250SEric Fiselier__thread_specific_ptr<_Tp>::set_pointer(pointer __p)
191167fd108SHoward Hinnant{
192ff94d250SEric Fiselier    _LIBCPP_ASSERT(get() == nullptr,
193ff94d250SEric Fiselier                   "Attempting to overwrite thread local data");
1948c2bf45dSAsiri Rathnayake    __libcpp_tls_set(__key_, __p);
195167fd108SHoward Hinnant}
196167fd108SHoward Hinnant
1973e519524SHoward Hinnanttemplate<>
198e2f2d1edSEric Fiselierstruct _LIBCPP_TEMPLATE_VIS hash<__thread_id>
1993e519524SHoward Hinnant    : public unary_function<__thread_id, size_t>
2003e519524SHoward Hinnant{
201e0601335SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
2027c803385SMarshall Clow    size_t operator()(__thread_id __v) const _NOEXCEPT
2033e519524SHoward Hinnant    {
204c7e4239fSAsiri Rathnayake        return hash<__libcpp_thread_id>()(__v.__id_);
2053e519524SHoward Hinnant    }
2063e519524SHoward Hinnant};
2073e519524SHoward Hinnant
2082b1d4254SMarshall Clowtemplate<class _CharT, class _Traits>
2092b1d4254SMarshall Clow_LIBCPP_INLINE_VISIBILITY
2102b1d4254SMarshall Clowbasic_ostream<_CharT, _Traits>&
2112b1d4254SMarshall Clowoperator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id)
2122b1d4254SMarshall Clow{return __os << __id.__id_;}
2133e519524SHoward Hinnant
2146e41256fSHoward Hinnantclass _LIBCPP_TYPE_VIS thread
2153e519524SHoward Hinnant{
216c7e4239fSAsiri Rathnayake    __libcpp_thread_t __t_;
2173e519524SHoward Hinnant
218eb269257SHoward Hinnant    thread(const thread&);
219eb269257SHoward Hinnant    thread& operator=(const thread&);
2203e519524SHoward Hinnantpublic:
2213e519524SHoward Hinnant    typedef __thread_id id;
222c7e4239fSAsiri Rathnayake    typedef __libcpp_thread_t native_handle_type;
2233e519524SHoward Hinnant
224e0601335SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
225af762e91SAsiri Rathnayake    thread() _NOEXCEPT : __t_(_LIBCPP_NULL_THREAD) {}
226e11fb13bSEric Fiselier#ifndef _LIBCPP_CXX03_LANG
227c003db1fSHoward Hinnant    template <class _Fp, class ..._Args,
2283e519524SHoward Hinnant              class = typename enable_if
2293e519524SHoward Hinnant              <
23069f1072bSMarshall Clow                   !is_same<typename __uncvref<_Fp>::type, thread>::value
2313e519524SHoward Hinnant              >::type
2323e519524SHoward Hinnant             >
233bda3c7dfSShoaib Meenai        _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
234c003db1fSHoward Hinnant        explicit thread(_Fp&& __f, _Args&&... __args);
235e11fb13bSEric Fiselier#else  // _LIBCPP_CXX03_LANG
236bda3c7dfSShoaib Meenai    template <class _Fp>
237bda3c7dfSShoaib Meenai    _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
238bda3c7dfSShoaib Meenai    explicit thread(_Fp __f);
2393e519524SHoward Hinnant#endif
2403e519524SHoward Hinnant    ~thread();
2413e519524SHoward Hinnant
242e0601335SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
24362cfa3a0SLouis Dionne    thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) {
24462cfa3a0SLouis Dionne        __t.__t_ = _LIBCPP_NULL_THREAD;
24562cfa3a0SLouis Dionne    }
24662cfa3a0SLouis Dionne
247906c872dSEvgeniy Stepanov    _LIBCPP_INLINE_VISIBILITY
24862cfa3a0SLouis Dionne    thread& operator=(thread&& __t) _NOEXCEPT {
24962cfa3a0SLouis Dionne        if (!__libcpp_thread_isnull(&__t_))
25062cfa3a0SLouis Dionne            terminate();
25162cfa3a0SLouis Dionne        __t_ = __t.__t_;
25262cfa3a0SLouis Dionne        __t.__t_ = _LIBCPP_NULL_THREAD;
25362cfa3a0SLouis Dionne        return *this;
25462cfa3a0SLouis Dionne    }
2553e519524SHoward Hinnant
256e0601335SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
25736101a5bSHoward Hinnant    void swap(thread& __t) _NOEXCEPT {_VSTD::swap(__t_, __t.__t_);}
2583e519524SHoward Hinnant
259e0601335SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
260af762e91SAsiri Rathnayake    bool joinable() const _NOEXCEPT {return !__libcpp_thread_isnull(&__t_);}
2613e519524SHoward Hinnant    void join();
2623e519524SHoward Hinnant    void detach();
263e0601335SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
264c7e4239fSAsiri Rathnayake    id get_id() const _NOEXCEPT {return __libcpp_thread_get_id(&__t_);}
265e0601335SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
26636101a5bSHoward Hinnant    native_handle_type native_handle() _NOEXCEPT {return __t_;}
2673e519524SHoward Hinnant
26836101a5bSHoward Hinnant    static unsigned hardware_concurrency() _NOEXCEPT;
2693e519524SHoward Hinnant};
2703e519524SHoward Hinnant
271e11fb13bSEric Fiselier#ifndef _LIBCPP_CXX03_LANG
2728df61ea8SHoward Hinnant
273e08afaf8SEric Fiseliertemplate <class _TSp, class _Fp, class ..._Args, size_t ..._Indices>
2748df61ea8SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
2758df61ea8SHoward Hinnantvoid
276e08afaf8SEric Fiselier__thread_execute(tuple<_TSp, _Fp, _Args...>& __t, __tuple_indices<_Indices...>)
2778df61ea8SHoward Hinnant{
27840950a44SArthur O'Dwyer    _VSTD::__invoke(_VSTD::move(_VSTD::get<1>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
2798df61ea8SHoward Hinnant}
2808df61ea8SHoward Hinnant
281c003db1fSHoward Hinnanttemplate <class _Fp>
282dd37e24aSLouis Dionne_LIBCPP_INLINE_VISIBILITY
283e08afaf8SEric Fiseliervoid* __thread_proxy(void* __vp)
2848df61ea8SHoward Hinnant{
28540950a44SArthur O'Dwyer    // _Fp = tuple< unique_ptr<__thread_struct>, Functor, Args...>
28640950a44SArthur O'Dwyer    unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
28740950a44SArthur O'Dwyer    __thread_local_data().set_pointer(_VSTD::get<0>(*__p.get()).release());
288e08afaf8SEric Fiselier    typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 2>::type _Index;
28940950a44SArthur O'Dwyer    _VSTD::__thread_execute(*__p.get(), _Index());
2908df61ea8SHoward Hinnant    return nullptr;
2918df61ea8SHoward Hinnant}
2928df61ea8SHoward Hinnant
293c003db1fSHoward Hinnanttemplate <class _Fp, class ..._Args,
2948df61ea8SHoward Hinnant          class
2958df61ea8SHoward Hinnant         >
296c003db1fSHoward Hinnantthread::thread(_Fp&& __f, _Args&&... __args)
2978df61ea8SHoward Hinnant{
298e08afaf8SEric Fiselier    typedef unique_ptr<__thread_struct> _TSPtr;
299e08afaf8SEric Fiselier    _TSPtr __tsp(new __thread_struct);
300e08afaf8SEric Fiselier    typedef tuple<_TSPtr, typename decay<_Fp>::type, typename decay<_Args>::type...> _Gp;
30140950a44SArthur O'Dwyer    unique_ptr<_Gp> __p(
30240950a44SArthur O'Dwyer            new _Gp(_VSTD::move(__tsp),
30340950a44SArthur O'Dwyer                    _VSTD::__decay_copy(_VSTD::forward<_Fp>(__f)),
30440950a44SArthur O'Dwyer                    _VSTD::__decay_copy(_VSTD::forward<_Args>(__args))...));
30540950a44SArthur O'Dwyer    int __ec = _VSTD::__libcpp_thread_create(&__t_, &__thread_proxy<_Gp>, __p.get());
3068df61ea8SHoward Hinnant    if (__ec == 0)
3078df61ea8SHoward Hinnant        __p.release();
3088df61ea8SHoward Hinnant    else
3098df61ea8SHoward Hinnant        __throw_system_error(__ec, "thread constructor failed");
3108df61ea8SHoward Hinnant}
3118df61ea8SHoward Hinnant
312e11fb13bSEric Fiselier#else  // _LIBCPP_CXX03_LANG
3138df61ea8SHoward Hinnant
314c003db1fSHoward Hinnanttemplate <class _Fp>
315e08afaf8SEric Fiselierstruct __thread_invoke_pair {
316e08afaf8SEric Fiselier    // This type is used to pass memory for thread local storage and a functor
317e08afaf8SEric Fiselier    // to a newly created thread because std::pair doesn't work with
318e08afaf8SEric Fiselier    // std::unique_ptr in C++03.
319e08afaf8SEric Fiselier    __thread_invoke_pair(_Fp& __f) : __tsp_(new __thread_struct), __fn_(__f) {}
320e08afaf8SEric Fiselier    unique_ptr<__thread_struct> __tsp_;
321e08afaf8SEric Fiselier    _Fp __fn_;
322e08afaf8SEric Fiselier};
323e08afaf8SEric Fiselier
324e08afaf8SEric Fiseliertemplate <class _Fp>
325e08afaf8SEric Fiseliervoid* __thread_proxy_cxx03(void* __vp)
3263e519524SHoward Hinnant{
32740950a44SArthur O'Dwyer    unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
328ff94d250SEric Fiselier    __thread_local_data().set_pointer(__p->__tsp_.release());
329e08afaf8SEric Fiselier    (__p->__fn_)();
3303e519524SHoward Hinnant    return nullptr;
3313e519524SHoward Hinnant}
3323e519524SHoward Hinnant
333c003db1fSHoward Hinnanttemplate <class _Fp>
334c003db1fSHoward Hinnantthread::thread(_Fp __f)
3353e519524SHoward Hinnant{
336e08afaf8SEric Fiselier
337e08afaf8SEric Fiselier    typedef __thread_invoke_pair<_Fp> _InvokePair;
33840950a44SArthur O'Dwyer    typedef unique_ptr<_InvokePair> _PairPtr;
339e08afaf8SEric Fiselier    _PairPtr __pp(new _InvokePair(__f));
34040950a44SArthur O'Dwyer    int __ec = _VSTD::__libcpp_thread_create(&__t_, &__thread_proxy_cxx03<_InvokePair>, __pp.get());
3413e519524SHoward Hinnant    if (__ec == 0)
342e08afaf8SEric Fiselier        __pp.release();
3433e519524SHoward Hinnant    else
3443e519524SHoward Hinnant        __throw_system_error(__ec, "thread constructor failed");
3453e519524SHoward Hinnant}
3463e519524SHoward Hinnant
347e11fb13bSEric Fiselier#endif // _LIBCPP_CXX03_LANG
3483e519524SHoward Hinnant
349e0601335SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
35036101a5bSHoward Hinnantvoid swap(thread& __x, thread& __y) _NOEXCEPT {__x.swap(__y);}
3513e519524SHoward Hinnant
3523e519524SHoward Hinnantnamespace this_thread
3533e519524SHoward Hinnant{
3543e519524SHoward Hinnant
3559e8a082dSJoerg Sonnenberger_LIBCPP_FUNC_VIS void sleep_for(const chrono::nanoseconds& __ns);
3563e519524SHoward Hinnant
3573e519524SHoward Hinnanttemplate <class _Rep, class _Period>
3583e519524SHoward Hinnantvoid
3593e519524SHoward Hinnantsleep_for(const chrono::duration<_Rep, _Period>& __d)
3603e519524SHoward Hinnant{
36140950a44SArthur O'Dwyer    if (__d > chrono::duration<_Rep, _Period>::zero())
362aad745a0SHoward Hinnant    {
3639f4022ffSJoerg Sonnenberger        // The standard guarantees a 64bit signed integer resolution for nanoseconds,
3649f4022ffSJoerg Sonnenberger        // so use INT64_MAX / 1e9 as cut-off point. Use a constant to avoid <climits>
3659f4022ffSJoerg Sonnenberger        // and issues with long double folding on PowerPC with GCC.
3669f4022ffSJoerg Sonnenberger        _LIBCPP_CONSTEXPR chrono::duration<long double> _Max =
3679f4022ffSJoerg Sonnenberger            chrono::duration<long double>(9223372036.0L);
36840950a44SArthur O'Dwyer        chrono::nanoseconds __ns;
369aad745a0SHoward Hinnant        if (__d < _Max)
370aad745a0SHoward Hinnant        {
37140950a44SArthur O'Dwyer            __ns = chrono::duration_cast<chrono::nanoseconds>(__d);
3723e519524SHoward Hinnant            if (__ns < __d)
3733e519524SHoward Hinnant                ++__ns;
374aad745a0SHoward Hinnant        }
375aad745a0SHoward Hinnant        else
37640950a44SArthur O'Dwyer            __ns = chrono::nanoseconds::max();
37740950a44SArthur O'Dwyer        this_thread::sleep_for(__ns);
3783e519524SHoward Hinnant    }
379aad745a0SHoward Hinnant}
3803e519524SHoward Hinnant
3813e519524SHoward Hinnanttemplate <class _Clock, class _Duration>
3823e519524SHoward Hinnantvoid
3833e519524SHoward Hinnantsleep_until(const chrono::time_point<_Clock, _Duration>& __t)
3843e519524SHoward Hinnant{
3853e519524SHoward Hinnant    mutex __mut;
3863e519524SHoward Hinnant    condition_variable __cv;
3873e519524SHoward Hinnant    unique_lock<mutex> __lk(__mut);
3883e519524SHoward Hinnant    while (_Clock::now() < __t)
3893e519524SHoward Hinnant        __cv.wait_until(__lk, __t);
3903e519524SHoward Hinnant}
3913e519524SHoward Hinnant
3923e519524SHoward Hinnanttemplate <class _Duration>
393e0601335SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
3943e519524SHoward Hinnantvoid
3953dc6455fSHoward Hinnantsleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t)
3963e519524SHoward Hinnant{
39740950a44SArthur O'Dwyer    this_thread::sleep_for(__t - chrono::steady_clock::now());
3983e519524SHoward Hinnant}
3993e519524SHoward Hinnant
400e0601335SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
401c7e4239fSAsiri Rathnayakevoid yield() _NOEXCEPT {__libcpp_thread_yield();}
4023e519524SHoward Hinnant
4033e519524SHoward Hinnant}  // this_thread
4043e519524SHoward Hinnant
4053e519524SHoward Hinnant_LIBCPP_END_NAMESPACE_STD
4063e519524SHoward Hinnant
407b3fcc67fSJonathan Roelofs#endif // !_LIBCPP_HAS_NO_THREADS
408b3fcc67fSJonathan Roelofs
409a016efb1SEric Fiselier_LIBCPP_POP_MACROS
410a016efb1SEric Fiselier
4113e519524SHoward Hinnant#endif // _LIBCPP_THREAD
412