death to trailing whitespace
[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 #define AFS_GLOBAL_RXLOCK_KERNEL
30
31 typedef int afs_kcondvar_t;
32
33 #define HEAVY_LOCKS
34 #if defined(NULL_LOCKS)
35 typedef struct {
36     struct proc *owner;
37 } afs_kmutex_t;
38
39 #define MUTEX_INIT(a,b,c,d) \
40     do { \
41         (a)->owner = 0; \
42     } while(0);
43 #define MUTEX_DESTROY(a) \
44     do { \
45         (a)->owner = (struct proc *)-1; \
46     } while(0);
47 #define MUTEX_ENTER(a) \
48     do { \
49         osi_Assert((a)->owner == 0); \
50         (a)->owner = curproc; \
51     } while(0);
52 #define MUTEX_TRYENTER(a) \
53     ( osi_Assert((a)->owner == 0), (a)->owner = curproc, 1)
54 #define MUTEX_EXIT(a) \
55     do { \
56         osi_Assert((a)->owner == curproc); \
57         (a)->owner = 0; \
58     } while(0);
59
60 #undef MUTEX_ISMINE
61 #define MUTEX_ISMINE(a) (((afs_kmutex_t *)(a))->owner == curproc)
62
63 #elif defined(AFS_FBSD70_ENV) /* dunno about 6.x */
64
65 typedef struct mtx afs_kmutex_t;
66
67 #if defined(AFS_FBSD80_ENV) && defined(WITNESS)
68 #define WITCLEAR_MTX(a)                                 \
69     do { memset((a), 0, sizeof(struct mtx)); } while(0);
70 #else
71 #define WITCLEAR_MTX(a) {}
72 #endif
73
74 #define MUTEX_INIT(a,b,c,d)                                          \
75   do {                                                               \
76       WITCLEAR_MTX(a);                                               \
77       mtx_init((a), (b), 0 /* type defaults to name */, MTX_DEF | MTX_DUPOK);    \
78   } while(0);
79
80 #define MUTEX_DESTROY(a)                        \
81     do {                                        \
82         mtx_destroy((a));                       \
83     } while(0);
84
85 #define MUTEX_ENTER(a) \
86     do {               \
87         mtx_lock((a)); \
88     } while(0);
89
90 #define MUTEX_TRYENTER(a)                       \
91     ( mtx_trylock((a)) )
92
93 #define MUTEX_EXIT(a)    \
94     do {                 \
95         mtx_unlock((a)); \
96     } while(0);
97
98 #undef MUTEX_ISMINE
99 #define MUTEX_ISMINE(a)                         \
100     ( mtx_owned((a)) )
101
102 #else
103
104 typedef struct {
105     struct lock lock;
106     struct thread *owner;
107 } afs_kmutex_t;
108
109
110 #define MUTEX_INIT(a,b,c,d) \
111     do { \
112         lockinit(&(a)->lock,PSOCK, "afs rx mutex", 0, 0); \
113         (a)->owner = 0; \
114     } while(0);
115 #define MUTEX_DESTROY(a) \
116     do { \
117         (a)->owner = (struct proc *)-1; \
118     } while(0);
119 #define MUTEX_ENTER(a) \
120     do { \
121         lockmgr(&(a)->lock, LK_EXCLUSIVE, 0, curthread); \
122         osi_Assert((a)->owner == 0); \
123         (a)->owner = curthread; \
124     } while(0);
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) \
130     do { \
131         osi_Assert((a)->owner == curthread); \
132         (a)->owner = 0; \
133         lockmgr(&(a)->lock, LK_RELEASE, 0, curthread); \
134     } while(0);
135
136 #undef MUTEX_ISMINE
137 #define MUTEX_ISMINE(a) (((afs_kmutex_t *)(a))->owner == curthread)
138 #endif
139
140
141 #undef osirx_AssertMine
142 extern void osirx_AssertMine(afs_kmutex_t * lockaddr, char *msg);
143
144
145 /*
146  * Condition variables
147  *
148  * In Digital Unix (OSF/1), we use something akin to the ancient sleep/wakeup
149  * mechanism.  The condition variable itself plays no role; we just use its
150  * address as a convenient unique number.
151  */
152 #define CV_INIT(cv,a,b,c)
153 #define CV_DESTROY(cv)
154
155 #if defined(AFS_FBSD70_ENV)
156
157 #define CV_WAIT(cv, lck)    { \
158     int isGlockOwner = ISAFS_GLOCK();                                   \
159     if (isGlockOwner) AFS_GUNLOCK();                                    \
160     msleep(cv, lck, PSOCK, "afs_rx_cv_wait", 0);                        \
161     if (isGlockOwner) AFS_GLOCK();                                      \
162   }
163
164 #define CV_TIMEDWAIT(cv,lck,t)  { \
165                                 int isGlockOwner = ISAFS_GLOCK(); \
166                                 if (isGlockOwner) AFS_GUNLOCK();  \
167                                 msleep(cv, lck, PSOCK, "afs_rx_cv_timedwait", t); \
168                                 if (isGlockOwner) AFS_GLOCK();  \
169                                 }
170 #else /* !AFS_FBSD70_ENV */
171 #define CV_WAIT(cv, lck)    { \
172                                 int isGlockOwner = ISAFS_GLOCK(); \
173                                 if (isGlockOwner) AFS_GUNLOCK();  \
174                                 MUTEX_EXIT(lck);        \
175                                 tsleep(cv, PSOCK, "afs_rx_cv_wait", 0);  \
176                                 if (isGlockOwner) AFS_GLOCK();  \
177                                 MUTEX_ENTER(lck); \
178                             }
179
180 #define CV_TIMEDWAIT(cv,lck,t)  { \
181                                 int isGlockOwner = ISAFS_GLOCK(); \
182                                 if (isGlockOwner) AFS_GUNLOCK();  \
183                                 MUTEX_EXIT(lck);        \
184                                 tsleep(cv, PSOCK, "afs_rx_cv_timedwait", t); \
185                                 if (isGlockOwner) AFS_GLOCK();  \
186                                 MUTEX_ENTER(lck);       \
187                                 }
188 #endif /* AFS_FBSD80_ENV */
189
190 #define CV_SIGNAL(cv)           wakeup_one(cv)
191 #define CV_BROADCAST(cv)        wakeup(cv)
192
193 /* #define osi_rxWakeup(cv)        wakeup(cv) */
194
195
196 #endif /* _RX_KMUTEX_H_ */