changes
[model-checker-benchmarks.git] / spsc-example / queue.h
1 #include <unrelacy.h>
2 #include <atomic>
3 #include "wildcard.h"
4
5 using namespace std;
6
7 struct node {
8         node(int d) {
9                 data.store(0, memory_order_normal);
10                 next.store(0, wildcard(1));
11         }
12         atomic<node*> next;
13         atomic_int data;
14 };
15
16 class spsc_queue
17 {
18         atomic<node*> head, tail;
19         public:
20         spsc_queue() {
21                 head = tail = new node(0);
22         }
23
24         void enqueue(int val) {
25                 node* n = new node(val);
26                 node *h = head.load(wildcard(2)); 
27                 h->next.store(n, wildcard(3));
28                 head.store(n, wildcard(4));
29         }
30
31         bool dequeue(int *v) {
32                 node* t = tail.load(wildcard(5));
33                 node* n = t->next.load(wildcard(6));
34                 if (0 == n)
35                         return false;
36                 *v = n->data.load(memory_order_normal);
37                 tail.store(n, wildcard(7));
38                 delete (t);
39                 return true;
40         }
41 };