| 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 | |
| 7 | void *Thread(void *arg) { |
| 8 | __atomic_fetch_add(&X, 1, __ATOMIC_SEQ_CST); |
| 9 | barrier_wait(barrier: &barrier); |
| 10 | barrier_wait(barrier: &barrier); |
| 11 | Y = 1; |
| 12 | return &Y; |
| 13 | } |
| 14 | |
| 15 | int main() { |
| 16 | __tsan_flush_memory(); |
| 17 | barrier_init(barrier: &barrier, count: 2); |
| 18 | __atomic_fetch_add(&X, 1, __ATOMIC_SEQ_CST); |
| 19 | pthread_t t; |
| 20 | pthread_create(newthread: &t, NULL, start_routine: Thread, NULL); |
| 21 | barrier_wait(barrier: &barrier); |
| 22 | __tsan_flush_memory(); |
| 23 | // Trigger a race to test flushing of the symbolizer cache. |
| 24 | Y = 2; |
| 25 | barrier_wait(barrier: &barrier); |
| 26 | pthread_join(th: t, NULL); |
| 27 | __atomic_fetch_add(&X, 1, __ATOMIC_SEQ_CST); |
| 28 | // Background runtime thread should do some flushes meanwhile. |
| 29 | sleep(seconds: 2); |
| 30 | __tsan_flush_memory(); |
| 31 | fprintf(stderr, format: "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 | |