hdb: Make HDB-as-keytab get function match API
[heimdal.git] / include / heim_threads.h
blob3ed06d9263e082a02ed4dca620b256b0fcd82581
1 /*
2 * Copyright (c) 2003-2016 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
34 /* $Id$ */
37 * Provide wrapper macros for thread synchronization primitives so we
38 * can use native thread functions for those operating system that
39 * supports it.
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.
46 #ifndef HEIM_THREADS_H
47 #define HEIM_THREADS_H 1
49 #include <errno.h>
51 #ifdef _MSC_VER
53 #define HEIMDAL_THREAD_LOCAL __declspec(thread)
55 #else
57 #if defined(__clang__) || defined(__GNUC__) || defined(__SUNPRO_C)
58 #define HEIMDAL_THREAD_LOCAL __thread
59 #else
60 #error "thread-local attribute not defined for your compiler"
61 #endif /* clang or gcc */
63 #endif /* _MSC_VER */
65 /* For testing the for-Windows implementation of thread keys on non-Windows */
66 typedef unsigned long HEIM_PRIV_thread_key;
69 /* assume headers already included */
71 #if defined(__NetBSD__) && __NetBSD_Version__ >= 106120000 && __NetBSD_Version__< 299001200 && defined(ENABLE_PTHREAD_SUPPORT)
74 * NetBSD have a thread lib that we can use that part of libc that
75 * works regardless if application are linked to pthreads or not.
76 * NetBSD newer then 2.99.11 just use pthread.h, and the same thing
77 * will happen.
79 #include <threadlib.h>
81 #define HEIMDAL_MUTEX mutex_t
82 #define HEIMDAL_MUTEX_INITIALIZER MUTEX_INITIALIZER
83 #define HEIMDAL_MUTEX_init(m) mutex_init(m, NULL)
84 #define HEIMDAL_MUTEX_lock(m) mutex_lock(m)
85 #define HEIMDAL_MUTEX_unlock(m) mutex_unlock(m)
86 #define HEIMDAL_MUTEX_destroy(m) mutex_destroy(m)
88 #define HEIMDAL_RWLOCK rwlock_t
89 #define HEIMDAL_RWLOCK_INITIALIZER RWLOCK_INITIALIZER
90 #define HEIMDAL_RWLOCK_init(l) rwlock_init(l, NULL)
91 #define HEIMDAL_RWLOCK_rdlock(l) rwlock_rdlock(l)
92 #define HEIMDAL_RWLOCK_wrlock(l) rwlock_wrlock(l)
93 #define HEIMDAL_RWLOCK_tryrdlock(l) rwlock_tryrdlock(l)
94 #define HEIMDAL_RWLOCK_trywrlock(l) rwlock_trywrlock(l)
95 #define HEIMDAL_RWLOCK_unlock(l) rwlock_unlock(l)
96 #define HEIMDAL_RWLOCK_destroy(l) rwlock_destroy(l)
98 #define HEIMDAL_thread_key thread_key_t
99 #define HEIMDAL_key_create(k,d,r) do { r = thr_keycreate(k,d); } while(0)
100 #define HEIMDAL_setspecific(k,s,r) do { r = thr_setspecific(k,s); } while(0)
101 #define HEIMDAL_getspecific(k) thr_getspecific(k)
102 #define HEIMDAL_key_delete(k) thr_keydelete(k)
104 #define HEIMDAL_THREAD_ID thr_t
105 #define HEIMDAL_THREAD_create(t,f,a) thr_create((t), 0, (f), (a))
107 #elif defined(ENABLE_PTHREAD_SUPPORT) && (!defined(__NetBSD__) || __NetBSD_Version__ >= 299001200)
109 #include <pthread.h>
111 #define HEIMDAL_MUTEX pthread_mutex_t
112 #define HEIMDAL_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
113 #define HEIMDAL_MUTEX_init(m) pthread_mutex_init(m, NULL)
114 #define HEIMDAL_MUTEX_lock(m) pthread_mutex_lock(m)
115 #define HEIMDAL_MUTEX_unlock(m) pthread_mutex_unlock(m)
116 #define HEIMDAL_MUTEX_destroy(m) pthread_mutex_destroy(m)
118 #define HEIMDAL_RWLOCK pthread_rwlock_t
119 #define HEIMDAL_RWLOCK_INITIALIZER PTHREAD_RWLOCK_INITIALIZER
120 #define HEIMDAL_RWLOCK_init(l) pthread_rwlock_init(l, NULL)
121 #define HEIMDAL_RWLOCK_rdlock(l) pthread_rwlock_rdlock(l)
122 #define HEIMDAL_RWLOCK_wrlock(l) pthread_rwlock_wrlock(l)
123 #define HEIMDAL_RWLOCK_tryrdlock(l) pthread_rwlock_tryrdlock(l)
124 #define HEIMDAL_RWLOCK_trywrlock(l) pthread_rwlock_trywrlock(l)
125 #define HEIMDAL_RWLOCK_unlock(l) pthread_rwlock_unlock(l)
126 #define HEIMDAL_RWLOCK_destroy(l) pthread_rwlock_destroy(l)
128 #ifdef HEIM_BASE_MAINTAINER
129 #define HEIMDAL_thread_key unsigned long
130 #define HEIM_PRIV_thread_key HEIMDAL_thread_key
131 #define HEIMDAL_key_create(k,d,r) do { r = heim_w32_key_create(k,d); } while(0)
132 #define HEIMDAL_setspecific(k,s,r) do { r = heim_w32_setspecific(k,s); } while(0)
133 #define HEIMDAL_getspecific(k) (heim_w32_getspecific(k))
134 #define HEIMDAL_key_delete(k) (heim_w32_delete_key(k))
135 #else
136 #define HEIMDAL_thread_key pthread_key_t
137 #define HEIMDAL_key_create(k,d,r) do { r = pthread_key_create(k,d); } while(0)
138 #define HEIMDAL_setspecific(k,s,r) do { r = pthread_setspecific(k,s); } while(0)
139 #define HEIMDAL_getspecific(k) pthread_getspecific(k)
140 #define HEIMDAL_key_delete(k) pthread_key_delete(k)
141 #endif
143 #define HEIMDAL_THREAD_ID pthread_t
144 #define HEIMDAL_THREAD_create(t,f,a) pthread_create((t), 0, (f), (a))
146 #elif defined(_WIN32)
148 typedef struct heim_mutex {
149 HANDLE h;
150 } heim_mutex_t;
152 static inline int
153 heim_mutex_init(heim_mutex_t *m)
155 m->h = CreateSemaphore(NULL, 1, 1, NULL);
156 if (m->h == INVALID_HANDLE_VALUE)
157 return EAGAIN;
158 return 0;
161 static inline int
162 heim_mutex_lock(heim_mutex_t *m)
164 HANDLE h, new_h;
165 int created = 0;
167 h = InterlockedCompareExchangePointer(&m->h, m->h, m->h);
168 if (h == INVALID_HANDLE_VALUE || h == NULL) {
169 created = 1;
170 new_h = CreateSemaphore(NULL, 0, 1, NULL);
171 if (new_h == INVALID_HANDLE_VALUE)
172 return EAGAIN;
173 if (InterlockedCompareExchangePointer(&m->h, new_h, h) != h) {
174 created = 0;
175 CloseHandle(new_h);
178 if (!created)
179 WaitForSingleObject(m->h, INFINITE);
180 return 0;
183 static inline int
184 heim_mutex_unlock(heim_mutex_t *m)
186 if (ReleaseSemaphore(m->h, 1, NULL) == FALSE)
187 return EPERM;
188 return 0;
191 static inline int
192 heim_mutex_destroy(heim_mutex_t *m)
194 HANDLE h;
196 h = InterlockedCompareExchangePointer(&m->h, INVALID_HANDLE_VALUE, m->h);
197 if (h != INVALID_HANDLE_VALUE)
198 CloseHandle(h);
199 return 0;
202 #define HEIMDAL_MUTEX heim_mutex_t
203 #define HEIMDAL_MUTEX_INITIALIZER { INVALID_HANDLE_VALUE }
204 #define HEIMDAL_MUTEX_init(m) heim_mutex_init((m))
205 #define HEIMDAL_MUTEX_lock(m) heim_mutex_lock((m))
206 #define HEIMDAL_MUTEX_unlock(m) heim_mutex_unlock((m))
207 #define HEIMDAL_MUTEX_destroy(m) heim_mutex_destroy((m))
209 typedef struct heim_rwlock {
210 SRWLOCK lock;
211 int exclusive;
212 } heim_rwlock_t;
214 static inline int
215 heim_rwlock_init(heim_rwlock_t *l)
217 InitializeSRWLock(&l->lock);
218 l->exclusive = 0;
219 return 0;
222 static inline int
223 heim_rwlock_rdlock(heim_rwlock_t *l)
225 AcquireSRWLockShared(&l->lock);
226 return 0;
229 static inline int
230 heim_rwlock_wrlock(heim_rwlock_t *l)
232 AcquireSRWLockExclusive(&l->lock);
233 l->exclusive = 1;
234 return 0;
237 static inline int
238 heim_rwlock_tryrdlock(heim_rwlock_t *l)
240 if (TryAcquireSRWLockShared(&l->lock))
241 return 0;
242 return EBUSY;
245 static inline int
246 heim_rwlock_trywrlock(heim_rwlock_t *l)
248 if (TryAcquireSRWLockExclusive(&l->lock))
249 return 0;
250 return EBUSY;
253 static inline int
254 heim_rwlock_unlock(heim_rwlock_t *l)
256 if (l->exclusive) {
257 l->exclusive = 0;
258 ReleaseSRWLockExclusive(&(l)->lock);
259 } else {
260 ReleaseSRWLockShared(&(l)->lock);
262 return 0;
265 static inline int
266 heim_rwlock_destroy(heim_rwlock_t *l)
268 /* SRW locks cannot be destroyed so re-initialize */
269 InitializeSRWLock(&l->lock);
270 l->exclusive = 0;
271 return 0;
274 #define HEIMDAL_RWLOCK heim_rwlock_t
275 #define HEIMDAL_RWLOCK_INITIALIZER {SRWLOCK_INIT, 0}
276 #define HEIMDAL_RWLOCK_init(l) heim_rwlock_init((l))
277 #define HEIMDAL_RWLOCK_rdlock(l) heim_rwlock_rdlock((l))
278 #define HEIMDAL_RWLOCK_wrlock(l) heim_rwlock_wrlock((l))
279 #define HEIMDAL_RWLOCK_tryrdlock(l) heim_rwlock_tryrdlock((l))
280 #define HEIMDAL_RWLOCK_trywrlock(l) heim_rwlock_trywrlock((l))
281 #define HEIMDAL_RWLOCK_unlock(l) heim_rwlock_unlock((l))
282 #define HEIMDAL_RWLOCK_destroy(l) heim_rwlock_destroy((l))
284 #define HEIMDAL_thread_key unsigned long
285 #define HEIM_PRIV_thread_key HEIMDAL_thread_key
286 #define HEIMDAL_key_create(k,d,r) do { r = heim_w32_key_create(k,d); } while(0)
287 #define HEIMDAL_setspecific(k,s,r) do { r = heim_w32_setspecific(k,s); } while(0)
288 #define HEIMDAL_getspecific(k) (heim_w32_getspecific(k))
289 #define HEIMDAL_key_delete(k) (heim_w32_delete_key(k))
291 #define HEIMDAL_THREAD_ID DWORD
292 #define HEIMDAL_THREAD_create(t,f,a) \
293 ((CreateThread(0, 0, (f), (a), 0, (t)) == INVALID_HANDLE_VALUE) ? EINVAL : 0)
295 #elif defined(HEIMDAL_DEBUG_THREADS)
297 /* no threads support, just do consistency checks */
298 #include <stdlib.h>
300 #define HEIMDAL_MUTEX int
301 #define HEIMDAL_MUTEX_INITIALIZER 0
302 #define HEIMDAL_MUTEX_init(m) do { (*(m)) = 0; } while(0)
303 #define HEIMDAL_MUTEX_lock(m) do { if ((*(m))++ != 0) abort(); } while(0)
304 #define HEIMDAL_MUTEX_unlock(m) do { if ((*(m))-- != 1) abort(); } while(0)
305 #define HEIMDAL_MUTEX_destroy(m) do {if ((*(m)) != 0) abort(); } while(0)
307 #define HEIMDAL_RWLOCK rwlock_t int
308 #define HEIMDAL_RWLOCK_INITIALIZER 0
309 #define HEIMDAL_RWLOCK_init(l) do { } while(0)
310 #define HEIMDAL_RWLOCK_rdlock(l) do { } while(0)
311 #define HEIMDAL_RWLOCK_wrlock(l) do { } while(0)
312 #define HEIMDAL_RWLOCK_tryrdlock(l) do { } while(0)
313 #define HEIMDAL_RWLOCK_trywrlock(l) do { } while(0)
314 #define HEIMDAL_RWLOCK_unlock(l) do { } while(0)
315 #define HEIMDAL_RWLOCK_destroy(l) do { } while(0)
317 #define HEIMDAL_internal_thread_key 1
319 #define HEIMDAL_THREAD_ID int
320 #define HEIMDAL_THREAD_create(t,f,a) abort()
322 #else /* no thread support, no debug case */
324 #define HEIMDAL_MUTEX int
325 #define HEIMDAL_MUTEX_INITIALIZER 0
326 #define HEIMDAL_MUTEX_init(m) do { (void)(m); } while(0)
327 #define HEIMDAL_MUTEX_lock(m) do { (void)(m); } while(0)
328 #define HEIMDAL_MUTEX_unlock(m) do { (void)(m); } while(0)
329 #define HEIMDAL_MUTEX_destroy(m) do { (void)(m); } while(0)
331 #define HEIMDAL_RWLOCK rwlock_t int
332 #define HEIMDAL_RWLOCK_INITIALIZER 0
333 #define HEIMDAL_RWLOCK_init(l) do { } while(0)
334 #define HEIMDAL_RWLOCK_rdlock(l) do { } while(0)
335 #define HEIMDAL_RWLOCK_wrlock(l) do { } while(0)
336 #define HEIMDAL_RWLOCK_tryrdlock(l) do { } while(0)
337 #define HEIMDAL_RWLOCK_trywrlock(l) do { } while(0)
338 #define HEIMDAL_RWLOCK_unlock(l) do { } while(0)
339 #define HEIMDAL_RWLOCK_destroy(l) do { } while(0)
341 #define HEIMDAL_THREAD_ID int
342 #define HEIMDAL_THREAD_create(t,f,a) abort()
344 #define HEIMDAL_internal_thread_key 1
346 #endif /* no thread support */
348 #ifdef HEIMDAL_internal_thread_key
350 typedef struct heim_thread_key {
351 void *value;
352 void (*destructor)(void *);
353 } heim_thread_key;
355 #define HEIMDAL_thread_key heim_thread_key
356 #define HEIMDAL_key_create(k,d,r) \
357 do { (k)->value = NULL; (k)->destructor = (d); r = 0; } while(0)
358 #define HEIMDAL_setspecific(k,s,r) do { (k).value = s ; r = 0; } while(0)
359 #define HEIMDAL_getspecific(k) ((k).value)
360 #define HEIMDAL_key_delete(k) do { (*(k).destructor)((k).value); } while(0)
362 #undef HEIMDAL_internal_thread_key
363 #endif /* HEIMDAL_internal_thread_key */
365 int heim_w32_key_create(HEIM_PRIV_thread_key *, void (*)(void *));
366 int heim_w32_delete_key(HEIM_PRIV_thread_key);
367 int heim_w32_setspecific(HEIM_PRIV_thread_key, void *);
368 void *heim_w32_getspecific(HEIM_PRIV_thread_key);
370 #endif /* HEIM_THREADS_H */