mesa: move GLbitfield any_valid_stages declaration before code
[mesa.git] / src / mapi / u_thread.h
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2006 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 * Thread support for gl dispatch.
28 *
29 * Initial version by John Stone (j.stone@acm.org) (johns@cs.umr.edu)
30 * and Christoph Poliwoda (poliwoda@volumegraphics.com)
31 * Revised by Keith Whitwell
32 * Adapted for new gl dispatcher by Brian Paul
33 * Modified for use in mapi by Chia-I Wu
34 */
35
36 /*
37 * If this file is accidentally included by a non-threaded build,
38 * it should not cause the build to fail, or otherwise cause problems.
39 * In general, it should only be included when needed however.
40 */
41
42 #ifndef _U_THREAD_H_
43 #define _U_THREAD_H_
44
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include "u_compiler.h"
48
49 #include "c11/threads.h"
50
51 #if defined(HAVE_PTHREAD) || defined(_WIN32)
52 #ifndef THREADS
53 #define THREADS
54 #endif
55 #endif
56
57 /*
58 * Error messages
59 */
60 #define INIT_TSD_ERROR "Mesa: failed to allocate key for thread specific data"
61 #define GET_TSD_ERROR "Mesa: failed to get thread specific data"
62 #define SET_TSD_ERROR "Mesa: thread failed to set thread specific data"
63
64
65 /*
66 * Magic number to determine if a TSD object has been initialized.
67 * Kind of a hack but there doesn't appear to be a better cross-platform
68 * solution.
69 */
70 #define INIT_MAGIC 0xff8adc98
71
72 #ifdef __cplusplus
73 extern "C" {
74 #endif
75
76
77 struct u_tsd {
78 tss_t key;
79 unsigned initMagic;
80 };
81
82
83 static INLINE unsigned long
84 u_thread_self(void)
85 {
86 /*
87 * XXX: Callers of u_thread_self assume it is a lightweight function,
88 * returning a numeric value. But unfortunately C11's thrd_current() gives
89 * no such guarantees. In fact, it's pretty hard to have a compliant
90 * implementation of thrd_current() on Windows with such characteristics.
91 * So for now, we side-step this mess and use Windows thread primitives
92 * directly here.
93 *
94 * FIXME: On the other hand, u_thread_self() is a bad
95 * abstraction. Even with pthreads, there is no guarantee that
96 * pthread_self() will return numeric IDs -- we should be using
97 * pthread_equal() instead of assuming we can compare thread ids...
98 */
99 #ifdef _WIN32
100 return GetCurrentThreadId();
101 #else
102 return (unsigned long) (uintptr_t) thrd_current();
103 #endif
104 }
105
106
107 static INLINE void
108 u_tsd_init(struct u_tsd *tsd)
109 {
110 if (tss_create(&tsd->key, NULL/*free*/) != 0) {
111 perror(INIT_TSD_ERROR);
112 exit(-1);
113 }
114 tsd->initMagic = INIT_MAGIC;
115 }
116
117
118 static INLINE void *
119 u_tsd_get(struct u_tsd *tsd)
120 {
121 if (tsd->initMagic != INIT_MAGIC) {
122 u_tsd_init(tsd);
123 }
124 return tss_get(tsd->key);
125 }
126
127
128 static INLINE void
129 u_tsd_set(struct u_tsd *tsd, void *ptr)
130 {
131 if (tsd->initMagic != INIT_MAGIC) {
132 u_tsd_init(tsd);
133 }
134 if (tss_set(tsd->key, ptr) != 0) {
135 perror(SET_TSD_ERROR);
136 exit(-1);
137 }
138 }
139
140
141 static INLINE void
142 u_tsd_destroy(struct u_tsd *tsd)
143 {
144 if (tsd->initMagic != INIT_MAGIC) {
145 return;
146 }
147 tss_delete(tsd->key);
148 tsd->initMagic = 0x0;
149 }
150
151
152 #ifdef __cplusplus
153 }
154 #endif
155
156 #endif /* _U_THREAD_H_ */