Revert "roken: refactor rk_getauxval"; broke build
[metze/heimdal/wip.git] / include / heim_threads.h
1 /*
2  * Copyright (c) 2003-2016 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the Institute nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 /* $Id$ */
35
36 /*
37  * Provide wrapper macros for thread synchronization primitives so we
38  * can use native thread functions for those operating system that
39  * supports it.
40  *
41  * This is so libkrb5.so (or more importantly, libgssapi.so) can have
42  * thread support while the program that that dlopen(3)s the library
43  * don't need to be linked to libpthread.
44  */
45
46 #ifndef HEIM_THREADS_H
47 #define HEIM_THREADS_H 1
48
49 #ifdef _MSC_VER
50
51 #define HEIMDAL_THREAD_LOCAL __declspec(thread)
52
53 #else
54
55 #if defined(__clang__) || defined(__GNUC__) || defined(__SUNPRO_CC)
56 #define HEIMDAL_THREAD_LOCAL __thread
57 #else
58 #error "thread-local attribute not defined for your compiler"
59 #endif /* clang or gcc */
60
61 #endif /* _MSC_VER */
62
63 /* For testing the for-Windows implementation of thread keys on non-Windows */
64 typedef unsigned long HEIM_PRIV_thread_key;
65
66
67 /* assume headers already included */
68
69 #if defined(__NetBSD__) && __NetBSD_Version__ >= 106120000 && __NetBSD_Version__< 299001200 && defined(ENABLE_PTHREAD_SUPPORT)
70
71 /*
72  * NetBSD have a thread lib that we can use that part of libc that
73  * works regardless if application are linked to pthreads or not.
74  * NetBSD newer then 2.99.11 just use pthread.h, and the same thing
75  * will happen.
76  */
77 #include <threadlib.h>
78
79 #define HEIMDAL_MUTEX mutex_t
80 #define HEIMDAL_MUTEX_INITIALIZER MUTEX_INITIALIZER
81 #define HEIMDAL_MUTEX_init(m) mutex_init(m, NULL)
82 #define HEIMDAL_MUTEX_lock(m) mutex_lock(m)
83 #define HEIMDAL_MUTEX_unlock(m) mutex_unlock(m)
84 #define HEIMDAL_MUTEX_destroy(m) mutex_destroy(m)
85
86 #define HEIMDAL_RWLOCK rwlock_t
87 #define HEIMDAL_RWLOCK_INITIALIZER RWLOCK_INITIALIZER
88 #define HEIMDAL_RWLOCK_init(l) rwlock_init(l, NULL)
89 #define HEIMDAL_RWLOCK_rdlock(l) rwlock_rdlock(l)
90 #define HEIMDAL_RWLOCK_wrlock(l) rwlock_wrlock(l)
91 #define HEIMDAL_RWLOCK_tryrdlock(l) rwlock_tryrdlock(l)
92 #define HEIMDAL_RWLOCK_trywrlock(l) rwlock_trywrlock(l)
93 #define HEIMDAL_RWLOCK_unlock(l) rwlock_unlock(l)
94 #define HEIMDAL_RWLOCK_destroy(l) rwlock_destroy(l)
95
96 #define HEIMDAL_thread_key thread_key_t
97 #define HEIMDAL_key_create(k,d,r) do { r = thr_keycreate(k,d); } while(0)
98 #define HEIMDAL_setspecific(k,s,r) do { r = thr_setspecific(k,s); } while(0)
99 #define HEIMDAL_getspecific(k) thr_getspecific(k)
100 #define HEIMDAL_key_delete(k) thr_keydelete(k)
101
102 #define HEIMDAL_THREAD_ID thr_t
103 #define HEIMDAL_THREAD_create(t,f,a) thr_create((t), 0, (f), (a))
104
105 #elif defined(ENABLE_PTHREAD_SUPPORT) && (!defined(__NetBSD__) || __NetBSD_Version__ >= 299001200)
106
107 #include <pthread.h>
108
109 #define HEIMDAL_MUTEX pthread_mutex_t
110 #define HEIMDAL_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
111 #define HEIMDAL_MUTEX_init(m) pthread_mutex_init(m, NULL)
112 #define HEIMDAL_MUTEX_lock(m) pthread_mutex_lock(m)
113 #define HEIMDAL_MUTEX_unlock(m) pthread_mutex_unlock(m)
114 #define HEIMDAL_MUTEX_destroy(m) pthread_mutex_destroy(m)
115
116 #define HEIMDAL_RWLOCK pthread_rwlock_t
117 #define HEIMDAL_RWLOCK_INITIALIZER PTHREAD_RWLOCK_INITIALIZER
118 #define HEIMDAL_RWLOCK_init(l) pthread_rwlock_init(l, NULL)
119 #define HEIMDAL_RWLOCK_rdlock(l) pthread_rwlock_rdlock(l)
120 #define HEIMDAL_RWLOCK_wrlock(l) pthread_rwlock_wrlock(l)
121 #define HEIMDAL_RWLOCK_tryrdlock(l) pthread_rwlock_tryrdlock(l)
122 #define HEIMDAL_RWLOCK_trywrlock(l) pthread_rwlock_trywrlock(l)
123 #define HEIMDAL_RWLOCK_unlock(l) pthread_rwlock_unlock(l)
124 #define HEIMDAL_RWLOCK_destroy(l) pthread_rwlock_destroy(l)
125
126 #ifdef HEIM_BASE_MAINTAINER
127 #define HEIMDAL_thread_key unsigned long
128 #define HEIM_PRIV_thread_key HEIMDAL_thread_key
129 #define HEIMDAL_key_create(k,d,r) do { r = heim_w32_key_create(k,d); } while(0)
130 #define HEIMDAL_setspecific(k,s,r) do { r = heim_w32_setspecific(k,s); } while(0)
131 #define HEIMDAL_getspecific(k) (heim_w32_getspecific(k))
132 #define HEIMDAL_key_delete(k) (heim_w32_delete_key(k))
133 #else
134 #define HEIMDAL_thread_key pthread_key_t
135 #define HEIMDAL_key_create(k,d,r) do { r = pthread_key_create(k,d); } while(0)
136 #define HEIMDAL_setspecific(k,s,r) do { r = pthread_setspecific(k,s); } while(0)
137 #define HEIMDAL_getspecific(k) pthread_getspecific(k)
138 #define HEIMDAL_key_delete(k) pthread_key_delete(k)
139 #endif
140
141 #define HEIMDAL_THREAD_ID pthread_t
142 #define HEIMDAL_THREAD_create(t,f,a) pthread_create((t), 0, (f), (a))
143
144 #elif defined(_WIN32)
145
146 typedef struct heim_mutex {
147     HANDLE      h;
148 } heim_mutex_t;
149
150 static inline int
151 heim_mutex_init(heim_mutex_t *m)
152 {
153     m->h = CreateSemaphore(NULL, 1, 1, NULL);
154     if (m->h == INVALID_HANDLE_VALUE)
155         return EAGAIN;
156     return 0;
157 }
158
159 static inline int
160 heim_mutex_lock(heim_mutex_t *m)
161 {
162     HANDLE h, new_h;
163     int created = 0;
164
165     h = InterlockedCompareExchangePointer(&m->h, m->h, m->h);
166     if (h == INVALID_HANDLE_VALUE || h == NULL) {
167         created = 1;
168         new_h = CreateSemaphore(NULL, 0, 1, NULL);
169         if (new_h == INVALID_HANDLE_VALUE)
170             return EAGAIN;
171         if (InterlockedCompareExchangePointer(&m->h, new_h, h) != h) {
172             created = 0;
173             CloseHandle(new_h);
174         }
175     }
176     if (!created)
177         WaitForSingleObject(m->h, INFINITE);
178     return 0;
179 }
180
181 static inline int
182 heim_mutex_unlock(heim_mutex_t *m)
183 {
184     if (ReleaseSemaphore(m->h, 1, NULL) == FALSE)
185         return EPERM;
186     return 0;
187 }
188
189 static inline int
190 heim_mutex_destroy(heim_mutex_t *m)
191 {
192     HANDLE h;
193
194     h = InterlockedCompareExchangePointer(&m->h, INVALID_HANDLE_VALUE, m->h);
195     if (h != INVALID_HANDLE_VALUE)
196         CloseHandle(h);
197     return 0;
198 }
199
200 #define HEIMDAL_MUTEX heim_mutex_t
201 #define HEIMDAL_MUTEX_INITIALIZER { INVALID_HANDLE_VALUE }
202 #define HEIMDAL_MUTEX_init(m) heim_mutex_init((m))
203 #define HEIMDAL_MUTEX_lock(m) heim_mutex_lock((m))
204 #define HEIMDAL_MUTEX_unlock(m) heim_mutex_unlock((m))
205 #define HEIMDAL_MUTEX_destroy(m) heim_mutex_destroy((m))
206
207 typedef struct heim_rwlock {
208     SRWLOCK lock;
209     int     exclusive;
210 } heim_rwlock_t;
211
212 static inline int
213 heim_rwlock_init(heim_rwlock_t *l)
214 {
215     InitializeSRWLock(&l->lock);
216     l->exclusive = 0;
217     return 0;
218 }
219
220 static inline int
221 heim_rwlock_rdlock(heim_rwlock_t *l)
222 {
223     AcquireSRWLockShared(&l->lock);
224     return 0;
225 }
226
227 static inline int
228 heim_rwlock_wrlock(heim_rwlock_t *l)
229 {
230     AcquireSRWLockExclusive(&l->lock);
231     l->exclusive = 1;
232     return 0;
233 }
234
235 static inline int
236 heim_rwlock_tryrdlock(heim_rwlock_t *l)
237 {
238     if (TryAcquireSRWLockShared(&l->lock))
239         return 0;
240     return EBUSY;
241 }
242
243 static inline int
244 heim_rwlock_trywrlock(heim_rwlock_t *l)
245 {
246     if (TryAcquireSRWLockExclusive(&l->lock))
247         return 0;
248     return EBUSY;
249 }
250
251 static inline int
252 heim_rwlock_unlock(heim_rwlock_t *l)
253 {
254     if (l->exclusive) {
255         l->exclusive = 0;
256         ReleaseSRWLockExclusive(&(l)->lock);
257     } else {
258         ReleaseSRWLockShared(&(l)->lock);
259     }
260     return 0;
261 }
262
263 static inline int
264 heim_rwlock_destroy(heim_rwlock_t *l)
265 {
266     /* SRW locks cannot be destroyed so re-initialize */
267     InitializeSRWLock(&l->lock);
268     l->exclusive = 0;
269     return 0;
270 }
271
272 #define HEIMDAL_RWLOCK heim_rwlock_t
273 #define HEIMDAL_RWLOCK_INITIALIZER {SRWLOCK_INIT, 0}
274 #define HEIMDAL_RWLOCK_init(l) heim_rwlock_init((l))
275 #define HEIMDAL_RWLOCK_rdlock(l) heim_rwlock_rdlock((l))
276 #define HEIMDAL_RWLOCK_wrlock(l) heim_rwlock_wrlock((l))
277 #define HEIMDAL_RWLOCK_tryrdlock(l) heim_rwlock_tryrdlock((l))
278 #define HEIMDAL_RWLOCK_trywrlock(l) heim_rwlock_trywrlock((l))
279 #define HEIMDAL_RWLOCK_unlock(l) heim_rwlock_unlock((l))
280 #define HEIMDAL_RWLOCK_destroy(l) heim_rwlock_destroy((l))
281
282 #define HEIMDAL_thread_key unsigned long
283 #define HEIM_PRIV_thread_key HEIMDAL_thread_key
284 #define HEIMDAL_key_create(k,d,r) do { r = heim_w32_key_create(k,d); } while(0)
285 #define HEIMDAL_setspecific(k,s,r) do { r = heim_w32_setspecific(k,s); } while(0)
286 #define HEIMDAL_getspecific(k) (heim_w32_getspecific(k))
287 #define HEIMDAL_key_delete(k) (heim_w32_delete_key(k))
288
289 #define HEIMDAL_THREAD_ID DWORD
290 #define HEIMDAL_THREAD_create(t,f,a) \
291     ((CreateThread(0, 0, (f), (a), 0, (t)) == INVALID_HANDLE_VALUE) ? EINVAL : 0)
292
293 #elif defined(HEIMDAL_DEBUG_THREADS)
294
295 /* no threads support, just do consistency checks */
296 #include <stdlib.h>
297
298 #define HEIMDAL_MUTEX int
299 #define HEIMDAL_MUTEX_INITIALIZER 0
300 #define HEIMDAL_MUTEX_init(m)  do { (*(m)) = 0; } while(0)
301 #define HEIMDAL_MUTEX_lock(m)  do { if ((*(m))++ != 0) abort(); } while(0)
302 #define HEIMDAL_MUTEX_unlock(m) do { if ((*(m))-- != 1) abort(); } while(0)
303 #define HEIMDAL_MUTEX_destroy(m) do {if ((*(m)) != 0) abort(); } while(0)
304
305 #define HEIMDAL_RWLOCK rwlock_t int
306 #define HEIMDAL_RWLOCK_INITIALIZER 0
307 #define HEIMDAL_RWLOCK_init(l) do { } while(0)
308 #define HEIMDAL_RWLOCK_rdlock(l) do { } while(0)
309 #define HEIMDAL_RWLOCK_wrlock(l) do { } while(0)
310 #define HEIMDAL_RWLOCK_tryrdlock(l) do { } while(0)
311 #define HEIMDAL_RWLOCK_trywrlock(l) do { } while(0)
312 #define HEIMDAL_RWLOCK_unlock(l) do { } while(0)
313 #define HEIMDAL_RWLOCK_destroy(l) do { } while(0)
314
315 #define HEIMDAL_internal_thread_key 1
316
317 #define HEIMDAL_THREAD_ID int
318 #define HEIMDAL_THREAD_create(t,f,a) abort()
319
320 #else /* no thread support, no debug case */
321
322 #define HEIMDAL_MUTEX int
323 #define HEIMDAL_MUTEX_INITIALIZER 0
324 #define HEIMDAL_MUTEX_init(m)  do { (void)(m); } while(0)
325 #define HEIMDAL_MUTEX_lock(m)  do { (void)(m); } while(0)
326 #define HEIMDAL_MUTEX_unlock(m) do { (void)(m); } while(0)
327 #define HEIMDAL_MUTEX_destroy(m) do { (void)(m); } while(0)
328
329 #define HEIMDAL_RWLOCK rwlock_t int
330 #define HEIMDAL_RWLOCK_INITIALIZER 0
331 #define HEIMDAL_RWLOCK_init(l) do { } while(0)
332 #define HEIMDAL_RWLOCK_rdlock(l) do { } while(0)
333 #define HEIMDAL_RWLOCK_wrlock(l) do { } while(0)
334 #define HEIMDAL_RWLOCK_tryrdlock(l) do { } while(0)
335 #define HEIMDAL_RWLOCK_trywrlock(l) do { } while(0)
336 #define HEIMDAL_RWLOCK_unlock(l) do { } while(0)
337 #define HEIMDAL_RWLOCK_destroy(l) do { } while(0)
338
339 #define HEIMDAL_THREAD_ID int
340 #define HEIMDAL_THREAD_create(t,f,a) abort()
341
342 #define HEIMDAL_internal_thread_key 1
343
344 #endif /* no thread support */
345
346 #ifdef HEIMDAL_internal_thread_key
347
348 typedef struct heim_thread_key {
349     void *value;
350     void (*destructor)(void *);
351 } heim_thread_key;
352
353 #define HEIMDAL_thread_key heim_thread_key
354 #define HEIMDAL_key_create(k,d,r) \
355         do { (k)->value = NULL; (k)->destructor = (d); r = 0; } while(0)
356 #define HEIMDAL_setspecific(k,s,r) do { (k).value = s ; r = 0; } while(0)
357 #define HEIMDAL_getspecific(k) ((k).value)
358 #define HEIMDAL_key_delete(k) do { (*(k).destructor)((k).value); } while(0)
359
360 #undef HEIMDAL_internal_thread_key
361 #endif /* HEIMDAL_internal_thread_key */
362
363 int heim_w32_key_create(HEIM_PRIV_thread_key *, void (*)(void *));
364 int heim_w32_delete_key(HEIM_PRIV_thread_key);
365 int heim_w32_setspecific(HEIM_PRIV_thread_key, void *);
366 void *heim_w32_getspecific(HEIM_PRIV_thread_key);
367
368 #endif /* HEIM_THREADS_H */