1/* $NetBSD: heim_threads.h,v 1.2 2017/01/28 21:31:44 christos Exp $ */
2
3/*
4 * Copyright (c) 2003-2016 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36/* Id */
37
38/*
39 * Provide wrapper macros for thread synchronization primitives so we
40 * can use native thread functions for those operating system that
41 * supports it.
42 *
43 * This is so libkrb5.so (or more importantly, libgssapi.so) can have
44 * thread support while the program that that dlopen(3)s the library
45 * don't need to be linked to libpthread.
46 */
47
48#ifndef HEIM_THREADS_H
49#define HEIM_THREADS_H 1
50
51#ifdef _MSC_VER
52
53#define HEIMDAL_THREAD_LOCAL __declspec(thread)
54
55#else
56
57#if defined(__clang__) || defined(__GNUC__) || defined(__SUNPRO_CC) || defined(__lint__)
58#define HEIMDAL_THREAD_LOCAL __thread
59#else
60#error "thread-local attribute not defined for your compiler"
61#endif /* clang or gcc */
62
63#endif /* _MSC_VER */
64
65/* For testing the for-Windows implementation of thread keys on non-Windows */
66typedef unsigned long HEIM_PRIV_thread_key;
67
68
69/* assume headers already included */
70
71#if defined(__NetBSD__) && __NetBSD_Version__ >= 106120000 && __NetBSD_Version__< 299001200 && defined(ENABLE_PTHREAD_SUPPORT)
72
73/*
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.
78 */
79#include <threadlib.h>
80
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)
87
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)
97
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)
103
104#define HEIMDAL_THREAD_ID thr_t
105#define HEIMDAL_THREAD_create(t,f,a) thr_create((t), 0, (f), (a))
106
107#elif defined(ENABLE_PTHREAD_SUPPORT) && (!defined(__NetBSD__) || __NetBSD_Version__ >= 299001200)
108
109#include <pthread.h>
110
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)
117
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)
127
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
142
143#define HEIMDAL_THREAD_ID pthread_t
144#define HEIMDAL_THREAD_create(t,f,a) pthread_create((t), 0, (f), (a))
145
146#elif defined(_WIN32)
147
148typedef struct heim_mutex {
149 HANDLE h;
150} heim_mutex_t;
151
152static inline int
153heim_mutex_init(heim_mutex_t *m)
154{
155 m->h = CreateSemaphore(NULL, 1, 1, NULL);
156 if (m->h == INVALID_HANDLE_VALUE)
157 return EAGAIN;
158 return 0;
159}
160
161static inline int
162heim_mutex_lock(heim_mutex_t *m)
163{
164 HANDLE h, new_h;
165 int created = 0;
166
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);
176 }
177 }
178 if (!created)
179 WaitForSingleObject(m->h, INFINITE);
180 return 0;
181}
182
183static inline int
184heim_mutex_unlock(heim_mutex_t *m)
185{
186 if (ReleaseSemaphore(m->h, 1, NULL) == FALSE)
187 return EPERM;
188 return 0;
189}
190
191static inline int
192heim_mutex_destroy(heim_mutex_t *m)
193{
194 HANDLE h;
195
196 h = InterlockedCompareExchangePointer(&m->h, INVALID_HANDLE_VALUE, m->h);
197 if (h != INVALID_HANDLE_VALUE)
198 CloseHandle(h);
199 return 0;
200}
201
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))
208
209typedef struct heim_rwlock {
210 SRWLOCK lock;
211 int exclusive;
212} heim_rwlock_t;
213
214static inline int
215heim_rwlock_init(heim_rwlock_t *l)
216{
217 InitializeSRWLock(&l->lock);
218 l->exclusive = 0;
219 return 0;
220}
221
222static inline int
223heim_rwlock_rdlock(heim_rwlock_t *l)
224{
225 AcquireSRWLockShared(&l->lock);
226 return 0;
227}
228
229static inline int
230heim_rwlock_wrlock(heim_rwlock_t *l)
231{
232 AcquireSRWLockExclusive(&l->lock);
233 l->exclusive = 1;
234 return 0;
235}
236
237static inline int
238heim_rwlock_tryrdlock(heim_rwlock_t *l)
239{
240 if (TryAcquireSRWLockShared(&l->lock))
241 return 0;
242 return EBUSY;
243}
244
245static inline int
246heim_rwlock_trywrlock(heim_rwlock_t *l)
247{
248 if (TryAcquireSRWLockExclusive(&l->lock))
249 return 0;
250 return EBUSY;
251}
252
253static inline int
254heim_rwlock_unlock(heim_rwlock_t *l)
255{
256 if (l->exclusive) {
257 l->exclusive = 0;
258 ReleaseSRWLockExclusive(&(l)->lock);
259 } else {
260 ReleaseSRWLockShared(&(l)->lock);
261 }
262 return 0;
263}
264
265static inline int
266heim_rwlock_destroy(heim_rwlock_t *l)
267{
268 /* SRW locks cannot be destroyed so re-initialize */
269 InitializeSRWLock(&l->lock);
270 l->exclusive = 0;
271 return 0;
272}
273
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))
283
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))
290
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)
294
295#elif defined(HEIMDAL_DEBUG_THREADS)
296
297/* no threads support, just do consistency checks */
298#include <stdlib.h>
299
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)
306
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)
316
317#define HEIMDAL_internal_thread_key 1
318
319#define HEIMDAL_THREAD_ID int
320#define HEIMDAL_THREAD_create(t,f,a) abort()
321
322#else /* no thread support, no debug case */
323
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)
330
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)
340
341#define HEIMDAL_THREAD_ID int
342#define HEIMDAL_THREAD_create(t,f,a) abort()
343
344#define HEIMDAL_internal_thread_key 1
345
346#endif /* no thread support */
347
348#ifdef HEIMDAL_internal_thread_key
349
350typedef struct heim_thread_key {
351 void *value;
352 void (*destructor)(void *);
353} heim_thread_key;
354
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)
361
362#undef HEIMDAL_internal_thread_key
363#endif /* HEIMDAL_internal_thread_key */
364
365int heim_w32_key_create(HEIM_PRIV_thread_key *, void (*)(void *));
366int heim_w32_delete_key(HEIM_PRIV_thread_key);
367int heim_w32_setspecific(HEIM_PRIV_thread_key, void *);
368void *heim_w32_getspecific(HEIM_PRIV_thread_key);
369
370#endif /* HEIM_THREADS_H */
371