3443449398458e5ef3087088582f5eb876165b35
[mesa.git] / src / mapi / u_current.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 * 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.
24 */
25
26
27 /*
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.
34 *
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.
38 *
39 * NOTE: There are no dependencies on Mesa in this code.
40 *
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.
50 */
51
52 #include "u_current.h"
53 #include "u_thread.h"
54
55 #ifndef MAPI_MODE_UTIL
56
57 #include "table.h"
58 #include "stub.h"
59
60 #else
61
62 extern void init_glapi_relocs_once(void);
63 extern void (*__glapi_noop_table[])(void);
64
65 #define table_noop_array __glapi_noop_table
66 #define stub_init_once() init_glapi_relocs_once()
67
68 #endif
69
70 /**
71 * \name Current dispatch and current context control variables
72 *
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
77 * purpose.
78 *
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.
87 *
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.
93 *
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.
99 */
100 /*@{*/
101 #if defined(GLX_USE_TLS)
102
103 __thread struct mapi_table *u_current_table
104 __attribute__((tls_model("initial-exec")))
105 = (struct mapi_table *) table_noop_array;
106
107 __thread void *u_current_user
108 __attribute__((tls_model("initial-exec")));
109
110 #else
111
112 struct mapi_table *u_current_table =
113 (struct mapi_table *) table_noop_array;
114 void *u_current_user;
115
116 #ifdef THREADS
117 struct u_tsd u_current_table_tsd;
118 static struct u_tsd u_current_user_tsd;
119 static int ThreadSafe;
120 #endif /* THREADS */
121
122 #endif /* defined(GLX_USE_TLS) */
123 /*@}*/
124
125
126 void
127 u_current_destroy(void)
128 {
129 #if defined(THREADS) && defined(_WIN32)
130 u_tsd_destroy(&u_current_table_tsd);
131 u_tsd_destroy(&u_current_user_tsd);
132 #endif
133 }
134
135
136 #if defined(THREADS) && !defined(GLX_USE_TLS)
137
138 static void
139 u_current_init_tsd(void)
140 {
141 u_tsd_init(&u_current_table_tsd);
142 u_tsd_init(&u_current_user_tsd);
143 }
144
145 /**
146 * Mutex for multithread check.
147 */
148 u_mutex_declare_static(ThreadCheckMutex);
149
150 /**
151 * We should call this periodically from a function such as glXMakeCurrent
152 * in order to test if multiple threads are being used.
153 */
154 void
155 u_current_init(void)
156 {
157 static unsigned long knownID;
158 static int firstCall = 1;
159
160 if (ThreadSafe)
161 return;
162
163 u_mutex_lock(ThreadCheckMutex);
164 if (firstCall) {
165 u_current_init_tsd();
166
167 knownID = u_thread_self();
168 firstCall = 0;
169 }
170 else if (knownID != u_thread_self()) {
171 ThreadSafe = 1;
172 u_current_set(NULL);
173 u_current_set_user(NULL);
174 }
175 u_mutex_unlock(ThreadCheckMutex);
176 }
177
178 #else
179
180 void
181 u_current_init(void)
182 {
183 }
184
185 #endif
186
187
188
189 /**
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.
193 */
194 void
195 u_current_set_user(const void *ptr)
196 {
197 u_current_init();
198
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;
204 #else
205 u_current_user = (void *) ptr;
206 #endif
207 }
208
209 /**
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.
213 */
214 void *
215 u_current_get_user_internal(void)
216 {
217 #if defined(GLX_USE_TLS)
218 return u_current_user;
219 #elif defined(THREADS)
220 return (ThreadSafe)
221 ? u_tsd_get(&u_current_user_tsd)
222 : u_current_user;
223 #else
224 return u_current_user;
225 #endif
226 }
227
228 /**
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).
232 */
233 void
234 u_current_set(const struct mapi_table *tbl)
235 {
236 u_current_init();
237
238 stub_init_once();
239
240 if (!tbl)
241 tbl = (const struct mapi_table *) table_noop_array;
242
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;
248 #else
249 u_current_table = (struct mapi_table *) tbl;
250 #endif
251 }
252
253 /**
254 * Return pointer to current dispatch table for calling thread.
255 */
256 struct mapi_table *
257 u_current_get_internal(void)
258 {
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);
264 #else
265 return u_current_table;
266 #endif
267 }