Apply band-aid to install-build-headers (#114)
[heimdal.git] / include / heim_threads.h
blobc1ebbaca30b4f6e4992cc1e68a9d79af7a6bd07d
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 #ifdef _MSC_VER
51 #define HEIMDAL_THREAD_LOCAL __declspec(thread)
53 #else
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 */
61 #endif /* _MSC_VER */
63 /* For testing the for-Windows implementation of thread keys on non-Windows */
64 typedef unsigned long HEIM_PRIV_thread_key;
67 /* assume headers already included */
69 #if defined(__NetBSD__) && __NetBSD_Version__ >= 106120000 && __NetBSD_Version__< 299001200 && defined(ENABLE_PTHREAD_SUPPORT)
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.
77 #include <threadlib.h>
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)
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)
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)
102 #define HEIMDAL_THREAD_ID thr_t
103 #define HEIMDAL_THREAD_create(t,f,a) thr_create((t), 0, (f), (a))
105 #elif defined(ENABLE_PTHREAD_SUPPORT) && (!defined(__NetBSD__) || __NetBSD_Version__ >= 299001200)
107 #include <pthread.h>
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)
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)
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
141 #define HEIMDAL_THREAD_ID pthread_t
142 #define HEIMDAL_THREAD_create(t,f,a) pthread_create((t), 0, (f), (a))
144 #elif defined(_WIN32)
146 typedef struct heim_mutex {
147 HANDLE h;
148 } heim_mutex_t;
150 static inline int
151 heim_mutex_init(heim_mutex_t *m)
153 m->h = CreateSemaphore(NULL, 1, 1, NULL);
154 if (m->h == INVALID_HANDLE_VALUE)
155 return EAGAIN;
156 return 0;
159 static inline int
160 heim_mutex_lock(heim_mutex_t *m)
162 HANDLE h, new_h;
163 int created = 0;
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);
176 if (!created)
177 WaitForSingleObject(m->h, INFINITE);
178 return 0;
181 static inline int
182 heim_mutex_unlock(heim_mutex_t *m)
184 if (ReleaseSemaphore(m->h, 1, NULL) == FALSE)
185 return EPERM;
186 return 0;
189 static inline int
190 heim_mutex_destroy(heim_mutex_t *m)
192 HANDLE h;
194 h = InterlockedCompareExchangePointer(&m->h, INVALID_HANDLE_VALUE, m->h);
195 if (h != INVALID_HANDLE_VALUE)
196 CloseHandle(h);
197 return 0;
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))
207 typedef struct heim_rwlock {
208 SRWLOCK lock;
209 int exclusive;
210 } heim_rwlock_t;
212 static inline int
213 heim_rwlock_init(heim_rwlock_t *l)
215 InitializeSRWLock(&l->lock);
216 l->exclusive = 0;
217 return 0;
220 static inline int
221 heim_rwlock_rdlock(heim_rwlock_t *l)
223 AcquireSRWLockShared(&l->lock);
224 return 0;
227 static inline int
228 heim_rwlock_wrlock(heim_rwlock_t *l)
230 AcquireSRWLockExclusive(&l->lock);
231 l->exclusive = 1;
232 return 0;
235 static inline int
236 heim_rwlock_tryrdlock(heim_rwlock_t *l)
238 if (TryAcquireSRWLockShared(&l->lock))
239 return 0;
240 return EBUSY;
243 static inline int
244 heim_rwlock_trywrlock(heim_rwlock_t *l)
246 if (TryAcquireSRWLockExclusive(&l->lock))
247 return 0;
248 return EBUSY;
251 static inline int
252 heim_rwlock_unlock(heim_rwlock_t *l)
254 if (l->exclusive) {
255 l->exclusive = 0;
256 ReleaseSRWLockExclusive(&(l)->lock);
257 } else {
258 ReleaseSRWLockShared(&(l)->lock);
260 return 0;
263 static inline int
264 heim_rwlock_destroy(heim_rwlock_t *l)
266 /* SRW locks cannot be destroyed so re-initialize */
267 InitializeSRWLock(&l->lock);
268 l->exclusive = 0;
269 return 0;
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))
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))
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)
293 #elif defined(HEIMDAL_DEBUG_THREADS)
295 /* no threads support, just do consistency checks */
296 #include <stdlib.h>
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)
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)
315 #define HEIMDAL_internal_thread_key 1
317 #define HEIMDAL_THREAD_ID int
318 #define HEIMDAL_THREAD_create(t,f,a) abort()
320 #else /* no thread support, no debug case */
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)
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)
339 #define HEIMDAL_THREAD_ID int
340 #define HEIMDAL_THREAD_create(t,f,a) abort()
342 #define HEIMDAL_internal_thread_key 1
344 #endif /* no thread support */
346 #ifdef HEIMDAL_internal_thread_key
348 typedef struct heim_thread_key {
349 void *value;
350 void (*destructor)(void *);
351 } heim_thread_key;
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)
360 #undef HEIMDAL_internal_thread_key
361 #endif /* HEIMDAL_internal_thread_key */
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);
368 #endif /* HEIM_THREADS_H */