2018-11-07 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / testsuite / g++.dg / tsan / vptr_harmful_race.C
blob1473f93ed24ccaefd046061ef353caf12fc0d0b5
1 /* { dg-shouldfail "tsan" } */
2 /* { dg-additional-options "-ldl" } */
4 #include <pthread.h>
5 #include <semaphore.h>
6 #include <stdio.h>
7 #include <unistd.h>
8 #include "tsan_barrier.h"
10 static pthread_barrier_t barrier;
12 struct A {
13   A() {
14     sem_init(&sem_, 0, 0);
15   }
16   virtual void F() {
17   }
18   void Done() {
19     sem_post(&sem_);
20   }
21   virtual ~A() {
22     sem_wait(&sem_);
23     sem_destroy(&sem_);
24   }
25   sem_t sem_;
28 struct B : A {
29   virtual void F() {
30   }
31   virtual ~B() { }
34 static A *obj = new B;
36 void *Thread1(void *x) {
37   obj->F();
38   obj->Done();
39   barrier_wait(&barrier);
40   return NULL;
43 void *Thread2(void *x) {
44   barrier_wait(&barrier);
45   delete obj;
46   return NULL;
49 int main() {
50   barrier_init(&barrier, 2);
51   pthread_t t[2];
52   pthread_create(&t[0], NULL, Thread1, NULL);
53   pthread_create(&t[1], NULL, Thread2, NULL);
54   pthread_join(t[0], NULL);
55   pthread_join(t[1], NULL);
58 /* { dg-output "WARNING: ThreadSanitizer: data race on vptr.*(\n|\r\n|\r)" } */