[demo] use pthread_cond_clockwait() with CLOCK_MONOTONIC #2291

This commit is contained in:
nick black 2021-10-21 16:04:34 -04:00 committed by nick black
parent 3e0ce64b76
commit 864e2710a3
3 changed files with 6 additions and 6 deletions

View File

@ -13,6 +13,8 @@ rearrangements of Notcurses.
* 2.4.8 (not yet released)
* Added `notcurses_canpixel()`.
* `notcurses_get()` now evaluates its timeout against `CLOCK_MONOTONIC`
instead of `CLOCK_REALTIME`.
* 2.4.7 (2021-10-16)
* Features 1, 2, and 8 of the Kitty keyboard protocol are now supported. This

View File

@ -1083,7 +1083,7 @@ ncinput_equal_p(const ncinput* n1, const ncinput* n2){
// timespec to bound blocking. Returns a single Unicode code point, or
// (uint32_t)-1 on error. Returns 0 on a timeout. If an event is processed, the
// return value is the 'id' field from that event. 'ni' may be NULL. 'ts' is an
// a delay bound against gettimeofday() (see pthread_cond_timedwait(3)).
// a delay bound against CLOCK_MONOTONIC (see pthread_cond_clockwait(3)).
API uint32_t notcurses_get(struct notcurses* n, const struct timespec* ts,
ncinput* ni)
__attribute__ ((nonnull (1)));

View File

@ -47,10 +47,8 @@ handle_mouse(const ncinput* ni){
// absolute deadline, so convert it up.
uint32_t demo_getc(struct notcurses* nc, const struct timespec* ts, ncinput* ni){
struct timespec now;
clock_gettime(CLOCK_REALTIME, &now);
clock_gettime(CLOCK_MONOTONIC, &now);
uint64_t ns;
// yes, i'd like CLOCK_MONOTONIC too, but pthread_cond_timedwait() is based off
// of crappy CLOCK_REALTIME :/
// abstime shouldn't be further out than our maximum sleep time -- this can
// lead to 0 frames output during the wait
if(ts){
@ -68,12 +66,12 @@ uint32_t demo_getc(struct notcurses* nc, const struct timespec* ts, ncinput* ni)
do{
pthread_mutex_lock(&lock);
while(!queue){
clock_gettime(CLOCK_REALTIME, &now);
clock_gettime(CLOCK_MONOTONIC, &now);
if(timespec_to_ns(&now) > timespec_to_ns(&abstime)){
pthread_mutex_unlock(&lock);
return 0;
}
pthread_cond_timedwait(&cond, &lock, &abstime);
pthread_cond_clockwait(&cond, &lock, CLOCK_MONOTONIC, &abstime);
}
nciqueue* q = queue;
queue = queue->next;