blob: 0958cd1b32c4a790b98f3d5dd18f91a49ce81a71 [file] [log] [blame]
Brian Paulc11371a1999-12-16 17:31:06 +00001/*
2 * Mesa 3-D graphics library
Brian Paul385f23e2006-06-16 14:50:05 +00003 * Version: 6.5.1
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00004 *
Brian Paul385f23e2006-06-16 14:50:05 +00005 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00006 *
Brian Paulc11371a1999-12-16 17:31:06 +00007 * 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:
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +000013 *
Brian Paulc11371a1999-12-16 17:31:06 +000014 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +000016 *
Brian Paulc11371a1999-12-16 17:31:06 +000017 * 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/*
27 * Thread support for gl dispatch.
28 *
29 * Initial version by John Stone (j.stone@acm.org) (johns@cs.umr.edu)
30 * and Christoph Poliwoda (poliwoda@volumegraphics.com)
31 * Revised by Keith Whitwell
32 * Adapted for new gl dispatcher by Brian Paul
Brian Paula9601f12000-02-10 21:27:25 +000033 *
34 *
35 *
36 * DOCUMENTATION
37 *
38 * This thread module exports the following types:
39 * _glthread_TSD Thread-specific data area
40 * _glthread_Thread Thread datatype
41 * _glthread_Mutex Mutual exclusion lock
42 *
43 * Macros:
44 * _glthread_DECLARE_STATIC_MUTEX(name) Declare a non-local mutex
45 * _glthread_INIT_MUTEX(name) Initialize a mutex
46 * _glthread_LOCK_MUTEX(name) Lock a mutex
47 * _glthread_UNLOCK_MUTEX(name) Unlock a mutex
48 *
49 * Functions:
50 * _glthread_GetID(v) Get integer thread ID
51 * _glthread_InitTSD() Initialize thread-specific data
52 * _glthread_GetTSD() Get thread-specific data
53 * _glthread_SetTSD() Set thread-specific data
54 *
Brian Paulc11371a1999-12-16 17:31:06 +000055 */
56
Brian Paulc11371a1999-12-16 17:31:06 +000057/*
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +000058 * If this file is accidentally included by a non-threaded build,
Brian Paulc11371a1999-12-16 17:31:06 +000059 * it should not cause the build to fail, or otherwise cause problems.
60 * In general, it should only be included when needed however.
61 */
Brian Paula360ab22000-02-10 21:54:06 +000062
Brian Paula360ab22000-02-10 21:54:06 +000063#ifndef GLTHREAD_H
64#define GLTHREAD_H
65
66
Ian Romanick6cae4f32005-04-14 21:05:55 +000067#if (defined(PTHREADS) || defined(SOLARIS_THREADS) ||\
Ian Romanick711555d2005-08-03 23:05:25 +000068 defined(WIN32_THREADS) || defined(USE_XTHREADS) || defined(BEOS_THREADS)) \
Ian Romanick6cae4f32005-04-14 21:05:55 +000069 && !defined(THREADS)
70# define THREADS
Brian Paulc11371a1999-12-16 17:31:06 +000071#endif
72
Jouk Jansen9e83e8c2000-11-17 11:00:55 +000073#ifdef VMS
74#include <GL/vms_x_fix.h>
75#endif
Brian Paulc11371a1999-12-16 17:31:06 +000076
77/*
78 * POSIX threads. This should be your choice in the Unix world
79 * whenever possible. When building with POSIX threads, be sure
80 * to enable any compiler flags which will cause the MT-safe
81 * libc (if one exists) to be used when linking, as well as any
82 * header macros for MT-safe errno, etc. For Solaris, this is the -mt
83 * compiler flag. On Solaris with gcc, use -D_REENTRANT to enable
84 * proper compiling for MT-safe libc etc.
85 */
Brian Paula360ab22000-02-10 21:54:06 +000086#if defined(PTHREADS)
Brian Paulc11371a1999-12-16 17:31:06 +000087#include <pthread.h> /* POSIX threads headers */
88
89typedef struct {
90 pthread_key_t key;
Brian Paula9601f12000-02-10 21:27:25 +000091 int initMagic;
Brian Paulc11371a1999-12-16 17:31:06 +000092} _glthread_TSD;
93
Brian Paulc11371a1999-12-16 17:31:06 +000094typedef pthread_t _glthread_Thread;
95
Brian Paulbc794052000-01-31 23:10:47 +000096typedef pthread_mutex_t _glthread_Mutex;
97
98#define _glthread_DECLARE_STATIC_MUTEX(name) \
99 static _glthread_Mutex name = PTHREAD_MUTEX_INITIALIZER
100
101#define _glthread_INIT_MUTEX(name) \
102 pthread_mutex_init(&(name), NULL)
103
Keith Whitwelle15fd852002-12-12 13:03:15 +0000104#define _glthread_DESTROY_MUTEX(name) \
105 pthread_mutex_destroy(&(name))
106
Brian Paulbc794052000-01-31 23:10:47 +0000107#define _glthread_LOCK_MUTEX(name) \
108 (void) pthread_mutex_lock(&(name))
109
110#define _glthread_UNLOCK_MUTEX(name) \
111 (void) pthread_mutex_unlock(&(name))
112
Brian Paulc11371a1999-12-16 17:31:06 +0000113#endif /* PTHREADS */
114
115
116
117
118/*
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +0000119 * Solaris threads. Use only up to Solaris 2.4.
Brian Paulc11371a1999-12-16 17:31:06 +0000120 * Solaris 2.5 and higher provide POSIX threads.
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +0000121 * Be sure to compile with -mt on the Solaris compilers, or
Brian Paulc11371a1999-12-16 17:31:06 +0000122 * use -D_REENTRANT if using gcc.
123 */
124#ifdef SOLARIS_THREADS
125#include <thread.h>
126
127typedef struct {
128 thread_key_t key;
129 mutex_t keylock;
Brian Paula9601f12000-02-10 21:27:25 +0000130 int initMagic;
Brian Paulc11371a1999-12-16 17:31:06 +0000131} _glthread_TSD;
132
Brian Paulc11371a1999-12-16 17:31:06 +0000133typedef thread_t _glthread_Thread;
134
Brian Paulbc794052000-01-31 23:10:47 +0000135typedef mutex_t _glthread_Mutex;
136
137/* XXX need to really implement mutex-related macros */
138#define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
139#define _glthread_INIT_MUTEX(name) (void) name
Keith Whitwelle15fd852002-12-12 13:03:15 +0000140#define _glthread_DESTROY_MUTEX(name) (void) name
Brian Paulbc794052000-01-31 23:10:47 +0000141#define _glthread_LOCK_MUTEX(name) (void) name
142#define _glthread_UNLOCK_MUTEX(name) (void) name
143
Brian Paulc11371a1999-12-16 17:31:06 +0000144#endif /* SOLARIS_THREADS */
145
146
147
148
149/*
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +0000150 * Windows threads. Should work with Windows NT and 95.
Brian Paulc11371a1999-12-16 17:31:06 +0000151 * IMPORTANT: Link with multithreaded runtime library when THREADS are
152 * used!
153 */
Brian Paulfa937f62000-02-11 21:38:33 +0000154#ifdef WIN32_THREADS
Brian Paulc11371a1999-12-16 17:31:06 +0000155#include <windows.h>
156
157typedef struct {
158 DWORD key;
Brian Paula9601f12000-02-10 21:27:25 +0000159 int initMagic;
Brian Paulc11371a1999-12-16 17:31:06 +0000160} _glthread_TSD;
161
Brian Paulc11371a1999-12-16 17:31:06 +0000162typedef HANDLE _glthread_Thread;
163
Brian Paulbc794052000-01-31 23:10:47 +0000164typedef CRITICAL_SECTION _glthread_Mutex;
165
Brian Paul385f23e2006-06-16 14:50:05 +0000166#define _glthread_DECLARE_STATIC_MUTEX(name) /*static*/ _glthread_Mutex name = {0,0,0,0,0,0}
167#define _glthread_INIT_MUTEX(name) InitializeCriticalSection(&name)
168#define _glthread_DESTROY_MUTEX(name) DeleteCriticalSection(&name)
169#define _glthread_LOCK_MUTEX(name) EnterCriticalSection(&name)
170#define _glthread_UNLOCK_MUTEX(name) LeaveCriticalSection(&name)
Brian Paulbc794052000-01-31 23:10:47 +0000171
Brian Paulfa937f62000-02-11 21:38:33 +0000172#endif /* WIN32_THREADS */
Brian Paulc11371a1999-12-16 17:31:06 +0000173
174
175
176
177/*
Brian Paula9601f12000-02-10 21:27:25 +0000178 * XFree86 has its own thread wrapper, Xthreads.h
179 * We wrap it again for GL.
Brian Paulc11371a1999-12-16 17:31:06 +0000180 */
Ian Romanick711555d2005-08-03 23:05:25 +0000181#ifdef USE_XTHREADS
Brian Paul263317d2004-11-22 19:11:01 +0000182#include <X11/Xthreads.h>
Brian Paulc11371a1999-12-16 17:31:06 +0000183
Brian Paula9601f12000-02-10 21:27:25 +0000184typedef struct {
185 xthread_key_t key;
186 int initMagic;
187} _glthread_TSD;
Brian Paulc11371a1999-12-16 17:31:06 +0000188
Brian Paula9601f12000-02-10 21:27:25 +0000189typedef xthread_t _glthread_Thread;
Brian Paulc11371a1999-12-16 17:31:06 +0000190
Brian Paula9601f12000-02-10 21:27:25 +0000191typedef xmutex_rec _glthread_Mutex;
Brian Paulc11371a1999-12-16 17:31:06 +0000192
Brian Paul5104b4d2002-03-07 21:50:41 +0000193#ifdef XMUTEX_INITIALIZER
Brian Paula9601f12000-02-10 21:27:25 +0000194#define _glthread_DECLARE_STATIC_MUTEX(name) \
195 static _glthread_Mutex name = XMUTEX_INITIALIZER
Brian Paul5104b4d2002-03-07 21:50:41 +0000196#else
197#define _glthread_DECLARE_STATIC_MUTEX(name) \
198 static _glthread_Mutex name
199#endif
Brian Paulc11371a1999-12-16 17:31:06 +0000200
Brian Paula9601f12000-02-10 21:27:25 +0000201#define _glthread_INIT_MUTEX(name) \
202 xmutex_init(&(name))
Brian Paulc11371a1999-12-16 17:31:06 +0000203
Keith Whitwelle15fd852002-12-12 13:03:15 +0000204#define _glthread_DESTROY_MUTEX(name) \
205 xmutex_clear(&(name))
206
Brian Paula9601f12000-02-10 21:27:25 +0000207#define _glthread_LOCK_MUTEX(name) \
208 (void) xmutex_lock(&(name))
Brian Paulc11371a1999-12-16 17:31:06 +0000209
Brian Paula9601f12000-02-10 21:27:25 +0000210#define _glthread_UNLOCK_MUTEX(name) \
211 (void) xmutex_unlock(&(name))
212
Ian Romanick711555d2005-08-03 23:05:25 +0000213#endif /* USE_XTHREADS */
Brian Paulc11371a1999-12-16 17:31:06 +0000214
215
216
Brian Paul1b37d6c2001-11-12 23:50:12 +0000217/*
218 * BeOS threads. R5.x required.
219 */
220#ifdef BEOS_THREADS
Philippe Houdoinb4907822004-08-14 09:48:57 +0000221
Brian Paul1b37d6c2001-11-12 23:50:12 +0000222#include <kernel/OS.h>
223#include <support/TLS.h>
224
225typedef struct {
226 int32 key;
227 int initMagic;
228} _glthread_TSD;
229
230typedef thread_id _glthread_Thread;
231
232/* Use Benaphore, aka speeder semaphore */
233typedef struct {
234 int32 lock;
235 sem_id sem;
236} benaphore;
237typedef benaphore _glthread_Mutex;
238
Philippe Houdoinb4907822004-08-14 09:48:57 +0000239#define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = { 0, 0 }
240#define _glthread_INIT_MUTEX(name) name.sem = create_sem(0, #name"_benaphore"), name.lock = 0
241#define _glthread_DESTROY_MUTEX(name) delete_sem(name.sem), name.lock = 0
242#define _glthread_LOCK_MUTEX(name) if (name.sem == 0) _glthread_INIT_MUTEX(name); \
243 if (atomic_add(&(name.lock), 1) >= 1) acquire_sem(name.sem)
244#define _glthread_UNLOCK_MUTEX(name) if (atomic_add(&(name.lock), -1) > 1) release_sem(name.sem)
Brian Paul1b37d6c2001-11-12 23:50:12 +0000245
246#endif /* BEOS_THREADS */
247
248
Brian Paulbc794052000-01-31 23:10:47 +0000249
Brian Paula360ab22000-02-10 21:54:06 +0000250#ifndef THREADS
Brian Paulbc794052000-01-31 23:10:47 +0000251
252/*
253 * THREADS not defined
254 */
255
256typedef GLuint _glthread_TSD;
257
258typedef GLuint _glthread_Thread;
259
260typedef GLuint _glthread_Mutex;
261
262#define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
263
264#define _glthread_INIT_MUTEX(name) (void) name
265
Keith Whitwelle15fd852002-12-12 13:03:15 +0000266#define _glthread_DESTROY_MUTEX(name) (void) name
267
Brian Paulbc794052000-01-31 23:10:47 +0000268#define _glthread_LOCK_MUTEX(name) (void) name
269
270#define _glthread_UNLOCK_MUTEX(name) (void) name
271
Brian Paulbc794052000-01-31 23:10:47 +0000272#endif /* THREADS */
273
Brian Paula9601f12000-02-10 21:27:25 +0000274
275
276/*
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +0000277 * Platform independent thread specific data API.
Brian Paula9601f12000-02-10 21:27:25 +0000278 */
279
280extern unsigned long
281_glthread_GetID(void);
282
283
284extern void
285_glthread_InitTSD(_glthread_TSD *);
286
287
288extern void *
289_glthread_GetTSD(_glthread_TSD *);
290
291
292extern void
293_glthread_SetTSD(_glthread_TSD *, void *);
294
Ian Romanick25fe93f2005-04-13 20:59:15 +0000295#if defined(GLX_USE_TLS)
296
297extern __thread struct _glapi_table * _glapi_tls_Dispatch
298 __attribute__((tls_model("initial-exec")));
299
Ian Romanick9bdfee32005-07-18 12:31:24 +0000300#define GET_DISPATCH() _glapi_tls_Dispatch
Ian Romanick25fe93f2005-04-13 20:59:15 +0000301
302#elif !defined(GL_CALL)
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000303# if defined(THREADS)
Ian Romanick9bdfee32005-07-18 12:31:24 +0000304# define GET_DISPATCH() \
Ian Romanick967b0062005-08-10 23:54:15 +0000305 ((__builtin_expect( _glapi_Dispatch != NULL, 1 )) \
306 ? _glapi_Dispatch : _glapi_get_dispatch())
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000307# else
Ian Romanick9bdfee32005-07-18 12:31:24 +0000308# define GET_DISPATCH() _glapi_Dispatch
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000309# endif /* defined(THREADS) */
310#endif /* ndef GL_CALL */
Brian Paula9601f12000-02-10 21:27:25 +0000311
312
Brian Paulbc794052000-01-31 23:10:47 +0000313#endif /* THREADS_H */