changes to treiber
[model-checker-benchmarks.git] / treiber-stack / stack_wildcard.h
1 #include <stdatomic.h>
2 #include <atomic>
3
4 #include "wildcard.h"
5
6 #define release memory_order_release 
7 #define acquire memory_order_acquire 
8 #define acq_rel memory_order_acq_rel
9 #define relaxed memory_order_relaxed
10
11 struct node {
12         unsigned int value;
13         node *next;
14
15         node(unsigned int v) {
16                 value = v;
17                 next = NULL;
18         }
19 };
20
21 struct stack {
22         atomic<node*> top;
23
24         stack() {
25                 atomic_init(&top, NULL);
26         }
27
28         void push(unsigned int val) {
29                 node *n = new node(val);
30                 node *old = top.load(wildcard(1)); // acquire
31                 do {
32                         n->next = old;
33                 } while (!top.compare_exchange_strong(old, n, wildcard(2), wildcard(3)));
34                 // acq_rel & relaxed
35         }
36
37         unsigned int pop() {
38                 node *old = top.load(wildcard(4)); // acquire
39                 node *n;
40                 do {
41                         if (!old)
42                                 return 0;
43                         n = old->next;
44                 } while (!top.compare_exchange_strong(old, n, wildcard(5), wildcard(6)));
45                 // acq_rel & acquire
46                 return old->value;
47         }
48 };
49