timer: account for negative time values

It can easily happen that mp_time_us_to_timespec() gets a time in the
past, and then the time difference will be negative. Regression
introduced in commit f47a4fc3.

Also fix an underflow check in mp_add_timeout().
This commit is contained in:
wm4 2014-05-18 21:22:09 +02:00
parent 7fec0c630b
commit 42a51310c1
1 changed files with 6 additions and 2 deletions

View File

@ -72,7 +72,7 @@ int64_t mp_add_timeout(int64_t time_us, double timeout_sec)
double t = timeout_sec * 1000 * 1000;
if (t >= (double)(INT64_MAX - time_us))
return INT64_MAX;
if (t <= (double)time_us)
if (t <= -(double)time_us)
return 1;
return time_us + (int64_t)t;
}
@ -99,7 +99,11 @@ struct timespec mp_time_us_to_timespec(int64_t time_us)
int64_t unow = mp_time_us();
int64_t diff_us = time_us - unow;
long diff_secs = diff_us / (1000L * 1000L);
unsigned long diff_nsecs = (diff_us - diff_secs * (1000L * 1000L)) * 1000UL;
long diff_nsecs = (diff_us - diff_secs * (1000L * 1000L)) * 1000L;
if (diff_nsecs < 0) {
diff_secs -= 1;
diff_nsecs += 1000000000L;
}
if (diff_nsecs + ts.tv_nsec >= 1000000000UL) {
diff_secs += 1;
diff_nsecs -= 1000000000UL;