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 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) \
122 if (pthread_in_use () && pthread_key_delete ((NAME).key) != 0) \
127 /* ========================================================================= */
131 /* Use the GNU Pth threads library. */
136 # if USE_PTH_THREADS_WEAK
138 /* Use weak references to the GNU Pth threads library. */
140 # pragma weak pth_key_create
141 # pragma weak pth_key_getdata
142 # pragma weak pth_key_setdata
143 # pragma weak pth_key_delete
145 # pragma weak pth_cancel
146 # define pth_in_use() (pth_cancel != NULL)
150 # define pth_in_use() 1
154 /* ------------------------- gl_tls_key_t datatype ------------------------- */
158 void *singlethread_value;
162 # define gl_tls_key_init(NAME, DESTRUCTOR) \
167 if (!pth_key_create (&(NAME).key, DESTRUCTOR)) \
171 (NAME).singlethread_value = NULL; \
174 # define gl_tls_get(NAME) \
176 ? pth_key_getdata ((NAME).key) \
177 : (NAME).singlethread_value)
178 # define gl_tls_set(NAME, POINTER) \
183 if (!pth_key_setdata ((NAME).key, (POINTER))) \
187 (NAME).singlethread_value = (POINTER); \
190 # define gl_tls_key_destroy(NAME) \
191 if (pth_in_use () && !pth_key_delete ((NAME).key)) \
196 /* ========================================================================= */
198 #if USE_SOLARIS_THREADS
200 /* Use the old Solaris threads library. */
205 # if USE_SOLARIS_THREADS_WEAK
207 /* Use weak references to the old Solaris threads library. */
209 # pragma weak thr_keycreate
210 # pragma weak thr_getspecific
211 # pragma weak thr_setspecific
213 # pragma weak thr_suspend
214 # define thread_in_use() (thr_suspend != NULL)
218 # define thread_in_use() 1
222 /* ------------------------- gl_tls_key_t datatype ------------------------- */
226 void *singlethread_value;
230 # define gl_tls_key_init(NAME, DESTRUCTOR) \
233 if (thread_in_use ()) \
235 if (thr_keycreate (&(NAME).key, DESTRUCTOR) != 0) \
239 (NAME).singlethread_value = NULL; \
242 # define gl_tls_get(NAME) \
244 ? glthread_tls_get ((NAME).key) \
245 : (NAME).singlethread_value)
246 extern void *glthread_tls_get (thread_key_t key);
247 # define gl_tls_set(NAME, POINTER) \
250 if (thread_in_use ()) \
252 if (thr_setspecific ((NAME).key, (POINTER)) != 0) \
256 (NAME).singlethread_value = (POINTER); \
259 # define gl_tls_key_destroy(NAME) \
265 /* ========================================================================= */
267 #if USE_WIN32_THREADS
269 # include <windows.h>
271 /* ------------------------- gl_tls_key_t datatype ------------------------- */
273 typedef DWORD gl_tls_key_t;
274 # define gl_tls_key_init(NAME, DESTRUCTOR) \
275 /* The destructor is unsupported. */ \
276 if (((NAME) = TlsAlloc ()) == (DWORD)-1) \
278 # define gl_tls_get(NAME) \
280 # define gl_tls_set(NAME, POINTER) \
281 if (!TlsSetValue (NAME, POINTER)) \
283 # define gl_tls_key_destroy(NAME) \
284 if (!TlsFree (NAME)) \
289 /* ========================================================================= */
291 #if !(USE_POSIX_THREADS || USE_PTH_THREADS || USE_SOLARIS_THREADS || USE_WIN32_THREADS)
293 /* Provide dummy implementation if threads are not supported. */
295 /* ------------------------- gl_tls_key_t datatype ------------------------- */
299 void *singlethread_value;
302 # define gl_tls_key_init(NAME, DESTRUCTOR) \
303 (NAME).singlethread_value = NULL
304 # define gl_tls_get(NAME) \
305 (NAME).singlethread_value
306 # define gl_tls_set(NAME, POINTER) \
307 (NAME).singlethread_value = (POINTER)
308 # define gl_tls_key_destroy(NAME) \
313 /* ========================================================================= */