Back to index

glibc  2.9
tst-join4.c
Go to the documentation of this file.
00001 /* Copyright (C) 2003, 2004 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
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 <errno.h>
00021 #include <pthread.h>
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 
00025 
00026 static pthread_barrier_t bar;
00027 
00028 
00029 static void *
00030 tf (void *arg)
00031 {
00032   if (pthread_barrier_wait (&bar) != 0)
00033     {
00034       puts ("tf: barrier_wait failed");
00035       exit (1);
00036     }
00037 
00038   return (void *) 1l;
00039 }
00040 
00041 
00042 static int
00043 do_test (void)
00044 {
00045   if (pthread_barrier_init (&bar, NULL, 3) != 0)
00046     {
00047       puts ("barrier_init failed");
00048       exit (1);
00049     }
00050 
00051   pthread_attr_t a;
00052 
00053   if (pthread_attr_init (&a) != 0)
00054     {
00055       puts ("attr_init failed");
00056       exit (1);
00057     }
00058 
00059   if (pthread_attr_setstacksize (&a, 1 * 1024 * 1024) != 0)
00060     {
00061       puts ("attr_setstacksize failed");
00062       return 1;
00063     }
00064 
00065   pthread_t th[2];
00066 
00067   if (pthread_create (&th[0], &a, tf, NULL) != 0)
00068     {
00069       puts ("1st create failed");
00070       exit (1);
00071     }
00072 
00073   if (pthread_attr_setdetachstate (&a, PTHREAD_CREATE_DETACHED) != 0)
00074     {
00075       puts ("attr_setdetachstate failed");
00076       exit (1);
00077     }
00078 
00079   if (pthread_create (&th[1], &a, tf, NULL) != 0)
00080     {
00081       puts ("1st create failed");
00082       exit (1);
00083     }
00084 
00085   if (pthread_attr_destroy (&a) != 0)
00086     {
00087       puts ("attr_destroy failed");
00088       exit (1);
00089     }
00090 
00091   if (pthread_detach (th[0]) != 0)
00092     {
00093       puts ("could not detach 1st thread");
00094       exit (1);
00095     }
00096 
00097   int err = pthread_detach (th[0]);
00098   if (err == 0)
00099     {
00100       puts ("second detach of 1st thread succeeded");
00101       exit (1);
00102     }
00103   if (err != EINVAL)
00104     {
00105       printf ("second detach of 1st thread returned %d, not EINVAL\n", err);
00106       exit (1);
00107     }
00108 
00109   err = pthread_detach (th[1]);
00110   if (err == 0)
00111     {
00112       puts ("detach of 2nd thread succeeded");
00113       exit (1);
00114     }
00115   if (err != EINVAL)
00116     {
00117       printf ("detach of 2nd thread returned %d, not EINVAL\n", err);
00118       exit (1);
00119     }
00120 
00121   exit (0);
00122 }
00123 
00124 #define TEST_FUNCTION do_test ()
00125 #include "../test-skeleton.c"