Back to index

glibc  2.9
tst-stack1.c
Go to the documentation of this file.
00001 /* Copyright (C) 2002, 2003 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 <limits.h>
00021 #include <pthread.h>
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <sys/param.h>
00025 #include <unistd.h>
00026 
00027 
00028 static void *stack;
00029 static size_t size;
00030 
00031 
00032 static void *
00033 tf (void *a)
00034 {
00035   int result = 0;
00036 
00037   puts ("child start");
00038 
00039   pthread_attr_t attr;
00040   if (pthread_getattr_np (pthread_self (), &attr) != 0)
00041     {
00042       puts ("getattr_np failed");
00043       exit (1);
00044     }
00045 
00046   size_t test_size;
00047   void *test_stack;
00048   if (pthread_attr_getstack (&attr, &test_stack, &test_size) != 0)
00049     {
00050       puts ("attr_getstack failed");
00051       exit (1);
00052     }
00053 
00054   if (test_size != size)
00055     {
00056       printf ("child: reported size differs: is %zu, expected %zu\n",
00057              test_size, size);
00058       result = 1;
00059     }
00060 
00061   if (test_stack != stack)
00062     {
00063       printf ("child: reported stack address differs: is %p, expected %p\n",
00064              test_stack, stack);
00065       result = 1;
00066     }
00067 
00068   puts ("child OK");
00069 
00070   return result ? (void *) 1l : NULL;
00071 }
00072 
00073 
00074 int
00075 do_test (void)
00076 {
00077   int result = 0;
00078 
00079   size = MAX (4 * getpagesize (), PTHREAD_STACK_MIN);
00080   if (posix_memalign (&stack, getpagesize (), size) != 0)
00081     {
00082       puts ("out of memory while allocating the stack memory");
00083       exit (1);
00084     }
00085 
00086   pthread_attr_t attr;
00087   if (pthread_attr_init (&attr) != 0)
00088     {
00089       puts ("attr_init failed");
00090       exit (1);
00091     }
00092 
00093   puts ("attr_setstack");
00094   if (pthread_attr_setstack (&attr, stack, size) != 0)
00095     {
00096       puts ("attr_setstack failed");
00097       exit (1);
00098     }
00099 
00100   size_t test_size;
00101   void *test_stack;
00102   puts ("attr_getstack");
00103   if (pthread_attr_getstack (&attr, &test_stack, &test_size) != 0)
00104     {
00105       puts ("attr_getstack failed");
00106       exit (1);
00107     }
00108 
00109   if (test_size != size)
00110     {
00111       printf ("reported size differs: is %zu, expected %zu\n",
00112              test_size, size);
00113       result = 1;
00114     }
00115 
00116   if (test_stack != stack)
00117     {
00118       printf ("reported stack address differs: is %p, expected %p\n",
00119              test_stack, stack);
00120       result = 1;
00121     }
00122 
00123   puts ("create");
00124 
00125   pthread_t th;
00126   if (pthread_create (&th, &attr, tf, NULL) != 0)
00127     {
00128       puts ("failed to create thread");
00129       exit (1);
00130     }
00131 
00132   void *status;
00133   if (pthread_join (th, &status) != 0)
00134     {
00135       puts ("join failed");
00136       exit (1);
00137     }
00138 
00139   result |= status != NULL;
00140 
00141   return result;
00142 }
00143 
00144 
00145 #define TEST_FUNCTION do_test ()
00146 #include "../test-skeleton.c"