Back to index

glibc  2.9
tst-cond7.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 Jakub Jelinek <jakub@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 <stdbool.h>
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <string.h>
00026 #include <time.h>
00027 #include <sys/time.h>
00028 
00029 
00030 typedef struct
00031   {
00032     pthread_cond_t cond;
00033     pthread_mutex_t lock;
00034     pthread_t h;
00035   } T;
00036 
00037 
00038 static volatile bool done;
00039 
00040 
00041 static void *
00042 tf (void *arg)
00043 {
00044   puts ("child created");
00045 
00046   if (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL) != 0
00047       || pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, NULL) != 0)
00048     {
00049       puts ("cannot set cancellation options");
00050       exit (1);
00051     }
00052 
00053   T *t = (T *) arg;
00054 
00055   if (pthread_mutex_lock (&t->lock) != 0)
00056     {
00057       puts ("child: lock failed");
00058       exit (1);
00059     }
00060 
00061   done = true;
00062 
00063   if (pthread_cond_signal (&t->cond) != 0)
00064     {
00065       puts ("child: cond_signal failed");
00066       exit (1);
00067     }
00068 
00069   if (pthread_cond_wait (&t->cond, &t->lock) != 0)
00070     {
00071       puts ("child: cond_wait failed");
00072       exit (1);
00073     }
00074 
00075   if (pthread_mutex_unlock (&t->lock) != 0)
00076     {
00077       puts ("child: unlock failed");
00078       exit (1);
00079     }
00080 
00081   return NULL;
00082 }
00083 
00084 
00085 static int
00086 do_test (void)
00087 {
00088   int i;
00089 #define N 100
00090   T *t[N];
00091   for (i = 0; i < N; ++i)
00092     {
00093       printf ("round %d\n", i);
00094 
00095       t[i] = (T *) malloc (sizeof (T));
00096       if (t[i] == NULL)
00097        {
00098          puts ("out of memory");
00099          exit (1);
00100        }
00101 
00102       if (pthread_mutex_init (&t[i]->lock, NULL) != 0
00103          || pthread_cond_init (&t[i]->cond, NULL) != 0)
00104        {
00105          puts ("an _init function failed");
00106          exit (1);
00107        }
00108 
00109       if (pthread_mutex_lock (&t[i]->lock) != 0)
00110        {
00111          puts ("initial mutex_lock failed");
00112          exit (1);
00113        }
00114 
00115       done = false;
00116 
00117       if (pthread_create (&t[i]->h, NULL, tf, t[i]) != 0)
00118        {
00119          puts ("pthread_create failed");
00120          exit (1);
00121        }
00122 
00123       do
00124        if (pthread_cond_wait (&t[i]->cond, &t[i]->lock) != 0)
00125          {
00126            puts ("cond_wait failed");
00127            exit (1);
00128          }
00129       while (! done);
00130 
00131       /* Release the lock since the cancel handler will get it.  */
00132       if (pthread_mutex_unlock (&t[i]->lock) != 0)
00133        {
00134          puts ("mutex_unlock failed");
00135          exit (1);
00136        }
00137 
00138       if (pthread_cancel (t[i]->h) != 0)
00139        {
00140          puts ("cancel failed");
00141          exit (1);
00142        }
00143 
00144       puts ("parent: joining now");
00145 
00146       void *result;
00147       if (pthread_join (t[i]->h, &result) != 0)
00148        {
00149          puts ("join failed");
00150          exit (1);
00151        }
00152 
00153       if (result != PTHREAD_CANCELED)
00154        {
00155          puts ("result != PTHREAD_CANCELED");
00156          exit (1);
00157        }
00158     }
00159 
00160   for (i = 0; i < N; ++i)
00161     free (t[i]);
00162 
00163   return 0;
00164 }
00165 
00166 
00167 #define TEST_FUNCTION do_test ()
00168 #include "../test-skeleton.c"