diff --git a/app/src/fps_counter.c b/app/src/fps_counter.c index 4fad6550..c92d4140 100644 --- a/app/src/fps_counter.c +++ b/app/src/fps_counter.c @@ -84,11 +84,9 @@ run_fps_counter(void *data) { sc_tick now = sc_tick_now(); check_interval_expired(counter, now); - assert(counter->next_timestamp > now); - sc_tick remaining = counter->next_timestamp - now; - // ignore the reason (timeout or signaled), we just loop anyway - sc_cond_timedwait(&counter->state_cond, &counter->mutex, remaining); + sc_cond_timedwait(&counter->state_cond, &counter->mutex, + counter->next_timestamp); } } sc_mutex_unlock(&counter->mutex); diff --git a/app/src/server.c b/app/src/server.c index ca609e25..4e0f2b23 100644 --- a/app/src/server.c +++ b/app/src/server.c @@ -557,7 +557,7 @@ server_stop(struct server *server) { #define WATCHDOG_DELAY SC_TICK_FROM_SEC(1) signaled = sc_cond_timedwait(&server->process_terminated_cond, &server->mutex, - WATCHDOG_DELAY); + sc_tick_now() + WATCHDOG_DELAY); } sc_mutex_unlock(&server->mutex); diff --git a/app/src/util/thread.c b/app/src/util/thread.c index 07b1f6a6..2c376e97 100644 --- a/app/src/util/thread.c +++ b/app/src/util/thread.c @@ -123,12 +123,13 @@ sc_cond_wait(sc_cond *cond, sc_mutex *mutex) { } bool -sc_cond_timedwait(sc_cond *cond, sc_mutex *mutex, sc_tick delay) { - if (delay < 0) { +sc_cond_timedwait(sc_cond *cond, sc_mutex *mutex, sc_tick deadline) { + sc_tick now = sc_tick_now(); + if (deadline <= now) { return false; // timeout } - uint32_t ms = SC_TICK_TO_MS(delay); + uint32_t ms = SC_TICK_TO_MS(deadline - now); int r = SDL_CondWaitTimeout(cond->cond, mutex->mutex, ms); #ifndef NDEBUG if (r < 0) { diff --git a/app/src/util/thread.h b/app/src/util/thread.h index a59c09a1..7add6f1c 100644 --- a/app/src/util/thread.h +++ b/app/src/util/thread.h @@ -73,7 +73,7 @@ sc_cond_wait(sc_cond *cond, sc_mutex *mutex); // return true on signaled, false on timeout bool -sc_cond_timedwait(sc_cond *cond, sc_mutex *mutex, sc_tick ms); +sc_cond_timedwait(sc_cond *cond, sc_mutex *mutex, sc_tick deadline); void sc_cond_signal(sc_cond *cond);