blob: b31b57940590065e6cd44a4eee2746a546aaea12 [file] [log] [blame]
Brian Paulfa937f62000-02-11 21:38:33 +00001/* $Id: glthread.h,v 1.5 2000/02/11 21:38:33 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 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
65
66#ifndef GLTHREAD_H
67#define GLTHREAD_H
68
69
70#if defined(PTHREADS) || defined(SOLARIS_THREADS) || defined(WIN32_THREADS) || defined(XTHREADS)
71#define THREADS
Brian Paulc11371a1999-12-16 17:31:06 +000072#endif
73
74
75
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
103#define _glthread_LOCK_MUTEX(name) \
104 (void) pthread_mutex_lock(&(name))
105
106#define _glthread_UNLOCK_MUTEX(name) \
107 (void) pthread_mutex_unlock(&(name))
108
Brian Paulc11371a1999-12-16 17:31:06 +0000109#endif /* PTHREADS */
110
111
112
113
114/*
115 * Solaris threads. Use only up to Solaris 2.4.
116 * Solaris 2.5 and higher provide POSIX threads.
117 * Be sure to compile with -mt on the Solaris compilers, or
118 * use -D_REENTRANT if using gcc.
119 */
120#ifdef SOLARIS_THREADS
121#include <thread.h>
122
123typedef struct {
124 thread_key_t key;
125 mutex_t keylock;
Brian Paula9601f12000-02-10 21:27:25 +0000126 int initMagic;
Brian Paulc11371a1999-12-16 17:31:06 +0000127} _glthread_TSD;
128
Brian Paulc11371a1999-12-16 17:31:06 +0000129typedef thread_t _glthread_Thread;
130
Brian Paulbc794052000-01-31 23:10:47 +0000131typedef mutex_t _glthread_Mutex;
132
133/* XXX need to really implement mutex-related macros */
134#define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
135#define _glthread_INIT_MUTEX(name) (void) name
136#define _glthread_LOCK_MUTEX(name) (void) name
137#define _glthread_UNLOCK_MUTEX(name) (void) name
138
Brian Paulc11371a1999-12-16 17:31:06 +0000139#endif /* SOLARIS_THREADS */
140
141
142
143
144/*
145 * Windows threads. Should work with Windows NT and 95.
146 * IMPORTANT: Link with multithreaded runtime library when THREADS are
147 * used!
148 */
Brian Paulfa937f62000-02-11 21:38:33 +0000149#ifdef WIN32_THREADS
Brian Paulc11371a1999-12-16 17:31:06 +0000150#include <windows.h>
151
152typedef struct {
153 DWORD key;
Brian Paula9601f12000-02-10 21:27:25 +0000154 int initMagic;
Brian Paulc11371a1999-12-16 17:31:06 +0000155} _glthread_TSD;
156
Brian Paulc11371a1999-12-16 17:31:06 +0000157typedef HANDLE _glthread_Thread;
158
Brian Paulbc794052000-01-31 23:10:47 +0000159typedef CRITICAL_SECTION _glthread_Mutex;
160
161/* XXX need to really implement mutex-related macros */
162#define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
163#define _glthread_INIT_MUTEX(name) (void) name
164#define _glthread_LOCK_MUTEX(name) (void) name
165#define _glthread_UNLOCK_MUTEX(name) (void) name
166
Brian Paulfa937f62000-02-11 21:38:33 +0000167#endif /* WIN32_THREADS */
Brian Paulc11371a1999-12-16 17:31:06 +0000168
169
170
171
172/*
Brian Paula9601f12000-02-10 21:27:25 +0000173 * XFree86 has its own thread wrapper, Xthreads.h
174 * We wrap it again for GL.
Brian Paulc11371a1999-12-16 17:31:06 +0000175 */
Brian Paula9601f12000-02-10 21:27:25 +0000176#ifdef XTHREADS
177#include "Xthreads.h"
Brian Paulc11371a1999-12-16 17:31:06 +0000178
Brian Paula9601f12000-02-10 21:27:25 +0000179typedef struct {
180 xthread_key_t key;
181 int initMagic;
182} _glthread_TSD;
Brian Paulc11371a1999-12-16 17:31:06 +0000183
Brian Paula9601f12000-02-10 21:27:25 +0000184typedef xthread_t _glthread_Thread;
Brian Paulc11371a1999-12-16 17:31:06 +0000185
Brian Paula9601f12000-02-10 21:27:25 +0000186typedef xmutex_rec _glthread_Mutex;
Brian Paulc11371a1999-12-16 17:31:06 +0000187
Brian Paula9601f12000-02-10 21:27:25 +0000188#define _glthread_DECLARE_STATIC_MUTEX(name) \
189 static _glthread_Mutex name = XMUTEX_INITIALIZER
Brian Paulc11371a1999-12-16 17:31:06 +0000190
Brian Paula9601f12000-02-10 21:27:25 +0000191#define _glthread_INIT_MUTEX(name) \
192 xmutex_init(&(name))
Brian Paulc11371a1999-12-16 17:31:06 +0000193
Brian Paula9601f12000-02-10 21:27:25 +0000194#define _glthread_LOCK_MUTEX(name) \
195 (void) xmutex_lock(&(name))
Brian Paulc11371a1999-12-16 17:31:06 +0000196
Brian Paula9601f12000-02-10 21:27:25 +0000197#define _glthread_UNLOCK_MUTEX(name) \
198 (void) xmutex_unlock(&(name))
199
200#endif /* XTHREADS */
Brian Paulc11371a1999-12-16 17:31:06 +0000201
202
203
Brian Paulbc794052000-01-31 23:10:47 +0000204
Brian Paula360ab22000-02-10 21:54:06 +0000205#ifndef THREADS
Brian Paulbc794052000-01-31 23:10:47 +0000206
207/*
208 * THREADS not defined
209 */
210
211typedef GLuint _glthread_TSD;
212
213typedef GLuint _glthread_Thread;
214
215typedef GLuint _glthread_Mutex;
216
217#define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
218
219#define _glthread_INIT_MUTEX(name) (void) name
220
221#define _glthread_LOCK_MUTEX(name) (void) name
222
223#define _glthread_UNLOCK_MUTEX(name) (void) name
224
Brian Paulbc794052000-01-31 23:10:47 +0000225#endif /* THREADS */
226
Brian Paula9601f12000-02-10 21:27:25 +0000227
228
229/*
230 * Platform independent thread specific data API.
231 */
232
233extern unsigned long
234_glthread_GetID(void);
235
236
237extern void
238_glthread_InitTSD(_glthread_TSD *);
239
240
241extern void *
242_glthread_GetTSD(_glthread_TSD *);
243
244
245extern void
246_glthread_SetTSD(_glthread_TSD *, void *);
247
248
249
Brian Paulbc794052000-01-31 23:10:47 +0000250#endif /* THREADS_H */
251