|
|
|
@ -25,7 +25,7 @@
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <ck_brlock.h>
|
|
|
|
|
#include <ck_spinlock.h>
|
|
|
|
|
#include <ck_rwlock.h>
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
|
@ -35,75 +35,13 @@
|
|
|
|
|
#define STEPS 1000000
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* This is a naive reader/writer spinlock.
|
|
|
|
|
*/
|
|
|
|
|
struct rwlock {
|
|
|
|
|
unsigned int readers;
|
|
|
|
|
ck_spinlock_fas_t writer;
|
|
|
|
|
};
|
|
|
|
|
typedef struct rwlock rwlock_t;
|
|
|
|
|
|
|
|
|
|
static CK_CC_INLINE void
|
|
|
|
|
rwlock_init(rwlock_t *rw)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
ck_pr_store_uint(&rw->readers, 0);
|
|
|
|
|
ck_spinlock_fas_init(&rw->writer);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static CK_CC_INLINE void
|
|
|
|
|
rwlock_write_lock(rwlock_t *rw)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
ck_spinlock_fas_lock(&rw->writer);
|
|
|
|
|
while (ck_pr_load_uint(&rw->readers) != 0)
|
|
|
|
|
ck_pr_stall();
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static CK_CC_INLINE void
|
|
|
|
|
rwlock_write_unlock(rwlock_t *rw)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
ck_spinlock_fas_unlock(&rw->writer);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static CK_CC_INLINE void
|
|
|
|
|
rwlock_read_lock(rwlock_t *rw)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
|
while (ck_pr_load_uint(&rw->writer.value) != 0)
|
|
|
|
|
ck_pr_stall();
|
|
|
|
|
|
|
|
|
|
ck_pr_inc_uint(&rw->readers);
|
|
|
|
|
if (ck_pr_load_uint(&rw->writer.value) == 0)
|
|
|
|
|
break;
|
|
|
|
|
ck_pr_dec_uint(&rw->readers);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static CK_CC_INLINE void
|
|
|
|
|
rwlock_read_unlock(rwlock_t *rw)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
ck_pr_dec_uint(&rw->readers);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
main(void)
|
|
|
|
|
{
|
|
|
|
|
uint64_t s_b, e_b, i;
|
|
|
|
|
ck_brlock_t brlock = CK_BRLOCK_INITIALIZER;
|
|
|
|
|
ck_brlock_reader_t r[8];
|
|
|
|
|
rwlock_t naive;
|
|
|
|
|
ck_rwlock_t naive;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof(r) / sizeof(*r); i++)
|
|
|
|
|
ck_brlock_read_register(&brlock, &r[i]);
|
|
|
|
@ -121,16 +59,16 @@ main(void)
|
|
|
|
|
e_b = rdtsc();
|
|
|
|
|
printf("WRITE: brlock %15" PRIu64 "\n", (e_b - s_b) / STEPS);
|
|
|
|
|
|
|
|
|
|
rwlock_init(&naive);
|
|
|
|
|
ck_rwlock_init(&naive);
|
|
|
|
|
for (i = 0; i < STEPS; i++) {
|
|
|
|
|
rwlock_write_lock(&naive);
|
|
|
|
|
rwlock_write_unlock(&naive);
|
|
|
|
|
ck_rwlock_write_lock(&naive);
|
|
|
|
|
ck_rwlock_write_unlock(&naive);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s_b = rdtsc();
|
|
|
|
|
for (i = 0; i < STEPS; i++) {
|
|
|
|
|
rwlock_write_lock(&naive);
|
|
|
|
|
rwlock_write_unlock(&naive);
|
|
|
|
|
ck_rwlock_write_lock(&naive);
|
|
|
|
|
ck_rwlock_write_unlock(&naive);
|
|
|
|
|
}
|
|
|
|
|
e_b = rdtsc();
|
|
|
|
|
printf("WRITE: naive %15" PRIu64 "\n", (e_b - s_b) / STEPS);
|
|
|
|
@ -148,14 +86,14 @@ main(void)
|
|
|
|
|
printf("READ: brlock %15" PRIu64 "\n", (e_b - s_b) / STEPS);
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < STEPS; i++) {
|
|
|
|
|
rwlock_read_lock(&naive);
|
|
|
|
|
rwlock_read_unlock(&naive);
|
|
|
|
|
ck_rwlock_read_lock(&naive);
|
|
|
|
|
ck_rwlock_read_unlock(&naive);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s_b = rdtsc();
|
|
|
|
|
for (i = 0; i < STEPS; i++) {
|
|
|
|
|
rwlock_read_lock(&naive);
|
|
|
|
|
rwlock_read_unlock(&naive);
|
|
|
|
|
ck_rwlock_read_lock(&naive);
|
|
|
|
|
ck_rwlock_read_unlock(&naive);
|
|
|
|
|
}
|
|
|
|
|
e_b = rdtsc();
|
|
|
|
|
printf("READ: naive %15" PRIu64 "\n", (e_b - s_b) / STEPS);
|
|
|
|
|