File: tsan_tiny_race.d

package info (click to toggle)
ldc 1%3A1.30.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 59,248 kB
  • sloc: cpp: 61,598; ansic: 14,545; sh: 1,014; makefile: 972; asm: 510; objc: 135; exp: 48; python: 12
file content (57 lines) | stat: -rw-r--r-- 1,680 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Test that ThreadSanitizer+LDC works on a very basic testcase.

// REQUIRES: TSan

// RUN: %ldc -g -fsanitize=thread %s -of=%t%exe
// RUN: %deflake 20 %t%exe | FileCheck %s

// CHECK: WARNING: ThreadSanitizer: data race

import core.thread;

shared int global;

void thread1() {
  barrier_wait(&barrier);
// CHECK-DAG: 7thread1{{.*}}[[@LINE+1]]
  global = 42;
}

int main() {
  barrier_init(&barrier, 2);
  auto t = new Thread(&thread1);
  t.start();
// CHECK-DAG: {{(_Dmain|D main).*}}[[@LINE+1]]
  global = 43;
  barrier_wait(&barrier);
  t.join();
  return global;
}

//----------------------------------------------------------------------------
// Code to facilitate thread synchronization to make this test deterministic.
// See LLVM: compiler-rt/test/tsan/test.h

// TSan-invisible barrier.
// Tests use it to establish necessary execution order in a way that does not
// interfere with tsan (does not establish synchronization between threads).
alias invisible_barrier_t = __c_ulonglong;
import core.stdc.config;
alias __c_unsigned = uint;
// Default instance of the barrier, but a test can declare more manually.
__gshared invisible_barrier_t barrier;

extern (C) {
  // These functions reside inside the tsan library.
  void __tsan_testonly_barrier_init(invisible_barrier_t *barrier, __c_unsigned count);
  void __tsan_testonly_barrier_wait(invisible_barrier_t *barrier);
}

void barrier_init(invisible_barrier_t *barrier, __c_unsigned count) {
  __tsan_testonly_barrier_init(barrier, count);
}

void barrier_wait(invisible_barrier_t *barrier) {
  __tsan_testonly_barrier_wait(barrier);
}
//----------------------------------------------------------------------------