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 #define MUTEX_INIT(a,b,c,d) \
43 #define MUTEX_DESTROY(a) \
45 (a)->owner = (struct proc *)-1; \
47 #define MUTEX_ENTER(a) \
49 osi_Assert((a)->owner == 0); \
50 (a)->owner = curproc; \
52 #define MUTEX_TRYENTER(a) \
53 ( osi_Assert((a)->owner == 0), (a)->owner = curproc, 1)
54 #define MUTEX_EXIT(a) \
56 osi_Assert((a)->owner == curproc); \
61 #define MUTEX_ISMINE(a) (((afs_kmutex_t *)(a))->owner == curproc)
63 #elif defined(AFS_FBSD70_ENV) /* dunno about 6.x */
65 typedef struct mtx afs_kmutex_t;
67 #if defined(AFS_FBSD80_ENV) && defined(WITNESS)
68 #define WITCLEAR_MTX(a) \
69 do { memset((a), 0, sizeof(struct mtx)); } while(0);
71 #define WITCLEAR_MTX(a) {}
74 #define MUTEX_INIT(a,b,c,d) \
77 mtx_init((a), (b), 0 /* type defaults to name */, MTX_DEF | MTX_DUPOK); \
80 #define MUTEX_DESTROY(a) \
85 #define MUTEX_ENTER(a) \
90 #define MUTEX_TRYENTER(a) \
93 #define MUTEX_EXIT(a) \
99 #define MUTEX_ISMINE(a) \
102 #elif defined(AFS_FBSD50_ENV)
106 struct thread *owner;
110 #define MUTEX_INIT(a,b,c,d) \
112 lockinit(&(a)->lock,PSOCK, "afs rx mutex", 0, 0); \
115 #define MUTEX_DESTROY(a) \
117 (a)->owner = (struct proc *)-1; \
119 #define MUTEX_ENTER(a) \
121 lockmgr(&(a)->lock, LK_EXCLUSIVE, 0, curthread); \
122 osi_Assert((a)->owner == 0); \
123 (a)->owner = curthread; \
125 #define MUTEX_TRYENTER(a) \
126 ( lockmgr(&(a)->lock, LK_EXCLUSIVE|LK_NOWAIT, 0, curthread) ? 0 : ((a)->owner = curthread, 1) )
127 #define xMUTEX_TRYENTER(a) \
128 ( osi_Assert((a)->owner == 0), (a)->owner = curthread, 1)
129 #define MUTEX_EXIT(a) \
131 osi_Assert((a)->owner == curthread); \
133 lockmgr(&(a)->lock, LK_RELEASE, 0, curthread); \
137 #define MUTEX_ISMINE(a) (((afs_kmutex_t *)(a))->owner == curthread)
138 #elif defined(HEAVY_LOCKS)
145 #define MUTEX_INIT(a,b,c,d) \
147 lockinit(&(a)->lock, PSOCK, "afs rx mutex", 0, 0); \
150 #define MUTEX_DESTROY(a) \
152 (a)->owner = (struct proc *)-1; \
154 #define MUTEX_ENTER(a) \
156 lockmgr(&(a)->lock, LK_EXCLUSIVE, 0, curproc); \
157 osi_Assert((a)->owner == 0); \
158 (a)->owner = curproc; \
160 #define MUTEX_TRYENTER(a) \
161 ( lockmgr(&(a)->lock, LK_EXCLUSIVE|LK_NOWAIT, 0, curproc) ? 0 : ((a)->owner = curproc, 1) )
162 #define xMUTEX_TRYENTER(a) \
163 ( osi_Assert((a)->owner == 0), (a)->owner = curproc, 1)
164 #define MUTEX_EXIT(a) \
166 osi_Assert((a)->owner == curproc); \
168 lockmgr(&(a)->lock, LK_RELEASE, 0, curproc); \
172 #define MUTEX_ISMINE(a) (((afs_kmutex_t *)(a))->owner == curproc)
175 struct simplelock lock;
180 #define MUTEX_INIT(a,b,c,d) \
182 simple_lock_init(&(a)->lock); \
185 #define MUTEX_DESTROY(a) \
187 (a)->owner = (struct proc *)-1; \
189 #define MUTEX_ENTER(a) \
191 simple_lock(&(a)->lock); \
192 osi_Assert((a)->owner == 0); \
193 (a)->owner = curproc; \
195 #define MUTEX_TRYENTER(a) \
196 ( simple_lock_try(&(a)->lock) ? 0 : ((a)->owner = curproc, 1) )
197 #define MUTEX_EXIT(a) \
199 osi_Assert((a)->owner == curproc); \
201 simple_unlock(&(a)->lock); \
205 #define MUTEX_ISMINE(a) (((afs_kmutex_t *)(a))->owner == curproc)
209 #undef osirx_AssertMine
210 extern void osirx_AssertMine(afs_kmutex_t * lockaddr, char *msg);
214 * Condition variables
216 * In Digital Unix (OSF/1), we use something akin to the ancient sleep/wakeup
217 * mechanism. The condition variable itself plays no role; we just use its
218 * address as a convenient unique number.
220 #define CV_INIT(cv,a,b,c)
221 #define CV_DESTROY(cv)
223 #if defined(AFS_FBSD70_ENV)
225 #define CV_WAIT(cv, lck) { \
226 int isGlockOwner = ISAFS_GLOCK(); \
227 if (isGlockOwner) AFS_GUNLOCK(); \
228 msleep(cv, lck, PSOCK, "afs_rx_cv_wait", 0); \
229 if (isGlockOwner) AFS_GLOCK(); \
232 #define CV_TIMEDWAIT(cv,lck,t) { \
233 int isGlockOwner = ISAFS_GLOCK(); \
234 if (isGlockOwner) AFS_GUNLOCK(); \
235 msleep(cv, lck, PSOCK, "afs_rx_cv_timedwait", t); \
236 if (isGlockOwner) AFS_GLOCK(); \
238 #else /* !AFS_FBSD70_ENV */
239 #define CV_WAIT(cv, lck) { \
240 int isGlockOwner = ISAFS_GLOCK(); \
241 if (isGlockOwner) AFS_GUNLOCK(); \
243 tsleep(cv, PSOCK, "afs_rx_cv_wait", 0); \
244 if (isGlockOwner) AFS_GLOCK(); \
248 #define CV_TIMEDWAIT(cv,lck,t) { \
249 int isGlockOwner = ISAFS_GLOCK(); \
250 if (isGlockOwner) AFS_GUNLOCK(); \
252 tsleep(cv, PSOCK, "afs_rx_cv_timedwait", t); \
253 if (isGlockOwner) AFS_GLOCK(); \
256 #endif /* AFS_FBSD80_ENV */
258 #define CV_SIGNAL(cv) wakeup_one(cv)
259 #define CV_BROADCAST(cv) wakeup(cv)
261 /* #define osi_rxWakeup(cv) wakeup(cv) */
264 #endif /* _RX_KMUTEX_H_ */