mesa: remove remaining uses of _glthread_GetID()
[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 "_glthread_: failed to allocate key for thread specific data"
61 #define GET_TSD_ERROR "_glthread_: failed to get thread specific data"
62 #define SET_TSD_ERROR "_glthread_: 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 typedef mtx_t u_mutex;
83
84 #define u_mutex_declare_static(name) \
85 static u_mutex name = _MTX_INITIALIZER_NP
86
87 #define u_mutex_init(name) mtx_init(&(name), mtx_plain)
88 #define u_mutex_destroy(name) mtx_destroy(&(name))
89 #define u_mutex_lock(name) (void) mtx_lock(&(name))
90 #define u_mutex_unlock(name) (void) mtx_unlock(&(name))
91
92
93 static INLINE unsigned long
94 u_thread_self(void)
95 {
96 /*
97 * XXX: Callers of u_thread_self assume it is a lightweight function,
98 * returning a numeric value. But unfortunately C11's thrd_current() gives
99 * no such guarantees. In fact, it's pretty hard to have a compliant
100 * implementation of thrd_current() on Windows with such characteristics.
101 * So for now, we side-step this mess and use Windows thread primitives
102 * directly here.
103 *
104 * FIXME: On the other hand, u_thread_self() is a bad
105 * abstraction. Even with pthreads, there is no guarantee that
106 * pthread_self() will return numeric IDs -- we should be using
107 * pthread_equal() instead of assuming we can compare thread ids...
108 */
109 #ifdef _WIN32
110 return GetCurrentThreadId();
111 #else
112 return (unsigned long) (uintptr_t) thrd_current();
113 #endif
114 }
115
116
117 static INLINE void
118 u_tsd_init(struct u_tsd *tsd)
119 {
120 if (tss_create(&tsd->key, NULL/*free*/) != 0) {
121 perror(INIT_TSD_ERROR);
122 exit(-1);
123 }
124 tsd->initMagic = INIT_MAGIC;
125 }
126
127
128 static INLINE void *
129 u_tsd_get(struct u_tsd *tsd)
130 {
131 if (tsd->initMagic != INIT_MAGIC) {
132 u_tsd_init(tsd);
133 }
134 return tss_get(tsd->key);
135 }
136
137
138 static INLINE void
139 u_tsd_set(struct u_tsd *tsd, void *ptr)
140 {
141 if (tsd->initMagic != INIT_MAGIC) {
142 u_tsd_init(tsd);
143 }
144 if (tss_set(tsd->key, ptr) != 0) {
145 perror(SET_TSD_ERROR);
146 exit(-1);
147 }
148 }
149
150
151 static INLINE void
152 u_tsd_destroy(struct u_tsd *tsd)
153 {
154 if (tsd->initMagic != INIT_MAGIC) {
155 return;
156 }
157 tss_delete(tsd->key);
158 tsd->initMagic = 0x0;
159 }
160
161
162 #ifdef __cplusplus
163 }
164 #endif
165
166 #endif /* _U_THREAD_H_ */