2 * Copyright 2009-2010 Niels Provos and Nick Mathewson
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "event2/event-config.h"
30 #define WIN32_LEAN_AND_MEAN
32 #undef WIN32_LEAN_AND_MEAN
33 #include <sys/locking.h>
37 #include "event2/thread.h"
39 #include "mm-internal.h"
40 #include "evthread-internal.h"
42 #define SPIN_COUNT 2000
45 evthread_win32_lock_create(unsigned locktype)
47 CRITICAL_SECTION *lock = mm_malloc(sizeof(CRITICAL_SECTION));
50 if (InitializeCriticalSectionAndSpinCount(lock, SPIN_COUNT) == 0) {
58 evthread_win32_lock_free(void *_lock, unsigned locktype)
60 CRITICAL_SECTION *lock = _lock;
61 DeleteCriticalSection(lock);
66 evthread_win32_lock(unsigned mode, void *_lock)
68 CRITICAL_SECTION *lock = _lock;
69 if ((mode & EVTHREAD_TRY)) {
70 return ! TryEnterCriticalSection(lock);
72 EnterCriticalSection(lock);
78 evthread_win32_unlock(unsigned mode, void *_lock)
80 CRITICAL_SECTION *lock = _lock;
81 LeaveCriticalSection(lock);
86 evthread_win32_get_id(void)
88 return (unsigned long) GetCurrentThreadId();
91 #ifdef WIN32_HAVE_CONDITION_VARIABLES
92 static void WINAPI (*InitializeConditionVariable_fn)(PCONDITION_VARIABLE)
94 static BOOL WINAPI (*SleepConditionVariableCS_fn)(
95 PCONDITION_VARIABLE, PCRITICAL_SECTION, DWORD) = NULL;
96 static void WINAPI (*WakeAllConditionVariable_fn)(PCONDITION_VARIABLE) = NULL;
97 static void WINAPI (*WakeConditionVariable_fn)(PCONDITION_VARIABLE) = NULL;
100 evthread_win32_condvar_init(void)
104 lib = GetModuleHandle(TEXT("kernel32.dll"));
109 name##_fn = GetProcAddress(lib, #name)
110 LOAD(InitializeConditionVariable);
111 LOAD(SleepConditionVariable);
112 LOAD(WakeAllConditionVariable);
113 LOAD(WakeConditionVariable);
115 return InitializeConditionVariable_fn && SleepConditionVariableCS_fn &&
116 WakeAllConditionVariable_fn && WakeConditionVariable_fn;
119 /* XXXX Even if we can build this, we don't necessarily want to: the functions
120 * in question didn't exist before Vista, so we'd better LoadProc them. */
122 evthread_win32_condvar_alloc(unsigned condflags)
124 CONDITION_VARIABLE *cond = mm_malloc(sizeof(CONDITION_VARIABLE));
127 InitializeConditionVariable_fn(cond);
132 evthread_win32_condvar_free(void *_cond)
134 CONDITION_VARIABLE *cond = _cond;
135 /* There doesn't _seem_ to be a cleaup fn here... */
140 evthread_win32_condvar_signal(void *_cond, int broadcast)
142 CONDITION_VARIABLE *cond = _cond;
144 WakeAllConditionVariable_fn(cond);
146 WakeConditionVariable_fn(cond);
151 evthread_win32_condvar_wait(void *_cond, void *_lock, const struct timeval *tv)
153 CONDITION_VARIABLE *cond = _cond;
154 CRITICAL_SECTION *lock = _lock;
159 ms = evutil_tv_to_msec(tv);
162 result = SleepConditionVariableCS_fn(cond, lock, ms);
164 if (GetLastError() == WAIT_TIMEOUT)
174 struct evthread_win32_cond {
177 CRITICAL_SECTION lock;
184 evthread_win32_cond_alloc(unsigned flags)
186 struct evthread_win32_cond *cond;
187 if (!(cond = mm_malloc(sizeof(struct evthread_win32_cond))))
189 if (InitializeCriticalSectionAndSpinCount(&cond->lock, SPIN_COUNT)==0) {
193 if ((cond->event = CreateEvent(NULL,TRUE,FALSE,NULL)) == NULL) {
194 DeleteCriticalSection(&cond->lock);
198 cond->n_waiting = cond->n_to_wake = cond->generation = 0;
203 evthread_win32_cond_free(void *_cond)
205 struct evthread_win32_cond *cond = _cond;
206 DeleteCriticalSection(&cond->lock);
207 CloseHandle(cond->event);
212 evthread_win32_cond_signal(void *_cond, int broadcast)
214 struct evthread_win32_cond *cond = _cond;
215 EnterCriticalSection(&cond->lock);
217 cond->n_to_wake = cond->n_waiting;
221 SetEvent(cond->event);
222 LeaveCriticalSection(&cond->lock);
227 evthread_win32_cond_wait(void *_cond, void *_lock, const struct timeval *tv)
229 struct evthread_win32_cond *cond = _cond;
230 CRITICAL_SECTION *lock = _lock;
231 int generation_at_start;
234 DWORD ms = INFINITE, ms_orig = INFINITE, startTime, endTime;
236 ms_orig = ms = evutil_tv_to_msec(tv);
238 EnterCriticalSection(&cond->lock);
240 generation_at_start = cond->generation;
241 LeaveCriticalSection(&cond->lock);
243 LeaveCriticalSection(lock);
245 startTime = GetTickCount();
248 res = WaitForSingleObject(cond->event, ms);
249 EnterCriticalSection(&cond->lock);
250 if (cond->n_to_wake &&
251 cond->generation != generation_at_start) {
257 } else if (res != WAIT_OBJECT_0) {
258 result = (res==WAIT_TIMEOUT) ? 1 : -1;
262 } else if (ms != INFINITE) {
263 endTime = GetTickCount();
264 if (startTime + ms_orig <= endTime) {
265 result = 1; /* Timeout */
270 ms = startTime + ms_orig - endTime;
273 /* If we make it here, we are still waiting. */
274 if (cond->n_to_wake == 0) {
275 /* There is nobody else who should wake up; reset
277 ResetEvent(cond->event);
280 LeaveCriticalSection(&cond->lock);
283 EnterCriticalSection(lock);
285 EnterCriticalSection(&cond->lock);
286 if (!cond->n_waiting)
287 ResetEvent(cond->event);
288 LeaveCriticalSection(&cond->lock);
294 evthread_use_windows_threads(void)
296 struct evthread_lock_callbacks cbs = {
297 EVTHREAD_LOCK_API_VERSION,
298 EVTHREAD_LOCKTYPE_RECURSIVE,
299 evthread_win32_lock_create,
300 evthread_win32_lock_free,
302 evthread_win32_unlock
306 struct evthread_condition_callbacks cond_cbs = {
307 EVTHREAD_CONDITION_API_VERSION,
308 evthread_win32_cond_alloc,
309 evthread_win32_cond_free,
310 evthread_win32_cond_signal,
311 evthread_win32_cond_wait
313 #ifdef WIN32_HAVE_CONDITION_VARIABLES
314 struct evthread_condition_callbacks condvar_cbs = {
315 EVTHREAD_CONDITION_API_VERSION,
316 evthread_win32_condvar_alloc,
317 evthread_win32_condvar_free,
318 evthread_win32_condvar_signal,
319 evthread_win32_condvar_wait
323 evthread_set_lock_callbacks(&cbs);
324 evthread_set_id_callback(evthread_win32_get_id);
325 #ifdef WIN32_HAVE_CONDITION_VARIABLES
326 if (evthread_win32_condvar_init()) {
327 evthread_set_condition_callbacks(&condvar_cbs);
331 evthread_set_condition_callbacks(&cond_cbs);