| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #include "../libnetdata.h" |
| 4 | |
| 5 | #ifdef OS_WINDOWS |
| 6 | ALWAYS_INLINE void tinysleep(void) { |
| 7 | Sleep(0); |
| 8 | // SwitchToThread(); |
| 9 | } |
| 10 | #else |
| 11 | ALWAYS_INLINE void tinysleep(void) { |
| 12 | static const struct timespec ns = { .tv_sec = 0, .tv_nsec = 1 }; |
| 13 | nanosleep(&ns, NULL); |
| 14 | } |
| 15 | #endif |
| 16 | |
| 17 | #ifdef OS_WINDOWS |
| 18 | ALWAYS_INLINE void yield_the_processor(void) { |
| 19 | Sleep(0); |
| 20 | } |
| 21 | #else |
| 22 | ALWAYS_INLINE void yield_the_processor(void) { |
| 23 | sched_yield(); |
| 24 | } |
| 25 | #endif |
| 26 | |
| 27 | #ifdef OS_WINDOWS |
| 28 | ALWAYS_INLINE void microsleep(usec_t ut) { |
| 29 | size_t ms = ut / USEC_PER_MS + ((ut == 0 || (ut % USEC_PER_MS)) ? 1 : 0); |
| 30 | Sleep(ms); |
| 31 | } |
| 32 | #else |
| 33 | ALWAYS_INLINE void microsleep(usec_t ut) { |
| 34 | time_t secs = (time_t)(ut / USEC_PER_SEC); |
| 35 | nsec_t nsec = (ut % USEC_PER_SEC) * NSEC_PER_USEC + ((ut == 0) ? 1 : 0); |
| 36 | |
| 37 | struct timespec remaining = { |
| 38 | .tv_sec = secs, |
| 39 | .tv_nsec = nsec, |
| 40 | }; |
| 41 | |
| 42 | errno_clear(); |
| 43 | while (nanosleep(&remaining, &remaining) == -1 && errno == EINTR && (remaining.tv_sec || remaining.tv_nsec)) { |
| 44 | // Loop continues if interrupted by a signal |
| 45 | } |
| 46 | } |
| 47 | #endif |