xref: /llvm-project-15.0.7/libcxx/include/thread (revision d2b0df35)
13e519524SHoward Hinnant// -*- C++ -*-
2eb8650a7SLouis Dionne//===----------------------------------------------------------------------===//
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>
86bfbd73f8SArthur O'Dwyer#include <__debug>
873e519524SHoward Hinnant#include <__functional_base>
88bfbd73f8SArthur O'Dwyer#include <__mutex_base>
8992832e48SLouis Dionne#include <__thread/poll_with_backoff.h>
90bfbd73f8SArthur O'Dwyer#include <__threading_support>
91c8b14c03SLouis Dionne#include <__utility/decay_copy.h>
926adbc83eSChristopher Di Bella#include <__utility/forward.h>
93bfbd73f8SArthur O'Dwyer#include <chrono>
943e519524SHoward Hinnant#include <cstddef>
953e519524SHoward Hinnant#include <functional>
96bfbd73f8SArthur O'Dwyer#include <iosfwd>
973e519524SHoward Hinnant#include <memory>
983e519524SHoward Hinnant#include <system_error>
998df61ea8SHoward Hinnant#include <tuple>
100bfbd73f8SArthur O'Dwyer#include <type_traits>
1013e519524SHoward Hinnant
102073458b1SHoward Hinnant#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
1033e519524SHoward Hinnant#pragma GCC system_header
104073458b1SHoward Hinnant#endif
1053e519524SHoward Hinnant
106a016efb1SEric Fiselier_LIBCPP_PUSH_MACROS
107a016efb1SEric Fiselier#include <__undef_macros>
108a016efb1SEric Fiselier
109b3fcc67fSJonathan Roelofs#ifdef _LIBCPP_HAS_NO_THREADS
110b3fcc67fSJonathan Roelofs#error <thread> is not supported on this single threaded system
111b3fcc67fSJonathan Roelofs#else // !_LIBCPP_HAS_NO_THREADS
112b3fcc67fSJonathan Roelofs
1133e519524SHoward Hinnant_LIBCPP_BEGIN_NAMESPACE_STD
1143e519524SHoward Hinnant
1154504cf2cSEric Fiseliertemplate <class _Tp> class __thread_specific_ptr;
1164504cf2cSEric Fiselierclass _LIBCPP_TYPE_VIS __thread_struct;
1174504cf2cSEric Fiselierclass _LIBCPP_HIDDEN __thread_struct_imp;
1184504cf2cSEric Fiselierclass __assoc_sub_state;
1194504cf2cSEric Fiselier
1204504cf2cSEric Fiselier_LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data();
1214504cf2cSEric Fiselier
1224504cf2cSEric Fiselierclass _LIBCPP_TYPE_VIS __thread_struct
1234504cf2cSEric Fiselier{
1244504cf2cSEric Fiselier    __thread_struct_imp* __p_;
1254504cf2cSEric Fiselier
1264504cf2cSEric Fiselier    __thread_struct(const __thread_struct&);
1274504cf2cSEric Fiselier    __thread_struct& operator=(const __thread_struct&);
1284504cf2cSEric Fiselierpublic:
1294504cf2cSEric Fiselier    __thread_struct();
1304504cf2cSEric Fiselier    ~__thread_struct();
1314504cf2cSEric Fiselier
1324504cf2cSEric Fiselier    void notify_all_at_thread_exit(condition_variable*, mutex*);
1334504cf2cSEric Fiselier    void __make_ready_at_thread_exit(__assoc_sub_state*);
1344504cf2cSEric Fiselier};
1354504cf2cSEric Fiselier
136167fd108SHoward Hinnanttemplate <class _Tp>
137167fd108SHoward Hinnantclass __thread_specific_ptr
138167fd108SHoward Hinnant{
1398c2bf45dSAsiri Rathnayake    __libcpp_tls_key __key_;
140167fd108SHoward Hinnant
1414504cf2cSEric Fiselier     // Only __thread_local_data() may construct a __thread_specific_ptr
1424504cf2cSEric Fiselier     // and only with _Tp == __thread_struct.
1439a5e62bfSEric Fiselier    static_assert((is_same<_Tp, __thread_struct>::value), "");
1444504cf2cSEric Fiselier    __thread_specific_ptr();
1454504cf2cSEric Fiselier    friend _LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data();
1464504cf2cSEric Fiselier
147167fd108SHoward Hinnant    __thread_specific_ptr(const __thread_specific_ptr&);
148167fd108SHoward Hinnant    __thread_specific_ptr& operator=(const __thread_specific_ptr&);
149167fd108SHoward Hinnant
1509b3222f6SLouis Dionne    _LIBCPP_HIDDEN static void _LIBCPP_TLS_DESTRUCTOR_CC __at_thread_exit(void*);
1516d7d0becSSaleem Abdulrasool
152167fd108SHoward Hinnantpublic:
153167fd108SHoward Hinnant    typedef _Tp* pointer;
154167fd108SHoward Hinnant
155167fd108SHoward Hinnant    ~__thread_specific_ptr();
156167fd108SHoward Hinnant
157e0601335SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
1588c2bf45dSAsiri Rathnayake    pointer get() const {return static_cast<_Tp*>(__libcpp_tls_get(__key_));}
159e0601335SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
160167fd108SHoward Hinnant    pointer operator*() const {return *get();}
161e0601335SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
162167fd108SHoward Hinnant    pointer operator->() const {return get();}
163ff94d250SEric Fiselier    void set_pointer(pointer __p);
164167fd108SHoward Hinnant};
165167fd108SHoward Hinnant
166167fd108SHoward Hinnanttemplate <class _Tp>
1676d7d0becSSaleem Abdulrasoolvoid _LIBCPP_TLS_DESTRUCTOR_CC
168167fd108SHoward Hinnant__thread_specific_ptr<_Tp>::__at_thread_exit(void* __p)
169167fd108SHoward Hinnant{
170167fd108SHoward Hinnant    delete static_cast<pointer>(__p);
171167fd108SHoward Hinnant}
172167fd108SHoward Hinnant
173167fd108SHoward Hinnanttemplate <class _Tp>
174167fd108SHoward Hinnant__thread_specific_ptr<_Tp>::__thread_specific_ptr()
175167fd108SHoward Hinnant{
1766d7d0becSSaleem Abdulrasool  int __ec =
1776d7d0becSSaleem Abdulrasool      __libcpp_tls_create(&__key_, &__thread_specific_ptr::__at_thread_exit);
178167fd108SHoward Hinnant  if (__ec)
1796d7d0becSSaleem Abdulrasool    __throw_system_error(__ec, "__thread_specific_ptr construction failed");
180167fd108SHoward Hinnant}
181167fd108SHoward Hinnant
182167fd108SHoward Hinnanttemplate <class _Tp>
183167fd108SHoward Hinnant__thread_specific_ptr<_Tp>::~__thread_specific_ptr()
184167fd108SHoward Hinnant{
1854504cf2cSEric Fiselier    // __thread_specific_ptr is only created with a static storage duration
1864504cf2cSEric Fiselier    // so this destructor is only invoked during program termination. Invoking
1874504cf2cSEric Fiselier    // pthread_key_delete(__key_) may prevent other threads from deleting their
1884504cf2cSEric Fiselier    // thread local data. For this reason we leak the key.
189167fd108SHoward Hinnant}
190167fd108SHoward Hinnant
191167fd108SHoward Hinnanttemplate <class _Tp>
192167fd108SHoward Hinnantvoid
193ff94d250SEric Fiselier__thread_specific_ptr<_Tp>::set_pointer(pointer __p)
194167fd108SHoward Hinnant{
195ff94d250SEric Fiselier    _LIBCPP_ASSERT(get() == nullptr,
196ff94d250SEric Fiselier                   "Attempting to overwrite thread local data");
1978c2bf45dSAsiri Rathnayake    __libcpp_tls_set(__key_, __p);
198167fd108SHoward Hinnant}
199167fd108SHoward Hinnant
2003e519524SHoward Hinnanttemplate<>
201e2f2d1edSEric Fiselierstruct _LIBCPP_TEMPLATE_VIS hash<__thread_id>
2023e519524SHoward Hinnant    : public unary_function<__thread_id, size_t>
2033e519524SHoward Hinnant{
204e0601335SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
2057c803385SMarshall Clow    size_t operator()(__thread_id __v) const _NOEXCEPT
2063e519524SHoward Hinnant    {
207c7e4239fSAsiri Rathnayake        return hash<__libcpp_thread_id>()(__v.__id_);
2083e519524SHoward Hinnant    }
2093e519524SHoward Hinnant};
2103e519524SHoward Hinnant
2112b1d4254SMarshall Clowtemplate<class _CharT, class _Traits>
2122b1d4254SMarshall Clow_LIBCPP_INLINE_VISIBILITY
2132b1d4254SMarshall Clowbasic_ostream<_CharT, _Traits>&
2142b1d4254SMarshall Clowoperator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id)
2152b1d4254SMarshall Clow{return __os << __id.__id_;}
2163e519524SHoward Hinnant
2176e41256fSHoward Hinnantclass _LIBCPP_TYPE_VIS thread
2183e519524SHoward Hinnant{
219c7e4239fSAsiri Rathnayake    __libcpp_thread_t __t_;
2203e519524SHoward Hinnant
221eb269257SHoward Hinnant    thread(const thread&);
222eb269257SHoward Hinnant    thread& operator=(const thread&);
2233e519524SHoward Hinnantpublic:
2243e519524SHoward Hinnant    typedef __thread_id id;
225c7e4239fSAsiri Rathnayake    typedef __libcpp_thread_t native_handle_type;
2263e519524SHoward Hinnant
227e0601335SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
228af762e91SAsiri Rathnayake    thread() _NOEXCEPT : __t_(_LIBCPP_NULL_THREAD) {}
229e11fb13bSEric Fiselier#ifndef _LIBCPP_CXX03_LANG
230c003db1fSHoward Hinnant    template <class _Fp, class ..._Args,
2313e519524SHoward Hinnant              class = typename enable_if
2323e519524SHoward Hinnant              <
23369f1072bSMarshall Clow                   !is_same<typename __uncvref<_Fp>::type, thread>::value
2343e519524SHoward Hinnant              >::type
2353e519524SHoward Hinnant             >
236bda3c7dfSShoaib Meenai        _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
237c003db1fSHoward Hinnant        explicit thread(_Fp&& __f, _Args&&... __args);
238e11fb13bSEric Fiselier#else  // _LIBCPP_CXX03_LANG
239bda3c7dfSShoaib Meenai    template <class _Fp>
240bda3c7dfSShoaib Meenai    _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
241bda3c7dfSShoaib Meenai    explicit thread(_Fp __f);
2423e519524SHoward Hinnant#endif
2433e519524SHoward Hinnant    ~thread();
2443e519524SHoward Hinnant
245e0601335SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
24662cfa3a0SLouis Dionne    thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) {
24762cfa3a0SLouis Dionne        __t.__t_ = _LIBCPP_NULL_THREAD;
24862cfa3a0SLouis Dionne    }
24962cfa3a0SLouis Dionne
250906c872dSEvgeniy Stepanov    _LIBCPP_INLINE_VISIBILITY
25162cfa3a0SLouis Dionne    thread& operator=(thread&& __t) _NOEXCEPT {
25262cfa3a0SLouis Dionne        if (!__libcpp_thread_isnull(&__t_))
25362cfa3a0SLouis Dionne            terminate();
25462cfa3a0SLouis Dionne        __t_ = __t.__t_;
25562cfa3a0SLouis Dionne        __t.__t_ = _LIBCPP_NULL_THREAD;
25662cfa3a0SLouis Dionne        return *this;
25762cfa3a0SLouis Dionne    }
2583e519524SHoward Hinnant
259e0601335SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
26036101a5bSHoward Hinnant    void swap(thread& __t) _NOEXCEPT {_VSTD::swap(__t_, __t.__t_);}
2613e519524SHoward Hinnant
262e0601335SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
263af762e91SAsiri Rathnayake    bool joinable() const _NOEXCEPT {return !__libcpp_thread_isnull(&__t_);}
2643e519524SHoward Hinnant    void join();
2653e519524SHoward Hinnant    void detach();
266e0601335SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
267c7e4239fSAsiri Rathnayake    id get_id() const _NOEXCEPT {return __libcpp_thread_get_id(&__t_);}
268e0601335SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
26936101a5bSHoward Hinnant    native_handle_type native_handle() _NOEXCEPT {return __t_;}
2703e519524SHoward Hinnant
27136101a5bSHoward Hinnant    static unsigned hardware_concurrency() _NOEXCEPT;
2723e519524SHoward Hinnant};
2733e519524SHoward Hinnant
274e11fb13bSEric Fiselier#ifndef _LIBCPP_CXX03_LANG
2758df61ea8SHoward Hinnant
276e08afaf8SEric Fiseliertemplate <class _TSp, class _Fp, class ..._Args, size_t ..._Indices>
2778df61ea8SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
2788df61ea8SHoward Hinnantvoid
279e08afaf8SEric Fiselier__thread_execute(tuple<_TSp, _Fp, _Args...>& __t, __tuple_indices<_Indices...>)
2808df61ea8SHoward Hinnant{
28140950a44SArthur O'Dwyer    _VSTD::__invoke(_VSTD::move(_VSTD::get<1>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
2828df61ea8SHoward Hinnant}
2838df61ea8SHoward Hinnant
284c003db1fSHoward Hinnanttemplate <class _Fp>
285dd37e24aSLouis Dionne_LIBCPP_INLINE_VISIBILITY
286e08afaf8SEric Fiseliervoid* __thread_proxy(void* __vp)
2878df61ea8SHoward Hinnant{
28840950a44SArthur O'Dwyer    // _Fp = tuple< unique_ptr<__thread_struct>, Functor, Args...>
28940950a44SArthur O'Dwyer    unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
29040950a44SArthur O'Dwyer    __thread_local_data().set_pointer(_VSTD::get<0>(*__p.get()).release());
291e08afaf8SEric Fiselier    typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 2>::type _Index;
29240950a44SArthur O'Dwyer    _VSTD::__thread_execute(*__p.get(), _Index());
2938df61ea8SHoward Hinnant    return nullptr;
2948df61ea8SHoward Hinnant}
2958df61ea8SHoward Hinnant
296c003db1fSHoward Hinnanttemplate <class _Fp, class ..._Args,
2978df61ea8SHoward Hinnant          class
2988df61ea8SHoward Hinnant         >
299c003db1fSHoward Hinnantthread::thread(_Fp&& __f, _Args&&... __args)
3008df61ea8SHoward Hinnant{
301e08afaf8SEric Fiselier    typedef unique_ptr<__thread_struct> _TSPtr;
302e08afaf8SEric Fiselier    _TSPtr __tsp(new __thread_struct);
303e08afaf8SEric Fiselier    typedef tuple<_TSPtr, typename decay<_Fp>::type, typename decay<_Args>::type...> _Gp;
30440950a44SArthur O'Dwyer    unique_ptr<_Gp> __p(
30540950a44SArthur O'Dwyer            new _Gp(_VSTD::move(__tsp),
30640950a44SArthur O'Dwyer                    _VSTD::__decay_copy(_VSTD::forward<_Fp>(__f)),
30740950a44SArthur O'Dwyer                    _VSTD::__decay_copy(_VSTD::forward<_Args>(__args))...));
30840950a44SArthur O'Dwyer    int __ec = _VSTD::__libcpp_thread_create(&__t_, &__thread_proxy<_Gp>, __p.get());
3098df61ea8SHoward Hinnant    if (__ec == 0)
3108df61ea8SHoward Hinnant        __p.release();
3118df61ea8SHoward Hinnant    else
3128df61ea8SHoward Hinnant        __throw_system_error(__ec, "thread constructor failed");
3138df61ea8SHoward Hinnant}
3148df61ea8SHoward Hinnant
315e11fb13bSEric Fiselier#else  // _LIBCPP_CXX03_LANG
3168df61ea8SHoward Hinnant
317c003db1fSHoward Hinnanttemplate <class _Fp>
318e08afaf8SEric Fiselierstruct __thread_invoke_pair {
319e08afaf8SEric Fiselier    // This type is used to pass memory for thread local storage and a functor
320e08afaf8SEric Fiselier    // to a newly created thread because std::pair doesn't work with
321e08afaf8SEric Fiselier    // std::unique_ptr in C++03.
322e08afaf8SEric Fiselier    __thread_invoke_pair(_Fp& __f) : __tsp_(new __thread_struct), __fn_(__f) {}
323e08afaf8SEric Fiselier    unique_ptr<__thread_struct> __tsp_;
324e08afaf8SEric Fiselier    _Fp __fn_;
325e08afaf8SEric Fiselier};
326e08afaf8SEric Fiselier
327e08afaf8SEric Fiseliertemplate <class _Fp>
328e08afaf8SEric Fiseliervoid* __thread_proxy_cxx03(void* __vp)
3293e519524SHoward Hinnant{
33040950a44SArthur O'Dwyer    unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
331ff94d250SEric Fiselier    __thread_local_data().set_pointer(__p->__tsp_.release());
332e08afaf8SEric Fiselier    (__p->__fn_)();
3333e519524SHoward Hinnant    return nullptr;
3343e519524SHoward Hinnant}
3353e519524SHoward Hinnant
336c003db1fSHoward Hinnanttemplate <class _Fp>
337c003db1fSHoward Hinnantthread::thread(_Fp __f)
3383e519524SHoward Hinnant{
339e08afaf8SEric Fiselier
340e08afaf8SEric Fiselier    typedef __thread_invoke_pair<_Fp> _InvokePair;
34140950a44SArthur O'Dwyer    typedef unique_ptr<_InvokePair> _PairPtr;
342e08afaf8SEric Fiselier    _PairPtr __pp(new _InvokePair(__f));
34340950a44SArthur O'Dwyer    int __ec = _VSTD::__libcpp_thread_create(&__t_, &__thread_proxy_cxx03<_InvokePair>, __pp.get());
3443e519524SHoward Hinnant    if (__ec == 0)
345e08afaf8SEric Fiselier        __pp.release();
3463e519524SHoward Hinnant    else
3473e519524SHoward Hinnant        __throw_system_error(__ec, "thread constructor failed");
3483e519524SHoward Hinnant}
3493e519524SHoward Hinnant
350e11fb13bSEric Fiselier#endif // _LIBCPP_CXX03_LANG
3513e519524SHoward Hinnant
352e0601335SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
35336101a5bSHoward Hinnantvoid swap(thread& __x, thread& __y) _NOEXCEPT {__x.swap(__y);}
3543e519524SHoward Hinnant
3553e519524SHoward Hinnantnamespace this_thread
3563e519524SHoward Hinnant{
3573e519524SHoward Hinnant
3589e8a082dSJoerg Sonnenberger_LIBCPP_FUNC_VIS void sleep_for(const chrono::nanoseconds& __ns);
3593e519524SHoward Hinnant
3603e519524SHoward Hinnanttemplate <class _Rep, class _Period>
3613e519524SHoward Hinnantvoid
3623e519524SHoward Hinnantsleep_for(const chrono::duration<_Rep, _Period>& __d)
3633e519524SHoward Hinnant{
36440950a44SArthur O'Dwyer    if (__d > chrono::duration<_Rep, _Period>::zero())
365aad745a0SHoward Hinnant    {
3669f4022ffSJoerg Sonnenberger        // The standard guarantees a 64bit signed integer resolution for nanoseconds,
3679f4022ffSJoerg Sonnenberger        // so use INT64_MAX / 1e9 as cut-off point. Use a constant to avoid <climits>
3689f4022ffSJoerg Sonnenberger        // and issues with long double folding on PowerPC with GCC.
3699f4022ffSJoerg Sonnenberger        _LIBCPP_CONSTEXPR chrono::duration<long double> _Max =
3709f4022ffSJoerg Sonnenberger            chrono::duration<long double>(9223372036.0L);
37140950a44SArthur O'Dwyer        chrono::nanoseconds __ns;
372aad745a0SHoward Hinnant        if (__d < _Max)
373aad745a0SHoward Hinnant        {
37440950a44SArthur O'Dwyer            __ns = chrono::duration_cast<chrono::nanoseconds>(__d);
3753e519524SHoward Hinnant            if (__ns < __d)
3763e519524SHoward Hinnant                ++__ns;
377aad745a0SHoward Hinnant        }
378aad745a0SHoward Hinnant        else
37940950a44SArthur O'Dwyer            __ns = chrono::nanoseconds::max();
38040950a44SArthur O'Dwyer        this_thread::sleep_for(__ns);
3813e519524SHoward Hinnant    }
382aad745a0SHoward Hinnant}
3833e519524SHoward Hinnant
3843e519524SHoward Hinnanttemplate <class _Clock, class _Duration>
3853e519524SHoward Hinnantvoid
3863e519524SHoward Hinnantsleep_until(const chrono::time_point<_Clock, _Duration>& __t)
3873e519524SHoward Hinnant{
3883e519524SHoward Hinnant    mutex __mut;
3893e519524SHoward Hinnant    condition_variable __cv;
3903e519524SHoward Hinnant    unique_lock<mutex> __lk(__mut);
3913e519524SHoward Hinnant    while (_Clock::now() < __t)
3923e519524SHoward Hinnant        __cv.wait_until(__lk, __t);
3933e519524SHoward Hinnant}
3943e519524SHoward Hinnant
3953e519524SHoward Hinnanttemplate <class _Duration>
396e0601335SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
3973e519524SHoward Hinnantvoid
3983dc6455fSHoward Hinnantsleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t)
3993e519524SHoward Hinnant{
40040950a44SArthur O'Dwyer    this_thread::sleep_for(__t - chrono::steady_clock::now());
4013e519524SHoward Hinnant}
4023e519524SHoward Hinnant
403e0601335SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
404c7e4239fSAsiri Rathnayakevoid yield() _NOEXCEPT {__libcpp_thread_yield();}
4053e519524SHoward Hinnant
406*d2b0df35SNikolas Klauser} // namespace this_thread
4073e519524SHoward Hinnant
4083e519524SHoward Hinnant_LIBCPP_END_NAMESPACE_STD
4093e519524SHoward Hinnant
410b3fcc67fSJonathan Roelofs#endif // !_LIBCPP_HAS_NO_THREADS
411b3fcc67fSJonathan Roelofs
412a016efb1SEric Fiselier_LIBCPP_POP_MACROS
413a016efb1SEric Fiselier
4143e519524SHoward Hinnant#endif // _LIBCPP_THREAD
415