Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / src / rx / FBSD / rx_kmutex.h
1 /*
2 * Copyright 2000, International Business Machines Corporation and others.
3 * All Rights Reserved.
4 *
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
8 */
9
10 /*
11 * rx_kmutex.h - mutex and condition variable macros for kernel environment.
12 *
13 * FBSD implementation.
14 */
15
16 #ifndef _RX_KMUTEX_H_
17 #define _RX_KMUTEX_H_
18
19 #include <sys/systm.h>
20 #include <sys/proc.h>
21 #ifdef AFS_FBSD70_ENV
22 #include <sys/lock.h>
23 #include <sys/lockmgr.h>
24 #else
25 #include <sys/lock.h>
26 #endif
27
28 #define RX_ENABLE_LOCKS 1
29
30 typedef int afs_kcondvar_t;
31
32 #define HEAVY_LOCKS
33 #if defined(NULL_LOCKS)
34 typedef struct {
35 struct proc *owner;
36 } afs_kmutex_t;
37
38 #define MUTEX_INIT(a,b,c,d) \
39 do { \
40 (a)->owner = 0; \
41 } while(0)
42 #define MUTEX_DESTROY(a) \
43 do { \
44 (a)->owner = (struct proc *)-1; \
45 } while(0)
46 #define MUTEX_ENTER(a) \
47 do { \
48 osi_Assert((a)->owner == 0); \
49 (a)->owner = curproc; \
50 } while(0)
51 #define MUTEX_TRYENTER(a) \
52 ( osi_Assert((a)->owner == 0), (a)->owner = curproc, 1)
53 #define MUTEX_EXIT(a) \
54 do { \
55 osi_Assert((a)->owner == curproc); \
56 (a)->owner = 0; \
57 } while(0)
58
59 #define MUTEX_ASSERT(a) osi_Assert(((afs_kmutex_t *)(a))->owner == curproc)
60
61 #elif defined(AFS_FBSD70_ENV) /* dunno about 6.x */
62
63 typedef struct mtx afs_kmutex_t;
64
65 #if defined(AFS_FBSD80_ENV) && defined(WITNESS)
66 #define WITCLEAR_MTX(a) \
67 do { memset((a), 0, sizeof(struct mtx)); } while(0)
68 #else
69 #define WITCLEAR_MTX(a) {}
70 #endif
71
72 #define MUTEX_INIT(a,b,c,d) \
73 do { \
74 WITCLEAR_MTX(a); \
75 mtx_init((a), (b), 0 /* type defaults to name */, MTX_DEF | MTX_DUPOK); \
76 } while(0)
77
78 #define MUTEX_DESTROY(a) \
79 do { \
80 mtx_destroy((a)); \
81 } while(0)
82
83 #define MUTEX_ENTER(a) \
84 do { \
85 mtx_lock((a)); \
86 } while(0)
87
88 #define MUTEX_TRYENTER(a) \
89 ( mtx_trylock((a)) )
90
91 #define MUTEX_EXIT(a) \
92 do { \
93 mtx_unlock((a)); \
94 } while(0)
95
96 #define MUTEX_ASSERT(a) \
97 osi_Assert(mtx_owned((a)))
98
99 #else
100
101 typedef struct {
102 struct lock lock;
103 struct thread *owner;
104 } afs_kmutex_t;
105
106
107 #define MUTEX_INIT(a,b,c,d) \
108 do { \
109 lockinit(&(a)->lock,PSOCK, "afs rx mutex", 0, 0); \
110 (a)->owner = 0; \
111 } while(0)
112 #define MUTEX_DESTROY(a) \
113 do { \
114 (a)->owner = (struct proc *)-1; \
115 } while(0)
116 #define MUTEX_ENTER(a) \
117 do { \
118 lockmgr(&(a)->lock, LK_EXCLUSIVE, 0, curthread); \
119 osi_Assert((a)->owner == 0); \
120 (a)->owner = curthread; \
121 } while(0)
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) \
127 do { \
128 osi_Assert((a)->owner == curthread); \
129 (a)->owner = 0; \
130 lockmgr(&(a)->lock, LK_RELEASE, 0, curthread); \
131 } while(0)
132
133 #define MUTEX_ASSERT(a) osi_Assert(((afs_kmutex_t *)(a))->owner == curthread)
134 #endif
135
136 /*
137 * Condition variables
138 *
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.
142 */
143 #define CV_INIT(cv,a,b,c)
144 #define CV_DESTROY(cv)
145
146 #if defined(AFS_FBSD70_ENV)
147
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(); \
153 }
154
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(); \
160 }
161 #else /* !AFS_FBSD70_ENV */
162 #define CV_WAIT(cv, lck) { \
163 int isGlockOwner = ISAFS_GLOCK(); \
164 if (isGlockOwner) AFS_GUNLOCK(); \
165 MUTEX_EXIT(lck); \
166 tsleep(cv, PSOCK, "afs_rx_cv_wait", 0); \
167 if (isGlockOwner) AFS_GLOCK(); \
168 MUTEX_ENTER(lck); \
169 }
170
171 #define CV_TIMEDWAIT(cv,lck,t) { \
172 int isGlockOwner = ISAFS_GLOCK(); \
173 if (isGlockOwner) AFS_GUNLOCK(); \
174 MUTEX_EXIT(lck); \
175 tsleep(cv, PSOCK, "afs_rx_cv_timedwait", t); \
176 if (isGlockOwner) AFS_GLOCK(); \
177 MUTEX_ENTER(lck); \
178 }
179 #endif /* AFS_FBSD80_ENV */
180
181 #define CV_SIGNAL(cv) wakeup_one(cv)
182 #define CV_BROADCAST(cv) wakeup(cv)
183
184 /* #define osi_rxWakeup(cv) wakeup(cv) */
185
186
187 #endif /* _RX_KMUTEX_H_ */