another benchmark
[satcheck.git] / benchmarks / satcheck / trieber-stack / trieber-stack_unannotated.c
1 #include <threads.h>
2 #include <stdlib.h>
3 // #include "librace.h"
4 #include "model-assert.h"
5 #include "libinterface.h"
6
7 #include "trieber-stack.h"
8
9 #define MAX_FREELIST 4 /* Each thread can own up to MAX_FREELIST free nodes */
10 #define INITIAL_FREE 2 /* Each thread starts with INITIAL_FREE free nodes */
11
12 #define POISON_IDX 0x666
13
14 static unsigned int (*free_lists)[MAX_FREELIST];
15
16 /* Search this thread's free list for a "new" node */
17 static unsigned int new_node()
18 {
19         int i;
20         int t = get_thread_num();
21         for (i = 0; i < MAX_FREELIST; i++) {
22                 unsigned int node = load_64(&free_lists[t][i]);
23                 if (node) {
24                         store_64(&free_lists[t][i], 0);
25                         return node;
26                 }
27         }
28         /* free_list is empty? */
29         MODEL_ASSERT(0);
30         return 0;
31 }
32
33 /* Place this node index back on this thread's free list */
34 static void reclaim(unsigned int node)
35 {
36         int i;
37         int t = get_thread_num();
38
39         /* Don't reclaim NULL node */
40         //MODEL_ASSERT(node);
41
42         for (i = 0; i < MAX_FREELIST; i++) {
43                 /* Should never race with our own thread here */
44                 unsigned int idx = load_64(&free_lists[t][i]);
45
46                 /* Found empty spot in free list */
47                 if (idx == 0) {
48                         store_64(&free_lists[t][i], node);
49                         return;
50                 }
51         }
52         /* free list is full? */
53         MODEL_ASSERT(0);
54 }
55
56 void init_stack(stack_t *s, int num_threads)
57 {
58         int i, j;
59
60         /* Initialize each thread's free list with INITIAL_FREE pointers */
61         /* The actual nodes are initialized with poison indexes */
62         free_lists = malloc(num_threads * sizeof(*free_lists));
63         for (i = 0; i < num_threads; i++) {
64                 for (j = 0; j < INITIAL_FREE; j++) {
65                         free_lists[i][j] = 1 + i * MAX_FREELIST + j;
66                         atomic_init(&s->nodes[free_lists[i][j]].next, MAKE_POINTER(POISON_IDX, 0));
67                 }
68         }
69
70         /* initialize stack */
71         atomic_init(&s->top, MAKE_POINTER(0, 0));
72 }
73
74 void push(stack_t *s, unsigned int val) {
75         unsigned int nodeIdx = new_node();
76         node_t *node = &s->nodes[nodeIdx];
77         node->value = val;
78         pointer oldTop, newTop;
79         bool success;
80         while (true) {
81                 // acquire
82                 oldTop = atomic_load_explicit(&s->top, acquire);
83                 newTop = MAKE_POINTER(nodeIdx, get_count(oldTop) + 1);
84                 // relaxed
85                 store_64(&node->next, oldTop);
86
87                 // release & relaxed
88                 success = rmw_64(CAS, &s->top, &oldTop, newTop);
89                 if (success)
90                         break;
91         } 
92 }
93
94 unsigned int pop(stack_t *s) 
95 {
96         pointer oldTop, newTop, next;
97         node_t *node;
98         bool success;
99         int val;
100         while (true) {
101                 // acquire
102                 oldTop = atomic_load_explicit(&s->top, acquire);
103                 if (get_ptr(oldTop) == 0)
104                         return 0;
105                 node = &s->nodes[get_ptr(oldTop)];
106                 // relaxed
107                 next = atomic_load_explicit(&node->next, relaxed);
108                 newTop = MAKE_POINTER(get_ptr(next), get_count(oldTop) + 1);
109                 // release & relaxed
110                 success = atomic_compare_exchange_strong_explicit(&s->top, &oldTop,
111                         newTop, release, relaxed);
112                 if (success)
113                         break;
114         }
115         val = node->value;
116         /* Reclaim the used slot */
117         reclaim(get_ptr(oldTop));
118         return val;
119 }