Skip to content

Commit 19a7a90

Browse files
rongjiecomputerjamill
authored andcommitted
Remove old code and macro-ize implementation
Signed-off-by: Loo Rong Jie <[email protected]>
1 parent aa6aa15 commit 19a7a90

File tree

2 files changed

+18
-209
lines changed

2 files changed

+18
-209
lines changed

compat/win32/pthread.c

Lines changed: 0 additions & 184 deletions
Original file line numberDiff line numberDiff line change
@@ -56,187 +56,3 @@ pthread_t pthread_self(void)
5656
t.tid = GetCurrentThreadId();
5757
return t;
5858
}
59-
60-
#ifdef GIT_WIN_XP_SUPPORT
61-
62-
int pthread_cond_init(pthread_cond_t *cond, const void *unused)
63-
{
64-
cond->waiters = 0;
65-
cond->was_broadcast = 0;
66-
InitializeCriticalSection(&cond->waiters_lock);
67-
68-
cond->sema = CreateSemaphore(NULL, 0, LONG_MAX, NULL);
69-
if (!cond->sema)
70-
die("CreateSemaphore() failed");
71-
72-
cond->continue_broadcast = CreateEvent(NULL, /* security */
73-
FALSE, /* auto-reset */
74-
FALSE, /* not signaled */
75-
NULL); /* name */
76-
if (!cond->continue_broadcast)
77-
die("CreateEvent() failed");
78-
79-
return 0;
80-
}
81-
82-
int pthread_cond_destroy(pthread_cond_t *cond)
83-
{
84-
CloseHandle(cond->sema);
85-
CloseHandle(cond->continue_broadcast);
86-
DeleteCriticalSection(&cond->waiters_lock);
87-
return 0;
88-
}
89-
90-
int pthread_cond_wait(pthread_cond_t *cond, CRITICAL_SECTION *mutex)
91-
{
92-
int last_waiter;
93-
94-
EnterCriticalSection(&cond->waiters_lock);
95-
cond->waiters++;
96-
LeaveCriticalSection(&cond->waiters_lock);
97-
98-
/*
99-
* Unlock external mutex and wait for signal.
100-
* NOTE: we've held mutex locked long enough to increment
101-
* waiters count above, so there's no problem with
102-
* leaving mutex unlocked before we wait on semaphore.
103-
*/
104-
LeaveCriticalSection(mutex);
105-
106-
/* let's wait - ignore return value */
107-
WaitForSingleObject(cond->sema, INFINITE);
108-
109-
/*
110-
* Decrease waiters count. If we are the last waiter, then we must
111-
* notify the broadcasting thread that it can continue.
112-
* But if we continued due to cond_signal, we do not have to do that
113-
* because the signaling thread knows that only one waiter continued.
114-
*/
115-
EnterCriticalSection(&cond->waiters_lock);
116-
cond->waiters--;
117-
last_waiter = cond->was_broadcast && cond->waiters == 0;
118-
LeaveCriticalSection(&cond->waiters_lock);
119-
120-
if (last_waiter) {
121-
/*
122-
* cond_broadcast was issued while mutex was held. This means
123-
* that all other waiters have continued, but are contending
124-
* for the mutex at the end of this function because the
125-
* broadcasting thread did not leave cond_broadcast, yet.
126-
* (This is so that it can be sure that each waiter has
127-
* consumed exactly one slice of the semaphor.)
128-
* The last waiter must tell the broadcasting thread that it
129-
* can go on.
130-
*/
131-
SetEvent(cond->continue_broadcast);
132-
/*
133-
* Now we go on to contend with all other waiters for
134-
* the mutex. Auf in den Kampf!
135-
*/
136-
}
137-
/* lock external mutex again */
138-
EnterCriticalSection(mutex);
139-
140-
return 0;
141-
}
142-
143-
/*
144-
* IMPORTANT: This implementation requires that pthread_cond_signal
145-
* is called while the mutex is held that is used in the corresponding
146-
* pthread_cond_wait calls!
147-
*/
148-
int pthread_cond_signal(pthread_cond_t *cond)
149-
{
150-
int have_waiters;
151-
152-
EnterCriticalSection(&cond->waiters_lock);
153-
have_waiters = cond->waiters > 0;
154-
LeaveCriticalSection(&cond->waiters_lock);
155-
156-
/*
157-
* Signal only when there are waiters
158-
*/
159-
if (have_waiters)
160-
return ReleaseSemaphore(cond->sema, 1, NULL) ?
161-
0 : err_win_to_posix(GetLastError());
162-
else
163-
return 0;
164-
}
165-
166-
/*
167-
* DOUBLY IMPORTANT: This implementation requires that pthread_cond_broadcast
168-
* is called while the mutex is held that is used in the corresponding
169-
* pthread_cond_wait calls!
170-
*/
171-
int pthread_cond_broadcast(pthread_cond_t *cond)
172-
{
173-
EnterCriticalSection(&cond->waiters_lock);
174-
175-
if ((cond->was_broadcast = cond->waiters > 0)) {
176-
/* wake up all waiters */
177-
ReleaseSemaphore(cond->sema, cond->waiters, NULL);
178-
LeaveCriticalSection(&cond->waiters_lock);
179-
/*
180-
* At this point all waiters continue. Each one takes its
181-
* slice of the semaphor. Now it's our turn to wait: Since
182-
* the external mutex is held, no thread can leave cond_wait,
183-
* yet. For this reason, we can be sure that no thread gets
184-
* a chance to eat *more* than one slice. OTOH, it means
185-
* that the last waiter must send us a wake-up.
186-
*/
187-
WaitForSingleObject(cond->continue_broadcast, INFINITE);
188-
/*
189-
* Since the external mutex is held, no thread can enter
190-
* cond_wait, and, hence, it is safe to reset this flag
191-
* without cond->waiters_lock held.
192-
*/
193-
cond->was_broadcast = 0;
194-
} else {
195-
LeaveCriticalSection(&cond->waiters_lock);
196-
}
197-
return 0;
198-
}
199-
200-
#else // GIT_WIN_XP_SUPPORT
201-
202-
WINBASEAPI VOID WINAPI
203-
InitializeConditionVariable(PCONDITION_VARIABLE ConditionVariable);
204-
WINBASEAPI VOID WINAPI
205-
WakeConditionVariable(PCONDITION_VARIABLE ConditionVariable);
206-
WINBASEAPI VOID WINAPI
207-
WakeAllConditionVariable(PCONDITION_VARIABLE ConditionVariable);
208-
WINBASEAPI WINBOOL WINAPI
209-
SleepConditionVariableCS(PCONDITION_VARIABLE ConditionVariable,
210-
PCRITICAL_SECTION CriticalSection,
211-
DWORD dwMilliseconds);
212-
213-
int pthread_cond_init(pthread_cond_t *cond, const void *unused)
214-
{
215-
InitializeConditionVariable(cond);
216-
return 0;
217-
}
218-
219-
int pthread_cond_destroy(pthread_cond_t *cond)
220-
{
221-
return 0;
222-
}
223-
224-
int pthread_cond_wait(pthread_cond_t *cond, CRITICAL_SECTION *mutex)
225-
{
226-
SleepConditionVariableCS(cond, mutex, INFINITE);
227-
return 0;
228-
}
229-
230-
int pthread_cond_signal(pthread_cond_t *cond)
231-
{
232-
WakeConditionVariable(cond);
233-
return 0;
234-
}
235-
236-
int pthread_cond_broadcast(pthread_cond_t *cond)
237-
{
238-
WakeAllConditionVariable(cond);
239-
return 0;
240-
}
241-
242-
#endif // GIT_WIN_XP_SUPPORT

