Merge from trunk
[official-gcc.git] / gcc / testsuite / c-c++-common / tsan / simple_race.c
bloba40accd40a25e826b31807ceb964ffe75b7a6a8c
1 /* { dg-set-target-env-var TSAN_OPTIONS "halt_on_error=1" } */
2 /* { dg-shouldfail "tsan" } */
4 #include <pthread.h>
5 #include <stdio.h>
6 #include <unistd.h>
8 #define MAX_ITERATIONS_NUMBER 100
9 #define SLEEP_STEP 128000
11 unsigned int delay_time = 1000;
13 static inline void delay () {
14 usleep(delay_time);
17 extern int main_1();
19 int main() {
20 int i;
21 for (i = 0; i < MAX_ITERATIONS_NUMBER; i++) {
22 main_1();
23 delay_time += delay_time < 256000 ? delay_time : SLEEP_STEP;
25 return 0;
28 int Global;
30 void *Thread1(void *x) {
31 delay();
32 Global = 42;
33 return NULL;
36 void *Thread2(void *x) {
37 Global = 43;
38 return NULL;
41 int main_1() {
42 pthread_t t[2];
43 pthread_create(&t[0], NULL, Thread1, NULL);
44 pthread_create(&t[1], NULL, Thread2, NULL);
45 pthread_join(t[0], NULL);
46 pthread_join(t[1], NULL);
47 return 0;
50 /* { dg-output "WARNING: ThreadSanitizer: data race.*(\n|\r\n|\r)" } */