2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
27 * Thread support for gl dispatch.
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
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.
45 #include "u_compiler.h"
47 #if defined(PTHREADS) || defined(WIN32_THREADS) || defined(BEOS_THREADS)
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.
63 #include <pthread.h> /* POSIX threads headers */
70 typedef pthread_mutex_t u_mutex
;
72 #define u_mutex_declare_static(name) \
73 static u_mutex name = PTHREAD_MUTEX_INITIALIZER
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))
84 * Windows threads. Should work with Windows NT and 95.
85 * IMPORTANT: Link with multithreaded runtime library when THREADS are
96 typedef CRITICAL_SECTION u_mutex
;
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}
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)
107 #endif /* WIN32_THREADS */
111 * BeOS threads. R5.x required.
115 /* Problem with OS.h and this file on haiku */
117 #include <kernel/OS.h>
120 #include <support/TLS.h>
122 /* The only two typedefs required here
123 * this is cause of the OS.h problem
126 typedef int32 thread_id
;
127 typedef int32 sem_id
;
135 /* Use Benaphore, aka speeder semaphore */
140 typedef benaphore u_mutex
;
142 #define u_mutex_declare_static(name) \
143 static u_mutex name = { 0, 0 }
145 #define u_mutex_init(name) \
146 name.sem = create_sem(0, #name"_benaphore"), \
149 #define u_mutex_destroy(name) \
150 delete_sem(name.sem), \
153 #define u_mutex_lock(name) \
155 u_mutex_init(name); \
156 if (atomic_add(&(name.lock), 1) >= 1) \
157 acquire_sem(name.sem)
159 #define u_mutex_unlock(name) \
160 if (atomic_add(&(name.lock), -1) > 1) \
161 release_sem(name.sem)
163 #endif /* BEOS_THREADS */
167 * THREADS not defined
175 typedef unsigned u_mutex
;
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
190 u_tsd_init(struct u_tsd
*tsd
);
193 u_tsd_destroy(struct u_tsd
*tsd
); /* WIN32 only */
196 u_tsd_get(struct u_tsd
*tsd
);
199 u_tsd_set(struct u_tsd
*tsd
, void *ptr
);
201 #endif /* _U_THREAD_H_ */