Deal with nanosecond overflow in both directions on both addition and subtration of timespecs
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 37 38 39 40 41 42 43 44
diff --git a/util.c b/util.c
index 086899a..2cf513a 100644
--- a/util.c
+++ b/util.c
@@ -1112,14 +1112,23 @@ void ms_to_timeval(struct timeval *val, int64_t ms)
val->tv_usec = tvdiv.rem * 1000;
}
+static void spec_nscheck(struct timespec *ts)
+{
+ while (ts->tv_nsec >= 1000000000) {
+ ts->tv_nsec -= 1000000000;
+ ts->tv_sec++;
+ }
+ while (ts->tv_nsec < 0) {
+ ts->tv_nsec += 1000000000;
+ ts->tv_sec--;
+ }
+}
+
void timeraddspec(struct timespec *a, const struct timespec *b)
{
a->tv_sec += b->tv_sec;
a->tv_nsec += b->tv_nsec;
- if (a->tv_nsec >= 1000000000) {
- a->tv_nsec -= 1000000000;
- a->tv_sec++;
- }
+ spec_nscheck(a);
}
static int __maybe_unused timespec_to_ms(struct timespec *ts)
@@ -1132,10 +1141,7 @@ static void __maybe_unused timersubspec(struct timespec *a, const struct timespe
{
a->tv_sec -= b->tv_sec;
a->tv_nsec -= b->tv_nsec;
- if (a->tv_nsec < 0) {
- a->tv_nsec += 1000000000;
- a->tv_sec--;
- }
+ spec_nscheck(a);
}
/* These are cgminer specific sleep functions that use an absolute nanosecond