From a384fa057c177e44adf33012279cf6dd7d87a4a1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Henrik=20Grubbstr=C3=B6m=20=28Grubba=29?=
 <grubba@grubba.org>
Date: Fri, 26 Nov 2021 18:05:45 +0100
Subject: [PATCH] Threads.Condition: Fix wait() with sub-second timeout.

pthread_cond_timedwait() does NOT normalize tv_nsec. A wait with
a tv_sec of zero and a tv_nsec >= 1000000000 seems to give no
wait at all.

Fixes #10070.
---
 src/threads.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/src/threads.c b/src/threads.c
index a0c0be1381..9529bfe68b 100644
--- a/src/threads.c
+++ b/src/threads.c
@@ -296,6 +296,11 @@ PMOD_EXPORT int co_wait_timeout(COND_T *c, PIKE_MUTEX_T *m, long s, long nanos)
   ACCURATE_GETTIMEOFDAY(&ct);
   timeout.tv_sec = ct.tv_sec + s;
   timeout.tv_nsec = ct.tv_usec * 1000 + nanos;
+  s = timeout.tv_nsec/1000000000;
+  if (s) {
+    timeout.tv_sec += s;
+    timeout.tv_nsec -= s * 1000000000;
+  }
   return pthread_cond_timedwait(c, m, &timeout);
 #endif /* HAVE_PTHREAD_COND_RELTIMEDWAIT_NP */
 #else /* !POSIX_THREADS */
-- 
GitLab