edits
[cdsspec-compiler.git] / benchmark / chase-lev-deque-bugfix / deque.c
index 3ad32afb74700a602a70632b690148ccb86de0d3..bb12be0b09a222cfd2bbd1cca1fa0e329fa3a39a 100644 (file)
@@ -25,35 +25,30 @@ int take(Deque *q) {
        /**
                @Begin
                @Commit_point_define_check: true
-               @Label: Take_Read_Bottom
+               @Label: TakeReadBottom
                @End
        */
        Array *a = (Array *) atomic_load_explicit(&q->array, memory_order_relaxed);
-       /**
-               //@Begin
-               @Commit_point_define_check: true
-               @Label: Take_Read_Array
-               @End
-       */
        atomic_store_explicit(&q->bottom, b, memory_order_relaxed);
        /**** SPEC (sequential) (testcase1.c) ****/
        atomic_thread_fence(memory_order_seq_cst);
        size_t t = atomic_load_explicit(&q->top, memory_order_relaxed);
-       /**
-               //@Begin
-               @Commit_point_define_check: t != b
-               @Label: Take_Read_Top
-               @End
-       */
        int x;
        if (t <= b) {
+               /**
+                       @Begin
+                       @Commit_point_clear: true
+                       @Label: TakeClear1
+                       @End
+               */
+
                /* Non-empty queue. */
                int size = atomic_load_explicit(&a->size,memory_order_relaxed);
                x = atomic_load_explicit(&a->buffer[b % size], memory_order_relaxed);
                /**
-                       //@Begin
-                       @Commit_point_define_check: t != b
-                       @Label: Take_Read_Buffer
+                       @Begin
+                       @Commit_point_define_check: true 
+                       @Label: TakeReadBuffer
                        @End
                */
                if (t == b) {
@@ -61,20 +56,21 @@ int take(Deque *q) {
                        //FIXME: weaken the following seq_cst causes no spec problem
                        bool succ = atomic_compare_exchange_strong_explicit(&q->top, &t, t +
                                1, memory_order_seq_cst, memory_order_relaxed);
-                       /**
-                               //@Begin
-                               @Commit_point_define_check: succ 
-                               @Label: Take_CAS_Top
-                               @End
-                       */
-
-                       /**
-                               @Begin
-                               @Additional_ordering_point_define_check: true
-                               @Label: Take_Additional_Point
-                               @End
-                       */
                        if (!succ) {
+                               /**
+                                       @Begin
+                                       @Commit_point_clear: true
+                                       @Label: TakeClear2
+                                       @End
+                               */
+
+                               /**
+                                       @Begin
+                                       @Commit_point_define_check: true
+                                       @Label: TakeReadTop
+                                       @End
+                               */
+
                                /* Failed race. */
                                x = EMPTY;
                        }
@@ -96,6 +92,14 @@ void resize(Deque *q) {
        size_t bottom=atomic_load_explicit(&q->bottom, memory_order_relaxed);
        atomic_store_explicit(&new_a->size, new_size, memory_order_relaxed);
        size_t i;
+
+       // Initialize the whole new array to turn off the CDSChecker UL error
+       // Check if CDSSpec checker can catch this bug
+       /*
+       for(i=0; i < new_size; i++) {
+               atomic_store_explicit(&new_a->buffer[i % new_size], atomic_load_explicit(&a->buffer[i % size], memory_order_relaxed), memory_order_relaxed);
+       }
+       */
        for(i=top; i < bottom; i++) {
                atomic_store_explicit(&new_a->buffer[i % new_size], atomic_load_explicit(&a->buffer[i % size], memory_order_relaxed), memory_order_relaxed);
        }
@@ -119,32 +123,18 @@ void push(Deque *q, int x) {
                // CDSSpec can actually detect the same bug if we avoid the UL error
                //Bug in paper...should have next line...
                a = (Array *) atomic_load_explicit(&q->array, memory_order_relaxed);
-               /**
-                       //@Begin
-                       @Commit_point_define_check: true
-                       @Label: Push_Read_Array
-                       @End
-               */
        }
        int size = atomic_load_explicit(&a->size, memory_order_relaxed);
 
        atomic_store_explicit(&a->buffer[b % size], x, memory_order_relaxed);
-       /**
-               //@Begin
-               @Commit_point_define_check: true
-               @Label: Push_Update_Buffer
-               @End
-       */
-       /**** SPEC (Sync) (run with -u100 to avoid the uninitialized bug) ****/
-       atomic_thread_fence(memory_order_release);
-       // Watch out!!! We need to specifiy the fence as the commit point because
-       // the synchronization in C11 actually is established at the fence.
        /**
                @Begin
                @Commit_point_define_check: true
-               @Label: Push_Update_Bottom
+               @Label: PushUpdateBuffer
                @End
        */
+       /**** UL & SPEC (Sync) (run with -u100 to avoid the uninitialized bug) ****/
+       atomic_thread_fence(memory_order_release);
        atomic_store_explicit(&q->bottom, b + 1, memory_order_relaxed);
        
 }
@@ -158,33 +148,55 @@ int steal(Deque *q) {
        //Watch out: actually on need to be an acquire (don't count it)
        // An old bug
        size_t t = atomic_load_explicit(&q->top, memory_order_acquire);
-       //FIXME: remove the fence causes no error and fewer executions..
-       atomic_thread_fence(memory_order_seq_cst);
-       /**** SPEC & UL ****/
-       size_t b = atomic_load_explicit(&q->bottom, memory_order_acquire);
        /**
                @Begin
-               @Commit_point_define_check: true 
-               @Label: Steal_Read_Bottom
+               @Commit_point_define_check: true
+               @Label: StealReadTop1
                @End
        */
+       /********** SPEC error (testcase3.c) **********/
+       atomic_thread_fence(memory_order_seq_cst);
+       /**** SPEC & UL ****/
+       size_t b = atomic_load_explicit(&q->bottom, memory_order_acquire);
+       
        int x = EMPTY;
        if (t < b) {
+               /**
+                       @Begin
+                       @Commit_point_clear: true
+                       @Label: StealClear1
+                       @End
+               */
+
                /* Non-empty queue. */
                /**** detected UL ****/
                Array *a = (Array *) atomic_load_explicit(&q->array, memory_order_acquire);
                int size = atomic_load_explicit(&a->size, memory_order_relaxed);
                x = atomic_load_explicit(&a->buffer[t % size], memory_order_relaxed);
-               /**** SPEC (sequential) ****/ 
-               bool succ = atomic_compare_exchange_strong_explicit(&q->top, &t, t + 1,
-                       memory_order_seq_cst, memory_order_relaxed);
                /**
                        @Begin
-                       @Additional_ordering_point_define_check: true 
-                       @Label: Steal_Additional_Point
+                       @Commit_point_define_check: true
+                       @Label: StealReadBuffer
                        @End
                */
+               /**** SPEC (sequential) ****/ 
+               bool succ = atomic_compare_exchange_strong_explicit(&q->top, &t, t + 1,
+                       memory_order_seq_cst, memory_order_relaxed);
                if (!succ) {
+                       /**
+                               @Begin
+                               @Commit_point_clear: true
+                               @Label: StealClear2
+                               @End
+                       */
+
+                       /**
+                               @Begin
+                               @Commit_point_define_check: true
+                               @Label: StealReadTop2
+                               @End
+                       */
+
                        /* Failed race. */
                        return ABORT;
                }