| 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 "khatus_lib_log.h" |
| 9 | #include "khatus_lib_time.h" |
| 10 | |
| 11 | struct timespec |
| 12 | 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 | snooze(struct timespec *t) |
| 27 | { |
| 28 | struct timespec remainder; |
| 29 | int result; |
| 30 | |
| 31 | result = nanosleep(t, &remainder); |
| 32 | |
| 33 | if (result < 0) { |
| 34 | if (errno == EINTR) { |
| 35 | warn( |
| 36 | "nanosleep interrupted. Remainder: " |
| 37 | "{ tv_sec = %ld, tv_nsec = %ld }", |
| 38 | remainder.tv_sec, remainder.tv_nsec); |
| 39 | /* No big deal if we occasionally sleep less, |
| 40 | * so not attempting to correct after an interruption. |
| 41 | */ |
| 42 | } else { |
| 43 | fatal("nanosleep: %s\n", strerror(errno)); |
| 44 | } |
| 45 | } |
| 46 | } |