1 /* Thread-local storage in multithreaded situations.
2 Copyright (C) 2005, 2007-2011 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);
28 Equivalent functions with control of error handling:
29 Initialization: err = glthread_tls_key_init (&name, destructor);
30 Setting per-thread value: err = glthread_tls_set (&name, pointer);
31 De-initialization: err = glthread_tls_key_destroy (&name);
33 A per-thread value is of type 'void *'.
35 A destructor is a function pointer of type 'void (*) (void *)', called
36 when a thread exits, and taking the last per-thread value as argument. It
37 is unspecified whether the destructor function is called when the last
38 per-thread value is NULL. On some platforms, the destructor function is
49 /* ========================================================================= */
53 /* Use the POSIX threads library. */
57 # if PTHREAD_IN_USE_DETECTION_HARD
59 /* The pthread_in_use() detection needs to be done at runtime. */
60 # define pthread_in_use() \
62 extern int glthread_in_use (void);
66 # if USE_POSIX_THREADS_WEAK
68 /* Use weak references to the POSIX threads library. */
70 # pragma weak pthread_key_create
71 # pragma weak pthread_getspecific
72 # pragma weak pthread_setspecific
73 # pragma weak pthread_key_delete
75 # pragma weak pthread_self
78 # if !PTHREAD_IN_USE_DETECTION_HARD
79 # pragma weak pthread_cancel
80 # define pthread_in_use() (pthread_cancel != NULL)
85 # if !PTHREAD_IN_USE_DETECTION_HARD
86 # define pthread_in_use() 1
91 /* ------------------------- gl_tls_key_t datatype ------------------------- */
95 void *singlethread_value;
99 # define glthread_tls_key_init(KEY, DESTRUCTOR) \
101 ? pthread_key_create (&(KEY)->key, DESTRUCTOR) \
102 : ((KEY)->singlethread_value = NULL, 0))
103 # define gl_tls_get(NAME) \
105 ? pthread_getspecific ((NAME).key) \
106 : (NAME).singlethread_value)
107 # define glthread_tls_set(KEY, POINTER) \
109 ? pthread_setspecific ((KEY)->key, (POINTER)) \
110 : ((KEY)->singlethread_value = (POINTER), 0))
111 # define glthread_tls_key_destroy(KEY) \
112 (pthread_in_use () ? pthread_key_delete ((KEY)->key) : 0)
116 /* ========================================================================= */
120 /* Use the GNU Pth threads library. */
124 # if USE_PTH_THREADS_WEAK
126 /* Use weak references to the GNU Pth threads library. */
128 # pragma weak pth_key_create
129 # pragma weak pth_key_getdata
130 # pragma weak pth_key_setdata
131 # pragma weak pth_key_delete
133 # pragma weak pth_cancel
134 # define pth_in_use() (pth_cancel != NULL)
138 # define pth_in_use() 1
142 /* ------------------------- gl_tls_key_t datatype ------------------------- */
146 void *singlethread_value;
150 # define glthread_tls_key_init(KEY, DESTRUCTOR) \
152 ? (!pth_key_create (&(KEY)->key, DESTRUCTOR) ? errno : 0) \
153 : ((KEY)->singlethread_value = NULL, 0))
154 # define gl_tls_get(NAME) \
156 ? pth_key_getdata ((NAME).key) \
157 : (NAME).singlethread_value)
158 # define glthread_tls_set(KEY, POINTER) \
160 ? (!pth_key_setdata ((KEY)->key, (POINTER)) ? errno : 0) \
161 : ((KEY)->singlethread_value = (POINTER), 0))
162 # define glthread_tls_key_destroy(KEY) \
164 ? (!pth_key_delete ((KEY)->key) ? errno : 0) \
169 /* ========================================================================= */
171 #if USE_SOLARIS_THREADS
173 /* Use the old Solaris threads library. */
177 # if USE_SOLARIS_THREADS_WEAK
179 /* Use weak references to the old Solaris threads library. */
181 # pragma weak thr_keycreate
182 # pragma weak thr_getspecific
183 # pragma weak thr_setspecific
185 # pragma weak thr_suspend
186 # define thread_in_use() (thr_suspend != NULL)
190 # define thread_in_use() 1
194 /* ------------------------- gl_tls_key_t datatype ------------------------- */
198 void *singlethread_value;
202 # define glthread_tls_key_init(KEY, DESTRUCTOR) \
204 ? thr_keycreate (&(KEY)->key, DESTRUCTOR) \
205 : ((KEY)->singlethread_value = NULL, 0))
206 # define gl_tls_get(NAME) \
208 ? glthread_tls_get_multithreaded ((NAME).key) \
209 : (NAME).singlethread_value)
210 extern void *glthread_tls_get_multithreaded (thread_key_t key);
211 # define glthread_tls_set(KEY, POINTER) \
213 ? thr_setspecific ((KEY)->key, (POINTER)) \
214 : ((KEY)->singlethread_value = (POINTER), 0))
215 # define glthread_tls_key_destroy(KEY) \
221 /* ========================================================================= */
223 #if USE_WIN32_THREADS
225 # include <windows.h>
227 /* ------------------------- gl_tls_key_t datatype ------------------------- */
229 typedef DWORD gl_tls_key_t;
230 # define glthread_tls_key_init(KEY, DESTRUCTOR) \
231 /* The destructor is unsupported. */ \
232 ((*(KEY) = TlsAlloc ()) == (DWORD)-1 ? EAGAIN : ((void) (DESTRUCTOR), 0))
233 # define gl_tls_get(NAME) \
235 # define glthread_tls_set(KEY, POINTER) \
236 (!TlsSetValue (*(KEY), POINTER) ? EINVAL : 0)
237 # define glthread_tls_key_destroy(KEY) \
238 (!TlsFree (*(KEY)) ? EINVAL : 0)
242 /* ========================================================================= */
244 #if !(USE_POSIX_THREADS || USE_PTH_THREADS || USE_SOLARIS_THREADS || USE_WIN32_THREADS)
246 /* Provide dummy implementation if threads are not supported. */
248 /* ------------------------- gl_tls_key_t datatype ------------------------- */
252 void *singlethread_value;
255 # define glthread_tls_key_init(KEY, DESTRUCTOR) \
256 ((KEY)->singlethread_value = NULL, \
257 (void) (DESTRUCTOR), \
259 # define gl_tls_get(NAME) \
260 (NAME).singlethread_value
261 # define glthread_tls_set(KEY, POINTER) \
262 ((KEY)->singlethread_value = (POINTER), 0)
263 # define glthread_tls_key_destroy(KEY) \
268 /* ========================================================================= */
270 /* Macros with built-in error handling. */
272 /* ------------------------- gl_tls_key_t datatype ------------------------- */
274 #define gl_tls_key_init(NAME, DESTRUCTOR) \
277 if (glthread_tls_key_init (&NAME, DESTRUCTOR)) \
281 #define gl_tls_set(NAME, POINTER) \
284 if (glthread_tls_set (&NAME, POINTER)) \
288 #define gl_tls_key_destroy(NAME) \
291 if (glthread_tls_key_destroy (&NAME)) \
296 /* ========================================================================= */