Back to index

glibc  2.9
tst-cond14.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 <pthread.h>
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024 #include <unistd.h>
00025 
00026 
00027 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
00028 static pthread_mutex_t mut = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
00029 static pthread_mutex_t mut2 = PTHREAD_MUTEX_INITIALIZER;
00030 
00031 static void *
00032 tf (void *p)
00033 {
00034   if (pthread_mutex_lock (&mut) != 0)
00035     {
00036       printf ("%s: 1st mutex_lock failed\n", __func__);
00037       exit (1);
00038     }
00039   if (pthread_mutex_lock (&mut) != 0)
00040     {
00041       printf ("%s: 2nd mutex_lock failed\n", __func__);
00042       exit (1);
00043     }
00044   if (pthread_mutex_lock (&mut) != 0)
00045     {
00046       printf ("%s: 3rd mutex_lock failed\n", __func__);
00047       exit (1);
00048     }
00049 
00050   if (pthread_mutex_unlock (&mut2) != 0)
00051     {
00052       printf ("%s: mutex_unlock failed\n", __func__);
00053       exit (1);
00054     }
00055 
00056   if (pthread_cond_wait (&cond, &mut) != 0)
00057     {
00058       printf ("%s: cond_wait failed\n", __func__);
00059       exit (1);
00060     }
00061 
00062   puts ("child: done");
00063 
00064   return NULL;
00065 }
00066 
00067 
00068 static int
00069 do_test (void)
00070 {
00071   if (pthread_mutex_lock (&mut2) != 0)
00072     {
00073       puts ("1st mutex_lock failed");
00074       return 1;
00075     }
00076 
00077   puts ("parent: create child");
00078 
00079   pthread_t th;
00080   int err = pthread_create (&th, NULL, tf, NULL);
00081   if (err != 0)
00082     {
00083       printf ("parent: cannot create thread: %s\n", strerror (err));
00084       return 1;
00085     }
00086 
00087   /* We have to synchronize with the child.  */
00088   if (pthread_mutex_lock (&mut2) != 0)
00089     {
00090       puts ("2nd mutex_lock failed");
00091       return 1;
00092     }
00093 
00094   /* Give the child to reach to pthread_cond_wait.  */
00095   sleep (1);
00096 
00097   if (pthread_cond_signal (&cond) != 0)
00098     {
00099       puts ("cond_signal failed");
00100       return 1;
00101     }
00102 
00103   err = pthread_join (th, NULL);
00104   if (err != 0)
00105     {
00106       printf ("parent: failed to join: %s\n", strerror (err));
00107       return 1;
00108     }
00109 
00110   puts ("done");
00111 
00112   return 0;
00113 }
00114 
00115 
00116 #define TEST_FUNCTION do_test ()
00117 #define TIMEOUT 3
00118 #include "../test-skeleton.c"