From: Brian Norris Date: Thu, 11 Oct 2012 00:14:52 +0000 (-0700) Subject: spsc-queue: unrelacy X-Git-Tag: pldi2013~41 X-Git-Url: http://demsky.eecs.uci.edu/git/?p=model-checker-benchmarks.git;a=commitdiff_plain;h=91cb95bde87d2b7023fc2d2e344fba24e6dbca70 spsc-queue: unrelacy --- diff --git a/spsc-queue/eventcount.h b/spsc-queue/eventcount.h index 226603e..f64946a 100644 --- a/spsc-queue/eventcount.h +++ b/spsc-queue/eventcount.h @@ -1,3 +1,5 @@ +#include + class eventcount { public: @@ -8,33 +10,33 @@ public: void signal_relaxed() { - unsigned cmp = count($).load(std::memory_order_relaxed); + unsigned cmp = count.load(std::memory_order_relaxed); signal_impl(cmp); } void signal() { - unsigned cmp = count($).fetch_add(0, std::memory_order_seq_cst); + unsigned cmp = count.fetch_add(0, std::memory_order_seq_cst); signal_impl(cmp); } unsigned get() { - unsigned cmp = count($).fetch_or(0x80000000, + unsigned cmp = count.fetch_or(0x80000000, std::memory_order_seq_cst); return cmp & 0x7FFFFFFF; } void wait(unsigned cmp) { - unsigned ec = count($).load(std::memory_order_seq_cst); + unsigned ec = count.load(std::memory_order_seq_cst); if (cmp == (ec & 0x7FFFFFFF)) { guard.lock($); - ec = count($).load(std::memory_order_seq_cst); + ec = count.load(std::memory_order_seq_cst); if (cmp == (ec & 0x7FFFFFFF)) { - waiters($) += 1; + waiters += 1; cv.wait(guard, $); } guard.unlock($); @@ -52,10 +54,10 @@ private: if (cmp & 0x80000000) { guard.lock($); - while (false == count($).compare_swap(cmp, + while (false == count.compare_swap(cmp, (cmp + 1) & 0x7FFFFFFF, std::memory_order_relaxed)); unsigned w = waiters($); - waiters($) = 0; + waiters = 0; guard.unlock($); if (w) cv.notify_all($); diff --git a/spsc-queue/queue.h b/spsc-queue/queue.h index d65477b..7a6f29e 100644 --- a/spsc-queue/queue.h +++ b/spsc-queue/queue.h @@ -1,3 +1,5 @@ +#include + #include "eventcount.h" template @@ -7,21 +9,21 @@ public: spsc_queue() { node* n = RL_NEW node (); - head($) = n; - tail($) = n; + head = n; + tail = n; } ~spsc_queue() { - RL_ASSERT(head($) == tail($)); + RL_ASSERT(head == tail); RL_DELETE((node*)head($)); } void enqueue(T data) { node* n = RL_NEW node (data); - head($)->next($).store(n, std::memory_order_release); - head($) = n; + head($)->next.store(n, std::memory_order_release); + head = n; ec.signal_relaxed(); } @@ -62,12 +64,12 @@ private: T try_dequeue() { node* t = tail($); - node* n = t->next($).load(std::memory_order_acquire); + node* n = t->next.load(std::memory_order_acquire); if (0 == n) return 0; T data = n->data($); RL_DELETE(t); - tail($) = n; + tail = n; return data; } };