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 760
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 #define MObtainReadLock(lock) ObtainReadLock(lock)
63 #define MObtainWriteLock(lock,src) ObtainWriteLock(lock,src)
64 #define MObtainSharedLock(lock,src) ObtainSharedLock(lock,src)
65 #define MUpgradeSToWLock(lock,src) UpgradeSToWLock(lock,src)
66 #define MConvertWToSLock(lock) ConvertWToSLock(lock)
67 #define MReleaseReadLock(lock) ReleaseReadLock(lock)
68 #define MReleaseWriteLock(lock) ReleaseWriteLock(lock)
69 #define MReleaseSharedLock(lock) ReleaseSharedLock(lock)
71 #define AFS_RWLOCK_INIT(lock, nm) Lock_Init(lock)
73 #define LOCK_INIT(lock, nm) Lock_Init(lock)
75 /* The following macros allow multi statement macros to be defined safely, i.e.
76 - the multi statement macro can be the object of an if statement;
77 - the call to the multi statement macro may be legally followed by a semi-colon.
78 BEGINMAC and ENDMAC have been tested with both the portable C compiler and
79 Hi-C. Both compilers were from the Palo Alto 4.2BSD software releases, and
80 both optimized out the constant loop code. For an example of the use
81 of BEGINMAC and ENDMAC, see the definition for ReleaseWriteLock, below.
82 An alternative to this, using "if(1)" for BEGINMAC is not used because it
83 may generate worse code with pcc, and may generate warning messages with hi-C.
87 #define ENDMAC } while (0)
89 #if defined(AFS_SUN57_ENV)
90 typedef kthread_t * afs_lock_tracker_t;
91 #define MyPidxx (curthread)
92 #define MyPidxx2Pid(x) (x ? ttoproc(x)->p_pid : 0)
93 #elif defined(AFS_SUN5_ENV) || defined(AFS_OBSD_ENV)
94 typedef unsigned int afs_lock_tracker_t;
95 #define MyPidxx (curproc->p_pid)
96 #define MyPidxx2Pid(x) (x)
98 #if defined(AFS_AIX41_ENV)
99 typedef tid_t afs_lock_tracker_t;
100 extern tid_t thread_self();
101 #define MyPidxx (thread_self())
102 #define MyPidxx2Pid(x) ((afs_int32)(x))
103 #else /* AFS_AIX41_ENV */
104 #if defined(AFS_HPUX101_ENV)
105 #if defined(AFS_HPUX1111_ENV)
106 typedef struct kthread * afs_lock_tracker_t;
107 #define MyPidxx (u.u_kthreadp)
108 #define MyPidxx2Pid(x) (x ? kt_tid(x) : 0)
110 typedef struct proc * afs_lock_tracker_t;
111 #define MyPidxx (u.u_procp)
112 #define MyPidxx2Pid(x) (x ? (afs_int32)p_pid(x) : 0)
115 #if defined(AFS_SGI64_ENV)
116 #if defined(AFS_SGI65_ENV)
117 typedef unsigned int afs_lock_tracker_t;
118 #define MyPidxx proc_pid(curproc())
119 #define MyPidxx2Pid(x) (x)
121 typedef unsigned int afs_lock_tracker_t;
122 #define MyPidxx current_pid()
123 #define MyPidxx2Pid(x) (x)
125 #else /* AFS_SGI64_ENV */
126 #ifdef AFS_LINUX20_ENV
127 typedef struct task_struct * afs_lock_tracker_t;
128 #define MyPidxx (current)
129 #define MyPidxx2Pid(x) (x? (x)->pid : 0)
131 #if defined(AFS_DARWIN_ENV)
132 #if defined(AFS_DARWIN80_ENV)
133 typedef unsigned int afs_lock_tracker_t;
134 #define MyPidxx (proc_selfpid())
135 #define MyPidxx2Pid(x) (x)
137 typedef unsigned int afs_lock_tracker_t;
138 #define MyPidxx (current_proc()->p_pid )
139 #define MyPidxx2Pid(x) (x)
142 #if defined(AFS_FBSD_ENV)
143 typedef unsigned int afs_lock_tracker_t;
144 #define MyPidxx (curproc->p_pid )
145 #define MyPidxx2Pid(x) (x)
147 typedef unsigned int afs_lock_tracker_t;
148 #define MyPidxx (u.u_procp->p_pid )
149 #define MyPidxx2Pid(x) (x)
150 #endif /* AFS_FBSD_ENV */
151 #endif /* AFS_DARWIN_ENV */
152 #endif /* AFS_LINUX20_ENV */
153 #endif /* AFS_SGI64_ENV */
154 #endif /* AFS_HPUX101_ENV */
155 #endif /* AFS_AIX41_ENV */
158 /* all locks wait on excl_locked except for READ_LOCK, which waits on readers_reading */
160 unsigned char wait_states; /* type of lockers waiting */
161 unsigned char excl_locked; /* anyone have boosted, shared or write lock? */
162 unsigned short readers_reading; /* # readers actually with read locks */
163 unsigned short num_waiting; /* probably need this soon */
164 unsigned short spare; /* not used now */
165 osi_timeval_t time_waiting; /* for statistics gathering */
166 #if defined(INSTRUMENT_LOCKS)
167 /* the following are useful for debugging
168 ** the field 'src_indicator' is updated only by ObtainLock() and
169 ** only for writes/shared locks. Hence, it indictes where in the
170 ** source code the shared/write lock was set.
172 afs_lock_tracker_t pid_last_reader; /* proceess id of last reader */
173 afs_lock_tracker_t pid_writer; /* process id of writer, else 0 */
174 unsigned int src_indicator; /* third param to ObtainLock() */
175 #endif /* INSTRUMENT_LOCKS */
177 typedef struct afs_lock afs_lock_t;
178 typedef struct afs_lock afs_rwlock_t;
182 #define SHARED_LOCK 4
183 /* this next is not a flag, but rather a parameter to Afs_Lock_Obtain */
184 #define BOOSTED_LOCK 6
186 /* next defines wait_states for which we wait on excl_locked */
187 #define EXCL_LOCKS (WRITE_LOCK|SHARED_LOCK)
192 extern int afs_trclock;
194 #define AFS_LOCK_TRACE_ENABLE 0
195 #if AFS_LOCK_TRACE_ENABLE
196 #define AFS_LOCK_TRACE(op, lock, type) \
197 if (afs_trclock) Afs_Lock_Trace(op, lock, type, __FILE__, __LINE__);
199 #define AFS_LOCK_TRACE(op, lock, type)
202 #if defined(INSTRUMENT_LOCKS)
204 #define ObtainReadLock(lock)\
206 AFS_LOCK_TRACE(CM_TRACE_LOCKOBTAIN, lock, READ_LOCK);\
207 if (!((lock)->excl_locked & WRITE_LOCK)) \
208 ((lock)->readers_reading)++; \
210 Afs_Lock_Obtain(lock, READ_LOCK); \
211 (lock)->pid_last_reader = MyPidxx; \
214 #define ObtainWriteLock(lock, src)\
216 AFS_LOCK_TRACE(CM_TRACE_LOCKOBTAIN, lock, WRITE_LOCK);\
217 if (!(lock)->excl_locked && !(lock)->readers_reading)\
218 (lock) -> excl_locked = WRITE_LOCK;\
220 Afs_Lock_Obtain(lock, WRITE_LOCK); \
221 (lock)->pid_writer = MyPidxx; \
222 (lock)->src_indicator = src;\
225 #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))
227 #define ObtainSharedLock(lock, src)\
229 AFS_LOCK_TRACE(CM_TRACE_LOCKOBTAIN, lock, SHARED_LOCK);\
230 if (!(lock)->excl_locked)\
231 (lock) -> excl_locked = SHARED_LOCK;\
233 Afs_Lock_Obtain(lock, SHARED_LOCK); \
234 (lock)->pid_writer = MyPidxx; \
235 (lock)->src_indicator = src;\
238 #define NBObtainSharedLock(lock, src) (((lock)->excl_locked) ? EWOULDBLOCK : (((lock) -> excl_locked = SHARED_LOCK), ((lock)->pid_writer = MyPidxx), ((lock)->src_indicator = src), 0))
240 #define UpgradeSToWLock(lock, src)\
242 AFS_LOCK_TRACE(CM_TRACE_LOCKOBTAIN, lock, BOOSTED_LOCK);\
243 if (!(lock)->readers_reading)\
244 (lock)->excl_locked = WRITE_LOCK;\
246 Afs_Lock_Obtain(lock, BOOSTED_LOCK); \
247 (lock)->pid_writer = MyPidxx; \
248 (lock)->src_indicator = src;\
251 /* this must only be called with a WRITE or boosted SHARED lock! */
252 #define ConvertWToSLock(lock)\
254 AFS_LOCK_TRACE(CM_TRACE_LOCKDOWN, lock, SHARED_LOCK);\
255 (lock)->excl_locked = SHARED_LOCK; \
256 if((lock)->wait_states) \
257 Afs_Lock_ReleaseR(lock); \
260 #define ConvertWToRLock(lock) \
262 AFS_LOCK_TRACE(CM_TRACE_LOCKDOWN, lock, READ_LOCK);\
263 (lock)->excl_locked &= ~(SHARED_LOCK | WRITE_LOCK);\
264 ((lock)->readers_reading)++;\
265 (lock)->pid_last_reader = MyPidxx ; \
266 (lock)->pid_writer = 0;\
267 Afs_Lock_ReleaseR(lock);\
270 #define ConvertSToRLock(lock) \
272 AFS_LOCK_TRACE(CM_TRACE_LOCKDOWN, lock, READ_LOCK);\
273 (lock)->excl_locked &= ~(SHARED_LOCK | WRITE_LOCK);\
274 ((lock)->readers_reading)++;\
275 (lock)->pid_last_reader = MyPidxx ; \
276 (lock)->pid_writer = 0;\
277 Afs_Lock_ReleaseR(lock);\
280 #define ReleaseReadLock(lock)\
282 AFS_LOCK_TRACE(CM_TRACE_LOCKDONE, lock, READ_LOCK);\
283 if (!(--((lock)->readers_reading)) && (lock)->wait_states)\
284 Afs_Lock_ReleaseW(lock) ; \
285 if ( (lock)->pid_last_reader == MyPidxx ) \
286 (lock)->pid_last_reader =0;\
289 #define ReleaseWriteLock(lock)\
291 AFS_LOCK_TRACE(CM_TRACE_LOCKDONE, lock, WRITE_LOCK);\
292 (lock)->excl_locked &= ~WRITE_LOCK;\
293 if ((lock)->wait_states) Afs_Lock_ReleaseR(lock);\
294 (lock)->pid_writer=0; \
297 /* can be used on shared or boosted (write) locks */
298 #define ReleaseSharedLock(lock)\
300 AFS_LOCK_TRACE(CM_TRACE_LOCKDONE, lock, SHARED_LOCK);\
301 (lock)->excl_locked &= ~(SHARED_LOCK | WRITE_LOCK);\
302 if ((lock)->wait_states) Afs_Lock_ReleaseR(lock);\
303 (lock)->pid_writer=0; \
306 #else /* INSTRUMENT_LOCKS */
308 #define ObtainReadLock(lock)\
310 AFS_LOCK_TRACE(CM_TRACE_LOCKOBTAIN, lock, READ_LOCK);\
311 if (!((lock)->excl_locked & WRITE_LOCK)) \
312 ((lock)->readers_reading)++; \
314 Afs_Lock_Obtain(lock, READ_LOCK); \
317 #define ObtainWriteLock(lock, src)\
319 AFS_LOCK_TRACE(CM_TRACE_LOCKOBTAIN, lock, WRITE_LOCK);\
320 if (!(lock)->excl_locked && !(lock)->readers_reading)\
321 (lock) -> excl_locked = WRITE_LOCK;\
323 Afs_Lock_Obtain(lock, WRITE_LOCK); \
326 #define NBObtainWriteLock(lock, src) (((lock)->excl_locked || (lock)->readers_reading) ? EWOULDBLOCK : (((lock) -> excl_locked = WRITE_LOCK), 0))
328 #define ObtainSharedLock(lock, src)\
330 AFS_LOCK_TRACE(CM_TRACE_LOCKOBTAIN, lock, SHARED_LOCK);\
331 if (!(lock)->excl_locked)\
332 (lock) -> excl_locked = SHARED_LOCK;\
334 Afs_Lock_Obtain(lock, SHARED_LOCK); \
337 #define NBObtainSharedLock(lock, src) (((lock)->excl_locked) ? EWOULDBLOCK : (((lock) -> excl_locked = SHARED_LOCK), 0))
339 #define UpgradeSToWLock(lock, src)\
341 AFS_LOCK_TRACE(CM_TRACE_LOCKOBTAIN, lock, BOOSTED_LOCK);\
342 if (!(lock)->readers_reading)\
343 (lock)->excl_locked = WRITE_LOCK;\
345 Afs_Lock_Obtain(lock, BOOSTED_LOCK); \
348 /* this must only be called with a WRITE or boosted SHARED lock! */
349 #define ConvertWToSLock(lock)\
351 AFS_LOCK_TRACE(CM_TRACE_LOCKDOWN, lock, SHARED_LOCK);\
352 (lock)->excl_locked = SHARED_LOCK; \
353 if((lock)->wait_states) \
354 Afs_Lock_ReleaseR(lock); \
357 #define ConvertWToRLock(lock) \
359 AFS_LOCK_TRACE(CM_TRACE_LOCKDOWN, lock, READ_LOCK);\
360 (lock)->excl_locked &= ~(SHARED_LOCK | WRITE_LOCK);\
361 ((lock)->readers_reading)++;\
362 Afs_Lock_ReleaseR(lock);\
365 #define ConvertSToRLock(lock) \
367 AFS_LOCK_TRACE(CM_TRACE_LOCKDOWN, lock, READ_LOCK);\
368 (lock)->excl_locked &= ~(SHARED_LOCK | WRITE_LOCK);\
369 ((lock)->readers_reading)++;\
370 Afs_Lock_ReleaseR(lock);\
373 #define ReleaseReadLock(lock)\
375 AFS_LOCK_TRACE(CM_TRACE_LOCKDONE, lock, READ_LOCK);\
376 if (!(--((lock)->readers_reading)) && (lock)->wait_states)\
377 Afs_Lock_ReleaseW(lock) ; \
380 #define ReleaseWriteLock(lock)\
382 AFS_LOCK_TRACE(CM_TRACE_LOCKDONE, lock, WRITE_LOCK);\
383 (lock)->excl_locked &= ~WRITE_LOCK;\
384 if ((lock)->wait_states) Afs_Lock_ReleaseR(lock);\
387 /* can be used on shared or boosted (write) locks */
388 #define ReleaseSharedLock(lock)\
390 AFS_LOCK_TRACE(CM_TRACE_LOCKDONE, lock, SHARED_LOCK);\
391 (lock)->excl_locked &= ~(SHARED_LOCK | WRITE_LOCK);\
392 if ((lock)->wait_states) Afs_Lock_ReleaseR(lock);\
395 #endif /* INSTRUMENT_LOCKS */
397 /* I added this next macro to make sure it is safe to nuke a lock -- Mike K. */
398 #define LockWaiters(lock)\
399 ((int) ((lock)->num_waiting))
401 #define CheckLock(lock)\
402 ((lock)->excl_locked? (int) -1 : (int) (lock)->readers_reading)
404 #define WriteLocked(lock)\
405 ((lock)->excl_locked & WRITE_LOCK)
410 You can also use the lock package for handling parent locks for independently-lockable sets of
411 small objects. The concept here is that the parent lock is at the same level in the
412 locking hierarchy as the little locks, but certain restrictions apply.
414 The general usage pattern is as follows. You have a set of entries to search. When searching it, you
415 have a "scan" lock on the table. If you find what you're looking for, you drop the lock down
416 to a "hold" lock, lock the entry, and release the parent lock. If you don't find what
417 you're looking for, you create the entry, downgrade the "scan" lock to a "hold" lock,
418 lock the entry and unlock the parent.
420 To delete an item from the table, you initially obtain a "purge" lock on the parent. Unlike all
421 of the other parent lock modes described herein, in order to obtain a "purge" lock mode, you
422 must have released all locks on any items in the table. Once you have obtained the parent
423 lock in "purge" mode, you should check to see if the entry is locked. If its not locked, you
424 are free to delete the entry, knowing that no one else can attempt to obtain a lock
425 on the entry while you have the purge lock held on the parent. Unfortunately, if it *is* locked,
426 you can not lock it yourself and wait for the other dude to release it, since the entry's locker
427 may need to lock another entry before unlocking the entry you want (which would result in
428 deadlock). Instead, then, you must release the parent lock, and try again "later" (see Lock_Wait
429 for assistance in waiting until later). Unfortunately, this is the best locking paradigm I've yet
432 What are the advantages to this scheme? First, the use of the parent lock ensures that
433 two people don't try to add the same entry at the same time or delete an entry while someone
434 else is adding it. It also ensures that when one process is deleting an entry, no one else is
435 preparing to lock the entry. Furthermore, when obtaining a lock on a little entry, you
436 are only holding a "hold" lock on the parent lock, so that others may come in and search
437 the table during this time. Thus it will not hold up the system if a little entry takes
438 a great deal of time to free up.
440 Here's how to compute the compatibility matrix:
444 add no deletions, additions allowed, additions will be performed, will obtain little locks
445 hold no deletions, additions allowed, no additions will be performed, will obtain little locks
446 purge no deletions or additions allowed, deletions will be performed, don't obtain little locks
448 When we compute the locking matrix, we note that hold is compatible with hold and add.
449 Add is compatible only with hold. purge is not compatible with anything. This is the same
450 matrix as obtained by mapping add->S, hold->read and purge->write locks. Thus we
451 can use the locks above to solve this problem, and we do.
455 #endif /* __AFSLOCK_INCLUDE__ */