Back to index

glibc  2.9
tst-cond15.c
Go to the documentation of this file.
00001 /* Copyright (C) 2004 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@redhat.com>, 2004.
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 <unistd.h>
00026 #include <sys/time.h>
00027 
00028 
00029 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
00030 static pthread_mutex_t mut = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
00031 static pthread_mutex_t mut2 = PTHREAD_MUTEX_INITIALIZER;
00032 
00033 static void *
00034 tf (void *p)
00035 {
00036   if (pthread_mutex_lock (&mut) != 0)
00037     {
00038       printf ("%s: 1st mutex_lock failed\n", __func__);
00039       exit (1);
00040     }
00041   if (pthread_mutex_lock (&mut) != 0)
00042     {
00043       printf ("%s: 2nd mutex_lock failed\n", __func__);
00044       exit (1);
00045     }
00046   if (pthread_mutex_lock (&mut) != 0)
00047     {
00048       printf ("%s: 3rd mutex_lock failed\n", __func__);
00049       exit (1);
00050     }
00051 
00052   if (pthread_mutex_unlock (&mut2) != 0)
00053     {
00054       printf ("%s: mutex_unlock failed\n", __func__);
00055       exit (1);
00056     }
00057 
00058   struct timeval tv;
00059   gettimeofday (&tv, NULL);
00060   struct timespec ts;
00061   TIMEVAL_TO_TIMESPEC (&tv, &ts);
00062   ts.tv_sec += p == NULL ? 100 : 1;
00063 
00064   int err = pthread_cond_timedwait (&cond, &mut, &ts);
00065   if ((err != 0 && p == NULL) || (err != ETIMEDOUT && p != NULL))
00066     {
00067       printf ("%s: cond_wait failed\n", __func__);
00068       exit (1);
00069     }
00070 
00071   if (pthread_mutex_unlock (&mut) != 0)
00072     {
00073       printf ("%s: 1st mutex_unlock failed\n", __func__);
00074       exit (1);
00075     }
00076   if (pthread_mutex_unlock (&mut) != 0)
00077     {
00078       printf ("%s: 2nd mutex_unlock failed\n", __func__);
00079       exit (1);
00080     }
00081   if (pthread_mutex_unlock (&mut) != 0)
00082     {
00083       printf ("%s: 3rd mutex_unlock failed\n", __func__);
00084       exit (1);
00085     }
00086 
00087   puts ("child: done");
00088 
00089   return NULL;
00090 }
00091 
00092 
00093 static int
00094 do_test (void)
00095 {
00096   if (pthread_mutex_lock (&mut2) != 0)
00097     {
00098       puts ("1st mutex_lock failed");
00099       return 1;
00100     }
00101 
00102   puts ("parent: create 1st child");
00103 
00104   pthread_t th;
00105   int err = pthread_create (&th, NULL, tf, NULL);
00106   if (err != 0)
00107     {
00108       printf ("parent: cannot 1st create thread: %s\n", strerror (err));
00109       return 1;
00110     }
00111 
00112   /* We have to synchronize with the child.  */
00113   if (pthread_mutex_lock (&mut2) != 0)
00114     {
00115       puts ("2nd mutex_lock failed");
00116       return 1;
00117     }
00118 
00119   /* Give the child to reach to pthread_cond_wait.  */
00120   sleep (1);
00121 
00122   if (pthread_cond_signal (&cond) != 0)
00123     {
00124       puts ("cond_signal failed");
00125       return 1;
00126     }
00127 
00128   err = pthread_join (th, NULL);
00129   if (err != 0)
00130     {
00131       printf ("parent: failed to join: %s\n", strerror (err));
00132       return 1;
00133     }
00134 
00135 
00136   puts ("parent: create 2nd child");
00137 
00138   err = pthread_create (&th, NULL, tf, (void *) 1l);
00139   if (err != 0)
00140     {
00141       printf ("parent: cannot 2nd create thread: %s\n", strerror (err));
00142       return 1;
00143     }
00144 
00145   err = pthread_join (th, NULL);
00146   if (err != 0)
00147     {
00148       printf ("parent: failed to join: %s\n", strerror (err));
00149       return 1;
00150     }
00151 
00152   puts ("done");
00153 
00154   return 0;
00155 }
00156 
00157 
00158 #define TEST_FUNCTION do_test ()
00159 #define TIMEOUT 6
00160 #include "../test-skeleton.c"