Back to index

glibc  2.9
tst-cond3.c
Go to the documentation of this file.
00001 /* Copyright (C) 2002 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 <pthread.h>
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024 #include <unistd.h>
00025 
00026 
00027 /* Note that this test requires more than the standard.  It is
00028    required that there are no spurious wakeups if only more readers
00029    are added.  This is a reasonable demand.  */
00030 
00031 
00032 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
00033 static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
00034 
00035 
00036 #define N 10
00037 
00038 
00039 static void *
00040 tf (void *arg)
00041 {
00042   int i = (long int) arg;
00043   int err;
00044 
00045   /* Get the mutex.  */
00046   err = pthread_mutex_lock (&mut);
00047   if (err != 0)
00048     {
00049       printf ("child %d mutex_lock failed: %s\n", i, strerror (err));
00050       exit (1);
00051     }
00052 
00053   /* This call should never return.  */
00054   pthread_cond_wait (&cond, &mut);
00055 
00056   /* We should never get here.  */
00057   exit (1);
00058 
00059   return NULL;
00060 }
00061 
00062 
00063 static int
00064 do_test (void)
00065 {
00066   int err;
00067   int i;
00068 
00069   for (i = 0; i < N; ++i)
00070     {
00071       pthread_t th;
00072 
00073       if (i != 0)
00074        {
00075          /* Release the mutex.  */
00076          err = pthread_mutex_unlock (&mut);
00077          if (err != 0)
00078            {
00079              printf ("mutex_unlock %d failed: %s\n", i, strerror (err));
00080              return 1;
00081            }
00082        }
00083 
00084       err = pthread_create (&th, NULL, tf, (void *) (long int) i);
00085       if (err != 0)
00086        {
00087          printf ("create %d failed: %s\n", i, strerror (err));
00088          return 1;
00089        }
00090 
00091       /* Get the mutex.  */
00092       err = pthread_mutex_lock (&mut);
00093       if (err != 0)
00094        {
00095          printf ("mutex_lock %d failed: %s\n", i, strerror (err));
00096          return 1;
00097        }
00098     }
00099 
00100   /* Set an alarm for 1 second.  The wrapper will expect this.  */
00101   alarm (1);
00102 
00103   /* This call should never return.  */
00104   pthread_cond_wait (&cond, &mut);
00105 
00106   puts ("cond_wait returned");
00107   return 1;
00108 }
00109 
00110 
00111 #define EXPECTED_SIGNAL SIGALRM
00112 #define TEST_FUNCTION do_test ()
00113 #include "../test-skeleton.c"