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