fix ms-queue spec
[cdsspec-compiler.git] / benchmark / ms-queue / my_queue.c
1 #include <threads.h>
2 #include <stdlib.h>
3 #include "librace.h"
4 #include "model-assert.h"
5
6 #include "my_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 3 /* 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 /* Place this node index back on this thread's free list */
39 static void reclaim(unsigned int node)
40 {
41         int i;
42         int t = get_thread_num();
43
44         /* Don't reclaim NULL node */
45         MODEL_ASSERT(node);
46
47         for (i = 0; i < MAX_FREELIST; i++) {
48                 /* Should never race with our own thread here */
49                 unsigned int idx = load_32(&free_lists[t][i]);
50                 //unsigned int idx = free_lists[t][i];
51
52                 /* Found empty spot in free list */
53                 if (idx == 0) {
54                         store_32(&free_lists[t][i], node);
55                         //free_lists[t][i] = node;
56                         return;
57                 }
58         }
59         /* free list is full? */
60         MODEL_ASSERT(0);
61 }
62
63 void init_queue(queue_t *q, int num_threads)
64 {
65         int i, j;
66
67         /* Initialize each thread's free list with INITIAL_FREE pointers */
68         /* The actual nodes are initialized with poison indexes */
69         free_lists = malloc(num_threads * sizeof(*free_lists));
70         for (i = 0; i < num_threads; i++) {
71                 for (j = 0; j < INITIAL_FREE; j++) {
72                         free_lists[i][j] = 2 + i * MAX_FREELIST + j;
73                         atomic_init(&q->nodes[free_lists[i][j]].next, MAKE_POINTER(POISON_IDX, 0));
74                 }
75         }
76
77         /* initialize queue */
78         atomic_init(&q->head, MAKE_POINTER(1, 0));
79         atomic_init(&q->tail, MAKE_POINTER(1, 0));
80         atomic_init(&q->nodes[1].next, MAKE_POINTER(0, 0));
81 }
82
83 /**
84         @Begin
85         @Interface_define: Enqueue
86         @End
87 */
88 void enqueue(queue_t *q, unsigned int val)
89 {
90         int success = 0;
91         unsigned int node;
92         pointer tail;
93         pointer next;
94         pointer tmp;
95
96         node = new_node();
97         store_32(&q->nodes[node].value, val);
98         //q->nodes[node].value = val;
99         tmp = atomic_load_explicit(&q->nodes[node].next, relaxed);
100         set_ptr(&tmp, 0); // NULL
101         atomic_store_explicit(&q->nodes[node].next, tmp, relaxed);
102
103         while (!success) {
104                 /****FIXME: detected UL ****/
105                 tail = atomic_load_explicit(&q->tail, acquire);
106                 /****FIXME: miss ****/
107                 next = atomic_load_explicit(&q->nodes[get_ptr(tail)].next, acquire);
108                 printf("miss1_enqueue\n");
109                 if (tail == atomic_load_explicit(&q->tail, relaxed)) {
110
111                         /* Check for uninitialized 'next' */
112                         //MODEL_ASSERT(get_ptr(next) != POISON_IDX);
113
114                         if (get_ptr(next) == 0) { // == NULL
115                                 pointer value = MAKE_POINTER(node, get_count(next) + 1);
116                                 /****FIXME: first release UL ****/
117                                 // Second release can be just relaxed
118                                 success = atomic_compare_exchange_strong_explicit(&q->nodes[get_ptr(tail)].next,
119                                                 &next, value, release, relaxed);
120                                 /**
121                                         @Begin
122                                         @Commit_point_define_check: success == true
123                                         @Label: Enqueue_Success_Point
124                                         @End
125                                 */
126                         }
127                         if (!success) {
128                                 // This routine helps the other enqueue to update the tail
129                                 /****FIXME: detected UL ****/
130                                 unsigned int ptr = get_ptr(atomic_load_explicit(&q->nodes[get_ptr(tail)].next, acquire));
131                                 pointer value = MAKE_POINTER(ptr,
132                                                 get_count(tail) + 1);
133                                 /****FIXME: miss ****/
134                                 // Seconde release can be just relaxed
135                                 bool succ = false;
136                                 succ = atomic_compare_exchange_strong_explicit(&q->tail,
137                                                 &tail, value, release, relaxed);
138                                 if (succ) {
139                                         printf("miss2_enqueue CAS succ\n");
140                                 }
141                                 printf("miss2_enqueue\n");
142                                 thrd_yield();
143                         }
144                 }
145         }
146         /****FIXME: first UL ****/
147         // Second release can be just relaxed
148         atomic_compare_exchange_strong_explicit(&q->tail,
149                         &tail,
150                         MAKE_POINTER(node, get_count(tail) + 1),
151                         release, relaxed);
152 }
153
154 /**
155         @Begin
156         @Interface_define: Dequeue
157         @End
158 */
159 unsigned int dequeue(queue_t *q)
160 {
161         unsigned int value;
162         int success = 0;
163         pointer head;
164         pointer tail;
165         pointer next;
166
167         while (!success) {
168                 /****FIXME: detected correctness error ****/
169                 head = atomic_load_explicit(&q->head, acquire);
170                 tail = atomic_load_explicit(&q->tail, relaxed);
171                 /****FIXME: miss ****/
172                 next = atomic_load_explicit(&q->nodes[get_ptr(head)].next, acquire);
173                 printf("miss3_dequeue\n");
174                 if (atomic_load_explicit(&q->head, relaxed) == head) {
175                         if (get_ptr(head) == get_ptr(tail)) {
176
177                                 /* Check for uninitialized 'next' */
178                                 MODEL_ASSERT(get_ptr(next) != POISON_IDX);
179
180                                 if (get_ptr(next) == 0) { // NULL
181                                         /**
182                                                 @Begin
183                                                 @Commit_point_define_check: true
184                                                 @Label: Dequeue_Empty_Point
185                                                 @End
186                                         */
187                                         return 0; // NULL
188                                 }
189                                 /****FIXME: miss (not reached) ****/
190                                 // Seconde release can be just relaxed
191                                 bool succ = false;
192                                 succ = atomic_compare_exchange_strong_explicit(&q->tail,
193                                                 &tail,
194                                                 MAKE_POINTER(get_ptr(next), get_count(tail) + 1),
195                                                 release, relaxed);
196                                 if (succ) {
197                                         printf("miss4_dequeue CAS succ\n");
198                                 }
199                                 printf("miss4_dequeue\n");
200                                 thrd_yield();
201                         } else {
202                                 value = load_32(&q->nodes[get_ptr(next)].value);
203                                 //value = q->nodes[get_ptr(next)].value;
204                                 /****FIXME: correctness error ****/
205                                 // Seconde release can be just relaxed
206                                 success = atomic_compare_exchange_strong_explicit(&q->head,
207                                                 &head,
208                                                 MAKE_POINTER(get_ptr(next), get_count(head) + 1),
209                                                 release, relaxed);
210                                 /**
211                                         @Begin
212                                         @Commit_point_define_check: success == true
213                                         @Label: Dequeue_Success_Point
214                                         @End
215                                 */
216                                 if (!success)
217                                         thrd_yield();
218                         }
219                 }
220         }
221         reclaim(get_ptr(head));
222         return value;
223 }