Import Upstream version 1.8.5
[hcoop/debian/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
31 extern lock_t *rx_sleepLock;
32
33 /* We use beta semaphores instead of sync semaphores for Rx locks as
34 * recommended by HP labs. Sync semaphores are not supported by HP
35 * any more.
36 */
37
38 #define CV_INIT(cv,a,b,c)
39
40 /* This is supposed to atomically drop the mutex and go to sleep
41 * and reacquire the mutex when it wakes up.
42 */
43
44 /* With 11.23, ksleep_prepare is not defined anywhere and
45 * ksleep_one is only referenced in a comment. sleep, get_sleep_lock
46 * and wakeup are defined in driver manuals.
47 * This works with 11.0, 11i, and 11.23
48 * Note: wakeup wakes up all threads waiting on cv.
49 */
50
51 #define CV_WAIT(cv, lck) \
52 do { \
53 get_sleep_lock((caddr_t)(cv)); \
54 if (!b_owns_sema(lck)) \
55 osi_Panic("CV_WAIT mutex not held \n"); \
56 b_vsema(lck); \
57 sleep((caddr_t)(cv), PRIBIO); \
58 b_psema(lck); \
59 } while(0)
60
61 #define CV_SIGNAL(cv) \
62 do { \
63 lock_t * sleep_lock = get_sleep_lock((caddr_t)(cv)); \
64 wakeup((caddr_t)(cv)); \
65 spinunlock(sleep_lock); \
66 } while(0)
67
68 #define CV_BROADCAST(cv) \
69 do { \
70 lock_t * sleep_lock = get_sleep_lock((caddr_t)(cv)); \
71 wakeup((caddr_t)(cv)); \
72 spinunlock(sleep_lock); \
73 } while(0)
74
75
76 #if 0
77 #define CV_WAIT(cv, lck) \
78 do { \
79 int code; \
80 ksleep_prepare(); \
81 MP_SPINLOCK(rx_sleepLock); \
82 if (!b_owns_sema(lck)) \
83 osi_Panic("mutex not held \n"); \
84 b_vsema(lck); \
85 code = ksleep_one(PCATCH | KERNEL_ADDRESS | KERN_SPINLOCK_OBJECT, \
86 (cv), rx_sleepLock, 0); \
87 if (code) { \
88 if (code == EINTR) { /* lock still held */ \
89 MP_SPINUNLOCK(rx_sleepLock); \
90 } else if (code != -EINTR) { \
91 osi_Panic("ksleep_one failed: code = %d \n", code); \
92 } \
93 } \
94 b_psema(lck); /* grab the mutex again */ \
95 } while(0)
96
97 /* Wakes up a thread waiting on this condition */
98 #define CV_SIGNAL(cv) \
99 do { \
100 int wo, code; \
101 MP_SPINLOCK(rx_sleepLock); \
102 if ((code = kwakeup_one(KERNEL_ADDRESS, (cv), WAKEUP_ONE, &wo)) < 0) \
103 osi_Panic("kwakeup_one failed: code = %d \n", code); \
104 MP_SPINUNLOCK(rx_sleepLock); \
105 } while (0)
106
107 /* Wakes up all threads waiting on this condition */
108 #define CV_BROADCAST(cv) \
109 do { \
110 int wo, code; \
111 MP_SPINLOCK(rx_sleepLock); \
112 if ((code = kwakeup_one(KERNEL_ADDRESS, (cv), WAKEUP_ALL, &wo)) < 0) \
113 osi_Panic("kwakeup_all failed: code = %d \n", code); \
114 MP_SPINUNLOCK(rx_sleepLock); \
115 } while (0)
116 #endif /* 0 */
117
118 #define CV_DESTROY(a)
119
120 /* We now use beta semaphores for mutexes */
121 typedef b_sema_t afs_kmutex_t;
122 typedef caddr_t afs_kcondvar_t;
123
124 #else /* AFS_HPUX110_ENV */
125
126 #if defined(AFS_HPUX102_ENV)
127 #define CV_INIT(a,b,c,d)
128 #define CV_DESTROY(a)
129 #endif /* AFS_HPUX102_ENV */
130 #endif /* else AFS_HPUX110_ENV */
131
132 #ifdef AFS_HPUX102_ENV
133
134 #if defined(AFS_HPUX110_ENV)
135
136 #define AFS_RX_ORDER 30
137
138 #define MUTEX_INIT(a,b,c,d) b_initsema((a), 1, AFS_RX_ORDER, (b))
139 #define MUTEX_DESTROY(a)
140
141 #define MUTEX_TRYENTER(a) b_cpsema(a)
142
143 #ifdef AFS_HPUX1111_ENV
144 #define MUTEX_ENTER(a) \
145 ((b_owns_sema(a)) ? osi_Panic("Already Held") : b_psema(a))
146 #define MUTEX_EXIT(a) \
147 ((b_owns_sema(a)) ? b_vsema(a) : osi_Panic("mutex not held"))
148 #else
149 #define MUTEX_ENTER(a) \
150 ((b_owns_sema(a)) ? (osi_Panic("Already Held"), 0) : b_psema(a))
151
152 #define MUTEX_EXIT(a) \
153 ((b_owns_sema(a)) ? b_vsema(a) : (osi_Panic("mutex not held"), 0))
154 #endif
155
156 #define MUTEX_ASSERT(a) osi_Assert(b_owns_sema(a))
157
158 #else /* AFS_HPUX110_ENV */
159
160 #define MUTEX_DESTROY(a)
161 #define MUTEX_ENTER(a)
162 #define MUTEX_TRYENTER(a) 1
163 #define MUTEX_EXIT(a)
164 #define MUTEX_INIT(a,b,c,d)
165 #define MUTEX_ASSERT(a)
166
167 #endif /* else AFS_HPUX110_ENV */
168 #endif /* AFS_HPUX102_ENV */
169 #endif /* _RX_KMUTEX_H_ */