edits
[cdsspec-compiler.git] / benchmark / ms-queue / my_queue.c
index 186745eaec079251e7e49ce57b9cfe68def3db1a..145c91d30dcd8723ceb5da0c57918ee3a711d097 100644 (file)
@@ -10,7 +10,7 @@
 #define acquire memory_order_acquire
 
 #define MAX_FREELIST 4 /* Each thread can own up to MAX_FREELIST free nodes */
-#define INITIAL_FREE 2 /* Each thread starts with INITIAL_FREE free nodes */
+#define INITIAL_FREE 3 /* Each thread starts with INITIAL_FREE free nodes */
 
 #define POISON_IDX 0x666
 
@@ -31,7 +31,7 @@ static unsigned int new_node()
                }
        }
        /* free_list is empty? */
-       MODEL_ASSERT(0);
+       //MODEL_ASSERT(0);
        return 0;
 }
 
@@ -42,7 +42,7 @@ static void reclaim(unsigned int node)
        int t = get_thread_num();
 
        /* Don't reclaim NULL node */
-       MODEL_ASSERT(node);
+       //MODEL_ASSERT(node);
 
        for (i = 0; i < MAX_FREELIST; i++) {
                /* Should never race with our own thread here */
@@ -57,12 +57,15 @@ static void reclaim(unsigned int node)
                }
        }
        /* free list is full? */
