llvm-project
42 строки · 1.1 Кб
1// RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2#include "java.h"
3
4jptr varaddr;
5jptr lockaddr;
6
7void *Thread(void *p) {
8while (__atomic_load_n((int*)lockaddr, __ATOMIC_RELAXED) == 0)
9usleep(1000); // spin-wait
10__tsan_java_acquire(lockaddr);
11*(int*)varaddr = 42;
12return 0;
13}
14
15int main() {
16barrier_init(&barrier, 2);
17int const kHeapSize = 1024 * 1024;
18jptr jheap = (jptr)malloc(kHeapSize + 8) + 8;
19__tsan_java_init(jheap, kHeapSize);
20const int kBlockSize = 16;
21__tsan_java_alloc(jheap, kBlockSize);
22varaddr = jheap;
23lockaddr = jheap + 8;
24pthread_t th;
25pthread_create(&th, 0, Thread, 0);
26*(int*)varaddr = 43;
27__tsan_java_release(lockaddr);
28__atomic_store_n((int*)lockaddr, 1, __ATOMIC_RELAXED);
29pthread_join(th, 0);
30*(int*)lockaddr = 0;
31pthread_create(&th, 0, Thread, 0);
32*(int*)varaddr = 43;
33__tsan_java_release_store(lockaddr);
34__atomic_store_n((int*)lockaddr, 1, __ATOMIC_RELAXED);
35pthread_join(th, 0);
36__tsan_java_free(jheap, kBlockSize);
37fprintf(stderr, "DONE\n");
38return __tsan_java_fini();
39}
40
41// CHECK-NOT: WARNING: ThreadSanitizer: data race
42// CHECK: DONE
43