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