(root)/
glibc-2.38/
sysdeps/
pthread/
tst-barrier2.c
       1  /* Tests process-shared barriers.
       2     Copyright (C) 2002-2023 Free Software Foundation, Inc.
       3     This file is part of the GNU C Library.
       4  
       5     The GNU C Library is free software; you can redistribute it and/or
       6     modify it under the terms of the GNU Lesser General Public
       7     License as published by the Free Software Foundation; either
       8     version 2.1 of the License, or (at your option) any later version.
       9  
      10     The GNU C Library is distributed in the hope that it will be useful,
      11     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13     Lesser General Public License for more details.
      14  
      15     You should have received a copy of the GNU Lesser General Public
      16     License along with the GNU C Library; if not, see
      17     <https://www.gnu.org/licenses/>.  */
      18  
      19  #include <errno.h>
      20  #include <pthread.h>
      21  #include <stdint.h>
      22  #include <stdio.h>
      23  #include <stdlib.h>
      24  #include <string.h>
      25  #include <unistd.h>
      26  #include <sys/mman.h>
      27  #include <sys/wait.h>
      28  
      29  
      30  static int
      31  do_test (void)
      32  {
      33    size_t ps = sysconf (_SC_PAGESIZE);
      34    char tmpfname[] = "/tmp/tst-barrier2.XXXXXX";
      35    char data[ps];
      36    void *mem;
      37    int fd;
      38    pthread_barrier_t *b;
      39    pthread_barrierattr_t a;
      40    pid_t pid;
      41    int serials = 0;
      42    int cnt;
      43    int status;
      44    int p;
      45  
      46    fd = mkstemp (tmpfname);
      47    if (fd == -1)
      48      {
      49        printf ("cannot open temporary file: %m\n");
      50        return 1;
      51      }
      52  
      53    /* Make sure it is always removed.  */
      54    unlink (tmpfname);
      55  
      56    /* Create one page of data.  */
      57    memset (data, '\0', ps);
      58  
      59    /* Write the data to the file.  */
      60    if (write (fd, data, ps) != (ssize_t) ps)
      61      {
      62        puts ("short write");
      63        return 1;
      64      }
      65  
      66    mem = mmap (NULL, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
      67    if (mem == MAP_FAILED)
      68      {
      69        printf ("mmap failed: %m\n");
      70        return 1;
      71      }
      72  
      73    b = (pthread_barrier_t *) (((uintptr_t) mem + __alignof (pthread_barrier_t))
      74  			     & ~(__alignof (pthread_barrier_t) - 1));
      75  
      76    if (pthread_barrierattr_init (&a) != 0)
      77      {
      78        puts ("barrierattr_init failed");
      79        return 1;
      80      }
      81  
      82    if (pthread_barrierattr_getpshared (&a, &p) != 0)
      83      {
      84        puts ("1st barrierattr_getpshared failed");
      85        return 1;
      86      }
      87  
      88    if (p != PTHREAD_PROCESS_PRIVATE)
      89      {
      90        puts ("default pshared value wrong");
      91        return 1;
      92      }
      93  
      94    if (pthread_barrierattr_setpshared (&a, PTHREAD_PROCESS_SHARED) != 0)
      95      {
      96        puts ("barrierattr_setpshared failed");
      97        return 1;
      98      }
      99  
     100    if (pthread_barrierattr_getpshared (&a, &p) != 0)
     101      {
     102        puts ("2nd barrierattr_getpshared failed");
     103        return 1;
     104      }
     105  
     106    if (p != PTHREAD_PROCESS_SHARED)
     107      {
     108        puts ("pshared value after setpshared call wrong");
     109        return 1;
     110      }
     111  
     112    if (pthread_barrier_init (b, &a, 2) != 0)
     113      {
     114        puts ("barrier_init failed");
     115        return 1;
     116      }
     117  
     118    if (pthread_barrierattr_destroy (&a) != 0)
     119      {
     120        puts ("barrierattr_destroy failed");
     121        return 1;
     122      }
     123  
     124    puts ("going to fork now");
     125    pid = fork ();
     126    if (pid == -1)
     127      {
     128        puts ("fork failed");
     129        return 1;
     130      }
     131  
     132    /* Just to be sure we don't hang forever.  */
     133    alarm (4);
     134  
     135  #define N 30
     136    for (cnt = 0; cnt < N; ++cnt)
     137      {
     138        int e;
     139  
     140        e = pthread_barrier_wait (b);
     141        if (e == PTHREAD_BARRIER_SERIAL_THREAD)
     142  	++serials;
     143        else if (e != 0)
     144  	{
     145  	  printf ("%s: barrier_wait returned value %d != 0 and PTHREAD_BARRIER_SERIAL_THREAD\n",
     146  		  pid == 0 ? "child" : "parent", e);
     147  	  return 1;
     148  	}
     149      }
     150  
     151    alarm (0);
     152  
     153    printf ("%s: was %d times the serial thread\n",
     154  	  pid == 0 ? "child" : "parent", serials);
     155  
     156    if (pid == 0)
     157      /* The child.  Pass the number of times we had the serializing
     158         thread back to the parent.  */
     159      exit (serials);
     160  
     161    if (waitpid (pid, &status, 0) != pid)
     162      {
     163        puts ("waitpid failed");
     164        return 1;
     165      }
     166  
     167    if (!WIFEXITED (status))
     168      {
     169        puts ("child exited abnormally");
     170        return 1;
     171      }
     172  
     173    if (WEXITSTATUS (status) + serials != N)
     174      {
     175        printf ("total number of serials is %d, expected %d\n",
     176  	      WEXITSTATUS (status) + serials, N);
     177        return 1;
     178      }
     179  
     180    return 0;
     181  }
     182  
     183  #define TEST_FUNCTION do_test ()
     184  #include "../test-skeleton.c"