Back to index

glibc  2.9
tst-rwlock12.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-rwlock12.XXXXXX";
00036   char data[ps];
00037   void *mem;
00038   int fd;
00039   pthread_mutex_t *m;
00040   pthread_mutexattr_t ma;
00041   pthread_rwlock_t *r;
00042   pthread_rwlockattr_t ra;
00043   pid_t pid;
00044   int status = 0;
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   /* The following assumes alignment for a mutex is at least as high
00076      as that for a rwlock.  Which is true in our case.  */
00077   m = (pthread_mutex_t *) (r + 1);
00078 
00079   if (pthread_rwlockattr_init (&ra) != 0)
00080     {
00081       puts ("rwlockattr_init failed");
00082       return 1;
00083     }
00084 
00085   if (pthread_rwlockattr_setpshared (&ra, PTHREAD_PROCESS_SHARED) != 0)
00086     {
00087       puts ("rwlockattr_setpshared failed");
00088       return 1;
00089     }
00090 
00091   if (pthread_rwlock_init (r, &ra) != 0)
00092     {
00093       puts ("rwlock_init failed");
00094       return 1;
00095     }
00096 
00097   if (pthread_mutexattr_init (&ma) != 0)
00098     {
00099       puts ("rwlockattr_init failed");
00100       return 1;
00101     }
00102 
00103   if (pthread_mutexattr_setpshared (&ma, PTHREAD_PROCESS_SHARED) != 0)
00104     {
00105       puts ("mutexattr_setpshared failed");
00106       return 1;
00107     }
00108 
00109   if (pthread_mutex_init (m, &ma) != 0)
00110     {
00111       puts ("mutex_init failed");
00112       return 1;
00113     }
00114 
00115   /* Lock the mutex.  */
00116   if (pthread_mutex_lock (m) != 0)
00117     {
00118       puts ("mutex_lock failed");
00119       return 1;
00120     }
00121 
00122   puts ("going to fork now");
00123   pid = fork ();
00124   if (pid == -1)
00125     {
00126       puts ("fork failed");
00127       return 1;
00128     }
00129   else if (pid == 0)
00130     {
00131       /* Lock the mutex.  */
00132       if (pthread_mutex_lock (m) != 0)
00133        {
00134          puts ("child: mutex_lock failed");
00135          return 1;
00136        }
00137 
00138       /* Try to get the rwlock.  */
00139       if (pthread_rwlock_trywrlock (r) == 0)
00140        {
00141          puts ("rwlock_trywrlock succeeded");
00142          return 1;
00143        }
00144 
00145       /* Try again.  */
00146       struct timespec ts = { .tv_sec = 0, .tv_nsec = 500000000 };
00147       int e = pthread_rwlock_timedwrlock (r, &ts);
00148       if (e == 0)
00149        {
00150          puts ("rwlock_timedwrlock succeeded");
00151          return 1;
00152        }
00153       if (e != ETIMEDOUT)
00154        {
00155          puts ("rwlock_timedwrlock didn't return ETIMEDOUT");
00156          status = 1;
00157        }
00158 
00159       if (pthread_rwlock_tryrdlock (r) == 0)
00160        {
00161          puts ("rwlock_tryrdlock succeeded");
00162          return 1;
00163        }
00164 
00165       e = pthread_rwlock_timedrdlock (r, &ts);
00166       if (e == 0)
00167        {
00168          puts ("rwlock_timedrdlock succeeded");
00169          return 1;
00170        }
00171       if (e != ETIMEDOUT)
00172        {
00173          puts ("rwlock_timedrdlock didn't return ETIMEDOUT");
00174          status = 1;
00175        }
00176     }
00177   else
00178     {
00179       /* Lock the rwlock for writing.  */
00180       if (pthread_rwlock_wrlock (r) != 0)
00181        {
00182          puts ("rwlock_wrlock failed");
00183          kill (pid, SIGTERM);
00184          return 1;
00185        }
00186 
00187       /* Allow the child to run.  */
00188       if (pthread_mutex_unlock (m) != 0)
00189        {
00190          puts ("mutex_unlock failed");
00191          kill (pid, SIGTERM);
00192          return 1;
00193        }
00194 
00195       /* Just wait for the child.  */
00196       if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
00197        {
00198          puts ("waitpid failed");
00199          kill (pid, SIGTERM);
00200          return 1;
00201        }
00202     }
00203 
00204   return status;
00205 }
00206 
00207 #define TEST_FUNCTION do_test ()
00208 #include "../test-skeleton.c"