blob: 283d549df0749d69a7dadd9f4e052ad8c427897d [file] [log] [blame]
Brian Paulc11371a1999-12-16 17:31:06 +00001
2/*
3 * Mesa 3-D graphics library
Gareth Hughes22144ab2001-03-12 00:48:37 +00004 * Version: 3.5
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00005 *
Gareth Hughes22144ab2001-03-12 00:48:37 +00006 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00007 *
Brian Paulc11371a1999-12-16 17:31:06 +00008 * 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:
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +000014 *
Brian Paulc11371a1999-12-16 17:31:06 +000015 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +000017 *
Brian Paulc11371a1999-12-16 17:31:06 +000018 * 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/*
28 * Thread support for gl dispatch.
29 *
30 * Initial version by John Stone (j.stone@acm.org) (johns@cs.umr.edu)
31 * and Christoph Poliwoda (poliwoda@volumegraphics.com)
32 * Revised by Keith Whitwell
33 * Adapted for new gl dispatcher by Brian Paul
Brian Paula9601f12000-02-10 21:27:25 +000034 *
35 *
36 *
37 * DOCUMENTATION
38 *
39 * This thread module exports the following types:
40 * _glthread_TSD Thread-specific data area
41 * _glthread_Thread Thread datatype
42 * _glthread_Mutex Mutual exclusion lock
43 *
44 * Macros:
45 * _glthread_DECLARE_STATIC_MUTEX(name) Declare a non-local mutex
46 * _glthread_INIT_MUTEX(name) Initialize a mutex
47 * _glthread_LOCK_MUTEX(name) Lock a mutex
48 * _glthread_UNLOCK_MUTEX(name) Unlock a mutex
49 *
50 * Functions:
51 * _glthread_GetID(v) Get integer thread ID
52 * _glthread_InitTSD() Initialize thread-specific data
53 * _glthread_GetTSD() Get thread-specific data
54 * _glthread_SetTSD() Set thread-specific data
55 *
Brian Paulc11371a1999-12-16 17:31:06 +000056 */
57
Brian Paulc11371a1999-12-16 17:31:06 +000058/*
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +000059 * If this file is accidentally included by a non-threaded build,
Brian Paulc11371a1999-12-16 17:31:06 +000060 * it should not cause the build to fail, or otherwise cause problems.
61 * In general, it should only be included when needed however.
62 */
Brian Paula360ab22000-02-10 21:54:06 +000063
Brian Paula360ab22000-02-10 21:54:06 +000064#ifndef GLTHREAD_H
65#define GLTHREAD_H
66
67
68#if defined(PTHREADS) || defined(SOLARIS_THREADS) || defined(WIN32_THREADS) || defined(XTHREADS)
69#define THREADS
Brian Paulc11371a1999-12-16 17:31:06 +000070#endif
71
Jouk Jansen9e83e8c2000-11-17 11:00:55 +000072#ifdef VMS
73#include <GL/vms_x_fix.h>
74#endif
Brian Paulc11371a1999-12-16 17:31:06 +000075
76/*
77 * POSIX threads. This should be your choice in the Unix world
78 * whenever possible. When building with POSIX threads, be sure
79 * to enable any compiler flags which will cause the MT-safe
80 * libc (if one exists) to be used when linking, as well as any
81 * header macros for MT-safe errno, etc. For Solaris, this is the -mt
82 * compiler flag. On Solaris with gcc, use -D_REENTRANT to enable
83 * proper compiling for MT-safe libc etc.
84 */
Brian Paula360ab22000-02-10 21:54:06 +000085#if defined(PTHREADS)
Brian Paulc11371a1999-12-16 17:31:06 +000086#include <pthread.h> /* POSIX threads headers */
87
88typedef struct {
89 pthread_key_t key;
Brian Paula9601f12000-02-10 21:27:25 +000090 int initMagic;
Brian Paulc11371a1999-12-16 17:31:06 +000091} _glthread_TSD;
92
Brian Paulc11371a1999-12-16 17:31:06 +000093typedef pthread_t _glthread_Thread;
94
Brian Paulbc794052000-01-31 23:10:47 +000095typedef pthread_mutex_t _glthread_Mutex;
96
97#define _glthread_DECLARE_STATIC_MUTEX(name) \
98 static _glthread_Mutex name = PTHREAD_MUTEX_INITIALIZER
99
100#define _glthread_INIT_MUTEX(name) \
101 pthread_mutex_init(&(name), NULL)
102
Keith Whitwelle15fd852002-12-12 13:03:15 +0000103#define _glthread_DESTROY_MUTEX(name) \
104 pthread_mutex_destroy(&(name))
105
Brian Paulbc794052000-01-31 23:10:47 +0000106#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
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000112/* This is temporarilly removed because driver binaries cannot count on
113 * the existance of _gl_DispatchTSD in libGL. It only exists in "new"
114 * libGL. We may be able to ressurect this optimization at some point
115 * for DRI driver or for software Mesa.
116 */
117#if 0
118extern struct _glapi_table * _glapi_DispatchTSD;
119extern _glthread_TSD _gl_DispatchTSD;
120
121#define GL_CALL(name) \
122 (((__builtin_expect( _glapi_DispatchTSD != NULL, 1 )) \
123 ? _glapi_DispatchTSD : (struct _glapi_table *) pthread_getspecific(_gl_DispatchTSD.key))-> name)
124#endif
125
Brian Paulc11371a1999-12-16 17:31:06 +0000126#endif /* PTHREADS */
127
128
129
130
131/*
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +0000132 * Solaris threads. Use only up to Solaris 2.4.
Brian Paulc11371a1999-12-16 17:31:06 +0000133 * Solaris 2.5 and higher provide POSIX threads.
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +0000134 * Be sure to compile with -mt on the Solaris compilers, or
Brian Paulc11371a1999-12-16 17:31:06 +0000135 * use -D_REENTRANT if using gcc.
136 */
137#ifdef SOLARIS_THREADS
138#include <thread.h>
139
140typedef struct {
141 thread_key_t key;
142 mutex_t keylock;
Brian Paula9601f12000-02-10 21:27:25 +0000143 int initMagic;
Brian Paulc11371a1999-12-16 17:31:06 +0000144} _glthread_TSD;
145
Brian Paulc11371a1999-12-16 17:31:06 +0000146typedef thread_t _glthread_Thread;
147
Brian Paulbc794052000-01-31 23:10:47 +0000148typedef mutex_t _glthread_Mutex;
149
150/* XXX need to really implement mutex-related macros */
151#define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
152#define _glthread_INIT_MUTEX(name) (void) name
Keith Whitwelle15fd852002-12-12 13:03:15 +0000153#define _glthread_DESTROY_MUTEX(name) (void) name
Brian Paulbc794052000-01-31 23:10:47 +0000154#define _glthread_LOCK_MUTEX(name) (void) name
155#define _glthread_UNLOCK_MUTEX(name) (void) name
156
Brian Paulc11371a1999-12-16 17:31:06 +0000157#endif /* SOLARIS_THREADS */
158
159
160
161
162/*
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +0000163 * Windows threads. Should work with Windows NT and 95.
Brian Paulc11371a1999-12-16 17:31:06 +0000164 * IMPORTANT: Link with multithreaded runtime library when THREADS are
165 * used!
166 */
Brian Paulfa937f62000-02-11 21:38:33 +0000167#ifdef WIN32_THREADS
Brian Paulc11371a1999-12-16 17:31:06 +0000168#include <windows.h>
169
170typedef struct {
171 DWORD key;
Brian Paula9601f12000-02-10 21:27:25 +0000172 int initMagic;
Brian Paulc11371a1999-12-16 17:31:06 +0000173} _glthread_TSD;
174
Brian Paulc11371a1999-12-16 17:31:06 +0000175typedef HANDLE _glthread_Thread;
176
Brian Paulbc794052000-01-31 23:10:47 +0000177typedef CRITICAL_SECTION _glthread_Mutex;
178
179/* XXX need to really implement mutex-related macros */
180#define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
181#define _glthread_INIT_MUTEX(name) (void) name
Keith Whitwelle15fd852002-12-12 13:03:15 +0000182#define _glthread_DESTROY_MUTEX(name) (void) name
Brian Paulbc794052000-01-31 23:10:47 +0000183#define _glthread_LOCK_MUTEX(name) (void) name
184#define _glthread_UNLOCK_MUTEX(name) (void) name
185
Brian Paulfa937f62000-02-11 21:38:33 +0000186#endif /* WIN32_THREADS */
Brian Paulc11371a1999-12-16 17:31:06 +0000187
188
189
190
191/*
Brian Paula9601f12000-02-10 21:27:25 +0000192 * XFree86 has its own thread wrapper, Xthreads.h
193 * We wrap it again for GL.
Brian Paulc11371a1999-12-16 17:31:06 +0000194 */
Brian Paula9601f12000-02-10 21:27:25 +0000195#ifdef XTHREADS
196#include "Xthreads.h"
Brian Paulc11371a1999-12-16 17:31:06 +0000197
Brian Paula9601f12000-02-10 21:27:25 +0000198typedef struct {
199 xthread_key_t key;
200 int initMagic;
201} _glthread_TSD;
Brian Paulc11371a1999-12-16 17:31:06 +0000202
Brian Paula9601f12000-02-10 21:27:25 +0000203typedef xthread_t _glthread_Thread;
Brian Paulc11371a1999-12-16 17:31:06 +0000204
Brian Paula9601f12000-02-10 21:27:25 +0000205typedef xmutex_rec _glthread_Mutex;
Brian Paulc11371a1999-12-16 17:31:06 +0000206
Brian Paul5104b4d2002-03-07 21:50:41 +0000207#ifdef XMUTEX_INITIALIZER
Brian Paula9601f12000-02-10 21:27:25 +0000208#define _glthread_DECLARE_STATIC_MUTEX(name) \
209 static _glthread_Mutex name = XMUTEX_INITIALIZER
Brian Paul5104b4d2002-03-07 21:50:41 +0000210#else
211#define _glthread_DECLARE_STATIC_MUTEX(name) \
212 static _glthread_Mutex name
213#endif
Brian Paulc11371a1999-12-16 17:31:06 +0000214
Brian Paula9601f12000-02-10 21:27:25 +0000215#define _glthread_INIT_MUTEX(name) \
216 xmutex_init(&(name))
Brian Paulc11371a1999-12-16 17:31:06 +0000217
Keith Whitwelle15fd852002-12-12 13:03:15 +0000218#define _glthread_DESTROY_MUTEX(name) \
219 xmutex_clear(&(name))
220
Brian Paula9601f12000-02-10 21:27:25 +0000221#define _glthread_LOCK_MUTEX(name) \
222 (void) xmutex_lock(&(name))
Brian Paulc11371a1999-12-16 17:31:06 +0000223
Brian Paula9601f12000-02-10 21:27:25 +0000224#define _glthread_UNLOCK_MUTEX(name) \
225 (void) xmutex_unlock(&(name))
226
227#endif /* XTHREADS */
Brian Paulc11371a1999-12-16 17:31:06 +0000228
229
230
Brian Paul1b37d6c2001-11-12 23:50:12 +0000231/*
232 * BeOS threads. R5.x required.
233 */
234#ifdef BEOS_THREADS
235#include <kernel/OS.h>
236#include <support/TLS.h>
237
238typedef struct {
239 int32 key;
240 int initMagic;
241} _glthread_TSD;
242
243typedef thread_id _glthread_Thread;
244
245/* Use Benaphore, aka speeder semaphore */
246typedef struct {
247 int32 lock;
248 sem_id sem;
249} benaphore;
250typedef benaphore _glthread_Mutex;
251
252#define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = { 0,
253create_sem(0, #name"_benaphore") }
Karl Schultze6373ba2001-11-30 22:11:45 +0000254#define _glthread_INIT_MUTEX(name) name.sem = create_sem(0, #name"_benaphore"), name.lock = 0
255#define _glthread_LOCK_MUTEX(name) if((atomic_add(&(name.lock), 1)) >= 1) acquire_sem(name.sem)
256#define _glthread_UNLOCK_MUTEX(name) if((atomic_add(&(name.lock), -1)) > 1) release_sem(name.sem)
Brian Paul1b37d6c2001-11-12 23:50:12 +0000257
258#endif /* BEOS_THREADS */
259
260
Brian Paulbc794052000-01-31 23:10:47 +0000261
Brian Paula360ab22000-02-10 21:54:06 +0000262#ifndef THREADS
Brian Paulbc794052000-01-31 23:10:47 +0000263
264/*
265 * THREADS not defined
266 */
267
268typedef GLuint _glthread_TSD;
269
270typedef GLuint _glthread_Thread;
271
272typedef GLuint _glthread_Mutex;
273
274#define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
275
276#define _glthread_INIT_MUTEX(name) (void) name
277
Keith Whitwelle15fd852002-12-12 13:03:15 +0000278#define _glthread_DESTROY_MUTEX(name) (void) name
279
Brian Paulbc794052000-01-31 23:10:47 +0000280#define _glthread_LOCK_MUTEX(name) (void) name
281
282#define _glthread_UNLOCK_MUTEX(name) (void) name
283
Brian Paulbc794052000-01-31 23:10:47 +0000284#endif /* THREADS */
285
Brian Paula9601f12000-02-10 21:27:25 +0000286
287
288/*
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +0000289 * Platform independent thread specific data API.
Brian Paula9601f12000-02-10 21:27:25 +0000290 */
291
292extern unsigned long
293_glthread_GetID(void);
294
295
296extern void
297_glthread_InitTSD(_glthread_TSD *);
298
299
300extern void *
301_glthread_GetTSD(_glthread_TSD *);
302
303
304extern void
305_glthread_SetTSD(_glthread_TSD *, void *);
306
Ian Romanickc1d455f2004-05-27 00:03:53 +0000307#ifndef GL_CALL
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000308# if defined(THREADS)
309extern struct _glapi_table * _glapi_DispatchTSD;
310# define GL_CALL(name) \
311 (((__builtin_expect( _glapi_DispatchTSD != NULL, 1 )) \
312 ? _glapi_DispatchTSD : _glapi_get_dispatch())-> name)
313# else
314# define GL_CALL(name) (*(_glapi_Dispatch-> name))
315# endif /* defined(THREADS) */
316#endif /* ndef GL_CALL */
Brian Paula9601f12000-02-10 21:27:25 +0000317
318
Brian Paulbc794052000-01-31 23:10:47 +0000319#endif /* THREADS_H */