Skip to content

Commit cbfb120

Browse files
rongjiecomputerdscho
authored andcommitted
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 <[email protected]> fast-forwarded.
1 parent 9940e9b commit cbfb120

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

compat/win32/pthread.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ pthread_t pthread_self(void)
5757
return t;
5858
}
5959

60+
#ifdef GIT_WIN_XP_SUPPORT
61+
6062
int pthread_cond_init(pthread_cond_t *cond, const void *unused)
6163
{
6264
cond->waiters = 0;
@@ -194,3 +196,41 @@ int pthread_cond_broadcast(pthread_cond_t *cond)
194196
}
195197
return 0;
196198
}
199+
200+
#else // GIT_WIN_XP_SUPPORT
201+
202+
WINBASEAPI VOID WINAPI InitializeConditionVariable (PCONDITION_VARIABLE ConditionVariable);
203+
WINBASEAPI VOID WINAPI WakeConditionVariable (PCONDITION_VARIABLE ConditionVariable);
204+
WINBASEAPI VOID WINAPI WakeAllConditionVariable (PCONDITION_VARIABLE ConditionVariable);
205+
WINBASEAPI WINBOOL WINAPI SleepConditionVariableCS (PCONDITION_VARIABLE ConditionVariable, PCRITICAL_SECTION CriticalSection, DWORD dwMilliseconds);
206+
207+
int pthread_cond_init(pthread_cond_t *cond, const void *unused)
208+
{
209+
InitializeConditionVariable(cond);
210+
return 0;
211+
}
212+
213+
int pthread_cond_destroy(pthread_cond_t *cond)
214+
{
215+
return 0;
216+
}
217+
218+
int pthread_cond_wait(pthread_cond_t *cond, CRITICAL_SECTION *mutex)
219+
{
220+
SleepConditionVariableCS(cond, mutex, INFINITE);
221+
return 0;
222+
}
223+
224+
int pthread_cond_signal(pthread_cond_t *cond)
225+
{
226+
WakeConditionVariable(cond);
227+
return 0;
228+
}
229+
230+
int pthread_cond_broadcast(pthread_cond_t *cond)
231+
{
232+
WakeAllConditionVariable(cond);
233+
return 0;
234+
}
235+
236+
#endif // GIT_WIN_XP_SUPPORT

compat/win32/pthread.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ typedef int pthread_mutexattr_t;
3232
#define pthread_mutexattr_settype(a, t) 0
3333
#define PTHREAD_MUTEX_RECURSIVE 0
3434

35+
#ifdef GIT_WIN_XP_SUPPORT
3536
/*
3637
* Implement simple condition variable for Windows threads, based on ACE
3738
* implementation.
@@ -47,6 +48,9 @@ typedef struct {
4748
HANDLE sema;
4849
HANDLE continue_broadcast;
4950
} pthread_cond_t;
51+
#else
52+
typedef CONDITION_VARIABLE pthread_cond_t;
53+
#endif
5054

5155
extern int pthread_cond_init(pthread_cond_t *cond, const void *unused);
5256
extern int pthread_cond_destroy(pthread_cond_t *cond);

0 commit comments

Comments
 (0)