Standardize License information
[openafs.git] / src / rx / LINUX / 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  * Linux implementation.
14  * This are noops until such time as the kernel no longer has a global lock.
15  */
16 #ifndef RX_KMUTEX_H_
17 #define RX_KMUTEX_H_
18
19 #include "../rx/rx_kernel.h"    /* for osi_Panic() */
20
21 /* AFS_GLOBAL_RXLOCK_KERNEL is defined so that the busy tq code paths are
22  * used. The thread can sleep when sending packets.
23  */
24 #define AFS_GLOBAL_RXLOCK_KERNEL 1
25
26
27 #ifdef CONFIG_SMP
28 #define RX_ENABLE_LOCKS 1
29
30 #ifndef _LINUX_CODA_FS_I
31 #define _LINUX_CODA_FS_I
32 struct coda_inode_info {};
33 #endif
34 #include "linux/wait.h"
35 #include "linux/sched.h"
36
37 typedef struct afs_kmutex {
38     struct semaphore sem;
39     int owner;
40 } afs_kmutex_t;
41
42 typedef struct wait_queue *afs_kcondvar_t;
43
44
45 static inline int MUTEX_ISMINE(afs_kmutex_t *l)
46 {
47     return l->owner == current->pid;
48 }
49
50
51 static inline void afs_mutex_init(afs_kmutex_t *l)
52 {
53     l->sem = MUTEX;
54     l->owner = 0;
55 }
56 #define MUTEX_INIT(a,b,c,d) afs_mutex_init(a)
57
58 #define MUTEX_DESTROY(a)
59
60 static inline void MUTEX_ENTER(afs_kmutex_t *l)
61 {
62     down(&l->sem);
63     if (l->owner)
64         osi_Panic("mutex_enter: 0x%x held by %d", l, l->owner);
65     l->owner = current->pid;
66 }
67                                                               
68 /* And how to do a good tryenter? */
69 static inline int MUTEX_TRYENTER(afs_kmutex_t *l)
70 {
71     if (!l->owner) {
72         MUTEX_ENTER(l);
73         return 1;
74     }
75     else
76         return 0;
77 }
78
79 static inline void MUTEX_EXIT(afs_kmutex_t *l)
80 {
81     if (l->owner != current->pid)
82         osi_Panic("mutex_exit: 0x%x held by %d",
83                   l, l->owner);
84     l->owner = 0;
85     up(&l->sem);
86 }
87
88 #define CV_INIT(cv,b,c,d) init_waitqueue((struct wait_queue**)(cv))
89 #define CV_DESTROY(cv)
90
91 /* CV_WAIT and CV_TIMEDWAIT rely on the fact that the Linux kernel has
92  * a global lock. Thus we can safely drop our locks before calling the
93  * kernel sleep services.
94  */
95 static inline CV_WAIT(afs_kcondvar_t *cv, afs_kmutex_t *l)
96 {
97     int isAFSGlocked = ISAFS_GLOCK(); 
98
99     if (isAFSGlocked) AFS_GUNLOCK();
100     MUTEX_EXIT(l);
101
102     interruptible_sleep_on((struct wait_queue**)cv);
103
104     MUTEX_ENTER(l);
105     if (isAFSGlocked) AFS_GLOCK();
106
107     return 0;
108 }
109
110 static inline CV_TIMEDWAIT(afs_kcondvar_t *cv, afs_kmutex_t *l, int waittime)
111 {
112     int isAFSGlocked = ISAFS_GLOCK();
113     long t = waittime * HZ / 1000;
114
115     if (isAFSGlocked) AFS_GUNLOCK();
116     MUTEX_EXIT(l);
117     
118     t = interruptible_sleep_on_timeout((struct wait_queue**)cv, t);
119     
120     MUTEX_ENTER(l);
121     if (isAFSGlocked) AFS_GLOCK();
122
123     return 0;
124 }
125
126 #define CV_SIGNAL(cv) wake_up((struct wait_queue**)cv)
127 #define CV_BROADCAST(cv) wake_up((struct wait_queue**)cv)
128
129 #else
130
131 #define MUTEX_ISMINE(a)
132 #define osirx_AssertMine(addr, msg)
133
134 #define MUTEX_DESTROY(a)
135 #define MUTEX_ENTER(a)
136 #define MUTEX_TRYENTER(a) 1
137 #define MUTEX_EXIT(a)  
138 #define MUTEX_INIT(a,b,c,d) 
139 #define CV_INIT(a,b,c,d)
140 #define CV_DESTROY(a)
141 #endif
142
143 /* Since we're using the RX listener daemon, we don't need to hold off
144  * interrupts.
145  */
146 #define SPLVAR
147 #define NETPRI
148 #define USERPRI
149
150 #endif /* RX_KMUTEX_H_ */