xref: /libevent-2.1.12/evthread_win32.c (revision 2f33e00a)
1 /*
2  * Copyright 2009 Niels Provos and Nick Mathewson
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
14  *
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.
25  */
26 #include "event-config.h"
27 
28 #ifdef WIN32
29 #include <winsock2.h>
30 #define WIN32_LEAN_AND_MEAN
31 #include <windows.h>
32 #undef WIN32_LEAN_AND_MEAN
33 #include <sys/locking.h>
34 #endif
35 
36 struct event_base;
37 #include <event2/thread.h>
38 
39 #include "mm-internal.h"
40 
41 static void *
42 evthread_win32_lock_create(unsigned locktype)
43 {
44 	CRITICAL_SECTION *lock = mm_malloc(sizeof(CRITICAL_SECTION));
45 	if (!lock)
46 		return NULL;
47 	InitializeCriticalSection(lock);
48 	return lock;
49 }
50 
51 static void
52 evthread_win32_lock_free(void *_lock, unsigned locktype)
53 {
54 	CRITICAL_SECTION *lock = _lock;
55 	DeleteCriticalSection(lock);
56 	mm_free(lock);
57 }
58 
59 static int
60 evthread_win32_lock(unsigned mode, void *_lock)
61 {
62 	CRITICAL_SECTION *lock = _lock;
63 	if ((mode & EVTHREAD_TRY)) {
64 		return TryEnterCriticalSection(lock) != 0;
65 	} else {
66 		EnterCriticalSection(lock);
67 		return 0;
68 	}
69 }
70 
71 static int
72 evthread_win32_unlock(unsigned mode, void *_lock)
73 {
74 	CRITICAL_SECTION *lock = _lock;
75 	LeaveCriticalSection(lock);
76 	return 0;
77 }
78 
79 static unsigned long
80 evthread_win32_get_id(void)
81 {
82 	return (unsigned long) GetCurrentThreadId();
83 }
84 
85 int
86 evthread_use_windows_threads(void)
87 {
88 	struct evthread_lock_callbacks cbs = {
89 		EVTHREAD_LOCK_API_VERSION,
90 		EVTHREAD_LOCKTYPE_RECURSIVE,
91 		evthread_win32_lock_create,
92 		evthread_win32_lock_free,
93 		evthread_win32_lock,
94 		evthread_win32_unlock
95 	};
96 
97 	evthread_set_lock_callbacks(&cbs);
98 	evthread_set_id_callback(evthread_win32_get_id);
99 	return 0;
100 }
101 
102