| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#ifndef _GLIBCXX_THIS_THREAD_SLEEP_H |
| 31 |
#define _GLIBCXX_THIS_THREAD_SLEEP_H 1 |
| 32 |
|
| 33 |
#ifdef _GLIBCXX_SYSHDR |
| 34 |
#pragma GCC system_header |
| 35 |
#endif |
| 36 |
|
| 37 |
#if __cplusplus >= 201103L |
| 38 |
#include <bits/chrono.h> |
| 39 |
|
| 40 |
#ifdef _GLIBCXX_USE_NANOSLEEP |
| 41 |
# include <cerrno> |
| 42 |
# include <time.h> |
| 43 |
#endif |
| 44 |
|
| 45 |
namespace std _GLIBCXX_VISIBILITY(default) |
| 46 |
{ |
| 47 |
_GLIBCXX_BEGIN_NAMESPACE_VERSION |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
namespace this_thread |
| 59 |
{ |
| 60 |
#ifndef _GLIBCXX_NO_SLEEP |
| 61 |
|
| 62 |
#ifndef _GLIBCXX_USE_NANOSLEEP |
| 63 |
void |
| 64 |
__sleep_for(chrono::seconds, chrono::nanoseconds); |
| 65 |
#endif |
| 66 |
|
| 67 |
|
| 68 |
template<typename _Rep, typename _Period> |
| 69 |
inline void |
| 70 |
sleep_for(const chrono::duration<_Rep, _Period>& __rtime) |
| 71 |
{ |
| 72 |
if (__rtime <= __rtime.zero()) |
| 73 |
return; |
| 74 |
auto __s = chrono::duration_cast<chrono::seconds>(__rtime); |
| 75 |
auto __ns = chrono::duration_cast<chrono::nanoseconds>(__rtime - __s); |
| 76 |
#ifdef _GLIBCXX_USE_NANOSLEEP |
| 77 |
struct ::timespec __ts = |
| 78 |
{ |
| 79 |
static_cast<std::time_t>(__s.count()), |
| 80 |
static_cast<long>(__ns.count()) |
| 81 |
}; |
| 82 |
while (::nanosleep(&__ts, &__ts) == -1 && errno == EINTR) |
| 83 |
{ } |
| 84 |
#else |
| 85 |
__sleep_for(__s, __ns); |
| 86 |
#endif |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
template<typename _Clock, typename _Duration> |
| 91 |
inline void |
| 92 |
sleep_until(const chrono::time_point<_Clock, _Duration>& __atime) |
| 93 |
{ |
| 94 |
#if __cplusplus > 201703L |
| 95 |
static_assert(chrono::is_clock_v<_Clock>); |
| 96 |
#endif |
| 97 |
auto __now = _Clock::now(); |
| 98 |
if (_Clock::is_steady) |
| 99 |
{ |
| 100 |
if (__now < __atime) |
| 101 |
sleep_for(__atime - __now); |
| 102 |
return; |
| 103 |
} |
| 104 |
while (__now < __atime) |
| 105 |
{ |
| 106 |
sleep_for(__atime - __now); |
| 107 |
__now = _Clock::now(); |
| 108 |
} |
| 109 |
} |
| 110 |
#endif |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
_GLIBCXX_END_NAMESPACE_VERSION |
| 116 |
} |
| 117 |
#endif |
| 118 |
|
| 119 |
#endif |