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) \
93 if (pthread_in_use ()) \
95 if (pthread_key_create (&(NAME).key, DESTRUCTOR) != 0) \
99 (NAME).singlethread_value = NULL
100 # define gl_tls_get(NAME) \
102 ? pthread_getspecific ((NAME).key) \
103 : (NAME).singlethread_value)
104 # define gl_tls_set(NAME, POINTER) \
105 if (pthread_in_use ()) \
107 if (pthread_setspecific ((NAME).key, (POINTER)) != 0) \
111 (NAME).singlethread_value = (POINTER)
112 # define gl_tls_key_destroy(NAME) \
113 if (pthread_in_use () && pthread_key_delete ((NAME).key) != 0) \
118 /* ========================================================================= */
122 /* Use the GNU Pth threads library. */
127 # if USE_PTH_THREADS_WEAK
129 /* Use weak references to the GNU Pth threads library. */
131 # pragma weak pth_key_create
132 # pragma weak pth_key_getdata
133 # pragma weak pth_key_setdata
134 # pragma weak pth_key_delete
136 # pragma weak pth_cancel
137 # define pth_in_use() (pth_cancel != NULL)
141 # define pth_in_use() 1
145 /* ------------------------- gl_tls_key_t datatype ------------------------- */
149 void *singlethread_value;
153 # define gl_tls_key_init(NAME, DESTRUCTOR) \
156 if (!pth_key_create (&(NAME).key, DESTRUCTOR)) \
160 (NAME).singlethread_value = NULL
161 # define gl_tls_get(NAME) \
163 ? pth_key_getdata ((NAME).key) \
164 : (NAME).singlethread_value)
165 # define gl_tls_set(NAME, POINTER) \
168 if (!pth_key_setdata ((NAME).key, (POINTER))) \
172 (NAME).singlethread_value = (POINTER)
173 # define gl_tls_key_destroy(NAME) \
174 if (pth_in_use () && !pth_key_delete ((NAME).key)) \
179 /* ========================================================================= */
181 #if USE_SOLARIS_THREADS
183 /* Use the old Solaris threads library. */
188 # if USE_SOLARIS_THREADS_WEAK
190 /* Use weak references to the old Solaris threads library. */
192 # pragma weak thr_keycreate
193 # pragma weak thr_getspecific
194 # pragma weak thr_setspecific
196 # pragma weak thr_suspend
197 # define thread_in_use() (thr_suspend != NULL)
201 # define thread_in_use() 1
205 /* ------------------------- gl_tls_key_t datatype ------------------------- */
209 void *singlethread_value;
213 # define gl_tls_key_init(NAME, DESTRUCTOR) \
214 if (thread_in_use ()) \
216 if (thr_keycreate (&(NAME).key, DESTRUCTOR) != 0) \
220 (NAME).singlethread_value = NULL
221 # define gl_tls_get(NAME) \
223 ? glthread_tls_get ((NAME).key) \
224 : (NAME).singlethread_value)
225 extern void *glthread_tls_get (thread_key_t key);
226 # define gl_tls_set(NAME, POINTER) \
227 if (thread_in_use ()) \
229 if (thr_setspecific ((NAME).key, (POINTER)) != 0) \
233 (NAME).singlethread_value = (POINTER)
234 # define gl_tls_key_destroy(NAME) \
240 /* ========================================================================= */
242 #if USE_WIN32_THREADS
244 # include <windows.h>
246 /* ------------------------- gl_tls_key_t datatype ------------------------- */
248 typedef DWORD gl_tls_key_t;
249 # define gl_tls_key_init(NAME, DESTRUCTOR) \
250 /* The destructor is unsupported. */ \
251 if (((NAME) = TlsAlloc ()) == (DWORD)-1) \
253 # define gl_tls_get(NAME) \
255 # define gl_tls_set(NAME, POINTER) \
256 if (!TlsSetValue (NAME, POINTER)) \
258 # define gl_tls_key_destroy(NAME) \
259 if (!TlsFree (NAME)) \
264 /* ========================================================================= */
266 #if !(USE_POSIX_THREADS || USE_PTH_THREADS || USE_SOLARIS_THREADS || USE_WIN32_THREADS)
268 /* Provide dummy implementation if threads are not supported. */
270 /* ------------------------- gl_tls_key_t datatype ------------------------- */
274 void *singlethread_value;
277 # define gl_tls_key_init(NAME, DESTRUCTOR) \
278 (NAME).singlethread_value = NULL
279 # define gl_tls_get(NAME) \
280 (NAME).singlethread_value
281 # define gl_tls_set(NAME, POINTER) \
282 (NAME).singlethread_value = (POINTER)
283 # define gl_tls_key_destroy(NAME) \