compat/win32/pthread.h

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,24 @@ 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
36-
/*
37-
* Implement simple condition variable for Windows threads, based on ACE
38-
* implementation.
39-
*
40-
* See original implementation: http://bit.ly/1vkDjo
41-
* ACE homepage: http://www.cse.wustl.edu/~schmidt/ACE.html
42-
* See also: http://www.cse.wustl.edu/~schmidt/win32-cv-1.html
43-
*/
44-
typedef struct {
45-
LONG waiters;
46-
int was_broadcast;
47-
CRITICAL_SECTION waiters_lock;
48-
HANDLE sema;
49-
HANDLE continue_broadcast;
50-
} pthread_cond_t;
51-
#else
52-
typedef CONDITION_VARIABLE pthread_cond_t;
53-
#endif
54-
55-
extern int pthread_cond_init(pthread_cond_t *cond, const void *unused);
56-
extern int pthread_cond_destroy(pthread_cond_t *cond);
57-
extern int pthread_cond_wait(pthread_cond_t *cond, CRITICAL_SECTION *mutex);
58-
extern int pthread_cond_signal(pthread_cond_t *cond);
59-
extern int pthread_cond_broadcast(pthread_cond_t *cond);
35+
#define pthread_cond_t CONDITION_VARIABLE
36+
37+
WINBASEAPI VOID WINAPI
38+
InitializeConditionVariable(PCONDITION_VARIABLE ConditionVariable);
39+
WINBASEAPI VOID WINAPI
40+
WakeConditionVariable(PCONDITION_VARIABLE ConditionVariable);
41+
WINBASEAPI VOID WINAPI
42+
WakeAllConditionVariable(PCONDITION_VARIABLE ConditionVariable);
43+
WINBASEAPI BOOL WINAPI
44+
SleepConditionVariableCS(PCONDITION_VARIABLE ConditionVariable,
45+
PCRITICAL_SECTION CriticalSection,
46+
DWORD dwMilliseconds);
47+
48+
#define pthread_cond_init(a,b) InitializeConditionVariable((a))
49+
#define pthread_cond_destroy(a) do {} while (0)
50+
#define pthread_cond_wait(a,b) return_0(SleepConditionVariableCS((a), (b), INFINITE))
51+
#define pthread_cond_signal WakeConditionVariable
52+
#define pthread_cond_broadcast WakeAllConditionVariable
6053

6154
/*
6255
* Simple thread creation implementation using pthread API

0 commit comments

Comments
 (0)