(root)/
gcc-13.2.0/
gcc/
testsuite/
c-c++-common/
tsan/
fd_pipe_race.c
       1  /* { dg-shouldfail "tsan" } */
       2  /* { dg-additional-options "-ldl" } */
       3  
       4  #include <pthread.h>
       5  #include <unistd.h>
       6  #include "tsan_barrier.h"
       7  
       8  static pthread_barrier_t barrier;
       9  int fds[2];
      10  
      11  void *Thread1(void *x) {
      12    write(fds[1], "a", 1);
      13    barrier_wait(&barrier);
      14    return NULL;
      15  }
      16  
      17  void *Thread2(void *x) {
      18    barrier_wait(&barrier);
      19    close(fds[0]);
      20    close(fds[1]);
      21    return NULL;
      22  }
      23  
      24  int main() {
      25    barrier_init(&barrier, 2);
      26    pipe(fds);
      27    pthread_t t[2];
      28    pthread_create(&t[0], NULL, Thread1, NULL);
      29    pthread_create(&t[1], NULL, Thread2, NULL);
      30    pthread_join(t[0], NULL);
      31    pthread_join(t[1], NULL);
      32    return 0;
      33  }
      34  
      35  /* { dg-output "WARNING: ThreadSanitizer: data race.*\n" } */
      36  /* { dg-output "  Write of size 8.*\n" } */
      37  /* { dg-output "    #0 close.*\n" } */
      38  /* { dg-output "    #1 Thread2.*\n" } */
      39  /* { dg-output "  Previous read of size 8.*\n" } */
      40  /* { dg-output "    #0 write.*\n" } */
      41  /* { dg-output "    #1 Thread1.*\n" } */