• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
Thread(void * p)7 void *Thread(void *p) {
8   __tsan_java_mutex_lock(lockaddr);
9   __tsan_java_mutex_lock(lockaddr);
10   __tsan_java_mutex_lock(lockaddr);
11   int rec = __tsan_java_mutex_unlock_rec(lockaddr);
12   if (rec != 3) {
13     fprintf(stderr, "FAILED 0 rec=%d\n", rec);
14     exit(1);
15   }
16   *(int*)varaddr = 42;
17   barrier_wait(&barrier);
18   barrier_wait(&barrier);
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   barrier_init(&barrier, 2);
28   int const kHeapSize = 1024 * 1024;
29   jptr jheap = (jptr)malloc(kHeapSize + 8) + 8;
30   __tsan_java_init(jheap, kHeapSize);
31   const int kBlockSize = 16;
32   __tsan_java_alloc(jheap, kBlockSize);
33   varaddr = jheap;
34   *(int*)varaddr = 0;
35   lockaddr = jheap + 8;
36   pthread_t th;
37   pthread_create(&th, 0, Thread, 0);
38   barrier_wait(&barrier);
39   __tsan_java_mutex_lock(lockaddr);
40   *(int*)varaddr = 43;
41   __tsan_java_mutex_unlock(lockaddr);
42   barrier_wait(&barrier);
43   pthread_join(th, 0);
44   __tsan_java_free(jheap, kBlockSize);
45   fprintf(stderr, "DONE\n");
46   return __tsan_java_fini();
47 }
48 
49 // CHECK: WARNING: ThreadSanitizer: data race
50 // CHECK-NOT: FAILED
51 // CHECK: DONE
52