ms-queue: relax the second load of head/tail
[model-checker-benchmarks.git] / ms-queue / my_queue.c
index 8fedd9c086379fd9ac7ca263f50837da528a08c3..d65840797b34e88402d78fe1b4b944533a2873dc 100644 (file)
-#include "main.h"
+#include <threads.h>
+#include <stdlib.h>
+#include "librace.h"
 
-extern unsigned int iterations;
-extern private_t private;
-extern shared_mem_t *smp;
+#include "my_queue.h"
 
-void init_private(int pid)
-{
-       private.node = 2 + pid;
-       private.value = 1 + (pid * iterations);
-
-}
+#define relaxed memory_order_relaxed
+#define release memory_order_release
+#define acquire memory_order_acquire
 
-void init_memory()
-{
-}
+static unsigned int *node_nums;
 
 static unsigned int new_node()
 {
-       return private.node;
+       return node_nums[get_thread_num()];
 }
 
 static void reclaim(unsigned int node)
 {
-       private.node = node;
+       node_nums[get_thread_num()] = node;
 }
 
-void init_queue()
+void init_queue(queue_t *q, int num_threads)
 {
        unsigned int i;
        pointer head;
        pointer tail;
        pointer next;
 
+       node_nums = malloc(num_threads * sizeof(*node_nums));
+       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);
        next = MAKE_POINTER(0, 0); // (NULL, 0)
 
-       atomic_init(&smp->nodes[0].next, 0); // assumed inititalized in original example
-
-       atomic_store(&smp->head, head);
-       atomic_store(&smp->tail, tail);
-       atomic_store(&smp->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(&smp->nodes[i].next, next);
+               atomic_init(&q->nodes[i].next, next);
        }
 
        next = MAKE_POINTER(0, 0); // (NULL, 0)
-       atomic_store(&smp->nodes[MAX_NODES].next, next);
+       atomic_init(&q->nodes[MAX_NODES].next, next);
 }
 
-void enqueue(unsigned int val)
+void enqueue(queue_t *q, unsigned int val)
 {
-       unsigned int success = 0;
+       int success = 0;
        unsigned int node;
        pointer tail;
        pointer next;
        pointer tmp;
 
        node = new_node();
-       smp->nodes[node].value = val;
-       tmp = atomic_load(&smp->nodes[node].next);
+       store_32(&q->nodes[node].value, val);
+       tmp = atomic_load_explicit(&q->nodes[node].next, relaxed);
        set_ptr(&tmp, 0); // NULL
-       atomic_store(&smp->nodes[node].next, tmp);
+       atomic_store_explicit(&q->nodes[node].next, tmp, relaxed);
 
        while (!success) {
-               tail = atomic_load(&smp->tail);
-               next = atomic_load(&smp->nodes[get_ptr(tail)].next);
-               if (tail == atomic_load(&smp->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 val = MAKE_POINTER(node, get_count(next) + 1);
-                               success = atomic_compare_exchange_weak(&smp->nodes[get_ptr(tail)].next,
-                                               &next,
-                                               val);
+                               pointer value = MAKE_POINTER(node, get_count(next) + 1);
+                               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(&smp->nodes[get_ptr(tail)].next));
-                               pointer val = MAKE_POINTER(ptr,
+                               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(&smp->tail,
-                                               &tail,
-                                               val);
+                               atomic_compare_exchange_strong_explicit(&q->tail,
+                                               &tail, value,
+                                               memory_order_acq_rel, memory_order_acq_rel);
                                thrd_yield();
                        }
                }
        }
-       atomic_compare_exchange_strong(&smp->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()
+unsigned int dequeue(queue_t *q)
 {
        unsigned int value;
-       unsigned int success;
+       int success = 0;
        pointer head;
        pointer tail;
        pointer next;
 
-       for (success = FALSE; success == FALSE; ) {
-               head = atomic_load(&smp->head);
-               tail = atomic_load(&smp->tail);
-               next = atomic_load(&smp->nodes[get_ptr(head)].next);
-               if (atomic_load(&smp->head) == head) {
+       while (!success) {
+               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_weak(&smp->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 = smp->nodes[get_ptr(next)].value;
-                               success = atomic_compare_exchange_weak(&smp->head,
+                               value = load_32(&q->nodes[get_ptr(next)].value);
+                               success = atomic_compare_exchange_strong_explicit(&q->head,
                                                &head,
-                                               MAKE_POINTER(get_ptr(next), get_count(head) + 1));
-                               if (success == FALSE) {
+                                               MAKE_POINTER(get_ptr(next), get_count(head) + 1),
+                                               memory_order_acq_rel, memory_order_acq_rel);
+                               if (!success)
                                        thrd_yield();
-                               }
                        }
                }
        }