]> Repos - portaudio/commit
Have PaUtil_GetTime() use monotonic time on Unix (#559)
authorTrent Piepho <35062987+xyzzy42@users.noreply.github.com>
Wed, 9 Jun 2021 00:52:32 +0000 (17:52 -0700)
committerGitHub <noreply@github.com>
Wed, 9 Jun 2021 00:52:32 +0000 (17:52 -0700)
commitd859f7d5df98253e9eb16acf201e3551dd01134f
treea02d9d48027d0e9539d398d5778971628ea667e1
parent52a78c72f3be6d2d538dc2b662ccadff7d1027de
Have PaUtil_GetTime() use monotonic time on Unix (#559)

The currently used clock, CLOCK_REALTIME, will have discontinuous jumps, forward
and backward, when the system clock is adjusted.  This is bad for synchronizing
audio.  CLOCK_MONOTONIC does not have jumps, and never moves backward, but does
have the continuous adjustments done by NTP to increase clock accuracy.

Since kernel 2.6.25 ALSA has use CLOCK_MONOTONIC by default for timestamps
(commit b751eef1 from Dec 13 2007).  This change means the timestamps provided
in the PA callbacks can be compared to PaUtil_GetTime(), which currently does
not work.

The function used on MacOS, mach_absolute_time(), behaves like CLOCK_MONOTONIC.

If the system does not have CLOCK_MONOTONIC, fallback to CLOCK_REALTIME, which
is what alsa-lib does for such systems.  Which at this point would need to be
very old.

* Use clock monotonic for condition variable

This is necessary to match PaUtil_GetTime() also using the monotonic
clock.  Otherwise the wait time, derived from that function, will be
completely incorrect.

This will also fix an obscure flaw if the system clock is adjust while
waiting for the stream to open, it might not wait at all and fail or
wait (effectively) forever instead of timing out, depend on the
adjustment direction.
src/os/unix/pa_unix_util.c