injection result for ms-queue
[cdsspec-compiler.git] / benchmark / linuxrwlocks / linuxrwlocks.c
index c7ef94c0e739d20681a0b077b63e5a0c0eb902c3..0a1a7b203f02f6477623c5ed8f64765ca341a10a 100644 (file)
@@ -7,6 +7,7 @@
 #include <cdsannotate.h>
 #include <specannotation.h>
 #include <model_memory.h>
+#include "common.h"
 
 #include "librace.h"
 
@@ -60,6 +61,11 @@ typedef union {
        @Happens_before:
                # Since commit_point_set has no ID attached, A -> B means that for any B,
                # the previous A happens before B.
+               Read_Unlock -> Read_Lock
+               Read_Unlock -> Write_Lock
+               Read_Unlock -> Write_Trylock(HB_Write_Trylock_Succ)
+               Read_Unlock -> Read_Trylock(HB_Read_Trylock_Succ)
+
                Write_Unlock -> Write_Lock
                Write_Unlock -> Write_Trylock(HB_Write_Trylock_Succ)
 
@@ -201,20 +207,17 @@ static inline int read_trylock(rwlock_t *rw)
        @Interface: Write_Trylock
        @Commit_point_set:
                Write_Trylock_Succ_Point | Write_Trylock_Fail_Point
-       @Condition:
-               !writer_lock_acquired && reader_lock_cnt == 0
        @HB_condition:
                HB_Write_Trylock_Succ :: __RET__ == 1
        @Action:
-               if (__COND_SAT__)
+               if (__RET__ == 1)
                        writer_lock_acquired = true;
-       @Post_check:
-               __COND_SAT__ ? __RET__ == 1 : __RET__ == 0
        @End
 */
 static inline int write_trylock(rwlock_t *rw)
 {
        int priorvalue = atomic_fetch_sub_explicit(&rw->lock, RW_LOCK_BIAS, memory_order_acquire);
+       //model_print("write_trylock: priorvalue: %d\n", priorvalue);
        /**
                @Begin
                @Potential_commit_point_define: true
@@ -293,16 +296,36 @@ static void a(void *obj)
        for(i = 0; i < 2; i++) {
                if ((i % 2) == 0) {
                        read_lock(&mylock);
-                       load_32(&shareddata);
+                       //load_32(&shareddata);
+                       printf("%d\n", shareddata);
                        read_unlock(&mylock);
                } else {
                        write_lock(&mylock);
-                       store_32(&shareddata,(unsigned int)i);
+                       //store_32(&shareddata,(unsigned int)i);
+                       shareddata = 47;
                        write_unlock(&mylock);
                }
        }
 }
 
+static void b(void *obj)
+{
+       int i;
+       for(i = 0; i < 2; i++) {
+               if ((i % 2) == 0) {
+                       if (read_trylock(&mylock) == 1) {
+                               printf("%d\n", shareddata);
+                               read_unlock(&mylock);
+                       }
+               } else {
+                       if (write_trylock(&mylock) == 1) {
+                               shareddata = 47;
+                               write_unlock(&mylock);
+                       }
+               }
+       }
+}
+
 int user_main(int argc, char **argv)
 {
        /**
@@ -314,7 +337,7 @@ int user_main(int argc, char **argv)
        atomic_init(&mylock.lock, RW_LOCK_BIAS);
 
        thrd_create(&t1, (thrd_start_t)&a, NULL);
-       thrd_create(&t2, (thrd_start_t)&a, NULL);
+       thrd_create(&t2, (thrd_start_t)&b, NULL);
 
        thrd_join(t1);
        thrd_join(t2);