threads: Windows mutex and rwlock implementation
[heimdal.git] / include / heim_threads.h
bloba7e9f69e10f57a883f20d38a460ad4cd02d076c6
1 /*
2 * Copyright (c) 2003 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 /* assume headers already included */
51 #if defined(__NetBSD__) && __NetBSD_Version__ >= 106120000 && __NetBSD_Version__< 299001200 && defined(ENABLE_PTHREAD_SUPPORT)
54 * NetBSD have a thread lib that we can use that part of libc that
55 * works regardless if application are linked to pthreads or not.
56 * NetBSD newer then 2.99.11 just use pthread.h, and the same thing
57 * will happen.
59 #include <threadlib.h>
61 #define HEIMDAL_MUTEX mutex_t
62 #define HEIMDAL_MUTEX_INITIALIZER MUTEX_INITIALIZER
63 #define HEIMDAL_MUTEX_init(m) mutex_init(m, NULL)
64 #define HEIMDAL_MUTEX_lock(m) mutex_lock(m)
65 #define HEIMDAL_MUTEX_unlock(m) mutex_unlock(m)
66 #define HEIMDAL_MUTEX_destroy(m) mutex_destroy(m)
68 #define HEIMDAL_RWLOCK rwlock_t
69 #define HEIMDAL_RWLOCK_INITIALIZER RWLOCK_INITIALIZER
70 #define HEIMDAL_RWLOCK_init(l) rwlock_init(l, NULL)
71 #define HEIMDAL_RWLOCK_rdlock(l) rwlock_rdlock(l)
72 #define HEIMDAL_RWLOCK_wrlock(l) rwlock_wrlock(l)
73 #define HEIMDAL_RWLOCK_tryrdlock(l) rwlock_tryrdlock(l)
74 #define HEIMDAL_RWLOCK_trywrlock(l) rwlock_trywrlock(l)
75 #define HEIMDAL_RWLOCK_unlock(l) rwlock_unlock(l)
76 #define HEIMDAL_RWLOCK_destroy(l) rwlock_destroy(l)
78 #define HEIMDAL_thread_key thread_key_t
79 #define HEIMDAL_key_create(k,d,r) do { r = thr_keycreate(k,d); } while(0)
80 #define HEIMDAL_setspecific(k,s,r) do { r = thr_setspecific(k,s); } while(0)
81 #define HEIMDAL_getspecific(k) thr_getspecific(k)
82 #define HEIMDAL_key_delete(k) thr_keydelete(k)
84 #elif defined(ENABLE_PTHREAD_SUPPORT) && (!defined(__NetBSD__) || __NetBSD_Version__ >= 299001200)
86 #include <pthread.h>
88 #define HEIMDAL_MUTEX pthread_mutex_t
89 #define HEIMDAL_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
90 #define HEIMDAL_MUTEX_init(m) pthread_mutex_init(m, NULL)
91 #define HEIMDAL_MUTEX_lock(m) pthread_mutex_lock(m)
92 #define HEIMDAL_MUTEX_unlock(m) pthread_mutex_unlock(m)
93 #define HEIMDAL_MUTEX_destroy(m) pthread_mutex_destroy(m)
95 #define HEIMDAL_RWLOCK rwlock_t
96 #define HEIMDAL_RWLOCK_INITIALIZER RWLOCK_INITIALIZER
97 #define HEIMDAL_RWLOCK_init(l) pthread_rwlock_init(l, NULL)
98 #define HEIMDAL_RWLOCK_rdlock(l) pthread_rwlock_rdlock(l)
99 #define HEIMDAL_RWLOCK_wrlock(l) pthread_rwlock_wrlock(l)
100 #define HEIMDAL_RWLOCK_tryrdlock(l) pthread_rwlock_tryrdlock(l)
101 #define HEIMDAL_RWLOCK_trywrlock(l) pthread_rwlock_trywrlock(l)
102 #define HEIMDAL_RWLOCK_unlock(l) pthread_rwlock_unlock(l)
103 #define HEIMDAL_RWLOCK_destroy(l) pthread_rwlock_destroy(l)
105 #define HEIMDAL_thread_key pthread_key_t
106 #define HEIMDAL_key_create(k,d,r) do { r = pthread_key_create(k,d); } while(0)
107 #define HEIMDAL_setspecific(k,s,r) do { r = pthread_setspecific(k,s); } while(0)
108 #define HEIMDAL_getspecific(k) pthread_getspecific(k)
109 #define HEIMDAL_key_delete(k) pthread_key_delete(k)
111 #elif defined(_WIN32)
113 typedef struct heim_mutex {
114 HANDLE h;
115 } heim_mutex_t;
117 static inline void
118 heim_mutex_init(heim_mutex_t *m)
120 m->h = CreateSemaphore(NULL, 1, 1, NULL);
121 if (m->h == INVALID_HANDLE_VALUE)
122 abort();
125 static inline void
126 heim_mutex_lock(heim_mutex_t *m)
128 HANDLE h, new_h;
129 int created = 0;
131 h = InterlockedCompareExchangePointer(&m->h, m->h, m->h);
132 if (h == INVALID_HANDLE_VALUE || h == NULL) {
133 created = 1;
134 new_h = CreateSemaphore(NULL, 0, 1, NULL);
135 if (new_h == INVALID_HANDLE_VALUE)
136 abort();
137 if (InterlockedCompareExchangePointer(&m->h, new_h, h) != h) {
138 created = 0;
139 CloseHandle(new_h);
142 if (!created)
143 WaitForSingleObject(m->h, INFINITE);
146 static inline void
147 heim_mutex_unlock(heim_mutex_t *m)
149 if (ReleaseSemaphore(m->h, 1, NULL) == FALSE)
150 abort();
153 static inline void
154 heim_mutex_destroy(heim_mutex_t *m)
156 HANDLE h;
158 h = InterlockedCompareExchangePointer(&m->h, INVALID_HANDLE_VALUE, m->h);
159 if (h != INVALID_HANDLE_VALUE)
160 CloseHandle(h);
163 #define HEIMDAL_MUTEX heim_mutex_t
164 #define HEIMDAL_MUTEX_INITIALIZER { INVALID_HANDLE_VALUE }
165 #define HEIMDAL_MUTEX_init(m) heim_mutex_init((m))
166 #define HEIMDAL_MUTEX_lock(m) heim_mutex_lock((m))
167 #define HEIMDAL_MUTEX_unlock(m) heim_mutex_unlock((m))
168 #define HEIMDAL_MUTEX_destroy(m) heim_mutex_destroy((m))
170 typedef struct heim_rwlock {
171 SRWLOCK lock;
172 int exclusive;
173 } heim_rwlock_t;
175 static inline void
176 heim_rwlock_init(heim_rwlock_t *l)
178 InitializeSRWLock(&l->lock);
179 l->exclusive = 0;
182 static inline void
183 heim_rwlock_rdlock(heim_rwlock_t *l)
185 AcquireSRWLockShared(&l->lock);
188 static inline void
189 heim_rwlock_wrlock(heim_rwlock_t *l)
191 AcquireSRWLockExclusive(&l->lock);
192 l->exclusive = 1;
195 static inline BOOLEAN
196 heim_rwlock_tryrdlock(heim_rwlock_t *l)
198 BOOLEAN bLocked;
200 bLocked = TryAcquireSRWLockShared(&l->lock);
202 return bLocked;
205 static inline BOOLEAN
206 heim_rwlock_trywrlock(heim_rwlock_t *l)
208 BOOLEAN bLocked;
210 bLocked = TryAcquireSRWLockExclusive(&l->lock);
211 if (bLocked)
212 l->exclusive = 1;
214 return bLocked;
217 static inline void
218 heim_rwlock_unlock(heim_rwlock_t *l)
220 if (l->exclusive) {
221 l->exclusive = 0;
222 ReleaseSRWLockExclusive(&(l)->lock);
223 } else {
224 ReleaseSRWLockShared(&(l)->lock);
228 static inline void
229 heim_rwlock_destroy(heim_rwlock_t *l)
231 /* SRW locks cannot be destroyed so re-initialize */
232 InitializeSRWLock(&l->lock);
233 l->exclusive = 0;
236 #define HEIMDAL_RWLOCK heim_rwlock_t
237 #define HEIMDAL_RWLOCK_INITIALIZER {SRWLOCK_INIT, 0}
238 #define HEIMDAL_RWLOCK_init(l) heim_rwlock_init((l))
239 #define HEIMDAL_RWLOCK_rdlock(l) heim_rwlock_rdlock((l))
240 #define HEIMDAL_RWLOCK_wrlock(l) heim_rwlock_wrlock((l))
241 #define HEIMDAL_RWLOCK_tryrdlock(l) heim_rwlock_tryrdlock((l))
242 #define HEIMDAL_RWLOCK_trywrlock(l) heim_rwlock_trywrlock((l))
243 #define HEIMDAL_RWLOCK_unlock(l) heim_rwlock_unlock((l))
244 #define HEIMDAL_RWLOCK_destroy(l) heim_rwlock_destroy((l))
246 #define HEIMDAL_internal_thread_key 1
248 #elif defined(HEIMDAL_DEBUG_THREADS)
250 /* no threads support, just do consistency checks */
251 #include <stdlib.h>
253 #define HEIMDAL_MUTEX int
254 #define HEIMDAL_MUTEX_INITIALIZER 0
255 #define HEIMDAL_MUTEX_init(m) do { (*(m)) = 0; } while(0)
256 #define HEIMDAL_MUTEX_lock(m) do { if ((*(m))++ != 0) abort(); } while(0)
257 #define HEIMDAL_MUTEX_unlock(m) do { if ((*(m))-- != 1) abort(); } while(0)
258 #define HEIMDAL_MUTEX_destroy(m) do {if ((*(m)) != 0) abort(); } while(0)
260 #define HEIMDAL_RWLOCK rwlock_t int
261 #define HEIMDAL_RWLOCK_INITIALIZER 0
262 #define HEIMDAL_RWLOCK_init(l) do { } while(0)
263 #define HEIMDAL_RWLOCK_rdlock(l) do { } while(0)
264 #define HEIMDAL_RWLOCK_wrlock(l) do { } while(0)
265 #define HEIMDAL_RWLOCK_tryrdlock(l) do { } while(0)
266 #define HEIMDAL_RWLOCK_trywrlock(l) do { } while(0)
267 #define HEIMDAL_RWLOCK_unlock(l) do { } while(0)
268 #define HEIMDAL_RWLOCK_destroy(l) do { } while(0)
270 #define HEIMDAL_internal_thread_key 1
272 #else /* no thread support, no debug case */
274 #define HEIMDAL_MUTEX int
275 #define HEIMDAL_MUTEX_INITIALIZER 0
276 #define HEIMDAL_MUTEX_init(m) do { (void)(m); } while(0)
277 #define HEIMDAL_MUTEX_lock(m) do { (void)(m); } while(0)
278 #define HEIMDAL_MUTEX_unlock(m) do { (void)(m); } while(0)
279 #define HEIMDAL_MUTEX_destroy(m) do { (void)(m); } while(0)
281 #define HEIMDAL_RWLOCK rwlock_t int
282 #define HEIMDAL_RWLOCK_INITIALIZER 0
283 #define HEIMDAL_RWLOCK_init(l) do { } while(0)
284 #define HEIMDAL_RWLOCK_rdlock(l) do { } while(0)
285 #define HEIMDAL_RWLOCK_wrlock(l) do { } while(0)
286 #define HEIMDAL_RWLOCK_tryrdlock(l) do { } while(0)
287 #define HEIMDAL_RWLOCK_trywrlock(l) do { } while(0)
288 #define HEIMDAL_RWLOCK_unlock(l) do { } while(0)
289 #define HEIMDAL_RWLOCK_destroy(l) do { } while(0)
291 #define HEIMDAL_internal_thread_key 1
293 #endif /* no thread support */
295 #ifdef HEIMDAL_internal_thread_key
297 typedef struct heim_thread_key {
298 void *value;
299 void (*destructor)(void *);
300 } heim_thread_key;
302 #define HEIMDAL_thread_key heim_thread_key
303 #define HEIMDAL_key_create(k,d,r) \
304 do { (k)->value = NULL; (k)->destructor = (d); r = 0; } while(0)
305 #define HEIMDAL_setspecific(k,s,r) do { (k).value = s ; r = 0; } while(0)
306 #define HEIMDAL_getspecific(k) ((k).value)
307 #define HEIMDAL_key_delete(k) do { (*(k).destructor)((k).value); } while(0)
309 #undef HEIMDAL_internal_thread_key
310 #endif /* HEIMDAL_internal_thread_key */
312 #endif /* HEIM_THREADS_H */