-       MODEL_ASSERT(0);
+       //MODEL_ASSERT(0);
 }
 
 void init_queue(queue_t *q, int num_threads)
 {
        int i, j;
+       for (i = 0; i < MAX_NODES; i++) {
+               atomic_init(&q->nodes[i].next, MAKE_POINTER(POISON_IDX, 0));
+       }
 
        /* Initialize each thread's free list with INITIAL_FREE pointers */
        /* The actual nodes are initialized with poison indexes */
@@ -101,11 +104,18 @@ void enqueue(queue_t *q, unsigned int val)
        atomic_store_explicit(&q->nodes[node].next, tmp, relaxed);
 
        while (!success) {
-               /****FIXME: detected UL ****/
+               
+               /**
+                       @Begin
+                       @Commit_point_clear: true
+                       @Label: Enqueue_Clear
+                       @End
+               */
+               /**** detected UL ****/
                tail = atomic_load_explicit(&q->tail, acquire);
                /****FIXME: miss ****/
                next = atomic_load_explicit(&q->nodes[get_ptr(tail)].next, acquire);
-               printf("miss1_enqueue\n");
+               //printf("miss1_enqueue\n");
                if (tail == atomic_load_explicit(&q->tail, relaxed)) {
 
                        /* Check for uninitialized 'next' */
@@ -113,34 +123,39 @@ void enqueue(queue_t *q, unsigned int val)
 
                        if (get_ptr(next) == 0) { // == NULL
                                pointer value = MAKE_POINTER(node, get_count(next) + 1);
-                               /****FIXME: first release UL ****/
+                               /**** detected UL ****/
                                // Second release can be just relaxed
                                success = atomic_compare_exchange_strong_explicit(&q->nodes[get_ptr(tail)].next,
                                                &next, value, release, relaxed);
                                /**
                                        @Begin
-                                       @Commit_point_define_check: success == true
-                                       @Label: Enqueue_Success_Point
+                                       @Commit_point_define_check: success
+                                       @Label: EnqueueUpdateNext
                                        @End
                                */
                        }
                        if (!success) {
-                               /****FIXME: detected UL ****/
+                               // This routine helps the other enqueue to update the tail
+                               /**** detected UL ****/
                                unsigned int ptr = get_ptr(atomic_load_explicit(&q->nodes[get_ptr(tail)].next, acquire));
                                pointer value = MAKE_POINTER(ptr,
                                                get_count(tail) + 1);
                                /****FIXME: miss ****/
-                               // Seconde release can be just relaxed
-                               atomic_compare_exchange_strong_explicit(&q->tail,
+                               // Second release can be just relaxed
+                               bool succ = false;
+                               succ = atomic_compare_exchange_strong_explicit(&q->tail,
                                                &tail, value, release, relaxed);
-                               printf("miss2_enqueue\n");
+                               if (succ) {
+                                       //printf("miss2_enqueue CAS succ\n");
+                               }
+                               //printf("miss2_enqueue\n");
                                thrd_yield();
                        }
                }
        }
-       /****FIXME: first UL ****/
-       // Seconde release can be just relaxed
-       atomic_compare_exchange_strong_explicit(&q->tail,
+       /**** dectected UL ****/
+       // Second release can be just relaxed
+       bool succ = atomic_compare_exchange_strong_explicit(&q->tail,
                        &tail,
                        MAKE_POINTER(node, get_count(tail) + 1),
                        release, relaxed);
@@ -151,57 +166,78 @@ void enqueue(queue_t *q, unsigned int val)
        @Interface_define: Dequeue
        @End
 */
-unsigned int dequeue(queue_t *q)
+bool dequeue(queue_t *q, int *retVal)
 {
-       unsigned int value;
+       unsigned int value = 0;
        int success = 0;
        pointer head;
        pointer tail;
        pointer next;
 
        while (!success) {
-               /****FIXME: detected correctness error ****/
+               /**
+                       @Begin
+                       @Commit_point_clear: true
+                       @Label: Dequeue_Clear
+                       @End
+               */
+               /**** detected correctness error ****/
                head = atomic_load_explicit(&q->head, acquire);
-               tail = atomic_load_explicit(&q->tail, relaxed);
-               /****FIXME: miss ****/
+               /** A new bug has been found here!!! It should be acquire instead of
+                * relaxed (it introduces a bug when there's two dequeuers and one
+                * enqueuer) correctness bug!!
+                */
+               tail = atomic_load_explicit(&q->tail, acquire);
+
+               /**** Detected UL/DR (testcase1.c) ****/
                next = atomic_load_explicit(&q->nodes[get_ptr(head)].next, acquire);
-               printf("miss3_dequeue\n");
+               /**
+                       @Begin
+                       @Potential_commit_point_define: true
+                       @Label: DequeueReadNext
+                       @End
+               */
                if (atomic_load_explicit(&q->head, relaxed) == head) {
                        if (get_ptr(head) == get_ptr(tail)) {
 
                                /* Check for uninitialized 'next' */
-                               MODEL_ASSERT(get_ptr(next) != POISON_IDX);
-
-                               if (get_ptr(next) == 0) { // NULL
-                                       /**
-                                               @Begin
-                                               @Commit_point_define_check: true
-                                               @Label: Dequeue_Empty_Point
-                                               @End
-                                       */
-                                       return 0; // NULL
+                               //MODEL_ASSERT(get_ptr(next) != POISON_IDX);
+
+                               if (get_ptr(next) == 0) { // NULL       
+                                       return false; // NULL
                                }
-                               /****FIXME: miss (not reached) ****/
-                               // Seconde release can be just relaxed
-                               atomic_compare_exchange_strong_explicit(&q->tail,
+                               /**** Detected UL (testcase1.c) ****/
+                               // Second release can be just relaxed
+                               bool succ = false;
+                               succ = atomic_compare_exchange_strong_explicit(&q->tail,
                                                &tail,
                                                MAKE_POINTER(get_ptr(next), get_count(tail) + 1),
                                                release, relaxed);
-                               printf("miss4_dequeue\n");
+                               if (succ) {
+                                       //printf("miss4_dequeue CAS succ\n");
+                               }
+                               //printf("miss4_dequeue\n");
                                thrd_yield();
                        } else {
                                value = load_32(&q->nodes[get_ptr(next)].value);
                                //value = q->nodes[get_ptr(next)].value;
-                               /****FIXME: correctness error ****/
-                               // Seconde release can be just relaxed
+                               /**** correctness error ****/
                                success = atomic_compare_exchange_strong_explicit(&q->head,
                                                &head,
                                                MAKE_POINTER(get_ptr(next), get_count(head) + 1),
                                                release, relaxed);
                                /**
                                        @Begin
-                                       @Commit_point_define_check: success == true
-                                       @Label: Dequeue_Success_Point
+                                       @Commit_point_define_check: success
+                                       @Label: DequeueUpdateHead
+                                       @End
+                               */
+
+                               /**
+                                       @Begin
+                                       @Commit_point_define: success
+                                       @Potential_commit_point_label: DequeueReadNext
+                                       @Label: DequeueReadNextVerify
                                        @End
                                */
                                if (!success)
@@ -210,5 +246,6 @@ unsigned int dequeue(queue_t *q)
                }
        }
        reclaim(get_ptr(head));
-       return value;
+       *retVal = value;
+       return true;
 }