]> git.ipfire.org Git - thirdparty/git.git/commitdiff
compat/win32: add pthread_cond_timedwait
authorPaul Tarjan <github@paulisageek.com>
Thu, 9 Apr 2026 04:59:26 +0000 (04:59 +0000)
committerJunio C Hamano <gitster@pobox.com>
Thu, 9 Apr 2026 17:59:27 +0000 (10:59 -0700)
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>
compat/win32/pthread.c
compat/win32/pthread.h

index 7e93146963ec56b9c6b126151fd3e01fdabd3f6f..398caa96029718acece706835633e9a2b3e95703 100644 (file)
@@ -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)
+               return ETIMEDOUT;
+       else
+               timeout_ms = (DWORD)(deadline_ms - now_ms);
+
+       if (SleepConditionVariableCS(cond, mutex, timeout_ms) == 0) {
+               DWORD err = GetLastError();
+               if (err == ERROR_TIMEOUT)
+                       return ETIMEDOUT;
+               return err_win_to_posix(err);
+       }
+       return 0;
+}
index ccacc5a53ba9766ca3a2c8fa100b1033199362f2..d80df8d12af2dc3078cd82e38d42fa7df941f66c 100644 (file)
@@ -64,6 +64,8 @@ int win32_pthread_join(pthread_t *thread, void **value_ptr);
 pthread_t pthread_self(void);
 
 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
+int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
+                          const struct timespec *abstime);
 
 static inline void NORETURN pthread_exit(void *ret)
 {