Change copyright notice from GPLv2+ to GPLv3+.
[gnulib.git] / lib / tls.h
1 /* Thread-local storage in multithreaded situations.
2    Copyright (C) 2005 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>.  */
16
17 /* Written by Bruno Haible <bruno@clisp.org>, 2005.  */
18
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.
22
23    Type:                      gl_tls_key_t
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
29    A per-thread value is of type 'void *'.
30
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
35    not called at all.
36 */
37
38
39 #ifndef _TLS_H
40 #define _TLS_H
41
42 /* ========================================================================= */
43
44 #if USE_POSIX_THREADS
45
46 /* Use the POSIX threads library.  */
47
48 # include <pthread.h>
49 # include <stdlib.h>
50
51 # if PTHREAD_IN_USE_DETECTION_HARD
52
53 /* The pthread_in_use() detection needs to be done at runtime.  */
54 #  define pthread_in_use() \
55      glthread_in_use ()
56 extern int glthread_in_use (void);
57
58 # endif
59
60 # if USE_POSIX_THREADS_WEAK
61
62 /* Use weak references to the POSIX threads library.  */
63
64 #  pragma weak pthread_key_create
65 #  pragma weak pthread_getspecific
66 #  pragma weak pthread_setspecific
67 #  pragma weak pthread_key_delete
68 #  ifndef pthread_self
69 #   pragma weak pthread_self
70 #  endif
71
72 #  if !PTHREAD_IN_USE_DETECTION_HARD
73 #   pragma weak pthread_cancel
74 #   define pthread_in_use() (pthread_cancel != NULL)
75 #  endif
76
77 # else
78
79 #  if !PTHREAD_IN_USE_DETECTION_HARD
80 #   define pthread_in_use() 1
81 #  endif
82
83 # endif
84
85 /* ------------------------- gl_tls_key_t datatype ------------------------- */
86
87 typedef union
88         {
89           void *singlethread_value;
90           pthread_key_t key;
91         }
92         gl_tls_key_t;
93 # define gl_tls_key_init(NAME, DESTRUCTOR) \
94     do                                                             \
95       {                                                            \
96         if (pthread_in_use ())                                     \
97           {                                                        \
98             if (pthread_key_create (&(NAME).key, DESTRUCTOR) != 0) \
99               abort ();                                            \
100           }                                                        \
101         else                                                       \
102           (NAME).singlethread_value = NULL;                        \
103       }                                                            \
104     while (0)
105 # define gl_tls_get(NAME) \
106     (pthread_in_use ()                  \
107      ? pthread_getspecific ((NAME).key) \
108      : (NAME).singlethread_value)
109 # define gl_tls_set(NAME, POINTER) \
110     do                                                            \
111       {                                                           \
112         if (pthread_in_use ())                                    \
113           {                                                       \
114             if (pthread_setspecific ((NAME).key, (POINTER)) != 0) \
115               abort ();                                           \
116           }                                                       \
117         else                                                      \
118           (NAME).singlethread_value = (POINTER);                  \
119       }                                                           \
120     while (0)
121 # define gl_tls_key_destroy(NAME) \
122     if (pthread_in_use () && pthread_key_delete ((NAME).key) != 0) \
123       abort ()
124
125 #endif
126
127 /* ========================================================================= */
128
129 #if USE_PTH_THREADS
130
131 /* Use the GNU Pth threads library.  */
132
133 # include <pth.h>
134 # include <stdlib.h>
135
136 # if USE_PTH_THREADS_WEAK
137
138 /* Use weak references to the GNU Pth threads library.  */
139
140 #  pragma weak pth_key_create
141 #  pragma weak pth_key_getdata
142 #  pragma weak pth_key_setdata
143 #  pragma weak pth_key_delete
144
145 #  pragma weak pth_cancel
146 #  define pth_in_use() (pth_cancel != NULL)
147
148 # else
149
150 #  define pth_in_use() 1
151
152 # endif
153
154 /* ------------------------- gl_tls_key_t datatype ------------------------- */
155
156 typedef union
157         {
158           void *singlethread_value;
159           pth_key_t key;
160         }
161         gl_tls_key_t;
162 # define gl_tls_key_init(NAME, DESTRUCTOR) \
163     do                                                     \
164       {                                                    \
165         if (pth_in_use ())                                 \
166           {                                                \
167             if (!pth_key_create (&(NAME).key, DESTRUCTOR)) \
168               abort ();                                    \
169           }                                                \
170         else                                               \
171           (NAME).singlethread_value = NULL;                \
172       }                                                    \
173     while (0)
174 # define gl_tls_get(NAME) \
175     (pth_in_use ()                  \
176      ? pth_key_getdata ((NAME).key) \
177      : (NAME).singlethread_value)
178 # define gl_tls_set(NAME, POINTER) \
179     do                                                    \
180       {                                                   \
181         if (pth_in_use ())                                \
182           {                                               \
183             if (!pth_key_setdata ((NAME).key, (POINTER))) \
184               abort ();                                   \
185           }                                               \
186         else                                              \
187           (NAME).singlethread_value = (POINTER);          \
188       }                                                   \
189     while (0)
190 # define gl_tls_key_destroy(NAME) \
191     if (pth_in_use () && !pth_key_delete ((NAME).key)) \
192       abort ()
193
194 #endif
195
196 /* ========================================================================= */
197
198 #if USE_SOLARIS_THREADS
199
200 /* Use the old Solaris threads library.  */
201
202 # include <thread.h>
203 # include <stdlib.h>
204
205 # if USE_SOLARIS_THREADS_WEAK
206
207 /* Use weak references to the old Solaris threads library.  */
208
209 #  pragma weak thr_keycreate
210 #  pragma weak thr_getspecific
211 #  pragma weak thr_setspecific
212
213 #  pragma weak thr_suspend
214 #  define thread_in_use() (thr_suspend != NULL)
215
216 # else
217
218 #  define thread_in_use() 1
219
220 # endif
221
222 /* ------------------------- gl_tls_key_t datatype ------------------------- */
223
224 typedef union
225         {
226           void *singlethread_value;
227           thread_key_t key;
228         }
229         gl_tls_key_t;
230 # define gl_tls_key_init(NAME, DESTRUCTOR) \
231     do                                                        \
232       {                                                       \
233         if (thread_in_use ())                                 \
234           {                                                   \
235             if (thr_keycreate (&(NAME).key, DESTRUCTOR) != 0) \
236               abort ();                                       \
237           }                                                   \
238         else                                                  \
239           (NAME).singlethread_value = NULL;                   \
240       }                                                       \
241     while (0)
242 # define gl_tls_get(NAME) \
243     (thread_in_use ()                \
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) \
248     do                                                        \
249       {                                                       \
250         if (thread_in_use ())                                 \
251           {                                                   \
252             if (thr_setspecific ((NAME).key, (POINTER)) != 0) \
253               abort ();                                       \
254           }                                                   \
255         else                                                  \
256           (NAME).singlethread_value = (POINTER);              \
257       }                                                       \
258     while (0)
259 # define gl_tls_key_destroy(NAME) \
260     /* Unsupported.  */ \
261     (void)0
262
263 #endif
264
265 /* ========================================================================= */
266
267 #if USE_WIN32_THREADS
268
269 # include <windows.h>
270
271 /* ------------------------- gl_tls_key_t datatype ------------------------- */
272
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) \
277       abort ()
278 # define gl_tls_get(NAME) \
279     TlsGetValue (NAME)
280 # define gl_tls_set(NAME, POINTER) \
281     if (!TlsSetValue (NAME, POINTER)) \
282       abort ()
283 # define gl_tls_key_destroy(NAME) \
284     if (!TlsFree (NAME)) \
285       abort ()
286
287 #endif
288
289 /* ========================================================================= */
290
291 #if !(USE_POSIX_THREADS || USE_PTH_THREADS || USE_SOLARIS_THREADS || USE_WIN32_THREADS)
292
293 /* Provide dummy implementation if threads are not supported.  */
294
295 /* ------------------------- gl_tls_key_t datatype ------------------------- */
296
297 typedef struct
298         {
299           void *singlethread_value;
300         }
301         gl_tls_key_t;
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) \
309     (void)0
310
311 #endif
312
313 /* ========================================================================= */
314
315 #endif /* _TLS_H */