summaryrefslogtreecommitdiffstats
path: root/src/libnetdata/os/sleep.c
blob: 131b47c44d4fd61560b46b38d297a744f6289682 (plain)
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
31
32
33
34
35
36
// SPDX-License-Identifier: GPL-3.0-or-later

#include "../libnetdata.h"

#ifdef OS_WINDOWS
void tinysleep(void) {
    Sleep(1);
}
#else
void tinysleep(void) {
    static const struct timespec ns = { .tv_sec = 0, .tv_nsec = 1 };
    nanosleep(&ns, NULL);
}
#endif

#ifdef OS_WINDOWS
void microsleep(usec_t ut) {
    size_t ms = ut / USEC_PER_MS + ((ut == 0 || (ut % USEC_PER_MS)) ? 1 : 0);
    Sleep(ms);
}
#else
void microsleep(usec_t ut) {
    time_t secs = (time_t)(ut / USEC_PER_SEC);
    nsec_t nsec = (ut % USEC_PER_SEC) * NSEC_PER_USEC + ((ut == 0) ? 1 : 0);

    struct timespec remaining = {
        .tv_sec = secs,
        .tv_nsec = nsec,
    };

    errno_clear();
    while (nanosleep(&remaining, &remaining) == -1 && errno == EINTR && (remaining.tv_sec || remaining.tv_nsec)) {
        // Loop continues if interrupted by a signal
    }
}
#endif