aboutsummaryrefslogtreecommitdiffstats
path: root/gcc-4.9/gcc/testsuite/c-c++-common/tsan/simple_race.c
blob: a40accd40a25e826b31807ceb964ffe75b7a6a8c (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
42
43
44
45
46
47
48
49
50
/* { dg-set-target-env-var TSAN_OPTIONS "halt_on_error=1" } */
/* { dg-shouldfail "tsan" } */

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>

#define MAX_ITERATIONS_NUMBER 100
#define SLEEP_STEP 128000 

unsigned int delay_time = 1000;

static inline void delay () {
  usleep(delay_time);
}

extern int main_1();

int main() {
  int i;
  for (i = 0; i < MAX_ITERATIONS_NUMBER; i++) {
    main_1();
    delay_time += delay_time < 256000 ? delay_time : SLEEP_STEP;
  }
  return 0;
}

int Global;

void *Thread1(void *x) {
  delay();
  Global = 42;
  return NULL;
}

void *Thread2(void *x) {
  Global = 43;
  return NULL;
}

int main_1() {
  pthread_t t[2];
  pthread_create(&t[0], NULL, Thread1, NULL);
  pthread_create(&t[1], NULL, Thread2, NULL);
  pthread_join(t[0], NULL);
  pthread_join(t[1], NULL);
  return 0;
}

/* { dg-output "WARNING: ThreadSanitizer: data race.*(\n|\r\n|\r)" } */