Wrap tick API

This avoids to use the SDL timer API directly, and will allow to handle
generic ticks (possibly negative).
This commit is contained in:
Romain Vimont 2021-07-04 16:50:19 +02:00
parent e9096e3e34
commit 1dcd3f295d
4 changed files with 27 additions and 10 deletions

View file

@ -1,7 +1,6 @@
#include "fps_counter.h"
#include <assert.h>
#include <SDL2/SDL_timer.h>
#include "util/log.h"
@ -82,11 +81,11 @@ run_fps_counter(void *data) {
sc_cond_wait(&counter->state_cond, &counter->mutex);
}
while (!counter->interrupted && is_started(counter)) {
uint32_t now = SDL_GetTicks();
sc_tick now = sc_tick_now();
check_interval_expired(counter, now);
assert(counter->next_timestamp > now);
uint32_t remaining = 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);
@ -99,7 +98,7 @@ run_fps_counter(void *data) {
bool
fps_counter_start(struct fps_counter *counter) {
sc_mutex_lock(&counter->mutex);
counter->next_timestamp = SDL_GetTicks() + FPS_COUNTER_INTERVAL_MS;
counter->next_timestamp = sc_tick_now() + FPS_COUNTER_INTERVAL_MS;
counter->nr_rendered = 0;
counter->nr_skipped = 0;
sc_mutex_unlock(&counter->mutex);
@ -165,7 +164,7 @@ fps_counter_add_rendered_frame(struct fps_counter *counter) {
}
sc_mutex_lock(&counter->mutex);
uint32_t now = SDL_GetTicks();
sc_tick now = sc_tick_now();
check_interval_expired(counter, now);
++counter->nr_rendered;
sc_mutex_unlock(&counter->mutex);
@ -178,7 +177,7 @@ fps_counter_add_skipped_frame(struct fps_counter *counter) {
}
sc_mutex_lock(&counter->mutex);
uint32_t now = SDL_GetTicks();
sc_tick now = sc_tick_now();
check_interval_expired(counter, now);
++counter->nr_skipped;
sc_mutex_unlock(&counter->mutex);

View file

@ -24,7 +24,7 @@ struct fps_counter {
bool interrupted;
unsigned nr_rendered;
unsigned nr_skipped;
uint32_t next_timestamp;
sc_tick next_timestamp;
};
bool

View file

@ -2,6 +2,7 @@
#include <assert.h>
#include <SDL2/SDL_thread.h>
#include <SDL2/SDL_timer.h>
#include "log.h"
@ -123,8 +124,12 @@ sc_cond_wait(sc_cond *cond, sc_mutex *mutex) {
}
bool
sc_cond_timedwait(sc_cond *cond, sc_mutex *mutex, uint32_t ms) {
int r = SDL_CondWaitTimeout(cond->cond, mutex->mutex, ms);
sc_cond_timedwait(sc_cond *cond, sc_mutex *mutex, sc_tick ms) {
if (ms < 0) {
return false; // timeout
}
int r = SDL_CondWaitTimeout(cond->cond, mutex->mutex, (uint32_t) ms);
#ifndef NDEBUG
if (r < 0) {
LOGC("Could not wait on condition with timeout: %s", SDL_GetError());
@ -163,3 +168,11 @@ sc_cond_broadcast(sc_cond *cond) {
(void) r;
#endif
}
sc_tick
sc_tick_now(void) {
// SDL ticks is an unsigned 32 bits, but this is an implementation detail.
// It wraps if the program runs for more than ~49 days, but in practice we
// can assume it does not.
return (sc_tick) SDL_GetTicks();
}

View file

@ -16,6 +16,8 @@ typedef int sc_thread_fn(void *);
typedef unsigned sc_thread_id;
typedef atomic_uint sc_atomic_thread_id;
typedef int64_t sc_tick;
typedef struct sc_thread {
SDL_Thread *thread;
} sc_thread;
@ -72,7 +74,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, uint32_t ms);
sc_cond_timedwait(sc_cond *cond, sc_mutex *mutex, sc_tick ms);
void
sc_cond_signal(sc_cond *cond);
@ -80,4 +82,7 @@ sc_cond_signal(sc_cond *cond);
void
sc_cond_broadcast(sc_cond *cond);
sc_tick
sc_tick_now(void);
#endif