From db39ffdbcc8c616b7010aa93f06637d87ee7ae02 Mon Sep 17 00:00:00 2001 From: Loo Rong Jie Date: Thu, 22 Jun 2017 09:03:29 +0800 Subject: [PATCH] Implement pthread_cond_t with Win32 CONDITION_VARIABLE Win32 CONDITION_VARIABLE has better performance and is easier to maintain. Since CONDITION_VARIABLE is not available in Windows XP and below, old implementation of pthread_cond_t is kept under define guard 'GIT_WIN_XP_SUPPORT'. To enable old implementation, build with make CFLAGS="-DGIT_WIN_XP_SUPPORT". Signed-off-by: Loo Rong Jie fast-forwarded. --- compat/win32/pthread.c | 40 ++++++++++++++++++++++++++++++++++++++++ compat/win32/pthread.h | 4 ++++ 2 files changed, 44 insertions(+) diff --git a/compat/win32/pthread.c b/compat/win32/pthread.c index e18f5c6e2e55a4..80243b67cb8dc1 100644 --- a/compat/win32/pthread.c +++ b/compat/win32/pthread.c @@ -57,6 +57,8 @@ pthread_t pthread_self(void) return t; } +#ifdef GIT_WIN_XP_SUPPORT + int pthread_cond_init(pthread_cond_t *cond, const void *unused) { cond->waiters = 0; @@ -194,3 +196,41 @@ int pthread_cond_broadcast(pthread_cond_t *cond) } return 0; } + +#else // GIT_WIN_XP_SUPPORT + +WINBASEAPI VOID WINAPI InitializeConditionVariable (PCONDITION_VARIABLE ConditionVariable); +WINBASEAPI VOID WINAPI WakeConditionVariable (PCONDITION_VARIABLE ConditionVariable); +WINBASEAPI VOID WINAPI WakeAllConditionVariable (PCONDITION_VARIABLE ConditionVariable); +WINBASEAPI WINBOOL WINAPI SleepConditionVariableCS (PCONDITION_VARIABLE ConditionVariable, PCRITICAL_SECTION CriticalSection, DWORD dwMilliseconds); + +int pthread_cond_init(pthread_cond_t *cond, const void *unused) +{ + InitializeConditionVariable(cond); + return 0; +} + +int pthread_cond_destroy(pthread_cond_t *cond) +{ + return 0; +} + +int pthread_cond_wait(pthread_cond_t *cond, CRITICAL_SECTION *mutex) +{ + SleepConditionVariableCS(cond, mutex, INFINITE); + return 0; +} + +int pthread_cond_signal(pthread_cond_t *cond) +{ + WakeConditionVariable(cond); + return 0; +} + +int pthread_cond_broadcast(pthread_cond_t *cond) +{ + WakeAllConditionVariable(cond); + return 0; +} + +#endif // GIT_WIN_XP_SUPPORT diff --git a/compat/win32/pthread.h b/compat/win32/pthread.h index 1c164088fbb64d..10a9d45f07eadd 100644 --- a/compat/win32/pthread.h +++ b/compat/win32/pthread.h @@ -32,6 +32,7 @@ typedef int pthread_mutexattr_t; #define pthread_mutexattr_settype(a, t) 0 #define PTHREAD_MUTEX_RECURSIVE 0 +#ifdef GIT_WIN_XP_SUPPORT /* * Implement simple condition variable for Windows threads, based on ACE * implementation. @@ -47,6 +48,9 @@ typedef struct { HANDLE sema; HANDLE continue_broadcast; } pthread_cond_t; +#else +typedef CONDITION_VARIABLE pthread_cond_t; +#endif extern int pthread_cond_init(pthread_cond_t *cond, const void *unused); extern int pthread_cond_destroy(pthread_cond_t *cond);