1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 #ifndef _TLS_H
44 #define _TLS_H
45
46 #include <errno.h>
47 #include <stdlib.h>
48
49 #if !defined c11_threads_in_use
50 # if HAVE_THREADS_H && USE_POSIX_THREADS_FROM_LIBC
51 # define c11_threads_in_use() 1
52 # elif HAVE_THREADS_H && USE_POSIX_THREADS_WEAK
53 # include <threads.h>
54 # pragma weak thrd_exit
55 # define c11_threads_in_use() (thrd_exit != NULL)
56 # else
57 # define c11_threads_in_use() 0
58 # endif
59 #endif
60
61
62
63 #if USE_ISOC_THREADS || USE_ISOC_AND_POSIX_THREADS
64
65
66
67 # include <threads.h>
68
69
70
71 typedef tss_t gl_tls_key_t;
72 # define glthread_tls_key_init(KEY, DESTRUCTOR) \
73 (tss_create (KEY, DESTRUCTOR) != thrd_success ? EAGAIN : 0)
74 # define gl_tls_get(NAME) \
75 tss_get (NAME)
76 # define glthread_tls_set(KEY, POINTER) \
77 (tss_set (*(KEY), (POINTER)) != thrd_success ? ENOMEM : 0)
78 # define glthread_tls_key_destroy(KEY) \
79 (tss_delete (*(KEY)), 0)
80
81 #endif
82
83
84
85 #if USE_POSIX_THREADS
86
87
88
89 # include <pthread.h>
90
91 # if PTHREAD_IN_USE_DETECTION_HARD
92
93
94 # define pthread_in_use() \
95 glthread_in_use ()
96 extern int glthread_in_use (void);
97
98 # endif
99
100 # if USE_POSIX_THREADS_WEAK
101
102
103
104 # pragma weak pthread_key_create
105 # pragma weak pthread_getspecific
106 # pragma weak pthread_setspecific
107 # pragma weak pthread_key_delete
108 # ifndef pthread_self
109 # pragma weak pthread_self
110 # endif
111
112 # if !PTHREAD_IN_USE_DETECTION_HARD
113 # pragma weak pthread_mutexattr_gettype
114 # define pthread_in_use() \
115 (pthread_mutexattr_gettype != NULL || c11_threads_in_use ())
116 # endif
117
118 # else
119
120 # if !PTHREAD_IN_USE_DETECTION_HARD
121 # define pthread_in_use() 1
122 # endif
123
124 # endif
125
126
127
128 typedef union
129 {
130 void *singlethread_value;
131 pthread_key_t key;
132 }
133 gl_tls_key_t;
134 # define glthread_tls_key_init(KEY, DESTRUCTOR) \
135 (pthread_in_use () \
136 ? pthread_key_create (&(KEY)->key, DESTRUCTOR) \
137 : ((KEY)->singlethread_value = NULL, 0))
138 # define gl_tls_get(NAME) \
139 (pthread_in_use () \
140 ? pthread_getspecific ((NAME).key) \
141 : (NAME).singlethread_value)
142 # define glthread_tls_set(KEY, POINTER) \
143 (pthread_in_use () \
144 ? pthread_setspecific ((KEY)->key, (POINTER)) \
145 : ((KEY)->singlethread_value = (POINTER), 0))
146 # define glthread_tls_key_destroy(KEY) \
147 (pthread_in_use () ? pthread_key_delete ((KEY)->key) : 0)
148
149 #endif
150
151
152
153 #if USE_WINDOWS_THREADS
154
155 # define WIN32_LEAN_AND_MEAN
156 # include <windows.h>
157
158 # include "windows-tls.h"
159
160
161
162 typedef glwthread_tls_key_t gl_tls_key_t;
163 # define glthread_tls_key_init(KEY, DESTRUCTOR) \
164 glwthread_tls_key_create (KEY, DESTRUCTOR)
165 # define gl_tls_get(NAME) \
166 TlsGetValue (NAME)
167 # define glthread_tls_set(KEY, POINTER) \
168 (!TlsSetValue (*(KEY), POINTER) ? EINVAL : 0)
169 # define glthread_tls_key_destroy(KEY) \
170 glwthread_tls_key_delete (*(KEY))
171
172 #endif
173
174
175
176 #if !(USE_ISOC_THREADS || USE_POSIX_THREADS || USE_ISOC_AND_POSIX_THREADS || USE_WINDOWS_THREADS)
177
178
179
180
181
182 typedef struct
183 {
184 void *singlethread_value;
185 }
186 gl_tls_key_t;
187 # define glthread_tls_key_init(KEY, DESTRUCTOR) \
188 ((KEY)->singlethread_value = NULL, \
189 (void) (DESTRUCTOR), \
190 0)
191 # define gl_tls_get(NAME) \
192 (NAME).singlethread_value
193 # define glthread_tls_set(KEY, POINTER) \
194 ((KEY)->singlethread_value = (POINTER), 0)
195 # define glthread_tls_key_destroy(KEY) \
196 0
197
198 #endif
199
200
201
202
203
204
205
206 #define gl_tls_key_init(NAME, DESTRUCTOR) \
207 do \
208 { \
209 if (glthread_tls_key_init (&NAME, DESTRUCTOR)) \
210 abort (); \
211 } \
212 while (0)
213 #define gl_tls_set(NAME, POINTER) \
214 do \
215 { \
216 if (glthread_tls_set (&NAME, POINTER)) \
217 abort (); \
218 } \
219 while (0)
220 #define gl_tls_key_destroy(NAME) \
221 do \
222 { \
223 if (glthread_tls_key_destroy (&NAME)) \
224 abort (); \
225 } \
226 while (0)
227
228
229
230 #endif