xref: /third_party/mesa3d/src/mapi/u_current.c (revision bf215546)
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 *
89 * In the TLS case, the variables \c _glapi_Dispatch and \c _glapi_Context are
90 * hardcoded to \c NULL.  Instead the TLS variables \c _glapi_tls_Dispatch and
91 * \c _glapi_tls_Context are used.  Having \c _glapi_Dispatch and
92 * \c _glapi_Context be hardcoded to \c NULL maintains binary compatability
93 * between TLS enabled loaders and non-TLS DRI drivers.
94 */
95/*@{*/
96
97__THREAD_INITIAL_EXEC struct _glapi_table *u_current_table
98    = (struct _glapi_table *) table_noop_array;
99
100__THREAD_INITIAL_EXEC void *u_current_context;
101
102/*@}*/
103
104/**
105 * Set the current context pointer for this thread.
106 * The context pointer is an opaque type which should be cast to
107 * void from the real context pointer type.
108 */
109void
110u_current_set_context(const void *ptr)
111{
112   u_current_context = (void *) ptr;
113}
114
115/**
116 * Get the current context pointer for this thread.
117 * The context pointer is an opaque type which should be cast from
118 * void to the real context pointer type.
119 */
120void *
121u_current_get_context_internal(void)
122{
123   return u_current_context;
124}
125
126/**
127 * Set the global or per-thread dispatch table pointer.
128 * If the dispatch parameter is NULL we'll plug in the no-op dispatch
129 * table (__glapi_noop_table).
130 */
131void
132u_current_set_table(const struct _glapi_table *tbl)
133{
134   stub_init_once();
135
136   if (!tbl)
137      tbl = (const struct _glapi_table *) table_noop_array;
138
139   u_current_table = (struct _glapi_table *) tbl;
140}
141
142/**
143 * Return pointer to current dispatch table for calling thread.
144 */
145struct _glapi_table *
146u_current_get_table_internal(void)
147{
148   return u_current_table;
149}
150