diff options
author | David Robillard <d@drobilla.net> | 2022-08-21 22:13:16 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2022-10-14 17:53:49 -0400 |
commit | 09cfa793d3e227807ed3d6a1a835c92af2f57311 (patch) | |
tree | c0a912fbc7defcf7d064a90af54d640753e1f4d3 | |
parent | ed6bd603040cdb76c94c7cde25f5dbb721ca10c8 (diff) | |
download | zix-09cfa793d3e227807ed3d6a1a835c92af2f57311.tar.gz zix-09cfa793d3e227807ed3d6a1a835c92af2f57311.tar.bz2 zix-09cfa793d3e227807ed3d6a1a835c92af2f57311.zip |
Fix zix_sem_timed_wait() interval calculation
-rw-r--r-- | src/sem.c | 9 | ||||
-rw-r--r-- | test/test_sem.c | 2 |
2 files changed, 10 insertions, 1 deletions
@@ -178,8 +178,11 @@ zix_sem_timed_wait(ZixSem* sem, const uint32_t seconds, const uint32_t nanoseconds) { +# define NS_PER_SECOND 1000000000L + # if !USE_CLOCK_GETTIME || !USE_SEM_TIMEDWAIT return ZIX_STATUS_NOT_SUPPORTED; + # else struct timespec ts = {0, 0}; @@ -188,6 +191,10 @@ zix_sem_timed_wait(ZixSem* sem, if (!(r = clock_gettime(CLOCK_REALTIME, &ts))) { ts.tv_sec += (time_t)seconds; ts.tv_nsec += (long)nanoseconds; + if (ts.tv_nsec >= NS_PER_SECOND) { + ts.tv_nsec -= NS_PER_SECOND; + ts.tv_sec++; + } while ((r = sem_timedwait(&sem->sem, &ts)) && errno == EINTR) { // Interrupted, try again @@ -196,6 +203,8 @@ zix_sem_timed_wait(ZixSem* sem, return zix_errno_status_if(r); # endif + +# undef NS_PER_SECOND } #endif diff --git a/test/test_sem.c b/test/test_sem.c index 36d01c5..9517659 100644 --- a/test/test_sem.c +++ b/test/test_sem.c @@ -57,7 +57,7 @@ test_timed_wait(void) { assert(!zix_sem_init(&sem, 0)); assert(zix_sem_timed_wait(&sem, 0, 0) == ZIX_STATUS_TIMEOUT); - assert(zix_sem_timed_wait(&sem, 0, 5000000) == ZIX_STATUS_TIMEOUT); + assert(zix_sem_timed_wait(&sem, 0, 999999999) == ZIX_STATUS_TIMEOUT); assert(!zix_sem_post(&sem)); assert(!zix_sem_timed_wait(&sem, 5, 0)); assert(!zix_sem_post(&sem)); |