Back to index

glibc  2.9
tst-rwlock4.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-rwlock4.XXXXXX";
00036   char data[ps];
00037   void *mem;
00038   int fd;
00039   pthread_rwlock_t *r;
00040   pthread_rwlockattr_t a;
00041   pid_t pid;
00042   char *p;
00043   int err;
00044   int s;
00045 
00046   fd = mkstemp (tmpfname);
00047   if (fd == -1)
00048     {
00049       printf ("cannot open temporary file: %m\n");
00050       return 1;
00051     }
00052 
00053   /* Make sure it is always removed.  */
00054   unlink (tmpfname);
00055 
00056   /* Create one page of data.  */
00057   memset (data, '\0', ps);
00058 
00059   /* Write the data to the file.  */
00060   if (write (fd, data, ps) != (ssize_t) ps)
00061     {
00062       puts ("short write");
00063       return 1;
00064     }
00065 
00066   mem = mmap (NULL, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
00067   if (mem == MAP_FAILED)
00068     {
00069       printf ("mmap failed: %m\n");
00070       return 1;
00071     }
00072 
00073   r = (pthread_rwlock_t *) (((uintptr_t) mem + __alignof (pthread_rwlock_t))
00074                          & ~(__alignof (pthread_rwlock_t) - 1));
00075   p = (char *) (r + 1);
00076 
00077   if (pthread_rwlockattr_init (&a) != 0)
00078     {
00079       puts ("rwlockattr_init failed");
00080       return 1;
00081     }
00082 
00083   if (pthread_rwlockattr_getpshared (&a, &s) != 0)
00084     {
00085       puts ("1st rwlockattr_getpshared failed");
00086       return 1;
00087     }
00088 
00089   if (s != PTHREAD_PROCESS_PRIVATE)
00090     {
00091       puts ("default pshared value wrong");
00092       return 1;
00093     }
00094 
00095   if (pthread_rwlockattr_setpshared (&a, PTHREAD_PROCESS_SHARED) != 0)
00096     {
00097       puts ("rwlockattr_setpshared failed");
00098       return 1;
00099     }
00100 
00101   if (pthread_rwlockattr_getpshared (&a, &s) != 0)
00102     {
00103       puts ("2nd rwlockattr_getpshared failed");
00104       return 1;
00105     }
00106 
00107   if (s != PTHREAD_PROCESS_SHARED)
00108     {
00109       puts ("pshared value after setpshared call wrong");
00110       return 1;
00111     }
00112 
00113   if (pthread_rwlock_init (r, &a) != 0)
00114     {
00115       puts ("rwlock_init failed");
00116       return 1;
00117     }
00118 
00119   if (pthread_rwlock_rdlock (r) != 0)
00120     {
00121       puts ("rwlock_rdlock failed");
00122       return 1;
00123     }
00124 
00125   if (pthread_rwlockattr_destroy (&a) != 0)
00126     {
00127       puts ("rwlockattr_destroy failed");
00128       return 1;
00129     }
00130 
00131   err = pthread_rwlock_trywrlock (r);
00132   if (err == 0)
00133     {
00134       puts ("rwlock_trywrlock succeeded");
00135       return 1;
00136     }
00137   else if (err != EBUSY)
00138     {
00139       puts ("rwlock_trywrlock didn't return EBUSY");
00140       return 1;
00141     }
00142 
00143   *p = 0;
00144 
00145   puts ("going to fork now");
00146   pid = fork ();
00147   if (pid == -1)
00148     {
00149       puts ("fork failed");
00150       return 1;
00151     }
00152   else if (pid == 0)
00153     {
00154       /* Play some lock ping-pong.  It's our turn to unlock first.  */
00155       if ((*p)++ != 0)
00156        {
00157          puts ("child: *p != 0");
00158          return 1;
00159        }
00160 
00161       if (pthread_rwlock_unlock (r) != 0)
00162        {
00163          puts ("child: 1st rwlock_unlock failed");
00164          return 1;
00165        }
00166 
00167       puts ("child done");
00168     }
00169   else
00170     {
00171       if (pthread_rwlock_wrlock (r) != 0)
00172        {
00173          puts ("parent: rwlock_wrlock failed");
00174          return 1;
00175        }
00176 
00177       if (*p != 1)
00178        {
00179          puts ("*p != 1");
00180          return 1;
00181        }
00182 
00183       puts ("parent done");
00184     }
00185 
00186   return 0;
00187 }
00188 
00189 #define TEST_FUNCTION do_test ()
00190 #include "../test-skeleton.c"