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