8a0f4eb97b08a504779072613b9cbd111ddc7eb9
[model-checker-benchmarks.git] / ms-queue / queue.c
1 #include <threads.h>
2 #include <stdlib.h>
3 #include "librace.h"
4 #include "model-assert.h"
5
6 #include "queue.h"
7
8 #define relaxed memory_order_relaxed
9 #define release memory_order_release
10 #define acquire memory_order_acquire
11
12 #define MAX_FREELIST 4 /* Each thread can own up to MAX_FREELIST free nodes */
13 #define INITIAL_FREE 2 /* Each thread starts with INITIAL_FREE free nodes */
14
15 #define POISON_IDX 0x666
16
17 static unsigned int (*free_lists)[MAX_FREELIST];
18
19 /* Search this thread's free list for a "new" node */
20 static unsigned int new_node()
21 {
22         int i;
23         int t = get_thread_num();
24         for (i = 0; i < MAX_FREELIST; i++) {
25                 //unsigned int node = load_32(&free_lists[t][i]);
26                 unsigned int node = free_lists[t][i];
27                 if (node) {
28                         //store_32(&free_lists[t][i], 0);
29                         free_lists[t][i] = 0;
30                         return node;
31                 }
32         }
33         /* free_list is empty? */
34         MODEL_ASSERT(0);
35         return 0;
36 }
37
38 /* Simulate the fact that when a node got recycled, it will get assigned to the
39  * same queue or for other usage */
40 void simulateRecycledNodeUpdate(queue_t *q, unsigned int node) {
41         atomic_store_explicit(&q->nodes[node].next, -1, memory_order_release);
42 }
43
44
45 /* Place this node index back on this thread's free list */
46 static void reclaim(unsigned int node)
47 {
48         int i;
49         int t = get_thread_num();
50
51         /* Don't reclaim NULL node */
52         //MODEL_ASSERT(node);
53
54         for (i = 0; i < MAX_FREELIST; i++) {
55                 /* Should never race with our own thread here */
56                 //unsigned int idx = load_32(&free_lists[t][i]);
57                 unsigned int idx = free_lists[t][i];
58
59                 /* Found empty spot in free list */
60                 if (idx == 0) {
61                         //store_32(&free_lists[t][i], node);
62                         free_lists[t][i] = node;
63                         return;
64                 }
65         }
66         /* free list is full? */
67         //MODEL_ASSERT(0);
68 }
69
70 void init_queue(queue_t *q, int num_threads)
71 {
72         int i, j;
73
74         /* Initialize each thread's free list with INITIAL_FREE pointers */
75         /* The actual nodes are initialized with poison indexes */
76         free_lists = ( unsigned int (*)[MAX_FREELIST] ) malloc(num_threads * sizeof(*free_lists));
77         for (i = 0; i < num_threads; i++) {
78                 for (j = 0; j < INITIAL_FREE; j++) {
79                         free_lists[i][j] = 2 + i * MAX_FREELIST + j;
80                         atomic_init(&q->nodes[free_lists[i][j]].next, MAKE_POINTER(POISON_IDX, 0));
81                 }
82         }
83
84         /* initialize queue */
85         atomic_init(&q->head, MAKE_POINTER(1, 0));
86         atomic_init(&q->tail, MAKE_POINTER(1, 0));
87         atomic_init(&q->nodes[1].next, MAKE_POINTER(0, 0));
88 }
89
90 /** @DeclareState: IntList *q;
91 @Initial: q = new IntList;
92 @Print:
93         model_print("\tSTATE(q): ");
94     printContainer(q);
95         model_print("\n"); */
96
97 /** @Transition: STATE(q)->push_back(val);
98 @Print: model_print("\tENQ #%d: val=%d\n", ID, val); */
99 void enqueue(queue_t *q, unsigned int val, int n)
100 {
101         int success = 0;
102         unsigned int node;
103         pointer tail;
104         pointer next;
105         pointer tmp;
106
107         node = new_node();
108         //store_32(&q->nodes[node].value, val);
109         q->nodes[node].value = val;
110         tmp = atomic_load_explicit(&q->nodes[node].next, relaxed);
111         set_ptr(&tmp, 0); // NULL
112         // This is a found bug in AutoMO, and testcase4 can reveal this known bug
113         /**********    Detected KNOWN BUG (testcase4)    **********/
114         atomic_store_explicit(&q->nodes[node].next, tmp, release);
115
116         while (!success) {
117                 /**********    Detected UL    **********/
118                 tail = atomic_load_explicit(&q->tail, acquire);
119                 /**********    Detected Correctness (testcase4)    **********/
120                 next = atomic_load_explicit(&q->nodes[get_ptr(tail)].next, acquire);
121                 if (tail == atomic_load_explicit(&q->tail, relaxed)) {
122
123                         /* Check for uninitialized 'next' */
124                         //MODEL_ASSERT(get_ptr(next) != POISON_IDX);
125
126                         if (get_ptr(next) == 0) { // == NULL
127                                 pointer value = MAKE_POINTER(node, get_count(next) + 1);
128                                 /**********    Detected Correctness (testcase1)    **********/
129                                 success = atomic_compare_exchange_strong_explicit(&q->nodes[get_ptr(tail)].next,
130                                                 &next, value, release, release);
131                                 /** @OPClearDefine: success */
132                         }
133                         if (!success) {
134                                 /**********    Detected UL    **********/
135                                 unsigned int ptr = get_ptr(atomic_load_explicit(&q->nodes[get_ptr(tail)].next, acquire));
136                                 pointer value = MAKE_POINTER(ptr,
137                                                 get_count(tail) + 1);
138                                 /**********    Detected Correctness (testcase2)    **********/
139                                 atomic_compare_exchange_strong_explicit(&q->tail,
140                                                 &tail, value,
141                                                 release, release);
142                                 thrd_yield();
143                         }
144                 }
145         }
146         /**********    Detected Corrctness (testcase1) **********/
147         atomic_compare_exchange_strong_explicit(&q->tail,
148                         &tail,
149                         MAKE_POINTER(node, get_count(tail) + 1),
150                         release, release);
151 }
152
153 /** @Transition: S_RET = STATE(q)->empty() ? 0 : STATE(q)->front();
154 if (S_RET) STATE(q)->pop_front();
155 @JustifyingPostcondition: if (!C_RET)
156     return S_RET == C_RET;
157 @PostCondition: return C_RET ? *retVal  == S_RET : true;
158 @Print: model_print("\tDEQ #%d: C_RET=%d && *retVal=%d && S_RET=%d\n", ID,
159             C_RET, *retVal, S_RET);
160 */
161 int dequeue(queue_t *q, unsigned int *retVal, unsigned int *reclaimNode)
162 {
163         int success = 0;
164         pointer head;
165         pointer tail;
166         pointer next;
167
168         while (!success) {
169                 /**********    Dectected Correctness (testcase3)    **********/
170                 head = atomic_load_explicit(&q->head, acquire);
171                 /**********    Detected KNOWN BUG (testcase2)    **********/
172                 tail = atomic_load_explicit(&q->tail, acquire);
173                 /**********    Detected Correctness (testcase1) **********/
174                 next = atomic_load_explicit(&q->nodes[get_ptr(head)].next, acquire);
175                 /** @OPClearDefine: true */
176                 if (atomic_load_explicit(&q->head, relaxed) == head) {
177                         if (get_ptr(head) == get_ptr(tail)) {
178
179                                 /* Check for uninitialized 'next' */
180                                 MODEL_ASSERT(get_ptr(next) != POISON_IDX);
181
182                                 if (get_ptr(next) == 0) { // NULL
183                                         return false; // NULL
184                                 }
185                                 /**********    Detected UL    **********/
186                                 atomic_compare_exchange_strong_explicit(&q->tail,
187                                                 &tail,
188                                                 MAKE_POINTER(get_ptr(next), get_count(tail) + 1),
189                                                 release, release);
190                                 thrd_yield();
191                         } else {
192                                 //*retVal = load_32(&q->nodes[get_ptr(next)].value);
193                                 *retVal = q->nodes[get_ptr(next)].value;
194                                 /**********    Detected Correctness (testcase3)    **********/
195                                 success = atomic_compare_exchange_strong_explicit(&q->head,
196                                                 &head,
197                                                 MAKE_POINTER(get_ptr(next), get_count(head) + 1),
198                                                 release, release);
199                                 if (!success)
200                                         thrd_yield();
201                         }
202                 }
203         }
204         *reclaimNode = get_ptr(head);
205         reclaim(get_ptr(head));
206         return true;
207 }