From f8c46d903b303b81ef0d25cb8495de22d299466b Mon Sep 17 00:00:00 2001 From: Peizhao Ou Date: Thu, 12 Feb 2015 18:26:01 -0800 Subject: [PATCH] changes to seqlock --- Makefile | 2 +- seqlock/seqlock-wildcard.h | 3 +-- seqlock/seqlock.c | 4 ++-- seqlock/seqlock.h | 9 ++++----- seqlock/testcase1.c | 5 +++-- 5 files changed, 11 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index 835f7d1..b681de1 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ DIRS := barrier mcs-lock mpmc-queue spsc-queue spsc-bugfix linuxrwlocks \ dekker-fences chase-lev-deque ms-queue chase-lev-deque-bugfix \ - concurrent-hashmap + concurrent-hashmap seqlock .PHONY: $(DIRS) diff --git a/seqlock/seqlock-wildcard.h b/seqlock/seqlock-wildcard.h index 85686ea..62b7583 100644 --- a/seqlock/seqlock-wildcard.h +++ b/seqlock/seqlock-wildcard.h @@ -27,9 +27,8 @@ typedef struct seqlock { } void write(int new_data) { + int old_seq = _seq.load(wildcard(4)); // acquire while (true) { - // This might be a relaxed too - int old_seq = _seq.load(wildcard(4)); // acquire if (old_seq % 2 == 1) continue; // Retry diff --git a/seqlock/seqlock.c b/seqlock/seqlock.c index d2e33b8..146239f 100644 --- a/seqlock/seqlock.c +++ b/seqlock/seqlock.c @@ -22,11 +22,11 @@ int user_main(int argc, char **argv) { lock = new seqlock_t(); thrd_create(&t1, (thrd_start_t)&a, NULL); - thrd_create(&t2, (thrd_start_t)&b, NULL); + //thrd_create(&t2, (thrd_start_t)&b, NULL); thrd_create(&t3, (thrd_start_t)&c, NULL); thrd_join(t1); - thrd_join(t2); + //thrd_join(t2); thrd_join(t3); return 0; } diff --git a/seqlock/seqlock.h b/seqlock/seqlock.h index f263bec..c5a965c 100644 --- a/seqlock/seqlock.h +++ b/seqlock/seqlock.h @@ -17,7 +17,7 @@ typedef struct seqlock { int old_seq = _seq.load(memory_order_acquire); // acquire if (old_seq % 2 == 1) continue; - int res = _data.load(memory_order_acquire); // acquire + int res = _data.load(memory_order_acquire); if (_seq.load(memory_order_relaxed) == old_seq) { // relaxed return res; } @@ -25,20 +25,19 @@ typedef struct seqlock { } void write(int new_data) { + int old_seq = _seq.load(memory_order_acquire); // acquire while (true) { // This might be a relaxed too - int old_seq = _seq.load(memory_order_acquire); // acquire if (old_seq % 2 == 1) continue; // Retry - // Should be relaxed!!! if (_seq.compare_exchange_strong(old_seq, old_seq + 1, - memory_order_relaxed, memory_order_relaxed)) // relaxed + memory_order_acq_rel, memory_order_acquire)) break; } // Update the data - _data.store(new_data, memory_order_release); // release + _data.store(new_data, memory_order_release); // Can be relaxed _seq.fetch_add(1, memory_order_release); // release } diff --git a/seqlock/testcase1.c b/seqlock/testcase1.c index 6417db3..8441972 100644 --- a/seqlock/testcase1.c +++ b/seqlock/testcase1.c @@ -14,6 +14,7 @@ static void b(void *obj) { } static void c(void *obj) { + lock->write(2); int r1 = lock->read(); } @@ -22,11 +23,11 @@ int user_main(int argc, char **argv) { lock = new seqlock_t(); thrd_create(&t1, (thrd_start_t)&a, NULL); - thrd_create(&t2, (thrd_start_t)&b, NULL); + //thrd_create(&t2, (thrd_start_t)&b, NULL); thrd_create(&t3, (thrd_start_t)&c, NULL); thrd_join(t1); - thrd_join(t2); + //thrd_join(t2); thrd_join(t3); return 0; } -- 2.34.1