Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / test / tsan / race_on_read.cpp
blobd388bbacde5b196c1845d0993e0a3e18950a0e7e
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>
8 int fd;
9 char buf;
11 void *Thread1(void *x) {
12 barrier_wait(&barrier);
13 read(fd, &buf, 1);
14 return NULL;
17 void *Thread2(void *x) {
18 read(fd, &buf, 1);
19 barrier_wait(&barrier);
20 return NULL;
23 int main() {
24 barrier_init(&barrier, 2);
25 fd = open("/dev/random", O_RDONLY);
26 if (fd < 0) {
27 fprintf(stderr, "failed to open /dev/random (%d)\n", errno);
28 return 1;
30 pthread_t t[2];
31 pthread_create(&t[0], NULL, Thread1, NULL);
32 pthread_create(&t[1], NULL, Thread2, NULL);
33 pthread_join(t[0], NULL);
34 pthread_join(t[1], NULL);
35 close(fd);
36 fprintf(stderr, "DONE\n");
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