Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Mesa 3-D graphics library |
Brian Paul | 263317d | 2004-11-22 19:11:01 +0000 | [diff] [blame] | 3 | * Version: 6.3 |
Jouk Jansen | 5e3bc0c | 2000-11-22 07:32:16 +0000 | [diff] [blame] | 4 | * |
Brian Paul | 263317d | 2004-11-22 19:11:01 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2003 Brian Paul All Rights Reserved. |
Jouk Jansen | 5e3bc0c | 2000-11-22 07:32:16 +0000 | [diff] [blame] | 6 | * |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 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: |
Jouk Jansen | 5e3bc0c | 2000-11-22 07:32:16 +0000 | [diff] [blame] | 13 | * |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 14 | * The above copyright notice and this permission notice shall be included |
| 15 | * in all copies or substantial portions of the Software. |
Jouk Jansen | 5e3bc0c | 2000-11-22 07:32:16 +0000 | [diff] [blame] | 16 | * |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 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 |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 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 | * |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 55 | */ |
| 56 | |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 57 | /* |
Jouk Jansen | 5e3bc0c | 2000-11-22 07:32:16 +0000 | [diff] [blame] | 58 | * If this file is accidentally included by a non-threaded build, |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 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 | */ |
Brian Paul | a360ab2 | 2000-02-10 21:54:06 +0000 | [diff] [blame] | 62 | |
Brian Paul | a360ab2 | 2000-02-10 21:54:06 +0000 | [diff] [blame] | 63 | #ifndef GLTHREAD_H |
| 64 | #define GLTHREAD_H |
| 65 | |
| 66 | |
Philippe Houdoin | b490782 | 2004-08-14 09:48:57 +0000 | [diff] [blame] | 67 | #if defined(PTHREADS) || defined(SOLARIS_THREADS) || defined(WIN32_THREADS) || \ |
| 68 | defined(XTHREADS) || defined(BEOS_THREADS) |
Brian Paul | a360ab2 | 2000-02-10 21:54:06 +0000 | [diff] [blame] | 69 | #define THREADS |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 70 | #endif |
| 71 | |
Jouk Jansen | 9e83e8c | 2000-11-17 11:00:55 +0000 | [diff] [blame] | 72 | #ifdef VMS |
| 73 | #include <GL/vms_x_fix.h> |
| 74 | #endif |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 75 | |
| 76 | /* |
| 77 | * POSIX threads. This should be your choice in the Unix world |
| 78 | * whenever possible. When building with POSIX threads, be sure |
| 79 | * to enable any compiler flags which will cause the MT-safe |
| 80 | * libc (if one exists) to be used when linking, as well as any |
| 81 | * header macros for MT-safe errno, etc. For Solaris, this is the -mt |
| 82 | * compiler flag. On Solaris with gcc, use -D_REENTRANT to enable |
| 83 | * proper compiling for MT-safe libc etc. |
| 84 | */ |
Brian Paul | a360ab2 | 2000-02-10 21:54:06 +0000 | [diff] [blame] | 85 | #if defined(PTHREADS) |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 86 | #include <pthread.h> /* POSIX threads headers */ |
| 87 | |
| 88 | typedef struct { |
| 89 | pthread_key_t key; |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 90 | int initMagic; |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 91 | } _glthread_TSD; |
| 92 | |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 93 | typedef pthread_t _glthread_Thread; |
| 94 | |
Brian Paul | bc79405 | 2000-01-31 23:10:47 +0000 | [diff] [blame] | 95 | typedef pthread_mutex_t _glthread_Mutex; |
| 96 | |
| 97 | #define _glthread_DECLARE_STATIC_MUTEX(name) \ |
| 98 | static _glthread_Mutex name = PTHREAD_MUTEX_INITIALIZER |
| 99 | |
| 100 | #define _glthread_INIT_MUTEX(name) \ |
| 101 | pthread_mutex_init(&(name), NULL) |
| 102 | |
Keith Whitwell | e15fd85 | 2002-12-12 13:03:15 +0000 | [diff] [blame] | 103 | #define _glthread_DESTROY_MUTEX(name) \ |
| 104 | pthread_mutex_destroy(&(name)) |
| 105 | |
Brian Paul | bc79405 | 2000-01-31 23:10:47 +0000 | [diff] [blame] | 106 | #define _glthread_LOCK_MUTEX(name) \ |
| 107 | (void) pthread_mutex_lock(&(name)) |
| 108 | |
| 109 | #define _glthread_UNLOCK_MUTEX(name) \ |
| 110 | (void) pthread_mutex_unlock(&(name)) |
| 111 | |
Ian Romanick | 8e77da1c | 2004-06-29 19:08:20 +0000 | [diff] [blame] | 112 | /* This is temporarilly removed because driver binaries cannot count on |
| 113 | * the existance of _gl_DispatchTSD in libGL. It only exists in "new" |
| 114 | * libGL. We may be able to ressurect this optimization at some point |
| 115 | * for DRI driver or for software Mesa. |
| 116 | */ |
| 117 | #if 0 |
| 118 | extern struct _glapi_table * _glapi_DispatchTSD; |
| 119 | extern _glthread_TSD _gl_DispatchTSD; |
| 120 | |
| 121 | #define GL_CALL(name) \ |
| 122 | (((__builtin_expect( _glapi_DispatchTSD != NULL, 1 )) \ |
| 123 | ? _glapi_DispatchTSD : (struct _glapi_table *) pthread_getspecific(_gl_DispatchTSD.key))-> name) |
| 124 | #endif |
| 125 | |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 126 | #endif /* PTHREADS */ |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | /* |
Jouk Jansen | 5e3bc0c | 2000-11-22 07:32:16 +0000 | [diff] [blame] | 132 | * Solaris threads. Use only up to Solaris 2.4. |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 133 | * Solaris 2.5 and higher provide POSIX threads. |
Jouk Jansen | 5e3bc0c | 2000-11-22 07:32:16 +0000 | [diff] [blame] | 134 | * Be sure to compile with -mt on the Solaris compilers, or |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 135 | * use -D_REENTRANT if using gcc. |
| 136 | */ |
| 137 | #ifdef SOLARIS_THREADS |
| 138 | #include <thread.h> |
| 139 | |
| 140 | typedef struct { |
| 141 | thread_key_t key; |
| 142 | mutex_t keylock; |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 143 | int initMagic; |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 144 | } _glthread_TSD; |
| 145 | |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 146 | typedef thread_t _glthread_Thread; |
| 147 | |
Brian Paul | bc79405 | 2000-01-31 23:10:47 +0000 | [diff] [blame] | 148 | typedef mutex_t _glthread_Mutex; |
| 149 | |
| 150 | /* XXX need to really implement mutex-related macros */ |
| 151 | #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0 |
| 152 | #define _glthread_INIT_MUTEX(name) (void) name |
Keith Whitwell | e15fd85 | 2002-12-12 13:03:15 +0000 | [diff] [blame] | 153 | #define _glthread_DESTROY_MUTEX(name) (void) name |
Brian Paul | bc79405 | 2000-01-31 23:10:47 +0000 | [diff] [blame] | 154 | #define _glthread_LOCK_MUTEX(name) (void) name |
| 155 | #define _glthread_UNLOCK_MUTEX(name) (void) name |
| 156 | |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 157 | #endif /* SOLARIS_THREADS */ |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | /* |
Jouk Jansen | 5e3bc0c | 2000-11-22 07:32:16 +0000 | [diff] [blame] | 163 | * Windows threads. Should work with Windows NT and 95. |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 164 | * IMPORTANT: Link with multithreaded runtime library when THREADS are |
| 165 | * used! |
| 166 | */ |
Brian Paul | fa937f6 | 2000-02-11 21:38:33 +0000 | [diff] [blame] | 167 | #ifdef WIN32_THREADS |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 168 | #include <windows.h> |
| 169 | |
| 170 | typedef struct { |
| 171 | DWORD key; |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 172 | int initMagic; |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 173 | } _glthread_TSD; |
| 174 | |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 175 | typedef HANDLE _glthread_Thread; |
| 176 | |
Brian Paul | bc79405 | 2000-01-31 23:10:47 +0000 | [diff] [blame] | 177 | typedef CRITICAL_SECTION _glthread_Mutex; |
| 178 | |
| 179 | /* XXX need to really implement mutex-related macros */ |
| 180 | #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0 |
| 181 | #define _glthread_INIT_MUTEX(name) (void) name |
Keith Whitwell | e15fd85 | 2002-12-12 13:03:15 +0000 | [diff] [blame] | 182 | #define _glthread_DESTROY_MUTEX(name) (void) name |
Brian Paul | bc79405 | 2000-01-31 23:10:47 +0000 | [diff] [blame] | 183 | #define _glthread_LOCK_MUTEX(name) (void) name |
| 184 | #define _glthread_UNLOCK_MUTEX(name) (void) name |
| 185 | |
Brian Paul | fa937f6 | 2000-02-11 21:38:33 +0000 | [diff] [blame] | 186 | #endif /* WIN32_THREADS */ |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | /* |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 192 | * XFree86 has its own thread wrapper, Xthreads.h |
| 193 | * We wrap it again for GL. |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 194 | */ |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 195 | #ifdef XTHREADS |
Brian Paul | 263317d | 2004-11-22 19:11:01 +0000 | [diff] [blame] | 196 | #include <X11/Xthreads.h> |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 197 | |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 198 | typedef struct { |
| 199 | xthread_key_t key; |
| 200 | int initMagic; |
| 201 | } _glthread_TSD; |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 202 | |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 203 | typedef xthread_t _glthread_Thread; |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 204 | |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 205 | typedef xmutex_rec _glthread_Mutex; |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 206 | |
Brian Paul | 5104b4d | 2002-03-07 21:50:41 +0000 | [diff] [blame] | 207 | #ifdef XMUTEX_INITIALIZER |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 208 | #define _glthread_DECLARE_STATIC_MUTEX(name) \ |
| 209 | static _glthread_Mutex name = XMUTEX_INITIALIZER |
Brian Paul | 5104b4d | 2002-03-07 21:50:41 +0000 | [diff] [blame] | 210 | #else |
| 211 | #define _glthread_DECLARE_STATIC_MUTEX(name) \ |
| 212 | static _glthread_Mutex name |
| 213 | #endif |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 214 | |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 215 | #define _glthread_INIT_MUTEX(name) \ |
| 216 | xmutex_init(&(name)) |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 217 | |
Keith Whitwell | e15fd85 | 2002-12-12 13:03:15 +0000 | [diff] [blame] | 218 | #define _glthread_DESTROY_MUTEX(name) \ |
| 219 | xmutex_clear(&(name)) |
| 220 | |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 221 | #define _glthread_LOCK_MUTEX(name) \ |
| 222 | (void) xmutex_lock(&(name)) |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 223 | |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 224 | #define _glthread_UNLOCK_MUTEX(name) \ |
| 225 | (void) xmutex_unlock(&(name)) |
| 226 | |
| 227 | #endif /* XTHREADS */ |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 228 | |
| 229 | |
| 230 | |
Brian Paul | 1b37d6c | 2001-11-12 23:50:12 +0000 | [diff] [blame] | 231 | /* |
| 232 | * BeOS threads. R5.x required. |
| 233 | */ |
| 234 | #ifdef BEOS_THREADS |
Philippe Houdoin | b490782 | 2004-08-14 09:48:57 +0000 | [diff] [blame] | 235 | |
Brian Paul | 1b37d6c | 2001-11-12 23:50:12 +0000 | [diff] [blame] | 236 | #include <kernel/OS.h> |
| 237 | #include <support/TLS.h> |
| 238 | |
| 239 | typedef struct { |
| 240 | int32 key; |
| 241 | int initMagic; |
| 242 | } _glthread_TSD; |
| 243 | |
| 244 | typedef thread_id _glthread_Thread; |
| 245 | |
| 246 | /* Use Benaphore, aka speeder semaphore */ |
| 247 | typedef struct { |
| 248 | int32 lock; |
| 249 | sem_id sem; |
| 250 | } benaphore; |
| 251 | typedef benaphore _glthread_Mutex; |
| 252 | |
Philippe Houdoin | b490782 | 2004-08-14 09:48:57 +0000 | [diff] [blame] | 253 | #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = { 0, 0 } |
| 254 | #define _glthread_INIT_MUTEX(name) name.sem = create_sem(0, #name"_benaphore"), name.lock = 0 |
| 255 | #define _glthread_DESTROY_MUTEX(name) delete_sem(name.sem), name.lock = 0 |
| 256 | #define _glthread_LOCK_MUTEX(name) if (name.sem == 0) _glthread_INIT_MUTEX(name); \ |
| 257 | if (atomic_add(&(name.lock), 1) >= 1) acquire_sem(name.sem) |
| 258 | #define _glthread_UNLOCK_MUTEX(name) if (atomic_add(&(name.lock), -1) > 1) release_sem(name.sem) |
Brian Paul | 1b37d6c | 2001-11-12 23:50:12 +0000 | [diff] [blame] | 259 | |
| 260 | #endif /* BEOS_THREADS */ |
| 261 | |
| 262 | |
Brian Paul | bc79405 | 2000-01-31 23:10:47 +0000 | [diff] [blame] | 263 | |
Brian Paul | a360ab2 | 2000-02-10 21:54:06 +0000 | [diff] [blame] | 264 | #ifndef THREADS |
Brian Paul | bc79405 | 2000-01-31 23:10:47 +0000 | [diff] [blame] | 265 | |
| 266 | /* |
| 267 | * THREADS not defined |
| 268 | */ |
| 269 | |
| 270 | typedef GLuint _glthread_TSD; |
| 271 | |
| 272 | typedef GLuint _glthread_Thread; |
| 273 | |
| 274 | typedef GLuint _glthread_Mutex; |
| 275 | |
| 276 | #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0 |
| 277 | |
| 278 | #define _glthread_INIT_MUTEX(name) (void) name |
| 279 | |
Keith Whitwell | e15fd85 | 2002-12-12 13:03:15 +0000 | [diff] [blame] | 280 | #define _glthread_DESTROY_MUTEX(name) (void) name |
| 281 | |
Brian Paul | bc79405 | 2000-01-31 23:10:47 +0000 | [diff] [blame] | 282 | #define _glthread_LOCK_MUTEX(name) (void) name |
| 283 | |
| 284 | #define _glthread_UNLOCK_MUTEX(name) (void) name |
| 285 | |
Brian Paul | bc79405 | 2000-01-31 23:10:47 +0000 | [diff] [blame] | 286 | #endif /* THREADS */ |
| 287 | |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 288 | |
| 289 | |
| 290 | /* |
Jouk Jansen | 5e3bc0c | 2000-11-22 07:32:16 +0000 | [diff] [blame] | 291 | * Platform independent thread specific data API. |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 292 | */ |
| 293 | |
| 294 | extern unsigned long |
| 295 | _glthread_GetID(void); |
| 296 | |
| 297 | |
| 298 | extern void |
| 299 | _glthread_InitTSD(_glthread_TSD *); |
| 300 | |
| 301 | |
| 302 | extern void * |
| 303 | _glthread_GetTSD(_glthread_TSD *); |
| 304 | |
| 305 | |
| 306 | extern void |
| 307 | _glthread_SetTSD(_glthread_TSD *, void *); |
| 308 | |
Ian Romanick | 25fe93f | 2005-04-13 20:59:15 +0000 | [diff] [blame] | 309 | #if defined(GLX_USE_TLS) |
| 310 | |
| 311 | extern __thread struct _glapi_table * _glapi_tls_Dispatch |
| 312 | __attribute__((tls_model("initial-exec"))); |
| 313 | |
| 314 | # define GL_CALL(name) (*(_glapi_tls_Dispatch-> name)) |
| 315 | |
| 316 | #elif !defined(GL_CALL) |
Ian Romanick | 8e77da1c | 2004-06-29 19:08:20 +0000 | [diff] [blame] | 317 | # if defined(THREADS) |
| 318 | extern struct _glapi_table * _glapi_DispatchTSD; |
| 319 | # define GL_CALL(name) \ |
| 320 | (((__builtin_expect( _glapi_DispatchTSD != NULL, 1 )) \ |
| 321 | ? _glapi_DispatchTSD : _glapi_get_dispatch())-> name) |
| 322 | # else |
| 323 | # define GL_CALL(name) (*(_glapi_Dispatch-> name)) |
| 324 | # endif /* defined(THREADS) */ |
| 325 | #endif /* ndef GL_CALL */ |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 326 | |
| 327 | |
Brian Paul | bc79405 | 2000-01-31 23:10:47 +0000 | [diff] [blame] | 328 | #endif /* THREADS_H */ |