Commit | Line | Data |
---|---|---|
17a27e48 SK |
1 | #include <errno.h> |
2 | #include <math.h> | |
3 | #include <stdio.h> | |
c7262f37 | 4 | #include <stdlib.h> |
17a27e48 SK |
5 | #include <string.h> |
6 | #include <time.h> | |
7 | ||
1084633a SK |
8 | #include "khlib_log.h" |
9 | #include "khlib_time.h" | |
17a27e48 | 10 | |
8345f2b3 | 11 | struct timespec |
1084633a | 12 | khlib_timespec_of_float(double n) |
8345f2b3 SK |
13 | { |
14 | double integral; | |
15 | double fractional; | |
16 | struct timespec t; | |
17 | ||
18 | fractional = modf(n, &integral); | |
19 | t.tv_sec = (int) integral; | |
20 | t.tv_nsec = (int) (1E9 * fractional); | |
21 | ||
22 | return t; | |
23 | } | |
24 | ||
17a27e48 | 25 | void |
1084633a | 26 | khlib_sleep(struct timespec *t) |
17a27e48 SK |
27 | { |
28 | struct timespec remainder; | |
17a27e48 | 29 | |
1084633a | 30 | if (nanosleep(t, &remainder) < 0) { |
17a27e48 | 31 | if (errno == EINTR) { |
1084633a | 32 | khlib_warn( |
17a27e48 SK |
33 | "nanosleep interrupted. Remainder: " |
34 | "{ tv_sec = %ld, tv_nsec = %ld }", | |
35 | remainder.tv_sec, remainder.tv_nsec); | |
36 | /* No big deal if we occasionally sleep less, | |
37 | * so not attempting to correct after an interruption. | |
38 | */ | |
39 | } else { | |
1084633a | 40 | khlib_fatal("nanosleep: %s\n", strerror(errno)); |
17a27e48 SK |
41 | } |
42 | } | |
43 | } |