blob: 09a1f02a04c4b86f097d85e6323999aabb9cde0e [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>
Arthur O'Dwyeref181602021-05-19 11:57:04 -040086#include <__debug>
Howard Hinnantc51e1022010-05-11 19:42:16 +000087#include <__functional_base>
Arthur O'Dwyeref181602021-05-19 11:57:04 -040088#include <__mutex_base>
89#include <__threading_support>
90#include <chrono>
Howard Hinnantc51e1022010-05-11 19:42:16 +000091#include <cstddef>
92#include <functional>
Arthur O'Dwyeref181602021-05-19 11:57:04 -040093#include <iosfwd>
Howard Hinnantc51e1022010-05-11 19:42:16 +000094#include <memory>
95#include <system_error>
Howard Hinnant4eea16b2011-05-16 18:40:35 +000096#include <tuple>
Arthur O'Dwyeref181602021-05-19 11:57:04 -040097#include <type_traits>
Howard Hinnantc51e1022010-05-11 19:42:16 +000098
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000099#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000100#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000101#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000102
Eric Fiselierf4433a32017-05-31 22:07:49 +0000103_LIBCPP_PUSH_MACROS
104#include <__undef_macros>
105
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000106#ifdef _LIBCPP_HAS_NO_THREADS
107#error <thread> is not supported on this single threaded system
108#else // !_LIBCPP_HAS_NO_THREADS
109
Howard Hinnantc51e1022010-05-11 19:42:16 +0000110_LIBCPP_BEGIN_NAMESPACE_STD
111
Eric Fiselier80b0e152015-08-18 19:40:38 +0000112template <class _Tp> class __thread_specific_ptr;
113class _LIBCPP_TYPE_VIS __thread_struct;
114class _LIBCPP_HIDDEN __thread_struct_imp;
115class __assoc_sub_state;
116
117_LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data();
118
119class _LIBCPP_TYPE_VIS __thread_struct
120{
121 __thread_struct_imp* __p_;
122
123 __thread_struct(const __thread_struct&);
124 __thread_struct& operator=(const __thread_struct&);
125public:
126 __thread_struct();
127 ~__thread_struct();
128
129 void notify_all_at_thread_exit(condition_variable*, mutex*);
130 void __make_ready_at_thread_exit(__assoc_sub_state*);
131};
132
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000133template <class _Tp>
134class __thread_specific_ptr
135{
Asiri Rathnayake94340d22016-09-11 21:46:40 +0000136 __libcpp_tls_key __key_;
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000137
Eric Fiselier80b0e152015-08-18 19:40:38 +0000138 // Only __thread_local_data() may construct a __thread_specific_ptr
139 // and only with _Tp == __thread_struct.
Eric Fiselierf34995b2015-08-19 03:38:41 +0000140 static_assert((is_same<_Tp, __thread_struct>::value), "");
Eric Fiselier80b0e152015-08-18 19:40:38 +0000141 __thread_specific_ptr();
142 friend _LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data();
143
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000144 __thread_specific_ptr(const __thread_specific_ptr&);
145 __thread_specific_ptr& operator=(const __thread_specific_ptr&);
146
Louis Dionne5254b372018-10-25 12:13:43 +0000147 _LIBCPP_HIDDEN static void _LIBCPP_TLS_DESTRUCTOR_CC __at_thread_exit(void*);
Saleem Abdulrasoolb75a5742017-01-07 03:07:45 +0000148
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000149public:
150 typedef _Tp* pointer;
151
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000152 ~__thread_specific_ptr();
153
Howard Hinnant186dca82010-09-23 17:31:07 +0000154 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayake94340d22016-09-11 21:46:40 +0000155 pointer get() const {return static_cast<_Tp*>(__libcpp_tls_get(__key_));}
Howard Hinnant186dca82010-09-23 17:31:07 +0000156 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000157 pointer operator*() const {return *get();}
Howard Hinnant186dca82010-09-23 17:31:07 +0000158 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000159 pointer operator->() const {return get();}
Eric Fiselier99245c52016-09-03 08:07:40 +0000160 void set_pointer(pointer __p);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000161};
162
163template <class _Tp>
Saleem Abdulrasoolb75a5742017-01-07 03:07:45 +0000164void _LIBCPP_TLS_DESTRUCTOR_CC
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000165__thread_specific_ptr<_Tp>::__at_thread_exit(void* __p)
166{
Howard Hinnant0eef25b2011-10-17 20:08:59 +0000167 delete static_cast<pointer>(__p);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000168}
169
170template <class _Tp>
171__thread_specific_ptr<_Tp>::__thread_specific_ptr()
172{
Saleem Abdulrasoolb75a5742017-01-07 03:07:45 +0000173 int __ec =
174 __libcpp_tls_create(&__key_, &__thread_specific_ptr::__at_thread_exit);
175 if (__ec)
176 __throw_system_error(__ec, "__thread_specific_ptr construction failed");
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000177}
178
179template <class _Tp>
180__thread_specific_ptr<_Tp>::~__thread_specific_ptr()
181{
Eric Fiselier80b0e152015-08-18 19:40:38 +0000182 // __thread_specific_ptr is only created with a static storage duration
183 // so this destructor is only invoked during program termination. Invoking
184 // pthread_key_delete(__key_) may prevent other threads from deleting their
185 // thread local data. For this reason we leak the key.
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000186}
187
188template <class _Tp>
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000189void
Eric Fiselier99245c52016-09-03 08:07:40 +0000190__thread_specific_ptr<_Tp>::set_pointer(pointer __p)
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000191{
Eric Fiselier99245c52016-09-03 08:07:40 +0000192 _LIBCPP_ASSERT(get() == nullptr,
193 "Attempting to overwrite thread local data");
Asiri Rathnayake94340d22016-09-11 21:46:40 +0000194 __libcpp_tls_set(__key_, __p);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000195}
196
Howard Hinnantc51e1022010-05-11 19:42:16 +0000197template<>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000198struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000199 : public unary_function<__thread_id, size_t>
200{
Howard Hinnant186dca82010-09-23 17:31:07 +0000201 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +0000202 size_t operator()(__thread_id __v) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000203 {
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000204 return hash<__libcpp_thread_id>()(__v.__id_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000205 }
206};
207
Marshall Clow58655362019-08-14 16:21:27 +0000208template<class _CharT, class _Traits>
209_LIBCPP_INLINE_VISIBILITY
210basic_ostream<_CharT, _Traits>&
211operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id)
212{return __os << __id.__id_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000213
Howard Hinnant8331b762013-03-06 23:30:19 +0000214class _LIBCPP_TYPE_VIS thread
Howard Hinnantc51e1022010-05-11 19:42:16 +0000215{
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000216 __libcpp_thread_t __t_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000217
Howard Hinnantaa87c0f2010-08-10 20:48:29 +0000218 thread(const thread&);
219 thread& operator=(const thread&);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000220public:
221 typedef __thread_id id;
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000222 typedef __libcpp_thread_t native_handle_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000223
Howard Hinnant186dca82010-09-23 17:31:07 +0000224 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayake3bab36f2017-01-16 12:19:54 +0000225 thread() _NOEXCEPT : __t_(_LIBCPP_NULL_THREAD) {}
Eric Fiselierb6159752017-04-18 23:05:08 +0000226#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc834c512011-11-29 18:15:50 +0000227 template <class _Fp, class ..._Args,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000228 class = typename enable_if
229 <
Marshall Clowaaf031b2018-03-20 22:37:37 +0000230 !is_same<typename __uncvref<_Fp>::type, thread>::value
Howard Hinnantc51e1022010-05-11 19:42:16 +0000231 >::type
232 >
Shoaib Meenai55f3a462017-03-02 03:22:18 +0000233 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Howard Hinnantc834c512011-11-29 18:15:50 +0000234 explicit thread(_Fp&& __f, _Args&&... __args);
Eric Fiselierb6159752017-04-18 23:05:08 +0000235#else // _LIBCPP_CXX03_LANG
Shoaib Meenai55f3a462017-03-02 03:22:18 +0000236 template <class _Fp>
237 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
238 explicit thread(_Fp __f);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000239#endif
240 ~thread();
241
Howard Hinnant186dca82010-09-23 17:31:07 +0000242 _LIBCPP_INLINE_VISIBILITY
Louis Dionne0b1dda02020-06-03 11:51:59 -0400243 thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) {
244 __t.__t_ = _LIBCPP_NULL_THREAD;
245 }
246
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000247 _LIBCPP_INLINE_VISIBILITY
Louis Dionne0b1dda02020-06-03 11:51:59 -0400248 thread& operator=(thread&& __t) _NOEXCEPT {
249 if (!__libcpp_thread_isnull(&__t_))
250 terminate();
251 __t_ = __t.__t_;
252 __t.__t_ = _LIBCPP_NULL_THREAD;
253 return *this;
254 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000255
Howard Hinnant186dca82010-09-23 17:31:07 +0000256 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000257 void swap(thread& __t) _NOEXCEPT {_VSTD::swap(__t_, __t.__t_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000258
Howard Hinnant186dca82010-09-23 17:31:07 +0000259 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayake3bab36f2017-01-16 12:19:54 +0000260 bool joinable() const _NOEXCEPT {return !__libcpp_thread_isnull(&__t_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000261 void join();
262 void detach();
Howard Hinnant186dca82010-09-23 17:31:07 +0000263 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000264 id get_id() const _NOEXCEPT {return __libcpp_thread_get_id(&__t_);}
Howard Hinnant186dca82010-09-23 17:31:07 +0000265 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000266 native_handle_type native_handle() _NOEXCEPT {return __t_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000267
Howard Hinnant4bd34702012-07-21 16:50:47 +0000268 static unsigned hardware_concurrency() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000269};
270
Eric Fiselierb6159752017-04-18 23:05:08 +0000271#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000272
Eric Fiselierfd996062016-04-20 02:21:33 +0000273template <class _TSp, class _Fp, class ..._Args, size_t ..._Indices>
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000274inline _LIBCPP_INLINE_VISIBILITY
275void
Eric Fiselierfd996062016-04-20 02:21:33 +0000276__thread_execute(tuple<_TSp, _Fp, _Args...>& __t, __tuple_indices<_Indices...>)
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000277{
Arthur O'Dwyerbaac1392020-11-27 14:44:53 -0500278 _VSTD::__invoke(_VSTD::move(_VSTD::get<1>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000279}
280
Howard Hinnantc834c512011-11-29 18:15:50 +0000281template <class _Fp>
Louis Dionne3b967d02019-12-10 18:00:42 -0500282_LIBCPP_INLINE_VISIBILITY
Eric Fiselierfd996062016-04-20 02:21:33 +0000283void* __thread_proxy(void* __vp)
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000284{
Arthur O'Dwyerbaac1392020-11-27 14:44:53 -0500285 // _Fp = tuple< unique_ptr<__thread_struct>, Functor, Args...>
286 unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
287 __thread_local_data().set_pointer(_VSTD::get<0>(*__p.get()).release());
Eric Fiselierfd996062016-04-20 02:21:33 +0000288 typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 2>::type _Index;
Arthur O'Dwyerbaac1392020-11-27 14:44:53 -0500289 _VSTD::__thread_execute(*__p.get(), _Index());
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000290 return nullptr;
291}
292
Howard Hinnantc834c512011-11-29 18:15:50 +0000293template <class _Fp, class ..._Args,
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000294 class
295 >
Howard Hinnantc834c512011-11-29 18:15:50 +0000296thread::thread(_Fp&& __f, _Args&&... __args)
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000297{
Eric Fiselierfd996062016-04-20 02:21:33 +0000298 typedef unique_ptr<__thread_struct> _TSPtr;
299 _TSPtr __tsp(new __thread_struct);
300 typedef tuple<_TSPtr, typename decay<_Fp>::type, typename decay<_Args>::type...> _Gp;
Arthur O'Dwyerbaac1392020-11-27 14:44:53 -0500301 unique_ptr<_Gp> __p(
302 new _Gp(_VSTD::move(__tsp),
303 _VSTD::__decay_copy(_VSTD::forward<_Fp>(__f)),
304 _VSTD::__decay_copy(_VSTD::forward<_Args>(__args))...));
305 int __ec = _VSTD::__libcpp_thread_create(&__t_, &__thread_proxy<_Gp>, __p.get());
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000306 if (__ec == 0)
307 __p.release();
308 else
309 __throw_system_error(__ec, "thread constructor failed");
310}
311
Eric Fiselierb6159752017-04-18 23:05:08 +0000312#else // _LIBCPP_CXX03_LANG
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000313
Howard Hinnantc834c512011-11-29 18:15:50 +0000314template <class _Fp>
Eric Fiselierfd996062016-04-20 02:21:33 +0000315struct __thread_invoke_pair {
316 // This type is used to pass memory for thread local storage and a functor
317 // to a newly created thread because std::pair doesn't work with
318 // std::unique_ptr in C++03.
319 __thread_invoke_pair(_Fp& __f) : __tsp_(new __thread_struct), __fn_(__f) {}
320 unique_ptr<__thread_struct> __tsp_;
321 _Fp __fn_;
322};
323
324template <class _Fp>
325void* __thread_proxy_cxx03(void* __vp)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000326{
Arthur O'Dwyerbaac1392020-11-27 14:44:53 -0500327 unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
Eric Fiselier99245c52016-09-03 08:07:40 +0000328 __thread_local_data().set_pointer(__p->__tsp_.release());
Eric Fiselierfd996062016-04-20 02:21:33 +0000329 (__p->__fn_)();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000330 return nullptr;
331}
332
Howard Hinnantc834c512011-11-29 18:15:50 +0000333template <class _Fp>
334thread::thread(_Fp __f)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000335{
Eric Fiselierfd996062016-04-20 02:21:33 +0000336
337 typedef __thread_invoke_pair<_Fp> _InvokePair;
Arthur O'Dwyerbaac1392020-11-27 14:44:53 -0500338 typedef unique_ptr<_InvokePair> _PairPtr;
Eric Fiselierfd996062016-04-20 02:21:33 +0000339 _PairPtr __pp(new _InvokePair(__f));
Arthur O'Dwyerbaac1392020-11-27 14:44:53 -0500340 int __ec = _VSTD::__libcpp_thread_create(&__t_, &__thread_proxy_cxx03<_InvokePair>, __pp.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000341 if (__ec == 0)
Eric Fiselierfd996062016-04-20 02:21:33 +0000342 __pp.release();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000343 else
344 __throw_system_error(__ec, "thread constructor failed");
345}
346
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400347#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000348
Howard Hinnant186dca82010-09-23 17:31:07 +0000349inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000350void swap(thread& __x, thread& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000351
Howard Hinnantc51e1022010-05-11 19:42:16 +0000352namespace this_thread
353{
354
Joerg Sonnenberger917a30a2017-02-09 14:12:29 +0000355_LIBCPP_FUNC_VIS void sleep_for(const chrono::nanoseconds& __ns);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000356
357template <class _Rep, class _Period>
358void
359sleep_for(const chrono::duration<_Rep, _Period>& __d)
360{
Arthur O'Dwyerbaac1392020-11-27 14:44:53 -0500361 if (__d > chrono::duration<_Rep, _Period>::zero())
Howard Hinnant9ce8dc52012-08-30 19:14:33 +0000362 {
Joerg Sonnenberger68796c72021-02-18 15:15:53 +0100363 // The standard guarantees a 64bit signed integer resolution for nanoseconds,
364 // so use INT64_MAX / 1e9 as cut-off point. Use a constant to avoid <climits>
365 // and issues with long double folding on PowerPC with GCC.
366 _LIBCPP_CONSTEXPR chrono::duration<long double> _Max =
367 chrono::duration<long double>(9223372036.0L);
Arthur O'Dwyerbaac1392020-11-27 14:44:53 -0500368 chrono::nanoseconds __ns;
Howard Hinnant9ce8dc52012-08-30 19:14:33 +0000369 if (__d < _Max)
370 {
Arthur O'Dwyerbaac1392020-11-27 14:44:53 -0500371 __ns = chrono::duration_cast<chrono::nanoseconds>(__d);
Howard Hinnant9ce8dc52012-08-30 19:14:33 +0000372 if (__ns < __d)
373 ++__ns;
374 }
375 else
Arthur O'Dwyerbaac1392020-11-27 14:44:53 -0500376 __ns = chrono::nanoseconds::max();
377 this_thread::sleep_for(__ns);
Howard Hinnant9ce8dc52012-08-30 19:14:33 +0000378 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000379}
380
381template <class _Clock, class _Duration>
382void
383sleep_until(const chrono::time_point<_Clock, _Duration>& __t)
384{
Howard Hinnantc51e1022010-05-11 19:42:16 +0000385 mutex __mut;
386 condition_variable __cv;
387 unique_lock<mutex> __lk(__mut);
388 while (_Clock::now() < __t)
389 __cv.wait_until(__lk, __t);
390}
391
392template <class _Duration>
Howard Hinnant186dca82010-09-23 17:31:07 +0000393inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000394void
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000395sleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000396{
Arthur O'Dwyerbaac1392020-11-27 14:44:53 -0500397 this_thread::sleep_for(__t - chrono::steady_clock::now());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000398}
399
Howard Hinnant186dca82010-09-23 17:31:07 +0000400inline _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000401void yield() _NOEXCEPT {__libcpp_thread_yield();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000402
403} // this_thread
404
405_LIBCPP_END_NAMESPACE_STD
406
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000407#endif // !_LIBCPP_HAS_NO_THREADS
408
Eric Fiselierf4433a32017-05-31 22:07:49 +0000409_LIBCPP_POP_MACROS
410
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400411#endif // _LIBCPP_THREAD