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