ff36155c2ea5741659130b8bc10b4479ff69cb3d
[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 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     if (pthread_in_use ())                                     \
94       {                                                        \
95         if (pthread_key_create (&(NAME).key, DESTRUCTOR) != 0) \
96           abort ();                                            \
97       }                                                        \
98     else                                                       \
99       (NAME).singlethread_value = NULL
100 # define gl_tls_get(NAME) \
101     (pthread_in_use ()                  \
102      ? pthread_getspecific ((NAME).key) \
103      : (NAME).singlethread_value)
104 # define gl_tls_set(NAME, POINTER) \
105     if (pthread_in_use ())                                    \
106       {                                                       \
107         if (pthread_setspecific ((NAME).key, (POINTER)) != 0) \
108           abort ();                                           \
109       }                                                       \
110     else                                                      \
111       (NAME).singlethread_value = (POINTER)
112 # define gl_tls_key_destroy(NAME) \
113     if (pthread_in_use () && pthread_key_delete ((NAME).key) != 0) \
114       abort ()
115
116 #endif
117
118 /* ========================================================================= */
119
120 #if USE_PTH_THREADS
121
122 /* Use the GNU Pth threads library.  */
123
124 # include <pth.h>
125 # include <stdlib.h>
126
127 # if USE_PTH_THREADS_WEAK
128
129 /* Use weak references to the GNU Pth threads library.  */
130
131 #  pragma weak pth_key_create
132 #  pragma weak pth_key_getdata
133 #  pragma weak pth_key_setdata
134 #  pragma weak pth_key_delete
135
136 #  pragma weak pth_cancel
137 #  define pth_in_use() (pth_cancel != NULL)
138
139 # else
140
141 #  define pth_in_use() 1
142
143 # endif
144
145 /* ------------------------- gl_tls_key_t datatype ------------------------- */
146
147 typedef union
148         {
149           void *singlethread_value;
150           pth_key_t key;
151         }
152         gl_tls_key_t;
153 # define gl_tls_key_init(NAME, DESTRUCTOR) \
154     if (pth_in_use ())                                 \
155       {                                                \
156         if (!pth_key_create (&(NAME).key, DESTRUCTOR)) \
157           abort ();                                    \
158       }                                                \
159     else                                               \
160       (NAME).singlethread_value = NULL
161 # define gl_tls_get(NAME) \
162     (pth_in_use ()                  \
163      ? pth_key_getdata ((NAME).key) \
164      : (NAME).singlethread_value)
165 # define gl_tls_set(NAME, POINTER) \
166     if (pth_in_use ())                                \
167       {                                               \
168         if (!pth_key_setdata ((NAME).key, (POINTER))) \
169           abort ();                                   \
170       }                                               \
171     else                                              \
172       (NAME).singlethread_value = (POINTER)
173 # define gl_tls_key_destroy(NAME) \
174     if (pth_in_use () && !pth_key_delete ((NAME).key)) \
175       abort ()
176
177 #endif
178
179 /* ========================================================================= */
180
181 #if USE_SOLARIS_THREADS
182
183 /* Use the old Solaris threads library.  */
184
185 # include <thread.h>
186 # include <stdlib.h>
187
188 # if USE_SOLARIS_THREADS_WEAK
189
190 /* Use weak references to the old Solaris threads library.  */
191
192 #  pragma weak thr_keycreate
193 #  pragma weak thr_getspecific
194 #  pragma weak thr_setspecific
195
196 #  pragma weak thr_suspend
197 #  define thread_in_use() (thr_suspend != NULL)
198
199 # else
200
201 #  define thread_in_use() 1
202
203 # endif
204
205 /* ------------------------- gl_tls_key_t datatype ------------------------- */
206
207 typedef union
208         {
209           void *singlethread_value;
210           thread_key_t key;
211         }
212         gl_tls_key_t;
213 # define gl_tls_key_init(NAME, DESTRUCTOR) \
214     if (thread_in_use ())                                 \
215       {                                                   \
216         if (thr_keycreate (&(NAME).key, DESTRUCTOR) != 0) \
217           abort ();                                       \
218       }                                                   \
219     else                                                  \
220       (NAME).singlethread_value = NULL
221 # define gl_tls_get(NAME) \
222     (thread_in_use ()                \
223      ? glthread_tls_get ((NAME).key) \
224      : (NAME).singlethread_value)
225 extern void *glthread_tls_get (thread_key_t key);
226 # define gl_tls_set(NAME, POINTER) \
227     if (thread_in_use ())                                 \
228       {                                                   \
229         if (thr_setspecific ((NAME).key, (POINTER)) != 0) \
230           abort ();                                       \
231       }                                                   \
232     else                                                  \
233       (NAME).singlethread_value = (POINTER)
234 # define gl_tls_key_destroy(NAME) \
235     /* Unsupported.  */ \
236     (void)0
237
238 #endif
239
240 /* ========================================================================= */
241
242 #if USE_WIN32_THREADS
243
244 # include <windows.h>
245
246 /* ------------------------- gl_tls_key_t datatype ------------------------- */
247
248 typedef DWORD gl_tls_key_t;
249 # define gl_tls_key_init(NAME, DESTRUCTOR) \
250     /* The destructor is unsupported.  */    \
251     if (((NAME) = TlsAlloc ()) == (DWORD)-1) \
252       abort ()
253 # define gl_tls_get(NAME) \
254     TlsGetValue (NAME)
255 # define gl_tls_set(NAME, POINTER) \
256     if (!TlsSetValue (NAME, POINTER)) \
257       abort ()
258 # define gl_tls_key_destroy(NAME) \
259     if (!TlsFree (NAME)) \
260       abort ()
261
262 #endif
263
264 /* ========================================================================= */
265
266 #if !(USE_POSIX_THREADS || USE_PTH_THREADS || USE_SOLARIS_THREADS || USE_WIN32_THREADS)
267
268 /* Provide dummy implementation if threads are not supported.  */
269
270 /* ------------------------- gl_tls_key_t datatype ------------------------- */
271
272 typedef struct
273         {
274           void *singlethread_value;
275         }
276         gl_tls_key_t;
277 # define gl_tls_key_init(NAME, DESTRUCTOR) \
278     (NAME).singlethread_value = NULL
279 # define gl_tls_get(NAME) \
280     (NAME).singlethread_value
281 # define gl_tls_set(NAME, POINTER) \
282     (NAME).singlethread_value = (POINTER)
283 # define gl_tls_key_destroy(NAME) \
284     (void)0
285
286 #endif