blob: 726811f696bdb1a70454dd9c340db6cc104d7252 [file] [log] [blame]
Jouk Jansen9e83e8c2000-11-17 11:00:55 +00001/* $Id: glthread.h,v 1.6 2000/11/17 11:00:56 joukj Exp $ */
Brian Paulc11371a1999-12-16 17:31:06 +00002
3/*
4 * Mesa 3-D graphics library
5 * Version: 3.3
6 *
Brian Paula9601f12000-02-10 21:27:25 +00007 * Copyright (C) 1999-2000 Brian Paul All Rights Reserved.
Brian Paulc11371a1999-12-16 17:31:06 +00008 *
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 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/*
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 */
Brian Paula360ab22000-02-10 21:54:06 +000064
Jouk Jansen9e83e8c2000-11-17 11:00:55 +000065#ifdef VMS
66# if defined(PTHREADS)
67#include "types.h"
68#endif
69#endif
Brian Paula360ab22000-02-10 21:54:06 +000070
71#ifndef GLTHREAD_H
72#define GLTHREAD_H
73
74
75#if defined(PTHREADS) || defined(SOLARIS_THREADS) || defined(WIN32_THREADS) || defined(XTHREADS)
76#define THREADS
Brian Paulc11371a1999-12-16 17:31:06 +000077#endif
78
Jouk Jansen9e83e8c2000-11-17 11:00:55 +000079#ifdef VMS
80#include <GL/vms_x_fix.h>
81#endif
Brian Paulc11371a1999-12-16 17:31:06 +000082
83/*
84 * POSIX threads. This should be your choice in the Unix world
85 * whenever possible. When building with POSIX threads, be sure
86 * to enable any compiler flags which will cause the MT-safe
87 * libc (if one exists) to be used when linking, as well as any
88 * header macros for MT-safe errno, etc. For Solaris, this is the -mt
89 * compiler flag. On Solaris with gcc, use -D_REENTRANT to enable
90 * proper compiling for MT-safe libc etc.
91 */
Brian Paula360ab22000-02-10 21:54:06 +000092#if defined(PTHREADS)
Brian Paulc11371a1999-12-16 17:31:06 +000093#include <pthread.h> /* POSIX threads headers */
94
95typedef struct {
96 pthread_key_t key;
Brian Paula9601f12000-02-10 21:27:25 +000097 int initMagic;
Brian Paulc11371a1999-12-16 17:31:06 +000098} _glthread_TSD;
99
Brian Paulc11371a1999-12-16 17:31:06 +0000100typedef pthread_t _glthread_Thread;
101
Brian Paulbc794052000-01-31 23:10:47 +0000102typedef pthread_mutex_t _glthread_Mutex;
103
104#define _glthread_DECLARE_STATIC_MUTEX(name) \
105 static _glthread_Mutex name = PTHREAD_MUTEX_INITIALIZER
106
107#define _glthread_INIT_MUTEX(name) \
108 pthread_mutex_init(&(name), NULL)
109
110#define _glthread_LOCK_MUTEX(name) \
111 (void) pthread_mutex_lock(&(name))
112
113#define _glthread_UNLOCK_MUTEX(name) \
114 (void) pthread_mutex_unlock(&(name))
115
Brian Paulc11371a1999-12-16 17:31:06 +0000116#endif /* PTHREADS */
117
118
119
120
121/*
122 * Solaris threads. Use only up to Solaris 2.4.
123 * Solaris 2.5 and higher provide POSIX threads.
124 * Be sure to compile with -mt on the Solaris compilers, or
125 * use -D_REENTRANT if using gcc.
126 */
127#ifdef SOLARIS_THREADS
128#include <thread.h>
129
130typedef struct {
131 thread_key_t key;
132 mutex_t keylock;
Brian Paula9601f12000-02-10 21:27:25 +0000133 int initMagic;
Brian Paulc11371a1999-12-16 17:31:06 +0000134} _glthread_TSD;
135
Brian Paulc11371a1999-12-16 17:31:06 +0000136typedef thread_t _glthread_Thread;
137
Brian Paulbc794052000-01-31 23:10:47 +0000138typedef mutex_t _glthread_Mutex;
139
140/* XXX need to really implement mutex-related macros */
141#define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
142#define _glthread_INIT_MUTEX(name) (void) name
143#define _glthread_LOCK_MUTEX(name) (void) name
144#define _glthread_UNLOCK_MUTEX(name) (void) name
145
Brian Paulc11371a1999-12-16 17:31:06 +0000146#endif /* SOLARIS_THREADS */
147
148
149
150
151/*
152 * Windows threads. Should work with Windows NT and 95.
153 * IMPORTANT: Link with multithreaded runtime library when THREADS are
154 * used!
155 */
Brian Paulfa937f62000-02-11 21:38:33 +0000156#ifdef WIN32_THREADS
Brian Paulc11371a1999-12-16 17:31:06 +0000157#include <windows.h>
158
159typedef struct {
160 DWORD key;
Brian Paula9601f12000-02-10 21:27:25 +0000161 int initMagic;
Brian Paulc11371a1999-12-16 17:31:06 +0000162} _glthread_TSD;
163
Brian Paulc11371a1999-12-16 17:31:06 +0000164typedef HANDLE _glthread_Thread;
165
Brian Paulbc794052000-01-31 23:10:47 +0000166typedef CRITICAL_SECTION _glthread_Mutex;
167
168/* XXX need to really implement mutex-related macros */
169#define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
170#define _glthread_INIT_MUTEX(name) (void) name
171#define _glthread_LOCK_MUTEX(name) (void) name
172#define _glthread_UNLOCK_MUTEX(name) (void) name
173
Brian Paulfa937f62000-02-11 21:38:33 +0000174#endif /* WIN32_THREADS */
Brian Paulc11371a1999-12-16 17:31:06 +0000175
176
177
178
179/*
Brian Paula9601f12000-02-10 21:27:25 +0000180 * XFree86 has its own thread wrapper, Xthreads.h
181 * We wrap it again for GL.
Brian Paulc11371a1999-12-16 17:31:06 +0000182 */
Brian Paula9601f12000-02-10 21:27:25 +0000183#ifdef XTHREADS
184#include "Xthreads.h"
Brian Paulc11371a1999-12-16 17:31:06 +0000185
Brian Paula9601f12000-02-10 21:27:25 +0000186typedef struct {
187 xthread_key_t key;
188 int initMagic;
189} _glthread_TSD;
Brian Paulc11371a1999-12-16 17:31:06 +0000190
Brian Paula9601f12000-02-10 21:27:25 +0000191typedef xthread_t _glthread_Thread;
Brian Paulc11371a1999-12-16 17:31:06 +0000192
Brian Paula9601f12000-02-10 21:27:25 +0000193typedef xmutex_rec _glthread_Mutex;
Brian Paulc11371a1999-12-16 17:31:06 +0000194
Brian Paula9601f12000-02-10 21:27:25 +0000195#define _glthread_DECLARE_STATIC_MUTEX(name) \
196 static _glthread_Mutex name = XMUTEX_INITIALIZER
Brian Paulc11371a1999-12-16 17:31:06 +0000197
Brian Paula9601f12000-02-10 21:27:25 +0000198#define _glthread_INIT_MUTEX(name) \
199 xmutex_init(&(name))
Brian Paulc11371a1999-12-16 17:31:06 +0000200
Brian Paula9601f12000-02-10 21:27:25 +0000201#define _glthread_LOCK_MUTEX(name) \
202 (void) xmutex_lock(&(name))
Brian Paulc11371a1999-12-16 17:31:06 +0000203
Brian Paula9601f12000-02-10 21:27:25 +0000204#define _glthread_UNLOCK_MUTEX(name) \
205 (void) xmutex_unlock(&(name))
206
207#endif /* XTHREADS */
Brian Paulc11371a1999-12-16 17:31:06 +0000208
209
210
Brian Paulbc794052000-01-31 23:10:47 +0000211
Brian Paula360ab22000-02-10 21:54:06 +0000212#ifndef THREADS
Brian Paulbc794052000-01-31 23:10:47 +0000213
214/*
215 * THREADS not defined
216 */
217
218typedef GLuint _glthread_TSD;
219
220typedef GLuint _glthread_Thread;
221
222typedef GLuint _glthread_Mutex;
223
224#define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
225
226#define _glthread_INIT_MUTEX(name) (void) name
227
228#define _glthread_LOCK_MUTEX(name) (void) name
229
230#define _glthread_UNLOCK_MUTEX(name) (void) name
231
Brian Paulbc794052000-01-31 23:10:47 +0000232#endif /* THREADS */
233
Brian Paula9601f12000-02-10 21:27:25 +0000234
235
236/*
237 * Platform independent thread specific data API.
238 */
239
240extern unsigned long
241_glthread_GetID(void);
242
243
244extern void
245_glthread_InitTSD(_glthread_TSD *);
246
247
248extern void *
249_glthread_GetTSD(_glthread_TSD *);
250
251
252extern void
253_glthread_SetTSD(_glthread_TSD *, void *);
254
255
256
Brian Paulbc794052000-01-31 23:10:47 +0000257#endif /* THREADS_H */
258