Remove AFS_PARISC_LINUX24_ENV references
[openafs.git] / src / lwp / lock.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 *                                                                   *
12 *       Information Technology Center                               *
13 *       Carnegie-Mellon University                                  *
14 *                                                                   *
15 *                                                                   *
16 *                                                                   *
17 \*******************************************************************/
18
19 /*
20         Include file for using Vice locking routines.
21 */
22
23 #ifndef LOCK_H
24 #define LOCK_H
25
26 #ifdef KERNEL
27 #error Do not include lwp/lock.h for kernel code. Use afs/lock.h instead.
28 #endif
29
30
31 /* The following macros allow multi statement macros to be defined safely, i.e.
32    - the multi statement macro can be the object of an if statement;
33    - the call to the multi statement macro may be legally followed by a semi-colon.
34    BEGINMAC and ENDMAC have been tested with both the portable C compiler and
35    Hi-C.  Both compilers were from the Palo Alto 4.2BSD software releases, and
36    both optimized out the constant loop code.  For an example of the use
37    of BEGINMAC and ENDMAC, see the definition for ReleaseWriteLock, below.
38    An alternative to this, using "if(1)" for BEGINMAC is not used because it
39    may generate worse code with pcc, and may generate warning messages with hi-C.
40 */
41
42 #define BEGINMAC do {
43 #define ENDMAC   } while (0)
44
45 #ifdef AFS_PTHREAD_ENV
46 #include <afs/opr.h>
47 #include <opr/lock.h>
48 #define LOCK_LOCK(A) opr_mutex_enter(&(A)->mutex)
49 #define LOCK_UNLOCK(A) opr_mutex_exit(&(A)->mutex)
50 #else /* AFS_PTHREAD_ENV */
51 #define LOCK_LOCK(A)
52 #define LOCK_UNLOCK(A)
53 #endif /* AFS_PTHREAD_ENV */
54
55 /* all locks wait on excl_locked except for READ_LOCK, which waits on readers_reading */
56 struct Lock {
57     unsigned char wait_states;  /* type of lockers waiting */
58     unsigned char excl_locked;  /* anyone have boosted, shared or write lock? */
59     unsigned char readers_reading;      /* # readers actually with read locks */
60     unsigned char num_waiting;  /* probably need this soon */
61 #ifdef AFS_PTHREAD_ENV
62     pthread_mutex_t mutex;      /* protects this structure */
63     pthread_cond_t read_cv;     /* wait for read locks */
64     pthread_cond_t write_cv;    /* wait for write/shared locks */
65 #endif                          /* AFS_PTHREAD_ENV */
66 };
67
68 extern void Afs_Lock_Obtain(struct Lock *lock, int how);
69 extern void Afs_Lock_ReleaseR(struct Lock *lock);
70 extern void Afs_Lock_ReleaseW(struct Lock *lock);
71 extern void Afs_Lock_WakeupR(struct Lock *lock);
72 void Lock_Init(struct Lock *lock);
73 void Lock_Destroy(struct Lock *lock);
74
75 #define READ_LOCK       1
76 #define WRITE_LOCK      2
77 #define SHARED_LOCK     4
78 /* this next is not a flag, but rather a parameter to Afs_Lock_Obtain */
79 #define BOOSTED_LOCK 6
80
81 /* next defines wait_states for which we wait on excl_locked */
82 #define EXCL_LOCKS (WRITE_LOCK|SHARED_LOCK)
83
84 #define ObtainReadLock(lock)\
85         BEGINMAC \
86             LOCK_LOCK(lock); \
87             if (!((lock)->excl_locked & WRITE_LOCK) && !(lock)->wait_states)\
88                 (lock) -> readers_reading++;\
89             else\
90                 Afs_Lock_Obtain(lock, READ_LOCK); \
91             LOCK_UNLOCK(lock); \
92         ENDMAC
93
94 #define ObtainReadLockNoBlock(lock, code)\
95         BEGINMAC \
96             LOCK_LOCK(lock); \
97             if (!((lock)->excl_locked & WRITE_LOCK) && !(lock)->wait_states) {\
98                 (lock) -> readers_reading++;\
99                 code = 0;\
100             }\
101             else\
102                 code = -1; \
103             LOCK_UNLOCK(lock); \
104         ENDMAC
105
106 #define ObtainWriteLock(lock)\
107         BEGINMAC \
108             LOCK_LOCK(lock); \
109             if (!(lock)->excl_locked && !(lock)->readers_reading)\
110                 (lock) -> excl_locked = WRITE_LOCK;\
111             else\
112                 Afs_Lock_Obtain(lock, WRITE_LOCK); \
113             LOCK_UNLOCK(lock); \
114         ENDMAC
115
116 #define ObtainWriteLockNoBlock(lock, code)\
117         BEGINMAC \
118             LOCK_LOCK(lock); \
119             if (!(lock)->excl_locked && !(lock)->readers_reading) {\
120                 (lock) -> excl_locked = WRITE_LOCK;\
121                 code = 0;\
122             }\
123             else\
124                 code = -1; \
125             LOCK_UNLOCK(lock); \
126         ENDMAC
127
128 #define ObtainSharedLock(lock)\
129         BEGINMAC \
130             LOCK_LOCK(lock); \
131             if (!(lock)->excl_locked && !(lock)->wait_states)\
132                 (lock) -> excl_locked = SHARED_LOCK;\
133             else\
134                 Afs_Lock_Obtain(lock, SHARED_LOCK); \
135             LOCK_UNLOCK(lock); \
136         ENDMAC
137
138 #define ObtainSharedLockNoBlock(lock, code)\
139         BEGINMAC \
140             LOCK_LOCK(lock); \
141             if (!(lock)->excl_locked && !(lock)->wait_states) {\
142                 (lock) -> excl_locked = SHARED_LOCK;\
143                 code = 0;\
144             }\
145             else\
146                 code = -1; \
147             LOCK_UNLOCK(lock); \
148         ENDMAC
149
150 #define BoostSharedLock(lock)\
151         BEGINMAC \
152             LOCK_LOCK(lock); \
153             if (!(lock)->readers_reading)\
154                 (lock)->excl_locked = WRITE_LOCK;\
155             else\
156                 Afs_Lock_Obtain(lock, BOOSTED_LOCK); \
157             LOCK_UNLOCK(lock); \
158         ENDMAC
159
160 /* this must only be called with a WRITE or boosted SHARED lock! */
161 #define UnboostSharedLock(lock)\
162         BEGINMAC\
163             LOCK_LOCK(lock); \
164             (lock)->excl_locked = SHARED_LOCK; \
165             if((lock)->wait_states) \
166                 Afs_Lock_ReleaseR(lock); \
167             LOCK_UNLOCK(lock); \
168         ENDMAC
169
170 #define ReleaseReadLock(lock)\
171         BEGINMAC\
172             LOCK_LOCK(lock); \
173             if (!--(lock)->readers_reading && (lock)->wait_states)\
174                 Afs_Lock_ReleaseW(lock) ; \
175             LOCK_UNLOCK(lock); \
176         ENDMAC
177
178
179 #define ReleaseWriteLock(lock)\
180         BEGINMAC\
181             LOCK_LOCK(lock); \
182             (lock)->excl_locked &= ~WRITE_LOCK;\
183             if ((lock)->wait_states) Afs_Lock_ReleaseR(lock);\
184             LOCK_UNLOCK(lock); \
185         ENDMAC
186
187 /* can be used on shared or boosted (write) locks */
188 #define ReleaseSharedLock(lock)\
189         BEGINMAC\
190             LOCK_LOCK(lock); \
191             (lock)->excl_locked &= ~(SHARED_LOCK | WRITE_LOCK);\
192             if ((lock)->wait_states) Afs_Lock_ReleaseR(lock);\
193             LOCK_UNLOCK(lock); \
194         ENDMAC
195
196 /* convert a write lock to a read lock */
197 #define ConvertWriteToReadLock(lock)\
198         BEGINMAC\
199             LOCK_LOCK(lock); \
200             (lock)->excl_locked &= ~WRITE_LOCK;\
201             (lock)->readers_reading++;\
202             if ((lock)->wait_states & READ_LOCK) \
203                 Afs_Lock_WakeupR(lock) ; \
204             LOCK_UNLOCK(lock); \
205         ENDMAC
206
207 /* I added this next macro to make sure it is safe to nuke a lock -- Mike K. */
208 #define LockWaiters(lock)\
209         ((int) ((lock)->num_waiting))
210
211 #define CheckLock(lock)\
212         ((lock)->excl_locked? (int) -1 : (int) (lock)->readers_reading)
213
214 #define WriteLocked(lock)\
215         ((lock)->excl_locked & WRITE_LOCK)
216
217 #define SharedLocked(lock)\
218         ((lock)->excl_locked & SHARED_LOCK)
219
220 #endif /* LOCK_H */