heim_threads.h revision 1.1 1 /* $NetBSD: heim_threads.h,v 1.1 2011/04/13 18:14:34 elric Exp $ */
2
3 /*
4 * Copyright (c) 2003 Kungliga Tekniska Hgskolan
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: heim_threads.h,v 1.1 2011/04/13 18:14:34 elric Exp $ */
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 /* assume headers already included */
52
53 #if defined(__NetBSD__) && __NetBSD_Version__ >= 106120000 && __NetBSD_Version__< 299001200 && defined(ENABLE_PTHREAD_SUPPORT)
54
55 /*
56 * NetBSD have a thread lib that we can use that part of libc that
57 * works regardless if application are linked to pthreads or not.
58 * NetBSD newer then 2.99.11 just use pthread.h, and the same thing
59 * will happen.
60 */
61 #include <threadlib.h>
62
63 #define HEIMDAL_MUTEX mutex_t
64 #define HEIMDAL_MUTEX_INITIALIZER MUTEX_INITIALIZER
65 #define HEIMDAL_MUTEX_init(m) mutex_init(m, NULL)
66 #define HEIMDAL_MUTEX_lock(m) mutex_lock(m)
67 #define HEIMDAL_MUTEX_unlock(m) mutex_unlock(m)
68 #define HEIMDAL_MUTEX_destroy(m) mutex_destroy(m)
69
70 #define HEIMDAL_RWLOCK rwlock_t
71 #define HEIMDAL_RWLOCK_INITIALIZER RWLOCK_INITIALIZER
72 #define HEIMDAL_RWLOCK_init(l) rwlock_init(l, NULL)
73 #define HEIMDAL_RWLOCK_rdlock(l) rwlock_rdlock(l)
74 #define HEIMDAL_RWLOCK_wrlock(l) rwlock_wrlock(l)
75 #define HEIMDAL_RWLOCK_tryrdlock(l) rwlock_tryrdlock(l)
76 #define HEIMDAL_RWLOCK_trywrlock(l) rwlock_trywrlock(l)
77 #define HEIMDAL_RWLOCK_unlock(l) rwlock_unlock(l)
78 #define HEIMDAL_RWLOCK_destroy(l) rwlock_destroy(l)
79
80 #define HEIMDAL_thread_key thread_key_t
81 #define HEIMDAL_key_create(k,d,r) do { r = thr_keycreate(k,d); } while(0)
82 #define HEIMDAL_setspecific(k,s,r) do { r = thr_setspecific(k,s); } while(0)
83 #define HEIMDAL_getspecific(k) thr_getspecific(k)
84 #define HEIMDAL_key_delete(k) thr_keydelete(k)
85
86 #elif defined(ENABLE_PTHREAD_SUPPORT) && (!defined(__NetBSD__) || __NetBSD_Version__ >= 299001200)
87
88 #include <pthread.h>
89
90 #define HEIMDAL_MUTEX pthread_mutex_t
91 #define HEIMDAL_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
92 #define HEIMDAL_MUTEX_init(m) pthread_mutex_init(m, NULL)
93 #define HEIMDAL_MUTEX_lock(m) pthread_mutex_lock(m)
94 #define HEIMDAL_MUTEX_unlock(m) pthread_mutex_unlock(m)
95 #define HEIMDAL_MUTEX_destroy(m) pthread_mutex_destroy(m)
96
97 #define HEIMDAL_RWLOCK rwlock_t
98 #define HEIMDAL_RWLOCK_INITIALIZER RWLOCK_INITIALIZER
99 #define HEIMDAL_RWLOCK_init(l) pthread_rwlock_init(l, NULL)
100 #define HEIMDAL_RWLOCK_rdlock(l) pthread_rwlock_rdlock(l)
101 #define HEIMDAL_RWLOCK_wrlock(l) pthread_rwlock_wrlock(l)
102 #define HEIMDAL_RWLOCK_tryrdlock(l) pthread_rwlock_tryrdlock(l)
103 #define HEIMDAL_RWLOCK_trywrlock(l) pthread_rwlock_trywrlock(l)
104 #define HEIMDAL_RWLOCK_unlock(l) pthread_rwlock_unlock(l)
105 #define HEIMDAL_RWLOCK_destroy(l) pthread_rwlock_destroy(l)
106
107 #define HEIMDAL_thread_key pthread_key_t
108 #define HEIMDAL_key_create(k,d,r) do { r = pthread_key_create(k,d); } while(0)
109 #define HEIMDAL_setspecific(k,s,r) do { r = pthread_setspecific(k,s); } while(0)
110 #define HEIMDAL_getspecific(k) pthread_getspecific(k)
111 #define HEIMDAL_key_delete(k) pthread_key_delete(k)
112
113 #elif defined(HEIMDAL_DEBUG_THREADS)
114
115 /* no threads support, just do consistency checks */
116 #include <stdlib.h>
117
118 #define HEIMDAL_MUTEX int
119 #define HEIMDAL_MUTEX_INITIALIZER 0
120 #define HEIMDAL_MUTEX_init(m) do { (*(m)) = 0; } while(0)
121 #define HEIMDAL_MUTEX_lock(m) do { if ((*(m))++ != 0) abort(); } while(0)
122 #define HEIMDAL_MUTEX_unlock(m) do { if ((*(m))-- != 1) abort(); } while(0)
123 #define HEIMDAL_MUTEX_destroy(m) do {if ((*(m)) != 0) abort(); } while(0)
124
125 #define HEIMDAL_RWLOCK rwlock_t int
126 #define HEIMDAL_RWLOCK_INITIALIZER 0
127 #define HEIMDAL_RWLOCK_init(l) do { } while(0)
128 #define HEIMDAL_RWLOCK_rdlock(l) do { } while(0)
129 #define HEIMDAL_RWLOCK_wrlock(l) do { } while(0)
130 #define HEIMDAL_RWLOCK_tryrdlock(l) do { } while(0)
131 #define HEIMDAL_RWLOCK_trywrlock(l) do { } while(0)
132 #define HEIMDAL_RWLOCK_unlock(l) do { } while(0)
133 #define HEIMDAL_RWLOCK_destroy(l) do { } while(0)
134
135 #define HEIMDAL_internal_thread_key 1
136
137 #else /* no thread support, no debug case */
138
139 #define HEIMDAL_MUTEX int
140 #define HEIMDAL_MUTEX_INITIALIZER 0
141 #define HEIMDAL_MUTEX_init(m) do { (void)(m); } while(0)
142 #define HEIMDAL_MUTEX_lock(m) do { (void)(m); } while(0)
143 #define HEIMDAL_MUTEX_unlock(m) do { (void)(m); } while(0)
144 #define HEIMDAL_MUTEX_destroy(m) do { (void)(m); } while(0)
145
146 #define HEIMDAL_RWLOCK rwlock_t int
147 #define HEIMDAL_RWLOCK_INITIALIZER 0
148 #define HEIMDAL_RWLOCK_init(l) do { } while(0)
149 #define HEIMDAL_RWLOCK_rdlock(l) do { } while(0)
150 #define HEIMDAL_RWLOCK_wrlock(l) do { } while(0)
151 #define HEIMDAL_RWLOCK_tryrdlock(l) do { } while(0)
152 #define HEIMDAL_RWLOCK_trywrlock(l) do { } while(0)
153 #define HEIMDAL_RWLOCK_unlock(l) do { } while(0)
154 #define HEIMDAL_RWLOCK_destroy(l) do { } while(0)
155
156 #define HEIMDAL_internal_thread_key 1
157
158 #endif /* no thread support */
159
160 #ifdef HEIMDAL_internal_thread_key
161
162 typedef struct heim_thread_key {
163 void *value;
164 void (*destructor)(void *);
165 } heim_thread_key;
166
167 #define HEIMDAL_thread_key heim_thread_key
168 #define HEIMDAL_key_create(k,d,r) \
169 do { (k)->value = NULL; (k)->destructor = (d); r = 0; } while(0)
170 #define HEIMDAL_setspecific(k,s,r) do { (k).value = s ; r = 0; } while(0)
171 #define HEIMDAL_getspecific(k) ((k).value)
172 #define HEIMDAL_key_delete(k) do { (*(k).destructor)((k).value); } while(0)
173
174 #undef HEIMDAL_internal_thread_key
175 #endif /* HEIMDAL_internal_thread_key */
176
177 #endif /* HEIM_THREADS_H */
178