Skip to content

Commit

Permalink
threading: Support pthread_cond_timedwait_relative_np
Browse files Browse the repository at this point in the history
- This function is available on Apple devices and older 32-bit Android
- Serves as a replacement for otherwise lacking CLOCK_MONOTONIC functionality
  • Loading branch information
LekKit committed Jun 23, 2023
1 parent ce141e5 commit 936ec4b
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/threading.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,19 @@ bool condvar_wait_ns(cond_var_t* cond, uint64_t timeout_ns)
if (timeout_ns == CONDVAR_INFINITE) {
ret = pthread_cond_wait(&cond->cond, &cond->lock) == 0;
} else {
#if defined(__APPLE__) || defined(HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE)
struct timespec ts = { .tv_sec = timeout_ns / 1000000000, .tv_nsec = timeout_ns % 1000000000, };
UNUSED(condvar_fill_timespec);
ret = pthread_cond_timedwait_relative_np(&cond->cond, &cond->lock, &ts) == 0;
#else
struct timespec ts = {0};
condvar_fill_timespec(&ts);
// Properly handle timespec addition without an overflow
timeout_ns += ts.tv_nsec;
ts.tv_sec += timeout_ns / 1000000000;
ts.tv_nsec = timeout_ns % 1000000000;
ret = pthread_cond_timedwait(&cond->cond, &cond->lock, &ts) == 0;
#endif
}
}
pthread_mutex_unlock(&cond->lock);
Expand Down

0 comments on commit 936ec4b

Please sign in to comment.