rf_stripelocks.c revision 1.2 1 /* $NetBSD: rf_stripelocks.c,v 1.2 1999/01/26 02:34:03 oster Exp $ */
2 /*
3 * Copyright (c) 1995 Carnegie-Mellon University.
4 * All rights reserved.
5 *
6 * Authors: Mark Holland, Jim Zelenka
7 *
8 * Permission to use, copy, modify and distribute this software and
9 * its documentation is hereby granted, provided that both the copyright
10 * notice and this permission notice appear in all copies of the
11 * software, derivative works or modified versions, and any portions
12 * thereof, and that both notices appear in supporting documentation.
13 *
14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
15 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
16 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17 *
18 * Carnegie Mellon requests users of this software to return to
19 *
20 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
21 * School of Computer Science
22 * Carnegie Mellon University
23 * Pittsburgh PA 15213-3890
24 *
25 * any improvements or extensions that they make and grant Carnegie the
26 * rights to redistribute these changes.
27 */
28
29 /*
30 * stripelocks.c -- code to lock stripes for read and write access
31 *
32 * The code distinguishes between read locks and write locks. There can be
33 * as many readers to given stripe as desired. When a write request comes
34 * in, no further readers are allowed to enter, and all subsequent requests
35 * are queued in FIFO order. When a the number of readers goes to zero, the
36 * writer is given the lock. When a writer releases the lock, the list of
37 * queued requests is scanned, and all readersq up to the next writer are
38 * given the lock.
39 *
40 * The lock table size must be one less than a power of two, but HASH_STRIPEID
41 * is the only function that requires this.
42 *
43 * The code now supports "range locks". When you ask to lock a stripe, you
44 * specify a range of addresses in that stripe that you want to lock. When
45 * you acquire the lock, you've locked only this range of addresses, and
46 * other threads can concurrently read/write any non-overlapping portions
47 * of the stripe. The "addresses" that you lock are abstract in that you
48 * can pass in anything you like. The expectation is that you'll pass in
49 * the range of physical disk offsets of the parity bits you're planning
50 * to update. The idea behind this, of course, is to allow sub-stripe
51 * locking. The implementation is perhaps not the best imaginable; in the
52 * worst case a lock release is O(n^2) in the total number of outstanding
53 * requests to a given stripe. Note that if you're striping with a
54 * stripe unit size equal to an entire disk (i.e. not striping), there will
55 * be only one stripe and you may spend some significant number of cycles
56 * searching through stripe lock descriptors.
57 */
58
59 #include "rf_types.h"
60 #include "rf_raid.h"
61 #include "rf_stripelocks.h"
62 #include "rf_alloclist.h"
63 #include "rf_threadid.h"
64 #include "rf_general.h"
65 #include "rf_freelist.h"
66 #include "rf_debugprint.h"
67 #include "rf_driver.h"
68 #include "rf_shutdown.h"
69
70 #define Dprintf1(s,a) rf_debug_printf(s,(void *)((unsigned long)a),NULL,NULL,NULL,NULL,NULL,NULL,NULL)
71 #define Dprintf2(s,a,b) rf_debug_printf(s,(void *)((unsigned long)a),(void *)((unsigned long)b),NULL,NULL,NULL,NULL,NULL,NULL)
72 #define Dprintf3(s,a,b,c) rf_debug_printf(s,(void *)((unsigned long)a),(void *)((unsigned long)b),(void *)((unsigned long)c),NULL,NULL,NULL,NULL,NULL)
73 #define Dprintf4(s,a,b,c,d) rf_debug_printf(s,(void *)((unsigned long)a),(void *)((unsigned long)b),(void *)((unsigned long)c),(void *)((unsigned long)d),NULL,NULL,NULL,NULL)
74 #define Dprintf5(s,a,b,c,d,e) rf_debug_printf(s,(void *)((unsigned long)a),(void *)((unsigned long)b),(void *)((unsigned long)c),(void *)((unsigned long)d),(void *)((unsigned long)e),NULL,NULL,NULL)
75 #define Dprintf6(s,a,b,c,d,e,f) rf_debug_printf(s,(void *)((unsigned long)a),(void *)((unsigned long)b),(void *)((unsigned long)c),(void *)((unsigned long)d),(void *)((unsigned long)e),(void *)((unsigned long)f),NULL,NULL)
76 #define Dprintf7(s,a,b,c,d,e,f,g) rf_debug_printf(s,(void *)((unsigned long)a),(void *)((unsigned long)b),(void *)((unsigned long)c),(void *)((unsigned long)d),(void *)((unsigned long)e),(void *)((unsigned long)f),(void *)((unsigned long)g),NULL)
77 #define Dprintf8(s,a,b,c,d,e,f,g,h) rf_debug_printf(s,(void *)((unsigned long)a),(void *)((unsigned long)b),(void *)((unsigned long)c),(void *)((unsigned long)d),(void *)((unsigned long)e),(void *)((unsigned long)f),(void *)((unsigned long)g),(void *)((unsigned long)h))
78
79 #define FLUSH
80
81 #define HASH_STRIPEID(_sid_) ( (_sid_) & (rf_lockTableSize-1) )
82 #define MAX_FREELIST 100
83
84 static void AddToWaitersQueue(RF_LockTableEntry_t *lockTable, RF_StripeLockDesc_t *lockDesc, RF_LockReqDesc_t *lockReqDesc);
85 static RF_StripeLockDesc_t *AllocStripeLockDesc(RF_StripeNum_t stripeID);
86 static void FreeStripeLockDesc(RF_StripeLockDesc_t *p);
87 static void PrintLockedStripes(RF_LockTableEntry_t *lockTable);
88
89 /* determines if two ranges overlap. always yields false if either start value is negative */
90 #define SINGLE_RANGE_OVERLAP(_strt1, _stop1, _strt2, _stop2) \
91 ( (_strt1 >= 0) && (_strt2 >= 0) && (RF_MAX(_strt1, _strt2) <= RF_MIN(_stop1, _stop2)) )
92
93 /* determines if any of the ranges specified in the two lock descriptors overlap each other */
94 #define RANGE_OVERLAP(_cand, _pred) \
95 ( SINGLE_RANGE_OVERLAP((_cand)->start, (_cand)->stop, (_pred)->start, (_pred)->stop ) || \
96 SINGLE_RANGE_OVERLAP((_cand)->start2, (_cand)->stop2, (_pred)->start, (_pred)->stop ) || \
97 SINGLE_RANGE_OVERLAP((_cand)->start, (_cand)->stop, (_pred)->start2, (_pred)->stop2) || \
98 SINGLE_RANGE_OVERLAP((_cand)->start2, (_cand)->stop2, (_pred)->start2, (_pred)->stop2) )
99
100 /* Determines if a candidate lock request conflicts with a predecessor lock req.
101 * Note that the arguments are not interchangeable.
102 * The rules are:
103 * a candidate read conflicts with a predecessor write if any ranges overlap
104 * a candidate write conflicts with a predecessor read if any ranges overlap
105 * a candidate write conflicts with a predecessor write if any ranges overlap
106 */
107 #define STRIPELOCK_CONFLICT(_cand, _pred) \
108 RANGE_OVERLAP((_cand), (_pred)) && \
109 ( ( (((_cand)->type == RF_IO_TYPE_READ) && ((_pred)->type == RF_IO_TYPE_WRITE)) || \
110 (((_cand)->type == RF_IO_TYPE_WRITE) && ((_pred)->type == RF_IO_TYPE_READ)) || \
111 (((_cand)->type == RF_IO_TYPE_WRITE) && ((_pred)->type == RF_IO_TYPE_WRITE)) \
112 ) \
113 )
114
115 static RF_FreeList_t *rf_stripelock_freelist;
116 #define RF_MAX_FREE_STRIPELOCK 128
117 #define RF_STRIPELOCK_INC 8
118 #define RF_STRIPELOCK_INITIAL 32
119
120 static void rf_ShutdownStripeLockFreeList(void *);
121 static void rf_RaidShutdownStripeLocks(void *);
122
123 static void rf_ShutdownStripeLockFreeList(ignored)
124 void *ignored;
125 {
126 RF_FREELIST_DESTROY(rf_stripelock_freelist,next,(RF_StripeLockDesc_t *));
127 }
128
129 int rf_ConfigureStripeLockFreeList(listp)
130 RF_ShutdownList_t **listp;
131 {
132 unsigned mask;
133 int rc;
134
135 RF_FREELIST_CREATE(rf_stripelock_freelist, RF_MAX_FREE_STRIPELOCK,
136 RF_STRIPELOCK_INITIAL,sizeof(RF_StripeLockDesc_t));
137 rc = rf_ShutdownCreate(listp, rf_ShutdownStripeLockFreeList, NULL);
138 if (rc) {
139 RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n",
140 __FILE__, __LINE__, rc);
141 rf_ShutdownStripeLockFreeList(NULL);
142 return(rc);
143 }
144 RF_FREELIST_PRIME(rf_stripelock_freelist,RF_STRIPELOCK_INITIAL,next,
145 (RF_StripeLockDesc_t *));
146 for (mask=0x1; mask; mask<<=1)
147 if (rf_lockTableSize==mask)
148 break;
149 if (!mask) {
150 printf("[WARNING: lock table size must be a power of two. Setting to %d.]\n",RF_DEFAULT_LOCK_TABLE_SIZE);
151 rf_lockTableSize = RF_DEFAULT_LOCK_TABLE_SIZE;
152 }
153 return(0);
154 }
155
156 RF_LockTableEntry_t *rf_MakeLockTable()
157 {
158 RF_LockTableEntry_t *lockTable;
159 int i, rc;
160
161 RF_Calloc(lockTable, ((int) rf_lockTableSize), sizeof(RF_LockTableEntry_t), (RF_LockTableEntry_t *));
162 if (lockTable == NULL)
163 return(NULL);
164 for (i=0; i<rf_lockTableSize; i++) {
165 rc = rf_mutex_init(&lockTable[i].mutex);
166 if (rc) {
167 RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
168 __LINE__, rc);
169 /* XXX clean up other mutexes */
170 return(NULL);
171 }
172 }
173 return(lockTable);
174 }
175
176 void rf_ShutdownStripeLocks(RF_LockTableEntry_t *lockTable)
177 {
178 int i;
179
180 if (rf_stripeLockDebug) {
181 PrintLockedStripes(lockTable);
182 }
183 for (i=0; i<rf_lockTableSize; i++) {
184 rf_mutex_destroy(&lockTable[i].mutex);
185 }
186 RF_Free(lockTable, rf_lockTableSize*sizeof(RF_LockTableEntry_t));
187 }
188
189 static void rf_RaidShutdownStripeLocks(arg)
190 void *arg;
191 {
192 RF_Raid_t *raidPtr = (RF_Raid_t *)arg;
193 rf_ShutdownStripeLocks(raidPtr->lockTable);
194 }
195
196 int rf_ConfigureStripeLocks(
197 RF_ShutdownList_t **listp,
198 RF_Raid_t *raidPtr,
199 RF_Config_t *cfgPtr)
200 {
201 int rc;
202
203 raidPtr->lockTable = rf_MakeLockTable();
204 if (raidPtr->lockTable == NULL)
205 return(ENOMEM);
206 rc = rf_ShutdownCreate(listp, rf_RaidShutdownStripeLocks, raidPtr);
207 if (rc) {
208 RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n",
209 __FILE__, __LINE__, rc);
210 rf_ShutdownStripeLocks(raidPtr->lockTable);
211 return(rc);
212 }
213 return(0);
214 }
215
216 /* returns 0 if you've got the lock, and non-zero if you have to wait.
217 * if and only if you have to wait, we'll cause cbFunc to get invoked
218 * with cbArg when you are granted the lock. We store a tag in *releaseTag
219 * that you need to give back to us when you release the lock.
220 */
221 int rf_AcquireStripeLock(
222 RF_LockTableEntry_t *lockTable,
223 RF_StripeNum_t stripeID,
224 RF_LockReqDesc_t *lockReqDesc)
225 {
226 RF_StripeLockDesc_t *lockDesc;
227 RF_LockReqDesc_t *p;
228 int tid=0, hashval = HASH_STRIPEID(stripeID);
229 int retcode = 0;
230
231 RF_ASSERT(RF_IO_IS_R_OR_W(lockReqDesc->type));
232
233 if (rf_stripeLockDebug) {
234 rf_get_threadid(tid);
235 if (stripeID == -1) Dprintf1("[%d] Lock acquisition supressed (stripeID == -1)\n",tid);
236 else {
237 Dprintf8("[%d] Trying to acquire stripe lock table 0x%lx SID %ld type %c range %ld-%ld, range2 %ld-%ld hashval %d\n",
238 tid, (unsigned long) lockTable, stripeID, lockReqDesc->type, lockReqDesc->start,
239 lockReqDesc->stop, lockReqDesc->start2, lockReqDesc->stop2);
240 Dprintf3("[%d] lock %ld hashval %d\n", tid, stripeID, hashval);
241 FLUSH;
242 }
243 }
244 if (stripeID == -1) return(0);
245 lockReqDesc->next = NULL; /* just to be sure */
246
247 RF_LOCK_MUTEX(lockTable[hashval].mutex);
248 for (lockDesc = lockTable[hashval].descList; lockDesc; lockDesc=lockDesc->next) {
249 if (lockDesc->stripeID == stripeID) break;
250 }
251
252 if (!lockDesc) { /* no entry in table => no one reading or writing */
253 lockDesc = AllocStripeLockDesc(stripeID);
254 lockDesc->next = lockTable[hashval].descList;
255 lockTable[hashval].descList = lockDesc;
256 if (lockReqDesc->type == RF_IO_TYPE_WRITE) lockDesc->nWriters++;
257 lockDesc->granted = lockReqDesc;
258 if (rf_stripeLockDebug) {Dprintf7("[%d] no one waiting: lock %ld %c %ld-%ld %ld-%ld granted\n",
259 tid,stripeID,lockReqDesc->type,lockReqDesc->start,lockReqDesc->stop,lockReqDesc->start2,lockReqDesc->stop2); FLUSH;}
260 } else {
261
262 if (lockReqDesc->type == RF_IO_TYPE_WRITE) lockDesc->nWriters++;
263
264 if (lockDesc->nWriters == 0) { /* no need to search any lists if there are no writers anywhere */
265 lockReqDesc->next = lockDesc->granted;
266 lockDesc->granted = lockReqDesc;
267 if (rf_stripeLockDebug) {Dprintf7("[%d] no writers: lock %ld %c %ld-%ld %ld-%ld granted\n",
268 tid,stripeID,lockReqDesc->type,lockReqDesc->start,lockReqDesc->stop,lockReqDesc->start2,lockReqDesc->stop2); FLUSH;}
269 } else {
270
271 /* search the granted & waiting lists for a conflict. stop searching as soon as we find one */
272 retcode = 0;
273 for (p = lockDesc->granted; p; p=p->next) if (STRIPELOCK_CONFLICT(lockReqDesc, p)) {retcode = 1; break;}
274 if (!retcode) for (p = lockDesc->waitersH; p; p=p->next) if (STRIPELOCK_CONFLICT(lockReqDesc, p)) {retcode = 2; break;}
275
276 if (!retcode) {
277 lockReqDesc->next = lockDesc->granted; /* no conflicts found => grant lock */
278 lockDesc->granted = lockReqDesc;
279 if (rf_stripeLockDebug) {
280 Dprintf7("[%d] no conflicts: lock %ld %c %ld-%ld %ld-%ld granted\n",
281 tid,stripeID,lockReqDesc->type,lockReqDesc->start,lockReqDesc->stop,
282 lockReqDesc->start2,lockReqDesc->stop2);
283 FLUSH;
284 }
285 } else {
286 if (rf_stripeLockDebug) {
287 Dprintf6("[%d] conflict: lock %ld %c %ld-%ld hashval=%d not granted\n",
288 tid,stripeID,lockReqDesc->type,lockReqDesc->start,lockReqDesc->stop,
289 hashval);
290 Dprintf3("[%d] lock %ld retcode=%d\n", tid, stripeID, retcode);
291 FLUSH;
292 }
293 AddToWaitersQueue(lockTable, lockDesc, lockReqDesc); /* conflict => the current access must wait */
294 }
295 }
296 }
297
298 RF_UNLOCK_MUTEX(lockTable[hashval].mutex);
299 return(retcode);
300 }
301
302 void rf_ReleaseStripeLock(
303 RF_LockTableEntry_t *lockTable,
304 RF_StripeNum_t stripeID,
305 RF_LockReqDesc_t *lockReqDesc)
306 {
307 RF_StripeLockDesc_t *lockDesc, *ld_t;
308 RF_LockReqDesc_t *lr, *lr_t, *callbacklist, *t;
309 RF_IoType_t type = lockReqDesc->type;
310 int tid=0, hashval = HASH_STRIPEID(stripeID);
311 int release_it, consider_it;
312 RF_LockReqDesc_t *candidate, *candidate_t, *predecessor;
313
314 RF_ASSERT(RF_IO_IS_R_OR_W(type));
315
316 if (rf_stripeLockDebug) {
317 rf_get_threadid(tid);
318 if (stripeID == -1) Dprintf1("[%d] Lock release supressed (stripeID == -1)\n",tid);
319 else {Dprintf8("[%d] Releasing stripe lock on stripe ID %ld, type %c range %ld-%ld %ld-%ld table 0x%lx\n",
320 tid,stripeID,lockReqDesc->type,lockReqDesc->start,lockReqDesc->stop,lockReqDesc->start2,lockReqDesc->stop2, lockTable); FLUSH;}
321 }
322
323 if (stripeID == -1) return;
324
325 RF_LOCK_MUTEX(lockTable[hashval].mutex);
326
327 /* find the stripe lock descriptor */
328 for (ld_t = NULL, lockDesc = lockTable[hashval].descList; lockDesc; ld_t = lockDesc, lockDesc=lockDesc->next) {
329 if (lockDesc->stripeID == stripeID) break;
330 }
331 RF_ASSERT(lockDesc); /* major error to release a lock that doesn't exist */
332
333 /* find the stripe lock request descriptor & delete it from the list */
334 for (lr_t = NULL, lr = lockDesc->granted; lr; lr_t = lr, lr=lr->next) if (lr == lockReqDesc) break;
335
336 RF_ASSERT(lr && (lr == lockReqDesc)); /* major error to release a lock that hasn't been granted */
337 if (lr_t) lr_t->next = lr->next; else {
338 RF_ASSERT(lr == lockDesc->granted);
339 lockDesc->granted = lr->next;
340 }
341 lr->next = NULL;
342
343 if (lockReqDesc->type == RF_IO_TYPE_WRITE) lockDesc->nWriters--;
344
345 /* search through the waiters list to see if anyone needs to be woken up.
346 * for each such descriptor in the wait list, we check it against everything granted and against
347 * everything _in front_ of it in the waiters queue. If it conflicts with none of these, we release it.
348 *
349 * DON'T TOUCH THE TEMPLINK POINTER OF ANYTHING IN THE GRANTED LIST HERE. This will roach the case where
350 * the callback tries to acquire a new lock in the same stripe. There are some asserts to try and detect this.
351 *
352 * We apply 2 performance optimizations:
353 * (1) if releasing this lock results in no more writers to this stripe, we just release everybody waiting,
354 * since we place no restrictions on the number of concurrent reads.
355 * (2) we consider as candidates for wakeup only those waiters that have a range overlap with either
356 * the descriptor being woken up or with something in the callbacklist (i.e. something we've just now woken up).
357 * This allows us to avoid the long evaluation for some descriptors.
358 */
359
360 callbacklist = NULL;
361 if (lockDesc->nWriters == 0) { /* performance tweak (1) */
362 while (lockDesc->waitersH) {
363
364 lr = lockDesc->waitersH; /* delete from waiters list */
365 lockDesc->waitersH = lr->next;
366
367 RF_ASSERT(lr->type == RF_IO_TYPE_READ);
368
369 lr->next = lockDesc->granted; /* add to granted list */
370 lockDesc->granted = lr;
371
372 RF_ASSERT(!lr->templink);
373 lr->templink = callbacklist; /* put on callback list so that we'll invoke callback below */
374 callbacklist = lr;
375 if (rf_stripeLockDebug) {Dprintf8("[%d] No writers: granting lock stripe ID %ld, type %c range %ld-%ld %ld-%ld table 0x%lx\n",
376 tid,stripeID,lr->type,lr->start,lr->stop,lr->start2,lr->stop2,(unsigned long) lockTable); FLUSH;}
377 }
378 lockDesc->waitersT = NULL; /* we've purged the whole waiters list */
379
380 } else for (candidate_t = NULL, candidate = lockDesc->waitersH; candidate; ) {
381
382 /* performance tweak (2) */
383 consider_it = 0;
384 if (RANGE_OVERLAP(lockReqDesc, candidate)) consider_it = 1;
385 else for (t = callbacklist; t; t=t->templink) if (RANGE_OVERLAP(t, candidate)) {
386 consider_it = 1;
387 break;
388 }
389 if (!consider_it) {
390 if (rf_stripeLockDebug) {Dprintf8("[%d] No overlap: rejecting candidate stripeID %ld, type %c range %ld-%ld %ld-%ld table 0x%lx\n",
391 tid, stripeID, candidate->type, candidate->start, candidate->stop, candidate->start2, candidate->stop2,
392 (unsigned long) lockTable); FLUSH;}
393 candidate_t = candidate; candidate = candidate->next;
394 continue;
395 }
396
397
398 /* we have a candidate for release. check to make sure it is not blocked by any granted locks */
399 release_it = 1;
400 for (predecessor = lockDesc->granted; predecessor; predecessor = predecessor->next) {
401 if (STRIPELOCK_CONFLICT(candidate, predecessor)) {
402 if (rf_stripeLockDebug) {
403 Dprintf8("[%d] Conflicts with granted lock: rejecting candidate stripeID %ld, type %c range %ld-%ld %ld-%ld table 0x%lx\n",
404 tid, stripeID, candidate->type, candidate->start, candidate->stop, candidate->start2, candidate->stop2,
405 (unsigned long) lockTable); FLUSH;
406 }
407 release_it = 0; break;
408 }
409 }
410
411 /* now check to see if the candidate is blocked by any waiters that occur before it it the wait queue */
412 if (release_it) for (predecessor = lockDesc->waitersH; predecessor != candidate; predecessor = predecessor->next) {
413 if (STRIPELOCK_CONFLICT(candidate, predecessor)) {
414 if (rf_stripeLockDebug) {
415 Dprintf8("[%d] Conflicts with waiting lock: rejecting candidate stripeID %ld, type %c range %ld-%ld %ld-%ld table 0x%lx\n",
416 tid, stripeID, candidate->type, candidate->start, candidate->stop, candidate->start2, candidate->stop2,
417 (unsigned long) lockTable); FLUSH;
418 }
419 release_it = 0; break;
420 }
421 }
422
423 /* release it if indicated */
424 if (release_it) {
425 if (rf_stripeLockDebug) {Dprintf8("[%d] Granting lock to candidate stripeID %ld, type %c range %ld-%ld %ld-%ld table 0x%lx\n",
426 tid, stripeID, candidate->type, candidate->start, candidate->stop, candidate->start2, candidate->stop2,
427 (unsigned long) lockTable); FLUSH;}
428 if (candidate_t) {
429 candidate_t->next = candidate->next;
430 if (lockDesc->waitersT == candidate) lockDesc->waitersT = candidate_t; /* cannot be waitersH since candidate_t is not NULL */
431 } else {
432 RF_ASSERT(candidate == lockDesc->waitersH);
433 lockDesc->waitersH = lockDesc->waitersH->next;
434 if (!lockDesc->waitersH) lockDesc->waitersT = NULL;
435 }
436 candidate->next = lockDesc->granted; /* move it to the granted list */
437 lockDesc->granted = candidate;
438
439 RF_ASSERT(!candidate->templink);
440 candidate->templink = callbacklist; /* put it on the list of things to be called after we release the mutex */
441 callbacklist = candidate;
442
443 if (!candidate_t) candidate = lockDesc->waitersH; else candidate = candidate_t->next; /* continue with the rest of the list */
444 } else {
445 candidate_t = candidate; candidate = candidate->next; /* continue with the rest of the list */
446 }
447 }
448
449 /* delete the descriptor if no one is waiting or active */
450 if (!lockDesc->granted && !lockDesc->waitersH) {
451 RF_ASSERT(lockDesc->nWriters == 0);
452 if (rf_stripeLockDebug) {
453 Dprintf3("[%d] Last lock released (table 0x%lx): deleting desc for stripeID %ld\n",tid,(unsigned long) lockTable, stripeID); FLUSH;
454 }
455 if (ld_t) ld_t->next = lockDesc->next; else {
456 RF_ASSERT(lockDesc == lockTable[hashval].descList);
457 lockTable[hashval].descList = lockDesc->next;
458 }
459 FreeStripeLockDesc(lockDesc);
460 lockDesc = NULL; /* only for the ASSERT below */
461 }
462
463 RF_UNLOCK_MUTEX(lockTable[hashval].mutex);
464
465 /* now that we've unlocked the mutex, invoke the callback on all the descriptors in the list */
466 RF_ASSERT(!( (callbacklist) && (!lockDesc) )); /* if we deleted the descriptor, we should have no callbacks to do */
467 for (candidate = callbacklist; candidate; ) {
468 t = candidate;
469 candidate = candidate->templink;
470 t->templink = NULL;
471 (t->cbFunc)(t->cbArg);
472 }
473 }
474
475 /* must have the indicated lock table mutex upon entry */
476 static void AddToWaitersQueue(
477 RF_LockTableEntry_t *lockTable,
478 RF_StripeLockDesc_t *lockDesc,
479 RF_LockReqDesc_t *lockReqDesc)
480 {
481 int tid;
482
483 if (rf_stripeLockDebug) {
484 rf_get_threadid(tid);
485 Dprintf3("[%d] Waiting on lock for stripe %ld table 0x%lx\n", tid, lockDesc->stripeID, (unsigned long) lockTable); FLUSH;
486 }
487 if (!lockDesc->waitersH) {
488 lockDesc->waitersH = lockDesc->waitersT = lockReqDesc;
489 } else {
490 lockDesc->waitersT->next = lockReqDesc;
491 lockDesc->waitersT = lockReqDesc;
492 }
493 }
494
495 static RF_StripeLockDesc_t *AllocStripeLockDesc(RF_StripeNum_t stripeID)
496 {
497 RF_StripeLockDesc_t *p;
498
499 RF_FREELIST_GET(rf_stripelock_freelist,p,next,(RF_StripeLockDesc_t *));
500 if (p) {
501 p->stripeID = stripeID;
502 }
503 return(p);
504 }
505
506 static void FreeStripeLockDesc(RF_StripeLockDesc_t *p)
507 {
508 RF_FREELIST_FREE(rf_stripelock_freelist,p,next);
509 }
510
511 static void PrintLockedStripes(lockTable)
512 RF_LockTableEntry_t *lockTable;
513 {
514 int i, j, foundone = 0, did;
515 RF_StripeLockDesc_t *p;
516 RF_LockReqDesc_t *q;
517
518 RF_LOCK_MUTEX(rf_printf_mutex);
519 printf("Locked stripes:\n");
520 for (i=0; i<rf_lockTableSize; i++) if (lockTable[i].descList) {
521 foundone = 1;
522 for (p = lockTable[i].descList; p; p=p->next) {
523 printf("Stripe ID 0x%lx (%d) nWriters %d\n",
524 (long)p->stripeID, (int)p->stripeID, p->nWriters);
525
526 if (! (p->granted) ) printf("Granted: (none)\n"); else printf("Granted:\n");
527 for (did=1,j=0,q = p->granted; q; j++,q=q->next) {
528 printf(" %c(%ld-%ld",q->type,(long)q->start,(long)q->stop);
529 if (q->start2 != -1) printf(",%ld-%ld) ",(long)q->start2,
530 (long)q->stop2); else printf(") ");
531 if (j && !(j%4)) {printf("\n"); did=1;} else did=0;
532 }
533 if (!did) printf("\n");
534
535 if (! (p->waitersH) ) printf("Waiting: (none)\n"); else printf("Waiting:\n");
536 for (did=1,j=0,q = p->waitersH; q; j++,q=q->next) {
537 printf("%c(%ld-%ld",q->type,(long)q->start,(long)q->stop);
538 if (q->start2 != -1) printf(",%ld-%ld) ",(long)q->start2,(long)q->stop2); else printf(") ");
539 if (j && !(j%4)) {printf("\n "); did=1;} else did=0;
540 }
541 if (!did) printf("\n");
542 }
543 }
544 if (!foundone) printf("(none)\n"); else printf("\n");
545 RF_UNLOCK_MUTEX(rf_printf_mutex);
546 }
547