File indexing completed on 2026-05-03 08:13:43
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef _LIBCPP___CXX03___THREAD_THIS_THREAD_H
0011 #define _LIBCPP___CXX03___THREAD_THIS_THREAD_H
0012
0013 #include <__cxx03/__chrono/steady_clock.h>
0014 #include <__cxx03/__chrono/time_point.h>
0015 #include <__cxx03/__condition_variable/condition_variable.h>
0016 #include <__cxx03/__config>
0017 #include <__cxx03/__mutex/mutex.h>
0018 #include <__cxx03/__mutex/unique_lock.h>
0019 #include <__cxx03/__thread/support.h>
0020
0021 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0022 # pragma GCC system_header
0023 #endif
0024
0025 _LIBCPP_PUSH_MACROS
0026 #include <__cxx03/__undef_macros>
0027
0028 _LIBCPP_BEGIN_NAMESPACE_STD
0029
0030 namespace this_thread {
0031
0032 _LIBCPP_EXPORTED_FROM_ABI void sleep_for(const chrono::nanoseconds& __ns);
0033
0034 template <class _Rep, class _Period>
0035 _LIBCPP_HIDE_FROM_ABI void sleep_for(const chrono::duration<_Rep, _Period>& __d) {
0036 if (__d > chrono::duration<_Rep, _Period>::zero()) {
0037
0038
0039
0040 _LIBCPP_CONSTEXPR chrono::duration<long double> __max = chrono::duration<long double>(9223372036.0L);
0041 chrono::nanoseconds __ns;
0042 if (__d < __max) {
0043 __ns = chrono::duration_cast<chrono::nanoseconds>(__d);
0044 if (__ns < __d)
0045 ++__ns;
0046 } else
0047 __ns = chrono::nanoseconds::max();
0048 this_thread::sleep_for(__ns);
0049 }
0050 }
0051
0052 template <class _Clock, class _Duration>
0053 _LIBCPP_HIDE_FROM_ABI void sleep_until(const chrono::time_point<_Clock, _Duration>& __t) {
0054 mutex __mut;
0055 condition_variable __cv;
0056 unique_lock<mutex> __lk(__mut);
0057 while (_Clock::now() < __t)
0058 __cv.wait_until(__lk, __t);
0059 }
0060
0061 template <class _Duration>
0062 inline _LIBCPP_HIDE_FROM_ABI void sleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t) {
0063 this_thread::sleep_for(__t - chrono::steady_clock::now());
0064 }
0065
0066 inline _LIBCPP_HIDE_FROM_ABI void yield() _NOEXCEPT { __libcpp_thread_yield(); }
0067
0068 }
0069
0070 _LIBCPP_END_NAMESPACE_STD
0071
0072 _LIBCPP_POP_MACROS
0073
0074 #endif