blob: bf751c87aa3ba71bf902477147bf241e8050ba5f [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
Louis Dionne9bd93882021-11-17 16:25:01 -05002//===----------------------------------------------------------------------===//
Howard Hinnantc51e1022010-05-11 19:42:16 +00003//
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>
Louis Dionneaf2d2a82021-11-17 10:59:31 -050089#include <__thread/poll_with_backoff.h>
Arthur O'Dwyeref181602021-05-19 11:57:04 -040090#include <__threading_support>
Louis Dionne64f0cbf2021-08-27 10:36:04 -040091#include <__utility/decay_copy.h>
Christopher Di Bella41f26e82021-06-05 02:47:47 +000092#include <__utility/forward.h>
Arthur O'Dwyeref181602021-05-19 11:57:04 -040093#include <chrono>
Howard Hinnantc51e1022010-05-11 19:42:16 +000094#include <cstddef>
95#include <functional>
Arthur O'Dwyeref181602021-05-19 11:57:04 -040096#include <iosfwd>
Howard Hinnantc51e1022010-05-11 19:42:16 +000097#include <memory>
98#include <system_error>
Howard Hinnant4eea16b2011-05-16 18:40:35 +000099#include <tuple>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400100#include <type_traits>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000101
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000102#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000103#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000104#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000105
Eric Fiselierf4433a32017-05-31 22:07:49 +0000106_LIBCPP_PUSH_MACROS
107#include <__undef_macros>
108
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000109#ifdef _LIBCPP_HAS_NO_THREADS
110#error <thread> is not supported on this single threaded system
111#else // !_LIBCPP_HAS_NO_THREADS
112
Howard Hinnantc51e1022010-05-11 19:42:16 +0000113_LIBCPP_BEGIN_NAMESPACE_STD
114
Eric Fiselier80b0e152015-08-18 19:40:38 +0000115template <class _Tp> class __thread_specific_ptr;
116class _LIBCPP_TYPE_VIS __thread_struct;
117class _LIBCPP_HIDDEN __thread_struct_imp;
118class __assoc_sub_state;
119
120_LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data();
121
122class _LIBCPP_TYPE_VIS __thread_struct
123{
124 __thread_struct_imp* __p_;
125
126 __thread_struct(const __thread_struct&);
127 __thread_struct& operator=(const __thread_struct&);
128public:
129 __thread_struct();
130 ~__thread_struct();
131
132 void notify_all_at_thread_exit(condition_variable*, mutex*);
133 void __make_ready_at_thread_exit(__assoc_sub_state*);
134};
135
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000136template <class _Tp>
137class __thread_specific_ptr
138{
Asiri Rathnayake94340d22016-09-11 21:46:40 +0000139 __libcpp_tls_key __key_;
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000140
Eric Fiselier80b0e152015-08-18 19:40:38 +0000141 // Only __thread_local_data() may construct a __thread_specific_ptr
142 // and only with _Tp == __thread_struct.
Eric Fiselierf34995b2015-08-19 03:38:41 +0000143 static_assert((is_same<_Tp, __thread_struct>::value), "");
Eric Fiselier80b0e152015-08-18 19:40:38 +0000144 __thread_specific_ptr();
145 friend _LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data();
146
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000147 __thread_specific_ptr(const __thread_specific_ptr&);
148 __thread_specific_ptr& operator=(const __thread_specific_ptr&);
149
Louis Dionne5254b372018-10-25 12:13:43 +0000150 _LIBCPP_HIDDEN static void _LIBCPP_TLS_DESTRUCTOR_CC __at_thread_exit(void*);
Saleem Abdulrasoolb75a5742017-01-07 03:07:45 +0000151
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000152public:
153 typedef _Tp* pointer;
154
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000155 ~__thread_specific_ptr();
156
Howard Hinnant186dca82010-09-23 17:31:07 +0000157 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayake94340d22016-09-11 21:46:40 +0000158 pointer get() const {return static_cast<_Tp*>(__libcpp_tls_get(__key_));}
Howard Hinnant186dca82010-09-23 17:31:07 +0000159 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000160 pointer operator*() const {return *get();}
Howard Hinnant186dca82010-09-23 17:31:07 +0000161 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000162 pointer operator->() const {return get();}
Eric Fiselier99245c52016-09-03 08:07:40 +0000163 void set_pointer(pointer __p);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000164};
165
166template <class _Tp>
Saleem Abdulrasoolb75a5742017-01-07 03:07:45 +0000167void _LIBCPP_TLS_DESTRUCTOR_CC
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000168__thread_specific_ptr<_Tp>::__at_thread_exit(void* __p)
169{
Howard Hinnant0eef25b2011-10-17 20:08:59 +0000170 delete static_cast<pointer>(__p);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000171}
172
173template <class _Tp>
174__thread_specific_ptr<_Tp>::__thread_specific_ptr()
175{
Saleem Abdulrasoolb75a5742017-01-07 03:07:45 +0000176 int __ec =
177 __libcpp_tls_create(&__key_, &__thread_specific_ptr::__at_thread_exit);
178 if (__ec)
179 __throw_system_error(__ec, "__thread_specific_ptr construction failed");
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000180}
181
182template <class _Tp>
183__thread_specific_ptr<_Tp>::~__thread_specific_ptr()
184{
Eric Fiselier80b0e152015-08-18 19:40:38 +0000185 // __thread_specific_ptr is only created with a static storage duration
186 // so this destructor is only invoked during program termination. Invoking
187 // pthread_key_delete(__key_) may prevent other threads from deleting their
188 // thread local data. For this reason we leak the key.
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000189}
190
191template <class _Tp>
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000192void
Eric Fiselier99245c52016-09-03 08:07:40 +0000193__thread_specific_ptr<_Tp>::set_pointer(pointer __p)
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000194{
Eric Fiselier99245c52016-09-03 08:07:40 +0000195 _LIBCPP_ASSERT(get() == nullptr,
196 "Attempting to overwrite thread local data");
Asiri Rathnayake94340d22016-09-11 21:46:40 +0000197 __libcpp_tls_set(__key_, __p);
Howard Hinnant3820f6b2010-08-27 20:10:19 +0000198}
199
Howard Hinnantc51e1022010-05-11 19:42:16 +0000200template<>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000201struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000202 : public unary_function<__thread_id, size_t>
203{
Howard Hinnant186dca82010-09-23 17:31:07 +0000204 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +0000205 size_t operator()(__thread_id __v) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000206 {
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000207 return hash<__libcpp_thread_id>()(__v.__id_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000208 }
209};
210
Marshall Clow58655362019-08-14 16:21:27 +0000211template<class _CharT, class _Traits>
212_LIBCPP_INLINE_VISIBILITY
213basic_ostream<_CharT, _Traits>&
214operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id)
215{return __os << __id.__id_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000216
Howard Hinnant8331b762013-03-06 23:30:19 +0000217class _LIBCPP_TYPE_VIS thread
Howard Hinnantc51e1022010-05-11 19:42:16 +0000218{
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000219 __libcpp_thread_t __t_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000220
Howard Hinnantaa87c0f2010-08-10 20:48:29 +0000221 thread(const thread&);
222 thread& operator=(const thread&);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000223public:
224 typedef __thread_id id;
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000225 typedef __libcpp_thread_t native_handle_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000226
Howard Hinnant186dca82010-09-23 17:31:07 +0000227 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayake3bab36f2017-01-16 12:19:54 +0000228 thread() _NOEXCEPT : __t_(_LIBCPP_NULL_THREAD) {}
Eric Fiselierb6159752017-04-18 23:05:08 +0000229#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc834c512011-11-29 18:15:50 +0000230 template <class _Fp, class ..._Args,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000231 class = typename enable_if
232 <
Marshall Clowaaf031b2018-03-20 22:37:37 +0000233 !is_same<typename __uncvref<_Fp>::type, thread>::value
Howard Hinnantc51e1022010-05-11 19:42:16 +0000234 >::type
235 >
Shoaib Meenai55f3a462017-03-02 03:22:18 +0000236 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Howard Hinnantc834c512011-11-29 18:15:50 +0000237 explicit thread(_Fp&& __f, _Args&&... __args);
Eric Fiselierb6159752017-04-18 23:05:08 +0000238#else // _LIBCPP_CXX03_LANG
Shoaib Meenai55f3a462017-03-02 03:22:18 +0000239 template <class _Fp>
240 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
241 explicit thread(_Fp __f);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000242#endif
243 ~thread();
244
Howard Hinnant186dca82010-09-23 17:31:07 +0000245 _LIBCPP_INLINE_VISIBILITY
Louis Dionne0b1dda02020-06-03 11:51:59 -0400246 thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) {
247 __t.__t_ = _LIBCPP_NULL_THREAD;
248 }
249
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000250 _LIBCPP_INLINE_VISIBILITY
Louis Dionne0b1dda02020-06-03 11:51:59 -0400251 thread& operator=(thread&& __t) _NOEXCEPT {
252 if (!__libcpp_thread_isnull(&__t_))
253 terminate();
254 __t_ = __t.__t_;
255 __t.__t_ = _LIBCPP_NULL_THREAD;
256 return *this;
257 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000258
Howard Hinnant186dca82010-09-23 17:31:07 +0000259 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000260 void swap(thread& __t) _NOEXCEPT {_VSTD::swap(__t_, __t.__t_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000261
Howard Hinnant186dca82010-09-23 17:31:07 +0000262 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayake3bab36f2017-01-16 12:19:54 +0000263 bool joinable() const _NOEXCEPT {return !__libcpp_thread_isnull(&__t_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000264 void join();
265 void detach();
Howard Hinnant186dca82010-09-23 17:31:07 +0000266 _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000267 id get_id() const _NOEXCEPT {return __libcpp_thread_get_id(&__t_);}
Howard Hinnant186dca82010-09-23 17:31:07 +0000268 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000269 native_handle_type native_handle() _NOEXCEPT {return __t_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000270
Howard Hinnant4bd34702012-07-21 16:50:47 +0000271 static unsigned hardware_concurrency() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000272};
273
Eric Fiselierb6159752017-04-18 23:05:08 +0000274#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000275
Eric Fiselierfd996062016-04-20 02:21:33 +0000276template <class _TSp, class _Fp, class ..._Args, size_t ..._Indices>
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000277inline _LIBCPP_INLINE_VISIBILITY
278void
Eric Fiselierfd996062016-04-20 02:21:33 +0000279__thread_execute(tuple<_TSp, _Fp, _Args...>& __t, __tuple_indices<_Indices...>)
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000280{
Arthur O'Dwyerbaac1392020-11-27 14:44:53 -0500281 _VSTD::__invoke(_VSTD::move(_VSTD::get<1>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000282}
283
Howard Hinnantc834c512011-11-29 18:15:50 +0000284template <class _Fp>
Louis Dionne3b967d02019-12-10 18:00:42 -0500285_LIBCPP_INLINE_VISIBILITY
Eric Fiselierfd996062016-04-20 02:21:33 +0000286void* __thread_proxy(void* __vp)
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000287{
Arthur O'Dwyerbaac1392020-11-27 14:44:53 -0500288 // _Fp = tuple< unique_ptr<__thread_struct>, Functor, Args...>
289 unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
290 __thread_local_data().set_pointer(_VSTD::get<0>(*__p.get()).release());
Eric Fiselierfd996062016-04-20 02:21:33 +0000291 typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 2>::type _Index;
Arthur O'Dwyerbaac1392020-11-27 14:44:53 -0500292 _VSTD::__thread_execute(*__p.get(), _Index());
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000293 return nullptr;
294}
295
Howard Hinnantc834c512011-11-29 18:15:50 +0000296template <class _Fp, class ..._Args,
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000297 class
298 >
Howard Hinnantc834c512011-11-29 18:15:50 +0000299thread::thread(_Fp&& __f, _Args&&... __args)
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000300{
Eric Fiselierfd996062016-04-20 02:21:33 +0000301 typedef unique_ptr<__thread_struct> _TSPtr;
302 _TSPtr __tsp(new __thread_struct);
303 typedef tuple<_TSPtr, typename decay<_Fp>::type, typename decay<_Args>::type...> _Gp;
Arthur O'Dwyerbaac1392020-11-27 14:44:53 -0500304 unique_ptr<_Gp> __p(
305 new _Gp(_VSTD::move(__tsp),
306 _VSTD::__decay_copy(_VSTD::forward<_Fp>(__f)),
307 _VSTD::__decay_copy(_VSTD::forward<_Args>(__args))...));
308 int __ec = _VSTD::__libcpp_thread_create(&__t_, &__thread_proxy<_Gp>, __p.get());
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000309 if (__ec == 0)
310 __p.release();
311 else
312 __throw_system_error(__ec, "thread constructor failed");
313}
314
Eric Fiselierb6159752017-04-18 23:05:08 +0000315#else // _LIBCPP_CXX03_LANG
Howard Hinnant4eea16b2011-05-16 18:40:35 +0000316
Howard Hinnantc834c512011-11-29 18:15:50 +0000317template <class _Fp>
Eric Fiselierfd996062016-04-20 02:21:33 +0000318struct __thread_invoke_pair {
319 // This type is used to pass memory for thread local storage and a functor
320 // to a newly created thread because std::pair doesn't work with
321 // std::unique_ptr in C++03.
322 __thread_invoke_pair(_Fp& __f) : __tsp_(new __thread_struct), __fn_(__f) {}
323 unique_ptr<__thread_struct> __tsp_;
324 _Fp __fn_;
325};
326
327template <class _Fp>
328void* __thread_proxy_cxx03(void* __vp)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000329{
Arthur O'Dwyerbaac1392020-11-27 14:44:53 -0500330 unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
Eric Fiselier99245c52016-09-03 08:07:40 +0000331 __thread_local_data().set_pointer(__p->__tsp_.release());
Eric Fiselierfd996062016-04-20 02:21:33 +0000332 (__p->__fn_)();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000333 return nullptr;
334}
335
Howard Hinnantc834c512011-11-29 18:15:50 +0000336template <class _Fp>
337thread::thread(_Fp __f)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000338{
Eric Fiselierfd996062016-04-20 02:21:33 +0000339
340 typedef __thread_invoke_pair<_Fp> _InvokePair;
Arthur O'Dwyerbaac1392020-11-27 14:44:53 -0500341 typedef unique_ptr<_InvokePair> _PairPtr;
Eric Fiselierfd996062016-04-20 02:21:33 +0000342 _PairPtr __pp(new _InvokePair(__f));
Arthur O'Dwyerbaac1392020-11-27 14:44:53 -0500343 int __ec = _VSTD::__libcpp_thread_create(&__t_, &__thread_proxy_cxx03<_InvokePair>, __pp.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000344 if (__ec == 0)
Eric Fiselierfd996062016-04-20 02:21:33 +0000345 __pp.release();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000346 else
347 __throw_system_error(__ec, "thread constructor failed");
348}
349
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400350#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000351
Howard Hinnant186dca82010-09-23 17:31:07 +0000352inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4bd34702012-07-21 16:50:47 +0000353void swap(thread& __x, thread& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000354
Howard Hinnantc51e1022010-05-11 19:42:16 +0000355namespace this_thread
356{
357
Joerg Sonnenberger917a30a2017-02-09 14:12:29 +0000358_LIBCPP_FUNC_VIS void sleep_for(const chrono::nanoseconds& __ns);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000359
360template <class _Rep, class _Period>
361void
362sleep_for(const chrono::duration<_Rep, _Period>& __d)
363{
Arthur O'Dwyerbaac1392020-11-27 14:44:53 -0500364 if (__d > chrono::duration<_Rep, _Period>::zero())
Howard Hinnant9ce8dc52012-08-30 19:14:33 +0000365 {
Joerg Sonnenberger68796c72021-02-18 15:15:53 +0100366 // The standard guarantees a 64bit signed integer resolution for nanoseconds,
367 // so use INT64_MAX / 1e9 as cut-off point. Use a constant to avoid <climits>
368 // and issues with long double folding on PowerPC with GCC.
369 _LIBCPP_CONSTEXPR chrono::duration<long double> _Max =
370 chrono::duration<long double>(9223372036.0L);
Arthur O'Dwyerbaac1392020-11-27 14:44:53 -0500371 chrono::nanoseconds __ns;
Howard Hinnant9ce8dc52012-08-30 19:14:33 +0000372 if (__d < _Max)
373 {
Arthur O'Dwyerbaac1392020-11-27 14:44:53 -0500374 __ns = chrono::duration_cast<chrono::nanoseconds>(__d);
Howard Hinnant9ce8dc52012-08-30 19:14:33 +0000375 if (__ns < __d)
376 ++__ns;
377 }
378 else
Arthur O'Dwyerbaac1392020-11-27 14:44:53 -0500379 __ns = chrono::nanoseconds::max();
380 this_thread::sleep_for(__ns);
Howard Hinnant9ce8dc52012-08-30 19:14:33 +0000381 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000382}
383
384template <class _Clock, class _Duration>
385void
386sleep_until(const chrono::time_point<_Clock, _Duration>& __t)
387{
Howard Hinnantc51e1022010-05-11 19:42:16 +0000388 mutex __mut;
389 condition_variable __cv;
390 unique_lock<mutex> __lk(__mut);
391 while (_Clock::now() < __t)
392 __cv.wait_until(__lk, __t);
393}
394
395template <class _Duration>
Howard Hinnant186dca82010-09-23 17:31:07 +0000396inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000397void
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000398sleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000399{
Arthur O'Dwyerbaac1392020-11-27 14:44:53 -0500400 this_thread::sleep_for(__t - chrono::steady_clock::now());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000401}
402
Howard Hinnant186dca82010-09-23 17:31:07 +0000403inline _LIBCPP_INLINE_VISIBILITY
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000404void yield() _NOEXCEPT {__libcpp_thread_yield();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000405
Nikolas Klauserd26407a2021-12-02 14:12:51 +0100406} // namespace this_thread
Howard Hinnantc51e1022010-05-11 19:42:16 +0000407
408_LIBCPP_END_NAMESPACE_STD
409
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000410#endif // !_LIBCPP_HAS_NO_THREADS
411
Eric Fiselierf4433a32017-05-31 22:07:49 +0000412_LIBCPP_POP_MACROS
413
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400414#endif // _LIBCPP_THREAD