ck_rwlock: Migrate to ck_elide.h.

ck_pring
Samy Al Bahra 12 years ago
parent 68a614328a
commit 671f82d7a8

@ -27,6 +27,7 @@
#ifndef _CK_RWLOCK_H
#define _CK_RWLOCK_H
#include <ck_elide.h>
#include <ck_pr.h>
#include <stdbool.h>
#include <stddef.h>
@ -58,20 +59,13 @@ ck_rwlock_write_unlock(ck_rwlock_t *rw)
return;
}
#ifdef CK_F_PR_RTM
CK_CC_INLINE static void
ck_rwlock_write_unlock_rtm(ck_rwlock_t *rw)
CK_CC_INLINE static bool
ck_rwlock_locked_writer(ck_rwlock_t *rw)
{
if (ck_pr_load_uint(&rw->writer) == 0) {
ck_pr_rtm_end();
return;
}
ck_rwlock_write_unlock(rw);
return;
ck_pr_fence_load();
return ck_pr_load_uint(&rw->writer);
}
#endif /* CK_F_PR_RTM */
CK_CC_INLINE static void
ck_rwlock_write_downgrade(ck_rwlock_t *rw)
@ -82,24 +76,17 @@ ck_rwlock_write_downgrade(ck_rwlock_t *rw)
return;
}
#ifdef CK_F_PR_RTM
CK_CC_INLINE static void
ck_rwlock_write_downgrade_rtm(ck_rwlock_t *rw)
CK_CC_INLINE static bool
ck_rwlock_locked(ck_rwlock_t *rw)
{
unsigned int r;
if (ck_pr_load_uint(&rw->writer) != 0) {
ck_rwlock_write_downgrade(rw);
return;
}
ck_pr_fence_load();
r = ck_pr_load_uint(&rw->writer);
ck_pr_fence_load();
/*
* Both reader and writer counters are in read-set. A transactional
* abort will occur in the presence of another writer. Inner-most
* read_unlock call will attempt a transactional commit.
*/
return;
return ck_pr_load_uint(&rw->n_readers) | r;
}
#endif /* CK_F_PR_RTM */
CK_CC_INLINE static bool
ck_rwlock_write_trylock(ck_rwlock_t *rw)
@ -118,25 +105,8 @@ ck_rwlock_write_trylock(ck_rwlock_t *rw)
return true;
}
#ifdef CK_F_PR_RTM
CK_CC_INLINE static bool
ck_rwlock_write_trylock_rtm(ck_rwlock_t *rw)
{
bool r;
if (ck_pr_rtm_begin() != CK_PR_RTM_STARTED)
return false;
r = ck_pr_load_uint(&rw->writer) != 0;
ck_pr_fence_load();
if (r | (ck_pr_load_uint(&rw->n_readers) != 0))
ck_pr_rtm_abort(0);
return true;
}
#endif /* CK_F_PR_RTM */
CK_ELIDE_TRYLOCK_PROTOTYPE(ck_rwlock_write, ck_rwlock_t,
ck_rwlock_locked, ck_rwlock_write_trylock)
CK_CC_INLINE static void
ck_rwlock_write_lock(ck_rwlock_t *rw)
@ -153,27 +123,9 @@ ck_rwlock_write_lock(ck_rwlock_t *rw)
return;
}
#ifdef CK_F_PR_RTM
CK_CC_INLINE static void
ck_rwlock_write_lock_rtm(ck_rwlock_t *rw)
{
bool r;
if (ck_pr_rtm_begin() != CK_PR_RTM_STARTED) {
ck_rwlock_write_lock(rw);
return;
}
r = ck_pr_load_uint(&rw->writer) != 0;
ck_pr_fence_load();
if (r | (ck_pr_load_uint(&rw->n_readers) != 0))
ck_pr_rtm_abort(0);
return;
}
#endif /* CK_F_PR_RTM */
CK_ELIDE_PROTOTYPE(ck_rwlock_write, ck_rwlock_t,
ck_rwlock_locked, ck_rwlock_write_lock,
ck_rwlock_locked_writer, ck_rwlock_write_unlock)
CK_CC_INLINE static bool
ck_rwlock_read_trylock(ck_rwlock_t *rw)
@ -199,21 +151,8 @@ ck_rwlock_read_trylock(ck_rwlock_t *rw)
return false;
}
#ifdef CK_F_PR_RTM
CK_CC_INLINE static bool
ck_rwlock_read_trylock_rtm(ck_rwlock_t *rw)
{
if (ck_pr_rtm_begin() != CK_PR_RTM_STARTED)
return false;
if (ck_pr_load_uint(&rw->writer) == 0)
return true;
ck_pr_rtm_abort(0);
return false;
}
#endif /* CK_F_PR_RTM */
CK_ELIDE_TRYLOCK_PROTOTYPE(ck_rwlock_read, ck_rwlock_t,
ck_rwlock_locked_writer, ck_rwlock_read_trylock)
CK_CC_INLINE static void
ck_rwlock_read_lock(ck_rwlock_t *rw)
@ -242,22 +181,13 @@ ck_rwlock_read_lock(ck_rwlock_t *rw)
return;
}
#ifdef CK_F_PR_RTM
CK_CC_INLINE static void
ck_rwlock_read_lock_rtm(ck_rwlock_t *rw)
CK_CC_INLINE static bool
ck_rwlock_locked_reader(ck_rwlock_t *rw)
{
if (ck_pr_rtm_begin() == CK_PR_RTM_STARTED) {
if (ck_pr_load_uint(&rw->writer) != 0)
ck_pr_rtm_abort(0);
return;
}
ck_rwlock_read_lock(rw);
return;
ck_pr_fence_load();
return ck_pr_load_uint(&rw->n_readers);
}
#endif /* CK_F_PR_RTM */
CK_CC_INLINE static void
ck_rwlock_read_unlock(ck_rwlock_t *rw)
@ -268,20 +198,9 @@ ck_rwlock_read_unlock(ck_rwlock_t *rw)
return;
}
#ifdef CK_F_PR_RTM
CK_CC_INLINE static void
ck_rwlock_read_unlock_rtm(ck_rwlock_t *rw)
{
if (ck_pr_load_uint(&rw->n_readers) == 0) {
ck_pr_rtm_end();
} else {
ck_rwlock_read_unlock(rw);
}
return;
}
#endif /* CK_F_PR_RTM */
CK_ELIDE_PROTOTYPE(ck_rwlock_read, ck_rwlock_t,
ck_rwlock_locked_writer, ck_rwlock_read_lock,
ck_rwlock_locked_reader, ck_rwlock_read_unlock)
/*
* Recursive writer reader-writer lock implementation.

@ -14,4 +14,4 @@ clean:
rm -rf *.dSYM *.exe *~ *.o $(OBJECTS)
include ../../../build/regressions.build
CFLAGS+=$(PTHREAD_CFLAGS) -D_GNU_SOURCE
CFLAGS+=$(PTHREAD_CFLAGS) -D_GNU_SOURCE -g

@ -55,14 +55,14 @@ main(void)
#ifdef CK_F_PR_RTM
for (i = 0; i < STEPS; i++) {
ck_rwlock_write_lock_rtm(&rwlock);
ck_rwlock_write_unlock_rtm(&rwlock);
CK_ELIDE_LOCK(ck_rwlock_write, &rwlock);
CK_ELIDE_UNLOCK(ck_rwlock_write, &rwlock);
}
s_b = rdtsc();
for (i = 0; i < STEPS; i++) {
ck_rwlock_write_lock_rtm(&rwlock);
ck_rwlock_write_unlock_rtm(&rwlock);
CK_ELIDE_LOCK(ck_rwlock_write, &rwlock);
CK_ELIDE_UNLOCK(ck_rwlock_write, &rwlock);
}
e_b = rdtsc();
printf(" (rtm) WRITE: rwlock %15" PRIu64 "\n", (e_b - s_b) / STEPS);
@ -83,14 +83,14 @@ main(void)
#ifdef CK_F_PR_RTM
for (i = 0; i < STEPS; i++) {
ck_rwlock_read_lock_rtm(&rwlock);
ck_rwlock_read_unlock_rtm(&rwlock);
CK_ELIDE_LOCK(ck_rwlock_read, &rwlock);
CK_ELIDE_UNLOCK(ck_rwlock_read, &rwlock);
}
s_b = rdtsc();
for (i = 0; i < STEPS; i++) {
ck_rwlock_read_lock_rtm(&rwlock);
ck_rwlock_read_unlock_rtm(&rwlock);
CK_ELIDE_LOCK(ck_rwlock_read, &rwlock);
CK_ELIDE_UNLOCK(ck_rwlock_read, &rwlock);
}
e_b = rdtsc();
printf(" (rtm) READ: rwlock %15" PRIu64 "\n", (e_b - s_b) / STEPS);

@ -67,38 +67,38 @@ thread_lock_rtm(void *pun)
for (i = 1, a = 0;; i++) {
s_b = rdtsc();
ck_rwlock_read_lock_rtm(&rw.lock);
ck_rwlock_read_unlock_rtm(&rw.lock);
ck_rwlock_read_lock_rtm(&rw.lock);
ck_rwlock_read_unlock_rtm(&rw.lock);
ck_rwlock_read_lock_rtm(&rw.lock);
ck_rwlock_read_unlock_rtm(&rw.lock);
ck_rwlock_read_lock_rtm(&rw.lock);
ck_rwlock_read_unlock_rtm(&rw.lock);
ck_rwlock_read_lock_rtm(&rw.lock);
ck_rwlock_read_unlock_rtm(&rw.lock);
ck_rwlock_read_lock_rtm(&rw.lock);
ck_rwlock_read_unlock_rtm(&rw.lock);
ck_rwlock_read_lock_rtm(&rw.lock);
ck_rwlock_read_unlock_rtm(&rw.lock);
ck_rwlock_read_lock_rtm(&rw.lock);
ck_rwlock_read_unlock_rtm(&rw.lock);
ck_rwlock_read_lock_rtm(&rw.lock);
ck_rwlock_read_unlock_rtm(&rw.lock);
ck_rwlock_read_lock_rtm(&rw.lock);
ck_rwlock_read_unlock_rtm(&rw.lock);
ck_rwlock_read_lock_rtm(&rw.lock);
ck_rwlock_read_unlock_rtm(&rw.lock);
ck_rwlock_read_lock_rtm(&rw.lock);
ck_rwlock_read_unlock_rtm(&rw.lock);
ck_rwlock_read_lock_rtm(&rw.lock);
ck_rwlock_read_unlock_rtm(&rw.lock);
ck_rwlock_read_lock_rtm(&rw.lock);
ck_rwlock_read_unlock_rtm(&rw.lock);
ck_rwlock_read_lock_rtm(&rw.lock);
ck_rwlock_read_unlock_rtm(&rw.lock);
ck_rwlock_read_lock_rtm(&rw.lock);
ck_rwlock_read_unlock_rtm(&rw.lock);
CK_ELIDE_LOCK(ck_rwlock_read, &rw.lock);
CK_ELIDE_UNLOCK(ck_rwlock_read, &rw.lock);
CK_ELIDE_LOCK(ck_rwlock_read, &rw.lock);
CK_ELIDE_UNLOCK(ck_rwlock_read, &rw.lock);
CK_ELIDE_LOCK(ck_rwlock_read, &rw.lock);
CK_ELIDE_UNLOCK(ck_rwlock_read, &rw.lock);
CK_ELIDE_LOCK(ck_rwlock_read, &rw.lock);
CK_ELIDE_UNLOCK(ck_rwlock_read, &rw.lock);
CK_ELIDE_LOCK(ck_rwlock_read, &rw.lock);
CK_ELIDE_UNLOCK(ck_rwlock_read, &rw.lock);
CK_ELIDE_LOCK(ck_rwlock_read, &rw.lock);
CK_ELIDE_UNLOCK(ck_rwlock_read, &rw.lock);
CK_ELIDE_LOCK(ck_rwlock_read, &rw.lock);
CK_ELIDE_UNLOCK(ck_rwlock_read, &rw.lock);
CK_ELIDE_LOCK(ck_rwlock_read, &rw.lock);
CK_ELIDE_UNLOCK(ck_rwlock_read, &rw.lock);
CK_ELIDE_LOCK(ck_rwlock_read, &rw.lock);
CK_ELIDE_UNLOCK(ck_rwlock_read, &rw.lock);
CK_ELIDE_LOCK(ck_rwlock_read, &rw.lock);
CK_ELIDE_UNLOCK(ck_rwlock_read, &rw.lock);
CK_ELIDE_LOCK(ck_rwlock_read, &rw.lock);
CK_ELIDE_UNLOCK(ck_rwlock_read, &rw.lock);
CK_ELIDE_LOCK(ck_rwlock_read, &rw.lock);
CK_ELIDE_UNLOCK(ck_rwlock_read, &rw.lock);
CK_ELIDE_LOCK(ck_rwlock_read, &rw.lock);
CK_ELIDE_UNLOCK(ck_rwlock_read, &rw.lock);
CK_ELIDE_LOCK(ck_rwlock_read, &rw.lock);
CK_ELIDE_UNLOCK(ck_rwlock_read, &rw.lock);
CK_ELIDE_LOCK(ck_rwlock_read, &rw.lock);
CK_ELIDE_UNLOCK(ck_rwlock_read, &rw.lock);
CK_ELIDE_LOCK(ck_rwlock_read, &rw.lock);
CK_ELIDE_UNLOCK(ck_rwlock_read, &rw.lock);
e_b = rdtsc();
a += (e_b - s_b) >> 4;

@ -137,7 +137,7 @@ thread_rtm_mix(void *null CK_CC_UNUSED)
while (i--) {
if (i & 1) {
ck_rwlock_write_lock_rtm(&lock);
CK_ELIDE_LOCK(ck_rwlock_write, &lock);
} else {
ck_rwlock_write_lock(&lock);
}
@ -178,13 +178,13 @@ thread_rtm_mix(void *null CK_CC_UNUSED)
}
if (i & 1) {
ck_rwlock_write_unlock_rtm(&lock);
CK_ELIDE_UNLOCK(ck_rwlock_write, &lock);
} else {
ck_rwlock_write_unlock(&lock);
}
if (i & 1) {
ck_rwlock_read_lock_rtm(&lock);
CK_ELIDE_LOCK(ck_rwlock_read, &lock);
} else {
ck_rwlock_read_lock(&lock);
}
@ -197,7 +197,7 @@ thread_rtm_mix(void *null CK_CC_UNUSED)
}
if (i & 1) {
ck_rwlock_read_unlock_rtm(&lock);
CK_ELIDE_UNLOCK(ck_rwlock_read, &lock);
} else {
ck_rwlock_read_unlock(&lock);
}
@ -218,7 +218,7 @@ thread_rtm(void *null CK_CC_UNUSED)
}
while (i--) {
ck_rwlock_write_lock_rtm(&lock);
CK_ELIDE_LOCK(ck_rwlock_write, &lock);
{
l = ck_pr_load_uint(&locked);
if (l != 0) {
@ -253,16 +253,16 @@ thread_rtm(void *null CK_CC_UNUSED)
ck_error("ERROR [WR:%d]: %u != 0\n", __LINE__, l);
}
}
ck_rwlock_write_unlock_rtm(&lock);
CK_ELIDE_UNLOCK(ck_rwlock_write, &lock);
ck_rwlock_read_lock_rtm(&lock);
CK_ELIDE_LOCK(ck_rwlock_read, &lock);
{
l = ck_pr_load_uint(&locked);
if (l != 0) {
ck_error("ERROR [RD:%d]: %u != 0\n", __LINE__, l);
}
}
ck_rwlock_read_unlock_rtm(&lock);
CK_ELIDE_UNLOCK(ck_rwlock_read, &lock);
}
return (NULL);

Loading…
Cancel
Save