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#include <errno.h>
7
8int fd;
9char buf;
10
11void *Thread1(void *x) {
12 barrier_wait(barrier: &barrier);
13 read(fd: fd, buf: &buf, nbytes: 1);
14 return NULL;
15}
16
17void *Thread2(void *x) {
18 read(fd: fd, buf: &buf, nbytes: 1);
19 barrier_wait(barrier: &barrier);
20 return NULL;
21}
22
23int main() {
24 barrier_init(barrier: &barrier, count: 2);
25 fd = open(file: "/dev/random", O_RDONLY);
26 if (fd < 0) {
27 fprintf(stderr, format: "failed to open /dev/random (%d)\n", errno);
28 return 1;
29 }
30 pthread_t t[2];
31 pthread_create(newthread: &t[0], NULL, start_routine: Thread1, NULL);
32 pthread_create(newthread: &t[1], NULL, start_routine: Thread2, NULL);
33 pthread_join(th: t[0], NULL);
34 pthread_join(th: t[1], NULL);
35 close(fd: fd);
36 fprintf(stderr, format: "DONE\n");
37}
38
39// CHECK: WARNING: ThreadSanitizer: data race
40// CHECK: Write of size 1
41// CHECK: #0 read
42// CHECK: Previous write of size 1
43// CHECK: #0 read
44// CHECK: DONE
45
46

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