Skip to content

Commit

Permalink
timer-linux: fallback to CLOCK_MONOTONIC instead of timespec_get
Browse files Browse the repository at this point in the history
CLOCK_MONOTONIC_RAW is linux-specific (macOS later supported it but it
has its own timer code) and not neccessarily available everywhere like
on BSDs. It makes sense to prefer it because mpv does a lot of
measurements at small intervals (e.g. every frame) so theoretically it
should be more accurate. However if the OS doesn't have it, fallback to
CLOCK_MONOTONIC instead which is almost exactly the same and very widely
supported across unix-like systems. This clock is technically optional
according to POSIX, but any half-decent OS supports it anyway (sorry
Solaris users). As a benefit, we now know that the clock from mp_time is
always monotonic.
  • Loading branch information
Dudemanguy committed Oct 27, 2023
1 parent bc3e850 commit 891efca
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions osdep/timer-linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ void mp_sleep_ns(int64_t ns)
uint64_t mp_raw_time_ns(void)
{
struct timespec tp = {0};
int ret = -1;
#if defined(CLOCK_MONOTONIC_RAW)
clock_gettime(CLOCK_MONOTONIC_RAW, &tp);
#else
timespec_get(&tp, TIME_UTC);
ret = clock_gettime(CLOCK_MONOTONIC_RAW, &tp);
#endif
if (ret)
clock_gettime(CLOCK_MONOTONIC, &tp);
return MP_TIME_S_TO_NS(tp.tv_sec) + tp.tv_nsec;
}

Expand Down

0 comments on commit 891efca

Please sign in to comment.