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);
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
48 /* ========================================================================= */
52 /* 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. */
125 # if USE_PTH_THREADS_WEAK
127 /* Use weak references to the GNU Pth threads library. */
129 # pragma weak pth_key_create
130 # pragma weak pth_key_getdata
131 # pragma weak pth_key_setdata
132 # pragma weak pth_key_delete
134 # pragma weak pth_cancel
135 # define pth_in_use() (pth_cancel != NULL)
139 # define pth_in_use() 1
143 /* ------------------------- gl_tls_key_t datatype ------------------------- */
147 void *singlethread_value;
151 # define glthread_tls_key_init(KEY, DESTRUCTOR) \
153 ? (!pth_key_create (&(KEY)->key, DESTRUCTOR) ? errno : 0) \
154 : ((KEY)->singlethread_value = NULL, 0))
155 # define gl_tls_get(NAME) \
157 ? pth_key_getdata ((NAME).key) \
158 : (NAME).singlethread_value)
159 # define glthread_tls_set(KEY, POINTER) \
161 ? (!pth_key_setdata ((KEY)->key, (POINTER)) ? errno : 0) \
162 : ((KEY)->singlethread_value = (POINTER), 0))
163 # define glthread_tls_key_destroy(KEY) \
165 ? (!pth_key_delete ((KEY)->key) ? errno : 0) \
170 /* ========================================================================= */
172 #if USE_SOLARIS_THREADS
174 /* Use the old Solaris threads library. */
179 # if USE_SOLARIS_THREADS_WEAK
181 /* Use weak references to the old Solaris threads library. */
183 # pragma weak thr_keycreate
184 # pragma weak thr_getspecific
185 # pragma weak thr_setspecific
187 # pragma weak thr_suspend
188 # define thread_in_use() (thr_suspend != NULL)
192 # define thread_in_use() 1
196 /* ------------------------- gl_tls_key_t datatype ------------------------- */
200 void *singlethread_value;
204 # define glthread_tls_key_init(KEY, DESTRUCTOR) \
206 ? thr_keycreate (&(KEY)->key, DESTRUCTOR) \
207 : ((KEY)->singlethread_value = NULL, 0))
208 # define gl_tls_get(NAME) \
210 ? glthread_tls_get_multithreaded ((NAME).key) \
211 : (NAME).singlethread_value)
212 extern void *glthread_tls_get_multithreaded (thread_key_t key);
213 # define glthread_tls_set(KEY, POINTER) \
215 ? thr_setspecific ((KEY)->key, (POINTER)) \
216 : ((KEY)->singlethread_value = (POINTER), 0))
217 # define glthread_tls_key_destroy(KEY) \
223 /* ========================================================================= */
225 #if USE_WIN32_THREADS
227 # include <windows.h>
229 /* ------------------------- gl_tls_key_t datatype ------------------------- */
231 typedef DWORD gl_tls_key_t;
232 # define glthread_tls_key_init(KEY, DESTRUCTOR) \
233 /* The destructor is unsupported. */ \
234 ((*(KEY) = TlsAlloc ()) == (DWORD)-1 ? EAGAIN : ((void) (DESTRUCTOR), 0))
235 # define gl_tls_get(NAME) \
237 # define glthread_tls_set(KEY, POINTER) \
238 (!TlsSetValue (*(KEY), POINTER) ? EINVAL : 0)
239 # define glthread_tls_key_destroy(KEY) \
240 (!TlsFree (*(KEY)) ? EINVAL : 0)
244 /* ========================================================================= */
246 #if !(USE_POSIX_THREADS || USE_PTH_THREADS || USE_SOLARIS_THREADS || USE_WIN32_THREADS)
248 /* Provide dummy implementation if threads are not supported. */
250 /* ------------------------- gl_tls_key_t datatype ------------------------- */
254 void *singlethread_value;
257 # define glthread_tls_key_init(KEY, DESTRUCTOR) \
258 ((KEY)->singlethread_value = NULL, \
259 (void) (DESTRUCTOR), \
261 # define gl_tls_get(NAME) \
262 (NAME).singlethread_value
263 # define glthread_tls_set(KEY, POINTER) \
264 ((KEY)->singlethread_value = (POINTER), 0)
265 # define glthread_tls_key_destroy(KEY) \
270 /* ========================================================================= */
272 /* Macros with built-in error handling. */
274 /* ------------------------- gl_tls_key_t datatype ------------------------- */
276 #define gl_tls_key_init(NAME, DESTRUCTOR) \
279 if (glthread_tls_key_init (&NAME, DESTRUCTOR)) \
283 #define gl_tls_set(NAME, POINTER) \
286 if (glthread_tls_set (&NAME, POINTER)) \
290 #define gl_tls_key_destroy(NAME) \
293 if (glthread_tls_key_destroy (&NAME)) \
298 /* ========================================================================= */