16a211c5814e25d6745a5058cc0e499e5235d3821Stephen Hines// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
2429ccc808d255b82f3924250028eb4747d7a84ccDmitry Vyukov#include "java.h"
3e78b620d10b4c59871480a130c10588a124d05f4Dmitry Vyukov
4e78b620d10b4c59871480a130c10588a124d05f4Dmitry Vyukovvoid *Thread(void *p) {
5cdce50bda3603770cc4ef80cbb613c78b8e47a17Pirama Arumuga Nainar  barrier_wait(&barrier);
6e78b620d10b4c59871480a130c10588a124d05f4Dmitry Vyukov  *(int*)p = 42;
7e78b620d10b4c59871480a130c10588a124d05f4Dmitry Vyukov  return 0;
8e78b620d10b4c59871480a130c10588a124d05f4Dmitry Vyukov}
9e78b620d10b4c59871480a130c10588a124d05f4Dmitry Vyukov
10e78b620d10b4c59871480a130c10588a124d05f4Dmitry Vyukovint main() {
11cdce50bda3603770cc4ef80cbb613c78b8e47a17Pirama Arumuga Nainar  barrier_init(&barrier, 2);
12e78b620d10b4c59871480a130c10588a124d05f4Dmitry Vyukov  int const kHeapSize = 1024 * 1024;
136a211c5814e25d6745a5058cc0e499e5235d3821Stephen Hines  jptr jheap = (jptr)malloc(kHeapSize + 8) + 8;
146a211c5814e25d6745a5058cc0e499e5235d3821Stephen Hines  __tsan_java_init(jheap, kHeapSize);
15e78b620d10b4c59871480a130c10588a124d05f4Dmitry Vyukov  const int kBlockSize = 16;
166a211c5814e25d6745a5058cc0e499e5235d3821Stephen Hines  __tsan_java_alloc(jheap, kBlockSize);
17e78b620d10b4c59871480a130c10588a124d05f4Dmitry Vyukov  pthread_t th;
186a211c5814e25d6745a5058cc0e499e5235d3821Stephen Hines  pthread_create(&th, 0, Thread, (void*)jheap);
19e78b620d10b4c59871480a130c10588a124d05f4Dmitry Vyukov  *(int*)jheap = 43;
20cdce50bda3603770cc4ef80cbb613c78b8e47a17Pirama Arumuga Nainar  barrier_wait(&barrier);
21e78b620d10b4c59871480a130c10588a124d05f4Dmitry Vyukov  pthread_join(th, 0);
226a211c5814e25d6745a5058cc0e499e5235d3821Stephen Hines  __tsan_java_free(jheap, kBlockSize);
236a211c5814e25d6745a5058cc0e499e5235d3821Stephen Hines  fprintf(stderr, "DONE\n");
24e78b620d10b4c59871480a130c10588a124d05f4Dmitry Vyukov  return __tsan_java_fini();
25e78b620d10b4c59871480a130c10588a124d05f4Dmitry Vyukov}
26e78b620d10b4c59871480a130c10588a124d05f4Dmitry Vyukov
27e78b620d10b4c59871480a130c10588a124d05f4Dmitry Vyukov// CHECK: WARNING: ThreadSanitizer: data race
286a211c5814e25d6745a5058cc0e499e5235d3821Stephen Hines// CHECK: DONE
29