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