blob: 600a7616823cd930376b8ca08c290e6086aaeca2 [file] [log] [blame]
Brian Paulbc794052000-01-31 23:10:47 +00001/* $Id: glthread.h,v 1.2 2000/01/31 23:10:47 brianp Exp $ */
Brian Paulc11371a1999-12-16 17:31:06 +00002
3/*
4 * Mesa 3-D graphics library
5 * Version: 3.3
6 *
7 * Copyright (C) 1999 Brian Paul All Rights Reserved.
8 *
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
35 */
36
Brian Paulbc794052000-01-31 23:10:47 +000037#ifndef GLTHREAD_H
38#define GLTHREAD_H
Brian Paulc11371a1999-12-16 17:31:06 +000039
40
41/*
42 * If this file is accidentally included by a non-threaded build,
43 * it should not cause the build to fail, or otherwise cause problems.
44 * In general, it should only be included when needed however.
45 */
46#ifdef THREADS
47/*
48 * It is an error not to select a specific threads API when compiling.
49 */
50#if !defined(PTHREADS) && !defined(SOLARIS_THREADS) && !defined(WIN32)
51#error One of PTHREADS, SOLARIS_THREADS or WIN32 must be defined.
52#endif
53
54
55
56/*
57 * POSIX threads. This should be your choice in the Unix world
58 * whenever possible. When building with POSIX threads, be sure
59 * to enable any compiler flags which will cause the MT-safe
60 * libc (if one exists) to be used when linking, as well as any
61 * header macros for MT-safe errno, etc. For Solaris, this is the -mt
62 * compiler flag. On Solaris with gcc, use -D_REENTRANT to enable
63 * proper compiling for MT-safe libc etc.
64 */
65#ifdef PTHREADS
66#include <pthread.h> /* POSIX threads headers */
67
68typedef struct {
69 pthread_key_t key;
70 pthread_once_t once;
71} _glthread_TSD;
72
Brian Paulc11371a1999-12-16 17:31:06 +000073typedef pthread_t _glthread_Thread;
74
Brian Paulbc794052000-01-31 23:10:47 +000075typedef pthread_mutex_t _glthread_Mutex;
76
77#define _glthread_DECLARE_STATIC_MUTEX(name) \
78 static _glthread_Mutex name = PTHREAD_MUTEX_INITIALIZER
79
80#define _glthread_INIT_MUTEX(name) \
81 pthread_mutex_init(&(name), NULL)
82
83#define _glthread_LOCK_MUTEX(name) \
84 (void) pthread_mutex_lock(&(name))
85
86#define _glthread_UNLOCK_MUTEX(name) \
87 (void) pthread_mutex_unlock(&(name))
88
Brian Paulc11371a1999-12-16 17:31:06 +000089#endif /* PTHREADS */
90
91
92
93
94/*
95 * Solaris threads. Use only up to Solaris 2.4.
96 * Solaris 2.5 and higher provide POSIX threads.
97 * Be sure to compile with -mt on the Solaris compilers, or
98 * use -D_REENTRANT if using gcc.
99 */
100#ifdef SOLARIS_THREADS
101#include <thread.h>
102
103typedef struct {
104 thread_key_t key;
105 mutex_t keylock;
106 int initfuncCalled;
107} _glthread_TSD;
108
Brian Paulc11371a1999-12-16 17:31:06 +0000109typedef thread_t _glthread_Thread;
110
Brian Paulbc794052000-01-31 23:10:47 +0000111typedef mutex_t _glthread_Mutex;
112
113/* XXX need to really implement mutex-related macros */
114#define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
115#define _glthread_INIT_MUTEX(name) (void) name
116#define _glthread_LOCK_MUTEX(name) (void) name
117#define _glthread_UNLOCK_MUTEX(name) (void) name
118
Brian Paulc11371a1999-12-16 17:31:06 +0000119#endif /* SOLARIS_THREADS */
120
121
122
123
124/*
125 * Windows threads. Should work with Windows NT and 95.
126 * IMPORTANT: Link with multithreaded runtime library when THREADS are
127 * used!
128 */
129
130#ifdef WIN32
131#include <windows.h>
132
133typedef struct {
134 DWORD key;
135 int initfuncCalled;
136} _glthread_TSD;
137
Brian Paulc11371a1999-12-16 17:31:06 +0000138typedef HANDLE _glthread_Thread;
139
Brian Paulbc794052000-01-31 23:10:47 +0000140typedef CRITICAL_SECTION _glthread_Mutex;
141
142/* XXX need to really implement mutex-related macros */
143#define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
144#define _glthread_INIT_MUTEX(name) (void) name
145#define _glthread_LOCK_MUTEX(name) (void) name
146#define _glthread_UNLOCK_MUTEX(name) (void) name
147
Brian Paulc11371a1999-12-16 17:31:06 +0000148#endif /* WIN32 */
149
150
151
152
Brian Paulbc794052000-01-31 23:10:47 +0000153
Brian Paulc11371a1999-12-16 17:31:06 +0000154/*
155 * Platform independent thread specific data API.
156 */
157
158extern unsigned long
159_glthread_GetID(void);
160
161
162extern void
163_glthread_InitTSD(_glthread_TSD *);
164
165
166extern void *
167_glthread_GetTSD(_glthread_TSD *);
168
169
170extern void
171_glthread_SetTSD(_glthread_TSD *, void *, void (*initfunc)(void));
172
173
174
Brian Paulbc794052000-01-31 23:10:47 +0000175#else /* THREADS */
176
177
178/*
179 * THREADS not defined
180 */
181
182typedef GLuint _glthread_TSD;
183
184typedef GLuint _glthread_Thread;
185
186typedef GLuint _glthread_Mutex;
187
188#define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
189
190#define _glthread_INIT_MUTEX(name) (void) name
191
192#define _glthread_LOCK_MUTEX(name) (void) name
193
194#define _glthread_UNLOCK_MUTEX(name) (void) name
195
196
197#endif /* THREADS */
198
199#endif /* THREADS_H */
200