Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Mesa 3-D graphics library |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 4 | * Version: 3.5 |
Jouk Jansen | 5e3bc0c | 2000-11-22 07:32:16 +0000 | [diff] [blame] | 5 | * |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 6 | * Copyright (C) 1999-2001 Brian Paul All Rights Reserved. |
Jouk Jansen | 5e3bc0c | 2000-11-22 07:32:16 +0000 | [diff] [blame] | 7 | * |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 8 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 9 | * copy of this software and associated documentation files (the "Software"), |
| 10 | * to deal in the Software without restriction, including without limitation |
| 11 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 12 | * and/or sell copies of the Software, and to permit persons to whom the |
| 13 | * Software is furnished to do so, subject to the following conditions: |
Jouk Jansen | 5e3bc0c | 2000-11-22 07:32:16 +0000 | [diff] [blame] | 14 | * |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 15 | * The above copyright notice and this permission notice shall be included |
| 16 | * in all copies or substantial portions of the Software. |
Jouk Jansen | 5e3bc0c | 2000-11-22 07:32:16 +0000 | [diff] [blame] | 17 | * |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 19 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 21 | * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN |
| 22 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 24 | */ |
| 25 | |
| 26 | |
| 27 | /* |
| 28 | * Thread support for gl dispatch. |
| 29 | * |
| 30 | * Initial version by John Stone (j.stone@acm.org) (johns@cs.umr.edu) |
| 31 | * and Christoph Poliwoda (poliwoda@volumegraphics.com) |
| 32 | * Revised by Keith Whitwell |
| 33 | * Adapted for new gl dispatcher by Brian Paul |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 34 | * |
| 35 | * |
| 36 | * |
| 37 | * DOCUMENTATION |
| 38 | * |
| 39 | * This thread module exports the following types: |
| 40 | * _glthread_TSD Thread-specific data area |
| 41 | * _glthread_Thread Thread datatype |
| 42 | * _glthread_Mutex Mutual exclusion lock |
| 43 | * |
| 44 | * Macros: |
| 45 | * _glthread_DECLARE_STATIC_MUTEX(name) Declare a non-local mutex |
| 46 | * _glthread_INIT_MUTEX(name) Initialize a mutex |
| 47 | * _glthread_LOCK_MUTEX(name) Lock a mutex |
| 48 | * _glthread_UNLOCK_MUTEX(name) Unlock a mutex |
| 49 | * |
| 50 | * Functions: |
| 51 | * _glthread_GetID(v) Get integer thread ID |
| 52 | * _glthread_InitTSD() Initialize thread-specific data |
| 53 | * _glthread_GetTSD() Get thread-specific data |
| 54 | * _glthread_SetTSD() Set thread-specific data |
| 55 | * |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 56 | */ |
| 57 | |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 58 | /* |
Jouk Jansen | 5e3bc0c | 2000-11-22 07:32:16 +0000 | [diff] [blame] | 59 | * If this file is accidentally included by a non-threaded build, |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 60 | * it should not cause the build to fail, or otherwise cause problems. |
| 61 | * In general, it should only be included when needed however. |
| 62 | */ |
Brian Paul | a360ab2 | 2000-02-10 21:54:06 +0000 | [diff] [blame] | 63 | |
Brian Paul | a360ab2 | 2000-02-10 21:54:06 +0000 | [diff] [blame] | 64 | #ifndef GLTHREAD_H |
| 65 | #define GLTHREAD_H |
| 66 | |
| 67 | |
Philippe Houdoin | b490782 | 2004-08-14 09:48:57 +0000 | [diff] [blame^] | 68 | #if defined(PTHREADS) || defined(SOLARIS_THREADS) || defined(WIN32_THREADS) || \ |
| 69 | defined(XTHREADS) || defined(BEOS_THREADS) |
Brian Paul | a360ab2 | 2000-02-10 21:54:06 +0000 | [diff] [blame] | 70 | #define THREADS |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 71 | #endif |
| 72 | |
Jouk Jansen | 9e83e8c | 2000-11-17 11:00:55 +0000 | [diff] [blame] | 73 | #ifdef VMS |
| 74 | #include <GL/vms_x_fix.h> |
| 75 | #endif |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 76 | |
| 77 | /* |
| 78 | * POSIX threads. This should be your choice in the Unix world |
| 79 | * whenever possible. When building with POSIX threads, be sure |
| 80 | * to enable any compiler flags which will cause the MT-safe |
| 81 | * libc (if one exists) to be used when linking, as well as any |
| 82 | * header macros for MT-safe errno, etc. For Solaris, this is the -mt |
| 83 | * compiler flag. On Solaris with gcc, use -D_REENTRANT to enable |
| 84 | * proper compiling for MT-safe libc etc. |
| 85 | */ |
Brian Paul | a360ab2 | 2000-02-10 21:54:06 +0000 | [diff] [blame] | 86 | #if defined(PTHREADS) |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 87 | #include <pthread.h> /* POSIX threads headers */ |
| 88 | |
| 89 | typedef struct { |
| 90 | pthread_key_t key; |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 91 | int initMagic; |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 92 | } _glthread_TSD; |
| 93 | |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 94 | typedef pthread_t _glthread_Thread; |
| 95 | |
Brian Paul | bc79405 | 2000-01-31 23:10:47 +0000 | [diff] [blame] | 96 | typedef pthread_mutex_t _glthread_Mutex; |
| 97 | |
| 98 | #define _glthread_DECLARE_STATIC_MUTEX(name) \ |
| 99 | static _glthread_Mutex name = PTHREAD_MUTEX_INITIALIZER |
| 100 | |
| 101 | #define _glthread_INIT_MUTEX(name) \ |
| 102 | pthread_mutex_init(&(name), NULL) |
| 103 | |
Keith Whitwell | e15fd85 | 2002-12-12 13:03:15 +0000 | [diff] [blame] | 104 | #define _glthread_DESTROY_MUTEX(name) \ |
| 105 | pthread_mutex_destroy(&(name)) |
| 106 | |
Brian Paul | bc79405 | 2000-01-31 23:10:47 +0000 | [diff] [blame] | 107 | #define _glthread_LOCK_MUTEX(name) \ |
| 108 | (void) pthread_mutex_lock(&(name)) |
| 109 | |
| 110 | #define _glthread_UNLOCK_MUTEX(name) \ |
| 111 | (void) pthread_mutex_unlock(&(name)) |
| 112 | |
Ian Romanick | 8e77da1c | 2004-06-29 19:08:20 +0000 | [diff] [blame] | 113 | /* This is temporarilly removed because driver binaries cannot count on |
| 114 | * the existance of _gl_DispatchTSD in libGL. It only exists in "new" |
| 115 | * libGL. We may be able to ressurect this optimization at some point |
| 116 | * for DRI driver or for software Mesa. |
| 117 | */ |
| 118 | #if 0 |
| 119 | extern struct _glapi_table * _glapi_DispatchTSD; |
| 120 | extern _glthread_TSD _gl_DispatchTSD; |
| 121 | |
| 122 | #define GL_CALL(name) \ |
| 123 | (((__builtin_expect( _glapi_DispatchTSD != NULL, 1 )) \ |
| 124 | ? _glapi_DispatchTSD : (struct _glapi_table *) pthread_getspecific(_gl_DispatchTSD.key))-> name) |
| 125 | #endif |
| 126 | |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 127 | #endif /* PTHREADS */ |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | /* |
Jouk Jansen | 5e3bc0c | 2000-11-22 07:32:16 +0000 | [diff] [blame] | 133 | * Solaris threads. Use only up to Solaris 2.4. |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 134 | * Solaris 2.5 and higher provide POSIX threads. |
Jouk Jansen | 5e3bc0c | 2000-11-22 07:32:16 +0000 | [diff] [blame] | 135 | * Be sure to compile with -mt on the Solaris compilers, or |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 136 | * use -D_REENTRANT if using gcc. |
| 137 | */ |
| 138 | #ifdef SOLARIS_THREADS |
| 139 | #include <thread.h> |
| 140 | |
| 141 | typedef struct { |
| 142 | thread_key_t key; |
| 143 | mutex_t keylock; |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 144 | int initMagic; |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 145 | } _glthread_TSD; |
| 146 | |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 147 | typedef thread_t _glthread_Thread; |
| 148 | |
Brian Paul | bc79405 | 2000-01-31 23:10:47 +0000 | [diff] [blame] | 149 | typedef mutex_t _glthread_Mutex; |
| 150 | |
| 151 | /* XXX need to really implement mutex-related macros */ |
| 152 | #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0 |
| 153 | #define _glthread_INIT_MUTEX(name) (void) name |
Keith Whitwell | e15fd85 | 2002-12-12 13:03:15 +0000 | [diff] [blame] | 154 | #define _glthread_DESTROY_MUTEX(name) (void) name |
Brian Paul | bc79405 | 2000-01-31 23:10:47 +0000 | [diff] [blame] | 155 | #define _glthread_LOCK_MUTEX(name) (void) name |
| 156 | #define _glthread_UNLOCK_MUTEX(name) (void) name |
| 157 | |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 158 | #endif /* SOLARIS_THREADS */ |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | /* |
Jouk Jansen | 5e3bc0c | 2000-11-22 07:32:16 +0000 | [diff] [blame] | 164 | * Windows threads. Should work with Windows NT and 95. |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 165 | * IMPORTANT: Link with multithreaded runtime library when THREADS are |
| 166 | * used! |
| 167 | */ |
Brian Paul | fa937f6 | 2000-02-11 21:38:33 +0000 | [diff] [blame] | 168 | #ifdef WIN32_THREADS |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 169 | #include <windows.h> |
| 170 | |
| 171 | typedef struct { |
| 172 | DWORD key; |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 173 | int initMagic; |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 174 | } _glthread_TSD; |
| 175 | |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 176 | typedef HANDLE _glthread_Thread; |
| 177 | |
Brian Paul | bc79405 | 2000-01-31 23:10:47 +0000 | [diff] [blame] | 178 | typedef CRITICAL_SECTION _glthread_Mutex; |
| 179 | |
| 180 | /* XXX need to really implement mutex-related macros */ |
| 181 | #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0 |
| 182 | #define _glthread_INIT_MUTEX(name) (void) name |
Keith Whitwell | e15fd85 | 2002-12-12 13:03:15 +0000 | [diff] [blame] | 183 | #define _glthread_DESTROY_MUTEX(name) (void) name |
Brian Paul | bc79405 | 2000-01-31 23:10:47 +0000 | [diff] [blame] | 184 | #define _glthread_LOCK_MUTEX(name) (void) name |
| 185 | #define _glthread_UNLOCK_MUTEX(name) (void) name |
| 186 | |
Brian Paul | fa937f6 | 2000-02-11 21:38:33 +0000 | [diff] [blame] | 187 | #endif /* WIN32_THREADS */ |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | /* |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 193 | * XFree86 has its own thread wrapper, Xthreads.h |
| 194 | * We wrap it again for GL. |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 195 | */ |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 196 | #ifdef XTHREADS |
| 197 | #include "Xthreads.h" |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 198 | |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 199 | typedef struct { |
| 200 | xthread_key_t key; |
| 201 | int initMagic; |
| 202 | } _glthread_TSD; |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 203 | |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 204 | typedef xthread_t _glthread_Thread; |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 205 | |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 206 | typedef xmutex_rec _glthread_Mutex; |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 207 | |
Brian Paul | 5104b4d | 2002-03-07 21:50:41 +0000 | [diff] [blame] | 208 | #ifdef XMUTEX_INITIALIZER |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 209 | #define _glthread_DECLARE_STATIC_MUTEX(name) \ |
| 210 | static _glthread_Mutex name = XMUTEX_INITIALIZER |
Brian Paul | 5104b4d | 2002-03-07 21:50:41 +0000 | [diff] [blame] | 211 | #else |
| 212 | #define _glthread_DECLARE_STATIC_MUTEX(name) \ |
| 213 | static _glthread_Mutex name |
| 214 | #endif |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 215 | |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 216 | #define _glthread_INIT_MUTEX(name) \ |
| 217 | xmutex_init(&(name)) |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 218 | |
Keith Whitwell | e15fd85 | 2002-12-12 13:03:15 +0000 | [diff] [blame] | 219 | #define _glthread_DESTROY_MUTEX(name) \ |
| 220 | xmutex_clear(&(name)) |
| 221 | |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 222 | #define _glthread_LOCK_MUTEX(name) \ |
| 223 | (void) xmutex_lock(&(name)) |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 224 | |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 225 | #define _glthread_UNLOCK_MUTEX(name) \ |
| 226 | (void) xmutex_unlock(&(name)) |
| 227 | |
| 228 | #endif /* XTHREADS */ |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 229 | |
| 230 | |
| 231 | |
Brian Paul | 1b37d6c | 2001-11-12 23:50:12 +0000 | [diff] [blame] | 232 | /* |
| 233 | * BeOS threads. R5.x required. |
| 234 | */ |
| 235 | #ifdef BEOS_THREADS |
Philippe Houdoin | b490782 | 2004-08-14 09:48:57 +0000 | [diff] [blame^] | 236 | |
Brian Paul | 1b37d6c | 2001-11-12 23:50:12 +0000 | [diff] [blame] | 237 | #include <kernel/OS.h> |
| 238 | #include <support/TLS.h> |
| 239 | |
| 240 | typedef struct { |
| 241 | int32 key; |
| 242 | int initMagic; |
| 243 | } _glthread_TSD; |
| 244 | |
| 245 | typedef thread_id _glthread_Thread; |
| 246 | |
| 247 | /* Use Benaphore, aka speeder semaphore */ |
| 248 | typedef struct { |
| 249 | int32 lock; |
| 250 | sem_id sem; |
| 251 | } benaphore; |
| 252 | typedef benaphore _glthread_Mutex; |
| 253 | |
Philippe Houdoin | b490782 | 2004-08-14 09:48:57 +0000 | [diff] [blame^] | 254 | #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = { 0, 0 } |
| 255 | #define _glthread_INIT_MUTEX(name) name.sem = create_sem(0, #name"_benaphore"), name.lock = 0 |
| 256 | #define _glthread_DESTROY_MUTEX(name) delete_sem(name.sem), name.lock = 0 |
| 257 | #define _glthread_LOCK_MUTEX(name) if (name.sem == 0) _glthread_INIT_MUTEX(name); \ |
| 258 | if (atomic_add(&(name.lock), 1) >= 1) acquire_sem(name.sem) |
| 259 | #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] | 260 | |
| 261 | #endif /* BEOS_THREADS */ |
| 262 | |
| 263 | |
Brian Paul | bc79405 | 2000-01-31 23:10:47 +0000 | [diff] [blame] | 264 | |
Brian Paul | a360ab2 | 2000-02-10 21:54:06 +0000 | [diff] [blame] | 265 | #ifndef THREADS |
Brian Paul | bc79405 | 2000-01-31 23:10:47 +0000 | [diff] [blame] | 266 | |
| 267 | /* |
| 268 | * THREADS not defined |
| 269 | */ |
| 270 | |
| 271 | typedef GLuint _glthread_TSD; |
| 272 | |
| 273 | typedef GLuint _glthread_Thread; |
| 274 | |
| 275 | typedef GLuint _glthread_Mutex; |
| 276 | |
| 277 | #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0 |
| 278 | |
| 279 | #define _glthread_INIT_MUTEX(name) (void) name |
| 280 | |
Keith Whitwell | e15fd85 | 2002-12-12 13:03:15 +0000 | [diff] [blame] | 281 | #define _glthread_DESTROY_MUTEX(name) (void) name |
| 282 | |
Brian Paul | bc79405 | 2000-01-31 23:10:47 +0000 | [diff] [blame] | 283 | #define _glthread_LOCK_MUTEX(name) (void) name |
| 284 | |
| 285 | #define _glthread_UNLOCK_MUTEX(name) (void) name |
| 286 | |
Brian Paul | bc79405 | 2000-01-31 23:10:47 +0000 | [diff] [blame] | 287 | #endif /* THREADS */ |
| 288 | |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 289 | |
| 290 | |
| 291 | /* |
Jouk Jansen | 5e3bc0c | 2000-11-22 07:32:16 +0000 | [diff] [blame] | 292 | * Platform independent thread specific data API. |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 293 | */ |
| 294 | |
| 295 | extern unsigned long |
| 296 | _glthread_GetID(void); |
| 297 | |
| 298 | |
| 299 | extern void |
| 300 | _glthread_InitTSD(_glthread_TSD *); |
| 301 | |
| 302 | |
| 303 | extern void * |
| 304 | _glthread_GetTSD(_glthread_TSD *); |
| 305 | |
| 306 | |
| 307 | extern void |
| 308 | _glthread_SetTSD(_glthread_TSD *, void *); |
| 309 | |
Ian Romanick | c1d455f | 2004-05-27 00:03:53 +0000 | [diff] [blame] | 310 | #ifndef GL_CALL |
Ian Romanick | 8e77da1c | 2004-06-29 19:08:20 +0000 | [diff] [blame] | 311 | # if defined(THREADS) |
| 312 | extern struct _glapi_table * _glapi_DispatchTSD; |
| 313 | # define GL_CALL(name) \ |
| 314 | (((__builtin_expect( _glapi_DispatchTSD != NULL, 1 )) \ |
| 315 | ? _glapi_DispatchTSD : _glapi_get_dispatch())-> name) |
| 316 | # else |
| 317 | # define GL_CALL(name) (*(_glapi_Dispatch-> name)) |
| 318 | # endif /* defined(THREADS) */ |
| 319 | #endif /* ndef GL_CALL */ |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 320 | |
| 321 | |
Brian Paul | bc79405 | 2000-01-31 23:10:47 +0000 | [diff] [blame] | 322 | #endif /* THREADS_H */ |