mesa: Add missing header to texgetimage.h.
[mesa.git] / src / mapi / 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 * 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 #include "u_current.h"
52 #include "u_thread.h"
53
54 #ifndef MAPI_GLAPI_CURRENT
55
56 #include "table.h"
57 #include "stub.h"
58
59 #else
60
61 extern void init_glapi_relocs_once(void);
62 extern void (*__glapi_noop_table[])(void);
63
64 #define table_noop_array __glapi_noop_table
65 #define stub_init_once() init_glapi_relocs_once()
66
67 #endif
68
69 /**
70 * \name Current dispatch and current context control variables
71 *
72 * Depending on whether or not multithreading is support, and the type of
73 * support available, several variables are used to store the current context
74 * pointer and the current dispatch table pointer. In the non-threaded case,
75 * the variables \c _glapi_Dispatch and \c _glapi_Context are used for this
76 * purpose.
77 *
78 * In the "normal" threaded case, the variables \c _glapi_Dispatch and
79 * \c _glapi_Context will be \c NULL if an application is detected as being
80 * multithreaded. Single-threaded applications will use \c _glapi_Dispatch
81 * and \c _glapi_Context just like the case without any threading support.
82 * When \c _glapi_Dispatch and \c _glapi_Context are \c NULL, the thread state
83 * data \c _gl_DispatchTSD and \c ContextTSD are used. Drivers and the
84 * static dispatch functions access these variables via \c _glapi_get_dispatch
85 * and \c _glapi_get_context.
86 *
87 * There is a race condition in setting \c _glapi_Dispatch to \c NULL. It is
88 * possible for the original thread to be setting it at the same instant a new
89 * thread, perhaps running on a different processor, is clearing it. Because
90 * of that, \c ThreadSafe, which can only ever be changed to \c GL_TRUE, is
91 * used to determine whether or not the application is multithreaded.
92 *
93 * In the TLS case, the variables \c _glapi_Dispatch and \c _glapi_Context are
94 * hardcoded to \c NULL. Instead the TLS variables \c _glapi_tls_Dispatch and
95 * \c _glapi_tls_Context are used. Having \c _glapi_Dispatch and
96 * \c _glapi_Context be hardcoded to \c NULL maintains binary compatability
97 * between TLS enabled loaders and non-TLS DRI drivers.
98 */
99 /*@{*/
100 #if defined(GLX_USE_TLS)
101
102 __thread struct _glapi_table *_glapi_tls_Dispatch
103 __attribute__((tls_model("initial-exec")))
104 = (struct _glapi_table *) table_noop_array;
105
106 __thread void * _glapi_tls_Context
107 __attribute__((tls_model("initial-exec")));
108
109 const struct _glapi_table *_glapi_Dispatch;
110 const void *_glapi_Context;
111
112 #else
113
114 struct _glapi_table *_glapi_Dispatch =
115 (struct _glapi_table *) table_noop_array;
116 void *_glapi_Context;
117
118 #ifdef THREADS
119 struct u_tsd _gl_DispatchTSD;
120 static struct u_tsd ContextTSD;
121 static int ThreadSafe;
122 #endif /* THREADS */
123
124 #endif /* defined(GLX_USE_TLS) */
125 /*@}*/
126
127
128 void
129 _glapi_destroy_multithread(void)
130 {
131 #if defined(THREADS) && defined(WIN32_THREADS)
132 u_tsd_destroy(&_gl_DispatchTSD);
133 u_tsd_destroy(&ContextTSD);
134 #endif
135 }
136
137
138 #if defined(THREADS) && !defined(GLX_USE_TLS)
139
140 static void
141 _glapi_init_multithread(void)
142 {
143 u_tsd_init(&_gl_DispatchTSD);
144 u_tsd_init(&ContextTSD);
145 }
146
147 /**
148 * Mutex for multithread check.
149 */
150 #ifdef WIN32_THREADS
151 /* _glthread_DECLARE_STATIC_MUTEX is broken on windows. There will be race! */
152 #define CHECK_MULTITHREAD_LOCK()
153 #define CHECK_MULTITHREAD_UNLOCK()
154 #else
155 u_mutex_declare_static(ThreadCheckMutex);
156 #define CHECK_MULTITHREAD_LOCK() u_mutex_lock(ThreadCheckMutex)
157 #define CHECK_MULTITHREAD_UNLOCK() u_mutex_unlock(ThreadCheckMutex)
158 #endif
159
160 /**
161 * We should call this periodically from a function such as glXMakeCurrent
162 * in order to test if multiple threads are being used.
163 */
164 void
165 _glapi_check_multithread(void)
166 {
167 static unsigned long knownID;
168 static int firstCall = 1;
169
170 if (ThreadSafe)
171 return;
172
173 CHECK_MULTITHREAD_LOCK();
174 if (firstCall) {
175 _glapi_init_multithread();
176
177 knownID = u_thread_self();
178 firstCall = 0;
179 }
180 else if (knownID != u_thread_self()) {
181 ThreadSafe = 1;
182 _glapi_set_dispatch(NULL);
183 _glapi_set_context(NULL);
184 }
185 CHECK_MULTITHREAD_UNLOCK();
186 }
187
188 #else
189
190 void
191 _glapi_check_multithread(void)
192 {
193 }
194
195 #endif
196
197
198
199 /**
200 * Set the current context pointer for this thread.
201 * The context pointer is an opaque type which should be cast to
202 * void from the real context pointer type.
203 */
204 void
205 _glapi_set_context(void *context)
206 {
207 #if defined(GLX_USE_TLS)
208 _glapi_tls_Context = context;
209 #elif defined(THREADS)
210 u_tsd_set(&ContextTSD, context);
211 _glapi_Context = (ThreadSafe) ? NULL : context;
212 #else
213 _glapi_Context = context;
214 #endif
215 }
216
217 /**
218 * Get the current context pointer for this thread.
219 * The context pointer is an opaque type which should be cast from
220 * void to the real context pointer type.
221 */
222 void *
223 _glapi_get_context(void)
224 {
225 #if defined(GLX_USE_TLS)
226 return _glapi_tls_Context;
227 #elif defined(THREADS)
228 return (ThreadSafe)
229 ? u_tsd_get(&ContextTSD)
230 : _glapi_Context;
231 #else
232 return _glapi_Context;
233 #endif
234 }
235
236 /**
237 * Set the global or per-thread dispatch table pointer.
238 * If the dispatch parameter is NULL we'll plug in the no-op dispatch
239 * table (__glapi_noop_table).
240 */
241 void
242 _glapi_set_dispatch(struct _glapi_table *dispatch)
243 {
244 stub_init_once();
245
246 if (!dispatch)
247 dispatch = (struct _glapi_table *) table_noop_array;
248
249 #if defined(GLX_USE_TLS)
250 _glapi_tls_Dispatch = dispatch;
251 #elif defined(THREADS)
252 u_tsd_set(&_gl_DispatchTSD, (void *) dispatch);
253 _glapi_Dispatch = (ThreadSafe) ? NULL : dispatch;
254 #else
255 _glapi_Dispatch = dispatch;
256 #endif
257 }
258
259 /**
260 * Return pointer to current dispatch table for calling thread.
261 */
262 struct _glapi_table *
263 _glapi_get_dispatch(void)
264 {
265 #if defined(GLX_USE_TLS)
266 return _glapi_tls_Dispatch;
267 #elif defined(THREADS)
268 return (ThreadSafe)
269 ? (struct _glapi_table *) u_tsd_get(&_gl_DispatchTSD)
270 : _glapi_Dispatch;
271 #else
272 return _glapi_Dispatch;
273 #endif
274 }