ck_brlock: Allow for recursive reader lock.

ck_pring
Samy Al Bahra 14 years ago
parent 0d8faa5cd8
commit 851aaf8dd6

@ -170,27 +170,39 @@ CK_CC_INLINE static void
ck_brlock_read_lock(struct ck_brlock *br, struct ck_brlock_reader *reader) ck_brlock_read_lock(struct ck_brlock *br, struct ck_brlock_reader *reader)
{ {
if (reader->n_readers >= 1) {
ck_pr_store_uint(&reader->n_readers, reader->n_readers + 1);
return;
}
for (;;) { for (;;) {
while (ck_pr_load_uint(&br->writer) == true) while (ck_pr_load_uint(&br->writer) == true)
ck_pr_stall(); ck_pr_stall();
ck_pr_store_uint(&reader->n_readers, reader->n_readers + 1); ck_pr_store_uint(&reader->n_readers, 1);
ck_pr_fence_strict_memory(); ck_pr_fence_strict_memory();
if (ck_pr_load_uint(&br->writer) == false) if (ck_pr_load_uint(&br->writer) == false)
break; break;
ck_pr_store_uint(&reader->n_readers, reader->n_readers - 1); ck_pr_store_uint(&reader->n_readers, 0);
} }
return; return;
} }
CK_CC_INLINE static bool CK_CC_INLINE static bool
ck_brlock_read_trylock(struct ck_brlock *br, struct ck_brlock_reader *reader, unsigned int factor) ck_brlock_read_trylock(struct ck_brlock *br,
struct ck_brlock_reader *reader,
unsigned int factor)
{ {
unsigned int steps = 0; unsigned int steps = 0;
if (reader->n_readers >= 1) {
ck_pr_store_uint(&reader->n_readers, reader->n_readers + 1);
return true;
}
for (;;) { for (;;) {
while (ck_pr_load_uint(&br->writer) == true) { while (ck_pr_load_uint(&br->writer) == true) {
if (++steps >= factor) if (++steps >= factor)
@ -199,13 +211,13 @@ ck_brlock_read_trylock(struct ck_brlock *br, struct ck_brlock_reader *reader, un
ck_pr_stall(); ck_pr_stall();
} }
ck_pr_store_uint(&reader->n_readers, reader->n_readers + 1); ck_pr_store_uint(&reader->n_readers, 1);
ck_pr_fence_strict_memory(); ck_pr_fence_strict_memory();
if (ck_pr_load_uint(&br->writer) == false) if (ck_pr_load_uint(&br->writer) == false)
break; break;
ck_pr_store_uint(&reader->n_readers, reader->n_readers - 1); ck_pr_store_uint(&reader->n_readers, 0);
if (++steps >= factor) if (++steps >= factor)
return false; return false;

Loading…
Cancel
Save