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
30 typedef int afs_kcondvar_t;
33 #if defined(NULL_LOCKS)
38 #define MUTEX_INIT(a,b,c,d) \
42 #define MUTEX_DESTROY(a) \
44 (a)->owner = (struct proc *)-1; \
46 #define MUTEX_ENTER(a) \
48 osi_Assert((a)->owner == 0); \
49 (a)->owner = curproc; \
51 #define MUTEX_TRYENTER(a) \
52 ( osi_Assert((a)->owner == 0), (a)->owner = curproc, 1)
53 #define MUTEX_EXIT(a) \
55 osi_Assert((a)->owner == curproc); \
59 #define MUTEX_ASSERT(a) osi_Assert(((afs_kmutex_t *)(a))->owner == curproc)
61 #elif defined(AFS_FBSD70_ENV) /* dunno about 6.x */
63 typedef struct mtx afs_kmutex_t;
65 #if defined(AFS_FBSD80_ENV) && defined(WITNESS)
66 #define WITCLEAR_MTX(a) \
67 do { memset((a), 0, sizeof(struct mtx)); } while(0);
69 #define WITCLEAR_MTX(a) {}
72 #define MUTEX_INIT(a,b,c,d) \
75 mtx_init((a), (b), 0 /* type defaults to name */, MTX_DEF | MTX_DUPOK); \
78 #define MUTEX_DESTROY(a) \
83 #define MUTEX_ENTER(a) \
88 #define MUTEX_TRYENTER(a) \
91 #define MUTEX_EXIT(a) \
96 #define MUTEX_ASSERT(a) \
97 osi_Assert(mtx_owned((a)))
103 struct thread *owner;
107 #define MUTEX_INIT(a,b,c,d) \
109 lockinit(&(a)->lock,PSOCK, "afs rx mutex", 0, 0); \
112 #define MUTEX_DESTROY(a) \
114 (a)->owner = (struct proc *)-1; \
116 #define MUTEX_ENTER(a) \
118 lockmgr(&(a)->lock, LK_EXCLUSIVE, 0, curthread); \
119 osi_Assert((a)->owner == 0); \
120 (a)->owner = curthread; \
122 #define MUTEX_TRYENTER(a) \
123 ( lockmgr(&(a)->lock, LK_EXCLUSIVE|LK_NOWAIT, 0, curthread) ? 0 : ((a)->owner = curthread, 1) )
124 #define xMUTEX_TRYENTER(a) \
125 ( osi_Assert((a)->owner == 0), (a)->owner = curthread, 1)
126 #define MUTEX_EXIT(a) \
128 osi_Assert((a)->owner == curthread); \
130 lockmgr(&(a)->lock, LK_RELEASE, 0, curthread); \
133 #define MUTEX_ASSERT(a) osi_Assert(((afs_kmutex_t *)(a))->owner == curthread)
137 * Condition variables
139 * In Digital Unix (OSF/1), we use something akin to the ancient sleep/wakeup
140 * mechanism. The condition variable itself plays no role; we just use its
141 * address as a convenient unique number.
143 #define CV_INIT(cv,a,b,c)
144 #define CV_DESTROY(cv)
146 #if defined(AFS_FBSD70_ENV)
148 #define CV_WAIT(cv, lck) { \
149 int isGlockOwner = ISAFS_GLOCK(); \
150 if (isGlockOwner) AFS_GUNLOCK(); \
151 msleep(cv, lck, PSOCK, "afs_rx_cv_wait", 0); \
152 if (isGlockOwner) AFS_GLOCK(); \
155 #define CV_TIMEDWAIT(cv,lck,t) { \
156 int isGlockOwner = ISAFS_GLOCK(); \
157 if (isGlockOwner) AFS_GUNLOCK(); \
158 msleep(cv, lck, PSOCK, "afs_rx_cv_timedwait", t); \
159 if (isGlockOwner) AFS_GLOCK(); \
161 #else /* !AFS_FBSD70_ENV */
162 #define CV_WAIT(cv, lck) { \
163 int isGlockOwner = ISAFS_GLOCK(); \
164 if (isGlockOwner) AFS_GUNLOCK(); \
166 tsleep(cv, PSOCK, "afs_rx_cv_wait", 0); \
167 if (isGlockOwner) AFS_GLOCK(); \
171 #define CV_TIMEDWAIT(cv,lck,t) { \
172 int isGlockOwner = ISAFS_GLOCK(); \
173 if (isGlockOwner) AFS_GUNLOCK(); \
175 tsleep(cv, PSOCK, "afs_rx_cv_timedwait", t); \
176 if (isGlockOwner) AFS_GLOCK(); \
179 #endif /* AFS_FBSD80_ENV */
181 #define CV_SIGNAL(cv) wakeup_one(cv)
182 #define CV_BROADCAST(cv) wakeup(cv)
184 /* #define osi_rxWakeup(cv) wakeup(cv) */
187 #endif /* _RX_KMUTEX_H_ */