1 /* Thread-local storage in multithreaded situations.
2 Copyright (C) 2005, 2007-2008 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
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
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2005. */
19 /* This file contains thread-local storage primitives for use with a given
20 thread library. It does not contain primitives for creating threads or
21 for other multithreading primitives.
24 Initialization: gl_tls_key_init (name, destructor);
25 Getting per-thread value: gl_tls_get (name)
26 Setting per-thread value: gl_tls_set (name, pointer);
27 De-initialization: gl_tls_key_destroy (name);
29 A per-thread value is of type 'void *'.
31 A destructor is a function pointer of type 'void (*) (void *)', called
32 when a thread exits, and taking the last per-thread value as argument. It
33 is unspecified whether the destructor function is called when the last
34 per-thread value is NULL. On some platforms, the destructor function is
42 /* ========================================================================= */
46 /* Use the POSIX threads library. */
51 # if PTHREAD_IN_USE_DETECTION_HARD
53 /* The pthread_in_use() detection needs to be done at runtime. */
54 # define pthread_in_use() \
56 extern int glthread_in_use (void);
60 # if USE_POSIX_THREADS_WEAK
62 /* Use weak references to the POSIX threads library. */
64 # pragma weak pthread_key_create
65 # pragma weak pthread_getspecific
66 # pragma weak pthread_setspecific
67 # pragma weak pthread_key_delete
69 # pragma weak pthread_self
72 # if !PTHREAD_IN_USE_DETECTION_HARD
73 # pragma weak pthread_cancel
74 # define pthread_in_use() (pthread_cancel != NULL)
79 # if !PTHREAD_IN_USE_DETECTION_HARD
80 # define pthread_in_use() 1
85 /* ------------------------- gl_tls_key_t datatype ------------------------- */
89 void *singlethread_value;
93 # define gl_tls_key_init(NAME, DESTRUCTOR) \
96 if (pthread_in_use ()) \
98 if (pthread_key_create (&(NAME).key, DESTRUCTOR) != 0) \
102 (NAME).singlethread_value = NULL; \
105 # define gl_tls_get(NAME) \
107 ? pthread_getspecific ((NAME).key) \
108 : (NAME).singlethread_value)
109 # define gl_tls_set(NAME, POINTER) \
112 if (pthread_in_use ()) \
114 if (pthread_setspecific ((NAME).key, (POINTER)) != 0) \
118 (NAME).singlethread_value = (POINTER); \
121 # define gl_tls_key_destroy(NAME) \
124 if (pthread_in_use () && pthread_key_delete ((NAME).key) != 0) \
131 /* ========================================================================= */
135 /* Use the GNU Pth threads library. */
140 # if USE_PTH_THREADS_WEAK
142 /* Use weak references to the GNU Pth threads library. */
144 # pragma weak pth_key_create
145 # pragma weak pth_key_getdata
146 # pragma weak pth_key_setdata
147 # pragma weak pth_key_delete
149 # pragma weak pth_cancel
150 # define pth_in_use() (pth_cancel != NULL)
154 # define pth_in_use() 1
158 /* ------------------------- gl_tls_key_t datatype ------------------------- */
162 void *singlethread_value;
166 # define gl_tls_key_init(NAME, DESTRUCTOR) \
171 if (!pth_key_create (&(NAME).key, DESTRUCTOR)) \
175 (NAME).singlethread_value = NULL; \
178 # define gl_tls_get(NAME) \
180 ? pth_key_getdata ((NAME).key) \
181 : (NAME).singlethread_value)
182 # define gl_tls_set(NAME, POINTER) \
187 if (!pth_key_setdata ((NAME).key, (POINTER))) \
191 (NAME).singlethread_value = (POINTER); \
194 # define gl_tls_key_destroy(NAME) \
197 if (pth_in_use () && !pth_key_delete ((NAME).key)) \
204 /* ========================================================================= */
206 #if USE_SOLARIS_THREADS
208 /* Use the old Solaris threads library. */
213 # if USE_SOLARIS_THREADS_WEAK
215 /* Use weak references to the old Solaris threads library. */
217 # pragma weak thr_keycreate
218 # pragma weak thr_getspecific
219 # pragma weak thr_setspecific
221 # pragma weak thr_suspend
222 # define thread_in_use() (thr_suspend != NULL)
226 # define thread_in_use() 1
230 /* ------------------------- gl_tls_key_t datatype ------------------------- */
234 void *singlethread_value;
238 # define gl_tls_key_init(NAME, DESTRUCTOR) \
241 if (thread_in_use ()) \
243 if (thr_keycreate (&(NAME).key, DESTRUCTOR) != 0) \
247 (NAME).singlethread_value = NULL; \
250 # define gl_tls_get(NAME) \
252 ? glthread_tls_get ((NAME).key) \
253 : (NAME).singlethread_value)
254 extern void *glthread_tls_get (thread_key_t key);
255 # define gl_tls_set(NAME, POINTER) \
258 if (thread_in_use ()) \
260 if (thr_setspecific ((NAME).key, (POINTER)) != 0) \
264 (NAME).singlethread_value = (POINTER); \
267 # define gl_tls_key_destroy(NAME) \
273 /* ========================================================================= */
275 #if USE_WIN32_THREADS
277 # include <windows.h>
279 /* ------------------------- gl_tls_key_t datatype ------------------------- */
281 typedef DWORD gl_tls_key_t;
282 # define gl_tls_key_init(NAME, DESTRUCTOR) \
283 /* The destructor is unsupported. */ \
286 if (((NAME) = TlsAlloc ()) == (DWORD)-1) \
288 (void) (DESTRUCTOR); \
291 # define gl_tls_get(NAME) \
293 # define gl_tls_set(NAME, POINTER) \
296 if (!TlsSetValue (NAME, POINTER)) \
300 # define gl_tls_key_destroy(NAME) \
303 if (!TlsFree (NAME)) \
310 /* ========================================================================= */
312 #if !(USE_POSIX_THREADS || USE_PTH_THREADS || USE_SOLARIS_THREADS || USE_WIN32_THREADS)
314 /* Provide dummy implementation if threads are not supported. */
316 /* ------------------------- gl_tls_key_t datatype ------------------------- */
320 void *singlethread_value;
323 # define gl_tls_key_init(NAME, DESTRUCTOR) \
324 ((NAME).singlethread_value = NULL, \
326 # define gl_tls_get(NAME) \
327 (NAME).singlethread_value
328 # define gl_tls_set(NAME, POINTER) \
329 (NAME).singlethread_value = (POINTER)
330 # define gl_tls_key_destroy(NAME) \
335 /* ========================================================================= */