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
44 /* ========================================================================= */
48 /* Use the POSIX threads library. */
53 # if PTHREAD_IN_USE_DETECTION_HARD
55 /* The pthread_in_use() detection needs to be done at runtime. */
56 # define pthread_in_use() \
58 extern int glthread_in_use (void);
62 # if USE_POSIX_THREADS_WEAK
64 /* Use weak references to the POSIX threads library. */
66 # pragma weak pthread_key_create
67 # pragma weak pthread_getspecific
68 # pragma weak pthread_setspecific
69 # pragma weak pthread_key_delete
71 # pragma weak pthread_self
74 # if !PTHREAD_IN_USE_DETECTION_HARD
75 # pragma weak pthread_cancel
76 # define pthread_in_use() (pthread_cancel != NULL)
81 # if !PTHREAD_IN_USE_DETECTION_HARD
82 # define pthread_in_use() 1
87 /* ------------------------- gl_tls_key_t datatype ------------------------- */
91 void *singlethread_value;
95 # define gl_tls_key_init(NAME, DESTRUCTOR) \
98 if (pthread_in_use ()) \
100 if (pthread_key_create (&(NAME).key, DESTRUCTOR) != 0) \
104 (NAME).singlethread_value = NULL; \
107 # define gl_tls_get(NAME) \
109 ? pthread_getspecific ((NAME).key) \
110 : (NAME).singlethread_value)
111 # define gl_tls_set(NAME, POINTER) \
114 if (pthread_in_use ()) \
116 if (pthread_setspecific ((NAME).key, (POINTER)) != 0) \
120 (NAME).singlethread_value = (POINTER); \
123 # define gl_tls_key_destroy(NAME) \
124 if (pthread_in_use () && pthread_key_delete ((NAME).key) != 0) \
129 /* ========================================================================= */
133 /* Use the GNU Pth threads library. */
138 # if USE_PTH_THREADS_WEAK
140 /* Use weak references to the GNU Pth threads library. */
142 # pragma weak pth_key_create
143 # pragma weak pth_key_getdata
144 # pragma weak pth_key_setdata
145 # pragma weak pth_key_delete
147 # pragma weak pth_cancel
148 # define pth_in_use() (pth_cancel != NULL)
152 # define pth_in_use() 1
156 /* ------------------------- gl_tls_key_t datatype ------------------------- */
160 void *singlethread_value;
164 # define gl_tls_key_init(NAME, DESTRUCTOR) \
169 if (!pth_key_create (&(NAME).key, DESTRUCTOR)) \
173 (NAME).singlethread_value = NULL; \
176 # define gl_tls_get(NAME) \
178 ? pth_key_getdata ((NAME).key) \
179 : (NAME).singlethread_value)
180 # define gl_tls_set(NAME, POINTER) \
185 if (!pth_key_setdata ((NAME).key, (POINTER))) \
189 (NAME).singlethread_value = (POINTER); \
192 # define gl_tls_key_destroy(NAME) \
193 if (pth_in_use () && !pth_key_delete ((NAME).key)) \
198 /* ========================================================================= */
200 #if USE_SOLARIS_THREADS
202 /* Use the old Solaris threads library. */
207 # if USE_SOLARIS_THREADS_WEAK
209 /* Use weak references to the old Solaris threads library. */
211 # pragma weak thr_keycreate
212 # pragma weak thr_getspecific
213 # pragma weak thr_setspecific
215 # pragma weak thr_suspend
216 # define thread_in_use() (thr_suspend != NULL)
220 # define thread_in_use() 1
224 /* ------------------------- gl_tls_key_t datatype ------------------------- */
228 void *singlethread_value;
232 # define gl_tls_key_init(NAME, DESTRUCTOR) \
235 if (thread_in_use ()) \
237 if (thr_keycreate (&(NAME).key, DESTRUCTOR) != 0) \
241 (NAME).singlethread_value = NULL; \
244 # define gl_tls_get(NAME) \
246 ? glthread_tls_get ((NAME).key) \
247 : (NAME).singlethread_value)
248 extern void *glthread_tls_get (thread_key_t key);
249 # define gl_tls_set(NAME, POINTER) \
252 if (thread_in_use ()) \
254 if (thr_setspecific ((NAME).key, (POINTER)) != 0) \
258 (NAME).singlethread_value = (POINTER); \
261 # define gl_tls_key_destroy(NAME) \
267 /* ========================================================================= */
269 #if USE_WIN32_THREADS
271 # include <windows.h>
273 /* ------------------------- gl_tls_key_t datatype ------------------------- */
275 typedef DWORD gl_tls_key_t;
276 # define gl_tls_key_init(NAME, DESTRUCTOR) \
277 /* The destructor is unsupported. */ \
278 if (((NAME) = TlsAlloc ()) == (DWORD)-1) \
280 # define gl_tls_get(NAME) \
282 # define gl_tls_set(NAME, POINTER) \
283 if (!TlsSetValue (NAME, POINTER)) \
285 # define gl_tls_key_destroy(NAME) \
286 if (!TlsFree (NAME)) \
291 /* ========================================================================= */
293 #if !(USE_POSIX_THREADS || USE_PTH_THREADS || USE_SOLARIS_THREADS || USE_WIN32_THREADS)
295 /* Provide dummy implementation if threads are not supported. */
297 /* ------------------------- gl_tls_key_t datatype ------------------------- */
301 void *singlethread_value;
304 # define gl_tls_key_init(NAME, DESTRUCTOR) \
305 (NAME).singlethread_value = NULL
306 # define gl_tls_get(NAME) \
307 (NAME).singlethread_value
308 # define gl_tls_set(NAME, POINTER) \
309 (NAME).singlethread_value = (POINTER)
310 # define gl_tls_key_destroy(NAME) \
315 /* ========================================================================= */