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 
8 int fd;
9 char buf;
10 
Thread1(void * x)11 void *Thread1(void *x) {
12   barrier_wait(&barrier);
13   read(fd, &buf, 1);
14   return NULL;
15 }
16 
Thread2(void * x)17 void *Thread2(void *x) {
18   read(fd, &buf, 1);
19   barrier_wait(&barrier);
20   return NULL;
21 }
22 
main()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;
29   }
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");
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