From 83fe94d4977745f6358d75a423673d5a5fc2036c Mon Sep 17 00:00:00 2001 From: Christian Buttner Date: Mon, 24 Jun 2024 11:52:21 +0200 Subject: [PATCH] Fix posix NativeConditionVariable.wait_timeout. (#1211) Fix posix NativeConditionVariable.wait_timeout. TimeSpec::ns may not exceed one second. --- lib/std/libc/libc.c3 | 4 ++-- lib/std/threads/os/thread_posix.c3 | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/std/libc/libc.c3 b/lib/std/libc/libc.c3 index 1a143777b..bf7c65eba 100644 --- a/lib/std/libc/libc.c3 +++ b/lib/std/libc/libc.c3 @@ -183,8 +183,8 @@ extern fn isz write(Fd fd, void* buffer, usz count) @if(!env::WIN32); extern fn CFile fmemopen(void* ptr, usz size, ZString mode); extern fn isz getline(char** linep, usz* linecapp, CFile stream); -extern fn int timespec_get(TimeSpec* ts, int base); -extern fn int nanosleep(TimeSpec* req, TimeSpec* remaining); +extern fn CInt timespec_get(TimeSpec* ts, CInt base); +extern fn CInt nanosleep(TimeSpec* req, TimeSpec* remaining); extern fn ZString ctime(Time_t *timer); extern fn Time_t time(Time_t *timer); diff --git a/lib/std/threads/os/thread_posix.c3 b/lib/std/threads/os/thread_posix.c3 index efb7ef710..676fc0752 100644 --- a/lib/std/threads/os/thread_posix.c3 +++ b/lib/std/threads/os/thread_posix.c3 @@ -128,8 +128,9 @@ fn void! NativeConditionVariable.wait_timeout(&cond, NativeMutex* mtx, ulong ms) { TimeSpec now; if (libc::timespec_get(&now, libc::TIME_UTC) != libc::TIME_UTC) return ThreadFault.WAIT_FAILED?; - now.s += (Time_t)(ms / 1000); now.ns += (CLong)((ms % 1000) * 1000_000); + now.s += (Time_t)(ms / 1000 + now.ns / 1000_000_000); + now.ns = now.ns % 1000_000_000; switch (posix::pthread_cond_timedwait(cond, &mtx.mutex, &now)) { case errno::ETIMEDOUT: