Back to index

glibc  2.9
tst-cond8.c
Go to the documentation of this file.
00001 /* Copyright (C) 2003 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
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 <time.h>
00025 #include <sys/time.h>
00026 
00027 
00028 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
00029 static pthread_mutex_t mut = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
00030 
00031 static pthread_barrier_t bar;
00032 
00033 
00034 static void
00035 ch (void *arg)
00036 {
00037   int e = pthread_mutex_lock (&mut);
00038   if (e == 0)
00039     {
00040       puts ("mutex not locked at all by cond_wait");
00041       exit (1);
00042     }
00043 
00044   if (e != EDEADLK)
00045     {
00046       puts ("no deadlock error signaled");
00047       exit (1);
00048     }
00049 
00050   if (pthread_mutex_unlock (&mut) != 0)
00051     {
00052       puts ("ch: cannot unlock mutex");
00053       exit (1);
00054     }
00055 
00056   puts ("ch done");
00057 }
00058 
00059 
00060 static void *
00061 tf1 (void *p)
00062 {
00063   int err;
00064 
00065   if (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL) != 0
00066       || pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, NULL) != 0)
00067     {
00068       puts ("cannot set cancellation options");
00069       exit (1);
00070     }
00071 
00072   err = pthread_mutex_lock (&mut);
00073   if (err != 0)
00074     {
00075       puts ("child: cannot get mutex");
00076       exit (1);
00077     }
00078 
00079   err = pthread_barrier_wait (&bar);
00080   if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
00081     {
00082       printf ("barrier_wait returned %d\n", err);
00083       exit (1);
00084     }
00085 
00086   puts ("child: got mutex; waiting");
00087 
00088   pthread_cleanup_push (ch, NULL);
00089 
00090   pthread_cond_wait (&cond, &mut);
00091 
00092   pthread_cleanup_pop (0);
00093 
00094   puts ("child: cond_wait should not have returned");
00095 
00096   return NULL;
00097 }
00098 
00099 
00100 static void *
00101 tf2 (void *p)
00102 {
00103   int err;
00104 
00105   if (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL) != 0
00106       || pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, NULL) != 0)
00107     {
00108       puts ("cannot set cancellation options");
00109       exit (1);
00110     }
00111 
00112   err = pthread_mutex_lock (&mut);
00113   if (err != 0)
00114     {
00115       puts ("child: cannot get mutex");
00116       exit (1);
00117     }
00118 
00119   err = pthread_barrier_wait (&bar);
00120   if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
00121     {
00122       printf ("barrier_wait returned %d\n", err);
00123       exit (1);
00124     }
00125 
00126   puts ("child: got mutex; waiting");
00127 
00128   pthread_cleanup_push (ch, NULL);
00129 
00130   /* Current time.  */
00131   struct timeval tv;
00132   (void) gettimeofday (&tv, NULL);
00133   /* +1000 seconds in correct format.  */
00134   struct timespec ts;
00135   TIMEVAL_TO_TIMESPEC (&tv, &ts);
00136   ts.tv_sec += 1000;
00137 
00138   pthread_cond_timedwait (&cond, &mut, &ts);
00139 
00140   pthread_cleanup_pop (0);
00141 
00142   puts ("child: cond_wait should not have returned");
00143 
00144   return NULL;
00145 }
00146 
00147 
00148 static int
00149 do_test (void)
00150 {
00151   pthread_t th;
00152   int err;
00153 
00154   printf ("&cond = %p\n&mut = %p\n", &cond, &mut);
00155 
00156   puts ("parent: get mutex");
00157 
00158   err = pthread_barrier_init (&bar, NULL, 2);
00159   if (err != 0)
00160     {
00161       puts ("parent: cannot init barrier");
00162       exit (1);
00163     }
00164 
00165   puts ("parent: create child");
00166 
00167   err = pthread_create (&th, NULL, tf1, NULL);
00168   if (err != 0)
00169     {
00170       puts ("parent: cannot create thread");
00171       exit (1);
00172     }
00173 
00174   puts ("parent: wait for child to lock mutex");
00175 
00176   err = pthread_barrier_wait (&bar);
00177   if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
00178     {
00179       puts ("parent: cannot wait for barrier");
00180       exit (1);
00181     }
00182 
00183   err = pthread_mutex_lock (&mut);
00184   if (err != 0)
00185     {
00186       puts ("parent: mutex_lock failed");
00187       exit (1);
00188     }
00189 
00190   err = pthread_mutex_unlock (&mut);
00191   if (err != 0)
00192     {
00193       puts ("parent: mutex_unlock failed");
00194       exit (1);
00195     }
00196 
00197   if (pthread_cancel (th) != 0)
00198     {
00199       puts ("cannot cancel thread");
00200       exit (1);
00201     }
00202 
00203   void *r;
00204   err = pthread_join (th, &r);
00205   if (err != 0)
00206     {
00207       puts ("parent: failed to join");
00208       exit (1);
00209     }
00210 
00211   if (r != PTHREAD_CANCELED)
00212     {
00213       puts ("child hasn't been canceled");
00214       exit (1);
00215     }
00216 
00217 
00218 
00219   puts ("parent: create 2nd child");
00220 
00221   err = pthread_create (&th, NULL, tf2, NULL);
00222   if (err != 0)
00223     {
00224       puts ("parent: cannot create thread");
00225       exit (1);
00226     }
00227 
00228   puts ("parent: wait for child to lock mutex");
00229 
00230   err = pthread_barrier_wait (&bar);
00231   if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
00232     {
00233       puts ("parent: cannot wait for barrier");
00234       exit (1);
00235     }
00236 
00237   err = pthread_mutex_lock (&mut);
00238   if (err != 0)
00239     {
00240       puts ("parent: mutex_lock failed");
00241       exit (1);
00242     }
00243 
00244   err = pthread_mutex_unlock (&mut);
00245   if (err != 0)
00246     {
00247       puts ("parent: mutex_unlock failed");
00248       exit (1);
00249     }
00250 
00251   if (pthread_cancel (th) != 0)
00252     {
00253       puts ("cannot cancel thread");
00254       exit (1);
00255     }
00256 
00257   err = pthread_join (th, &r);
00258   if (err != 0)
00259     {
00260       puts ("parent: failed to join");
00261       exit (1);
00262     }
00263 
00264   if (r != PTHREAD_CANCELED)
00265     {
00266       puts ("child hasn't been canceled");
00267       exit (1);
00268     }
00269 
00270   puts ("done");
00271 
00272   return 0;
00273 }
00274 
00275 
00276 #define TEST_FUNCTION do_test ()
00277 #include "../test-skeleton.c"