summaryrefslogtreecommitdiffstats
path: root/src/sem.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2022-08-21 22:13:16 -0400
committerDavid Robillard <d@drobilla.net>2022-10-14 17:53:49 -0400
commit09cfa793d3e227807ed3d6a1a835c92af2f57311 (patch)
treec0a912fbc7defcf7d064a90af54d640753e1f4d3 /src/sem.c
parented6bd603040cdb76c94c7cde25f5dbb721ca10c8 (diff)
downloadzix-09cfa793d3e227807ed3d6a1a835c92af2f57311.tar.gz
zix-09cfa793d3e227807ed3d6a1a835c92af2f57311.tar.bz2
zix-09cfa793d3e227807ed3d6a1a835c92af2f57311.zip
Fix zix_sem_timed_wait() interval calculation
Diffstat (limited to 'src/sem.c')
-rw-r--r--src/sem.c9
1 files changed, 9 insertions, 0 deletions
diff --git a/src/sem.c b/src/sem.c
index d9aa6d1..cb00c3a 100644
--- a/src/sem.c
+++ b/src/sem.c
@@ -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