From 9282ed19b2810430bb0766b5e6263842ad49d405 Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Mon, 6 Jan 2020 20:36:45 +1100 Subject: [PATCH] [client] check for clock drift and correct for it Fixes #224 --- VERSION | 2 +- client/src/main.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/VERSION b/VERSION index d3612f22..aa8ea2a0 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -B1-52-g0dc0e6490c+1 \ No newline at end of file +B1-54-g5033d17990+1 \ No newline at end of file diff --git a/client/src/main.c b/client/src/main.c index 934445c4..af3c4867 100644 --- a/client/src/main.c +++ b/client/src/main.c @@ -137,11 +137,39 @@ static int renderThread(void * unused) /* signal to other threads that the renderer is ready */ lgSignalEvent(e_startup); + unsigned int resyncCheck = 0; struct timespec time; clock_gettime(CLOCK_MONOTONIC, &time); while(state.running) { + // if our clock is too far out of sync, resync it + // this can happen when switching to/from a TTY, or due to clock drift + // we only check this once every 100 frames + if (++resyncCheck == 100) + { + resyncCheck = 0; + + struct timespec tmp; + clock_gettime(CLOCK_MONOTONIC, &tmp); + if (tmp.tv_nsec - time.tv_nsec < 0) + { + tmp.tv_sec -= time.tv_sec - 1; + tmp.tv_nsec = 1000000000 + tmp.tv_nsec - time.tv_nsec; + } + else + { + tmp.tv_sec -= time.tv_sec; + tmp.tv_nsec -= time.tv_nsec; + } + const unsigned long diff = tmp.tv_sec * 1000000000 + tmp.tv_nsec; + if (diff > state.frameTime) + { + DEBUG_INFO("Timer drift detected, %lu is > %lu", diff, state.frameTime); + clock_gettime(CLOCK_MONOTONIC, &time); + } + } + if (state.lgrResize) { if (state.lgr)