nv30,nvc0: only claim a single viewport
[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 static INLINE unsigned long
93 u_thread_self(void)
94 {
95 return (unsigned long) (uintptr_t) thrd_current();
96 }
97
98
99 static INLINE void
100 u_tsd_init(struct u_tsd *tsd)
101 {
102 if (tss_create(&tsd->key, NULL/*free*/) != 0) {
103 perror(INIT_TSD_ERROR);
104 exit(-1);
105 }
106 tsd->initMagic = INIT_MAGIC;
107 }
108
109
110 static INLINE void *
111 u_tsd_get(struct u_tsd *tsd)
112 {
113 if (tsd->initMagic != INIT_MAGIC) {
114 u_tsd_init(tsd);
115 }
116 return tss_get(tsd->key);
117 }
118
119
120 static INLINE void
121 u_tsd_set(struct u_tsd *tsd, void *ptr)
122 {
123 if (tsd->initMagic != INIT_MAGIC) {
124 u_tsd_init(tsd);
125 }
126 if (tss_set(tsd->key, ptr) != 0) {
127 perror(SET_TSD_ERROR);
128 exit(-1);
129 }
130 }
131
132
133 static INLINE void
134 u_tsd_destroy(struct u_tsd *tsd)
135 {
136 if (tsd->initMagic != INIT_MAGIC) {
137 return;
138 }
139 tss_delete(tsd->key);
140 tsd->initMagic = 0x0;
141 }
142
143
144 #ifdef __cplusplus
145 }
146 #endif
147
148 #endif /* _U_THREAD_H_ */