blob: ff3c7463c094d416532399be06475f9a1e95ea2d [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===--------------------------- thread -----------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_THREAD
11#define _LIBCPP_THREAD
12
13/*
14
15 thread synopsis
16
Howard Hinnantc51e1022010-05-11 19:42:16 +000017namespace std
18{
19
20class thread
21{
22public:
23 class id;
24 typedef pthread_t native_handle_type;
25
Howard Hinnant4bd34702012-07-21 16:50:47 +000026 thread() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000027 template <class F, class ...Args> explicit thread(F&& f, Args&&... args);
28 ~thread();
29
30 thread(const thread&) = delete;
Howard Hinnant4bd34702012-07-21 16:50:47 +000031 thread(thread&& t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000032
33 thread& operator=(const thread&) = delete;
Howard Hinnant4bd34702012-07-21 16:50:47 +000034 thread& operator=(thread&& t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000035
Howard Hinnant4bd34702012-07-21 16:50:47 +000036 void swap(thread& t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000037
Howard Hinnant4bd34702012-07-21 16:50:47 +000038 bool joinable() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000039 void join();
40 void detach();
Howard Hinnant4bd34702012-07-21 16:50:47 +000041 id get_id() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000042 native_handle_type native_handle();
43
Howard Hinnant4bd34702012-07-21 16:50:47 +000044 static unsigned hardware_concurrency() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000045};
46
Howard Hinnant4bd34702012-07-21 16:50:47 +000047void swap(thread& x, thread& y) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000048
49class thread::id
50{
51public:
Howard Hinnant4bd34702012-07-21 16:50:47 +000052 id() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000053};
54
Howard Hinnant4bd34702012-07-21 16:50:47 +000055bool operator==(thread::id x, thread::id y) noexcept;
56bool operator!=(thread::id x, thread::id y) noexcept;
57bool operator< (thread::id x, thread::id y) noexcept;
58bool operator<=(thread::id x, thread::id y) noexcept;
59bool operator> (thread::id x, thread::id y) noexcept;
60bool operator>=(thread::id x, thread::id y) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000061
62template<class charT, class traits>
63basic_ostream<charT, traits>&
64operator<<(basic_ostream<charT, traits>& out, thread::id id);
65
66namespace this_thread
67{
68
Howard Hinnant4bd34702012-07-21 16:50:47 +000069thread::id get_id() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000070
Howard Hinnant4bd34702012-07-21 16:50:47 +000071void yield() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000072
73template <class Clock, class Duration>
74void sleep_until(const chrono::time_point<Clock, Duration>& abs_time);
75
76template <class Rep, class Period>
77void sleep_for(const chrono::duration<Rep, Period>& rel_time);
78
79} // this_thread
80
81} // std
82
83*/
84
85#include <__config>
86#include <iosfwd>
87#include <__functional_base>
88#include <type_traits>
89#include <cstddef>
90#include <functional>
91#include <memory>
92#include <system_error>
93#include <chrono>
94#include <__mutex_base>
Eric Fiselierb6159752017-04-18 23:05:08 +000095#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant4eea16b2011-05-16 18:40:35 +000096#include <tuple>
97#endif
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +000098#include <__threading_support>
Eric Fiselier99245c52016-09-03 08:07:40 +000099#include <__debug>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000100
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000101#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000102#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000103#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000104
Eric Fiselierf4433a32017-05-31 22:07:49 +0000105_LIBCPP_PUSH_MACROS
106#include <__undef_macros>
107
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000108#ifdef _LIBCPP_HAS_NO_THREADS
109#error <thread> is not supported on this single threaded system
110#else // !_LIBCPP_HAS_NO_THREADS
111
Howard Hinnantc51e1022010-05-11 19:42:16 +0000112_LIBCPP_BEGIN_NAMESPACE_STD
113
Eric Fiselier80b0e152015-08-18 19:40:38 +0000114template <class _Tp> class __thread_specific_ptr;
115class _LIBCPP_TYPE_VIS __thread_struct;
116class _LIBCPP_HIDDEN __thread_struct_imp;
117class __assoc_sub_state;
118
119_LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data();
120
121class _LIBCPP_TYPE_VIS __thread_struct
122{
123 __thread_struct_imp* __p_;
124
125 __thread_struct(const __thread_struct&);
126 __thread_struct& operator=(const __thread_struct&);
127public:
128 __thread_struct();
129 ~__thread_struct();
130
131 void notify_all_at_thread_exit(condition_variable*, mutex*);
132 void __make_ready_at_thread_exit(__assoc_sub_state*);
133};
134
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000135template <class _Tp>
136class __thread_specific_ptr
137{
Asiri Rathnayake94340d22016-09-11 21:46:40 +0000138 __libcpp_tls_key __key_;
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000139
Eric Fiselier80b0e152015-08-18 19:40:38 +0000140 // Only __thread_local_data() may construct a __thread_specific_ptr
141 // and only with _Tp == __thread_struct.
Eric Fiselierf34995b2015-08-19 03:38:41 +0000142 static_assert((is_same<_Tp, __thread_struct>::value), "");
Eric Fiselier80b0e152015-08-18 19:40:38 +0000143 __thread_specific_ptr();
144 friend _LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data();
145
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000146 __thread_specific_ptr(const __thread_specific_ptr&);
147 __thread_specific_ptr& operator=(const __thread_specific_ptr&);
148
Louis Dionne5254b372018-10-25 12:13:43 +0000149 _LIBCPP_HIDDEN static void _LIBCPP_TLS_DESTRUCTOR_CC __at_thread_exit(void*);
Saleem Abdulrasoolb75a5742017-01-07 03:07:45 +0000150
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000151public:
152 typedef _Tp* pointer;
153
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000154 ~__thread_specific_ptr();
155
Howard Hinnant186dca82010-09-23 17:31:07 +0000156 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayake94340d22016-09-11 21:46:40 +0000157 pointer get() const {return static_cast<_Tp*>(__libcpp_tls_get(__key_));}
Howard Hinnant186dca82010-09-23 17:31:07 +0000158 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000159 pointer operator*() const {return *get();}
Howard Hinnant186dca82010-09-23 17:31:07 +0000160 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000161 pointer operator->() const {return get();}
Eric Fiselier99245c52016-09-03 08:07:40 +0000162 void set_pointer(pointer __p);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000163};
164
165template <class _Tp>
Saleem Abdulrasoolb75a5742017-01-07 03:07:45 +0000166void _LIBCPP_TLS_DESTRUCTOR_CC
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000167__thread_specific_ptr<_Tp>::__at_thread_exit(void* __p)
168{
Howard Hinnant0eef25b2011-10-17 20:08:59 +0000169 delete static_cast<pointer>(__p);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000170}
171
172template <class _Tp>
173__thread_specific_ptr<_Tp>::__thread_specific_ptr()
174{
Saleem Abdulrasoolb75a5742017-01-07 03:07:45 +0000175 int __ec =
176 __libcpp_tls_create(&__key_, &__thread_specific_ptr::__at_thread_exit);
177 if (__ec)
178 __throw_system_error(__ec, "__thread_specific_ptr construction failed");
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000179}
180
181template <class _Tp>
182__thread_specific_ptr<_Tp>::~__thread_specific_ptr()
183{
Eric Fiselier80b0e152015-08-18 19:40:38 +0000184 // __thread_specific_ptr is only created with a static storage duration
185 // so this destructor is only invoked during program termination. Invoking
186 // pthread_key_delete(__key_) may prevent other threads from deleting their
187 // thread local data. For this reason we leak the key.
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000188}
189
190template <class _Tp>
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000191void
Eric Fiselier99245c52016-09-03 08:07:40 +0000192__thread_specific_ptr<_Tp>::set_pointer(pointer __p)
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000193{
Eric Fiselier99245c52016-09-03 08:07:40 +0000194 _LIBCPP_ASSERT(get() == nullptr,
195 "Attempting to overwrite thread local data");
Asiri Rathnayake94340d22016-09-11 21:46:40 +0000196 __libcpp_tls_set(__key_, __p);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000197}
198
Howard Hinnantc51e1022010-05-11 19:42:16 +0000199template<>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000200struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000201 : public unary_function<__thread_id, size_t>
202{
Howard Hinnant186dca82010-09-23 17:31:07 +0000203 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +0000204 size_t operator()(__thread_id __v) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000205 {
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000206 return hash<__libcpp_thread_id>()(__v.__id_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000207 }
208};
209
Marshall Clow58655362019-08-14 16:21:27 +0000210template<class _CharT, class _Traits>
211_LIBCPP_INLINE_VISIBILITY
212basic_ostream<_CharT, _Traits>&
213operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id)
214{return __os << __id.__id_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000215
Howard Hinnant8331b762013-03-06 23:30:19 +0000216class _LIBCPP_TYPE_VIS thread
Howard Hinnantc51e1022010-05-11 19:42:16 +0000217{
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000218 __libcpp_thread_t __t_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000219
Howard Hinnantaa87c0f2010-08-10 20:48:29 +0000220 thread(const thread&);
221 thread& operator=(const thread&);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000222public:
223 typedef __thread_id id;
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000224 typedef __libcpp_thread_t native_handle_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000225
Howard Hinnant186dca82010-09-23 17:31:07 +0000226 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayake3bab36f2017-01-16 12:19:54 +0000227 thread() _NOEXCEPT : __t_(_LIBCPP_NULL_THREAD) {}
Eric Fiselierb6159752017-04-18 23:05:08 +0000228#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc834c512011-11-29 18:15:50 +0000229 template <class _Fp, class ..._Args,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000230 class = typename enable_if
231 <
Marshall Clowaaf031b2018-03-20 22:37:37 +0000232 !is_same<typename __uncvref<_Fp>::type, thread>::value
Howard Hinnantc51e1022010-05-11 19:42:16 +0000233 >::type
234 >
Shoaib Meenai55f3a462017-03-02 03:22:18 +0000235 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Howard Hinnantc834c512011-11-29 18:15:50 +0000236 explicit thread(_Fp&& __f, _Args&&... __args);
Eric Fiselierb6159752017-04-18 23:05:08 +0000237#else // _LIBCPP_CXX03_LANG
Shoaib Meenai55f3a462017-03-02 03:22:18 +0000238 template <class _Fp>
239 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
240 explicit thread(_Fp __f);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000241#endif
242 ~thread();
243
Howard Hinnant186dca82010-09-23 17:31:07 +0000244 _LIBCPP_INLINE_VISIBILITY
Louis Dionne0b1dda02020-06-03 11:51:59 -0400245 thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) {
246 __t.__t_ = _LIBCPP_NULL_THREAD;
247 }
248
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000249 _LIBCPP_INLINE_VISIBILITY
Louis Dionne0b1dda02020-06-03 11:51:59 -0400250 thread& operator=(thread&& __t) _NOEXCEPT {
251 if (!__libcpp_thread_isnull(&__t_))
252 terminate();
253 __t_ = __t.__t_;
254 __t.__t_ = _LIBCPP_NULL_THREAD;
255 return *this;
256 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000257
Howard Hinnant186dca82010-09-23 17:31:07 +0000258 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000259 void swap(thread& __t) _NOEXCEPT {_VSTD::swap(__t_, __t.__t_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000260
Howard Hinnant186dca82010-09-23 17:31:07 +0000261 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayake3bab36f2017-01-16 12:19:54 +0000262 bool joinable() const _NOEXCEPT {return !__libcpp_thread_isnull(&__t_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000263 void join();
264 void detach();
Howard Hinnant186dca82010-09-23 17:31:07 +0000265 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000266 id get_id() const _NOEXCEPT {return __libcpp_thread_get_id(&__t_);}
Howard Hinnant186dca82010-09-23 17:31:07 +0000267 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000268 native_handle_type native_handle() _NOEXCEPT {return __t_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000269
Howard Hinnant4bd34702012-07-21 16:50:47 +0000270 static unsigned hardware_concurrency() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000271};
272
Eric Fiselierb6159752017-04-18 23:05:08 +0000273#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000274
Eric Fiselierfd996062016-04-20 02:21:33 +0000275template <class _TSp, class _Fp, class ..._Args, size_t ..._Indices>
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000276inline _LIBCPP_INLINE_VISIBILITY
277void
Eric Fiselierfd996062016-04-20 02:21:33 +0000278__thread_execute(tuple<_TSp, _Fp, _Args...>& __t, __tuple_indices<_Indices...>)
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000279{
Arthur O'Dwyerbaac1392020-11-27 14:44:53 -0500280 _VSTD::__invoke(_VSTD::move(_VSTD::get<1>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000281}
282
Howard Hinnantc834c512011-11-29 18:15:50 +0000283template <class _Fp>
Louis Dionne3b967d02019-12-10 18:00:42 -0500284_LIBCPP_INLINE_VISIBILITY
Eric Fiselierfd996062016-04-20 02:21:33 +0000285void* __thread_proxy(void* __vp)
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000286{
Arthur O'Dwyerbaac1392020-11-27 14:44:53 -0500287 // _Fp = tuple< unique_ptr<__thread_struct>, Functor, Args...>
288 unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
289 __thread_local_data().set_pointer(_VSTD::get<0>(*__p.get()).release());
Eric Fiselierfd996062016-04-20 02:21:33 +0000290 typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 2>::type _Index;
Arthur O'Dwyerbaac1392020-11-27 14:44:53 -0500291 _VSTD::__thread_execute(*__p.get(), _Index());
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000292 return nullptr;
293}
294
Howard Hinnantc834c512011-11-29 18:15:50 +0000295template <class _Fp, class ..._Args,
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000296 class
297 >
Howard Hinnantc834c512011-11-29 18:15:50 +0000298thread::thread(_Fp&& __f, _Args&&... __args)
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000299{
Eric Fiselierfd996062016-04-20 02:21:33 +0000300 typedef unique_ptr<__thread_struct> _TSPtr;
301 _TSPtr __tsp(new __thread_struct);
302 typedef tuple<_TSPtr, typename decay<_Fp>::type, typename decay<_Args>::type...> _Gp;
Arthur O'Dwyerbaac1392020-11-27 14:44:53 -0500303 unique_ptr<_Gp> __p(
304 new _Gp(_VSTD::move(__tsp),
305 _VSTD::__decay_copy(_VSTD::forward<_Fp>(__f)),
306 _VSTD::__decay_copy(_VSTD::forward<_Args>(__args))...));
307 int __ec = _VSTD::__libcpp_thread_create(&__t_, &__thread_proxy<_Gp>, __p.get());
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000308 if (__ec == 0)
309 __p.release();
310 else
311 __throw_system_error(__ec, "thread constructor failed");
312}
313
Eric Fiselierb6159752017-04-18 23:05:08 +0000314#else // _LIBCPP_CXX03_LANG
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000315
Howard Hinnantc834c512011-11-29 18:15:50 +0000316template <class _Fp>
Eric Fiselierfd996062016-04-20 02:21:33 +0000317struct __thread_invoke_pair {
318 // This type is used to pass memory for thread local storage and a functor
319 // to a newly created thread because std::pair doesn't work with
320 // std::unique_ptr in C++03.
321 __thread_invoke_pair(_Fp& __f) : __tsp_(new __thread_struct), __fn_(__f) {}
322 unique_ptr<__thread_struct> __tsp_;
323 _Fp __fn_;
324};
325
326template <class _Fp>
327void* __thread_proxy_cxx03(void* __vp)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000328{
Arthur O'Dwyerbaac1392020-11-27 14:44:53 -0500329 unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
Eric Fiselier99245c52016-09-03 08:07:40 +0000330 __thread_local_data().set_pointer(__p->__tsp_.release());
Eric Fiselierfd996062016-04-20 02:21:33 +0000331 (__p->__fn_)();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000332 return nullptr;
333}
334
Howard Hinnantc834c512011-11-29 18:15:50 +0000335template <class _Fp>
336thread::thread(_Fp __f)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000337{
Eric Fiselierfd996062016-04-20 02:21:33 +0000338
339 typedef __thread_invoke_pair<_Fp> _InvokePair;
Arthur O'Dwyerbaac1392020-11-27 14:44:53 -0500340 typedef unique_ptr<_InvokePair> _PairPtr;
Eric Fiselierfd996062016-04-20 02:21:33 +0000341 _PairPtr __pp(new _InvokePair(__f));
Arthur O'Dwyerbaac1392020-11-27 14:44:53 -0500342 int __ec = _VSTD::__libcpp_thread_create(&__t_, &__thread_proxy_cxx03<_InvokePair>, __pp.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000343 if (__ec == 0)
Eric Fiselierfd996062016-04-20 02:21:33 +0000344 __pp.release();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000345 else
346 __throw_system_error(__ec, "thread constructor failed");
347}
348
Eric Fiselierb6159752017-04-18 23:05:08 +0000349#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000350
Howard Hinnant186dca82010-09-23 17:31:07 +0000351inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000352void swap(thread& __x, thread& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000353
Howard Hinnantc51e1022010-05-11 19:42:16 +0000354namespace this_thread
355{
356
Joerg Sonnenberger917a30a2017-02-09 14:12:29 +0000357_LIBCPP_FUNC_VIS void sleep_for(const chrono::nanoseconds& __ns);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000358
359template <class _Rep, class _Period>
360void
361sleep_for(const chrono::duration<_Rep, _Period>& __d)
362{
Arthur O'Dwyerbaac1392020-11-27 14:44:53 -0500363 if (__d > chrono::duration<_Rep, _Period>::zero())
Howard Hinnant9ce8dc52012-08-30 19:14:33 +0000364 {
Joerg Sonnenberger68796c72021-02-18 15:15:53 +0100365 // The standard guarantees a 64bit signed integer resolution for nanoseconds,
366 // so use INT64_MAX / 1e9 as cut-off point. Use a constant to avoid <climits>
367 // and issues with long double folding on PowerPC with GCC.
368 _LIBCPP_CONSTEXPR chrono::duration<long double> _Max =
369 chrono::duration<long double>(9223372036.0L);
Arthur O'Dwyerbaac1392020-11-27 14:44:53 -0500370 chrono::nanoseconds __ns;
Howard Hinnant9ce8dc52012-08-30 19:14:33 +0000371 if (__d < _Max)
372 {
Arthur O'Dwyerbaac1392020-11-27 14:44:53 -0500373 __ns = chrono::duration_cast<chrono::nanoseconds>(__d);
Howard Hinnant9ce8dc52012-08-30 19:14:33 +0000374 if (__ns < __d)
375 ++__ns;
376 }
377 else
Arthur O'Dwyerbaac1392020-11-27 14:44:53 -0500378 __ns = chrono::nanoseconds::max();
379 this_thread::sleep_for(__ns);
Howard Hinnant9ce8dc52012-08-30 19:14:33 +0000380 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000381}
382
383template <class _Clock, class _Duration>
384void
385sleep_until(const chrono::time_point<_Clock, _Duration>& __t)
386{
Howard Hinnantc51e1022010-05-11 19:42:16 +0000387 mutex __mut;
388 condition_variable __cv;
389 unique_lock<mutex> __lk(__mut);
390 while (_Clock::now() < __t)
391 __cv.wait_until(__lk, __t);
392}
393
394template <class _Duration>
Howard Hinnant186dca82010-09-23 17:31:07 +0000395inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000396void
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000397sleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000398{
Arthur O'Dwyerbaac1392020-11-27 14:44:53 -0500399 this_thread::sleep_for(__t - chrono::steady_clock::now());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000400}
401
Howard Hinnant186dca82010-09-23 17:31:07 +0000402inline _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000403void yield() _NOEXCEPT {__libcpp_thread_yield();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000404
405} // this_thread
406
407_LIBCPP_END_NAMESPACE_STD
408
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000409#endif // !_LIBCPP_HAS_NO_THREADS
410
Eric Fiselierf4433a32017-05-31 22:07:49 +0000411_LIBCPP_POP_MACROS
412
Howard Hinnantc51e1022010-05-11 19:42:16 +0000413#endif // _LIBCPP_THREAD