1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // 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
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef _LIBCPP___THREAD_THIS_THREAD_H
11 #define _LIBCPP___THREAD_THIS_THREAD_H
12
13 #include <__chrono/steady_clock.h>
14 #include <__chrono/time_point.h>
15 #include <__condition_variable/condition_variable.h>
16 #include <__config>
17 #include <__mutex/mutex.h>
18 #include <__mutex/unique_lock.h>
19 #include <__threading_support>
20
21 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
22 # pragma GCC system_header
23 #endif
24
25 _LIBCPP_PUSH_MACROS
26 #include <__undef_macros>
27
28 _LIBCPP_BEGIN_NAMESPACE_STD
29
30 namespace this_thread
31 {
32
33 _LIBCPP_EXPORTED_FROM_ABI void sleep_for(const chrono::nanoseconds& __ns);
34
35 template <class _Rep, class _Period>
36 _LIBCPP_HIDE_FROM_ABI void
sleep_for(const chrono::duration<_Rep,_Period> & __d)37 sleep_for(const chrono::duration<_Rep, _Period>& __d)
38 {
39 if (__d > chrono::duration<_Rep, _Period>::zero())
40 {
41 // The standard guarantees a 64bit signed integer resolution for nanoseconds,
42 // so use INT64_MAX / 1e9 as cut-off point. Use a constant to avoid <climits>
43 // and issues with long double folding on PowerPC with GCC.
44 _LIBCPP_CONSTEXPR chrono::duration<long double> __max =
45 chrono::duration<long double>(9223372036.0L);
46 chrono::nanoseconds __ns;
47 if (__d < __max)
48 {
49 __ns = chrono::duration_cast<chrono::nanoseconds>(__d);
50 if (__ns < __d)
51 ++__ns;
52 }
53 else
54 __ns = chrono::nanoseconds::max();
55 this_thread::sleep_for(__ns);
56 }
57 }
58
59 template <class _Clock, class _Duration>
60 _LIBCPP_HIDE_FROM_ABI void
sleep_until(const chrono::time_point<_Clock,_Duration> & __t)61 sleep_until(const chrono::time_point<_Clock, _Duration>& __t)
62 {
63 mutex __mut;
64 condition_variable __cv;
65 unique_lock<mutex> __lk(__mut);
66 while (_Clock::now() < __t)
67 __cv.wait_until(__lk, __t);
68 }
69
70 template <class _Duration>
71 inline _LIBCPP_INLINE_VISIBILITY
72 void
sleep_until(const chrono::time_point<chrono::steady_clock,_Duration> & __t)73 sleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t)
74 {
75 this_thread::sleep_for(__t - chrono::steady_clock::now());
76 }
77
78 inline _LIBCPP_INLINE_VISIBILITY
yield()79 void yield() _NOEXCEPT {__libcpp_thread_yield();}
80
81 } // namespace this_thread
82
83 _LIBCPP_END_NAMESPACE_STD
84
85 _LIBCPP_POP_MACROS
86
87 #endif // _LIBCPP___THREAD_THIS_THREAD_H
88