Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:14:04

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
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     // The standard guarantees a 64bit signed integer resolution for nanoseconds,
0041     // so use INT64_MAX / 1e9 as cut-off point. Use a constant to avoid <climits>
0042     // and issues with long double folding on PowerPC with GCC.
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 // _LIBCPP_HAS_THREADS
0072 
0073 } // namespace this_thread
0074 
0075 _LIBCPP_END_NAMESPACE_STD
0076 
0077 _LIBCPP_POP_MACROS
0078 
0079 #endif // _LIBCPP___THREAD_THIS_THREAD_H