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