Back to index

glibc  2.9
tst-barrier2.c
Go to the documentation of this file.
00001 /* Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <errno.h>
00021 #include <pthread.h>
00022 #include <stdint.h>
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <string.h>
00026 #include <unistd.h>
00027 #include <sys/mman.h>
00028 #include <sys/wait.h>
00029 
00030 
00031 static int
00032 do_test (void)
00033 {
00034   size_t ps = sysconf (_SC_PAGESIZE);
00035   char tmpfname[] = "/tmp/tst-barrier2.XXXXXX";
00036   char data[ps];
00037   void *mem;
00038   int fd;
00039   pthread_barrier_t *b;
00040   pthread_barrierattr_t a;
00041   pid_t pid;
00042   int serials = 0;
00043   int cnt;
00044   int status;
00045   int p;
00046 
00047   fd = mkstemp (tmpfname);
00048   if (fd == -1)
00049     {
00050       printf ("cannot open temporary file: %m\n");
00051       return 1;
00052     }
00053 
00054   /* Make sure it is always removed.  */
00055   unlink (tmpfname);
00056 
00057   /* Create one page of data.  */
00058   memset (data, '\0', ps);
00059 
00060   /* Write the data to the file.  */
00061   if (write (fd, data, ps) != (ssize_t) ps)
00062     {
00063       puts ("short write");
00064       return 1;
00065     }
00066 
00067   mem = mmap (NULL, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
00068   if (mem == MAP_FAILED)
00069     {
00070       printf ("mmap failed: %m\n");
00071       return 1;
00072     }
00073 
00074   b = (pthread_barrier_t *) (((uintptr_t) mem + __alignof (pthread_barrier_t))
00075                           & ~(__alignof (pthread_barrier_t) - 1));
00076 
00077   if (pthread_barrierattr_init (&a) != 0)
00078     {
00079       puts ("barrierattr_init failed");
00080       return 1;
00081     }
00082 
00083   if (pthread_barrierattr_getpshared (&a, &p) != 0)
00084     {
00085       puts ("1st barrierattr_getpshared failed");
00086       return 1;
00087     }
00088 
00089   if (p != PTHREAD_PROCESS_PRIVATE)
00090     {
00091       puts ("default pshared value wrong");
00092       return 1;
00093     }
00094 
00095   if (pthread_barrierattr_setpshared (&a, PTHREAD_PROCESS_SHARED) != 0)
00096     {
00097       puts ("barrierattr_setpshared failed");
00098       return 1;
00099     }
00100 
00101   if (pthread_barrierattr_getpshared (&a, &p) != 0)
00102     {
00103       puts ("2nd barrierattr_getpshared failed");
00104       return 1;
00105     }
00106 
00107   if (p != PTHREAD_PROCESS_SHARED)
00108     {
00109       puts ("pshared value after setpshared call wrong");
00110       return 1;
00111     }
00112 
00113   if (pthread_barrier_init (b, &a, 2) != 0)
00114     {
00115       puts ("barrier_init failed");
00116       return 1;
00117     }
00118 
00119   if (pthread_barrierattr_destroy (&a) != 0)
00120     {
00121       puts ("barrierattr_destroy failed");
00122       return 1;
00123     }
00124 
00125   puts ("going to fork now");
00126   pid = fork ();
00127   if (pid == -1)
00128     {
00129       puts ("fork failed");
00130       return 1;
00131     }
00132 
00133   /* Just to be sure we don't hang forever.  */
00134   alarm (4);
00135 
00136 #define N 30
00137   for (cnt = 0; cnt < N; ++cnt)
00138     {
00139       int e;
00140 
00141       e = pthread_barrier_wait (b);
00142       if (e == PTHREAD_BARRIER_SERIAL_THREAD)
00143        ++serials;
00144       else if (e != 0)
00145        {
00146          printf ("%s: barrier_wait returned value %d != 0 and PTHREAD_BARRIER_SERIAL_THREAD\n",
00147                 pid == 0 ? "child" : "parent", e);
00148          return 1;
00149        }
00150     }
00151 
00152   alarm (0);
00153 
00154   printf ("%s: was %d times the serial thread\n",
00155          pid == 0 ? "child" : "parent", serials);
00156 
00157   if (pid == 0)
00158     /* The child.  Pass the number of times we had the serializing
00159        thread back to the parent.  */
00160     exit (serials);
00161 
00162   if (waitpid (pid, &status, 0) != pid)
00163     {
00164       puts ("waitpid failed");
00165       return 1;
00166     }
00167 
00168   if (!WIFEXITED (status))
00169     {
00170       puts ("child exited abnormally");
00171       return 1;
00172     }
00173 
00174   if (WEXITSTATUS (status) + serials != N)
00175     {
00176       printf ("total number of serials is %d, expected %d\n",
00177              WEXITSTATUS (status) + serials, N);
00178       return 1;
00179     }
00180 
00181   return 0;
00182 }
00183 
00184 #define TEST_FUNCTION do_test ()
00185 #include "../test-skeleton.c"