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 __tsan_java_mutex_lock(addr: lockaddr);
9 __tsan_java_mutex_lock(addr: lockaddr);
10 *(int*)varaddr = 42;
11 int rec = __tsan_java_mutex_unlock_rec(addr: lockaddr);
12 if (rec != 2) {
13 fprintf(stderr, format: "FAILED 0 rec=%d\n", rec);
14 exit(status: 1);
15 }
16 barrier_wait(barrier: &barrier);
17 barrier_wait(barrier: &barrier);
18 __tsan_java_mutex_lock_rec(addr: lockaddr, rec);
19 if (*(int*)varaddr != 43) {
20 fprintf(stderr, format: "FAILED 3 var=%d\n", *(int*)varaddr);
21 exit(status: 1);
22 }
23 __tsan_java_mutex_unlock(addr: lockaddr);
24 __tsan_java_mutex_unlock(addr: lockaddr);
25 return 0;
26}
27
28int main() {
29 barrier_init(barrier: &barrier, count: 2);
30 int const kHeapSize = 1024 * 1024;
31 jptr jheap = (jptr)malloc(size: kHeapSize + 8) + 8;
32 __tsan_java_init(heap_begin: jheap, heap_size: kHeapSize);
33 const int kBlockSize = 16;
34 __tsan_java_alloc(ptr: jheap, size: kBlockSize);
35 varaddr = jheap;
36 *(int*)varaddr = 0;
37 lockaddr = jheap + 8;
38 pthread_t th;
39 pthread_create(newthread: &th, attr: 0, start_routine: Thread, arg: 0);
40 barrier_wait(barrier: &barrier);
41 __tsan_java_mutex_lock(addr: lockaddr);
42 if (*(int*)varaddr != 42) {
43 fprintf(stderr, format: "FAILED 1 var=%d\n", *(int*)varaddr);
44 exit(status: 1);
45 }
46 *(int*)varaddr = 43;
47 __tsan_java_mutex_unlock(addr: lockaddr);
48 barrier_wait(barrier: &barrier);
49 pthread_join(th: th, thread_return: 0);
50 __tsan_java_free(ptr: jheap, size: kBlockSize);
51 fprintf(stderr, format: "DONE\n");
52 return __tsan_java_fini();
53}
54
55// CHECK-NOT: WARNING: ThreadSanitizer: data race
56// CHECK-NOT: FAILED
57// CHECK: DONE
58

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