33a62925c7a6a195fc0002cd54b2d3ce8807553a
[model-checker-benchmarks.git] / ms-queue / my_queue.c
1 #include <threads.h>
2 #include <stdlib.h>
3
4 #include "my_queue.h"
5
6 static unsigned int *node_nums;
7
8 static unsigned int new_node()
9 {
10         return node_nums[get_thread_num()];
11 }
12
13 static void reclaim(unsigned int node)
14 {
15         node_nums[get_thread_num()] = node;
16 }
17
18 void init_queue(queue_t *q, int num_threads)
19 {
20         unsigned int i;
21         pointer head;
22         pointer tail;
23         pointer next;
24
25         node_nums = malloc(num_threads * sizeof(*node_nums));
26         for (i = 0; i < num_threads; i++)
27                 node_nums[i] = 2 + i;
28
29         /* initialize queue */
30         head = MAKE_POINTER(1, 0);
31         tail = MAKE_POINTER(1, 0);
32         next = MAKE_POINTER(0, 0); // (NULL, 0)
33
34         atomic_init(&q->nodes[0].next, 0); // assumed inititalized in original example
35
36         atomic_store(&q->head, head);
37         atomic_store(&q->tail, tail);
38         atomic_store(&q->nodes[1].next, next);
39
40         /* initialize avail list */
41         for (i = 2; i < MAX_NODES; i++) {
42                 next = MAKE_POINTER(i + 1, 0);
43                 atomic_store(&q->nodes[i].next, next);
44         }
45
46         next = MAKE_POINTER(0, 0); // (NULL, 0)
47         atomic_store(&q->nodes[MAX_NODES].next, next);
48 }
49
50 void enqueue(queue_t *q, unsigned int val)
51 {
52         int success = 0;
53         unsigned int node;
54         pointer tail;
55         pointer next;
56         pointer tmp;
57
58         node = new_node();
59         q->nodes[node].value = val;
60         tmp = atomic_load(&q->nodes[node].next);
61         set_ptr(&tmp, 0); // NULL
62         atomic_store(&q->nodes[node].next, tmp);
63
64         while (!success) {
65                 tail = atomic_load(&q->tail);
66                 next = atomic_load(&q->nodes[get_ptr(tail)].next);
67                 if (tail == atomic_load(&q->tail)) {
68                         if (get_ptr(next) == 0) { // == NULL
69                                 pointer value = MAKE_POINTER(node, get_count(next) + 1);
70                                 success = atomic_compare_exchange_weak(&q->nodes[get_ptr(tail)].next,
71                                                 &next, value);
72                         }
73                         if (!success) {
74                                 unsigned int ptr = get_ptr(atomic_load(&q->nodes[get_ptr(tail)].next));
75                                 pointer value = MAKE_POINTER(ptr,
76                                                 get_count(tail) + 1);
77                                 atomic_compare_exchange_strong(&q->tail,
78                                                 &tail, value);
79                                 thrd_yield();
80                         }
81                 }
82         }
83         atomic_compare_exchange_strong(&q->tail,
84                         &tail,
85                         MAKE_POINTER(node, get_count(tail) + 1));
86 }
87
88 unsigned int dequeue(queue_t *q)
89 {
90         unsigned int value;
91         int success = 0;
92         pointer head;
93         pointer tail;
94         pointer next;
95
96         while (!success) {
97                 head = atomic_load(&q->head);
98                 tail = atomic_load(&q->tail);
99                 next = atomic_load(&q->nodes[get_ptr(head)].next);
100                 if (atomic_load(&q->head) == head) {
101                         if (get_ptr(head) == get_ptr(tail)) {
102                                 if (get_ptr(next) == 0) { // NULL
103                                         return 0; // NULL
104                                 }
105                                 atomic_compare_exchange_strong(&q->tail,
106                                                 &tail,
107                                                 MAKE_POINTER(get_ptr(next), get_count(tail) + 1));
108                                 thrd_yield();
109                         } else {
110                                 value = q->nodes[get_ptr(next)].value;
111                                 success = atomic_compare_exchange_weak(&q->head,
112                                                 &head,
113                                                 MAKE_POINTER(get_ptr(next), get_count(head) + 1));
114                                 if (!success)
115                                         thrd_yield();
116                         }
117                 }
118         }
119         reclaim(get_ptr(head));
120         return value;
121 }