/*1* Mesa 3-D graphics library2*3* Copyright (C) 1999-2008 Brian Paul All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the "Software"),7* to deal in the Software without restriction, including without limitation8* the rights to use, copy, modify, merge, publish, distribute, sublicense,9* and/or sell copies of the Software, and to permit persons to whom the10* Software is furnished to do so, subject to the following conditions:11*12* The above copyright notice and this permission notice shall be included13* in all copies or substantial portions of the Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS16* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL18* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR19* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,20* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR21* OTHER DEALINGS IN THE SOFTWARE.22*/232425/*26* This file manages the OpenGL API dispatch layer.27* The dispatch table (struct _glapi_table) is basically just a list28* of function pointers.29* There are functions to set/get the current dispatch table for the30* current thread and to manage registration/dispatch of dynamically31* added extension functions.32*33* It's intended that this file and the other glapi*.[ch] files are34* flexible enough to be reused in several places: XFree86, DRI-35* based libGL.so, and perhaps the SGI SI.36*37* NOTE: There are no dependencies on Mesa in this code.38*39* Versions (API changes):40* 2000/02/23 - original version for Mesa 3.3 and XFree86 4.041* 2001/01/16 - added dispatch override feature for Mesa 3.542* 2002/06/28 - added _glapi_set_warning_func(), Mesa 4.1.43* 2002/10/01 - _glapi_get_proc_address() will now generate new entrypoints44* itself (using offset ~0). _glapi_add_entrypoint() can be45* called afterward and it'll fill in the correct dispatch46* offset. This allows DRI libGL to avoid probing for DRI47* drivers! No changes to the public glapi interface.48*/4950#include "c11/threads.h"51#include "util/u_thread.h"52#include "u_current.h"5354#ifndef MAPI_MODE_UTIL5556#include "table.h"57#include "stub.h"5859#else6061extern void init_glapi_relocs_once(void);62extern void (*__glapi_noop_table[])(void);6364#define table_noop_array __glapi_noop_table65#define stub_init_once() init_glapi_relocs_once()6667#endif6869/**70* \name Current dispatch and current context control variables71*72* Depending on whether or not multithreading is support, and the type of73* support available, several variables are used to store the current context74* 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 this76* purpose.77*78* In the "normal" threaded case, the variables \c _glapi_Dispatch and79* \c _glapi_Context will be \c NULL if an application is detected as being80* multithreaded. Single-threaded applications will use \c _glapi_Dispatch81* 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 state83* data \c _gl_DispatchTSD and \c ContextTSD are used. Drivers and the84* static dispatch functions access these variables via \c _glapi_get_dispatch85* and \c _glapi_get_context.86*87* There is a race condition in setting \c _glapi_Dispatch to \c NULL. It is88* possible for the original thread to be setting it at the same instant a new89* thread, perhaps running on a different processor, is clearing it. Because90* of that, \c ThreadSafe, which can only ever be changed to \c GL_TRUE, is91* 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 are94* hardcoded to \c NULL. Instead the TLS variables \c _glapi_tls_Dispatch and95* \c _glapi_tls_Context are used. Having \c _glapi_Dispatch and96* \c _glapi_Context be hardcoded to \c NULL maintains binary compatability97* between TLS enabled loaders and non-TLS DRI drivers.98*/99/*@{*/100#if defined(USE_ELF_TLS)101102__THREAD_INITIAL_EXEC struct _glapi_table *u_current_table103= (struct _glapi_table *) table_noop_array;104105__THREAD_INITIAL_EXEC void *u_current_context;106107#else108109struct _glapi_table *u_current_table =110(struct _glapi_table *) table_noop_array;111void *u_current_context;112113tss_t u_current_table_tsd;114static tss_t u_current_context_tsd;115static int ThreadSafe;116117#endif /* defined(USE_ELF_TLS) */118/*@}*/119120121void122u_current_destroy(void)123{124#if !defined(USE_ELF_TLS)125tss_delete(u_current_table_tsd);126tss_delete(u_current_context_tsd);127#endif128}129130131#if !defined(USE_ELF_TLS)132133static void134u_current_init_tsd(void)135{136tss_create(&u_current_table_tsd, NULL);137tss_create(&u_current_context_tsd, NULL);138}139140/**141* Mutex for multithread check.142*/143static mtx_t ThreadCheckMutex = _MTX_INITIALIZER_NP;144145static thread_id knownID;146147/**148* We should call this periodically from a function such as glXMakeCurrent149* in order to test if multiple threads are being used.150*/151void152u_current_init(void)153{154static int firstCall = 1;155156if (ThreadSafe)157return;158159mtx_lock(&ThreadCheckMutex);160if (firstCall) {161u_current_init_tsd();162163knownID = util_get_thread_id();164firstCall = 0;165}166else if (!util_thread_id_equal(knownID, util_get_thread_id())) {167ThreadSafe = 1;168u_current_set_table(NULL);169u_current_set_context(NULL);170}171mtx_unlock(&ThreadCheckMutex);172}173174#else175176void177u_current_init(void)178{179}180181#endif182183184185/**186* Set the current context pointer for this thread.187* The context pointer is an opaque type which should be cast to188* void from the real context pointer type.189*/190void191u_current_set_context(const void *ptr)192{193u_current_init();194195#if defined(USE_ELF_TLS)196u_current_context = (void *) ptr;197#else198tss_set(u_current_context_tsd, (void *) ptr);199u_current_context = (ThreadSafe) ? NULL : (void *) ptr;200#endif201}202203/**204* Get the current context pointer for this thread.205* The context pointer is an opaque type which should be cast from206* void to the real context pointer type.207*/208void *209u_current_get_context_internal(void)210{211#if defined(USE_ELF_TLS)212return u_current_context;213#else214if (ThreadSafe)215return tss_get(u_current_context_tsd);216else if (!util_thread_id_equal(knownID, util_get_thread_id()))217return NULL;218else219return u_current_context;220#endif221}222223/**224* Set the global or per-thread dispatch table pointer.225* If the dispatch parameter is NULL we'll plug in the no-op dispatch226* table (__glapi_noop_table).227*/228void229u_current_set_table(const struct _glapi_table *tbl)230{231u_current_init();232233stub_init_once();234235if (!tbl)236tbl = (const struct _glapi_table *) table_noop_array;237238#if defined(USE_ELF_TLS)239u_current_table = (struct _glapi_table *) tbl;240#else241tss_set(u_current_table_tsd, (void *) tbl);242u_current_table = (ThreadSafe) ? NULL : (void *) tbl;243#endif244}245246/**247* Return pointer to current dispatch table for calling thread.248*/249struct _glapi_table *250u_current_get_table_internal(void)251{252#if defined(USE_ELF_TLS)253return u_current_table;254#else255if (ThreadSafe)256return (struct _glapi_table *) tss_get(u_current_table_tsd);257else if (!util_thread_id_equal(knownID, util_get_thread_id()))258return (struct _glapi_table *) table_noop_array;259else260return (struct _glapi_table *) u_current_table;261#endif262}263264265