diff options
author | wm4 <wm4@nowhere> | 2014-05-18 21:22:09 +0200 |
---|---|---|
committer | wm4 <wm4@nowhere> | 2014-05-18 21:44:45 +0200 |
commit | 42a51310c1a72805fe6c01438366354641d6d6ce (patch) | |
tree | 681c78acea1564f0f07b42d8b06526711314d560 /osdep | |
parent | 7fec0c630b80831915115c14327cccaf70d37c50 (diff) |
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().
Diffstat (limited to 'osdep')
-rw-r--r-- | osdep/timer.c | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/osdep/timer.c b/osdep/timer.c index de37653a67..8851fbd8b3 100644 --- a/osdep/timer.c +++ b/osdep/timer.c @@ -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; |