my changes
authorBrian Demsky <bdemsky@uci.edu>
Fri, 8 Mar 2013 07:54:06 +0000 (23:54 -0800)
committerBrian Demsky <bdemsky@uci.edu>
Fri, 8 Mar 2013 07:54:06 +0000 (23:54 -0800)
ms-queue/my_queue.c

index d658407..c63f254 100644 (file)
@@ -61,6 +61,7 @@ void enqueue(queue_t *q, unsigned int val)
        pointer next;
        pointer tmp;
 
+       /* Get a fresh node, set its value field, then set it's next reference to null */
        node = new_node();
        store_32(&q->nodes[node].value, val);
        tmp = atomic_load_explicit(&q->nodes[node].next, relaxed);
@@ -68,29 +69,30 @@ void enqueue(queue_t *q, unsigned int val)
        atomic_store_explicit(&q->nodes[node].next, tmp, relaxed);
 
        while (!success) {
-               tail = atomic_load_explicit(&q->tail, acquire);
-               next = atomic_load_explicit(&q->nodes[get_ptr(tail)].next, acquire);
+               //tail isn't actually synchronization point
+               tail = atomic_load_explicit(&q->tail, relaxed);
+               //acquire reference
+               next = atomic_load_explicit(&q->nodes[get_ptr(tail)].next, relaxed);
                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_strong_explicit(&q->nodes[get_ptr(tail)].next,
-                                               &next, value, memory_order_acq_rel, memory_order_acq_rel);
+                                               &next, value, memory_order_release, memory_order_release);
                        }
                        if (!success) {
-                               unsigned int ptr = get_ptr(atomic_load_explicit(&q->nodes[get_ptr(tail)].next, memory_order_seq_cst));
+                               unsigned int ptr = get_ptr(atomic_load_explicit(&q->nodes[get_ptr(tail)].next, memory_order_relaxed));
                                pointer value = MAKE_POINTER(ptr,
                                                get_count(tail) + 1);
                                atomic_compare_exchange_strong_explicit(&q->tail,
                                                &tail, value,
-                                               memory_order_acq_rel, memory_order_acq_rel);
-                               thrd_yield();
+                                               memory_order_relaxed, memory_order_relaxed);
                        }
                }
        }
        atomic_compare_exchange_strong_explicit(&q->tail,
                        &tail,
                        MAKE_POINTER(node, get_count(tail) + 1),
-                       memory_order_acq_rel, memory_order_acq_rel);
+                       memory_order_relaxed, memory_order_relaxed);
 }
 
 unsigned int dequeue(queue_t *q)
@@ -102,8 +104,8 @@ unsigned int dequeue(queue_t *q)
        pointer next;
 
        while (!success) {
-               head = atomic_load_explicit(&q->head, acquire);
-               tail = atomic_load_explicit(&q->tail, acquire);
+               head = atomic_load_explicit(&q->head, relaxed);
+               tail = atomic_load_explicit(&q->tail, relaxed);
                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)) {
@@ -113,16 +115,13 @@ unsigned int dequeue(queue_t *q)
                                atomic_compare_exchange_strong_explicit(&q->tail,
                                                &tail,
                                                MAKE_POINTER(get_ptr(next), get_count(tail) + 1),
-                                               memory_order_acq_rel, memory_order_acq_rel);
-                               thrd_yield();
+                                               memory_order_relaxed, memory_order_relaxed);
                        } else {
                                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),
-                                               memory_order_acq_rel, memory_order_acq_rel);
-                               if (!success)
-                                       thrd_yield();
+                                               memory_order_relaxed, memory_order_relaxed);
                        }
                }
        }