Back to index

glibc  2.9
tst-barrier4.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 /* This is a test for behavior not guaranteed by POSIX.  */
00021 #include <errno.h>
00022 #include <pthread.h>
00023 #include <stdio.h>
00024 
00025 
00026 static pthread_barrier_t b1;
00027 static pthread_barrier_t b2;
00028 
00029 
00030 #define N 20
00031 
00032 static void *
00033 tf (void *arg)
00034 {
00035   int round = 0;
00036 
00037   while (round++ < 30)
00038     {
00039       if (pthread_barrier_wait (&b1) == PTHREAD_BARRIER_SERIAL_THREAD)
00040        {
00041          pthread_barrier_destroy (&b1);
00042          if (pthread_barrier_init (&b1, NULL, N) != 0)
00043            {
00044              puts ("tf: 1st barrier_init failed");
00045              exit (1);
00046            }
00047        }
00048 
00049       if (pthread_barrier_wait (&b2) == PTHREAD_BARRIER_SERIAL_THREAD)
00050        {
00051          pthread_barrier_destroy (&b2);
00052          if (pthread_barrier_init (&b2, NULL, N) != 0)
00053            {
00054              puts ("tf: 2nd barrier_init failed");
00055              exit (1);
00056            }
00057        }
00058     }
00059 
00060   return NULL;
00061 }
00062 
00063 
00064 static int
00065 do_test (void)
00066 {
00067   pthread_attr_t at;
00068   int cnt;
00069 
00070   if (pthread_attr_init (&at) != 0)
00071     {
00072       puts ("attr_init failed");
00073       return 1;
00074     }
00075 
00076   if (pthread_attr_setstacksize (&at, 1 * 1024 * 1024) != 0)
00077     {
00078       puts ("attr_setstacksize failed");
00079       return 1;
00080     }
00081 
00082   if (pthread_barrier_init (&b1, NULL, N) != 0)
00083     {
00084       puts ("1st barrier_init failed");
00085       return 1;
00086     }
00087 
00088   if (pthread_barrier_init (&b2, NULL, N) != 0)
00089     {
00090       puts ("2nd barrier_init failed");
00091       return 1;
00092     }
00093 
00094   pthread_t th[N - 1];
00095   for (cnt = 0; cnt < N - 1; ++cnt)
00096     if (pthread_create (&th[cnt], &at, tf, NULL) != 0)
00097       {
00098        puts ("pthread_create failed");
00099        return 1;
00100       }
00101 
00102   if (pthread_attr_destroy (&at) != 0)
00103     {
00104       puts ("attr_destroy failed");
00105       return 1;
00106     }
00107 
00108   tf (NULL);
00109 
00110   for (cnt = 0; cnt < N - 1; ++cnt)
00111     if (pthread_join (th[cnt], NULL) != 0)
00112       {
00113        puts ("pthread_join failed");
00114        return 1;
00115       }
00116 
00117   return 0;
00118 }
00119 
00120 #define TEST_FUNCTION do_test ()
00121 #include "../test-skeleton.c"