xref: /llvm-project/compiler-rt/test/tsan/flush_memory.cpp (revision 7de546e9e8b6c07ef1247f518b2995ec4efbb7d2)
1 // RUN: %clangxx_tsan -O1 %s -o %t
2 // RUN: %env_tsan_opts=flush_memory_ms=1:flush_symbolizer_ms=1:memory_limit_mb=1 not %run %t 2>&1 | FileCheck %s
3 #include "test.h"
4 
5 long X, Y;
6 
Thread(void * arg)7 void *Thread(void *arg) {
8   __atomic_fetch_add(&X, 1, __ATOMIC_SEQ_CST);
9   barrier_wait(&barrier);
10   barrier_wait(&barrier);
11   Y = 1;
12   return &Y;
13 }
14 
main()15 int main() {
16   __tsan_flush_memory();
17   barrier_init(&barrier, 2);
18   __atomic_fetch_add(&X, 1, __ATOMIC_SEQ_CST);
19   pthread_t t;
20   pthread_create(&t, NULL, Thread, NULL);
21   barrier_wait(&barrier);
22   __tsan_flush_memory();
23   // Trigger a race to test flushing of the symbolizer cache.
24   Y = 2;
25   barrier_wait(&barrier);
26   pthread_join(t, NULL);
27   __atomic_fetch_add(&X, 1, __ATOMIC_SEQ_CST);
28   // Background runtime thread should do some flushes meanwhile.
29   sleep(2);
30   __tsan_flush_memory();
31   fprintf(stderr, "DONE\n");
32   // The race may not be detected since we are doing aggressive flushes
33   // (if the state flush happens between racing accesses, tsan won't
34   // detect the race). So return 1 to make the test deterministic.
35   return 1;
36 }
37 
38 // CHECK: DONE
39