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> 916adbc83eSChristopher Di Bella#include <__utility/forward.h> 92bfbd73f8SArthur O'Dwyer#include <chrono> 933e519524SHoward Hinnant#include <cstddef> 943e519524SHoward Hinnant#include <functional> 95bfbd73f8SArthur O'Dwyer#include <iosfwd> 963e519524SHoward Hinnant#include <memory> 973e519524SHoward Hinnant#include <system_error> 988df61ea8SHoward Hinnant#include <tuple> 99bfbd73f8SArthur O'Dwyer#include <type_traits> 1003e519524SHoward Hinnant 101073458b1SHoward Hinnant#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 1023e519524SHoward Hinnant#pragma GCC system_header 103073458b1SHoward Hinnant#endif 1043e519524SHoward Hinnant 105a016efb1SEric Fiselier_LIBCPP_PUSH_MACROS 106a016efb1SEric Fiselier#include <__undef_macros> 107a016efb1SEric Fiselier 108b3fcc67fSJonathan Roelofs#ifdef _LIBCPP_HAS_NO_THREADS 109b3fcc67fSJonathan Roelofs#error <thread> is not supported on this single threaded system 110b3fcc67fSJonathan Roelofs#else // !_LIBCPP_HAS_NO_THREADS 111b3fcc67fSJonathan Roelofs 1123e519524SHoward Hinnant_LIBCPP_BEGIN_NAMESPACE_STD 1133e519524SHoward Hinnant 1144504cf2cSEric Fiseliertemplate <class _Tp> class __thread_specific_ptr; 1154504cf2cSEric Fiselierclass _LIBCPP_TYPE_VIS __thread_struct; 1164504cf2cSEric Fiselierclass _LIBCPP_HIDDEN __thread_struct_imp; 1174504cf2cSEric Fiselierclass __assoc_sub_state; 1184504cf2cSEric Fiselier 1194504cf2cSEric Fiselier_LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data(); 1204504cf2cSEric Fiselier 1214504cf2cSEric Fiselierclass _LIBCPP_TYPE_VIS __thread_struct 1224504cf2cSEric Fiselier{ 1234504cf2cSEric Fiselier __thread_struct_imp* __p_; 1244504cf2cSEric Fiselier 1254504cf2cSEric Fiselier __thread_struct(const __thread_struct&); 1264504cf2cSEric Fiselier __thread_struct& operator=(const __thread_struct&); 1274504cf2cSEric Fiselierpublic: 1284504cf2cSEric Fiselier __thread_struct(); 1294504cf2cSEric Fiselier ~__thread_struct(); 1304504cf2cSEric Fiselier 1314504cf2cSEric Fiselier void notify_all_at_thread_exit(condition_variable*, mutex*); 1324504cf2cSEric Fiselier void __make_ready_at_thread_exit(__assoc_sub_state*); 1334504cf2cSEric Fiselier}; 1344504cf2cSEric Fiselier 135167fd108SHoward Hinnanttemplate <class _Tp> 136167fd108SHoward Hinnantclass __thread_specific_ptr 137167fd108SHoward Hinnant{ 1388c2bf45dSAsiri Rathnayake __libcpp_tls_key __key_; 139167fd108SHoward Hinnant 1404504cf2cSEric Fiselier // Only __thread_local_data() may construct a __thread_specific_ptr 1414504cf2cSEric Fiselier // and only with _Tp == __thread_struct. 1429a5e62bfSEric Fiselier static_assert((is_same<_Tp, __thread_struct>::value), ""); 1434504cf2cSEric Fiselier __thread_specific_ptr(); 1444504cf2cSEric Fiselier friend _LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data(); 1454504cf2cSEric Fiselier 146167fd108SHoward Hinnant __thread_specific_ptr(const __thread_specific_ptr&); 147167fd108SHoward Hinnant __thread_specific_ptr& operator=(const __thread_specific_ptr&); 148167fd108SHoward Hinnant 1499b3222f6SLouis Dionne _LIBCPP_HIDDEN static void _LIBCPP_TLS_DESTRUCTOR_CC __at_thread_exit(void*); 1506d7d0becSSaleem Abdulrasool 151167fd108SHoward Hinnantpublic: 152167fd108SHoward Hinnant typedef _Tp* pointer; 153167fd108SHoward Hinnant 154167fd108SHoward Hinnant ~__thread_specific_ptr(); 155167fd108SHoward Hinnant 156e0601335SHoward Hinnant _LIBCPP_INLINE_VISIBILITY 1578c2bf45dSAsiri Rathnayake pointer get() const {return static_cast<_Tp*>(__libcpp_tls_get(__key_));} 158e0601335SHoward Hinnant _LIBCPP_INLINE_VISIBILITY 159167fd108SHoward Hinnant pointer operator*() const {return *get();} 160e0601335SHoward Hinnant _LIBCPP_INLINE_VISIBILITY 161167fd108SHoward Hinnant pointer operator->() const {return get();} 162ff94d250SEric Fiselier void set_pointer(pointer __p); 163167fd108SHoward Hinnant}; 164167fd108SHoward Hinnant 165167fd108SHoward Hinnanttemplate <class _Tp> 1666d7d0becSSaleem Abdulrasoolvoid _LIBCPP_TLS_DESTRUCTOR_CC 167167fd108SHoward Hinnant__thread_specific_ptr<_Tp>::__at_thread_exit(void* __p) 168167fd108SHoward Hinnant{ 169167fd108SHoward Hinnant delete static_cast<pointer>(__p); 170167fd108SHoward Hinnant} 171167fd108SHoward Hinnant 172167fd108SHoward Hinnanttemplate <class _Tp> 173167fd108SHoward Hinnant__thread_specific_ptr<_Tp>::__thread_specific_ptr() 174167fd108SHoward Hinnant{ 1756d7d0becSSaleem Abdulrasool int __ec = 1766d7d0becSSaleem Abdulrasool __libcpp_tls_create(&__key_, &__thread_specific_ptr::__at_thread_exit); 177167fd108SHoward Hinnant if (__ec) 1786d7d0becSSaleem Abdulrasool __throw_system_error(__ec, "__thread_specific_ptr construction failed"); 179167fd108SHoward Hinnant} 180167fd108SHoward Hinnant 181167fd108SHoward Hinnanttemplate <class _Tp> 182167fd108SHoward Hinnant__thread_specific_ptr<_Tp>::~__thread_specific_ptr() 183167fd108SHoward Hinnant{ 1844504cf2cSEric Fiselier // __thread_specific_ptr is only created with a static storage duration 1854504cf2cSEric Fiselier // so this destructor is only invoked during program termination. Invoking 1864504cf2cSEric Fiselier // pthread_key_delete(__key_) may prevent other threads from deleting their 1874504cf2cSEric Fiselier // thread local data. For this reason we leak the key. 188167fd108SHoward Hinnant} 189167fd108SHoward Hinnant 190167fd108SHoward Hinnanttemplate <class _Tp> 191167fd108SHoward Hinnantvoid 192ff94d250SEric Fiselier__thread_specific_ptr<_Tp>::set_pointer(pointer __p) 193167fd108SHoward Hinnant{ 194ff94d250SEric Fiselier _LIBCPP_ASSERT(get() == nullptr, 195ff94d250SEric Fiselier "Attempting to overwrite thread local data"); 1968c2bf45dSAsiri Rathnayake __libcpp_tls_set(__key_, __p); 197167fd108SHoward Hinnant} 198167fd108SHoward Hinnant 1993e519524SHoward Hinnanttemplate<> 200e2f2d1edSEric Fiselierstruct _LIBCPP_TEMPLATE_VIS hash<__thread_id> 2013e519524SHoward Hinnant : public unary_function<__thread_id, size_t> 2023e519524SHoward Hinnant{ 203e0601335SHoward Hinnant _LIBCPP_INLINE_VISIBILITY 2047c803385SMarshall Clow size_t operator()(__thread_id __v) const _NOEXCEPT 2053e519524SHoward Hinnant { 206c7e4239fSAsiri Rathnayake return hash<__libcpp_thread_id>()(__v.__id_); 2073e519524SHoward Hinnant } 2083e519524SHoward Hinnant}; 2093e519524SHoward Hinnant 2102b1d4254SMarshall Clowtemplate<class _CharT, class _Traits> 2112b1d4254SMarshall Clow_LIBCPP_INLINE_VISIBILITY 2122b1d4254SMarshall Clowbasic_ostream<_CharT, _Traits>& 2132b1d4254SMarshall Clowoperator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id) 2142b1d4254SMarshall Clow{return __os << __id.__id_;} 2153e519524SHoward Hinnant 2166e41256fSHoward Hinnantclass _LIBCPP_TYPE_VIS thread 2173e519524SHoward Hinnant{ 218c7e4239fSAsiri Rathnayake __libcpp_thread_t __t_; 2193e519524SHoward Hinnant 220eb269257SHoward Hinnant thread(const thread&); 221eb269257SHoward Hinnant thread& operator=(const thread&); 2223e519524SHoward Hinnantpublic: 2233e519524SHoward Hinnant typedef __thread_id id; 224c7e4239fSAsiri Rathnayake typedef __libcpp_thread_t native_handle_type; 2253e519524SHoward Hinnant 226e0601335SHoward Hinnant _LIBCPP_INLINE_VISIBILITY 227af762e91SAsiri Rathnayake thread() _NOEXCEPT : __t_(_LIBCPP_NULL_THREAD) {} 228e11fb13bSEric Fiselier#ifndef _LIBCPP_CXX03_LANG 229c003db1fSHoward Hinnant template <class _Fp, class ..._Args, 2303e519524SHoward Hinnant class = typename enable_if 2313e519524SHoward Hinnant < 23269f1072bSMarshall Clow !is_same<typename __uncvref<_Fp>::type, thread>::value 2333e519524SHoward Hinnant >::type 2343e519524SHoward Hinnant > 235bda3c7dfSShoaib Meenai _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS 236c003db1fSHoward Hinnant explicit thread(_Fp&& __f, _Args&&... __args); 237e11fb13bSEric Fiselier#else // _LIBCPP_CXX03_LANG 238bda3c7dfSShoaib Meenai template <class _Fp> 239bda3c7dfSShoaib Meenai _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS 240bda3c7dfSShoaib Meenai explicit thread(_Fp __f); 2413e519524SHoward Hinnant#endif 2423e519524SHoward Hinnant ~thread(); 2433e519524SHoward Hinnant 244e0601335SHoward Hinnant _LIBCPP_INLINE_VISIBILITY 24562cfa3a0SLouis Dionne thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) { 24662cfa3a0SLouis Dionne __t.__t_ = _LIBCPP_NULL_THREAD; 24762cfa3a0SLouis Dionne } 24862cfa3a0SLouis Dionne 249906c872dSEvgeniy Stepanov _LIBCPP_INLINE_VISIBILITY 25062cfa3a0SLouis Dionne thread& operator=(thread&& __t) _NOEXCEPT { 25162cfa3a0SLouis Dionne if (!__libcpp_thread_isnull(&__t_)) 25262cfa3a0SLouis Dionne terminate(); 25362cfa3a0SLouis Dionne __t_ = __t.__t_; 25462cfa3a0SLouis Dionne __t.__t_ = _LIBCPP_NULL_THREAD; 25562cfa3a0SLouis Dionne return *this; 25662cfa3a0SLouis Dionne } 2573e519524SHoward Hinnant 258e0601335SHoward Hinnant _LIBCPP_INLINE_VISIBILITY 25936101a5bSHoward Hinnant void swap(thread& __t) _NOEXCEPT {_VSTD::swap(__t_, __t.__t_);} 2603e519524SHoward Hinnant 261e0601335SHoward Hinnant _LIBCPP_INLINE_VISIBILITY 262af762e91SAsiri Rathnayake bool joinable() const _NOEXCEPT {return !__libcpp_thread_isnull(&__t_);} 2633e519524SHoward Hinnant void join(); 2643e519524SHoward Hinnant void detach(); 265e0601335SHoward Hinnant _LIBCPP_INLINE_VISIBILITY 266c7e4239fSAsiri Rathnayake id get_id() const _NOEXCEPT {return __libcpp_thread_get_id(&__t_);} 267e0601335SHoward Hinnant _LIBCPP_INLINE_VISIBILITY 26836101a5bSHoward Hinnant native_handle_type native_handle() _NOEXCEPT {return __t_;} 2693e519524SHoward Hinnant 27036101a5bSHoward Hinnant static unsigned hardware_concurrency() _NOEXCEPT; 2713e519524SHoward Hinnant}; 2723e519524SHoward Hinnant 273e11fb13bSEric Fiselier#ifndef _LIBCPP_CXX03_LANG 2748df61ea8SHoward Hinnant 275e08afaf8SEric Fiseliertemplate <class _TSp, class _Fp, class ..._Args, size_t ..._Indices> 2768df61ea8SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY 2778df61ea8SHoward Hinnantvoid 278e08afaf8SEric Fiselier__thread_execute(tuple<_TSp, _Fp, _Args...>& __t, __tuple_indices<_Indices...>) 2798df61ea8SHoward Hinnant{ 28040950a44SArthur O'Dwyer _VSTD::__invoke(_VSTD::move(_VSTD::get<1>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...); 2818df61ea8SHoward Hinnant} 2828df61ea8SHoward Hinnant 283c003db1fSHoward Hinnanttemplate <class _Fp> 284dd37e24aSLouis Dionne_LIBCPP_INLINE_VISIBILITY 285e08afaf8SEric Fiseliervoid* __thread_proxy(void* __vp) 2868df61ea8SHoward Hinnant{ 28740950a44SArthur O'Dwyer // _Fp = tuple< unique_ptr<__thread_struct>, Functor, Args...> 28840950a44SArthur O'Dwyer unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp)); 28940950a44SArthur O'Dwyer __thread_local_data().set_pointer(_VSTD::get<0>(*__p.get()).release()); 290e08afaf8SEric Fiselier typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 2>::type _Index; 29140950a44SArthur O'Dwyer _VSTD::__thread_execute(*__p.get(), _Index()); 2928df61ea8SHoward Hinnant return nullptr; 2938df61ea8SHoward Hinnant} 2948df61ea8SHoward Hinnant 295c003db1fSHoward Hinnanttemplate <class _Fp, class ..._Args, 2968df61ea8SHoward Hinnant class 2978df61ea8SHoward Hinnant > 298c003db1fSHoward Hinnantthread::thread(_Fp&& __f, _Args&&... __args) 2998df61ea8SHoward Hinnant{ 300e08afaf8SEric Fiselier typedef unique_ptr<__thread_struct> _TSPtr; 301e08afaf8SEric Fiselier _TSPtr __tsp(new __thread_struct); 302e08afaf8SEric Fiselier typedef tuple<_TSPtr, typename decay<_Fp>::type, typename decay<_Args>::type...> _Gp; 30340950a44SArthur O'Dwyer unique_ptr<_Gp> __p( 30440950a44SArthur O'Dwyer new _Gp(_VSTD::move(__tsp), 305*cb8a0b07SArthur O'Dwyer _VSTD::forward<_Fp>(__f), 306*cb8a0b07SArthur O'Dwyer _VSTD::forward<_Args>(__args)...)); 30740950a44SArthur O'Dwyer int __ec = _VSTD::__libcpp_thread_create(&__t_, &__thread_proxy<_Gp>, __p.get()); 3088df61ea8SHoward Hinnant if (__ec == 0) 3098df61ea8SHoward Hinnant __p.release(); 3108df61ea8SHoward Hinnant else 3118df61ea8SHoward Hinnant __throw_system_error(__ec, "thread constructor failed"); 3128df61ea8SHoward Hinnant} 3138df61ea8SHoward Hinnant 314e11fb13bSEric Fiselier#else // _LIBCPP_CXX03_LANG 3158df61ea8SHoward Hinnant 316c003db1fSHoward Hinnanttemplate <class _Fp> 317e08afaf8SEric Fiselierstruct __thread_invoke_pair { 318e08afaf8SEric Fiselier // This type is used to pass memory for thread local storage and a functor 319e08afaf8SEric Fiselier // to a newly created thread because std::pair doesn't work with 320e08afaf8SEric Fiselier // std::unique_ptr in C++03. 321e08afaf8SEric Fiselier __thread_invoke_pair(_Fp& __f) : __tsp_(new __thread_struct), __fn_(__f) {} 322e08afaf8SEric Fiselier unique_ptr<__thread_struct> __tsp_; 323e08afaf8SEric Fiselier _Fp __fn_; 324e08afaf8SEric Fiselier}; 325e08afaf8SEric Fiselier 326e08afaf8SEric Fiseliertemplate <class _Fp> 327e08afaf8SEric Fiseliervoid* __thread_proxy_cxx03(void* __vp) 3283e519524SHoward Hinnant{ 32940950a44SArthur O'Dwyer unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp)); 330ff94d250SEric Fiselier __thread_local_data().set_pointer(__p->__tsp_.release()); 331e08afaf8SEric Fiselier (__p->__fn_)(); 3323e519524SHoward Hinnant return nullptr; 3333e519524SHoward Hinnant} 3343e519524SHoward Hinnant 335c003db1fSHoward Hinnanttemplate <class _Fp> 336c003db1fSHoward Hinnantthread::thread(_Fp __f) 3373e519524SHoward Hinnant{ 338e08afaf8SEric Fiselier 339e08afaf8SEric Fiselier typedef __thread_invoke_pair<_Fp> _InvokePair; 34040950a44SArthur O'Dwyer typedef unique_ptr<_InvokePair> _PairPtr; 341e08afaf8SEric Fiselier _PairPtr __pp(new _InvokePair(__f)); 34240950a44SArthur O'Dwyer int __ec = _VSTD::__libcpp_thread_create(&__t_, &__thread_proxy_cxx03<_InvokePair>, __pp.get()); 3433e519524SHoward Hinnant if (__ec == 0) 344e08afaf8SEric Fiselier __pp.release(); 3453e519524SHoward Hinnant else 3463e519524SHoward Hinnant __throw_system_error(__ec, "thread constructor failed"); 3473e519524SHoward Hinnant} 3483e519524SHoward Hinnant 349e11fb13bSEric Fiselier#endif // _LIBCPP_CXX03_LANG 3503e519524SHoward Hinnant 351e0601335SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY 35236101a5bSHoward Hinnantvoid swap(thread& __x, thread& __y) _NOEXCEPT {__x.swap(__y);} 3533e519524SHoward Hinnant 3543e519524SHoward Hinnantnamespace this_thread 3553e519524SHoward Hinnant{ 3563e519524SHoward Hinnant 3579e8a082dSJoerg Sonnenberger_LIBCPP_FUNC_VIS void sleep_for(const chrono::nanoseconds& __ns); 3583e519524SHoward Hinnant 3593e519524SHoward Hinnanttemplate <class _Rep, class _Period> 3603e519524SHoward Hinnantvoid 3613e519524SHoward Hinnantsleep_for(const chrono::duration<_Rep, _Period>& __d) 3623e519524SHoward Hinnant{ 36340950a44SArthur O'Dwyer if (__d > chrono::duration<_Rep, _Period>::zero()) 364aad745a0SHoward Hinnant { 3659f4022ffSJoerg Sonnenberger // The standard guarantees a 64bit signed integer resolution for nanoseconds, 3669f4022ffSJoerg Sonnenberger // so use INT64_MAX / 1e9 as cut-off point. Use a constant to avoid <climits> 3679f4022ffSJoerg Sonnenberger // and issues with long double folding on PowerPC with GCC. 3689f4022ffSJoerg Sonnenberger _LIBCPP_CONSTEXPR chrono::duration<long double> _Max = 3699f4022ffSJoerg Sonnenberger chrono::duration<long double>(9223372036.0L); 37040950a44SArthur O'Dwyer chrono::nanoseconds __ns; 371aad745a0SHoward Hinnant if (__d < _Max) 372aad745a0SHoward Hinnant { 37340950a44SArthur O'Dwyer __ns = chrono::duration_cast<chrono::nanoseconds>(__d); 3743e519524SHoward Hinnant if (__ns < __d) 3753e519524SHoward Hinnant ++__ns; 376aad745a0SHoward Hinnant } 377aad745a0SHoward Hinnant else 37840950a44SArthur O'Dwyer __ns = chrono::nanoseconds::max(); 37940950a44SArthur O'Dwyer this_thread::sleep_for(__ns); 3803e519524SHoward Hinnant } 381aad745a0SHoward Hinnant} 3823e519524SHoward Hinnant 3833e519524SHoward Hinnanttemplate <class _Clock, class _Duration> 3843e519524SHoward Hinnantvoid 3853e519524SHoward Hinnantsleep_until(const chrono::time_point<_Clock, _Duration>& __t) 3863e519524SHoward Hinnant{ 3873e519524SHoward Hinnant mutex __mut; 3883e519524SHoward Hinnant condition_variable __cv; 3893e519524SHoward Hinnant unique_lock<mutex> __lk(__mut); 3903e519524SHoward Hinnant while (_Clock::now() < __t) 3913e519524SHoward Hinnant __cv.wait_until(__lk, __t); 3923e519524SHoward Hinnant} 3933e519524SHoward Hinnant 3943e519524SHoward Hinnanttemplate <class _Duration> 395e0601335SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY 3963e519524SHoward Hinnantvoid 3973dc6455fSHoward Hinnantsleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t) 3983e519524SHoward Hinnant{ 39940950a44SArthur O'Dwyer this_thread::sleep_for(__t - chrono::steady_clock::now()); 4003e519524SHoward Hinnant} 4013e519524SHoward Hinnant 402e0601335SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY 403c7e4239fSAsiri Rathnayakevoid yield() _NOEXCEPT {__libcpp_thread_yield();} 4043e519524SHoward Hinnant 405d2b0df35SNikolas Klauser} // namespace this_thread 4063e519524SHoward Hinnant 4073e519524SHoward Hinnant_LIBCPP_END_NAMESPACE_STD 4083e519524SHoward Hinnant 409b3fcc67fSJonathan Roelofs#endif // !_LIBCPP_HAS_NO_THREADS 410b3fcc67fSJonathan Roelofs 411a016efb1SEric Fiselier_LIBCPP_POP_MACROS 412a016efb1SEric Fiselier 4133e519524SHoward Hinnant#endif // _LIBCPP_THREAD 414