Home | History | Annotate | Download | only in tsan
      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 
      7 int fd;
      8 char buf;
      9 
     10 void *Thread1(void *x) {
     11   buf = 1;
     12   barrier_wait(&barrier);
     13   return NULL;
     14 }
     15 
     16 void *Thread2(void *x) {
     17   write(fd, &buf, 1);
     18   return NULL;
     19 }
     20 
     21 int main() {
     22   barrier_init(&barrier, 2);
     23   fd = open("/dev/null", O_WRONLY);
     24   if (fd < 0) return 1;
     25   pthread_t t[2];
     26   pthread_create(&t[0], NULL, Thread1, NULL);
     27   barrier_wait(&barrier);
     28   pthread_create(&t[1], NULL, Thread2, NULL);
     29   pthread_join(t[0], NULL);
     30   pthread_join(t[1], NULL);
     31   close(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