Brian Paul | a360ab2 | 2000-02-10 21:54:06 +0000 | [diff] [blame^] | 1 | /* $Id: glthread.c,v 1.5 2000/02/10 21:54:06 brianp Exp $ */ |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 2 | |
| 3 | /* |
| 4 | * Mesa 3-D graphics library |
| 5 | * Version: 3.3 |
| 6 | * |
Brian Paul | 8d365ab | 2000-01-28 18:57:56 +0000 | [diff] [blame] | 7 | * Copyright (C) 1999-2000 Brian Paul All Rights Reserved. |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 8 | * |
| 9 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 10 | * copy of this software and associated documentation files (the "Software"), |
| 11 | * to deal in the Software without restriction, including without limitation |
| 12 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 13 | * and/or sell copies of the Software, and to permit persons to whom the |
| 14 | * Software is furnished to do so, subject to the following conditions: |
| 15 | * |
| 16 | * The above copyright notice and this permission notice shall be included |
| 17 | * in all copies or substantial portions of the Software. |
| 18 | * |
| 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 20 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 22 | * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN |
| 23 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 24 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 25 | */ |
| 26 | |
| 27 | |
| 28 | /* |
Brian Paul | 8d365ab | 2000-01-28 18:57:56 +0000 | [diff] [blame] | 29 | * XXX There's probably some work to do in order to make this file |
| 30 | * truly reusable outside of Mesa. First, the glheader.h include must go. |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 31 | */ |
| 32 | |
| 33 | |
| 34 | #ifdef PC_ALL |
| 35 | #include "all.h" |
| 36 | #else |
| 37 | #include "glheader.h" |
Brian Paul | a360ab2 | 2000-02-10 21:54:06 +0000 | [diff] [blame^] | 38 | #include "glthread.h" |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 39 | #endif |
| 40 | |
| 41 | |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 42 | /* |
| 43 | * This file should still compile even when THREADS is not defined. |
| 44 | * This is to make things easier to deal with on the makefile scene.. |
| 45 | */ |
| 46 | #ifdef THREADS |
| 47 | #include <errno.h> |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 48 | |
| 49 | /* |
| 50 | * Error messages |
| 51 | */ |
| 52 | #define INIT_TSD_ERROR "_glthread_: failed to allocate key for thread specific data" |
| 53 | #define GET_TSD_ERROR "_glthread_: failed to get thread specific data" |
| 54 | #define SET_TSD_ERROR "_glthread_: thread failed to set thread specific data" |
| 55 | |
| 56 | |
| 57 | /* |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 58 | * Magic number to determine if a TSD object has been initialized. |
| 59 | * Kind of a hack but there doesn't appear to be a better cross-platform |
| 60 | * solution. |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 61 | */ |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 62 | #define INIT_MAGIC 0xff8adc98 |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 63 | |
| 64 | |
| 65 | |
| 66 | /* |
| 67 | * POSIX Threads -- The best way to go if your platform supports them. |
| 68 | * Solaris >= 2.5 have POSIX threads, IRIX >= 6.4 reportedly |
| 69 | * has them, and many of the free Unixes now have them. |
| 70 | * Be sure to use appropriate -mt or -D_REENTRANT type |
| 71 | * compile flags when building. |
| 72 | */ |
| 73 | #ifdef PTHREADS |
| 74 | |
| 75 | unsigned long |
| 76 | _glthread_GetID(void) |
| 77 | { |
| 78 | return (unsigned long) pthread_self(); |
| 79 | } |
| 80 | |
| 81 | |
| 82 | void |
| 83 | _glthread_InitTSD(_glthread_TSD *tsd) |
| 84 | { |
Brian Paul | e2b10e7 | 1999-12-17 11:13:54 +0000 | [diff] [blame] | 85 | if (pthread_key_create(&tsd->key, NULL/*free*/) != 0) { |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 86 | perror(INIT_TSD_ERROR); |
| 87 | exit(-1); |
| 88 | } |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 89 | tsd->initMagic = INIT_MAGIC; |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | |
| 93 | void * |
| 94 | _glthread_GetTSD(_glthread_TSD *tsd) |
| 95 | { |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 96 | if (tsd->initMagic != INIT_MAGIC) { |
| 97 | _glthread_InitTSD(tsd); |
| 98 | } |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 99 | return pthread_getspecific(tsd->key); |
| 100 | } |
| 101 | |
| 102 | |
| 103 | void |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 104 | _glthread_SetTSD(_glthread_TSD *tsd, void *ptr) |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 105 | { |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 106 | if (tsd->initMagic != INIT_MAGIC) { |
| 107 | _glthread_InitTSD(tsd); |
| 108 | } |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 109 | if (pthread_setspecific(tsd->key, ptr) != 0) { |
| 110 | perror(SET_TSD_ERROR); |
| 111 | exit(-1); |
Brian Paul | e2b10e7 | 1999-12-17 11:13:54 +0000 | [diff] [blame] | 112 | } |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | #endif /* PTHREADS */ |
| 116 | |
| 117 | |
| 118 | |
| 119 | /* |
| 120 | * Solaris/Unix International Threads -- Use only if POSIX threads |
| 121 | * aren't available on your Unix platform. Solaris 2.[34] are examples |
| 122 | * of platforms where this is the case. Be sure to use -mt and/or |
| 123 | * -D_REENTRANT when compiling. |
| 124 | */ |
| 125 | #ifdef SOLARIS_THREADS |
| 126 | #define USE_LOCK_FOR_KEY /* undef this to try a version without |
| 127 | lock for the global key... */ |
| 128 | |
| 129 | unsigned long |
| 130 | _glthread_GetID(void) |
| 131 | { |
| 132 | abort(); /* XXX not implemented yet */ |
| 133 | return (unsigned long) 0; |
| 134 | } |
| 135 | |
| 136 | |
| 137 | void |
| 138 | _glthread_InitTSD(_glthread_TSD *tsd) |
| 139 | { |
| 140 | if ((errno = mutex_init(&tsd->keylock, 0, NULL)) != 0 || |
| 141 | (errno = thr_keycreate(&(tsd->key), free)) != 0) { |
| 142 | perror(INIT_TSD_ERROR); |
| 143 | exit(-1); |
| 144 | } |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 145 | tsd->initMagic = INIT_MAGIC; |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | |
| 149 | void * |
| 150 | _glthread_GetTSD(_glthread_TSD *tsd) |
| 151 | { |
| 152 | void* ret; |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 153 | if (tsd->initMagic != INIT_MAGIC) { |
| 154 | _glthread_InitTSD(tsd); |
| 155 | } |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 156 | #ifdef USE_LOCK_FOR_KEY |
| 157 | mutex_lock(&tsd->keylock); |
| 158 | thr_getspecific(tsd->key, &ret); |
| 159 | mutex_unlock(&tsd->keylock); |
| 160 | #else |
| 161 | if ((errno = thr_getspecific(tsd->key, &ret)) != 0) { |
| 162 | perror(GET_TSD_ERROR); |
| 163 | exit(-1); |
Brian Paul | e2b10e7 | 1999-12-17 11:13:54 +0000 | [diff] [blame] | 164 | } |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 165 | #endif |
| 166 | return ret; |
| 167 | } |
| 168 | |
| 169 | |
| 170 | void |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 171 | _glthread_SetTSD(_glthread_TSD *tsd, void *ptr) |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 172 | { |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 173 | if (tsd->initMagic != INIT_MAGIC) { |
| 174 | _glthread_InitTSD(tsd); |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 175 | } |
| 176 | if ((errno = thr_setspecific(tsd->key, ptr)) != 0) { |
| 177 | perror(SET_TSD_ERROR); |
| 178 | exit(-1); |
Brian Paul | e2b10e7 | 1999-12-17 11:13:54 +0000 | [diff] [blame] | 179 | } |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | #undef USE_LOCK_FOR_KEY |
| 183 | #endif /* SOLARIS_THREADS */ |
| 184 | |
| 185 | |
| 186 | |
| 187 | /* |
| 188 | * Win32 Threads. The only available option for Windows 95/NT. |
| 189 | * Be sure that you compile using the Multithreaded runtime, otherwise |
| 190 | * bad things will happen. |
| 191 | */ |
| 192 | #ifdef WIN32 |
| 193 | |
| 194 | unsigned long |
| 195 | _glthread_GetID(void) |
| 196 | { |
| 197 | abort(); /* XXX not implemented yet */ |
| 198 | return (unsigned long) 0; |
| 199 | } |
| 200 | |
| 201 | |
| 202 | void |
| 203 | _glthread_InitTSD(_glthread_TSD *tsd) |
| 204 | { |
| 205 | tsd->key = TlsAlloc(); |
| 206 | if (tsd->key == 0xffffffff) { |
| 207 | /* Can Windows handle stderr messages for non-console |
| 208 | applications? Does Windows have perror? */ |
| 209 | /* perror(SET_INIT_ERROR);*/ |
| 210 | exit(-1); |
| 211 | } |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 212 | tsd->initMagic = INIT_MAGIC; |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | |
| 216 | void * |
| 217 | _glthread_GetTSD(_glthread_TSD *tsd) |
| 218 | { |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 219 | if (tsd->initMagic != INIT_MAGIC) { |
| 220 | _glthread_InitTSD(tsd); |
| 221 | } |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 222 | return TlsGetValue(tsd->key); |
| 223 | } |
| 224 | |
| 225 | |
| 226 | void |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 227 | _glthread_SetTSD(_glthread_TSD *tsd, void *ptr) |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 228 | { |
| 229 | /* the following code assumes that the _glthread_TSD has been initialized |
| 230 | to zero at creation */ |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 231 | if (tsd->initMagic != INIT_MAGIC) { |
| 232 | _glthread_InitTSD(tsd); |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 233 | } |
| 234 | if (TlsSetValue(tsd->key, ptr) == 0) { |
| 235 | /* Can Windows handle stderr messages for non-console |
| 236 | applications? Does Windows have perror? */ |
| 237 | /* perror(SET_TSD_ERROR);*/ |
| 238 | exit(-1); |
Brian Paul | e2b10e7 | 1999-12-17 11:13:54 +0000 | [diff] [blame] | 239 | } |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | #endif /* WIN32 */ |
| 243 | |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame] | 244 | |
| 245 | |
| 246 | /* |
| 247 | * XFree86 has its own thread wrapper, Xthreads.h |
| 248 | * We wrap it again for GL. |
| 249 | */ |
| 250 | #ifdef XTHREADS |
| 251 | |
| 252 | unsigned long |
| 253 | _glthread_GetID(void) |
| 254 | { |
| 255 | return (unsigned long) xthread_self(); |
| 256 | } |
| 257 | |
| 258 | |
| 259 | void |
| 260 | _glthread_InitTSD(_glthread_TSD *tsd) |
| 261 | { |
| 262 | if (xthread_key_create(&tsd->key, NULL) != 0) { |
| 263 | perror(INIT_TSD_ERROR); |
| 264 | exit(-1); |
| 265 | } |
| 266 | tsd->initMagic = INIT_MAGIC; |
| 267 | } |
| 268 | |
| 269 | |
| 270 | void * |
| 271 | _glthread_GetTSD(_glthread_TSD *tsd) |
| 272 | { |
| 273 | void *ptr; |
| 274 | if (tsd->initMagic != INIT_MAGIC) { |
| 275 | _glthread_InitTSD(tsd); |
| 276 | } |
| 277 | xthread_get_specific(tsd->key, &ptr); |
| 278 | return ptr; |
| 279 | } |
| 280 | |
| 281 | |
| 282 | void |
| 283 | _glthread_SetTSD(_glthread_TSD *tsd, void *ptr) |
| 284 | { |
| 285 | if (tsd->initMagic != INIT_MAGIC) { |
| 286 | _glthread_InitTSD(tsd); |
| 287 | } |
| 288 | xthread_set_specific(tsd->key, ptr); |
| 289 | } |
| 290 | |
| 291 | #endif /* XTHREAD */ |
| 292 | |
| 293 | |
Brian Paul | a360ab2 | 2000-02-10 21:54:06 +0000 | [diff] [blame^] | 294 | |
| 295 | #else /* THREADS */ |
| 296 | |
| 297 | |
| 298 | /* |
| 299 | * no-op functions |
| 300 | */ |
| 301 | |
| 302 | unsigned long |
| 303 | _glthread_GetID(void) |
| 304 | { |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | |
| 309 | void |
| 310 | _glthread_InitTSD(_glthread_TSD *tsd) |
| 311 | { |
| 312 | (void) tsd; |
| 313 | } |
| 314 | |
| 315 | |
| 316 | void * |
| 317 | _glthread_GetTSD(_glthread_TSD *tsd) |
| 318 | { |
| 319 | (void) tsd; |
| 320 | return NULL; |
| 321 | } |
| 322 | |
| 323 | |
| 324 | void |
| 325 | _glthread_SetTSD(_glthread_TSD *tsd, void *ptr) |
| 326 | { |
| 327 | (void) tsd; |
| 328 | (void) ptr; |
| 329 | } |
| 330 | |
| 331 | |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 332 | #endif /* THREADS */ |