Re: [PATCH v7 03/10] compat/win32: add pthread_cond_timedwait
- From
Patrick Steinhardt <ps@pks.im>
- Date
- Mar 4, 2026, 07:42 UTC
- Message-ID
- <aafieD42pMaYsnRw@pks.im>
- In-Reply-To
- <d2c5ca09396e020adb717055d82f50de7c1b7431.1772065643.git.gitgitgadget@gmail.com>
On Thu, Feb 26, 2026 at 12:27:16AM +0000, Paul Tarjan via GitGitGadget wrote:
Show 23 quoted lines
> diff --git a/compat/win32/pthread.c b/compat/win32/pthread.c
> index 7e93146963..538ef92d9d 100644
> --- a/compat/win32/pthread.c
> +++ b/compat/win32/pthread.c
> @@ -66,3 +66,29 @@ int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
> return err_win_to_posix(GetLastError());
> return 0;
> }
> +
> +int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
> + const struct timespec *abstime)
> +{
> + struct timeval now;
> + long long now_ms, deadline_ms;
> + DWORD timeout_ms;
> +
> + gettimeofday(&now, NULL);
> + now_ms = (long long)now.tv_sec * 1000 + now.tv_usec / 1000;
> + deadline_ms = (long long)abstime->tv_sec * 1000 +
> + abstime->tv_nsec / 1000000;
> +
> + if (deadline_ms <= now_ms)
> + timeout_ms = 0;According to pthread_cond_timedwait(3p) we should return an error in thas case:
The pthread_cond_timedwait() function shall be equivalent to pthread_cond_wait(), except that an error is returned if the absolute time specified by abstime passes (that is, system time equals or exceeds abstime) before the condition cond is signaled or broadcasted, or if the absolute time specified by abstime has already been passed at the time of the call.
So I guess it's safe to return ETIMEDOUT directly here?
> + else
> + timeout_ms = (DWORD)(deadline_ms - now_ms);
> +
> + if (SleepConditionVariableCS(cond, mutex, timeout_ms) == 0) {The function returns a BOOL, so comparing with `== 0` is misleading. I see that this is following the pattern of `pthread_cond_wait()` though, so I guess it's okayish.
> + DWORD err = GetLastError(); > + if (err == ERROR_TIMEOUT) > + return ETIMEDOUT; > + return err_win_to_posix(err);
Wouldn't it make sense to extend `err_win_to_posix()` instead?
Patrick