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