Enforce deadline reached on timeout
The value of sc_tick_now() has microsecond precision, but
sc_cond_timedwait() has only millisecond precision.
To guarantee that sc_tick_now() >= deadline when sc_cond_timedwait()
returns due to timeout, round up to the next millisecond.
This avoids to call a non-blocking sc_cond_timedwait() in a loop for no
reason until a target deadline during up to 1 millisecond.
Refs 682a691173
This commit is contained in:
parent
2a872c3865
commit
85edba20e7
1 changed files with 5 additions and 1 deletions
|
@ -136,7 +136,9 @@ sc_cond_timedwait(sc_cond *cond, sc_mutex *mutex, sc_tick deadline) {
|
||||||
return false; // timeout
|
return false; // timeout
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t ms = SC_TICK_TO_MS(deadline - now);
|
// Round up to the next millisecond to guarantee that the deadline is
|
||||||
|
// reached when returning due to timeout
|
||||||
|
uint32_t ms = SC_TICK_TO_MS(deadline - now + SC_TICK_FROM_MS(1) - 1);
|
||||||
int r = SDL_CondWaitTimeout(cond->cond, mutex->mutex, ms);
|
int r = SDL_CondWaitTimeout(cond->cond, mutex->mutex, ms);
|
||||||
#ifndef NDEBUG
|
#ifndef NDEBUG
|
||||||
if (r < 0) {
|
if (r < 0) {
|
||||||
|
@ -148,6 +150,8 @@ sc_cond_timedwait(sc_cond *cond, sc_mutex *mutex, sc_tick deadline) {
|
||||||
memory_order_relaxed);
|
memory_order_relaxed);
|
||||||
#endif
|
#endif
|
||||||
assert(r == 0 || r == SDL_MUTEX_TIMEDOUT);
|
assert(r == 0 || r == SDL_MUTEX_TIMEDOUT);
|
||||||
|
// The deadline is reached on timeout
|
||||||
|
assert(r != SDL_MUTEX_TIMEDOUT || sc_tick_now() >= deadline);
|
||||||
return r == 0;
|
return r == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue