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