Move the lock and tls source files into a subdirectory.
[gnulib.git] / lib / glthread / tls.h
1 /* Thread-local storage in multithreaded situations.
2    Copyright (C) 2005, 2007-2008 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    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);
32
33    A per-thread value is of type 'void *'.
34
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
39    not called at all.
40 */
41
42
43 #ifndef _TLS_H
44 #define _TLS_H
45
46 #include <errno.h>
47
48 /* ========================================================================= */
49
50 #if USE_POSIX_THREADS
51
52 /* Use the POSIX threads library.  */
53
54 # include <pthread.h>
55 # include <stdlib.h>
56
57 # if PTHREAD_IN_USE_DETECTION_HARD
58
59 /* The pthread_in_use() detection needs to be done at runtime.  */
60 #  define pthread_in_use() \
61      glthread_in_use ()
62 extern int glthread_in_use (void);
63
64 # endif
65
66 # if USE_POSIX_THREADS_WEAK
67
68 /* Use weak references to the POSIX threads library.  */
69
70 #  pragma weak pthread_key_create
71 #  pragma weak pthread_getspecific
72 #  pragma weak pthread_setspecific
73 #  pragma weak pthread_key_delete
74 #  ifndef pthread_self
75 #   pragma weak pthread_self
76 #  endif
77
78 #  if !PTHREAD_IN_USE_DETECTION_HARD
79 #   pragma weak pthread_cancel
80 #   define pthread_in_use() (pthread_cancel != NULL)
81 #  endif
82
83 # else
84
85 #  if !PTHREAD_IN_USE_DETECTION_HARD
86 #   define pthread_in_use() 1
87 #  endif
88
89 # endif
90
91 /* ------------------------- gl_tls_key_t datatype ------------------------- */
92
93 typedef union
94         {
95           void *singlethread_value;
96           pthread_key_t key;
97         }
98         gl_tls_key_t;
99 # define glthread_tls_key_init(KEY, DESTRUCTOR) \
100     (pthread_in_use ()                              \
101      ? pthread_key_create (&(KEY)->key, DESTRUCTOR) \
102      : ((KEY)->singlethread_value = NULL, 0))
103 # define gl_tls_get(NAME) \
104     (pthread_in_use ()                  \
105      ? pthread_getspecific ((NAME).key) \
106      : (NAME).singlethread_value)
107 # define glthread_tls_set(KEY, POINTER) \
108     (pthread_in_use ()                             \
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)
113
114 #endif
115
116 /* ========================================================================= */
117
118 #if USE_PTH_THREADS
119
120 /* Use the GNU Pth threads library.  */
121
122 # include <pth.h>
123 # include <stdlib.h>
124
125 # if USE_PTH_THREADS_WEAK
126
127 /* Use weak references to the GNU Pth threads library.  */
128
129 #  pragma weak pth_key_create
130 #  pragma weak pth_key_getdata
131 #  pragma weak pth_key_setdata
132 #  pragma weak pth_key_delete
133
134 #  pragma weak pth_cancel
135 #  define pth_in_use() (pth_cancel != NULL)
136
137 # else
138
139 #  define pth_in_use() 1
140
141 # endif
142
143 /* ------------------------- gl_tls_key_t datatype ------------------------- */
144
145 typedef union
146         {
147           void *singlethread_value;
148           pth_key_t key;
149         }
150         gl_tls_key_t;
151 # define glthread_tls_key_init(KEY, DESTRUCTOR) \
152     (pth_in_use ()                                             \
153      ? (!pth_key_create (&(KEY)->key, DESTRUCTOR) ? errno : 0) \
154      : ((KEY)->singlethread_value = NULL, 0))
155 # define gl_tls_get(NAME) \
156     (pth_in_use ()                  \
157      ? pth_key_getdata ((NAME).key) \
158      : (NAME).singlethread_value)
159 # define glthread_tls_set(KEY, POINTER) \
160     (pth_in_use ()                                            \
161      ? (!pth_key_setdata ((KEY)->key, (POINTER)) ? errno : 0) \
162      : ((KEY)->singlethread_value = (POINTER), 0))
163 # define glthread_tls_key_destroy(KEY) \
164     (pth_in_use ()                                \
165      ? (!pth_key_delete ((KEY)->key) ? errno : 0) \
166      : 0)
167
168 #endif
169
170 /* ========================================================================= */
171
172 #if USE_SOLARIS_THREADS
173
174 /* Use the old Solaris threads library.  */
175
176 # include <thread.h>
177 # include <stdlib.h>
178
179 # if USE_SOLARIS_THREADS_WEAK
180
181 /* Use weak references to the old Solaris threads library.  */
182
183 #  pragma weak thr_keycreate
184 #  pragma weak thr_getspecific
185 #  pragma weak thr_setspecific
186
187 #  pragma weak thr_suspend
188 #  define thread_in_use() (thr_suspend != NULL)
189
190 # else
191
192 #  define thread_in_use() 1
193
194 # endif
195
196 /* ------------------------- gl_tls_key_t datatype ------------------------- */
197
198 typedef union
199         {
200           void *singlethread_value;
201           thread_key_t key;
202         }
203         gl_tls_key_t;
204 # define glthread_tls_key_init(KEY, DESTRUCTOR) \
205     (thread_in_use ()                          \
206      ? thr_keycreate (&(KEY)->key, DESTRUCTOR) \
207      : ((KEY)->singlethread_value = NULL, 0))
208 # define gl_tls_get(NAME) \
209     (thread_in_use ()                \
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) \
214     (thread_in_use ()                              \
215      ? thr_setspecific ((KEY)->key, (POINTER))     \
216      : ((KEY)->singlethread_value = (POINTER), 0))
217 # define glthread_tls_key_destroy(KEY) \
218     /* Unsupported.  */ \
219     0
220
221 #endif
222
223 /* ========================================================================= */
224
225 #if USE_WIN32_THREADS
226
227 # include <windows.h>
228
229 /* ------------------------- gl_tls_key_t datatype ------------------------- */
230
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) \
236     TlsGetValue (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)
241
242 #endif
243
244 /* ========================================================================= */
245
246 #if !(USE_POSIX_THREADS || USE_PTH_THREADS || USE_SOLARIS_THREADS || USE_WIN32_THREADS)
247
248 /* Provide dummy implementation if threads are not supported.  */
249
250 /* ------------------------- gl_tls_key_t datatype ------------------------- */
251
252 typedef struct
253         {
254           void *singlethread_value;
255         }
256         gl_tls_key_t;
257 # define glthread_tls_key_init(KEY, DESTRUCTOR) \
258     ((KEY)->singlethread_value = NULL, \
259      (void) (DESTRUCTOR),              \
260      0)
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) \
266     0
267
268 #endif
269
270 /* ========================================================================= */
271
272 /* Macros with built-in error handling.  */
273
274 /* ------------------------- gl_tls_key_t datatype ------------------------- */
275
276 #define gl_tls_key_init(NAME, DESTRUCTOR) \
277    do                                                 \
278      {                                                \
279        if (glthread_tls_key_init (&NAME, DESTRUCTOR)) \
280          abort ();                                    \
281      }                                                \
282    while (0)
283 #define gl_tls_set(NAME, POINTER) \
284    do                                         \
285      {                                        \
286        if (glthread_tls_set (&NAME, POINTER)) \
287          abort ();                            \
288      }                                        \
289    while (0)
290 #define gl_tls_key_destroy(NAME) \
291    do                                        \
292      {                                       \
293        if (glthread_tls_key_destroy (&NAME)) \
294          abort ();                           \
295      }                                       \
296    while (0)
297
298 /* ========================================================================= */
299
300 #endif /* _TLS_H */