2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
28 * This file manages the OpenGL API dispatch layer.
29 * The dispatch table (struct _glapi_table) is basically just a list
30 * of function pointers.
31 * There are functions to set/get the current dispatch table for the
32 * current thread and to manage registration/dispatch of dynamically
33 * added extension functions.
35 * It's intended that this file and the other glapi*.[ch] files are
36 * flexible enough to be reused in several places: XFree86, DRI-
37 * based libGL.so, and perhaps the SGI SI.
39 * NOTE: There are no dependencies on Mesa in this code.
41 * Versions (API changes):
42 * 2000/02/23 - original version for Mesa 3.3 and XFree86 4.0
43 * 2001/01/16 - added dispatch override feature for Mesa 3.5
44 * 2002/06/28 - added _glapi_set_warning_func(), Mesa 4.1.
45 * 2002/10/01 - _glapi_get_proc_address() will now generate new entrypoints
46 * itself (using offset ~0). _glapi_add_entrypoint() can be
47 * called afterward and it'll fill in the correct dispatch
48 * offset. This allows DRI libGL to avoid probing for DRI
49 * drivers! No changes to the public glapi interface.
52 #include "u_current.h"
55 #ifndef MAPI_MODE_UTIL
62 extern void init_glapi_relocs_once(void);
63 extern void (*__glapi_noop_table
[])(void);
65 #define table_noop_array __glapi_noop_table
66 #define stub_init_once() init_glapi_relocs_once()
71 * \name Current dispatch and current context control variables
73 * Depending on whether or not multithreading is support, and the type of
74 * support available, several variables are used to store the current context
75 * pointer and the current dispatch table pointer. In the non-threaded case,
76 * the variables \c _glapi_Dispatch and \c _glapi_Context are used for this
79 * In the "normal" threaded case, the variables \c _glapi_Dispatch and
80 * \c _glapi_Context will be \c NULL if an application is detected as being
81 * multithreaded. Single-threaded applications will use \c _glapi_Dispatch
82 * and \c _glapi_Context just like the case without any threading support.
83 * When \c _glapi_Dispatch and \c _glapi_Context are \c NULL, the thread state
84 * data \c _gl_DispatchTSD and \c ContextTSD are used. Drivers and the
85 * static dispatch functions access these variables via \c _glapi_get_dispatch
86 * and \c _glapi_get_context.
88 * There is a race condition in setting \c _glapi_Dispatch to \c NULL. It is
89 * possible for the original thread to be setting it at the same instant a new
90 * thread, perhaps running on a different processor, is clearing it. Because
91 * of that, \c ThreadSafe, which can only ever be changed to \c GL_TRUE, is
92 * used to determine whether or not the application is multithreaded.
94 * In the TLS case, the variables \c _glapi_Dispatch and \c _glapi_Context are
95 * hardcoded to \c NULL. Instead the TLS variables \c _glapi_tls_Dispatch and
96 * \c _glapi_tls_Context are used. Having \c _glapi_Dispatch and
97 * \c _glapi_Context be hardcoded to \c NULL maintains binary compatability
98 * between TLS enabled loaders and non-TLS DRI drivers.
101 #if defined(GLX_USE_TLS)
103 __thread
struct mapi_table
*u_current_table
104 __attribute__((tls_model("initial-exec")))
105 = (struct mapi_table
*) table_noop_array
;
107 __thread
void *u_current_user
108 __attribute__((tls_model("initial-exec")));
112 struct mapi_table
*u_current_table
=
113 (struct mapi_table
*) table_noop_array
;
114 void *u_current_user
;
117 struct u_tsd u_current_table_tsd
;
118 static struct u_tsd u_current_user_tsd
;
119 static int ThreadSafe
;
122 #endif /* defined(GLX_USE_TLS) */
127 u_current_destroy(void)
129 #if defined(THREADS) && defined(_WIN32)
130 u_tsd_destroy(&u_current_table_tsd
);
131 u_tsd_destroy(&u_current_user_tsd
);
136 #if defined(THREADS) && !defined(GLX_USE_TLS)
139 u_current_init_tsd(void)
141 u_tsd_init(&u_current_table_tsd
);
142 u_tsd_init(&u_current_user_tsd
);
146 * Mutex for multithread check.
148 u_mutex_declare_static(ThreadCheckMutex
);
151 * We should call this periodically from a function such as glXMakeCurrent
152 * in order to test if multiple threads are being used.
157 static unsigned long knownID
;
158 static int firstCall
= 1;
163 u_mutex_lock(ThreadCheckMutex
);
165 u_current_init_tsd();
167 knownID
= u_thread_self();
170 else if (knownID
!= u_thread_self()) {
173 u_current_set_user(NULL
);
175 u_mutex_unlock(ThreadCheckMutex
);
190 * Set the current context pointer for this thread.
191 * The context pointer is an opaque type which should be cast to
192 * void from the real context pointer type.
195 u_current_set_user(const void *ptr
)
199 #if defined(GLX_USE_TLS)
200 u_current_user
= (void *) ptr
;
201 #elif defined(THREADS)
202 u_tsd_set(&u_current_user_tsd
, (void *) ptr
);
203 u_current_user
= (ThreadSafe
) ? NULL
: (void *) ptr
;
205 u_current_user
= (void *) ptr
;
210 * Get the current context pointer for this thread.
211 * The context pointer is an opaque type which should be cast from
212 * void to the real context pointer type.
215 u_current_get_user_internal(void)
217 #if defined(GLX_USE_TLS)
218 return u_current_user
;
219 #elif defined(THREADS)
221 ? u_tsd_get(&u_current_user_tsd
)
224 return u_current_user
;
229 * Set the global or per-thread dispatch table pointer.
230 * If the dispatch parameter is NULL we'll plug in the no-op dispatch
231 * table (__glapi_noop_table).
234 u_current_set(const struct mapi_table
*tbl
)
241 tbl
= (const struct mapi_table
*) table_noop_array
;
243 #if defined(GLX_USE_TLS)
244 u_current_table
= (struct mapi_table
*) tbl
;
245 #elif defined(THREADS)
246 u_tsd_set(&u_current_table_tsd
, (void *) tbl
);
247 u_current_table
= (ThreadSafe
) ? NULL
: (void *) tbl
;
249 u_current_table
= (struct mapi_table
*) tbl
;
254 * Return pointer to current dispatch table for calling thread.
257 u_current_get_internal(void)
259 #if defined(GLX_USE_TLS)
260 return u_current_table
;
261 #elif defined(THREADS)
262 return (struct mapi_table
*) ((ThreadSafe
) ?
263 u_tsd_get(&u_current_table_tsd
) : (void *) u_current_table
);
265 return u_current_table
;