Debian Codegraph @

gcc/testsuite/g++.dg/tsan/vptr_harmful_race.C

raw
1 /* { dg-shouldfail "tsan" } */
2 /* { dg-additional-options "-ldl" } */
3
4 #include <pthread.h>
5 #include <semaphore.h>
6 #include <stdio.h>
7 #include <unistd.h>
8 #include "tsan_barrier.h"
9
10 static pthread_barrier_t barrier;
11
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_;
26 };
27
28 struct B : A {
29 virtual void F() {
30 }
31 virtual ~B() { }
32 };
33
34 static A *obj = new B;
35
36 void *Thread1(void *x) {
37 obj->F();
38 obj->Done();
39 barrier_wait(&barrier);
40 return NULL;
41 }
42
43 void *Thread2(void *x) {
44 barrier_wait(&barrier);
45 delete obj;
46 return NULL;
47 }
48
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);
56 }
57
58 /* { dg-output "WARNING: ThreadSanitizer: data race on vptr.*(\n|\r\n|\r)" } */