Home | History | Annotate | Download | only in lit_tests
      1 // RUN: %clangxx_tsan -O1 %s -o %t && not %t 2>&1 | FileCheck %s
      2 #include <pthread.h>
      3 #include <stdio.h>
      4 #include <unistd.h>
      5 
      6 int Global;
      7 pthread_mutex_t mtx1;
      8 pthread_spinlock_t mtx2;
      9 pthread_rwlock_t mtx3;
     10 
     11 void *Thread1(void *x) {
     12   sleep(1);
     13   pthread_mutex_lock(&mtx1);
     14   Global++;
     15   pthread_mutex_unlock(&mtx1);
     16   return NULL;
     17 }
     18 
     19 void *Thread2(void *x) {
     20   pthread_mutex_lock(&mtx1);
     21   pthread_mutex_unlock(&mtx1);
     22   pthread_spin_lock(&mtx2);
     23   pthread_rwlock_rdlock(&mtx3);
     24   Global--;
     25   pthread_spin_unlock(&mtx2);
     26   pthread_rwlock_unlock(&mtx3);
     27   return NULL;
     28 }
     29 
     30 int main() {
     31   // CHECK: WARNING: ThreadSanitizer: data race
     32   // CHECK:   Write of size 4 at {{.*}} by thread T1
     33   // CHECK:                          (mutexes: write [[M1:M[0-9]+]]):
     34   // CHECK:   Previous write of size 4 at {{.*}} by thread T2
     35   // CHECK:               (mutexes: write [[M2:M[0-9]+]], read [[M3:M[0-9]+]]):
     36   // CHECK:   Mutex [[M1]] created at:
     37   // CHECK:     #1 main {{.*}}/mutexset6.cc:[[@LINE+5]]
     38   // CHECK:   Mutex [[M2]] created at:
     39   // CHECK:     #1 main {{.*}}/mutexset6.cc:[[@LINE+4]]
     40   // CHECK:   Mutex [[M3]] created at:
     41   // CHECK:     #1 main {{.*}}/mutexset6.cc:[[@LINE+3]]
     42   pthread_mutex_init(&mtx1, 0);
     43   pthread_spin_init(&mtx2, 0);
     44   pthread_rwlock_init(&mtx3, 0);
     45   pthread_t t[2];
     46   pthread_create(&t[0], NULL, Thread1, NULL);
     47   pthread_create(&t[1], NULL, Thread2, NULL);
     48   pthread_join(t[0], NULL);
     49   pthread_join(t[1], NULL);
     50   pthread_mutex_destroy(&mtx1);
     51   pthread_spin_destroy(&mtx2);
     52   pthread_rwlock_destroy(&mtx3);
     53 }
     54