Skip to content

Commit 4ffb00f

Browse files
ptarjangitster
authored andcommitted
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>
1 parent 524c3e6 commit 4ffb00f

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

compat/win32/pthread.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,29 @@ int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
6666
return err_win_to_posix(GetLastError());
6767
return 0;
6868
}
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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ int win32_pthread_join(pthread_t *thread, void **value_ptr);
6464
pthread_t pthread_self(void);
6565

6666
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);
6769

6870
static inline void NORETURN pthread_exit(void *ret)
6971
{

0 commit comments

Comments
 (0)