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
5 it under the terms of the GNU General Public License as published by
6 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
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, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Written by Bruno Haible <bruno@clisp.org>, 2005. */
20 /* This file contains thread-local storage primitives for use with a given
21 thread library. It does not contain primitives for creating threads or
22 for other multithreading primitives.
25 Initialization: gl_tls_key_init (name, destructor);
26 Getting per-thread value: gl_tls_get (name)
27 Setting per-thread value: gl_tls_set (name, pointer);
28 De-initialization: gl_tls_key_destroy (name);
30 A per-thread value is of type 'void *'.
32 A destructor is a function pointer of type 'void (*) (void *)', called
33 when a thread exits, and taking the last per-thread value as argument. It
34 is unspecified whether the destructor function is called when the last
35 per-thread value is NULL. On some platforms, the destructor function is
43 /* ========================================================================= */
47 /* Use the POSIX threads library. */
52 # if PTHREAD_IN_USE_DETECTION_HARD
54 /* The pthread_in_use() detection needs to be done at runtime. */
55 # define pthread_in_use() \
57 extern int glthread_in_use (void);
61 # if USE_POSIX_THREADS_WEAK
63 /* Use weak references to the POSIX threads library. */
65 # pragma weak pthread_key_create
66 # pragma weak pthread_getspecific
67 # pragma weak pthread_setspecific
68 # pragma weak pthread_key_delete
70 # pragma weak pthread_self
73 # if !PTHREAD_IN_USE_DETECTION_HARD
74 # pragma weak pthread_cancel
75 # define pthread_in_use() (pthread_cancel != NULL)
80 # if !PTHREAD_IN_USE_DETECTION_HARD
81 # define pthread_in_use() 1
86 /* ------------------------- gl_tls_key_t datatype ------------------------- */
90 void *singlethread_value;
94 # define gl_tls_key_init(NAME, DESTRUCTOR) \
97 if (pthread_in_use ()) \
99 if (pthread_key_create (&(NAME).key, DESTRUCTOR) != 0) \
103 (NAME).singlethread_value = NULL; \
106 # define gl_tls_get(NAME) \
108 ? pthread_getspecific ((NAME).key) \
109 : (NAME).singlethread_value)
110 # define gl_tls_set(NAME, POINTER) \
113 if (pthread_in_use ()) \
115 if (pthread_setspecific ((NAME).key, (POINTER)) != 0) \
119 (NAME).singlethread_value = (POINTER); \
122 # define gl_tls_key_destroy(NAME) \
123 if (pthread_in_use () && pthread_key_delete ((NAME).key) != 0) \
128 /* ========================================================================= */
132 /* Use the GNU Pth threads library. */
137 # if USE_PTH_THREADS_WEAK
139 /* Use weak references to the GNU Pth threads library. */
141 # pragma weak pth_key_create
142 # pragma weak pth_key_getdata
143 # pragma weak pth_key_setdata
144 # pragma weak pth_key_delete
146 # pragma weak pth_cancel
147 # define pth_in_use() (pth_cancel != NULL)
151 # define pth_in_use() 1
155 /* ------------------------- gl_tls_key_t datatype ------------------------- */
159 void *singlethread_value;
163 # define gl_tls_key_init(NAME, DESTRUCTOR) \
168 if (!pth_key_create (&(NAME).key, DESTRUCTOR)) \
172 (NAME).singlethread_value = NULL; \
175 # define gl_tls_get(NAME) \
177 ? pth_key_getdata ((NAME).key) \
178 : (NAME).singlethread_value)
179 # define gl_tls_set(NAME, POINTER) \
184 if (!pth_key_setdata ((NAME).key, (POINTER))) \
188 (NAME).singlethread_value = (POINTER); \
191 # define gl_tls_key_destroy(NAME) \
192 if (pth_in_use () && !pth_key_delete ((NAME).key)) \
197 /* ========================================================================= */
199 #if USE_SOLARIS_THREADS
201 /* Use the old Solaris threads library. */
206 # if USE_SOLARIS_THREADS_WEAK
208 /* Use weak references to the old Solaris threads library. */
210 # pragma weak thr_keycreate
211 # pragma weak thr_getspecific
212 # pragma weak thr_setspecific
214 # pragma weak thr_suspend
215 # define thread_in_use() (thr_suspend != NULL)
219 # define thread_in_use() 1
223 /* ------------------------- gl_tls_key_t datatype ------------------------- */
227 void *singlethread_value;
231 # define gl_tls_key_init(NAME, DESTRUCTOR) \
234 if (thread_in_use ()) \
236 if (thr_keycreate (&(NAME).key, DESTRUCTOR) != 0) \
240 (NAME).singlethread_value = NULL; \
243 # define gl_tls_get(NAME) \
245 ? glthread_tls_get ((NAME).key) \
246 : (NAME).singlethread_value)
247 extern void *glthread_tls_get (thread_key_t key);
248 # define gl_tls_set(NAME, POINTER) \
251 if (thread_in_use ()) \
253 if (thr_setspecific ((NAME).key, (POINTER)) != 0) \
257 (NAME).singlethread_value = (POINTER); \
260 # define gl_tls_key_destroy(NAME) \
266 /* ========================================================================= */
268 #if USE_WIN32_THREADS
270 # include <windows.h>
272 /* ------------------------- gl_tls_key_t datatype ------------------------- */
274 typedef DWORD gl_tls_key_t;
275 # define gl_tls_key_init(NAME, DESTRUCTOR) \
276 /* The destructor is unsupported. */ \
277 if (((NAME) = TlsAlloc ()) == (DWORD)-1) \
279 # define gl_tls_get(NAME) \
281 # define gl_tls_set(NAME, POINTER) \
282 if (!TlsSetValue (NAME, POINTER)) \
284 # define gl_tls_key_destroy(NAME) \
285 if (!TlsFree (NAME)) \
290 /* ========================================================================= */
292 #if !(USE_POSIX_THREADS || USE_PTH_THREADS || USE_SOLARIS_THREADS || USE_WIN32_THREADS)
294 /* Provide dummy implementation if threads are not supported. */
296 /* ------------------------- gl_tls_key_t datatype ------------------------- */
300 void *singlethread_value;
303 # define gl_tls_key_init(NAME, DESTRUCTOR) \
304 (NAME).singlethread_value = NULL
305 # define gl_tls_get(NAME) \
306 (NAME).singlethread_value
307 # define gl_tls_set(NAME, POINTER) \
308 (NAME).singlethread_value = (POINTER)
309 # define gl_tls_key_destroy(NAME) \
314 /* ========================================================================= */