Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame^] | 1 | /* $Id: glthread.h,v 1.3 2000/02/10 21:27:25 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 | a9601f1 | 2000-02-10 21:27:25 +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 | /* |
| 29 | * Thread support for gl dispatch. |
| 30 | * |
| 31 | * Initial version by John Stone (j.stone@acm.org) (johns@cs.umr.edu) |
| 32 | * and Christoph Poliwoda (poliwoda@volumegraphics.com) |
| 33 | * Revised by Keith Whitwell |
| 34 | * Adapted for new gl dispatcher by Brian Paul |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame^] | 35 | * |
| 36 | * |
| 37 | * |
| 38 | * DOCUMENTATION |
| 39 | * |
| 40 | * This thread module exports the following types: |
| 41 | * _glthread_TSD Thread-specific data area |
| 42 | * _glthread_Thread Thread datatype |
| 43 | * _glthread_Mutex Mutual exclusion lock |
| 44 | * |
| 45 | * Macros: |
| 46 | * _glthread_DECLARE_STATIC_MUTEX(name) Declare a non-local mutex |
| 47 | * _glthread_INIT_MUTEX(name) Initialize a mutex |
| 48 | * _glthread_LOCK_MUTEX(name) Lock a mutex |
| 49 | * _glthread_UNLOCK_MUTEX(name) Unlock a mutex |
| 50 | * |
| 51 | * Functions: |
| 52 | * _glthread_GetID(v) Get integer thread ID |
| 53 | * _glthread_InitTSD() Initialize thread-specific data |
| 54 | * _glthread_GetTSD() Get thread-specific data |
| 55 | * _glthread_SetTSD() Set thread-specific data |
| 56 | * |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 57 | */ |
| 58 | |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame^] | 59 | |
Brian Paul | bc79405 | 2000-01-31 23:10:47 +0000 | [diff] [blame] | 60 | #ifndef GLTHREAD_H |
| 61 | #define GLTHREAD_H |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 62 | |
| 63 | |
| 64 | /* |
| 65 | * If this file is accidentally included by a non-threaded build, |
| 66 | * it should not cause the build to fail, or otherwise cause problems. |
| 67 | * In general, it should only be included when needed however. |
| 68 | */ |
| 69 | #ifdef THREADS |
| 70 | /* |
| 71 | * It is an error not to select a specific threads API when compiling. |
| 72 | */ |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame^] | 73 | #if !defined(PTHREADS) && !defined(SOLARIS_THREADS) && !defined(WIN32) && !defined(XTHREADS) |
| 74 | #error One of PTHREADS, SOLARIS_THREADS, WIN32 or XTHREADS must be defined. |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 75 | #endif |
| 76 | |
| 77 | |
| 78 | |
| 79 | /* |
| 80 | * POSIX threads. This should be your choice in the Unix world |
| 81 | * whenever possible. When building with POSIX threads, be sure |
| 82 | * to enable any compiler flags which will cause the MT-safe |
| 83 | * libc (if one exists) to be used when linking, as well as any |
| 84 | * header macros for MT-safe errno, etc. For Solaris, this is the -mt |
| 85 | * compiler flag. On Solaris with gcc, use -D_REENTRANT to enable |
| 86 | * proper compiling for MT-safe libc etc. |
| 87 | */ |
| 88 | #ifdef PTHREADS |
| 89 | #include <pthread.h> /* POSIX threads headers */ |
| 90 | |
| 91 | typedef struct { |
| 92 | pthread_key_t key; |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame^] | 93 | int initMagic; |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 94 | } _glthread_TSD; |
| 95 | |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 96 | typedef pthread_t _glthread_Thread; |
| 97 | |
Brian Paul | bc79405 | 2000-01-31 23:10:47 +0000 | [diff] [blame] | 98 | typedef pthread_mutex_t _glthread_Mutex; |
| 99 | |
| 100 | #define _glthread_DECLARE_STATIC_MUTEX(name) \ |
| 101 | static _glthread_Mutex name = PTHREAD_MUTEX_INITIALIZER |
| 102 | |
| 103 | #define _glthread_INIT_MUTEX(name) \ |
| 104 | pthread_mutex_init(&(name), NULL) |
| 105 | |
| 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 | |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 112 | #endif /* PTHREADS */ |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | /* |
| 118 | * Solaris threads. Use only up to Solaris 2.4. |
| 119 | * Solaris 2.5 and higher provide POSIX threads. |
| 120 | * Be sure to compile with -mt on the Solaris compilers, or |
| 121 | * use -D_REENTRANT if using gcc. |
| 122 | */ |
| 123 | #ifdef SOLARIS_THREADS |
| 124 | #include <thread.h> |
| 125 | |
| 126 | typedef struct { |
| 127 | thread_key_t key; |
| 128 | mutex_t keylock; |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame^] | 129 | int initMagic; |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 130 | } _glthread_TSD; |
| 131 | |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 132 | typedef thread_t _glthread_Thread; |
| 133 | |
Brian Paul | bc79405 | 2000-01-31 23:10:47 +0000 | [diff] [blame] | 134 | typedef mutex_t _glthread_Mutex; |
| 135 | |
| 136 | /* XXX need to really implement mutex-related macros */ |
| 137 | #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0 |
| 138 | #define _glthread_INIT_MUTEX(name) (void) name |
| 139 | #define _glthread_LOCK_MUTEX(name) (void) name |
| 140 | #define _glthread_UNLOCK_MUTEX(name) (void) name |
| 141 | |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 142 | #endif /* SOLARIS_THREADS */ |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | /* |
| 148 | * Windows threads. Should work with Windows NT and 95. |
| 149 | * IMPORTANT: Link with multithreaded runtime library when THREADS are |
| 150 | * used! |
| 151 | */ |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 152 | #ifdef WIN32 |
| 153 | #include <windows.h> |
| 154 | |
| 155 | typedef struct { |
| 156 | DWORD key; |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame^] | 157 | int initMagic; |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 158 | } _glthread_TSD; |
| 159 | |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 160 | typedef HANDLE _glthread_Thread; |
| 161 | |
Brian Paul | bc79405 | 2000-01-31 23:10:47 +0000 | [diff] [blame] | 162 | typedef CRITICAL_SECTION _glthread_Mutex; |
| 163 | |
| 164 | /* XXX need to really implement mutex-related macros */ |
| 165 | #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0 |
| 166 | #define _glthread_INIT_MUTEX(name) (void) name |
| 167 | #define _glthread_LOCK_MUTEX(name) (void) name |
| 168 | #define _glthread_UNLOCK_MUTEX(name) (void) name |
| 169 | |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 170 | #endif /* WIN32 */ |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | /* |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame^] | 176 | * XFree86 has its own thread wrapper, Xthreads.h |
| 177 | * We wrap it again for GL. |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 178 | */ |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame^] | 179 | #ifdef XTHREADS |
| 180 | #include "Xthreads.h" |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 181 | |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame^] | 182 | typedef struct { |
| 183 | xthread_key_t key; |
| 184 | int initMagic; |
| 185 | } _glthread_TSD; |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 186 | |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame^] | 187 | typedef xthread_t _glthread_Thread; |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 188 | |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame^] | 189 | typedef xmutex_rec _glthread_Mutex; |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 190 | |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame^] | 191 | #define _glthread_DECLARE_STATIC_MUTEX(name) \ |
| 192 | static _glthread_Mutex name = XMUTEX_INITIALIZER |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 193 | |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame^] | 194 | #define _glthread_INIT_MUTEX(name) \ |
| 195 | xmutex_init(&(name)) |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 196 | |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame^] | 197 | #define _glthread_LOCK_MUTEX(name) \ |
| 198 | (void) xmutex_lock(&(name)) |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 199 | |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame^] | 200 | #define _glthread_UNLOCK_MUTEX(name) \ |
| 201 | (void) xmutex_unlock(&(name)) |
| 202 | |
| 203 | #endif /* XTHREADS */ |
Brian Paul | c11371a | 1999-12-16 17:31:06 +0000 | [diff] [blame] | 204 | |
| 205 | |
| 206 | |
Brian Paul | bc79405 | 2000-01-31 23:10:47 +0000 | [diff] [blame] | 207 | #else /* THREADS */ |
| 208 | |
| 209 | |
| 210 | /* |
| 211 | * THREADS not defined |
| 212 | */ |
| 213 | |
| 214 | typedef GLuint _glthread_TSD; |
| 215 | |
| 216 | typedef GLuint _glthread_Thread; |
| 217 | |
| 218 | typedef GLuint _glthread_Mutex; |
| 219 | |
| 220 | #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0 |
| 221 | |
| 222 | #define _glthread_INIT_MUTEX(name) (void) name |
| 223 | |
| 224 | #define _glthread_LOCK_MUTEX(name) (void) name |
| 225 | |
| 226 | #define _glthread_UNLOCK_MUTEX(name) (void) name |
| 227 | |
| 228 | |
| 229 | #endif /* THREADS */ |
| 230 | |
Brian Paul | a9601f1 | 2000-02-10 21:27:25 +0000 | [diff] [blame^] | 231 | |
| 232 | |
| 233 | /* |
| 234 | * Platform independent thread specific data API. |
| 235 | */ |
| 236 | |
| 237 | extern unsigned long |
| 238 | _glthread_GetID(void); |
| 239 | |
| 240 | |
| 241 | extern void |
| 242 | _glthread_InitTSD(_glthread_TSD *); |
| 243 | |
| 244 | |
| 245 | extern void * |
| 246 | _glthread_GetTSD(_glthread_TSD *); |
| 247 | |
| 248 | |
| 249 | extern void |
| 250 | _glthread_SetTSD(_glthread_TSD *, void *); |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
Brian Paul | bc79405 | 2000-01-31 23:10:47 +0000 | [diff] [blame] | 255 | #endif /* THREADS_H */ |
| 256 | |