1// RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2#include "java.h"
3
4jptr varaddr;
5jptr lockaddr;
6
7void *Thread(void *p) {
8 while (__atomic_load_n((int*)lockaddr, __ATOMIC_RELAXED) == 0)
9 usleep(useconds: 1000); // spin-wait
10 __tsan_java_acquire(addr: lockaddr);
11 *(int*)varaddr = 42;
12 return 0;
13}
14
15int main() {
16 barrier_init(barrier: &barrier, count: 2);
17 int const kHeapSize = 1024 * 1024;
18 jptr jheap = (jptr)malloc(size: kHeapSize + 8) + 8;
19 __tsan_java_init(heap_begin: jheap, heap_size: kHeapSize);
20 const int kBlockSize = 16;
21 __tsan_java_alloc(ptr: jheap, size: kBlockSize);
22 varaddr = jheap;
23 lockaddr = jheap + 8;
24 pthread_t th;
25 pthread_create(newthread: &th, attr: 0, start_routine: Thread, arg: 0);
26 *(int*)varaddr = 43;
27 __tsan_java_release(addr: lockaddr);
28 __atomic_store_n((int*)lockaddr, 1, __ATOMIC_RELAXED);
29 pthread_join(th: th, thread_return: 0);
30 *(int*)lockaddr = 0;
31 pthread_create(newthread: &th, attr: 0, start_routine: Thread, arg: 0);
32 *(int*)varaddr = 43;
33 __tsan_java_release_store(addr: lockaddr);
34 __atomic_store_n((int*)lockaddr, 1, __ATOMIC_RELAXED);
35 pthread_join(th: th, thread_return: 0);
36 __tsan_java_free(ptr: jheap, size: kBlockSize);
37 fprintf(stderr, format: "DONE\n");
38 return __tsan_java_fini();
39}
40
41// CHECK-NOT: WARNING: ThreadSanitizer: data race
42// CHECK: DONE
43

source code of compiler-rt/test/tsan/java_volatile.cpp