Home | History | Annotate | Download | only in tsan
      1 // RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
      2 #include <pthread.h>
      3 #include <unistd.h>
      4 #include <stdio.h>
      5 
      6 const int kTestCount = 4;
      7 typedef long long T;
      8 T atomics[kTestCount * 2];
      9 
     10 void Test(int test, T *p, bool main_thread) {
     11   volatile T sink;
     12   if (test == 0) {
     13     if (main_thread)
     14       __atomic_fetch_add(p, 1, __ATOMIC_RELAXED);
     15     else
     16       *p = 42;
     17   } else if (test == 1) {
     18     if (main_thread)
     19       __atomic_fetch_add(p, 1, __ATOMIC_RELAXED);
     20     else
     21       sink = *p;
     22   } else if (test == 2) {
     23     if (main_thread)
     24       sink = __atomic_load_n(p, __ATOMIC_SEQ_CST);
     25     else
     26       *p = 42;
     27   } else if (test == 3) {
     28     if (main_thread)
     29       __atomic_store_n(p, 1, __ATOMIC_SEQ_CST);
     30     else
     31       sink = *p;
     32   }
     33 }
     34 
     35 void *Thread(void *p) {
     36   for (int i = 0; i < kTestCount; i++) {
     37     Test(i, &atomics[i], false);
     38   }
     39   sleep(2);
     40   for (int i = 0; i < kTestCount; i++) {
     41     fprintf(stderr, "Test %d reverse\n", i);
     42     Test(i, &atomics[kTestCount + i], false);
     43   }
     44   return 0;
     45 }
     46 
     47 int main() {
     48   pthread_t t;
     49   pthread_create(&t, 0, Thread, 0);
     50   sleep(1);
     51   for (int i = 0; i < kTestCount; i++) {
     52     fprintf(stderr, "Test %d\n", i);
     53     Test(i, &atomics[i], true);
     54   }
     55   for (int i = 0; i < kTestCount; i++) {
     56     Test(i, &atomics[kTestCount + i], true);
     57   }
     58   pthread_join(t, 0);
     59 }
     60 
     61 // CHECK: Test 0
     62 // CHECK: ThreadSanitizer: data race
     63 // CHECK-NOT: SUMMARY{{.*}}tsan_interface_atomic
     64 // CHECK: Test 1
     65 // CHECK: ThreadSanitizer: data race
     66 // CHECK-NOT: SUMMARY{{.*}}tsan_interface_atomic
     67 // CHECK: Test 2
     68 // CHECK: ThreadSanitizer: data race
     69 // CHECK-NOT: SUMMARY{{.*}}tsan_interface_atomic
     70 // CHECK: Test 3
     71 // CHECK: ThreadSanitizer: data race
     72 // CHECK-NOT: SUMMARY{{.*}}tsan_interface_atomic
     73 // CHECK: Test 0 reverse
     74 // CHECK: ThreadSanitizer: data race
     75 // CHECK: Test 1 reverse
     76 // CHECK: ThreadSanitizer: data race
     77 // CHECK: Test 2 reverse
     78 // CHECK: ThreadSanitizer: data race
     79 // CHECK: Test 3 reverse
     80 // CHECK: ThreadSanitizer: data race
     81