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