1 // RUN: %clangxx_tsan -O1 %s -o %t && %env_tsan_opts="flush_memory_ms=1 flush_symbolizer_ms=1 memory_limit_mb=1" %deflake %t | FileCheck %s
2 #include "test.h"
3 
4 long X, Y;
5 
6 void *Thread(void *arg) {
7   __atomic_fetch_add(&X, 1, __ATOMIC_SEQ_CST);
8   barrier_wait(&barrier);
9   barrier_wait(&barrier);
10   Y = 1;
11   return &Y;
12 }
13 
14 int main() {
15   __tsan_flush_memory();
16   barrier_init(&barrier, 2);
17   __atomic_fetch_add(&X, 1, __ATOMIC_SEQ_CST);
18   pthread_t t;
19   pthread_create(&t, NULL, Thread, NULL);
20   barrier_wait(&barrier);
21   __tsan_flush_memory();
22   // Trigger a race to test flushing of the symbolizer cache.
23   Y = 2;
24   barrier_wait(&barrier);
25   pthread_join(t, NULL);
26   __atomic_fetch_add(&X, 1, __ATOMIC_SEQ_CST);
27   // Background runtime thread should do some flushes meanwhile.
28   sleep(2);
29   __tsan_flush_memory();
30   fprintf(stderr, "DONE\n");
31   return 0;
32 }
33 
34 // CHECK: WARNING: ThreadSanitizer: data race
35 // CHECK: DONE
36