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