1 // RUN: %clangxx_tsan -O1 %s -o %t && %t 2>&1 | FileCheck %s 2 #include "java.h" 3 #include <unistd.h> 4 5 jptr varaddr; 6 jptr lockaddr; 7 8 void *Thread(void *p) { 9 sleep(1); 10 __tsan_java_mutex_read_lock(lockaddr); 11 *(int*)varaddr = 42; 12 __tsan_java_mutex_read_unlock(lockaddr); 13 return 0; 14 } 15 16 int main() { 17 int const kHeapSize = 1024 * 1024; 18 void *jheap = malloc(kHeapSize); 19 __tsan_java_init((jptr)jheap, kHeapSize); 20 const int kBlockSize = 16; 21 __tsan_java_alloc((jptr)jheap, kBlockSize); 22 varaddr = (jptr)jheap; 23 lockaddr = (jptr)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((jptr)jheap, kBlockSize); 31 printf("OK\n"); 32 return __tsan_java_fini(); 33 } 34 35 // CHECK-NOT: WARNING: ThreadSanitizer: data race 36