blob: 1b2a648da8164cbeb12ff269fd06cb418161a5df [file] [log] [blame]
Brian Paul5104b4d2002-03-07 21:50:41 +00001/* $Id: glthread.h,v 1.11 2002/03/07 21:50:41 brianp Exp $ */
Brian Paulc11371a1999-12-16 17:31:06 +00002
3/*
4 * Mesa 3-D graphics library
Gareth Hughes22144ab2001-03-12 00:48:37 +00005 * Version: 3.5
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00006 *
Gareth Hughes22144ab2001-03-12 00:48:37 +00007 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00008 *
Brian Paulc11371a1999-12-16 17:31:06 +00009 * 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:
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +000015 *
Brian Paulc11371a1999-12-16 17:31:06 +000016 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +000018 *
Brian Paulc11371a1999-12-16 17:31:06 +000019 * 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 Paula9601f12000-02-10 21:27:25 +000035 *
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 Paulc11371a1999-12-16 17:31:06 +000057 */
58
Brian Paulc11371a1999-12-16 17:31:06 +000059/*
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +000060 * If this file is accidentally included by a non-threaded build,
Brian Paulc11371a1999-12-16 17:31:06 +000061 * it should not cause the build to fail, or otherwise cause problems.
62 * In general, it should only be included when needed however.
63 */
Brian Paula360ab22000-02-10 21:54:06 +000064
Brian Paula360ab22000-02-10 21:54:06 +000065#ifndef GLTHREAD_H
66#define GLTHREAD_H
67
68
69#if defined(PTHREADS) || defined(SOLARIS_THREADS) || defined(WIN32_THREADS) || defined(XTHREADS)
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
104#define _glthread_LOCK_MUTEX(name) \
105 (void) pthread_mutex_lock(&(name))
106
107#define _glthread_UNLOCK_MUTEX(name) \
108 (void) pthread_mutex_unlock(&(name))
109
Brian Paulc11371a1999-12-16 17:31:06 +0000110#endif /* PTHREADS */
111
112
113
114
115/*
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +0000116 * Solaris threads. Use only up to Solaris 2.4.
Brian Paulc11371a1999-12-16 17:31:06 +0000117 * Solaris 2.5 and higher provide POSIX threads.
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +0000118 * Be sure to compile with -mt on the Solaris compilers, or
Brian Paulc11371a1999-12-16 17:31:06 +0000119 * use -D_REENTRANT if using gcc.
120 */
121#ifdef SOLARIS_THREADS
122#include <thread.h>
123
124typedef struct {
125 thread_key_t key;
126 mutex_t keylock;
Brian Paula9601f12000-02-10 21:27:25 +0000127 int initMagic;
Brian Paulc11371a1999-12-16 17:31:06 +0000128} _glthread_TSD;
129
Brian Paulc11371a1999-12-16 17:31:06 +0000130typedef thread_t _glthread_Thread;
131
Brian Paulbc794052000-01-31 23:10:47 +0000132typedef mutex_t _glthread_Mutex;
133
134/* XXX need to really implement mutex-related macros */
135#define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
136#define _glthread_INIT_MUTEX(name) (void) name
137#define _glthread_LOCK_MUTEX(name) (void) name
138#define _glthread_UNLOCK_MUTEX(name) (void) name
139
Brian Paulc11371a1999-12-16 17:31:06 +0000140#endif /* SOLARIS_THREADS */
141
142
143
144
145/*
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +0000146 * Windows threads. Should work with Windows NT and 95.
Brian Paulc11371a1999-12-16 17:31:06 +0000147 * IMPORTANT: Link with multithreaded runtime library when THREADS are
148 * used!
149 */
Brian Paulfa937f62000-02-11 21:38:33 +0000150#ifdef WIN32_THREADS
Brian Paulc11371a1999-12-16 17:31:06 +0000151#include <windows.h>
152
153typedef struct {
154 DWORD key;
Brian Paula9601f12000-02-10 21:27:25 +0000155 int initMagic;
Brian Paulc11371a1999-12-16 17:31:06 +0000156} _glthread_TSD;
157
Brian Paulc11371a1999-12-16 17:31:06 +0000158typedef HANDLE _glthread_Thread;
159
Brian Paulbc794052000-01-31 23:10:47 +0000160typedef CRITICAL_SECTION _glthread_Mutex;
161
162/* XXX need to really implement mutex-related macros */
163#define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
164#define _glthread_INIT_MUTEX(name) (void) name
165#define _glthread_LOCK_MUTEX(name) (void) name
166#define _glthread_UNLOCK_MUTEX(name) (void) name
167
Brian Paulfa937f62000-02-11 21:38:33 +0000168#endif /* WIN32_THREADS */
Brian Paulc11371a1999-12-16 17:31:06 +0000169
170
171
172
173/*
Brian Paula9601f12000-02-10 21:27:25 +0000174 * XFree86 has its own thread wrapper, Xthreads.h
175 * We wrap it again for GL.
Brian Paulc11371a1999-12-16 17:31:06 +0000176 */
Brian Paula9601f12000-02-10 21:27:25 +0000177#ifdef XTHREADS
178#include "Xthreads.h"
Brian Paulc11371a1999-12-16 17:31:06 +0000179
Brian Paula9601f12000-02-10 21:27:25 +0000180typedef struct {
181 xthread_key_t key;
182 int initMagic;
183} _glthread_TSD;
Brian Paulc11371a1999-12-16 17:31:06 +0000184
Brian Paula9601f12000-02-10 21:27:25 +0000185typedef xthread_t _glthread_Thread;
Brian Paulc11371a1999-12-16 17:31:06 +0000186
Brian Paula9601f12000-02-10 21:27:25 +0000187typedef xmutex_rec _glthread_Mutex;
Brian Paulc11371a1999-12-16 17:31:06 +0000188
Brian Paul5104b4d2002-03-07 21:50:41 +0000189#ifdef XMUTEX_INITIALIZER
Brian Paula9601f12000-02-10 21:27:25 +0000190#define _glthread_DECLARE_STATIC_MUTEX(name) \
191 static _glthread_Mutex name = XMUTEX_INITIALIZER
Brian Paul5104b4d2002-03-07 21:50:41 +0000192#else
193#define _glthread_DECLARE_STATIC_MUTEX(name) \
194 static _glthread_Mutex name
195#endif
Brian Paulc11371a1999-12-16 17:31:06 +0000196
Brian Paula9601f12000-02-10 21:27:25 +0000197#define _glthread_INIT_MUTEX(name) \
198 xmutex_init(&(name))
Brian Paulc11371a1999-12-16 17:31:06 +0000199
Brian Paula9601f12000-02-10 21:27:25 +0000200#define _glthread_LOCK_MUTEX(name) \
201 (void) xmutex_lock(&(name))
Brian Paulc11371a1999-12-16 17:31:06 +0000202
Brian Paula9601f12000-02-10 21:27:25 +0000203#define _glthread_UNLOCK_MUTEX(name) \
204 (void) xmutex_unlock(&(name))
205
206#endif /* XTHREADS */
Brian Paulc11371a1999-12-16 17:31:06 +0000207
208
209
Brian Paul1b37d6c2001-11-12 23:50:12 +0000210/*
211 * BeOS threads. R5.x required.
212 */
213#ifdef BEOS_THREADS
214#include <kernel/OS.h>
215#include <support/TLS.h>
216
217typedef struct {
218 int32 key;
219 int initMagic;
220} _glthread_TSD;
221
222typedef thread_id _glthread_Thread;
223
224/* Use Benaphore, aka speeder semaphore */
225typedef struct {
226 int32 lock;
227 sem_id sem;
228} benaphore;
229typedef benaphore _glthread_Mutex;
230
231#define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = { 0,
232create_sem(0, #name"_benaphore") }
Karl Schultze6373ba2001-11-30 22:11:45 +0000233#define _glthread_INIT_MUTEX(name) name.sem = create_sem(0, #name"_benaphore"), name.lock = 0
234#define _glthread_LOCK_MUTEX(name) if((atomic_add(&(name.lock), 1)) >= 1) acquire_sem(name.sem)
235#define _glthread_UNLOCK_MUTEX(name) if((atomic_add(&(name.lock), -1)) > 1) release_sem(name.sem)
Brian Paul1b37d6c2001-11-12 23:50:12 +0000236
237#endif /* BEOS_THREADS */
238
239
Brian Paulbc794052000-01-31 23:10:47 +0000240
Brian Paula360ab22000-02-10 21:54:06 +0000241#ifndef THREADS
Brian Paulbc794052000-01-31 23:10:47 +0000242
243/*
244 * THREADS not defined
245 */
246
247typedef GLuint _glthread_TSD;
248
249typedef GLuint _glthread_Thread;
250
251typedef GLuint _glthread_Mutex;
252
253#define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
254
255#define _glthread_INIT_MUTEX(name) (void) name
256
257#define _glthread_LOCK_MUTEX(name) (void) name
258
259#define _glthread_UNLOCK_MUTEX(name) (void) name
260
Brian Paulbc794052000-01-31 23:10:47 +0000261#endif /* THREADS */
262
Brian Paula9601f12000-02-10 21:27:25 +0000263
264
265/*
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +0000266 * Platform independent thread specific data API.
Brian Paula9601f12000-02-10 21:27:25 +0000267 */
268
269extern unsigned long
270_glthread_GetID(void);
271
272
273extern void
274_glthread_InitTSD(_glthread_TSD *);
275
276
277extern void *
278_glthread_GetTSD(_glthread_TSD *);
279
280
281extern void
282_glthread_SetTSD(_glthread_TSD *, void *);
283
284
285
Brian Paulbc794052000-01-31 23:10:47 +0000286#endif /* THREADS_H */