2 * Copyright 2000, International Business Machines Corporation and others.
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
10 #if !defined(lint) && !defined(LOCORE) && defined(RCS_HDRS)
12 /*******************************************************************\
14 * Information Technology Center *
15 * Carnegie-Mellon University *
19 \*******************************************************************/
22 Include file for using Vice locking routines.
28 /* The following macros allow multi statement macros to be defined safely, i.e.
29 - the multi statement macro can be the object of an if statement;
30 - the call to the multi statement macro may be legally followed by a semi-colon.
31 BEGINMAC and ENDMAC have been tested with both the portable C compiler and
32 Hi-C. Both compilers were from the Palo Alto 4.2BSD software releases, and
33 both optimized out the constant loop code. For an example of the use
34 of BEGINMAC and ENDMAC, see the definition for ReleaseWriteLock, below.
35 An alternative to this, using "if(1)" for BEGINMAC is not used because it
36 may generate worse code with pcc, and may generate warning messages with hi-C.
40 #define ENDMAC } while (0)
42 #ifdef AFS_PTHREAD_ENV
45 #define LOCK_LOCK(A) assert(pthread_mutex_lock(&(A)->mutex) == 0);
46 #define LOCK_UNLOCK(A) assert(pthread_mutex_unlock(&(A)->mutex) == 0);
47 #else /* AFS_PTHREAD_ENV */
49 #define LOCK_UNLOCK(A)
50 #endif /* AFS_PTHREAD_ENV */
52 /* all locks wait on excl_locked except for READ_LOCK, which waits on readers_reading */
54 unsigned char wait_states; /* type of lockers waiting */
55 unsigned char excl_locked; /* anyone have boosted, shared or write lock? */
56 unsigned char readers_reading; /* # readers actually with read locks */
57 unsigned char num_waiting; /* probably need this soon */
58 #ifdef AFS_PTHREAD_ENV
59 pthread_mutex_t mutex; /* protects this structure */
60 pthread_cond_t read_cv; /* wait for read locks */
61 pthread_cond_t write_cv; /* wait for write/shared locks */
62 #endif /* AFS_PTHREAD_ENV */
65 extern void Afs_Lock_Obtain(struct Lock * lock, int how);
66 extern void Afs_Lock_ReleaseR(struct Lock *lock);
67 extern void Afs_Lock_ReleaseW(struct Lock * lock);
68 void Lock_Init(struct Lock *lock);
69 void Lock_Destroy(struct Lock *lock);
74 /* this next is not a flag, but rather a parameter to Afs_Lock_Obtain */
75 #define BOOSTED_LOCK 6
77 /* next defines wait_states for which we wait on excl_locked */
78 #define EXCL_LOCKS (WRITE_LOCK|SHARED_LOCK)
80 #define ObtainReadLock(lock)\
83 if (!((lock)->excl_locked & WRITE_LOCK) && !(lock)->wait_states)\
84 (lock) -> readers_reading++;\
86 Afs_Lock_Obtain(lock, READ_LOCK); \
90 #define ObtainWriteLock(lock)\
93 if (!(lock)->excl_locked && !(lock)->readers_reading)\
94 (lock) -> excl_locked = WRITE_LOCK;\
96 Afs_Lock_Obtain(lock, WRITE_LOCK); \
100 #define ObtainSharedLock(lock)\
103 if (!(lock)->excl_locked && !(lock)->wait_states)\
104 (lock) -> excl_locked = SHARED_LOCK;\
106 Afs_Lock_Obtain(lock, SHARED_LOCK); \
110 #define BoostSharedLock(lock)\
113 if (!(lock)->readers_reading)\
114 (lock)->excl_locked = WRITE_LOCK;\
116 Afs_Lock_Obtain(lock, BOOSTED_LOCK); \
120 /* this must only be called with a WRITE or boosted SHARED lock! */
121 #define UnboostSharedLock(lock)\
124 (lock)->excl_locked = SHARED_LOCK; \
125 if((lock)->wait_states) \
126 Afs_Lock_ReleaseR(lock); \
131 /* this is what UnboostSharedLock looked like before the hi-C compiler */
132 /* this must only be called with a WRITE or boosted SHARED lock! */
133 #define UnboostSharedLock(lock)\
134 ((lock)->excl_locked = SHARED_LOCK,\
135 ((lock)->wait_states ?\
136 Afs_Lock_ReleaseR(lock) : 0))
139 #define ReleaseReadLock(lock)\
142 if (!--(lock)->readers_reading && (lock)->wait_states)\
143 Afs_Lock_ReleaseW(lock) ; \
149 /* This is what the previous definition should be, but the hi-C compiler generates
150 a warning for each invocation */
151 #define ReleaseReadLock(lock)\
152 (!--(lock)->readers_reading && (lock)->wait_states ?\
153 Afs_Lock_ReleaseW(lock) :\
157 #define ReleaseWriteLock(lock)\
160 (lock)->excl_locked &= ~WRITE_LOCK;\
161 if ((lock)->wait_states) Afs_Lock_ReleaseR(lock);\
166 /* This is what the previous definition should be, but the hi-C compiler generates
167 a warning for each invocation */
168 #define ReleaseWriteLock(lock)\
169 ((lock)->excl_locked &= ~WRITE_LOCK,\
170 ((lock)->wait_states ?\
171 Afs_Lock_ReleaseR(lock) : 0))
174 /* can be used on shared or boosted (write) locks */
175 #define ReleaseSharedLock(lock)\
178 (lock)->excl_locked &= ~(SHARED_LOCK | WRITE_LOCK);\
179 if ((lock)->wait_states) Afs_Lock_ReleaseR(lock);\
184 /* This is what the previous definition should be, but the hi-C compiler generates
185 a warning for each invocation */
186 /* can be used on shared or boosted (write) locks */
187 #define ReleaseSharedLock(lock)\
188 ((lock)->excl_locked &= ~(SHARED_LOCK | WRITE_LOCK),\
189 ((lock)->wait_states ?\
190 Afs_Lock_ReleaseR(lock) : 0))
193 /* convert a write lock to a read lock */
194 #define ConvertWriteToReadLock(lock)\
197 (lock)->excl_locked &= ~WRITE_LOCK;\
198 (lock)->readers_reading++;\
199 if ((lock)->wait_states & READ_LOCK) \
200 Afs_Lock_WakeupR(lock) ; \
204 /* I added this next macro to make sure it is safe to nuke a lock -- Mike K. */
205 #define LockWaiters(lock)\
206 ((int) ((lock)->num_waiting))
208 #define CheckLock(lock)\
209 ((lock)->excl_locked? (int) -1 : (int) (lock)->readers_reading)
211 #define WriteLocked(lock)\
212 ((lock)->excl_locked & WRITE_LOCK)