• 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   barrier_wait(&barrier);
9   __tsan_java_mutex_lock(lockaddr);
10   *(int*)varaddr = 42;
11   __tsan_java_mutex_unlock(lockaddr);
12   return 0;
13 }
14 
main()15 int main() {
16   barrier_init(&barrier, 2);
17   int const kHeapSize = 1024 * 1024;
18   jptr jheap = (jptr)malloc(kHeapSize + 8) + 8;
19   __tsan_java_init(jheap, kHeapSize);
20   const int kBlockSize = 16;
21   __tsan_java_alloc(jheap, kBlockSize);
22   varaddr = jheap;
23   lockaddr = jheap + 8;
24   pthread_t th;
25   pthread_create(&th, 0, Thread, 0);
26   __tsan_java_mutex_lock(lockaddr);
27   *(int*)varaddr = 43;
28   __tsan_java_mutex_unlock(lockaddr);
29   barrier_wait(&barrier);
30   pthread_join(th, 0);
31   __tsan_java_free(jheap, kBlockSize);
32   fprintf(stderr, "DONE\n");
33   return __tsan_java_fini();
34 }
35 
36 // CHECK-NOT: WARNING: ThreadSanitizer: data race
37 // CHECK: DONE
38