Home | History | Annotate | Download | only in tsan
      1 // RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
      2 #include <pthread.h>
      3 #include <semaphore.h>
      4 #include <stdio.h>
      5 
      6 struct A {
      7   A() {
      8     sem_init(&sem_, 0, 0);
      9   }
     10   virtual void F() {
     11   }
     12   void Done() {
     13     sem_post(&sem_);
     14   }
     15   virtual ~A() {
     16   }
     17   sem_t sem_;
     18 };
     19 
     20 struct B : A {
     21   virtual void F() {
     22   }
     23   virtual ~B() {
     24     sem_wait(&sem_);
     25     sem_destroy(&sem_);
     26   }
     27 };
     28 
     29 static A *obj = new B;
     30 
     31 void *Thread1(void *x) {
     32   obj->F();
     33   obj->Done();
     34   return NULL;
     35 }
     36 
     37 void *Thread2(void *x) {
     38   delete obj;
     39   return NULL;
     40 }
     41 
     42 int main() {
     43   pthread_t t[2];
     44   pthread_create(&t[0], NULL, Thread1, NULL);
     45   pthread_create(&t[1], NULL, Thread2, NULL);
     46   pthread_join(t[0], NULL);
     47   pthread_join(t[1], NULL);
     48   fprintf(stderr, "PASS\n");
     49 }
     50 // CHECK: PASS
     51 // CHECK-NOT: WARNING: ThreadSanitizer: data race
     52