1 /* Thread-local storage in multithreaded situations.
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published
6 by the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 /* Written by Bruno Haible <bruno@clisp.org>, 2005. */
21 /* This file contains thread-local storage primitives for use with a given
22 thread library. It does not contain primitives for creating threads or
23 for other multithreading primitives.
26 Initialization: gl_tls_key_init (name, destructor);
27 Getting per-thread value: gl_tls_get (name)
28 Setting per-thread value: gl_tls_set (name, pointer);
29 De-initialization: gl_tls_key_destroy (name);
31 A per-thread value is of type 'void *'.
33 A destructor is a function pointer of type 'void (*) (void *)', called
34 when a thread exits, and taking the last per-thread value as argument. It
35 is unspecified whether the destructor function is called when the last
36 per-thread value is NULL. On some platforms, the destructor function is
41 /* ========================================================================= */
45 /* Use the POSIX threads library. */
50 # if PTHREAD_IN_USE_DETECTION_HARD
52 /* The pthread_in_use() detection needs to be done at runtime. */
53 # define pthread_in_use() \
55 extern int glthread_in_use (void);
59 # if USE_POSIX_THREADS_WEAK
61 /* Use weak references to the POSIX threads library. */
63 # pragma weak pthread_key_create
64 # pragma weak pthread_getspecific
65 # pragma weak pthread_setspecific
66 # pragma weak pthread_key_delete
68 # pragma weak pthread_self
71 # if !PTHREAD_IN_USE_DETECTION_HARD
72 # pragma weak pthread_cancel
73 # define pthread_in_use() (pthread_cancel != NULL)
78 # if !PTHREAD_IN_USE_DETECTION_HARD
79 # define pthread_in_use() 1
84 /* ------------------------- gl_tls_key_t datatype ------------------------- */
88 void *singlethread_value;
92 # define gl_tls_key_init(NAME, DESTRUCTOR) \
95 if (pthread_in_use ()) \
97 if (pthread_key_create (&(NAME).key, DESTRUCTOR) != 0) \
101 (NAME).singlethread_value = NULL; \
104 # define gl_tls_get(NAME) \
106 ? pthread_getspecific ((NAME).key) \
107 : (NAME).singlethread_value)
108 # define gl_tls_set(NAME, POINTER) \
111 if (pthread_in_use ()) \
113 if (pthread_setspecific ((NAME).key, (POINTER)) != 0) \
117 (NAME).singlethread_value = (POINTER); \
120 # define gl_tls_key_destroy(NAME) \
121 if (pthread_in_use () && pthread_key_delete ((NAME).key) != 0) \
126 /* ========================================================================= */
130 /* Use the GNU Pth threads library. */
135 # if USE_PTH_THREADS_WEAK
137 /* Use weak references to the GNU Pth threads library. */
139 # pragma weak pth_key_create
140 # pragma weak pth_key_getdata
141 # pragma weak pth_key_setdata
142 # pragma weak pth_key_delete
144 # pragma weak pth_cancel
145 # define pth_in_use() (pth_cancel != NULL)
149 # define pth_in_use() 1
153 /* ------------------------- gl_tls_key_t datatype ------------------------- */
157 void *singlethread_value;
161 # define gl_tls_key_init(NAME, DESTRUCTOR) \
166 if (!pth_key_create (&(NAME).key, DESTRUCTOR)) \
170 (NAME).singlethread_value = NULL; \
173 # define gl_tls_get(NAME) \
175 ? pth_key_getdata ((NAME).key) \
176 : (NAME).singlethread_value)
177 # define gl_tls_set(NAME, POINTER) \
182 if (!pth_key_setdata ((NAME).key, (POINTER))) \
186 (NAME).singlethread_value = (POINTER); \
189 # define gl_tls_key_destroy(NAME) \
190 if (pth_in_use () && !pth_key_delete ((NAME).key)) \
195 /* ========================================================================= */
197 #if USE_SOLARIS_THREADS
199 /* Use the old Solaris threads library. */
204 # if USE_SOLARIS_THREADS_WEAK
206 /* Use weak references to the old Solaris threads library. */
208 # pragma weak thr_keycreate
209 # pragma weak thr_getspecific
210 # pragma weak thr_setspecific
212 # pragma weak thr_suspend
213 # define thread_in_use() (thr_suspend != NULL)
217 # define thread_in_use() 1
221 /* ------------------------- gl_tls_key_t datatype ------------------------- */
225 void *singlethread_value;
229 # define gl_tls_key_init(NAME, DESTRUCTOR) \
232 if (thread_in_use ()) \
234 if (thr_keycreate (&(NAME).key, DESTRUCTOR) != 0) \
238 (NAME).singlethread_value = NULL; \
241 # define gl_tls_get(NAME) \
243 ? glthread_tls_get ((NAME).key) \
244 : (NAME).singlethread_value)
245 extern void *glthread_tls_get (thread_key_t key);
246 # define gl_tls_set(NAME, POINTER) \
249 if (thread_in_use ()) \
251 if (thr_setspecific ((NAME).key, (POINTER)) != 0) \
255 (NAME).singlethread_value = (POINTER); \
258 # define gl_tls_key_destroy(NAME) \
264 /* ========================================================================= */
266 #if USE_WIN32_THREADS
268 # include <windows.h>
270 /* ------------------------- gl_tls_key_t datatype ------------------------- */
272 typedef DWORD gl_tls_key_t;
273 # define gl_tls_key_init(NAME, DESTRUCTOR) \
274 /* The destructor is unsupported. */ \
275 if (((NAME) = TlsAlloc ()) == (DWORD)-1) \
277 # define gl_tls_get(NAME) \
279 # define gl_tls_set(NAME, POINTER) \
280 if (!TlsSetValue (NAME, POINTER)) \
282 # define gl_tls_key_destroy(NAME) \
283 if (!TlsFree (NAME)) \
288 /* ========================================================================= */
290 #if !(USE_POSIX_THREADS || USE_PTH_THREADS || USE_SOLARIS_THREADS || USE_WIN32_THREADS)
292 /* Provide dummy implementation if threads are not supported. */
294 /* ------------------------- gl_tls_key_t datatype ------------------------- */
298 void *singlethread_value;
301 # define gl_tls_key_init(NAME, DESTRUCTOR) \
302 (NAME).singlethread_value = NULL
303 # define gl_tls_get(NAME) \
304 (NAME).singlethread_value
305 # define gl_tls_set(NAME, POINTER) \
306 (NAME).singlethread_value = (POINTER)
307 # define gl_tls_key_destroy(NAME) \