1/* 2 * Mesa 3-D graphics library 3 * 4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included 14 * in all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 * 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 "c11/threads.h" 52#include "util/u_thread.h" 53#include "u_current.h" 54 55#ifndef MAPI_MODE_UTIL 56 57#include "table.h" 58#include "stub.h" 59 60#else 61 62extern void init_glapi_relocs_once(void); 63extern 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(USE_ELF_TLS) 102 103__THREAD_INITIAL_EXEC struct _glapi_table *u_current_table 104#if defined(__NetBSD__) 105 = NULL; /* non-zero initializers not supported with dlopen */ 106#else 107 = (struct _glapi_table *) table_noop_array; 108#endif 109 110__THREAD_INITIAL_EXEC void *u_current_context; 111 112#else 113 114struct _glapi_table *u_current_table = 115 (struct _glapi_table *) table_noop_array; 116void *u_current_context; 117 118tss_t u_current_table_tsd; 119static tss_t u_current_context_tsd; 120static int ThreadSafe; 121 122#endif /* defined(USE_ELF_TLS) */ 123/*@}*/ 124 125 126void 127u_current_destroy(void) 128{ 129#if !defined(USE_ELF_TLS) 130 tss_delete(u_current_table_tsd); 131 tss_delete(u_current_context_tsd); 132#endif 133} 134 135 136#if !defined(USE_ELF_TLS) 137 138static void 139u_current_init_tsd(void) 140{ 141 tss_create(&u_current_table_tsd, NULL); 142 tss_create(&u_current_context_tsd, NULL); 143} 144 145/** 146 * Mutex for multithread check. 147 */ 148static mtx_t ThreadCheckMutex = _MTX_INITIALIZER_NP; 149 150static thread_id knownID; 151 152/** 153 * We should call this periodically from a function such as glXMakeCurrent 154 * in order to test if multiple threads are being used. 155 */ 156void 157u_current_init(void) 158{ 159 static int firstCall = 1; 160 161 if (ThreadSafe) 162 return; 163 164 mtx_lock(&ThreadCheckMutex); 165 if (firstCall) { 166 u_current_init_tsd(); 167 168 knownID = util_get_thread_id(); 169 firstCall = 0; 170 } 171 else if (!util_thread_id_equal(knownID, util_get_thread_id())) { 172 ThreadSafe = 1; 173 u_current_set_table(NULL); 174 u_current_set_context(NULL); 175 } 176 mtx_unlock(&ThreadCheckMutex); 177} 178 179#else 180 181void 182u_current_init(void) 183{ 184} 185 186#endif 187 188 189 190/** 191 * Set the current context pointer for this thread. 192 * The context pointer is an opaque type which should be cast to 193 * void from the real context pointer type. 194 */ 195void 196u_current_set_context(const void *ptr) 197{ 198 u_current_init(); 199 200#if defined(USE_ELF_TLS) 201 u_current_context = (void *) ptr; 202#else 203 tss_set(u_current_context_tsd, (void *) ptr); 204 u_current_context = (ThreadSafe) ? NULL : (void *) ptr; 205#endif 206} 207 208/** 209 * Get the current context pointer for this thread. 210 * The context pointer is an opaque type which should be cast from 211 * void to the real context pointer type. 212 */ 213void * 214u_current_get_context_internal(void) 215{ 216#if defined(USE_ELF_TLS) 217 return u_current_context; 218#else 219 if (ThreadSafe) 220 return tss_get(u_current_context_tsd); 221 else if (!util_thread_id_equal(knownID, util_get_thread_id())) 222 return NULL; 223 else 224 return u_current_context; 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 */ 233void 234u_current_set_table(const struct _glapi_table *tbl) 235{ 236 u_current_init(); 237 238 stub_init_once(); 239 240 if (!tbl) 241 tbl = (const struct _glapi_table *) table_noop_array; 242 243#if defined(USE_ELF_TLS) 244 u_current_table = (struct _glapi_table *) tbl; 245#else 246 tss_set(u_current_table_tsd, (void *) tbl); 247 u_current_table = (ThreadSafe) ? NULL : (void *) tbl; 248#endif 249} 250 251/** 252 * Return pointer to current dispatch table for calling thread. 253 */ 254struct _glapi_table * 255u_current_get_table_internal(void) 256{ 257#if defined(USE_ELF_TLS) 258# if defined(__NetBSD__) 259 return (likely(u_current_table) ? u_current_table : (struct _glapi_table *) table_noop_array); 260# else 261 return u_current_table; 262# endif 263#else 264 if (ThreadSafe) 265 return (struct _glapi_table *) tss_get(u_current_table_tsd); 266 else if (!util_thread_id_equal(knownID, util_get_thread_id())) 267 return (struct _glapi_table *) table_noop_array; 268 else 269 return (struct _glapi_table *) u_current_table; 270#endif 271} 272