timer: fix a corner case on clock changes

It's conceivable that the OS time source is subject to clock changes.
The time could jump back to before when mpv was started, which would
cause mp_time_us() to return values smaller than 1. This is unexpected
by the code and could trigger assertions. If there's no monotonic time
source there's not much we can do anyway, so just sanitize the return
value. It will cause strange behavior until the "lost" time offset has
passed, but if you make such huge changes to the system clock while
everything is running, you're asking for trouble anyway.

(Normally we try to get a monotonic time source, though. This problem
sometimes happened on Windows when compiled without winpthreads, when
the code was falling back to gettimeofday(). This was already fixed by
always using another method.)
This commit is contained in:
wm4 2015-07-04 17:24:10 +02:00
parent f544bcf105
commit 5728295dab
1 changed files with 4 additions and 1 deletions

View File

@ -48,7 +48,10 @@ void mp_time_init(void)
int64_t mp_time_us(void)
{
return mp_raw_time_us() - raw_time_offset;
int64_t r = mp_raw_time_us() - raw_time_offset;
if (r < MP_START_TIME)
r = MP_START_TIME;
return r;
}
double mp_time_sec(void)