Initial IBM OpenAFS 1.0 tree
[openafs.git] / src / venus / lock.h
1 #ifndef __AFSLOCK_INCLUDE__
2 #define __AFSLOCK_INCLUDE__         1
3
4 #if !defined(lint) && !defined(LOCORE) && defined(RCS_HDRS)
5 #endif
6
7 /*
8  * (C) COPYRIGHT IBM CORPORATION 1987
9  * LICENSED MATERIALS - PROPERTY OF IBM
10  */
11
12 /*******************************************************************\
13 *                                                                   *
14 *       Information Technology Center                               *
15 *       Carnegie-Mellon University                                  *
16 *                                                                   *
17 *                                                                   *
18 *                                                                   *
19 \*******************************************************************/
20
21 /*
22         Include file for using Vice locking routines.
23 */
24
25 /* The following macros allow multi statement macros to be defined safely, i.e.
26    - the multi statement macro can be the object of an if statement;
27    - the call to the multi statement macro may be legally followed by a semi-colon.
28    BEGINMAC and ENDMAC have been tested with both the portable C compiler and
29    Hi-C.  Both compilers were from the Palo Alto 4.2BSD software releases, and
30    both optimized out the constant loop code.  For an example of the use
31    of BEGINMAC and ENDMAC, see the definition for ReleaseWriteLock, below.
32    An alternative to this, using "if(1)" for BEGINMAC is not used because it
33    may generate worse code with pcc, and may generate warning messages with hi-C.
34 */
35
36 #define BEGINMAC do {
37 #define ENDMAC   } while (0)
38
39 struct afs_bozoLock {
40     short count;    /* count of excl locks */
41     char flags;     /* bit 1: is anyone waiting? */
42     char spare;     /* for later */
43     char *proc;     /* process holding the lock, really a struct proc * */
44 };
45
46 #define AFS_BOZONWAITING    1       /* someone is waiting for this lock */
47
48 /* all locks wait on excl_locked except for READ_LOCK, which waits on readers_reading */
49 struct afs_lock {
50     unsigned char       wait_states;    /* type of lockers waiting */
51     unsigned char       excl_locked;    /* anyone have boosted, shared or write lock? */
52     unsigned char       readers_reading;        /* # readers actually with read locks */
53     unsigned char       num_waiting;    /* probably need this soon */
54 };
55
56 #define READ_LOCK       1
57 #define WRITE_LOCK      2
58 #define SHARED_LOCK     4
59 /* this next is not a flag, but rather a parameter to Afs_Lock_Obtain */
60 #define BOOSTED_LOCK 6
61
62 /* next defines wait_states for which we wait on excl_locked */
63 #define EXCL_LOCKS (WRITE_LOCK|SHARED_LOCK)
64
65 #define ObtainReadLock(lock)\
66         if (!((lock)->excl_locked & WRITE_LOCK))\
67             (lock) -> readers_reading++;\
68         else\
69             Afs_Lock_Obtain(lock, READ_LOCK)
70
71 #define ObtainWriteLock(lock)\
72         if (!(lock)->excl_locked && !(lock)->readers_reading)\
73             (lock) -> excl_locked = WRITE_LOCK;\
74         else\
75             Afs_Lock_Obtain(lock, WRITE_LOCK)
76
77 #define ObtainSharedLock(lock)\
78         if (!(lock)->excl_locked)\
79             (lock) -> excl_locked = SHARED_LOCK;\
80         else\
81             Afs_Lock_Obtain(lock, SHARED_LOCK)
82
83 #define UpgradeSToWLock(lock)\
84         if (!(lock)->readers_reading)\
85             (lock)->excl_locked = WRITE_LOCK;\
86         else\
87             Afs_Lock_Obtain(lock, BOOSTED_LOCK)
88
89 /* this must only be called with a WRITE or boosted SHARED lock! */
90 #define ConvertWToSLock(lock)\
91         BEGINMAC\
92             (lock)->excl_locked = SHARED_LOCK; \
93             if((lock)->wait_states) \
94                 Afs_Lock_ReleaseR(lock); \
95         ENDMAC
96
97 #define ConvertWToRLock(lock) \
98         BEGINMAC\
99             (lock)->excl_locked &= ~(SHARED_LOCK | WRITE_LOCK);\
100             (lock)->readers_reading++;\
101             Afs_Lock_ReleaseR(lock);\
102         ENDMAC
103
104 #define ConvertSToRLock(lock) \
105         BEGINMAC\
106             (lock)->excl_locked &= ~(SHARED_LOCK | WRITE_LOCK);\
107             (lock)->readers_reading++;\
108             Afs_Lock_ReleaseR(lock);\
109         ENDMAC
110
111 #define ReleaseReadLock(lock)\
112         BEGINMAC\
113             if (!--(lock)->readers_reading && (lock)->wait_states)\
114                 Afs_Lock_ReleaseW(lock) ; \
115         ENDMAC
116
117 #define ReleaseWriteLock(lock)\
118         BEGINMAC\
119             (lock)->excl_locked &= ~WRITE_LOCK;\
120             if ((lock)->wait_states) Afs_Lock_ReleaseR(lock);\
121         ENDMAC
122
123 /* can be used on shared or boosted (write) locks */
124 #define ReleaseSharedLock(lock)\
125         BEGINMAC\
126             (lock)->excl_locked &= ~(SHARED_LOCK | WRITE_LOCK);\
127             if ((lock)->wait_states) Afs_Lock_ReleaseR(lock);\
128         ENDMAC
129
130 /* I added this next macro to make sure it is safe to nuke a lock -- Mike K. */
131 #define LockWaiters(lock)\
132         ((int) ((lock)->num_waiting))
133
134 #define CheckLock(lock)\
135         ((lock)->excl_locked? (int) -1 : (int) (lock)->readers_reading)
136
137 #define WriteLocked(lock)\
138         ((lock)->excl_locked & WRITE_LOCK)
139
140 /*
141
142 You can also use the lock package for handling parent locks for independently-lockable sets of
143 small objects.  The concept here is that the parent lock is at the same level in the
144 locking hierarchy as the little locks, but certain restrictions apply.
145
146 The general usage pattern is as follows.  You have a set of entries to search.  When searching it, you
147 have a "scan" lock on the table.  If you find what you're looking for, you drop the lock down
148 to a "hold" lock, lock the entry, and release the parent lock.  If you don't find what
149 you're looking for, you create the entry, downgrade the "scan" lock to a "hold" lock,
150 lock the entry and unlock the parent.
151
152 To delete an item from the table, you initially obtain a "purge" lock on the parent.  Unlike all
153 of the other parent lock modes described herein, in order to obtain a "purge" lock mode, you
154 must have released all locks on any items in the table.  Once you have obtained the parent
155 lock in "purge" mode, you should check to see if the entry is locked.  If its not locked, you
156 are free to delete the entry, knowing that no one else can attempt to obtain a lock
157 on the entry while you have the purge lock held on the parent.  Unfortunately, if it *is* locked,
158 you can not lock it yourself and wait for the other dude to release it, since the entry's locker
159 may need to lock another entry before unlocking the entry you want (which would result in
160 deadlock).  Instead, then, you must release the parent lock, and try again "later" (see Lock_Wait
161 for assistance in waiting until later). Unfortunately, this is the best locking paradigm I've yet
162 come up with.
163
164 What are the advantages to this scheme?  First, the use of the parent lock ensures that
165 two people don't try to add the same entry at the same time or delete an entry while someone
166 else is adding it.  It also ensures that when one process is deleting an entry, no one else is
167 preparing to lock the entry.  Furthermore, when obtaining a lock on a little entry, you
168 are only holding a "hold" lock on the parent lock, so that others may come in and search
169 the table during this time.  Thus it will not hold up the system if a little entry takes
170 a great deal of time to free up.
171
172 Here's how to compute the compatibility matrix:
173
174 The invariants are:
175
176 add     no deletions, additions allowed, additions will be performed, will obtain little locks
177 hold    no deletions, additions allowed, no additions will be performed, will obtain little locks
178 purge   no deletions or additions allowed, deletions will be performed, don't obtain little locks
179
180 When we compute the locking matrix, we note that hold is compatible with hold and add.
181 Add is compatible only with hold.  purge is not compatible with anything.  This is the same
182 matrix as obtained by mapping add->S, hold->read and purge->write locks.  Thus we
183 can use the locks above to solve this problem, and we do.
184
185 */
186 #endif /* __AFSLOCK_INCLUDE__ */