| 1 | #include <errno.h> |
| 2 | #include <math.h> |
| 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <string.h> |
| 6 | #include <time.h> |
| 7 | |
| 8 | #include "khlib_log.h" |
| 9 | #include "khlib_time.h" |
| 10 | |
| 11 | struct timespec |
| 12 | khlib_timespec_of_float(double n) |
| 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 | |
| 25 | void |
| 26 | khlib_sleep(struct timespec *t) |
| 27 | { |
| 28 | struct timespec remainder; |
| 29 | |
| 30 | if (nanosleep(t, &remainder) < 0) { |
| 31 | if (errno == EINTR) { |
| 32 | khlib_warn( |
| 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 { |
| 40 | khlib_fatal("nanosleep: %s\n", strerror(errno)); |
| 41 | } |
| 42 | } |
| 43 | } |