compat/win32: add pthread_cond_timedwait
Add a pthread_cond_timedwait() implementation to the Windows pthread compatibility layer using SleepConditionVariableCS() with a millisecond timeout computed from the absolute deadline. Signed-off-by: Paul Tarjan <github@paulisageek.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Paul Tarjan committed
Apr 15, 2026 at 13:27 UTC
8372c88f583b8910f1e57c00c89c0afcca7018dc
2 files changed
+28
compat/win32/pthread.c
+26
@@ -66,3 +66,29 @@ int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
66
return err_win_to_posix(GetLastError());
67
return 0;
68
}
69
+
70
+int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
71
+ const struct timespec *abstime)
72
+{
73
+ struct timeval now;
74
+ long long now_ms, deadline_ms;
75
+ DWORD timeout_ms;
76
+
77
+ gettimeofday(&now, NULL);
78
+ now_ms = (long long)now.tv_sec * 1000 + now.tv_usec / 1000;
79
+ deadline_ms = (long long)abstime->tv_sec * 1000 +
80
+ abstime->tv_nsec / 1000000;
81
+
82
+ if (deadline_ms <= now_ms)
83
+ return ETIMEDOUT;
84
+ else
85
+ timeout_ms = (DWORD)(deadline_ms - now_ms);
86
+
87
+ if (SleepConditionVariableCS(cond, mutex, timeout_ms) == 0) {
88
+ DWORD err = GetLastError();
89
+ if (err == ERROR_TIMEOUT)
90
+ return ETIMEDOUT;
91
+ return err_win_to_posix(err);
92
+ }
93
+ return 0;
94
+}
compat/win32/pthread.h
+2
@@ -64,6 +64,8 @@ int win32_pthread_join(pthread_t *thread, void **value_ptr);
64
pthread_t pthread_self(void);
65
66
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
67
+int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
68
+ const struct timespec *abstime);
69
70
static inline void NORETURN pthread_exit(void *ret)
71
{