ms-queue note
[model-checker-benchmarks.git] / ms-queue / queue_wildcard.c
1 #include <threads.h>
2 #include <stdlib.h>
3 #include "librace.h"
4 #include "model-assert.h"
5
6 #include "queue.h"
7 #include "wildcard.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_32(&free_lists[t][i]);
23                 unsigned int node = free_lists[t][i];
24                 if (node) {
25                         //store_32(&free_lists[t][i], 0);
26                         free_lists[t][i] = 0;
27                         return node;
28                 }
29         }
30         /* free_list is empty? */
31         MODEL_ASSERT(0);
32         return 0;
33 }
34
35 /* Place this node index back on this thread's free list */
36 static void reclaim(unsigned int node)
37 {
38         int i;
39         int t = get_thread_num();
40
41         /* Don't reclaim NULL node */
42         //MODEL_ASSERT(node);
43
44         for (i = 0; i < MAX_FREELIST; i++) {
45                 /* Should never race with our own thread here */
46                 //unsigned int idx = load_32(&free_lists[t][i]);
47                 unsigned int idx = free_lists[t][i];
48
49                 /* Found empty spot in free list */
50                 if (idx == 0) {
51                         //store_32(&free_lists[t][i], node);
52                         free_lists[t][i] = node;
53                         return;
54                 }
55         }
56         /* free list is full? */
57         MODEL_ASSERT(0);
58 }
59
60 void init_queue(queue_t *q, int num_threads)
61 {
62         int i, j;
63
64         /* Initialize each thread's free list with INITIAL_FREE pointers */
65         /* The actual nodes are initialized with poison indexes */
66         free_lists = malloc(num_threads * sizeof(*free_lists));
67         for (i = 0; i < num_threads; i++) {
68                 for (j = 0; j < INITIAL_FREE; j++) {
69                         free_lists[i][j] = 2 + i * MAX_FREELIST + j;
70                         atomic_init(&q->nodes[free_lists[i][j]].next, MAKE_POINTER(POISON_IDX, 0));
71                 }
72         }
73
74         /* initialize queue */
75         atomic_init(&q->head, MAKE_POINTER(1, 0));
76         atomic_init(&q->tail, MAKE_POINTER(1, 0));
77         atomic_init(&q->nodes[1].next, MAKE_POINTER(0, 0));
78 }
79
80 void enqueue(queue_t *q, unsigned int val)
81 {
82         int success = 0;
83         unsigned int node;
84         pointer tail;
85         pointer next;
86         pointer tmp;
87
88         node = new_node();
89         //store_32(&q->nodes[node].value, val);
90         q->nodes[node].value = val;
91         tmp = atomic_load_explicit(&q->nodes[node].next, wildcard(1)); // relaxed
92         set_ptr(&tmp, 0); // NULL
93         atomic_store_explicit(&q->nodes[node].next, tmp, wildcard(2)); // relaxed
94
95         while (!success) {
96                 tail = atomic_load_explicit(&q->tail, wildcard(3)); // acquire
97                 // FIXME: SCFence makes this relaxed 
98                 next = atomic_load_explicit(&q->nodes[get_ptr(tail)].next, wildcard(4)); //acquire
99                 if (tail == atomic_load_explicit(&q->tail, wildcard(5))) { // relaxed
100
101                         /* Check for uninitialized 'next' */
102                         //MODEL_ASSERT(get_ptr(next) != POISON_IDX);
103
104                         if (get_ptr(next) == 0) { // == NULL
105                                 pointer value = MAKE_POINTER(node, get_count(next) + 1);
106                                 
107                                 // ***********************************
108                                 // Inference analysis results have two choices here, it either
109                                 // makes wildcard(6) acq_rel and wildcard(8) relaxed or
110                                 // wildcard(6) release and wildcard(8) acquire. The
111                                 // synchronization here is for enqueue() to dequeue(), and
112                                 // actually either synchronization options will work!!!
113                                 success = atomic_compare_exchange_strong_explicit(&q->nodes[get_ptr(tail)].next,
114                                                 &next, value, wildcard(6), wildcard(7)); // release & relaxed
115                         }
116                         if (!success) {
117                                 unsigned int ptr =
118                                         get_ptr(atomic_load_explicit(&q->nodes[get_ptr(tail)].next, wildcard(8))); // acquire
119                                 pointer value = MAKE_POINTER(ptr,
120                                                 get_count(tail) + 1);
121                                 atomic_compare_exchange_strong_explicit(&q->tail,
122                                                 &tail, value,
123                                                 wildcard(9), wildcard(10)); // release & relaxed
124                                 thrd_yield();
125                         }
126                 }
127         }
128         atomic_compare_exchange_strong_explicit(&q->tail,
129                         &tail,
130                         MAKE_POINTER(node, get_count(tail) + 1),
131                         wildcard(11), wildcard(12)); // release & relaxed
132 }
133
134 bool dequeue(queue_t *q, unsigned int *retVal)
135 {
136         unsigned int value;
137         int success = 0;
138         pointer head;
139         pointer tail;
140         pointer next;
141
142         while (!success) {
143                 head = atomic_load_explicit(&q->head, wildcard(13)); // acquire
144                 // SCFence makes this acquire, and we actually need an acquire here!!!
145                 tail = atomic_load_explicit(&q->tail, wildcard(14)); // relaxed 
146                 next = atomic_load_explicit(&q->nodes[get_ptr(head)].next, wildcard(15)); // acquire
147                 if (atomic_load_explicit(&q->head, wildcard(16)) == head) { // relaxed
148                         if (get_ptr(head) == get_ptr(tail)) {
149
150                                 /* Check for uninitialized 'next' */
151                                 //MODEL_ASSERT(get_ptr(next) != POISON_IDX);
152
153                                 if (get_ptr(next) == 0) { // NULL
154                                         return false; // NULL
155                                 }
156                                 atomic_compare_exchange_strong_explicit(&q->tail,
157                                                 &tail,
158                                                 MAKE_POINTER(get_ptr(next), get_count(tail) + 1),
159                                                 wildcard(17), wildcard(18)); // release & relaxed
160                                 thrd_yield();
161                         } else {
162                                 //value = load_32(&q->nodes[get_ptr(next)].value);
163                                 value = q->nodes[get_ptr(next)].value;
164                                 success = atomic_compare_exchange_strong_explicit(&q->head,
165                                                 &head, MAKE_POINTER(get_ptr(next), get_count(head) + 1),
166                                                 wildcard(19), wildcard(20)); // release & relaxed
167                                 if (!success)
168                                         thrd_yield();
169                         }
170                 }
171         }
172         reclaim(get_ptr(head));
173         *retVal = value;
174         return true;
175 }