Back to index

glibc  2.9
tst-stack1.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 pthread_create/pthread_join with user defined stacks.  */
00021 
00022 #include <limits.h>
00023 #include <pthread.h>
00024 #include <stdlib.h>
00025 #include <stdio.h>
00026 #include <string.h>
00027 #include <unistd.h>
00028 
00029 static int seen;
00030 
00031 static void *
00032 tf (void *p)
00033 {
00034   ++seen;
00035   return NULL;
00036 }
00037 
00038 #define N 16
00039 
00040 static int
00041 do_test (void)
00042 {
00043   void *stack;
00044   int res = posix_memalign (&stack, getpagesize (), N * 4 * PTHREAD_STACK_MIN);
00045   if (res)
00046     {
00047       printf ("malloc failed %s\n", strerror (res));
00048       return 1;
00049     }
00050 
00051   pthread_attr_t attr;
00052   pthread_attr_init (&attr);
00053 
00054   int result = 0;
00055   for (int i = 0; i < N; ++i)
00056     {
00057       res = pthread_attr_setstack (&attr, stack + i * 4 * PTHREAD_STACK_MIN,
00058                                4 * PTHREAD_STACK_MIN);
00059       if (res)
00060        {
00061          printf ("pthread_attr_setstack failed %d\n", res);
00062          result = 1;
00063          continue;
00064        }
00065 
00066       /* Create the thread.  */
00067       pthread_t th;
00068       res = pthread_create (&th, &attr, tf, NULL);
00069       if (res)
00070        {
00071          printf ("pthread_create failed %d\n", res);
00072          result = 1;
00073        }
00074       else
00075        {
00076          res = pthread_join (th, NULL);
00077          if (res)
00078            {
00079              printf ("pthread_join failed %d\n", res);
00080              result = 1;
00081            }
00082        }
00083     }
00084 
00085   pthread_attr_destroy (&attr);
00086 
00087   if (seen != N)
00088     {
00089       printf ("seen %d != %d\n", seen, N);
00090       result = 1;
00091     }
00092 
00093   return result;
00094 }
00095 
00096 #define TEST_FUNCTION do_test ()
00097 #include "../test-skeleton.c"