459d82dedeb4924e92082eee00b64e610be3609d
[openafs.git] / src / rx / HPUX / 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 /* rx_kmutex.h - mutex and condition variable macros for kernel environment.
11  *
12  * HPUX implementation.
13  */
14
15 #ifndef _RX_KMUTEX_H_
16 #define _RX_KMUTEX_H_
17
18 #if defined(AFS_HPUX110_ENV) && defined(KERNEL)
19 /* rx fine grain locking primitives */
20
21 #include <sys/ksleep.h>
22 #include <sys/spinlock.h>
23 #include <sys/sem_beta.h>
24 #include <sys/errno.h>
25 #include <net/netmp.h>
26
27 #include "../rx/rx_kernel.h" /* For osi_Panic() */
28
29 #define RX_ENABLE_LOCKS         1
30 #define AFS_GLOBAL_RXLOCK_KERNEL
31
32 extern lock_t*  rx_sleepLock;
33
34 /* We use beta semaphores instead of sync semaphores for Rx locks as
35  * recommended by HP labs. Sync semaphores are not supported by HP
36  * any more.
37  */
38
39 #define CV_INIT(cv,a,b,c) 
40
41 /* This is supposed to atomically drop the mutex and go to sleep
42  * and reacquire the mutex when it wakes up.
43  */
44 #define CV_WAIT(cv, lck) \
45     do { \
46         int code; \
47         ksleep_prepare(); \
48         MP_SPINLOCK(rx_sleepLock); \
49         if (!b_owns_sema(lck)) \
50             osi_Panic("mutex not held \n"); \
51         b_vsema(lck); \
52         code = ksleep_one(PCATCH | KERNEL_ADDRESS | KERN_SPINLOCK_OBJECT, \
53             (cv), rx_sleepLock, 0); \
54         if (code) { \
55             if (code == EINTR) { /* lock still held */ \
56                 MP_SPINUNLOCK(rx_sleepLock); \
57             } else if (code != -EINTR) { \
58                 osi_Panic("ksleep_one failed: code = %d \n", code); \
59             } \
60         } \
61         b_psema(lck); /* grab the mutex again */ \
62     } while(0)
63
64 /* Wakes up a thread waiting on this condition */
65 #define CV_SIGNAL(cv) \
66     do { \
67         int wo, code; \
68         MP_SPINLOCK(rx_sleepLock); \
69         if ((code = kwakeup_one(KERNEL_ADDRESS, (cv), WAKEUP_ONE, &wo)) < 0) \
70             osi_Panic("kwakeup_one failed: code = %d \n", code); \
71         MP_SPINUNLOCK(rx_sleepLock); \
72     } while (0)
73
74 /* Wakes up all threads waiting on this condition */
75 #define CV_BROADCAST(cv) \
76     do { \
77         int wo, code; \
78         MP_SPINLOCK(rx_sleepLock); \
79         if ((code = kwakeup_one(KERNEL_ADDRESS, (cv), WAKEUP_ALL, &wo)) < 0) \
80             osi_Panic("kwakeup_all failed: code = %d \n", code); \
81         MP_SPINUNLOCK(rx_sleepLock); \
82     } while (0)
83
84 #define CV_DESTROY(a)
85
86 /* We now use beta semaphores for mutexes */
87 typedef b_sema_t afs_kmutex_t;
88 typedef caddr_t afs_kcondvar_t;
89
90 #else /* AFS_HPUX110_ENV */
91
92 #if defined(AFS_HPUX102_ENV)
93 #define CV_INIT(a,b,c,d)
94 #define CV_DESTROY(a)
95 #endif /* AFS_HPUX102_ENV */
96 #endif /* else AFS_HPUX110_ENV */
97
98 #ifdef AFS_HPUX102_ENV
99
100 #define RXObtainWriteLock(a) AFS_ASSERT_RXGLOCK()
101 #define RXReleaseWriteLock(a)
102
103
104 #if defined(AFS_HPUX110_ENV) 
105 #undef osirx_AssertMine
106 extern void osirx_AssertMine(afs_kmutex_t *lockaddr, char *msg);
107
108 #define AFS_RX_ORDER 30
109
110 #define MUTEX_INIT(a,b,c,d) b_initsema((a), 1, AFS_RX_ORDER, (b))
111 #define MUTEX_DESTROY(a)
112
113 #define MUTEX_TRYENTER(a) b_cpsema(a)
114
115 #ifdef AFS_HPUX1122_ENV
116 #define MUTEX_ENTER(a) \
117         ((b_owns_sema(a)) ? osi_Panic("Already Held") : b_psema(a))
118 #define MUTEX_EXIT(a) \
119         ((b_owns_sema(a)) ? b_vsema(a) : osi_Panic("mutex not held"))
120 #else
121 #define MUTEX_ENTER(a) \
122     ((b_owns_sema(a)) ? (osi_Panic("Already Held"), 0) : b_psema(a))
123
124 #define MUTEX_EXIT(a) \
125     ((b_owns_sema(a)) ? b_vsema(a) : (osi_Panic("mutex not held"), 0))
126 #endif
127
128 #undef MUTEX_ISMINE
129 #define MUTEX_ISMINE(a) b_owns_sema(a)
130
131 #else /* AFS_HPUX110_ENV */
132
133 #define osirx_AssertMine(addr, msg)
134
135 #define MUTEX_DESTROY(a)
136 #define MUTEX_ENTER(a)
137 #define MUTEX_TRYENTER(a) 1
138 #define MUTEX_EXIT(a)  
139 #define MUTEX_INIT(a,b,c,d) 
140
141 #endif /* else AFS_HPUX110_ENV */
142 #endif /* AFS_HPUX102_ENV */
143 #endif /* _RX_KMUTEX_H_ */
144