Merge remote branch 'origin/7.8'
[mesa.git] / src / mesa / glapi / glapi.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 /*
27 * This file manages the OpenGL API dispatch layer.
28 * The dispatch table (struct _glapi_table) is basically just a list
29 * of function pointers.
30 * There are functions to set/get the current dispatch table for the
31 * current thread and to manage registration/dispatch of dynamically
32 * added extension functions.
33 *
34 * It's intended that this file and the other glapi*.[ch] files are
35 * flexible enough to be reused in several places: XFree86, DRI-
36 * based libGL.so, and perhaps the SGI SI.
37 *
38 * NOTE: There are no dependencies on Mesa in this code.
39 *
40 * Versions (API changes):
41 * 2000/02/23 - original version for Mesa 3.3 and XFree86 4.0
42 * 2001/01/16 - added dispatch override feature for Mesa 3.5
43 * 2002/06/28 - added _glapi_set_warning_func(), Mesa 4.1.
44 * 2002/10/01 - _glapi_get_proc_address() will now generate new entrypoints
45 * itself (using offset ~0). _glapi_add_entrypoint() can be
46 * called afterward and it'll fill in the correct dispatch
47 * offset. This allows DRI libGL to avoid probing for DRI
48 * drivers! No changes to the public glapi interface.
49 */
50
51
52
53 #ifdef HAVE_DIX_CONFIG_H
54 #include <dix-config.h>
55 #include "glapi/mesa.h"
56 #else
57 #include "main/glheader.h"
58 #include "main/compiler.h"
59 #endif
60
61 #include "glapi/glapi.h"
62 #include "glapi/glapi_priv.h"
63
64 extern _glapi_proc __glapi_noop_table[];
65
66
67 /**
68 * \name Current dispatch and current context control variables
69 *
70 * Depending on whether or not multithreading is support, and the type of
71 * support available, several variables are used to store the current context
72 * pointer and the current dispatch table pointer. In the non-threaded case,
73 * the variables \c _glapi_Dispatch and \c _glapi_Context are used for this
74 * purpose.
75 *
76 * In the "normal" threaded case, the variables \c _glapi_Dispatch and
77 * \c _glapi_Context will be \c NULL if an application is detected as being
78 * multithreaded. Single-threaded applications will use \c _glapi_Dispatch
79 * and \c _glapi_Context just like the case without any threading support.
80 * When \c _glapi_Dispatch and \c _glapi_Context are \c NULL, the thread state
81 * data \c _gl_DispatchTSD and \c ContextTSD are used. Drivers and the
82 * static dispatch functions access these variables via \c _glapi_get_dispatch
83 * and \c _glapi_get_context.
84 *
85 * There is a race condition in setting \c _glapi_Dispatch to \c NULL. It is
86 * possible for the original thread to be setting it at the same instant a new
87 * thread, perhaps running on a different processor, is clearing it. Because
88 * of that, \c ThreadSafe, which can only ever be changed to \c GL_TRUE, is
89 * used to determine whether or not the application is multithreaded.
90 *
91 * In the TLS case, the variables \c _glapi_Dispatch and \c _glapi_Context are
92 * hardcoded to \c NULL. Instead the TLS variables \c _glapi_tls_Dispatch and
93 * \c _glapi_tls_Context are used. Having \c _glapi_Dispatch and
94 * \c _glapi_Context be hardcoded to \c NULL maintains binary compatability
95 * between TLS enabled loaders and non-TLS DRI drivers.
96 */
97 /*@{*/
98 #if defined(GLX_USE_TLS)
99
100 PUBLIC __thread struct _glapi_table * _glapi_tls_Dispatch
101 __attribute__((tls_model("initial-exec")))
102 = (struct _glapi_table *) __glapi_noop_table;
103
104 PUBLIC __thread void * _glapi_tls_Context
105 __attribute__((tls_model("initial-exec")));
106
107 PUBLIC const struct _glapi_table *_glapi_Dispatch = NULL;
108
109 PUBLIC const void *_glapi_Context = NULL;
110
111 #else
112
113 #if defined(THREADS)
114
115 static GLboolean ThreadSafe = GL_FALSE; /**< In thread-safe mode? */
116
117 _glthread_TSD _gl_DispatchTSD; /**< Per-thread dispatch pointer */
118
119 static _glthread_TSD ContextTSD; /**< Per-thread context pointer */
120
121 #endif /* defined(THREADS) */
122
123 PUBLIC struct _glapi_table *_glapi_Dispatch = (struct _glapi_table *) __glapi_noop_table;
124
125 PUBLIC void *_glapi_Context = NULL;
126
127 #endif /* defined(GLX_USE_TLS) */
128 /*@}*/
129
130
131
132 #if defined(THREADS) && !defined(GLX_USE_TLS)
133
134 void
135 _glapi_init_multithread(void)
136 {
137 _glthread_InitTSD(&_gl_DispatchTSD);
138 _glthread_InitTSD(&ContextTSD);
139 }
140
141 void
142 _glapi_destroy_multithread(void)
143 {
144 #ifdef WIN32_THREADS
145 _glthread_DestroyTSD(&_gl_DispatchTSD);
146 _glthread_DestroyTSD(&ContextTSD);
147 #endif
148 }
149
150 /**
151 * Mutex for multithread check.
152 */
153 #ifdef WIN32_THREADS
154 /* _glthread_DECLARE_STATIC_MUTEX is broken on windows. There will be race! */
155 #define CHECK_MULTITHREAD_LOCK()
156 #define CHECK_MULTITHREAD_UNLOCK()
157 #else
158 _glthread_DECLARE_STATIC_MUTEX(ThreadCheckMutex);
159 #define CHECK_MULTITHREAD_LOCK() _glthread_LOCK_MUTEX(ThreadCheckMutex)
160 #define CHECK_MULTITHREAD_UNLOCK() _glthread_UNLOCK_MUTEX(ThreadCheckMutex)
161 #endif
162
163 /**
164 * We should call this periodically from a function such as glXMakeCurrent
165 * in order to test if multiple threads are being used.
166 */
167 PUBLIC void
168 _glapi_check_multithread(void)
169 {
170 static unsigned long knownID;
171 static GLboolean firstCall = GL_TRUE;
172
173 if (ThreadSafe)
174 return;
175
176 CHECK_MULTITHREAD_LOCK();
177 if (firstCall) {
178 _glapi_init_multithread();
179
180 knownID = _glthread_GetID();
181 firstCall = GL_FALSE;
182 }
183 else if (knownID != _glthread_GetID()) {
184 ThreadSafe = GL_TRUE;
185 _glapi_set_dispatch(NULL);
186 _glapi_set_context(NULL);
187 }
188 CHECK_MULTITHREAD_UNLOCK();
189 }
190
191 #else
192
193 void
194 _glapi_init_multithread(void) { }
195
196 void
197 _glapi_destroy_multithread(void) { }
198
199 PUBLIC void
200 _glapi_check_multithread(void) { }
201
202 #endif
203
204
205
206 /**
207 * Set the current context pointer for this thread.
208 * The context pointer is an opaque type which should be cast to
209 * void from the real context pointer type.
210 */
211 PUBLIC void
212 _glapi_set_context(void *context)
213 {
214 #if defined(GLX_USE_TLS)
215 _glapi_tls_Context = context;
216 #elif defined(THREADS)
217 _glthread_SetTSD(&ContextTSD, context);
218 _glapi_Context = (ThreadSafe) ? NULL : context;
219 #else
220 _glapi_Context = context;
221 #endif
222 }
223
224
225
226 /**
227 * Get the current context pointer for this thread.
228 * The context pointer is an opaque type which should be cast from
229 * void to the real context pointer type.
230 */
231 PUBLIC void *
232 _glapi_get_context(void)
233 {
234 #if defined(GLX_USE_TLS)
235 return _glapi_tls_Context;
236 #elif defined(THREADS)
237 return (ThreadSafe) ? _glthread_GetTSD(&ContextTSD) : _glapi_Context;
238 #else
239 return _glapi_Context;
240 #endif
241 }
242
243
244
245 /**
246 * Set the global or per-thread dispatch table pointer.
247 * If the dispatch parameter is NULL we'll plug in the no-op dispatch
248 * table (__glapi_noop_table).
249 */
250 PUBLIC void
251 _glapi_set_dispatch(struct _glapi_table *dispatch)
252 {
253 init_glapi_relocs_once();
254
255 if (dispatch == NULL) {
256 /* use the no-op functions */
257 dispatch = (struct _glapi_table *) __glapi_noop_table;
258 }
259 #ifdef DEBUG
260 else {
261 _glapi_check_table_not_null(dispatch);
262 _glapi_check_table(dispatch);
263 }
264 #endif
265
266 #if defined(GLX_USE_TLS)
267 _glapi_tls_Dispatch = dispatch;
268 #elif defined(THREADS)
269 _glthread_SetTSD(&_gl_DispatchTSD, (void *) dispatch);
270 _glapi_Dispatch = (ThreadSafe) ? NULL : dispatch;
271 #else
272 _glapi_Dispatch = dispatch;
273 #endif
274 }
275
276
277
278 /**
279 * Return pointer to current dispatch table for calling thread.
280 */
281 PUBLIC struct _glapi_table *
282 _glapi_get_dispatch(void)
283 {
284 #if defined(GLX_USE_TLS)
285 return _glapi_tls_Dispatch;
286 #elif defined(THREADS)
287 return (ThreadSafe)
288 ? (struct _glapi_table *) _glthread_GetTSD(&_gl_DispatchTSD)
289 : _glapi_Dispatch;
290 #else
291 return _glapi_Dispatch;
292 #endif
293 }