require local
[heimdal.git] / include / heim_threads.h
blobc4f841fb61f08fb54337ff5a83c217e8ce65c88d
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(HEIMDAL_DEBUG_THREADS)
113 /* no threads support, just do consistency checks */
114 #include <stdlib.h>
116 #define HEIMDAL_MUTEX int
117 #define HEIMDAL_MUTEX_INITIALIZER 0
118 #define HEIMDAL_MUTEX_init(m) do { (*(m)) = 0; } while(0)
119 #define HEIMDAL_MUTEX_lock(m) do { if ((*(m))++ != 0) abort(); } while(0)
120 #define HEIMDAL_MUTEX_unlock(m) do { if ((*(m))-- != 1) abort(); } while(0)
121 #define HEIMDAL_MUTEX_destroy(m) do {if ((*(m)) != 0) abort(); } while(0)
123 #define HEIMDAL_RWLOCK rwlock_t int
124 #define HEIMDAL_RWLOCK_INITIALIZER 0
125 #define HEIMDAL_RWLOCK_init(l) do { } while(0)
126 #define HEIMDAL_RWLOCK_rdlock(l) do { } while(0)
127 #define HEIMDAL_RWLOCK_wrlock(l) do { } while(0)
128 #define HEIMDAL_RWLOCK_tryrdlock(l) do { } while(0)
129 #define HEIMDAL_RWLOCK_trywrlock(l) do { } while(0)
130 #define HEIMDAL_RWLOCK_unlock(l) do { } while(0)
131 #define HEIMDAL_RWLOCK_destroy(l) do { } while(0)
133 #define HEIMDAL_internal_thread_key 1
135 #else /* no thread support, no debug case */
137 #define HEIMDAL_MUTEX int
138 #define HEIMDAL_MUTEX_INITIALIZER 0
139 #define HEIMDAL_MUTEX_init(m) do { (void)(m); } while(0)
140 #define HEIMDAL_MUTEX_lock(m) do { (void)(m); } while(0)
141 #define HEIMDAL_MUTEX_unlock(m) do { (void)(m); } while(0)
142 #define HEIMDAL_MUTEX_destroy(m) do { (void)(m); } while(0)
144 #define HEIMDAL_RWLOCK rwlock_t int
145 #define HEIMDAL_RWLOCK_INITIALIZER 0
146 #define HEIMDAL_RWLOCK_init(l) do { } while(0)
147 #define HEIMDAL_RWLOCK_rdlock(l) do { } while(0)
148 #define HEIMDAL_RWLOCK_wrlock(l) do { } while(0)
149 #define HEIMDAL_RWLOCK_tryrdlock(l) do { } while(0)
150 #define HEIMDAL_RWLOCK_trywrlock(l) do { } while(0)
151 #define HEIMDAL_RWLOCK_unlock(l) do { } while(0)
152 #define HEIMDAL_RWLOCK_destroy(l) do { } while(0)
154 #define HEIMDAL_internal_thread_key 1
156 #endif /* no thread support */
158 #ifdef HEIMDAL_internal_thread_key
160 typedef struct heim_thread_key {
161 void *value;
162 void (*destructor)(void *);
163 } heim_thread_key;
165 #define HEIMDAL_thread_key heim_thread_key
166 #define HEIMDAL_key_create(k,d,r) \
167 do { (k)->value = NULL; (k)->destructor = (d); r = 0; } while(0)
168 #define HEIMDAL_setspecific(k,s,r) do { (k).value = s ; r = 0; } while(0)
169 #define HEIMDAL_getspecific(k) ((k).value)
170 #define HEIMDAL_key_delete(k) do { (*(k).destructor)((k).value); } while(0)
172 #undef HEIMDAL_internal_thread_key
173 #endif /* HEIMDAL_internal_thread_key */
175 #endif /* HEIM_THREADS_H */