ms-queue: relax the initializations
[model-checker-benchmarks.git] / ms-queue / my_queue.c
index 9ffb343dd3174ec970da9685c2e203ad1c38f242..bb62fa781183c98f5c7727cdbb79114012749029 100644 (file)
@@ -1,5 +1,6 @@
 #include <threads.h>
 #include <stdlib.h>
+#include "librace.h"
 
 #include "my_queue.h"
 
@@ -31,20 +32,18 @@ void init_queue(queue_t *q, int num_threads)
        tail = MAKE_POINTER(1, 0);
        next = MAKE_POINTER(0, 0); // (NULL, 0)
 
-       atomic_init(&q->nodes[0].next, 0); // assumed inititalized in original example
-
-       atomic_store(&q->head, head);
-       atomic_store(&q->tail, tail);
-       atomic_store(&q->nodes[1].next, next);
+       atomic_init(&q->head, head);
+       atomic_init(&q->tail, tail);
+       atomic_init(&q->nodes[1].next, next);
 
        /* initialize avail list */
        for (i = 2; i < MAX_NODES; i++) {
                next = MAKE_POINTER(i + 1, 0);
-               atomic_store(&q->nodes[i].next, next);
+               atomic_init(&q->nodes[i].next, next);
        }
 
        next = MAKE_POINTER(0, 0); // (NULL, 0)
-       atomic_store(&q->nodes[MAX_NODES].next, next);
+       atomic_init(&q->nodes[MAX_NODES].next, next);
 }
 
 void enqueue(queue_t *q, unsigned int val)
@@ -56,7 +55,7 @@ void enqueue(queue_t *q, unsigned int val)
        pointer tmp;
 
        node = new_node();
-       q->nodes[node].value = val;
+       store_32(&q->nodes[node].value, val);
        tmp = atomic_load(&q->nodes[node].next);
        set_ptr(&tmp, 0); // NULL
        atomic_store(&q->nodes[node].next, tmp);
@@ -66,18 +65,16 @@ void enqueue(queue_t *q, unsigned int val)
                next = atomic_load(&q->nodes[get_ptr(tail)].next);
                if (tail == atomic_load(&q->tail)) {
                        if (get_ptr(next) == 0) { // == NULL
-                               pointer val = MAKE_POINTER(node, get_count(next) + 1);
+                               pointer value = MAKE_POINTER(node, get_count(next) + 1);
                                success = atomic_compare_exchange_weak(&q->nodes[get_ptr(tail)].next,
-                                               &next,
-                                               val);
+                                               &next, value);
                        }
                        if (!success) {
                                unsigned int ptr = get_ptr(atomic_load(&q->nodes[get_ptr(tail)].next));
-                               pointer val = MAKE_POINTER(ptr,
+                               pointer value = MAKE_POINTER(ptr,
                                                get_count(tail) + 1);
                                atomic_compare_exchange_strong(&q->tail,
-                                               &tail,
-                                               val);
+                                               &tail, value);
                                thrd_yield();
                        }
                }
@@ -104,12 +101,12 @@ unsigned int dequeue(queue_t *q)
                                if (get_ptr(next) == 0) { // NULL
                                        return 0; // NULL
                                }
-                               atomic_compare_exchange_weak(&q->tail,
+                               atomic_compare_exchange_strong(&q->tail,
                                                &tail,
                                                MAKE_POINTER(get_ptr(next), get_count(tail) + 1));
                                thrd_yield();
                        } else {
-                               value = q->nodes[get_ptr(next)].value;
+                               value = load_32(&q->nodes[get_ptr(next)].value);
                                success = atomic_compare_exchange_weak(&q->head,
                                                &head,
                                                MAKE_POINTER(get_ptr(next), get_count(head) + 1));