1// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
2#include "test.h"
3#include <sys/types.h>
4#include <sys/stat.h>
5#include <fcntl.h>
6
7int fd;
8char buf;
9
10void *Thread1(void *x) {
11 buf = 1;
12 barrier_wait(barrier: &barrier);
13 return NULL;
14}
15
16void *Thread2(void *x) {
17 write(fd: fd, buf: &buf, n: 1);
18 return NULL;
19}
20
21int main() {
22 barrier_init(barrier: &barrier, count: 2);
23 fd = open(file: "/dev/null", O_WRONLY);
24 if (fd < 0) return 1;
25 pthread_t t[2];
26 pthread_create(newthread: &t[0], NULL, start_routine: Thread1, NULL);
27 barrier_wait(barrier: &barrier);
28 pthread_create(newthread: &t[1], NULL, start_routine: Thread2, NULL);
29 pthread_join(th: t[0], NULL);
30 pthread_join(th: t[1], NULL);
31 close(fd: fd);
32}
33
34// CHECK: WARNING: ThreadSanitizer: data race
35// CHECK: Read of size 1
36// CHECK: #0 write
37// CHECK: Previous write of size 1
38// CHECK: #0 Thread1
39

source code of compiler-rt/test/tsan/race_on_write.cpp