@@ -56,187 +56,3 @@ pthread_t pthread_self(void)
56
56
t.tid = GetCurrentThreadId();
57
57
return t;
58
58
}
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
0 commit comments