mapi: Avoid Data Execution Prevention on windows.
[mesa.git] / src / mapi / mapi / u_thread.h
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.2
4 *
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 /*
27 * Thread support for gl dispatch.
28 *
29 * Initial version by John Stone (j.stone@acm.org) (johns@cs.umr.edu)
30 * and Christoph Poliwoda (poliwoda@volumegraphics.com)
31 * Revised by Keith Whitwell
32 * Adapted for new gl dispatcher by Brian Paul
33 * Modified for use in mapi by Chia-I Wu
34 */
35
36 /*
37 * If this file is accidentally included by a non-threaded build,
38 * it should not cause the build to fail, or otherwise cause problems.
39 * In general, it should only be included when needed however.
40 */
41
42 #ifndef _U_THREAD_H_
43 #define _U_THREAD_H_
44
45 #include "u_compiler.h"
46
47 #if defined(PTHREADS) || defined(WIN32_THREADS) || defined(BEOS_THREADS)
48 #ifndef THREADS
49 #define THREADS
50 #endif
51 #endif
52
53 /*
54 * POSIX threads. This should be your choice in the Unix world
55 * whenever possible. When building with POSIX threads, be sure
56 * to enable any compiler flags which will cause the MT-safe
57 * libc (if one exists) to be used when linking, as well as any
58 * header macros for MT-safe errno, etc. For Solaris, this is the -mt
59 * compiler flag. On Solaris with gcc, use -D_REENTRANT to enable
60 * proper compiling for MT-safe libc etc.
61 */
62 #if defined(PTHREADS)
63 #include <pthread.h> /* POSIX threads headers */
64
65 struct u_tsd {
66 pthread_key_t key;
67 int initMagic;
68 };
69
70 typedef pthread_mutex_t u_mutex;
71
72 #define u_mutex_declare_static(name) \
73 static u_mutex name = PTHREAD_MUTEX_INITIALIZER
74
75 #define u_mutex_init(name) pthread_mutex_init(&(name), NULL)
76 #define u_mutex_destroy(name) pthread_mutex_destroy(&(name))
77 #define u_mutex_lock(name) (void) pthread_mutex_lock(&(name))
78 #define u_mutex_unlock(name) (void) pthread_mutex_unlock(&(name))
79
80 #endif /* PTHREADS */
81
82
83 /*
84 * Windows threads. Should work with Windows NT and 95.
85 * IMPORTANT: Link with multithreaded runtime library when THREADS are
86 * used!
87 */
88 #ifdef WIN32_THREADS
89 #include <windows.h>
90
91 struct u_tsd {
92 DWORD key;
93 int initMagic;
94 };
95
96 typedef CRITICAL_SECTION u_mutex;
97
98 #define u_mutex_declare_static(name) \
99 /* static */ u_mutex name = { 0, 0, 0, 0, 0, 0 }
100
101 #define u_mutex_init(name) InitializeCriticalSection(&name)
102 #define u_mutex_destroy(name) DeleteCriticalSection(&name)
103 #define u_mutex_lock(name) EnterCriticalSection(&name)
104 #define u_mutex_unlock(name) LeaveCriticalSection(&name)
105
106 #endif /* WIN32_THREADS */
107
108
109 /*
110 * BeOS threads. R5.x required.
111 */
112 #ifdef BEOS_THREADS
113
114 /* Problem with OS.h and this file on haiku */
115 #ifndef __HAIKU__
116 #include <kernel/OS.h>
117 #endif
118
119 #include <support/TLS.h>
120
121 /* The only two typedefs required here
122 * this is cause of the OS.h problem
123 */
124 #ifdef __HAIKU__
125 typedef int32 thread_id;
126 typedef int32 sem_id;
127 #endif
128
129 struct u_tsd {
130 int32 key;
131 int initMagic;
132 };
133
134 /* Use Benaphore, aka speeder semaphore */
135 typedef struct {
136 int32 lock;
137 sem_id sem;
138 } benaphore;
139 typedef benaphore u_mutex;
140
141 #define u_mutex_declare_static(name) \
142 static u_mutex name = { 0, 0 }
143
144 #define u_mutex_init(name) \
145 name.sem = create_sem(0, #name"_benaphore"), \
146 name.lock = 0
147
148 #define u_mutex_destroy(name) \
149 delete_sem(name.sem), \
150 name.lock = 0
151
152 #define u_mutex_lock(name) \
153 if (name.sem == 0) \
154 u_mutex_init(name); \
155 if (atomic_add(&(name.lock), 1) >= 1) \
156 acquire_sem(name.sem)
157
158 #define u_mutex_unlock(name) \
159 if (atomic_add(&(name.lock), -1) > 1) \
160 release_sem(name.sem)
161
162 #endif /* BEOS_THREADS */
163
164
165 /*
166 * THREADS not defined
167 */
168 #ifndef THREADS
169
170 struct u_tsd {
171 int initMagic;
172 };
173
174 typedef unsigned u_mutex;
175
176 #define u_mutex_declare_static(name) static u_mutex name = 0
177 #define u_mutex_init(name) (void) name
178 #define u_mutex_destroy(name) (void) name
179 #define u_mutex_lock(name) (void) name
180 #define u_mutex_unlock(name) (void) name
181
182 #endif /* THREADS */
183
184
185 unsigned long
186 u_thread_self(void);
187
188 void
189 u_tsd_init(struct u_tsd *tsd);
190
191 void
192 u_tsd_destroy(struct u_tsd *tsd); /* WIN32 only */
193
194 void *
195 u_tsd_get(struct u_tsd *tsd);
196
197 void
198 u_tsd_set(struct u_tsd *tsd, void *ptr);
199
200 #endif /* _U_THREAD_H_ */