Back to index

glibc  2.9
tst-cond6.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 <stdio.h>
00023 #include <stdlib.h>
00024 #include <string.h>
00025 #include <time.h>
00026 #include <unistd.h>
00027 #include <sys/mman.h>
00028 #include <sys/time.h>
00029 #include <sys/wait.h>
00030 
00031 
00032 int *condition;
00033 
00034 static int
00035 do_test (void)
00036 {
00037   size_t ps = sysconf (_SC_PAGESIZE);
00038   char tmpfname[] = "/tmp/tst-cond6.XXXXXX";
00039   char data[ps];
00040   void *mem;
00041   int fd;
00042   pthread_mutexattr_t ma;
00043   pthread_mutex_t *mut1;
00044   pthread_mutex_t *mut2;
00045   pthread_condattr_t ca;
00046   pthread_cond_t *cond;
00047   pid_t pid;
00048   int result = 0;
00049 
00050   fd = mkstemp (tmpfname);
00051   if (fd == -1)
00052     {
00053       printf ("cannot open temporary file: %m\n");
00054       exit (1);
00055     }
00056 
00057   /* Make sure it is always removed.  */
00058   unlink (tmpfname);
00059 
00060   /* Create one page of data.  */
00061   memset (data, '\0', ps);
00062 
00063   /* Write the data to the file.  */
00064   if (write (fd, data, ps) != (ssize_t) ps)
00065     {
00066       puts ("short write");
00067       exit (1);
00068     }
00069 
00070   mem = mmap (NULL, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
00071   if (mem == MAP_FAILED)
00072     {
00073       printf ("mmap failed: %m\n");
00074       exit (1);
00075     }
00076 
00077   mut1 = (pthread_mutex_t *) (((uintptr_t) mem
00078                             + __alignof (pthread_mutex_t))
00079                            & ~(__alignof (pthread_mutex_t) - 1));
00080   mut2 = mut1 + 1;
00081 
00082   cond = (pthread_cond_t *) (((uintptr_t) (mut2 + 1)
00083                            + __alignof (pthread_cond_t))
00084                           & ~(__alignof (pthread_cond_t) - 1));
00085 
00086   condition = (int *) (((uintptr_t) (cond + 1) + __alignof (int))
00087                      & ~(__alignof (int) - 1));
00088 
00089   if (pthread_mutexattr_init (&ma) != 0)
00090     {
00091       puts ("mutexattr_init failed");
00092       exit (1);
00093     }
00094 
00095   if (pthread_mutexattr_setpshared (&ma, PTHREAD_PROCESS_SHARED) != 0)
00096     {
00097       puts ("mutexattr_setpshared failed");
00098       exit (1);
00099     }
00100 
00101   if (pthread_mutex_init (mut1, &ma) != 0)
00102     {
00103       puts ("1st mutex_init failed");
00104       exit (1);
00105     }
00106 
00107   if (pthread_mutex_init (mut2, &ma) != 0)
00108     {
00109       puts ("2nd mutex_init failed");
00110       exit (1);
00111     }
00112 
00113   if (pthread_condattr_init (&ca) != 0)
00114     {
00115       puts ("condattr_init failed");
00116       exit (1);
00117     }
00118 
00119   if (pthread_condattr_setpshared (&ca, PTHREAD_PROCESS_SHARED) != 0)
00120     {
00121       puts ("condattr_setpshared failed");
00122       exit (1);
00123     }
00124 
00125   if (pthread_cond_init (cond, &ca) != 0)
00126     {
00127       puts ("cond_init failed");
00128       exit (1);
00129     }
00130 
00131   if (pthread_mutex_lock (mut1) != 0)
00132     {
00133       puts ("parent: 1st mutex_lock failed");
00134       exit (1);
00135     }
00136 
00137   puts ("going to fork now");
00138   pid = fork ();
00139   if (pid == -1)
00140     {
00141       puts ("fork failed");
00142       exit (1);
00143     }
00144   else if (pid == 0)
00145     {
00146       struct timespec ts;
00147       struct timeval tv;
00148 
00149       if (pthread_mutex_lock (mut2) != 0)
00150        {
00151          puts ("child: mutex_lock failed");
00152          exit (1);
00153        }
00154 
00155       if (pthread_mutex_unlock (mut1) != 0)
00156        {
00157          puts ("child: 1st mutex_unlock failed");
00158          exit (1);
00159        }
00160 
00161       if (gettimeofday (&tv, NULL) != 0)
00162        {
00163          puts ("gettimeofday failed");
00164          exit (1);
00165        }
00166 
00167       TIMEVAL_TO_TIMESPEC (&tv, &ts);
00168       ts.tv_nsec += 500000000;
00169       if (ts.tv_nsec >= 1000000000)
00170        {
00171          ts.tv_nsec -= 1000000000;
00172          ++ts.tv_sec;
00173        }
00174 
00175       do
00176        if (pthread_cond_timedwait (cond, mut2, &ts) != 0)
00177          {
00178            puts ("child: cond_wait failed");
00179            exit (1);
00180          }
00181       while (*condition == 0);
00182 
00183       if (pthread_mutex_unlock (mut2) != 0)
00184        {
00185          puts ("child: 2nd mutex_unlock failed");
00186          exit (1);
00187        }
00188 
00189       puts ("child done");
00190     }
00191   else
00192     {
00193       int status;
00194 
00195       if (pthread_mutex_lock (mut1) != 0)
00196        {
00197          puts ("parent: 2nd mutex_lock failed");
00198          exit (1);
00199        }
00200 
00201       if (pthread_mutex_lock (mut2) != 0)
00202        {
00203          puts ("parent: 3rd mutex_lock failed");
00204          exit (1);
00205        }
00206 
00207       if (pthread_cond_signal (cond) != 0)
00208        {
00209          puts ("parent: cond_signal failed");
00210          exit (1);
00211        }
00212 
00213       *condition = 1;
00214 
00215       if (pthread_mutex_unlock (mut2) != 0)
00216        {
00217          puts ("parent: mutex_unlock failed");
00218          exit (1);
00219        }
00220 
00221       puts ("waiting for child");
00222 
00223       waitpid (pid, &status, 0);
00224       result |= status;
00225 
00226       puts ("parent done");
00227     }
00228 
00229  return result;
00230 }
00231 
00232 #define TEST_FUNCTION do_test ()
00233 #include "../test-skeleton.c"