Back to index

glibc  2.9
tst-stack3.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 Jakub Jelinek <jakub@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 /* Test whether pthread_create/pthread_join with user defined stacks
00021    doesn't leak memory.
00022    NOTE: this tests functionality beyond POSIX.  In POSIX user defined
00023    stacks cannot be ever freed once used by pthread_create nor they can
00024    be reused for other thread.  */
00025 
00026 #include <limits.h>
00027 #include <mcheck.h>
00028 #include <pthread.h>
00029 #include <stdlib.h>
00030 #include <stdio.h>
00031 #include <string.h>
00032 #include <unistd.h>
00033 
00034 static int seen;
00035 
00036 static void *
00037 tf (void *p)
00038 {
00039   ++seen;
00040   return NULL;
00041 }
00042 
00043 static int
00044 do_test (void)
00045 {
00046   mtrace ();
00047 
00048   void *stack;
00049   int res = posix_memalign (&stack, getpagesize (), 4 * PTHREAD_STACK_MIN);
00050   if (res)
00051     {
00052       printf ("malloc failed %s\n", strerror (res));
00053       return 1;
00054     }
00055 
00056   pthread_attr_t attr;
00057   pthread_attr_init (&attr);
00058 
00059   int result = 0;
00060   res = pthread_attr_setstack (&attr, stack, 4 * PTHREAD_STACK_MIN);
00061   if (res)
00062     {
00063       printf ("pthread_attr_setstack failed %d\n", res);
00064       result = 1;
00065     }
00066 
00067   for (int i = 0; i < 16; ++i)
00068     {
00069       /* Create the thread.  */
00070       pthread_t th;
00071       res = pthread_create (&th, &attr, tf, NULL);
00072       if (res)
00073        {
00074          printf ("pthread_create failed %d\n", res);
00075          result = 1;
00076        }
00077       else
00078        {
00079          res = pthread_join (th, NULL);
00080          if (res)
00081            {
00082              printf ("pthread_join failed %d\n", res);
00083              result = 1;
00084            }
00085        }
00086     }
00087 
00088   pthread_attr_destroy (&attr);
00089 
00090   if (seen != 16)
00091     {
00092       printf ("seen %d != 16\n", seen);
00093       result = 1;
00094     }
00095 
00096   free (stack);
00097   return result;
00098 }
00099 
00100 #define TEST_FUNCTION do_test ()
00101 #include "../test-skeleton.c"