From fa286223861165ee5617712a9bf9c6e6553be3fd Mon Sep 17 00:00:00 2001 From: Samy Al Bahra Date: Thu, 17 Apr 2014 13:54:59 -0400 Subject: [PATCH] ck_rwlock: Re-observe latch bits if necessary. --- include/ck_rwlock.h | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/include/ck_rwlock.h b/include/ck_rwlock.h index f0002e3..5470b05 100644 --- a/include/ck_rwlock.h +++ b/include/ck_rwlock.h @@ -213,8 +213,21 @@ ck_rwlock_read_lock(ck_rwlock_t *rw) */ ck_pr_fence_atomic_load(); - if (ck_pr_load_32(&rw->writer) == 0) + if (ck_pr_load_32(&rw->writer) == 0) { + /* + * It is possible that a latch operation has itself + * gone unnoticed across a write-side critical section. + * However, the write-side critical section itself + * guarantees an ordering with respect to the previous + * fence operation. If writer is observed to 0, then + * it is guaranteed that the latch will be visible. + */ + snapshot = ck_pr_load_32(&rw->n_readers); + if (snapshot >> CK_RWLOCK_LATCH_SHIFT) + continue; + break; + } ck_pr_dec_32(&rw->n_readers); }