Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Mesa 3-D graphics library |
Brian Paul | 3c63452 | 2002-10-24 23:57:19 +0000 | [diff] [blame] | 4 | * Version: 4.1 |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 5 | * |
Brian Paul | 3c63452 | 2002-10-24 23:57:19 +0000 | [diff] [blame] | 6 | * Copyright (C) 1999-2002 Brian Paul All Rights Reserved. |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +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: |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +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. |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +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 | /* |
Brian Paul | 8d365ab | 2000-01-28 18:57:56 +0000 | [diff] [blame] | 28 | * XXX There's probably some work to do in order to make this file |
| 29 | * truly reusable outside of Mesa. First, the glheader.h include must go. |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 30 | */ |
| 31 | |
| 32 | |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 33 | #include "glheader.h" |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 34 | #include "glthread.h" |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 35 | |
| 36 | |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 37 | /* |
| 38 | * This file should still compile even when THREADS is not defined. |
| 39 | * This is to make things easier to deal with on the makefile scene.. |
| 40 | */ |
| 41 | #ifdef THREADS |
| 42 | #include <errno.h> |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 43 | |
| 44 | /* |
| 45 | * Error messages |
| 46 | */ |
| 47 | #define INIT_TSD_ERROR "_glthread_: failed to allocate key for thread specific data" |
| 48 | #define GET_TSD_ERROR "_glthread_: failed to get thread specific data" |
| 49 | #define SET_TSD_ERROR "_glthread_: thread failed to set thread specific data" |
| 50 | |
| 51 | |
| 52 | /* |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 53 | * Magic number to determine if a TSD object has been initialized. |
| 54 | * Kind of a hack but there doesn't appear to be a better cross-platform |
| 55 | * solution. |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 56 | */ |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 57 | #define INIT_MAGIC 0xff8adc98 |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 58 | |
| 59 | |
| 60 | |
| 61 | /* |
| 62 | * POSIX Threads -- The best way to go if your platform supports them. |
| 63 | * Solaris >= 2.5 have POSIX threads, IRIX >= 6.4 reportedly |
| 64 | * has them, and many of the free Unixes now have them. |
| 65 | * Be sure to use appropriate -mt or -D_REENTRANT type |
| 66 | * compile flags when building. |
| 67 | */ |
| 68 | #ifdef PTHREADS |
| 69 | |
| 70 | unsigned long |
| 71 | _glthread_GetID(void) |
| 72 | { |
| 73 | return (unsigned long) pthread_self(); |
| 74 | } |
| 75 | |
| 76 | |
| 77 | void |
| 78 | _glthread_InitTSD(_glthread_TSD *tsd) |
| 79 | { |
Brian Paul | e2b10e7 | 1999-12-17 11:13:54 +0000 | [diff] [blame] | 80 | if (pthread_key_create(&tsd->key, NULL/*free*/) != 0) { |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 81 | perror(INIT_TSD_ERROR); |
| 82 | exit(-1); |
| 83 | } |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 84 | tsd->initMagic = INIT_MAGIC; |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | |
| 88 | void * |
| 89 | _glthread_GetTSD(_glthread_TSD *tsd) |
| 90 | { |
Brian Paul | b51b0a8 | 2001-03-07 05:06:11 +0000 | [diff] [blame] | 91 | if (tsd->initMagic != (int) INIT_MAGIC) { |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 92 | _glthread_InitTSD(tsd); |
| 93 | } |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 94 | return pthread_getspecific(tsd->key); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | void |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 99 | _glthread_SetTSD(_glthread_TSD *tsd, void *ptr) |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 100 | { |
Brian Paul | b51b0a8 | 2001-03-07 05:06:11 +0000 | [diff] [blame] | 101 | if (tsd->initMagic != (int) INIT_MAGIC) { |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 102 | _glthread_InitTSD(tsd); |
| 103 | } |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 104 | if (pthread_setspecific(tsd->key, ptr) != 0) { |
| 105 | perror(SET_TSD_ERROR); |
| 106 | exit(-1); |
Brian Paul | e2b10e7 | 1999-12-17 11:13:54 +0000 | [diff] [blame] | 107 | } |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | #endif /* PTHREADS */ |
| 111 | |
| 112 | |
| 113 | |
| 114 | /* |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 115 | * Solaris/Unix International Threads -- Use only if POSIX threads |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 116 | * aren't available on your Unix platform. Solaris 2.[34] are examples |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 117 | * of platforms where this is the case. Be sure to use -mt and/or |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 118 | * -D_REENTRANT when compiling. |
| 119 | */ |
| 120 | #ifdef SOLARIS_THREADS |
| 121 | #define USE_LOCK_FOR_KEY /* undef this to try a version without |
| 122 | lock for the global key... */ |
| 123 | |
| 124 | unsigned long |
| 125 | _glthread_GetID(void) |
| 126 | { |
| 127 | abort(); /* XXX not implemented yet */ |
| 128 | return (unsigned long) 0; |
| 129 | } |
| 130 | |
| 131 | |
| 132 | void |
| 133 | _glthread_InitTSD(_glthread_TSD *tsd) |
| 134 | { |
| 135 | if ((errno = mutex_init(&tsd->keylock, 0, NULL)) != 0 || |
| 136 | (errno = thr_keycreate(&(tsd->key), free)) != 0) { |
| 137 | perror(INIT_TSD_ERROR); |
| 138 | exit(-1); |
| 139 | } |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 140 | tsd->initMagic = INIT_MAGIC; |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | |
| 144 | void * |
| 145 | _glthread_GetTSD(_glthread_TSD *tsd) |
| 146 | { |
| 147 | void* ret; |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 148 | if (tsd->initMagic != INIT_MAGIC) { |
| 149 | _glthread_InitTSD(tsd); |
| 150 | } |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 151 | #ifdef USE_LOCK_FOR_KEY |
| 152 | mutex_lock(&tsd->keylock); |
| 153 | thr_getspecific(tsd->key, &ret); |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 154 | mutex_unlock(&tsd->keylock); |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 155 | #else |
| 156 | if ((errno = thr_getspecific(tsd->key, &ret)) != 0) { |
| 157 | perror(GET_TSD_ERROR); |
| 158 | exit(-1); |
Brian Paul | e2b10e7 | 1999-12-17 11:13:54 +0000 | [diff] [blame] | 159 | } |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 160 | #endif |
| 161 | return ret; |
| 162 | } |
| 163 | |
| 164 | |
| 165 | void |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 166 | _glthread_SetTSD(_glthread_TSD *tsd, void *ptr) |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 167 | { |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 168 | if (tsd->initMagic != INIT_MAGIC) { |
| 169 | _glthread_InitTSD(tsd); |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 170 | } |
| 171 | if ((errno = thr_setspecific(tsd->key, ptr)) != 0) { |
| 172 | perror(SET_TSD_ERROR); |
| 173 | exit(-1); |
Brian Paul | e2b10e7 | 1999-12-17 11:13:54 +0000 | [diff] [blame] | 174 | } |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | #undef USE_LOCK_FOR_KEY |
| 178 | #endif /* SOLARIS_THREADS */ |
| 179 | |
| 180 | |
| 181 | |
| 182 | /* |
| 183 | * Win32 Threads. The only available option for Windows 95/NT. |
| 184 | * Be sure that you compile using the Multithreaded runtime, otherwise |
| 185 | * bad things will happen. |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 186 | */ |
Brian Paul | fa937f6 | 2000-02-11 21:38:33 +0000 | [diff] [blame] | 187 | #ifdef WIN32_THREADS |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 188 | |
| 189 | unsigned long |
| 190 | _glthread_GetID(void) |
| 191 | { |
| 192 | abort(); /* XXX not implemented yet */ |
| 193 | return (unsigned long) 0; |
| 194 | } |
| 195 | |
| 196 | |
| 197 | void |
| 198 | _glthread_InitTSD(_glthread_TSD *tsd) |
| 199 | { |
| 200 | tsd->key = TlsAlloc(); |
| 201 | if (tsd->key == 0xffffffff) { |
| 202 | /* Can Windows handle stderr messages for non-console |
| 203 | applications? Does Windows have perror? */ |
| 204 | /* perror(SET_INIT_ERROR);*/ |
| 205 | exit(-1); |
| 206 | } |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 207 | tsd->initMagic = INIT_MAGIC; |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | |
| 211 | void * |
| 212 | _glthread_GetTSD(_glthread_TSD *tsd) |
| 213 | { |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 214 | if (tsd->initMagic != INIT_MAGIC) { |
| 215 | _glthread_InitTSD(tsd); |
| 216 | } |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 217 | return TlsGetValue(tsd->key); |
| 218 | } |
| 219 | |
| 220 | |
| 221 | void |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 222 | _glthread_SetTSD(_glthread_TSD *tsd, void *ptr) |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 223 | { |
| 224 | /* the following code assumes that the _glthread_TSD has been initialized |
| 225 | to zero at creation */ |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 226 | if (tsd->initMagic != INIT_MAGIC) { |
| 227 | _glthread_InitTSD(tsd); |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 228 | } |
| 229 | if (TlsSetValue(tsd->key, ptr) == 0) { |
| 230 | /* Can Windows handle stderr messages for non-console |
| 231 | applications? Does Windows have perror? */ |
| 232 | /* perror(SET_TSD_ERROR);*/ |
| 233 | exit(-1); |
Brian Paul | e2b10e7 | 1999-12-17 11:13:54 +0000 | [diff] [blame] | 234 | } |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Brian Paul | fa937f6 | 2000-02-11 21:38:33 +0000 | [diff] [blame] | 237 | #endif /* WIN32_THREADS */ |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 238 | |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 239 | |
| 240 | |
| 241 | /* |
| 242 | * XFree86 has its own thread wrapper, Xthreads.h |
| 243 | * We wrap it again for GL. |
| 244 | */ |
| 245 | #ifdef XTHREADS |
| 246 | |
| 247 | unsigned long |
| 248 | _glthread_GetID(void) |
| 249 | { |
| 250 | return (unsigned long) xthread_self(); |
| 251 | } |
| 252 | |
| 253 | |
| 254 | void |
| 255 | _glthread_InitTSD(_glthread_TSD *tsd) |
| 256 | { |
| 257 | if (xthread_key_create(&tsd->key, NULL) != 0) { |
| 258 | perror(INIT_TSD_ERROR); |
| 259 | exit(-1); |
| 260 | } |
| 261 | tsd->initMagic = INIT_MAGIC; |
| 262 | } |
| 263 | |
| 264 | |
| 265 | void * |
| 266 | _glthread_GetTSD(_glthread_TSD *tsd) |
| 267 | { |
| 268 | void *ptr; |
| 269 | if (tsd->initMagic != INIT_MAGIC) { |
| 270 | _glthread_InitTSD(tsd); |
| 271 | } |
| 272 | xthread_get_specific(tsd->key, &ptr); |
| 273 | return ptr; |
| 274 | } |
| 275 | |
| 276 | |
| 277 | void |
| 278 | _glthread_SetTSD(_glthread_TSD *tsd, void *ptr) |
| 279 | { |
| 280 | if (tsd->initMagic != INIT_MAGIC) { |
| 281 | _glthread_InitTSD(tsd); |
| 282 | } |
| 283 | xthread_set_specific(tsd->key, ptr); |
| 284 | } |
| 285 | |
| 286 | #endif /* XTHREAD */ |
| 287 | |
| 288 | |
Brian Paul | a360ab2 | 2000-02-10 21:54:06 +0000 | [diff] [blame] | 289 | |
Brian Paul | 1b37d6c | 2001-11-12 23:50:12 +0000 | [diff] [blame] | 290 | /* |
| 291 | * BeOS threads |
| 292 | */ |
| 293 | #ifdef BEOS_THREADS |
| 294 | |
| 295 | unsigned long |
| 296 | _glthread_GetID(void) |
| 297 | { |
| 298 | return (unsigned long) find_thread(NULL); |
| 299 | } |
| 300 | |
| 301 | void |
| 302 | _glthread_InitTSD(_glthread_TSD *tsd) |
| 303 | { |
| 304 | tsd->key = tls_allocate(); |
| 305 | tsd->initMagic = INIT_MAGIC; |
| 306 | } |
| 307 | |
| 308 | void * |
| 309 | _glthread_GetTSD(_glthread_TSD *tsd) |
| 310 | { |
| 311 | if (tsd->initMagic != (int) INIT_MAGIC) { |
| 312 | _glthread_InitTSD(tsd); |
| 313 | } |
| 314 | return tls_get(tsd->key); |
| 315 | } |
| 316 | |
| 317 | void |
| 318 | _glthread_SetTSD(_glthread_TSD *tsd, void *ptr) |
| 319 | { |
| 320 | if (tsd->initMagic != (int) INIT_MAGIC) { |
| 321 | _glthread_InitTSD(tsd); |
| 322 | } |
| 323 | tls_set(tsd->key, ptr); |
| 324 | } |
| 325 | |
| 326 | #endif /* BEOS_THREADS */ |
| 327 | |
| 328 | |
| 329 | |
Brian Paul | a360ab2 | 2000-02-10 21:54:06 +0000 | [diff] [blame] | 330 | #else /* THREADS */ |
| 331 | |
| 332 | |
| 333 | /* |
| 334 | * no-op functions |
| 335 | */ |
| 336 | |
| 337 | unsigned long |
| 338 | _glthread_GetID(void) |
| 339 | { |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | |
| 344 | void |
| 345 | _glthread_InitTSD(_glthread_TSD *tsd) |
| 346 | { |
| 347 | (void) tsd; |
| 348 | } |
| 349 | |
| 350 | |
| 351 | void * |
| 352 | _glthread_GetTSD(_glthread_TSD *tsd) |
| 353 | { |
| 354 | (void) tsd; |
| 355 | return NULL; |
| 356 | } |
| 357 | |
| 358 | |
| 359 | void |
| 360 | _glthread_SetTSD(_glthread_TSD *tsd, void *ptr) |
| 361 | { |
| 362 | (void) tsd; |
| 363 | (void) ptr; |
| 364 | } |
| 365 | |
| 366 | |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 367 | #endif /* THREADS */ |