2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
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:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
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.
27 * Thread support for gl dispatch.
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
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.
47 #include "u_compiler.h"
49 #include "c11/threads.h"
51 #if defined(HAVE_PTHREAD) || defined(_WIN32)
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"
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
70 #define INIT_MAGIC 0xff8adc98
83 static INLINE
unsigned long
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
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...
100 return GetCurrentThreadId();
102 return (unsigned long) (uintptr_t) thrd_current();
108 u_tsd_init(struct u_tsd
*tsd
)
110 if (tss_create(&tsd
->key
, NULL
/*free*/) != 0) {
111 perror(INIT_TSD_ERROR
);
114 tsd
->initMagic
= INIT_MAGIC
;
119 u_tsd_get(struct u_tsd
*tsd
)
121 if (tsd
->initMagic
!= INIT_MAGIC
) {
124 return tss_get(tsd
->key
);
129 u_tsd_set(struct u_tsd
*tsd
, void *ptr
)
131 if (tsd
->initMagic
!= INIT_MAGIC
) {
134 if (tss_set(tsd
->key
, ptr
) != 0) {
135 perror(SET_TSD_ERROR
);
142 u_tsd_destroy(struct u_tsd
*tsd
)
144 if (tsd
->initMagic
!= INIT_MAGIC
) {
147 tss_delete(tsd
->key
);
148 tsd
->initMagic
= 0x0;
156 #endif /* _U_THREAD_H_ */