Back to index

glibc  2.9
tst-cond1.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 <error.h>
00021 #include <pthread.h>
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 
00025 
00026 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
00027 static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
00028 
00029 
00030 static void *
00031 tf (void *p)
00032 {
00033   int err;
00034 
00035   err = pthread_mutex_lock (&mut);
00036   if (err != 0)
00037     error (EXIT_FAILURE, err, "child: cannot get mutex");
00038 
00039   puts ("child: got mutex; signalling");
00040 
00041   pthread_cond_signal (&cond);
00042 
00043   puts ("child: unlock");
00044 
00045   err = pthread_mutex_unlock (&mut);
00046   if (err != 0)
00047     error (EXIT_FAILURE, err, "child: cannot unlock");
00048 
00049   puts ("child: done");
00050 
00051   return NULL;
00052 }
00053 
00054 
00055 static int
00056 do_test (void)
00057 {
00058   pthread_t th;
00059   int err;
00060 
00061   printf ("&cond = %p\n&mut = %p\n", &cond, &mut);
00062 
00063   puts ("parent: get mutex");
00064 
00065   err = pthread_mutex_lock (&mut);
00066   if (err != 0)
00067     error (EXIT_FAILURE, err, "parent: cannot get mutex");
00068 
00069   puts ("parent: create child");
00070 
00071   err = pthread_create (&th, NULL, tf, NULL);
00072   if (err != 0)
00073     error (EXIT_FAILURE, err, "parent: cannot create thread");
00074 
00075   puts ("parent: wait for condition");
00076 
00077   err = pthread_cond_wait (&cond, &mut);
00078   if (err != 0)
00079     error (EXIT_FAILURE, err, "parent: cannot wait fir signal");
00080 
00081   puts ("parent: got signal");
00082 
00083   err = pthread_join (th, NULL);
00084   if (err != 0)
00085     error (EXIT_FAILURE, err, "parent: failed to join");
00086 
00087   puts ("done");
00088 
00089   return 0;
00090 }
00091 
00092 
00093 #define TEST_FUNCTION do_test ()
00094 #include "../test-skeleton.c"