blob: 72fe08ff7f9c0db72b06bdffc7643f7706b887e2 [file] [log] [blame]
Brian Paula9601f12000-02-10 21:27:25 +00001/* $Id: glthread.h,v 1.3 2000/02/10 21:27:25 brianp 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 Paula9601f12000-02-10 21:27:25 +000059
Brian Paulbc794052000-01-31 23:10:47 +000060#ifndef GLTHREAD_H
61#define GLTHREAD_H
Brian Paulc11371a1999-12-16 17:31:06 +000062
63
64/*
65 * If this file is accidentally included by a non-threaded build,
66 * it should not cause the build to fail, or otherwise cause problems.
67 * In general, it should only be included when needed however.
68 */
69#ifdef THREADS
70/*
71 * It is an error not to select a specific threads API when compiling.
72 */
Brian Paula9601f12000-02-10 21:27:25 +000073#if !defined(PTHREADS) && !defined(SOLARIS_THREADS) && !defined(WIN32) && !defined(XTHREADS)
74#error One of PTHREADS, SOLARIS_THREADS, WIN32 or XTHREADS must be defined.
Brian Paulc11371a1999-12-16 17:31:06 +000075#endif
76
77
78
79/*
80 * POSIX threads. This should be your choice in the Unix world
81 * whenever possible. When building with POSIX threads, be sure
82 * to enable any compiler flags which will cause the MT-safe
83 * libc (if one exists) to be used when linking, as well as any
84 * header macros for MT-safe errno, etc. For Solaris, this is the -mt
85 * compiler flag. On Solaris with gcc, use -D_REENTRANT to enable
86 * proper compiling for MT-safe libc etc.
87 */
88#ifdef PTHREADS
89#include <pthread.h> /* POSIX threads headers */
90
91typedef struct {
92 pthread_key_t key;
Brian Paula9601f12000-02-10 21:27:25 +000093 int initMagic;
Brian Paulc11371a1999-12-16 17:31:06 +000094} _glthread_TSD;
95
Brian Paulc11371a1999-12-16 17:31:06 +000096typedef pthread_t _glthread_Thread;
97
Brian Paulbc794052000-01-31 23:10:47 +000098typedef pthread_mutex_t _glthread_Mutex;
99
100#define _glthread_DECLARE_STATIC_MUTEX(name) \
101 static _glthread_Mutex name = PTHREAD_MUTEX_INITIALIZER
102
103#define _glthread_INIT_MUTEX(name) \
104 pthread_mutex_init(&(name), NULL)
105
106#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
Brian Paulc11371a1999-12-16 17:31:06 +0000112#endif /* PTHREADS */
113
114
115
116
117/*
118 * Solaris threads. Use only up to Solaris 2.4.
119 * Solaris 2.5 and higher provide POSIX threads.
120 * Be sure to compile with -mt on the Solaris compilers, or
121 * use -D_REENTRANT if using gcc.
122 */
123#ifdef SOLARIS_THREADS
124#include <thread.h>
125
126typedef struct {
127 thread_key_t key;
128 mutex_t keylock;
Brian Paula9601f12000-02-10 21:27:25 +0000129 int initMagic;
Brian Paulc11371a1999-12-16 17:31:06 +0000130} _glthread_TSD;
131
Brian Paulc11371a1999-12-16 17:31:06 +0000132typedef thread_t _glthread_Thread;
133
Brian Paulbc794052000-01-31 23:10:47 +0000134typedef mutex_t _glthread_Mutex;
135
136/* XXX need to really implement mutex-related macros */
137#define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
138#define _glthread_INIT_MUTEX(name) (void) name
139#define _glthread_LOCK_MUTEX(name) (void) name
140#define _glthread_UNLOCK_MUTEX(name) (void) name
141
Brian Paulc11371a1999-12-16 17:31:06 +0000142#endif /* SOLARIS_THREADS */
143
144
145
146
147/*
148 * Windows threads. Should work with Windows NT and 95.
149 * IMPORTANT: Link with multithreaded runtime library when THREADS are
150 * used!
151 */
Brian Paulc11371a1999-12-16 17:31:06 +0000152#ifdef WIN32
153#include <windows.h>
154
155typedef struct {
156 DWORD key;
Brian Paula9601f12000-02-10 21:27:25 +0000157 int initMagic;
Brian Paulc11371a1999-12-16 17:31:06 +0000158} _glthread_TSD;
159
Brian Paulc11371a1999-12-16 17:31:06 +0000160typedef HANDLE _glthread_Thread;
161
Brian Paulbc794052000-01-31 23:10:47 +0000162typedef CRITICAL_SECTION _glthread_Mutex;
163
164/* XXX need to really implement mutex-related macros */
165#define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
166#define _glthread_INIT_MUTEX(name) (void) name
167#define _glthread_LOCK_MUTEX(name) (void) name
168#define _glthread_UNLOCK_MUTEX(name) (void) name
169
Brian Paulc11371a1999-12-16 17:31:06 +0000170#endif /* WIN32 */
171
172
173
174
175/*
Brian Paula9601f12000-02-10 21:27:25 +0000176 * XFree86 has its own thread wrapper, Xthreads.h
177 * We wrap it again for GL.
Brian Paulc11371a1999-12-16 17:31:06 +0000178 */
Brian Paula9601f12000-02-10 21:27:25 +0000179#ifdef XTHREADS
180#include "Xthreads.h"
Brian Paulc11371a1999-12-16 17:31:06 +0000181
Brian Paula9601f12000-02-10 21:27:25 +0000182typedef struct {
183 xthread_key_t key;
184 int initMagic;
185} _glthread_TSD;
Brian Paulc11371a1999-12-16 17:31:06 +0000186
Brian Paula9601f12000-02-10 21:27:25 +0000187typedef xthread_t _glthread_Thread;
Brian Paulc11371a1999-12-16 17:31:06 +0000188
Brian Paula9601f12000-02-10 21:27:25 +0000189typedef xmutex_rec _glthread_Mutex;
Brian Paulc11371a1999-12-16 17:31:06 +0000190
Brian Paula9601f12000-02-10 21:27:25 +0000191#define _glthread_DECLARE_STATIC_MUTEX(name) \
192 static _glthread_Mutex name = XMUTEX_INITIALIZER
Brian Paulc11371a1999-12-16 17:31:06 +0000193
Brian Paula9601f12000-02-10 21:27:25 +0000194#define _glthread_INIT_MUTEX(name) \
195 xmutex_init(&(name))
Brian Paulc11371a1999-12-16 17:31:06 +0000196
Brian Paula9601f12000-02-10 21:27:25 +0000197#define _glthread_LOCK_MUTEX(name) \
198 (void) xmutex_lock(&(name))
Brian Paulc11371a1999-12-16 17:31:06 +0000199
Brian Paula9601f12000-02-10 21:27:25 +0000200#define _glthread_UNLOCK_MUTEX(name) \
201 (void) xmutex_unlock(&(name))
202
203#endif /* XTHREADS */
Brian Paulc11371a1999-12-16 17:31:06 +0000204
205
206
Brian Paulbc794052000-01-31 23:10:47 +0000207#else /* THREADS */
208
209
210/*
211 * THREADS not defined
212 */
213
214typedef GLuint _glthread_TSD;
215
216typedef GLuint _glthread_Thread;
217
218typedef GLuint _glthread_Mutex;
219
220#define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
221
222#define _glthread_INIT_MUTEX(name) (void) name
223
224#define _glthread_LOCK_MUTEX(name) (void) name
225
226#define _glthread_UNLOCK_MUTEX(name) (void) name
227
228
229#endif /* THREADS */
230
Brian Paula9601f12000-02-10 21:27:25 +0000231
232
233/*
234 * Platform independent thread specific data API.
235 */
236
237extern unsigned long
238_glthread_GetID(void);
239
240
241extern void
242_glthread_InitTSD(_glthread_TSD *);
243
244
245extern void *
246_glthread_GetTSD(_glthread_TSD *);
247
248
249extern void
250_glthread_SetTSD(_glthread_TSD *, void *);
251
252
253
254
Brian Paulbc794052000-01-31 23:10:47 +0000255#endif /* THREADS_H */
256