aboutsummaryrefslogtreecommitdiffstats
path: root/gcc-4.9/gcc/testsuite/g++.dg/tsan/cond_race.C
blob: d72d0fb54f60f87a34d9fe2645f597163bf5a79b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/* { dg-shouldfail "tsan" } */
/* { dg-additional-options "-ldl" } */
/* { dg-output "ThreadSanitizer: data race.*" } */
/* { dg-output "pthread_cond_signal.*" } */

#include <pthread.h>
#include "tsan_barrier.h"

static pthread_barrier_t barrier;

struct Ctx {
  pthread_mutex_t m;
  pthread_cond_t c;
  bool done;
};

void *thr(void *p) {
  Ctx *c = (Ctx*)p;
  pthread_mutex_lock(&c->m);
  c->done = true;
  pthread_mutex_unlock(&c->m);
  pthread_cond_signal(&c->c);
  barrier_wait(&barrier);
  return 0;
}

int main() {
  barrier_init(&barrier, 2);
  Ctx *c = new Ctx();
  pthread_mutex_init(&c->m, 0);
  pthread_cond_init(&c->c, 0);
  pthread_t th;
  pthread_create(&th, 0, thr, c);
  pthread_mutex_lock(&c->m);
  while (!c->done)
    pthread_cond_wait(&c->c, &c->m);
  pthread_mutex_unlock(&c->m);
  barrier_wait(&barrier);
  delete c;
  pthread_join(th, 0);
}