Lines Matching refs:rw
41 __nisdb_rwinit(__nisdb_rwlock_t *rw) {
45 if (rw == 0) {
52 if ((ret = mutex_init(&rw->mutex, USYNC_THREAD, 0)) != 0)
54 if ((ret = cond_init(&rw->cv, USYNC_THREAD, 0)) != 0)
56 rw->destroyed = 0;
77 rw->force_write = NISDB_FORCE_WRITE;
79 rw->writer_count = rw->reader_count = rw->reader_blocked = 0;
80 rw->writer.id = rw->reader.id = INV_PTHREAD_ID;
81 rw->writer.count = rw->reader.count = 0;
82 rw->writer.next = rw->reader.next = 0;
89 find_reader(pthread_t id, __nisdb_rwlock_t *rw) {
93 for (rr = &rw->reader; rr != 0; rr = rr->next) {
107 __nisdb_rw_readlock_ok(__nisdb_rwlock_t *rw) {
110 if (rw == 0)
113 if (rw->destroyed != 0)
116 if ((ret = mutex_lock(&rw->mutex)) != 0)
123 if (rw->destroyed == 0 && rw->reader_count == 0) {
124 rw->force_write = 0;
130 (void) mutex_unlock(&rw->mutex);
137 __nisdb_rw_force_writelock(__nisdb_rwlock_t *rw) {
140 if (rw == 0 || rw->destroyed != 0)
143 if ((ret = mutex_lock(&rw->mutex)) != 0)
150 if (rw->destroyed == 0 && rw->reader_count == 0) {
151 rw->force_write = 1;
157 (void) mutex_unlock(&rw->mutex);
164 __nisdb_wlock_trylock(__nisdb_rwlock_t *rw, int trylock) {
171 if (rw == 0) {
179 if (rw->destroyed != 0)
182 if ((ret = mutex_lock(&rw->mutex)) != 0)
185 if (rw->destroyed != 0) {
186 (void) mutex_unlock(&rw->mutex);
191 if (rw->reader_count == 0 && rw->writer_count == 0) {
192 rw->writer_count = 1;
193 rw->writer.id = myself;
194 rw->writer.count = 1;
195 return (mutex_unlock(&rw->mutex));
202 if (rw->reader_count > 0) {
203 if ((rr = find_reader(myself, rw)) != 0) {
211 if (rw->reader_count ==
212 (rw->reader_blocked + 1))
221 if (rw->reader_count == rw->reader_blocked)
238 (rw->writer_count == 0 && rw->reader_count == 0) ||
239 (((rw->writer_count == 0 || rw->writer.id == myself) &&
240 (rw->reader_count == 0)) ||
241 (rw->reader_count == 1 &&
242 rw->reader.id == myself))) {
251 if (rw->writer_count == 0 || rw->writer.id == myself) {
261 (void) mutex_unlock(&rw->mutex);
268 rw->reader_blocked++;
270 if ((ret = cond_wait(&rw->cv, &rw->mutex)) != 0) {
273 if (rw->reader_blocked > 0)
274 rw->reader_blocked--;
280 (void) mutex_unlock(&rw->mutex);
285 if (rw->reader_blocked > 0)
286 rw->reader_blocked--;
295 rw->writer.id = myself;
297 rw->writer.count++;
299 if (rw->writer_count == 0)
300 rw->writer_count = 1;
302 return (mutex_unlock(&rw->mutex));
306 __nisdb_wlock(__nisdb_rwlock_t *rw) {
307 return (__nisdb_wlock_trylock(rw, 0));
312 increment_reader(pthread_t id, __nisdb_rwlock_t *rw) {
316 for (rr = &rw->reader; rr != 0; rr = rr->next) {
320 if (rw->reader_count == 0 && rr == &rw->reader) {
323 rw->reader_count = 1;
333 rr->next = rw->reader.next;
334 rw->reader.next = rr;
335 rw->reader_count++;
344 __nisdb_rlock(__nisdb_rwlock_t *rw) {
350 if (rw == 0) {
358 if (rw->destroyed != 0)
361 if (rw->force_write)
362 return (__nisdb_wlock(rw));
364 if ((ret = mutex_lock(&rw->mutex)) != 0)
367 if (rw->destroyed != 0) {
368 (void) mutex_unlock(&rw->mutex);
372 rr = find_reader(myself, rw);
375 while (rw->writer_count > 0 && rw->writer.id != myself) {
378 rw->reader_blocked++;
380 if ((ret = cond_wait(&rw->cv, &rw->mutex)) != 0) {
383 if (rw->reader_blocked > 0)
384 rw->reader_blocked--;
390 (void) mutex_unlock(&rw->mutex);
395 if (rw->reader_blocked > 0)
396 rw->reader_blocked--;
404 rr = increment_reader(myself, rw);
405 ret = mutex_unlock(&rw->mutex);
411 __nisdb_wulock(__nisdb_rwlock_t *rw) {
416 if (rw == 0) {
424 if (rw->destroyed != 0)
427 if ((ret = mutex_lock(&rw->mutex)) != 0)
430 if (rw->destroyed != 0) {
431 (void) mutex_unlock(&rw->mutex);
436 if (rw->writer_count == 0 ||
437 rw->writer.id != myself || rw->writer.count == 0) {
441 (void) mutex_unlock(&rw->mutex);
445 rw->writer.count--;
446 if (rw->writer.count == 0) {
447 rw->writer.id = INV_PTHREAD_ID;
448 rw->writer_count = 0;
449 if ((ret = cond_broadcast(&rw->cv)) != 0) {
450 (void) mutex_unlock(&rw->mutex);
455 return (mutex_unlock(&rw->mutex));
460 __nisdb_rulock(__nisdb_rwlock_t *rw) {
466 if (rw == 0) {
473 if (rw->destroyed != 0)
476 if (rw->force_write)
477 return (__nisdb_wulock(rw));
479 if ((ret = mutex_lock(&rw->mutex)) != 0)
482 if (rw->destroyed != 0) {
483 (void) mutex_unlock(&rw->mutex);
488 if (rw->reader_count == 0 ||
489 (rw->writer_count > 0 && rw->writer.id != myself)) {
493 (void) mutex_unlock(&rw->mutex);
498 for (rr = &rw->reader, prev = 0; rr != 0; prev = rr, rr = rr->next) {
507 (void) mutex_unlock(&rw->mutex);
513 if (rr != &rw->reader) {
524 rw->reader.id = rr->id;
525 rw->reader.count = rr->count;
526 rw->reader.next = rr->next;
533 rw->reader_count--;
537 if (rw->reader_count == 0) {
538 if ((ret = cond_broadcast(&rw->cv)) != 0) {
539 (void) mutex_unlock(&rw->mutex);
544 return (mutex_unlock(&rw->mutex));
550 __nisdb_assert_wheld(__nisdb_rwlock_t *rw) {
555 if (rw == 0) {
562 if (rw->destroyed != 0)
565 if ((ret = mutex_lock(&rw->mutex)) != 0)
568 if (rw->destroyed != 0) {
569 (void) mutex_unlock(&rw->mutex);
573 if (rw->writer_count == 0 || rw->writer.id != pthread_self()) {
574 ret = mutex_unlock(&rw->mutex);
583 return (mutex_unlock(&rw->mutex));
589 __nisdb_assert_rheld(__nisdb_rwlock_t *rw) {
596 if (rw == 0) {
603 if (rw->destroyed != 0)
606 if (rw->force_write)
607 return (__nisdb_assert_wheld(rw));
609 if ((ret = mutex_lock(&rw->mutex)) != 0)
612 if (rw->destroyed != 0) {
613 (void) mutex_unlock(&rw->mutex);
618 if (rw->writer_count > 0 && rw->writer.id == myself) {
619 (void) mutex_unlock(&rw->mutex);
623 if (rw->reader_count == 0) {
624 (void) mutex_unlock(&rw->mutex);
628 rr = &rw->reader;
631 (void) mutex_unlock(&rw->mutex);
637 ret = mutex_unlock(&rw->mutex);
643 __nisdb_destroy_lock(__nisdb_rwlock_t *rw) {
649 if (rw == 0) {
656 if (rw->destroyed != 0)
659 if ((ret = mutex_lock(&rw->mutex)) != 0)
662 if (rw->destroyed != 0) {
663 (void) mutex_unlock(&rw->mutex);
672 if (((rw->writer_count > 0 &&
673 (rw->writer.id != myself || rw->writer.count != 1)) ||
674 (rw->reader_count > 0 &&
675 !(rw->reader_count == 1 && rw->reader.id == myself &&
676 rw->reader.count == 1))) ||
677 (rw->writer_count > 0 && rw->reader_count > 0)) {
681 (void) mutex_unlock(&rw->mutex);
693 rw->destroyed++;
695 return (mutex_unlock(&rw->mutex));
699 __nisdb_lock_report(__nisdb_rwlock_t *rw) {
702 if (rw == 0) {
707 if (rw->destroyed)
708 printf("0x%x: DESTROYED\n", rw);
711 rw, rw->force_write ? "disallowed" : "allowed");
713 if (rw->writer_count == 0)
714 printf("0x%x: No writer\n", rw);
715 else if (rw->writer_count == 1) {
717 rw, rw->writer.id, rw->writer.count);
718 if (rw->writer.wait)
719 printf("0x%x:\tWriter blocked\n", rw);
722 rw, rw->writer_count);
724 if (rw->reader_count == 0)
725 printf("0x%x: No readers\n", rw);
730 rw, rw->reader_count, rw->reader_blocked);
731 for (r = &rw->reader; r != 0; r = r->next) {
733 rw, r->id, r->count,