Back to index

glibc  2.9
tst-cond2.c
Go to the documentation of this file.
00001 /* Copyright (C) 2002, 2003, 2004 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_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
00027 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
00028 
00029 static pthread_barrier_t bar;
00030 
00031 
00032 static void *
00033 tf (void *a)
00034 {
00035   int i = (long int) a;
00036   int err;
00037 
00038   printf ("child %d: lock\n", i);
00039 
00040   err = pthread_mutex_lock (&mut);
00041   if (err != 0)
00042     error (EXIT_FAILURE, err, "locking in child failed");
00043 
00044   printf ("child %d: sync\n", i);
00045 
00046   int e = pthread_barrier_wait (&bar);
00047   if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
00048     {
00049       puts ("child: barrier_wait failed");
00050       exit (1);
00051     }
00052 
00053   printf ("child %d: wait\n", i);
00054 
00055   err = pthread_cond_wait (&cond, &mut);
00056   if (err != 0)
00057     error (EXIT_FAILURE, err, "child %d: failed to wait", i);
00058 
00059   printf ("child %d: woken up\n", i);
00060 
00061   err = pthread_mutex_unlock (&mut);
00062   if (err != 0)
00063     error (EXIT_FAILURE, err, "child %d: unlock[2] failed", i);
00064 
00065   printf ("child %d: done\n", i);
00066 
00067   return NULL;
00068 }
00069 
00070 
00071 #define N 10
00072 
00073 
00074 static int
00075 do_test (void)
00076 {
00077   pthread_t th[N];
00078   int i;
00079   int err;
00080 
00081   printf ("&cond = %p\n&mut = %p\n", &cond, &mut);
00082 
00083   if (pthread_barrier_init (&bar, NULL, 2) != 0)
00084     {
00085       puts ("barrier_init failed");
00086       exit (1);
00087     }
00088 
00089   pthread_attr_t at;
00090 
00091   if (pthread_attr_init (&at) != 0)
00092     {
00093       puts ("attr_init failed");
00094       return 1;
00095     }
00096 
00097   if (pthread_attr_setstacksize (&at, 1 * 1024 * 1024) != 0)
00098     {
00099       puts ("attr_setstacksize failed");
00100       return 1;
00101     }
00102 
00103   for (i = 0; i < N; ++i)
00104     {
00105       printf ("create thread %d\n", i);
00106 
00107       err = pthread_create (&th[i], &at, tf, (void *) (long int) i);
00108       if (err != 0)
00109        error (EXIT_FAILURE, err, "cannot create thread %d", i);
00110 
00111       printf ("wait for child %d\n", i);
00112 
00113       /* Wait for the child to start up and get the mutex for the
00114         conditional variable.  */
00115       int e = pthread_barrier_wait (&bar);
00116       if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
00117        {
00118          puts ("barrier_wait failed");
00119          exit (1);
00120        }
00121     }
00122 
00123   if (pthread_attr_destroy (&at) != 0)
00124     {
00125       puts ("attr_destroy failed");
00126       return 1;
00127     }
00128 
00129   puts ("get lock outselves");
00130 
00131   err = pthread_mutex_lock (&mut);
00132   if (err != 0)
00133     error (EXIT_FAILURE, err, "mut locking failed");
00134 
00135   puts ("broadcast");
00136 
00137   /* Wake up all threads.  */
00138   err = pthread_cond_broadcast (&cond);
00139   if (err != 0)
00140     error (EXIT_FAILURE, err, "parent: broadcast failed");
00141 
00142   err = pthread_mutex_unlock (&mut);
00143   if (err != 0)
00144     error (EXIT_FAILURE, err, "mut unlocking failed");
00145 
00146   /* Join all threads.  */
00147   for (i = 0; i < N; ++i)
00148     {
00149       printf ("join thread %d\n", i);
00150 
00151       err = pthread_join (th[i], NULL);
00152       if (err != 0)
00153        error (EXIT_FAILURE, err, "join of child %d failed", i);
00154     }
00155 
00156   puts ("done");
00157 
00158   return 0;
00159 }
00160 
00161 
00162 #define TEST_FUNCTION do_test ()
00163 #include "../test-skeleton.c"