File: mop1.c

package info (click to toggle)
llvm-toolchain-17 1%3A17.0.6-22
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,799,624 kB
  • sloc: cpp: 6,428,607; ansic: 1,383,196; asm: 793,408; python: 223,504; objc: 75,364; f90: 60,502; lisp: 33,869; pascal: 15,282; sh: 9,684; perl: 7,453; ml: 4,937; awk: 3,523; makefile: 2,889; javascript: 2,149; xml: 888; fortran: 619; cs: 573
file content (40 lines) | stat: -rw-r--r-- 1,010 bytes parent folder | download | duplicates (42)
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
// RUN: %clang_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
#include "test.h"

// We want to establish the following sequence of accesses to X:
// - main thread writes X
// - thread2 reads X, this read happens-before the write in main thread
// - thread1 reads X, this read is concurrent with the write in main thread
// Write in main thread and read in thread1 should be detected as a race.
// Previously tsan replaced write by main thread with read by thread1,
// as the result the race was not detected.

volatile long X, Y, Z;

void *Thread1(void *x) {
  barrier_wait(&barrier);
  barrier_wait(&barrier);
  Y = X;
  return NULL;
}

void *Thread2(void *x) {
  Z = X;
  barrier_wait(&barrier);
  return NULL;
}

int main() {
  barrier_init(&barrier, 2);
  pthread_t t[2];
  pthread_create(&t[0], 0, Thread1, 0);
  X = 42;
  barrier_wait(&barrier);
  pthread_create(&t[1], 0, Thread2, 0);
  pthread_join(t[0], 0);
  pthread_join(t[1], 0);
  return 0;
}

// CHECK: WARNING: ThreadSanitizer: data race