]> git.ipfire.org Git - thirdparty/glibc.git/blame - nptl/tst-stack1.c
Initial revision
[thirdparty/glibc.git] / nptl / tst-stack1.c
CommitLineData
76a50749
UD
1/* Copyright (C) 2002 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 <pthread.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <unistd.h>
24
25
26static void *stack;
27static size_t size;
28
29
30static void *
31tf (void *a)
32{
33 int result = 0;
34
35 puts ("child start");
36
37 pthread_attr_t attr;
38 if (pthread_getattr_np (pthread_self (), &attr) != 0)
39 {
40 puts ("getattr_np failed");
41 exit (1);
42 }
43
44 size_t test_size;
45 void *test_stack;
46 if (pthread_attr_getstack (&attr, &test_stack, &test_size) != 0)
47 {
48 puts ("attr_getstack failed");
49 exit (1);
50 }
51
52 if (test_size != size)
53 {
54 printf ("child: reported size differs: is %zu, expected %zu\n",
55 test_size, size);
56 result = 1;
57 }
58
59 if (test_stack != stack)
60 {
61 printf ("child: reported stack address differs: is %p, expected %p\n",
62 test_stack, stack);
63 result = 1;
64 }
65
66 puts ("child OK");
67
68 return result ? (void *) 1l : NULL;
69}
70
71
72int
73do_test (void)
74{
75 int result = 0;
76
77 size = 4 * getpagesize ();
78 if (posix_memalign (&stack, getpagesize (), size) != 0)
79 {
80 puts ("out of memory while allocating the stack memory");
81 exit (1);
82 }
83
84 pthread_attr_t attr;
85 if (pthread_attr_init (&attr) != 0)
86 {
87 puts ("attr_init failed");
88 exit (1);
89 }
90
91 puts ("attr_setstack");
92 if (pthread_attr_setstack (&attr, stack, size) != 0)
93 {
94 puts ("attr_setstack failed");
95 exit (1);
96 }
97
98 size_t test_size;
99 void *test_stack;
100 puts ("attr_getstack");
101 if (pthread_attr_getstack (&attr, &test_stack, &test_size) != 0)
102 {
103 puts ("attr_getstack failed");
104 exit (1);
105 }
106
107 if (test_size != size)
108 {
109 printf ("reported size differs: is %zu, expected %zu\n",
110 test_size, size);
111 result = 1;
112 }
113
114 if (test_stack != stack)
115 {
116 printf ("reported stack address differs: is %p, expected %p\n",
117 test_stack, stack);
118 result = 1;
119 }
120
121 puts ("create");
122
123 pthread_t th;
124 if (pthread_create (&th, &attr, tf, NULL) != 0)
125 {
126 puts ("failed to create thread");
127 exit (1);
128 }
129
130 void *status;
131 if (pthread_join (th, &status) != 0)
132 {
133 puts ("join failed");
134 exit (1);
135 }
136
137 result |= status != NULL;
138
139 return result;
140}
141
142
143#define TEST_FUNCTION do_test ()
144#include "../test-skeleton.c"