[3597] | 1 | /* $Id: glthread.h,v 1.1 2000-05-23 20:34:50 jeroen Exp $ */
|
---|
| 2 |
|
---|
| 3 | /*
|
---|
| 4 | * Mesa 3-D graphics library
|
---|
| 5 | * Version: 3.3
|
---|
| 6 | *
|
---|
| 7 | * Copyright (C) 1999-2000 Brian Paul All Rights Reserved.
|
---|
| 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
|
---|
| 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 | *
|
---|
| 57 | */
|
---|
| 58 |
|
---|
| 59 | /*
|
---|
| 60 | * If this file is accidentally included by a non-threaded build,
|
---|
| 61 | * it should not cause the build to fail, or otherwise cause problems.
|
---|
| 62 | * In general, it should only be included when needed however.
|
---|
| 63 | */
|
---|
| 64 |
|
---|
| 65 |
|
---|
| 66 | #ifndef GLTHREAD_H
|
---|
| 67 | #define GLTHREAD_H
|
---|
| 68 |
|
---|
| 69 | #if defined(PTHREADS) || defined(SOLARIS_THREADS) || defined(WIN32_THREADS) || defined(XTHREADS) || defined(OS2_THREADS)
|
---|
| 70 | #define THREADS
|
---|
| 71 | #endif
|
---|
| 72 |
|
---|
| 73 |
|
---|
| 74 | /*
|
---|
| 75 | * POSIX threads. This should be your choice in the Unix world
|
---|
| 76 | * whenever possible. When building with POSIX threads, be sure
|
---|
| 77 | * to enable any compiler flags which will cause the MT-safe
|
---|
| 78 | * libc (if one exists) to be used when linking, as well as any
|
---|
| 79 | * header macros for MT-safe errno, etc. For Solaris, this is the -mt
|
---|
| 80 | * compiler flag. On Solaris with gcc, use -D_REENTRANT to enable
|
---|
| 81 | * proper compiling for MT-safe libc etc.
|
---|
| 82 | */
|
---|
| 83 | #if defined(PTHREADS)
|
---|
| 84 | #include <pthread.h> /* POSIX threads headers */
|
---|
| 85 |
|
---|
| 86 | typedef struct {
|
---|
| 87 | pthread_key_t key;
|
---|
| 88 | int initMagic;
|
---|
| 89 | } _glthread_TSD;
|
---|
| 90 |
|
---|
| 91 | typedef pthread_t _glthread_Thread;
|
---|
| 92 |
|
---|
| 93 | typedef pthread_mutex_t _glthread_Mutex;
|
---|
| 94 |
|
---|
| 95 | #define _glthread_DECLARE_STATIC_MUTEX(name) \
|
---|
| 96 | static _glthread_Mutex name = PTHREAD_MUTEX_INITIALIZER
|
---|
| 97 |
|
---|
| 98 | #define _glthread_INIT_MUTEX(name) \
|
---|
| 99 | pthread_mutex_init(&(name), NULL)
|
---|
| 100 |
|
---|
| 101 | #define _glthread_LOCK_MUTEX(name) \
|
---|
| 102 | (void) pthread_mutex_lock(&(name))
|
---|
| 103 |
|
---|
| 104 | #define _glthread_UNLOCK_MUTEX(name) \
|
---|
| 105 | (void) pthread_mutex_unlock(&(name))
|
---|
| 106 |
|
---|
| 107 | #endif /* PTHREADS */
|
---|
| 108 |
|
---|
| 109 |
|
---|
| 110 |
|
---|
| 111 |
|
---|
| 112 | /*
|
---|
| 113 | * Solaris threads. Use only up to Solaris 2.4.
|
---|
| 114 | * Solaris 2.5 and higher provide POSIX threads.
|
---|
| 115 | * Be sure to compile with -mt on the Solaris compilers, or
|
---|
| 116 | * use -D_REENTRANT if using gcc.
|
---|
| 117 | */
|
---|
| 118 | #ifdef SOLARIS_THREADS
|
---|
| 119 | #include <thread.h>
|
---|
| 120 |
|
---|
| 121 | typedef struct {
|
---|
| 122 | thread_key_t key;
|
---|
| 123 | mutex_t keylock;
|
---|
| 124 | int initMagic;
|
---|
| 125 | } _glthread_TSD;
|
---|
| 126 |
|
---|
| 127 | typedef thread_t _glthread_Thread;
|
---|
| 128 |
|
---|
| 129 | typedef mutex_t _glthread_Mutex;
|
---|
| 130 |
|
---|
| 131 | /* XXX need to really implement mutex-related macros */
|
---|
| 132 | #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
|
---|
| 133 | #define _glthread_INIT_MUTEX(name) (void) name
|
---|
| 134 | #define _glthread_LOCK_MUTEX(name) (void) name
|
---|
| 135 | #define _glthread_UNLOCK_MUTEX(name) (void) name
|
---|
| 136 |
|
---|
| 137 | #endif /* SOLARIS_THREADS */
|
---|
| 138 |
|
---|
| 139 |
|
---|
| 140 |
|
---|
| 141 |
|
---|
| 142 | /*
|
---|
| 143 | * Windows threads. Should work with Windows NT and 95.
|
---|
| 144 | * IMPORTANT: Link with multithreaded runtime library when THREADS are
|
---|
| 145 | * used!
|
---|
| 146 | */
|
---|
| 147 | #ifdef WIN32_THREADS
|
---|
| 148 | #include <windows.h>
|
---|
| 149 |
|
---|
| 150 | typedef struct {
|
---|
| 151 | DWORD key;
|
---|
| 152 | int initMagic;
|
---|
| 153 | } _glthread_TSD;
|
---|
| 154 |
|
---|
| 155 | typedef HANDLE _glthread_Thread;
|
---|
| 156 |
|
---|
| 157 | typedef CRITICAL_SECTION _glthread_Mutex;
|
---|
| 158 |
|
---|
| 159 | /* XXX need to really implement mutex-related macros */
|
---|
| 160 | #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name
|
---|
| 161 | #define _glthread_INIT_MUTEX(name) (void) name
|
---|
| 162 | #define _glthread_LOCK_MUTEX(name) (void) name
|
---|
| 163 | #define _glthread_UNLOCK_MUTEX(name) (void) name
|
---|
| 164 |
|
---|
| 165 | #endif /* WIN32_THREADS */
|
---|
| 166 |
|
---|
| 167 | /* Use OS/2's MUXSEM logic for Odin port of Mesa */
|
---|
| 168 | #ifdef OS2_THREADS
|
---|
| 169 | #include "os2thread.h"
|
---|
| 170 |
|
---|
| 171 | typedef struct {
|
---|
| 172 | DWORD key;
|
---|
| 173 | int initMagic;
|
---|
| 174 | } _glthread_TSD;
|
---|
| 175 |
|
---|
| 176 | typedef HANDLE _glthread_Thread;
|
---|
| 177 |
|
---|
| 178 | typedef HMTX _glthread_Mutex;
|
---|
| 179 |
|
---|
| 180 | /* XXX need to really implement mutex-related macros */
|
---|
| 181 | #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name
|
---|
| 182 | #define _glthread_INIT_MUTEX(name) (void) OS2CreateMutexSem(NULL,&name,0,0)
|
---|
| 183 | #define _glthread_LOCK_MUTEX(name) (void) OS2RequestMutexSem(name,-1)
|
---|
| 184 | #define _glthread_UNLOCK_MUTEX(name) (void) OS2ReleaseMutexSem(name)
|
---|
| 185 |
|
---|
| 186 | #endif /* OS2_THREADS */
|
---|
| 187 |
|
---|
| 188 |
|
---|
| 189 | /*
|
---|
| 190 | * XFree86 has its own thread wrapper, Xthreads.h
|
---|
| 191 | * We wrap it again for GL.
|
---|
| 192 | */
|
---|
| 193 | #ifdef XTHREADS
|
---|
| 194 | #include "Xthreads.h"
|
---|
| 195 |
|
---|
| 196 | typedef struct {
|
---|
| 197 | xthread_key_t key;
|
---|
| 198 | int initMagic;
|
---|
| 199 | } _glthread_TSD;
|
---|
| 200 |
|
---|
| 201 | typedef xthread_t _glthread_Thread;
|
---|
| 202 |
|
---|
| 203 | typedef xmutex_rec _glthread_Mutex;
|
---|
| 204 |
|
---|
| 205 | #define _glthread_DECLARE_STATIC_MUTEX(name) \
|
---|
| 206 | static _glthread_Mutex name = XMUTEX_INITIALIZER
|
---|
| 207 |
|
---|
| 208 | #define _glthread_INIT_MUTEX(name) \
|
---|
| 209 | xmutex_init(&(name))
|
---|
| 210 |
|
---|
| 211 | #define _glthread_LOCK_MUTEX(name) \
|
---|
| 212 | (void) xmutex_lock(&(name))
|
---|
| 213 |
|
---|
| 214 | #define _glthread_UNLOCK_MUTEX(name) \
|
---|
| 215 | (void) xmutex_unlock(&(name))
|
---|
| 216 |
|
---|
| 217 | #endif /* XTHREADS */
|
---|
| 218 |
|
---|
| 219 |
|
---|
| 220 |
|
---|
| 221 |
|
---|
| 222 | #ifndef THREADS
|
---|
| 223 |
|
---|
| 224 | /*
|
---|
| 225 | * THREADS not defined
|
---|
| 226 | */
|
---|
| 227 |
|
---|
| 228 | typedef GLuint _glthread_TSD;
|
---|
| 229 |
|
---|
| 230 | typedef GLuint _glthread_Thread;
|
---|
| 231 |
|
---|
| 232 | typedef GLuint _glthread_Mutex;
|
---|
| 233 |
|
---|
| 234 | #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
|
---|
| 235 |
|
---|
| 236 | #define _glthread_INIT_MUTEX(name) (void) name
|
---|
| 237 |
|
---|
| 238 | #define _glthread_LOCK_MUTEX(name) (void) name
|
---|
| 239 |
|
---|
| 240 | #define _glthread_UNLOCK_MUTEX(name) (void) name
|
---|
| 241 |
|
---|
| 242 | #endif /* THREADS */
|
---|
| 243 |
|
---|
| 244 |
|
---|
| 245 |
|
---|
| 246 | /*
|
---|
| 247 | * Platform independent thread specific data API.
|
---|
| 248 | */
|
---|
| 249 |
|
---|
| 250 | extern unsigned long
|
---|
| 251 | _glthread_GetID(void);
|
---|
| 252 |
|
---|
| 253 |
|
---|
| 254 | extern void
|
---|
| 255 | _glthread_InitTSD(_glthread_TSD *);
|
---|
| 256 |
|
---|
| 257 |
|
---|
| 258 | extern void *
|
---|
| 259 | _glthread_GetTSD(_glthread_TSD *);
|
---|
| 260 |
|
---|
| 261 |
|
---|
| 262 | extern void
|
---|
| 263 | _glthread_SetTSD(_glthread_TSD *, void *);
|
---|
| 264 |
|
---|
| 265 |
|
---|
| 266 |
|
---|
| 267 | #endif /* THREADS_H */
|
---|
| 268 |
|
---|