glapi: Move to src/mapi/.
[mesa.git] / src / mapi / glapi / glthread.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 *
34 *
35 *
36 * DOCUMENTATION
37 *
38 * This thread module exports the following types:
39 * _glthread_TSD Thread-specific data area
40 * _glthread_Thread Thread datatype
41 * _glthread_Mutex Mutual exclusion lock
42 *
43 * Macros:
44 * _glthread_DECLARE_STATIC_MUTEX(name) Declare a non-local mutex
45 * _glthread_INIT_MUTEX(name) Initialize a mutex
46 * _glthread_LOCK_MUTEX(name) Lock a mutex
47 * _glthread_UNLOCK_MUTEX(name) Unlock a mutex
48 *
49 * Functions:
50 * _glthread_GetID(v) Get integer thread ID
51 * _glthread_InitTSD() Initialize thread-specific data
52 * _glthread_GetTSD() Get thread-specific data
53 * _glthread_SetTSD() Set thread-specific data
54 *
55 */
56
57 /*
58 * If this file is accidentally included by a non-threaded build,
59 * it should not cause the build to fail, or otherwise cause problems.
60 * In general, it should only be included when needed however.
61 */
62
63 #ifndef GLTHREAD_H
64 #define GLTHREAD_H
65
66
67 #if defined(PTHREADS) || defined(WIN32_THREADS) || defined(BEOS_THREADS)
68 #ifndef THREADS
69 #define THREADS
70 #endif
71 #endif
72
73
74 /*
75 * POSIX threads. This should be your choice in the Unix world
76 * whenever possible. When building with POSIX threads, be sure
77 * to enable any compiler flags which will cause the MT-safe
78 * libc (if one exists) to be used when linking, as well as any
79 * header macros for MT-safe errno, etc. For Solaris, this is the -mt
80 * compiler flag. On Solaris with gcc, use -D_REENTRANT to enable
81 * proper compiling for MT-safe libc etc.
82 */
83 #if defined(PTHREADS)
84 #include <pthread.h> /* POSIX threads headers */
85
86 typedef struct {
87 pthread_key_t key;
88 int initMagic;
89 } _glthread_TSD;
90
91 typedef pthread_t _glthread_Thread;
92
93 typedef pthread_mutex_t _glthread_Mutex;
94
95 #define _glthread_DECLARE_STATIC_MUTEX(name) \
96 static _glthread_Mutex name = PTHREAD_MUTEX_INITIALIZER
97
98 #define _glthread_INIT_MUTEX(name) \
99 pthread_mutex_init(&(name), NULL)
100
101 #define _glthread_DESTROY_MUTEX(name) \
102 pthread_mutex_destroy(&(name))
103
104 #define _glthread_LOCK_MUTEX(name) \
105 (void) pthread_mutex_lock(&(name))
106
107 #define _glthread_UNLOCK_MUTEX(name) \
108 (void) pthread_mutex_unlock(&(name))
109
110 #endif /* PTHREADS */
111
112
113 /*
114 * Windows threads. Should work with Windows NT and 95.
115 * IMPORTANT: Link with multithreaded runtime library when THREADS are
116 * used!
117 */
118 #ifdef WIN32_THREADS
119 #include <windows.h>
120
121 typedef struct {
122 DWORD key;
123 int initMagic;
124 } _glthread_TSD;
125
126 typedef HANDLE _glthread_Thread;
127
128 typedef CRITICAL_SECTION _glthread_Mutex;
129
130 #define _glthread_DECLARE_STATIC_MUTEX(name) \
131 /* static */ _glthread_Mutex name = { 0, 0, 0, 0, 0, 0 }
132
133 #define _glthread_INIT_MUTEX(name) \
134 InitializeCriticalSection(&name)
135
136 #define _glthread_DESTROY_MUTEX(name) \
137 DeleteCriticalSection(&name)
138
139 #define _glthread_LOCK_MUTEX(name) \
140 EnterCriticalSection(&name)
141
142 #define _glthread_UNLOCK_MUTEX(name) \
143 LeaveCriticalSection(&name)
144
145 #endif /* WIN32_THREADS */
146
147
148 /*
149 * BeOS threads. R5.x required.
150 */
151 #ifdef BEOS_THREADS
152
153 /* Problem with OS.h and this file on haiku */
154 #ifndef __HAIKU__
155 #include <kernel/OS.h>
156 #endif
157
158 #include <support/TLS.h>
159
160 /* The only two typedefs required here
161 * this is cause of the OS.h problem
162 */
163 #ifdef __HAIKU__
164 typedef int32 thread_id;
165 typedef int32 sem_id;
166 #endif
167
168 typedef struct {
169 int32 key;
170 int initMagic;
171 } _glthread_TSD;
172
173 typedef thread_id _glthread_Thread;
174
175 /* Use Benaphore, aka speeder semaphore */
176 typedef struct {
177 int32 lock;
178 sem_id sem;
179 } benaphore;
180 typedef benaphore _glthread_Mutex;
181
182 #define _glthread_DECLARE_STATIC_MUTEX(name) \
183 static _glthread_Mutex name = { 0, 0 }
184
185 #define _glthread_INIT_MUTEX(name) \
186 name.sem = create_sem(0, #name"_benaphore"), \
187 name.lock = 0
188
189 #define _glthread_DESTROY_MUTEX(name) \
190 delete_sem(name.sem), \
191 name.lock = 0
192
193 #define _glthread_LOCK_MUTEX(name) \
194 if (name.sem == 0) \
195 _glthread_INIT_MUTEX(name); \
196 if (atomic_add(&(name.lock), 1) >= 1) \
197 acquire_sem(name.sem)
198
199 #define _glthread_UNLOCK_MUTEX(name) \
200 if (atomic_add(&(name.lock), -1) > 1) \
201 release_sem(name.sem)
202
203 #endif /* BEOS_THREADS */
204
205
206 /*
207 * THREADS not defined
208 */
209 #ifndef THREADS
210
211 typedef unsigned _glthread_TSD;
212
213 typedef unsigned _glthread_Thread;
214
215 typedef unsigned _glthread_Mutex;
216
217 #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
218
219 #define _glthread_INIT_MUTEX(name) (void) name
220
221 #define _glthread_DESTROY_MUTEX(name) (void) name
222
223 #define _glthread_LOCK_MUTEX(name) (void) name
224
225 #define _glthread_UNLOCK_MUTEX(name) (void) name
226
227 #endif /* THREADS */
228
229
230
231 /*
232 * Platform independent thread specific data API.
233 */
234
235 extern unsigned long
236 _glthread_GetID(void);
237
238
239 extern void
240 _glthread_InitTSD(_glthread_TSD *);
241
242
243 extern void
244 _glthread_DestroyTSD(_glthread_TSD *); /* WIN32 only */
245
246
247 extern void *
248 _glthread_GetTSD(_glthread_TSD *);
249
250
251 extern void
252 _glthread_SetTSD(_glthread_TSD *, void *);
253
254
255 #endif /* THREADS_H */