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)8void *Thread(void *p) { 9 sleep(1); 10 __tsan_java_mutex_lock(lockaddr); 11 *(int*)varaddr = 42; 12 __tsan_java_mutex_unlock(lockaddr); 13 return 0; 14 } 15 main()16int main() { 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 pthread_join(th, 0); 30 __tsan_java_free(jheap, kBlockSize); 31 fprintf(stderr, "DONE\n"); 32 return __tsan_java_fini(); 33 } 34 35 // CHECK-NOT: WARNING: ThreadSanitizer: data race 36 // CHECK: DONE 37