2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 * rx_kmutex.h - mutex and condition variable macros for kernel environment.
13 * FBSD implementation.
19 #include <sys/systm.h>
23 #include <sys/lockmgr.h>
28 #define RX_ENABLE_LOCKS 1
29 #define AFS_GLOBAL_RXLOCK_KERNEL
31 typedef int afs_kcondvar_t;
34 #if defined(NULL_LOCKS)
39 typedef afs_kmutex_t afs_krwlock_t;
40 #define RWLOCK_INIT(a, b, c, d) MUTEX_INIT(a,b,c,d)
41 #define RWLOCK_DESTROY(l) MUTEX_DESTROY(l)
42 #define RWLOCK_UPLOCK(l)
43 #define RWLOCK_WRLOCK(l) MUTEX_ENTER(l)
44 #define RWLOCK_RDLOCK(l) MUTEX_ENTER(l)
45 #define RWLOCK_TRYWRLOCK(l) MUTEX_TRYENTER(l)
46 #define RWLOCK_TRYRDLOCK(l) MUTEX_TRYENTER(l)
47 #define RWLOCK_UNLOCK(l) MUTEX_EXIT(l)
49 #define MUTEX_INIT(a,b,c,d) \
53 #define MUTEX_DESTROY(a) \
55 (a)->owner = (struct proc *)-1; \
57 #define MUTEX_ENTER(a) \
59 osi_Assert((a)->owner == 0); \
60 (a)->owner = curproc; \
62 #define MUTEX_TRYENTER(a) \
63 ( osi_Assert((a)->owner == 0), (a)->owner = curproc, 1)
64 #define MUTEX_EXIT(a) \
66 osi_Assert((a)->owner == curproc); \
71 #define MUTEX_ISMINE(a) (((afs_kmutex_t *)(a))->owner == curproc)
73 #elif defined(AFS_FBSD70_ENV) /* dunno about 6.x */
75 typedef struct mtx afs_kmutex_t;
77 #if defined(AFS_FBSD80_ENV) && defined(WITNESS)
78 #define WITCLEAR_MTX(a) \
79 do { memset((a), 0, sizeof(struct mtx)); } while(0);
81 #define WITCLEAR_MTX(a) {}
84 #define MUTEX_INIT(a,b,c,d) \
87 mtx_init((a), (b), 0 /* type defaults to name */, MTX_DEF | MTX_DUPOK); \
90 #define MUTEX_DESTROY(a) \
95 #define MUTEX_ENTER(a) \
100 #define MUTEX_TRYENTER(a) \
103 #define MUTEX_EXIT(a) \
109 #define MUTEX_ISMINE(a) \
112 #elif defined(AFS_FBSD50_ENV)
116 struct thread *owner;
120 #define MUTEX_INIT(a,b,c,d) \
122 lockinit(&(a)->lock,PSOCK, "afs rx mutex", 0, 0); \
125 #define MUTEX_DESTROY(a) \
127 (a)->owner = (struct proc *)-1; \
129 #define MUTEX_ENTER(a) \
131 lockmgr(&(a)->lock, LK_EXCLUSIVE, 0, curthread); \
132 osi_Assert((a)->owner == 0); \
133 (a)->owner = curthread; \
135 #define MUTEX_TRYENTER(a) \
136 ( lockmgr(&(a)->lock, LK_EXCLUSIVE|LK_NOWAIT, 0, curthread) ? 0 : ((a)->owner = curthread, 1) )
137 #define xMUTEX_TRYENTER(a) \
138 ( osi_Assert((a)->owner == 0), (a)->owner = curthread, 1)
139 #define MUTEX_EXIT(a) \
141 osi_Assert((a)->owner == curthread); \
143 lockmgr(&(a)->lock, LK_RELEASE, 0, curthread); \
147 #define MUTEX_ISMINE(a) (((afs_kmutex_t *)(a))->owner == curthread)
148 #elif defined(HEAVY_LOCKS)
155 #define MUTEX_INIT(a,b,c,d) \
157 lockinit(&(a)->lock, PSOCK, "afs rx mutex", 0, 0); \
160 #define MUTEX_DESTROY(a) \
162 (a)->owner = (struct proc *)-1; \
164 #define MUTEX_ENTER(a) \
166 lockmgr(&(a)->lock, LK_EXCLUSIVE, 0, curproc); \
167 osi_Assert((a)->owner == 0); \
168 (a)->owner = curproc; \
170 #define MUTEX_TRYENTER(a) \
171 ( lockmgr(&(a)->lock, LK_EXCLUSIVE|LK_NOWAIT, 0, curproc) ? 0 : ((a)->owner = curproc, 1) )
172 #define xMUTEX_TRYENTER(a) \
173 ( osi_Assert((a)->owner == 0), (a)->owner = curproc, 1)
174 #define MUTEX_EXIT(a) \
176 osi_Assert((a)->owner == curproc); \
178 lockmgr(&(a)->lock, LK_RELEASE, 0, curproc); \
182 #define MUTEX_ISMINE(a) (((afs_kmutex_t *)(a))->owner == curproc)
185 struct simplelock lock;
190 #define MUTEX_INIT(a,b,c,d) \
192 simple_lock_init(&(a)->lock); \
195 #define MUTEX_DESTROY(a) \
197 (a)->owner = (struct proc *)-1; \
199 #define MUTEX_ENTER(a) \
201 simple_lock(&(a)->lock); \
202 osi_Assert((a)->owner == 0); \
203 (a)->owner = curproc; \
205 #define MUTEX_TRYENTER(a) \
206 ( simple_lock_try(&(a)->lock) ? 0 : ((a)->owner = curproc, 1) )
207 #define MUTEX_EXIT(a) \
209 osi_Assert((a)->owner == curproc); \
211 simple_unlock(&(a)->lock); \
215 #define MUTEX_ISMINE(a) (((afs_kmutex_t *)(a))->owner == curproc)
219 #undef osirx_AssertMine
220 extern void osirx_AssertMine(afs_kmutex_t * lockaddr, char *msg);
224 * Condition variables
226 * In Digital Unix (OSF/1), we use something akin to the ancient sleep/wakeup
227 * mechanism. The condition variable itself plays no role; we just use its
228 * address as a convenient unique number.
230 #define CV_INIT(cv,a,b,c)
231 #define CV_DESTROY(cv)
233 #if defined(AFS_FBSD70_ENV)
235 #define CV_WAIT(cv, lck) { \
236 int isGlockOwner = ISAFS_GLOCK(); \
237 if (isGlockOwner) AFS_GUNLOCK(); \
238 msleep(cv, lck, PSOCK, "afs_rx_cv_wait", 0); \
239 if (isGlockOwner) AFS_GLOCK(); \
242 #define CV_TIMEDWAIT(cv,lck,t) { \
243 int isGlockOwner = ISAFS_GLOCK(); \
244 if (isGlockOwner) AFS_GUNLOCK(); \
245 msleep(cv, lck, PSOCK, "afs_rx_cv_timedwait", t); \
246 if (isGlockOwner) AFS_GLOCK(); \
248 #else /* !AFS_FBSD70_ENV */
249 #define CV_WAIT(cv, lck) { \
250 int isGlockOwner = ISAFS_GLOCK(); \
251 if (isGlockOwner) AFS_GUNLOCK(); \
253 tsleep(cv, PSOCK, "afs_rx_cv_wait", 0); \
254 if (isGlockOwner) AFS_GLOCK(); \
258 #define CV_TIMEDWAIT(cv,lck,t) { \
259 int isGlockOwner = ISAFS_GLOCK(); \
260 if (isGlockOwner) AFS_GUNLOCK(); \
262 tsleep(cv, PSOCK, "afs_rx_cv_timedwait", t); \
263 if (isGlockOwner) AFS_GLOCK(); \
266 #endif /* AFS_FBSD80_ENV */
268 #define CV_SIGNAL(cv) wakeup_one(cv)
269 #define CV_BROADCAST(cv) wakeup(cv)
271 /* #define osi_rxWakeup(cv) wakeup(cv) */
274 #endif /* _RX_KMUTEX_H_ */