Back to index

glibc  2.9
tst-basic3.c
Go to the documentation of this file.
00001 /* Copyright (C) 2003 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 <pthread.h>
00021 #include <signal.h>
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <unistd.h>
00025 
00026 
00027 static int nrunning = 1;
00028 
00029 
00030 static void
00031 final_test (void)
00032 {
00033   puts ("final_test has been called");
00034 
00035 #define THE_SIGNAL SIGUSR1
00036   kill (getpid (), SIGUSR1);
00037 }
00038 
00039 
00040 static void *
00041 tf (void *a)
00042 {
00043   if (pthread_join ((pthread_t) a, NULL) != 0)
00044     {
00045       printf ("join failed while %d are running\n", nrunning);
00046       _exit (1);
00047     }
00048 
00049   printf ("%2d left\n", --nrunning);
00050 
00051   return NULL;
00052 }
00053 
00054 
00055 int
00056 do_test (void)
00057 {
00058 #define N 20
00059   pthread_t t[N];
00060   pthread_t last = pthread_self ();
00061   int i;
00062 
00063   atexit (final_test);
00064 
00065   printf ("starting %d + 1 threads\n", N);
00066   for (i = 0; i < N; ++i)
00067     {
00068       if (pthread_create (&t[i], NULL, tf, (void *) last) != 0)
00069        {
00070          puts ("create failed");
00071          _exit (1);
00072        }
00073 
00074       ++nrunning;
00075 
00076       last = t[i];
00077     }
00078 
00079   printf ("%2d left\n", --nrunning);
00080 
00081   pthread_exit (NULL);
00082 }
00083 
00084 
00085 #define EXPECTED_SIGNAL THE_SIGNAL
00086 #define TEST_FUNCTION do_test ()
00087 #include "../test-skeleton.c"