X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=spsc-example%2Fqueue.h;fp=spsc-example%2Fqueue.h;h=f96f9f09359860530cb3264125787b1c7071dd89;hb=28d17af30c170deda6f470b93d46dde123415fda;hp=94f98e2782533d351bba55b424d6a2d8a4a2e307;hpb=357cc2d31eaf9df96c2fc676fa00f40a54e83019;p=model-checker-benchmarks.git diff --git a/spsc-example/queue.h b/spsc-example/queue.h index 94f98e2..f96f9f0 100644 --- a/spsc-example/queue.h +++ b/spsc-example/queue.h @@ -4,38 +4,38 @@ using namespace std; +#define acquire memory_order_acquire +#define release memory_order_release +#define relaxed memory_order_relaxed + struct node { - node(int d) { - data.store(0, memory_order_normal); - next.store(0, wildcard(1)); + node(int idx) { + index.store(idx, relaxed); + next.store(0, relaxed); } atomic next; - atomic_int data; + atomic index; }; class spsc_queue { - atomic head, tail; + node *head, *tail; public: spsc_queue() { - head = tail = new node(0); + head = tail = new node(-1); } - - void enqueue(int val) { - node* n = new node(val); - node *h = head.load(wildcard(2)); - h->next.store(n, wildcard(3)); - head.store(n, wildcard(4)); + void enqueue(int idx) { + node* n = new node(idx); + tail->next.store(n, release); + tail = n; } - - bool dequeue(int *v) { - node* t = tail.load(wildcard(5)); - node* n = t->next.load(wildcard(6)); - if (0 == n) - return false; - *v = n->data.load(memory_order_normal); - tail.store(n, wildcard(7)); - delete (t); + bool dequeue(int *idx) { + node *tmp = head; + node *n = tmp->next.load(acquire); + if (NULL == n) return false; + head = n; + *idx = n->index.load(relaxed); + delete (tmp); return true; } };