3ca95041ba70721fe30ca864c1bc64aada5301ad
[pspp] / 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 it
5    under the terms of the GNU Library General Public License as published
6    by the Free Software Foundation; either version 2, or (at your option)
7    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 GNU
12    Library General Public License for more details.
13
14    You should have received a copy of the GNU Library General Public
15    License along with this program; if not, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17    USA.  */
18
19 /* Written by Bruno Haible <bruno@clisp.org>, 2005.  */
20
21 /* This file contains thread-local storage primitives for use with a given
22    thread library.  It does not contain primitives for creating threads or
23    for other multithreading primitives.
24
25    Type:                      gl_tls_key_t
26    Initialization:            gl_tls_key_init (name, destructor);
27    Getting per-thread value:  gl_tls_get (name)
28    Setting per-thread value:  gl_tls_set (name, pointer);
29    De-initialization:         gl_tls_key_destroy (name);
30
31    A per-thread value is of type 'void *'.
32
33    A destructor is a function pointer of type 'void (*) (void *)', called
34    when a thread exits, and taking the last per-thread value as argument.  It
35    is unspecified whether the destructor function is called when the last
36    per-thread value is NULL.  On some platforms, the destructor function is
37    not called at all.
38 */
39
40
41 /* ========================================================================= */
42
43 #if USE_POSIX_THREADS
44
45 /* Use the POSIX threads library.  */
46
47 # include <pthread.h>
48 # include <stdlib.h>
49
50 # if PTHREAD_IN_USE_DETECTION_HARD
51
52 /* The pthread_in_use() detection needs to be done at runtime.  */
53 #  define pthread_in_use() \
54      glthread_in_use ()
55 extern int glthread_in_use (void);
56
57 # endif
58
59 # if USE_POSIX_THREADS_WEAK
60
61 /* Use weak references to the POSIX threads library.  */
62
63 #  pragma weak pthread_key_create
64 #  pragma weak pthread_getspecific
65 #  pragma weak pthread_setspecific
66 #  pragma weak pthread_key_delete
67 #  ifndef pthread_self
68 #   pragma weak pthread_self
69 #  endif
70
71 #  if !PTHREAD_IN_USE_DETECTION_HARD
72 #   pragma weak pthread_cancel
73 #   define pthread_in_use() (pthread_cancel != NULL)
74 #  endif
75
76 # else
77
78 #  if !PTHREAD_IN_USE_DETECTION_HARD
79 #   define pthread_in_use() 1
80 #  endif
81
82 # endif
83
84 /* ------------------------- gl_tls_key_t datatype ------------------------- */
85
86 typedef union
87         {
88           void *singlethread_value;
89           pthread_key_t key;
90         }
91         gl_tls_key_t;
92 # define gl_tls_key_init(NAME, DESTRUCTOR) \
93     do                                                             \
94       {                                                            \
95         if (pthread_in_use ())                                     \
96           {                                                        \
97             if (pthread_key_create (&(NAME).key, DESTRUCTOR) != 0) \
98               abort ();                                            \
99           }                                                        \
100         else                                                       \
101           (NAME).singlethread_value = NULL;                        \
102       }                                                            \
103     while (0)
104 # define gl_tls_get(NAME) \
105     (pthread_in_use ()                  \
106      ? pthread_getspecific ((NAME).key) \
107      : (NAME).singlethread_value)
108 # define gl_tls_set(NAME, POINTER) \
109     do                                                            \
110       {                                                           \
111         if (pthread_in_use ())                                    \
112           {                                                       \
113             if (pthread_setspecific ((NAME).key, (POINTER)) != 0) \
114               abort ();                                           \
115           }                                                       \
116         else                                                      \
117           (NAME).singlethread_value = (POINTER);                  \
118       }                                                           \
119     while (0)
120 # define gl_tls_key_destroy(NAME) \
121     if (pthread_in_use () && pthread_key_delete ((NAME).key) != 0) \
122       abort ()
123
124 #endif
125
126 /* ========================================================================= */
127
128 #if USE_PTH_THREADS
129
130 /* Use the GNU Pth threads library.  */
131
132 # include <pth.h>
133 # include <stdlib.h>
134
135 # if USE_PTH_THREADS_WEAK
136
137 /* Use weak references to the GNU Pth threads library.  */
138
139 #  pragma weak pth_key_create
140 #  pragma weak pth_key_getdata
141 #  pragma weak pth_key_setdata
142 #  pragma weak pth_key_delete
143
144 #  pragma weak pth_cancel
145 #  define pth_in_use() (pth_cancel != NULL)
146
147 # else
148
149 #  define pth_in_use() 1
150
151 # endif
152
153 /* ------------------------- gl_tls_key_t datatype ------------------------- */
154
155 typedef union
156         {
157           void *singlethread_value;
158           pth_key_t key;
159         }
160         gl_tls_key_t;
161 # define gl_tls_key_init(NAME, DESTRUCTOR) \
162     do                                                     \
163       {                                                    \
164         if (pth_in_use ())                                 \
165           {                                                \
166             if (!pth_key_create (&(NAME).key, DESTRUCTOR)) \
167               abort ();                                    \
168           }                                                \
169         else                                               \
170           (NAME).singlethread_value = NULL;                \
171       }                                                    \
172     while (0)
173 # define gl_tls_get(NAME) \
174     (pth_in_use ()                  \
175      ? pth_key_getdata ((NAME).key) \
176      : (NAME).singlethread_value)
177 # define gl_tls_set(NAME, POINTER) \
178     do                                                    \
179       {                                                   \
180         if (pth_in_use ())                                \
181           {                                               \
182             if (!pth_key_setdata ((NAME).key, (POINTER))) \
183               abort ();                                   \
184           }                                               \
185         else                                              \
186           (NAME).singlethread_value = (POINTER);          \
187       }                                                   \
188     while (0)
189 # define gl_tls_key_destroy(NAME) \
190     if (pth_in_use () && !pth_key_delete ((NAME).key)) \
191       abort ()
192
193 #endif
194
195 /* ========================================================================= */
196
197 #if USE_SOLARIS_THREADS
198
199 /* Use the old Solaris threads library.  */
200
201 # include <thread.h>
202 # include <stdlib.h>
203
204 # if USE_SOLARIS_THREADS_WEAK
205
206 /* Use weak references to the old Solaris threads library.  */
207
208 #  pragma weak thr_keycreate
209 #  pragma weak thr_getspecific
210 #  pragma weak thr_setspecific
211
212 #  pragma weak thr_suspend
213 #  define thread_in_use() (thr_suspend != NULL)
214
215 # else
216
217 #  define thread_in_use() 1
218
219 # endif
220
221 /* ------------------------- gl_tls_key_t datatype ------------------------- */
222
223 typedef union
224         {
225           void *singlethread_value;
226           thread_key_t key;
227         }
228         gl_tls_key_t;
229 # define gl_tls_key_init(NAME, DESTRUCTOR) \
230     do                                                        \
231       {                                                       \
232         if (thread_in_use ())                                 \
233           {                                                   \
234             if (thr_keycreate (&(NAME).key, DESTRUCTOR) != 0) \
235               abort ();                                       \
236           }                                                   \
237         else                                                  \
238           (NAME).singlethread_value = NULL;                   \
239       }                                                       \
240     while (0)
241 # define gl_tls_get(NAME) \
242     (thread_in_use ()                \
243      ? glthread_tls_get ((NAME).key) \
244      : (NAME).singlethread_value)
245 extern void *glthread_tls_get (thread_key_t key);
246 # define gl_tls_set(NAME, POINTER) \
247     do                                                        \
248       {                                                       \
249         if (thread_in_use ())                                 \
250           {                                                   \
251             if (thr_setspecific ((NAME).key, (POINTER)) != 0) \
252               abort ();                                       \
253           }                                                   \
254         else                                                  \
255           (NAME).singlethread_value = (POINTER);              \
256       }                                                       \
257     while (0)
258 # define gl_tls_key_destroy(NAME) \
259     /* Unsupported.  */ \
260     (void)0
261
262 #endif
263
264 /* ========================================================================= */
265
266 #if USE_WIN32_THREADS
267
268 # include <windows.h>
269
270 /* ------------------------- gl_tls_key_t datatype ------------------------- */
271
272 typedef DWORD gl_tls_key_t;
273 # define gl_tls_key_init(NAME, DESTRUCTOR) \
274     /* The destructor is unsupported.  */    \
275     if (((NAME) = TlsAlloc ()) == (DWORD)-1) \
276       abort ()
277 # define gl_tls_get(NAME) \
278     TlsGetValue (NAME)
279 # define gl_tls_set(NAME, POINTER) \
280     if (!TlsSetValue (NAME, POINTER)) \
281       abort ()
282 # define gl_tls_key_destroy(NAME) \
283     if (!TlsFree (NAME)) \
284       abort ()
285
286 #endif
287
288 /* ========================================================================= */
289
290 #if !(USE_POSIX_THREADS || USE_PTH_THREADS || USE_SOLARIS_THREADS || USE_WIN32_THREADS)
291
292 /* Provide dummy implementation if threads are not supported.  */
293
294 /* ------------------------- gl_tls_key_t datatype ------------------------- */
295
296 typedef struct
297         {
298           void *singlethread_value;
299         }
300         gl_tls_key_t;
301 # define gl_tls_key_init(NAME, DESTRUCTOR) \
302     (NAME).singlethread_value = NULL
303 # define gl_tls_get(NAME) \
304     (NAME).singlethread_value
305 # define gl_tls_set(NAME, POINTER) \
306     (NAME).singlethread_value = (POINTER)
307 # define gl_tls_key_destroy(NAME) \
308     (void)0
309
310 #endif