WIN32_THREADS -> WIN32
[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) || 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
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 /* http://locklessinc.com/articles/pthreads_on_windows/ */
99 #define u_mutex_declare_static(name) \
100 /* static */ u_mutex name = {(void*)-1, -1, 0, 0, 0, 0}
101
102 #define u_mutex_init(name) InitializeCriticalSection(&name)
103 #define u_mutex_destroy(name) DeleteCriticalSection(&name)
104 #define u_mutex_lock(name) EnterCriticalSection(&name)
105 #define u_mutex_unlock(name) LeaveCriticalSection(&name)
106
107 #endif /* WIN32 */
108
109
110 /*
111 * BeOS threads. R5.x required.
112 */
113 #ifdef BEOS_THREADS
114
115 /* Problem with OS.h and this file on haiku */
116 #ifndef __HAIKU__
117 #include <kernel/OS.h>
118 #endif
119
120 #include <support/TLS.h>
121
122 /* The only two typedefs required here
123 * this is cause of the OS.h problem
124 */
125 #ifdef __HAIKU__
126 typedef int32 thread_id;
127 typedef int32 sem_id;
128 #endif
129
130 struct u_tsd {
131 int32 key;
132 int initMagic;
133 };
134
135 /* Use Benaphore, aka speeder semaphore */
136 typedef struct {
137 int32 lock;
138 sem_id sem;
139 } benaphore;
140 typedef benaphore u_mutex;
141
142 #define u_mutex_declare_static(name) \
143 static u_mutex name = { 0, 0 }
144
145 #define u_mutex_init(name) \
146 name.sem = create_sem(0, #name"_benaphore"), \
147 name.lock = 0
148
149 #define u_mutex_destroy(name) \
150 delete_sem(name.sem), \
151 name.lock = 0
152
153 #define u_mutex_lock(name) \
154 if (name.sem == 0) \
155 u_mutex_init(name); \
156 if (atomic_add(&(name.lock), 1) >= 1) \
157 acquire_sem(name.sem)
158
159 #define u_mutex_unlock(name) \
160 if (atomic_add(&(name.lock), -1) > 1) \
161 release_sem(name.sem)
162
163 #endif /* BEOS_THREADS */
164
165
166 /*
167 * THREADS not defined
168 */
169 #ifndef THREADS
170
171 struct u_tsd {
172 int initMagic;
173 };
174
175 typedef unsigned u_mutex;
176
177 #define u_mutex_declare_static(name) static u_mutex name = 0
178 #define u_mutex_init(name) (void) name
179 #define u_mutex_destroy(name) (void) name
180 #define u_mutex_lock(name) (void) name
181 #define u_mutex_unlock(name) (void) name
182
183 #endif /* THREADS */
184
185
186 unsigned long
187 u_thread_self(void);
188
189 void
190 u_tsd_init(struct u_tsd *tsd);
191
192 void
193 u_tsd_destroy(struct u_tsd *tsd); /* WIN32 only */
194
195 void *
196 u_tsd_get(struct u_tsd *tsd);
197
198 void
199 u_tsd_set(struct u_tsd *tsd, void *ptr);
200
201 #endif /* _U_THREAD_H_ */