ms-queue: don't initialize node 0
[model-checker-benchmarks.git] / ms-queue / my_queue.c
index 2377d01a4679cad3a4bdcc3175aa01bfed26ccec..8a74060fb458e935b67faafc9322fdc4c3d445e2 100644 (file)
@@ -4,6 +4,10 @@
 
 #include "my_queue.h"
 
+#define relaxed memory_order_relaxed
+#define release memory_order_release
+#define acquire memory_order_acquire
+
 static unsigned int *node_nums;
 
 static unsigned int new_node()
@@ -27,9 +31,6 @@ void init_queue(queue_t *q, int num_threads)
        for (i = 0; i < num_threads; i++)
                node_nums[i] = 2 + i;
 
-       /* Note: needed to add this init manually */
-       atomic_init(&q->nodes[0].next, 0);
-
        /* initialize queue */
        head = MAKE_POINTER(1, 0);
        tail = MAKE_POINTER(1, 0);
@@ -59,32 +60,34 @@ void enqueue(queue_t *q, unsigned int val)
 
        node = new_node();
        store_32(&q->nodes[node].value, val);
-       tmp = atomic_load(&q->nodes[node].next);
+       tmp = atomic_load_explicit(&q->nodes[node].next, relaxed);
        set_ptr(&tmp, 0); // NULL
-       atomic_store(&q->nodes[node].next, tmp);
+       atomic_store_explicit(&q->nodes[node].next, tmp, relaxed);
 
        while (!success) {
-               tail = atomic_load(&q->tail);
-               next = atomic_load(&q->nodes[get_ptr(tail)].next);
-               if (tail == atomic_load(&q->tail)) {
+               tail = atomic_load_explicit(&q->tail, acquire);
+               next = atomic_load_explicit(&q->nodes[get_ptr(tail)].next, acquire);
+               if (tail == atomic_load_explicit(&q->tail, relaxed)) {
                        if (get_ptr(next) == 0) { // == NULL
                                pointer value = MAKE_POINTER(node, get_count(next) + 1);
-                               success = atomic_compare_exchange_weak(&q->nodes[get_ptr(tail)].next,
-                                               &next, value);
+                               success = atomic_compare_exchange_strong_explicit(&q->nodes[get_ptr(tail)].next,
+                                               &next, value, memory_order_acq_rel, memory_order_acq_rel);
                        }
                        if (!success) {
-                               unsigned int ptr = get_ptr(atomic_load(&q->nodes[get_ptr(tail)].next));
+                               unsigned int ptr = get_ptr(atomic_load_explicit(&q->nodes[get_ptr(tail)].next, memory_order_seq_cst));
                                pointer value = MAKE_POINTER(ptr,
                                                get_count(tail) + 1);
-                               atomic_compare_exchange_strong(&q->tail,
-                                               &tail, value);
+                               atomic_compare_exchange_strong_explicit(&q->tail,
+                                               &tail, value,
+                                               memory_order_acq_rel, memory_order_acq_rel);
                                thrd_yield();
                        }
                }
        }
-       atomic_compare_exchange_strong(&q->tail,
+       atomic_compare_exchange_strong_explicit(&q->tail,
                        &tail,
-                       MAKE_POINTER(node, get_count(tail) + 1));
+                       MAKE_POINTER(node, get_count(tail) + 1),
+                       memory_order_acq_rel, memory_order_acq_rel);
 }
 
 unsigned int dequeue(queue_t *q)
@@ -96,23 +99,25 @@ unsigned int dequeue(queue_t *q)
        pointer next;
 
        while (!success) {
-               head = atomic_load(&q->head);
-               tail = atomic_load(&q->tail);
-               next = atomic_load(&q->nodes[get_ptr(head)].next);
-               if (atomic_load(&q->head) == head) {
+               head = atomic_load_explicit(&q->head, acquire);
+               tail = atomic_load_explicit(&q->tail, acquire);
+               next = atomic_load_explicit(&q->nodes[get_ptr(head)].next, acquire);
+               if (atomic_load_explicit(&q->head, relaxed) == head) {
                        if (get_ptr(head) == get_ptr(tail)) {
                                if (get_ptr(next) == 0) { // NULL
                                        return 0; // NULL
                                }
-                               atomic_compare_exchange_strong(&q->tail,
+                               atomic_compare_exchange_strong_explicit(&q->tail,
                                                &tail,
-                                               MAKE_POINTER(get_ptr(next), get_count(tail) + 1));
+                                               MAKE_POINTER(get_ptr(next), get_count(tail) + 1),
+                                               memory_order_acq_rel, memory_order_acq_rel);
                                thrd_yield();
                        } else {
                                value = load_32(&q->nodes[get_ptr(next)].value);
-                               success = atomic_compare_exchange_weak(&q->head,
+                               success = atomic_compare_exchange_strong_explicit(&q->head,
                                                &head,
-                                               MAKE_POINTER(get_ptr(next), get_count(head) + 1));
+                                               MAKE_POINTER(get_ptr(next), get_count(head) + 1),
+                                               memory_order_acq_rel, memory_order_acq_rel);
                                if (!success)
                                        thrd_yield();
                        }