blob: c0068955129f9c1c3de2d94a05bb1336f15bc4b9 [file] [log] [blame]
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001// RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2#include <pthread.h>
Stephen Hines2d1fdb22014-05-28 23:58:16 -07003#include <stdio.h>
4
5struct A {
6 A() {
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08007 pthread_mutex_init(&m, 0);
8 pthread_cond_init(&c, 0);
9 signaled = false;
Stephen Hines2d1fdb22014-05-28 23:58:16 -070010 }
11 virtual void F() {
12 }
13 void Done() {
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080014 pthread_mutex_lock(&m);
15 signaled = true;
16 pthread_cond_signal(&c);
17 pthread_mutex_unlock(&m);
Stephen Hines2d1fdb22014-05-28 23:58:16 -070018 }
19 virtual ~A() {
20 }
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080021 pthread_mutex_t m;
22 pthread_cond_t c;
23 bool signaled;
Stephen Hines2d1fdb22014-05-28 23:58:16 -070024};
25
26struct B : A {
27 virtual void F() {
28 }
29 virtual ~B() {
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080030 pthread_mutex_lock(&m);
31 while (!signaled)
32 pthread_cond_wait(&c, &m);
33 pthread_mutex_unlock(&m);
Stephen Hines2d1fdb22014-05-28 23:58:16 -070034 }
35};
36
37static A *obj = new B;
38
39void *Thread1(void *x) {
40 obj->F();
41 obj->Done();
42 return NULL;
43}
44
45void *Thread2(void *x) {
46 delete obj;
47 return NULL;
48}
49
50int main() {
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 fprintf(stderr, "PASS\n");
57}
58// CHECK: PASS
59// CHECK-NOT: WARNING: ThreadSanitizer: data race