]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/uclibc/lib/contrib/uclibc/test/nptl/tst-stack1.c
update
[l4.git] / l4 / pkg / uclibc / lib / contrib / uclibc / test / nptl / tst-stack1.c
1 /* Copyright (C) 2002, 2003 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20 #include <limits.h>
21 #include <pthread.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <sys/param.h>
25 #include <unistd.h>
26
27
28 static void *stack;
29 static size_t size;
30
31
32 static void *
33 tf (void *a)
34 {
35   int result = 0;
36
37   puts ("child start");
38
39   pthread_attr_t attr;
40   if (pthread_getattr_np (pthread_self (), &attr) != 0)
41     {
42       puts ("getattr_np failed");
43       exit (1);
44     }
45
46   size_t test_size;
47   void *test_stack;
48   if (pthread_attr_getstack (&attr, &test_stack, &test_size) != 0)
49     {
50       puts ("attr_getstack failed");
51       exit (1);
52     }
53
54   if (test_size != size)
55     {
56       printf ("child: reported size differs: is %zu, expected %zu\n",
57               test_size, size);
58       result = 1;
59     }
60
61   if (test_stack != stack)
62     {
63       printf ("child: reported stack address differs: is %p, expected %p\n",
64               test_stack, stack);
65       result = 1;
66     }
67
68   puts ("child OK");
69
70   return result ? (void *) 1l : NULL;
71 }
72
73
74 int
75 do_test (void)
76 {
77   int result = 0;
78
79   size = MAX (4 * getpagesize (), PTHREAD_STACK_MIN);
80   if (posix_memalign (&stack, getpagesize (), size) != 0)
81     {
82       puts ("out of memory while allocating the stack memory");
83       exit (1);
84     }
85
86   pthread_attr_t attr;
87   if (pthread_attr_init (&attr) != 0)
88     {
89       puts ("attr_init failed");
90       exit (1);
91     }
92
93   puts ("attr_setstack");
94   if (pthread_attr_setstack (&attr, stack, size) != 0)
95     {
96       puts ("attr_setstack failed");
97       exit (1);
98     }
99
100   size_t test_size;
101   void *test_stack;
102   puts ("attr_getstack");
103   if (pthread_attr_getstack (&attr, &test_stack, &test_size) != 0)
104     {
105       puts ("attr_getstack failed");
106       exit (1);
107     }
108
109   if (test_size != size)
110     {
111       printf ("reported size differs: is %zu, expected %zu\n",
112               test_size, size);
113       result = 1;
114     }
115
116   if (test_stack != stack)
117     {
118       printf ("reported stack address differs: is %p, expected %p\n",
119               test_stack, stack);
120       result = 1;
121     }
122
123   puts ("create");
124
125   pthread_t th;
126   if (pthread_create (&th, &attr, tf, NULL) != 0)
127     {
128       puts ("failed to create thread");
129       exit (1);
130     }
131
132   void *status;
133   if (pthread_join (th, &status) != 0)
134     {
135       puts ("join failed");
136       exit (1);
137     }
138
139   result |= status != NULL;
140
141   return result;
142 }
143
144
145 #define TEST_FUNCTION do_test ()
146 #include "../test-skeleton.c"