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 #ifndef __AFSLOCK_INCLUDE__
11 #define __AFSLOCK_INCLUDE__ 1
13 #if !defined(KERNEL) && !defined(KDUMP_KERNEL)
14 #error Do not include afs/lock.h except for kernel code.
18 * (C) COPYRIGHT IBM CORPORATION 1987
19 * LICENSED MATERIALS - PROPERTY OF IBM
22 #if (defined(AFS_SUN5_ENV)) || defined(AFS_OSF_ENV)
23 #define AFS_NOBOZO_LOCK
26 #if !defined(AFS_OSF20_ENV) || defined(AFS_OSF30_ENV) || defined(AFS_OSF32_ENV)
27 /* We do not instrument locks on osf20 because the vcache structure
28 ** exceeds the maximim possible limit for a vnode.
30 #define INSTRUMENT_LOCKS
31 /* This is the max lock number in use. Please update it if you add any new
34 #define MAX_LOCK_NUMBER 700
38 short count; /* count of excl locks */
39 char flags; /* bit 1: is anyone waiting? */
40 char spare; /* for later */
41 char *proc; /* process holding the lock, really a struct proc * */
43 #ifndef AFS_NOBOZO_LOCK
44 typedef struct afs_bozoLock afs_bozoLock_t;
47 typedef kmutex_t afs_bozoLock_t;
49 typedef struct afs_bozoLock afs_bozoLock_t;
51 #define afs_BozonLock(lock, avc)
52 #define afs_BozonUnlock(lock, avc)
53 #define afs_BozonInit(lock, nm)
54 #define afs_CheckBozonLock(lock) 0
55 #define afs_CheckBozonLockBlocking(lock) 0
58 #define AFS_BOZONWAITING 1 /* someone is waiting for this lock */
60 #undef MObtainWriteLock /* Defined also in ../rx/rx_machdep.h" */
61 #undef MReleaseWriteLock
62 #ifndef RXObtainWriteLock
63 #define RXObtainWriteLock(lock) ObtainWriteLock(lock)
64 #define RXReleaseWriteLock(lock) ReleaseWriteLock(lock)
66 #define MObtainReadLock(lock) ObtainReadLock(lock)
67 #define MObtainWriteLock(lock,src) ObtainWriteLock(lock,src)
68 #define MObtainSharedLock(lock,src) ObtainSharedLock(lock,src)
69 #define MUpgradeSToWLock(lock,src) UpgradeSToWLock(lock,src)
70 #define MConvertWToSLock(lock) ConvertWToSLock(lock)
71 #define MReleaseReadLock(lock) ReleaseReadLock(lock)
72 #define MReleaseWriteLock(lock) ReleaseWriteLock(lock)
73 #define MReleaseSharedLock(lock) ReleaseSharedLock(lock)
75 #define RWLOCK_INIT(lock, nm) Lock_Init(lock)
77 #define LOCK_INIT(lock, nm) Lock_Init(lock)
79 /* The following macros allow multi statement macros to be defined safely, i.e.
80 - the multi statement macro can be the object of an if statement;
81 - the call to the multi statement macro may be legally followed by a semi-colon.
82 BEGINMAC and ENDMAC have been tested with both the portable C compiler and
83 Hi-C. Both compilers were from the Palo Alto 4.2BSD software releases, and
84 both optimized out the constant loop code. For an example of the use
85 of BEGINMAC and ENDMAC, see the definition for ReleaseWriteLock, below.
86 An alternative to this, using "if(1)" for BEGINMAC is not used because it
87 may generate worse code with pcc, and may generate warning messages with hi-C.
91 #define ENDMAC } while (0)
93 #if defined(AFS_SUN5_ENV) || defined(AFS_OBSD_ENV)
94 #define MyPidxx (curproc->p_pid)
96 #if defined(AFS_AIX41_ENV)
97 extern tid_t thread_self();
98 #define MyPidxx thread_self()
99 #else /* AFS_AIX41_ENV */
100 #if defined(AFS_HPUX101_ENV)
101 #define MyPidxx ((int)p_pid(u.u_procp))
103 #if defined(AFS_SGI64_ENV)
104 #if defined(AFS_SGI65_ENV)
105 #define MyPidxx proc_pid(curproc())
107 #define MyPidxx current_pid()
109 #else /* AFS_SGI64_ENV */
110 #ifdef AFS_LINUX20_ENV
111 #define MyPidxx current->pid
113 #if defined(AFS_DARWIN_ENV)
114 #define MyPidxx (current_proc()->p_pid )
116 #if defined(AFS_FBSD_ENV)
117 #define MyPidxx (curproc->p_pid )
119 #define MyPidxx (u.u_procp->p_pid )
120 #endif /* AFS_FBSD_ENV */
121 #endif /* AFS_DARWIN_ENV */
122 #endif /* AFS_LINUX20_ENV */
123 #endif /* AFS_SGI64_ENV */
124 #endif /* AFS_HPUX101_ENV */
125 #endif /* AFS_AIX41_ENV */
128 /* all locks wait on excl_locked except for READ_LOCK, which waits on readers_reading */
130 unsigned char wait_states; /* type of lockers waiting */
131 unsigned char excl_locked; /* anyone have boosted, shared or write lock? */
132 unsigned short readers_reading;/* # readers actually with read locks */
133 unsigned short num_waiting; /* probably need this soon */
134 unsigned short spare; /* not used now */
135 osi_timeval_t time_waiting; /* for statistics gathering */
136 #if defined(INSTRUMENT_LOCKS)
137 /* the following are useful for debugging
138 ** the field 'src_indicator' is updated only by ObtainLock() and
139 ** only for writes/shared locks. Hence, it indictes where in the
140 ** source code the shared/write lock was set.
142 unsigned int pid_last_reader;/* proceess id of last reader */
143 unsigned int pid_writer; /* process id of writer, else 0 */
144 unsigned int src_indicator; /* third param to ObtainLock()*/
145 #endif /* INSTRUMENT_LOCKS */
147 typedef struct afs_lock afs_lock_t;
148 typedef struct afs_lock afs_rwlock_t;
152 #define SHARED_LOCK 4
153 /* this next is not a flag, but rather a parameter to Afs_Lock_Obtain */
154 #define BOOSTED_LOCK 6
156 /* next defines wait_states for which we wait on excl_locked */
157 #define EXCL_LOCKS (WRITE_LOCK|SHARED_LOCK)
162 extern int afs_trclock;
164 #define AFS_LOCK_TRACE_ENABLE 0
165 #if AFS_LOCK_TRACE_ENABLE
166 #define AFS_LOCK_TRACE(op, lock, type) \
167 if (afs_trclock) Afs_Lock_Trace(op, lock, type, __FILE__, __LINE__);
169 #define AFS_LOCK_TRACE(op, lock, type)
172 #if defined(INSTRUMENT_LOCKS)
174 #define ObtainReadLock(lock)\
176 AFS_LOCK_TRACE(CM_TRACE_LOCKOBTAIN, lock, READ_LOCK);\
177 if (!((lock)->excl_locked & WRITE_LOCK)) \
178 ((lock)->readers_reading)++; \
180 Afs_Lock_Obtain(lock, READ_LOCK); \
181 (lock)->pid_last_reader = MyPidxx; \
184 #define ObtainWriteLock(lock, src)\
186 AFS_LOCK_TRACE(CM_TRACE_LOCKOBTAIN, lock, WRITE_LOCK);\
187 if (!(lock)->excl_locked && !(lock)->readers_reading)\
188 (lock) -> excl_locked = WRITE_LOCK;\
190 Afs_Lock_Obtain(lock, WRITE_LOCK); \
191 (lock)->pid_writer = MyPidxx; \
192 (lock)->src_indicator = src;\
195 #define NBObtainWriteLock(lock, src) (((lock)->excl_locked || (lock)->readers_reading) ? EWOULDBLOCK : (((lock) -> excl_locked = WRITE_LOCK), ((lock)->pid_writer = MyPidxx), ((lock)->src_indicator = src), 0))
197 #define ObtainSharedLock(lock, src)\
199 AFS_LOCK_TRACE(CM_TRACE_LOCKOBTAIN, lock, SHARED_LOCK);\
200 if (!(lock)->excl_locked)\
201 (lock) -> excl_locked = SHARED_LOCK;\
203 Afs_Lock_Obtain(lock, SHARED_LOCK); \
204 (lock)->pid_writer = MyPidxx; \
205 (lock)->src_indicator = src;\
208 #define NBObtainSharedLock(lock, src) (((lock)->excl_locked) ? EWOULDBLOCK : (((lock) -> excl_locked = SHARED_LOCK), ((lock)->pid_writer = MyPidxx), ((lock)->src_indicator = src), 0))
210 #define UpgradeSToWLock(lock, src)\
212 AFS_LOCK_TRACE(CM_TRACE_LOCKOBTAIN, lock, BOOSTED_LOCK);\
213 if (!(lock)->readers_reading)\
214 (lock)->excl_locked = WRITE_LOCK;\
216 Afs_Lock_Obtain(lock, BOOSTED_LOCK); \
217 (lock)->pid_writer = MyPidxx; \
218 (lock)->src_indicator = src;\
221 /* this must only be called with a WRITE or boosted SHARED lock! */
222 #define ConvertWToSLock(lock)\
224 AFS_LOCK_TRACE(CM_TRACE_LOCKDOWN, lock, SHARED_LOCK);\
225 (lock)->excl_locked = SHARED_LOCK; \
226 if((lock)->wait_states) \
227 Afs_Lock_ReleaseR(lock); \
230 #define ConvertWToRLock(lock) \
232 AFS_LOCK_TRACE(CM_TRACE_LOCKDOWN, lock, READ_LOCK);\
233 (lock)->excl_locked &= ~(SHARED_LOCK | WRITE_LOCK);\
234 ((lock)->readers_reading)++;\
235 (lock)->pid_last_reader = MyPidxx ; \
236 (lock)->pid_writer = 0;\
237 Afs_Lock_ReleaseR(lock);\
240 #define ConvertSToRLock(lock) \
242 AFS_LOCK_TRACE(CM_TRACE_LOCKDOWN, lock, READ_LOCK);\
243 (lock)->excl_locked &= ~(SHARED_LOCK | WRITE_LOCK);\
244 ((lock)->readers_reading)++;\
245 (lock)->pid_last_reader = MyPidxx ; \
246 (lock)->pid_writer = 0;\
247 Afs_Lock_ReleaseR(lock);\
250 #define ReleaseReadLock(lock)\
252 AFS_LOCK_TRACE(CM_TRACE_LOCKDONE, lock, READ_LOCK);\
253 if (!(--((lock)->readers_reading)) && (lock)->wait_states)\
254 Afs_Lock_ReleaseW(lock) ; \
255 if ( (lock)->pid_last_reader == MyPidxx ) \
256 (lock)->pid_last_reader =0;\
259 #define ReleaseWriteLock(lock)\
261 AFS_LOCK_TRACE(CM_TRACE_LOCKDONE, lock, WRITE_LOCK);\
262 (lock)->excl_locked &= ~WRITE_LOCK;\
263 if ((lock)->wait_states) Afs_Lock_ReleaseR(lock);\
264 (lock)->pid_writer=0; \
267 /* can be used on shared or boosted (write) locks */
268 #define ReleaseSharedLock(lock)\
270 AFS_LOCK_TRACE(CM_TRACE_LOCKDONE, lock, SHARED_LOCK);\
271 (lock)->excl_locked &= ~(SHARED_LOCK | WRITE_LOCK);\
272 if ((lock)->wait_states) Afs_Lock_ReleaseR(lock);\
273 (lock)->pid_writer=0; \
276 #else /* INSTRUMENT_LOCKS */
278 #define ObtainReadLock(lock)\
280 AFS_LOCK_TRACE(CM_TRACE_LOCKOBTAIN, lock, READ_LOCK);\
281 if (!((lock)->excl_locked & WRITE_LOCK)) \
282 ((lock)->readers_reading)++; \
284 Afs_Lock_Obtain(lock, READ_LOCK); \
287 #define ObtainWriteLock(lock, src)\
289 AFS_LOCK_TRACE(CM_TRACE_LOCKOBTAIN, lock, WRITE_LOCK);\
290 if (!(lock)->excl_locked && !(lock)->readers_reading)\
291 (lock) -> excl_locked = WRITE_LOCK;\
293 Afs_Lock_Obtain(lock, WRITE_LOCK); \
296 #define NBObtainWriteLock(lock, src) (((lock)->excl_locked || (lock)->readers_reading) ? EWOULDBLOCK : (((lock) -> excl_locked = WRITE_LOCK), 0))
298 #define ObtainSharedLock(lock, src)\
300 AFS_LOCK_TRACE(CM_TRACE_LOCKOBTAIN, lock, SHARED_LOCK);\
301 if (!(lock)->excl_locked)\
302 (lock) -> excl_locked = SHARED_LOCK;\
304 Afs_Lock_Obtain(lock, SHARED_LOCK); \
307 #define NBObtainSharedLock(lock, src) (((lock)->excl_locked) ? EWOULDBLOCK : (((lock) -> excl_locked = SHARED_LOCK), 0))
309 #define UpgradeSToWLock(lock, src)\
311 AFS_LOCK_TRACE(CM_TRACE_LOCKOBTAIN, lock, BOOSTED_LOCK);\
312 if (!(lock)->readers_reading)\
313 (lock)->excl_locked = WRITE_LOCK;\
315 Afs_Lock_Obtain(lock, BOOSTED_LOCK); \
318 /* this must only be called with a WRITE or boosted SHARED lock! */
319 #define ConvertWToSLock(lock)\
321 AFS_LOCK_TRACE(CM_TRACE_LOCKDOWN, lock, SHARED_LOCK);\
322 (lock)->excl_locked = SHARED_LOCK; \
323 if((lock)->wait_states) \
324 Afs_Lock_ReleaseR(lock); \
327 #define ConvertWToRLock(lock) \
329 AFS_LOCK_TRACE(CM_TRACE_LOCKDOWN, lock, READ_LOCK);\
330 (lock)->excl_locked &= ~(SHARED_LOCK | WRITE_LOCK);\
331 ((lock)->readers_reading)++;\
332 Afs_Lock_ReleaseR(lock);\
335 #define ConvertSToRLock(lock) \
337 AFS_LOCK_TRACE(CM_TRACE_LOCKDOWN, lock, READ_LOCK);\
338 (lock)->excl_locked &= ~(SHARED_LOCK | WRITE_LOCK);\
339 ((lock)->readers_reading)++;\
340 Afs_Lock_ReleaseR(lock);\
343 #define ReleaseReadLock(lock)\
345 AFS_LOCK_TRACE(CM_TRACE_LOCKDONE, lock, READ_LOCK);\
346 if (!(--((lock)->readers_reading)) && (lock)->wait_states)\
347 Afs_Lock_ReleaseW(lock) ; \
350 #define ReleaseWriteLock(lock)\
352 AFS_LOCK_TRACE(CM_TRACE_LOCKDONE, lock, WRITE_LOCK);\
353 (lock)->excl_locked &= ~WRITE_LOCK;\
354 if ((lock)->wait_states) Afs_Lock_ReleaseR(lock);\
357 /* can be used on shared or boosted (write) locks */
358 #define ReleaseSharedLock(lock)\
360 AFS_LOCK_TRACE(CM_TRACE_LOCKDONE, lock, SHARED_LOCK);\
361 (lock)->excl_locked &= ~(SHARED_LOCK | WRITE_LOCK);\
362 if ((lock)->wait_states) Afs_Lock_ReleaseR(lock);\
365 #endif /* INSTRUMENT_LOCKS */
367 /* I added this next macro to make sure it is safe to nuke a lock -- Mike K. */
368 #define LockWaiters(lock)\
369 ((int) ((lock)->num_waiting))
371 #define CheckLock(lock)\
372 ((lock)->excl_locked? (int) -1 : (int) (lock)->readers_reading)
374 #define WriteLocked(lock)\
375 ((lock)->excl_locked & WRITE_LOCK)
380 You can also use the lock package for handling parent locks for independently-lockable sets of
381 small objects. The concept here is that the parent lock is at the same level in the
382 locking hierarchy as the little locks, but certain restrictions apply.
384 The general usage pattern is as follows. You have a set of entries to search. When searching it, you
385 have a "scan" lock on the table. If you find what you're looking for, you drop the lock down
386 to a "hold" lock, lock the entry, and release the parent lock. If you don't find what
387 you're looking for, you create the entry, downgrade the "scan" lock to a "hold" lock,
388 lock the entry and unlock the parent.
390 To delete an item from the table, you initially obtain a "purge" lock on the parent. Unlike all
391 of the other parent lock modes described herein, in order to obtain a "purge" lock mode, you
392 must have released all locks on any items in the table. Once you have obtained the parent
393 lock in "purge" mode, you should check to see if the entry is locked. If its not locked, you
394 are free to delete the entry, knowing that no one else can attempt to obtain a lock
395 on the entry while you have the purge lock held on the parent. Unfortunately, if it *is* locked,
396 you can not lock it yourself and wait for the other dude to release it, since the entry's locker
397 may need to lock another entry before unlocking the entry you want (which would result in
398 deadlock). Instead, then, you must release the parent lock, and try again "later" (see Lock_Wait
399 for assistance in waiting until later). Unfortunately, this is the best locking paradigm I've yet
402 What are the advantages to this scheme? First, the use of the parent lock ensures that
403 two people don't try to add the same entry at the same time or delete an entry while someone
404 else is adding it. It also ensures that when one process is deleting an entry, no one else is
405 preparing to lock the entry. Furthermore, when obtaining a lock on a little entry, you
406 are only holding a "hold" lock on the parent lock, so that others may come in and search
407 the table during this time. Thus it will not hold up the system if a little entry takes
408 a great deal of time to free up.
410 Here's how to compute the compatibility matrix:
414 add no deletions, additions allowed, additions will be performed, will obtain little locks
415 hold no deletions, additions allowed, no additions will be performed, will obtain little locks
416 purge no deletions or additions allowed, deletions will be performed, don't obtain little locks
418 When we compute the locking matrix, we note that hold is compatible with hold and add.
419 Add is compatible only with hold. purge is not compatible with anything. This is the same
420 matrix as obtained by mapping add->S, hold->read and purge->write locks. Thus we
421 can use the locks above to solve this problem, and we do.
425 #endif /* __AFSLOCK_INCLUDE__ */