1 /* Copyright (C) 2003-2022 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3 
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8 
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13 
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, see
16    <https://www.gnu.org/licenses/>.  */
17 
18 #include <errno.h>
19 #include <pthread.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 
24 
25 int
do_test(void)26 do_test (void)
27 {
28   pthread_attr_t a;
29 
30   if (pthread_attr_init (&a) != 0)
31     {
32       puts ("attr_init failed");
33       exit (1);
34     }
35 
36   /* Check default value of detach state.  */
37   int s;
38   if (pthread_attr_getdetachstate (&a, &s) != 0)
39     {
40       puts ("1st attr_getdestachstate failed");
41       exit (1);
42     }
43   if (s != PTHREAD_CREATE_JOINABLE)
44     {
45       printf ("\
46 default detach state wrong: %d, expected %d (PTHREAD_CREATE_JOINABLE)\n",
47 	      s, PTHREAD_CREATE_JOINABLE);
48       exit (1);
49     }
50 
51   int e = pthread_attr_setdetachstate (&a, PTHREAD_CREATE_DETACHED);
52   if (e != 0)
53     {
54       puts ("1st attr_setdetachstate failed");
55       exit (1);
56     }
57   if (pthread_attr_getdetachstate (&a, &s) != 0)
58     {
59       puts ("2nd attr_getdestachstate failed");
60       exit (1);
61     }
62   if (s != PTHREAD_CREATE_DETACHED)
63     {
64       puts ("PTHREAD_CREATE_DETACHED set, but not given back");
65       exit (1);
66     }
67 
68   e = pthread_attr_setdetachstate (&a, PTHREAD_CREATE_JOINABLE);
69   if (e != 0)
70     {
71       puts ("2nd attr_setdetachstate failed");
72       exit (1);
73     }
74   if (pthread_attr_getdetachstate (&a, &s) != 0)
75     {
76       puts ("3rd attr_getdestachstate failed");
77       exit (1);
78     }
79   if (s != PTHREAD_CREATE_JOINABLE)
80     {
81       puts ("PTHREAD_CREATE_JOINABLE set, but not given back");
82       exit (1);
83     }
84 
85 
86   size_t g;
87   if (pthread_attr_getguardsize (&a, &g) != 0)
88     {
89       puts ("1st attr_getguardsize failed");
90       exit (1);
91     }
92   if (g != (size_t) sysconf (_SC_PAGESIZE))
93     {
94       printf ("default guardsize %zu, expected %ld (PAGESIZE)\n",
95 	      g, sysconf (_SC_PAGESIZE));
96       exit (1);
97     }
98 
99   e = pthread_attr_setguardsize (&a, 0);
100   if (e != 0)
101     {
102       puts ("1st attr_setguardsize failed");
103       exit (1);
104     }
105   if (pthread_attr_getguardsize (&a, &g) != 0)
106     {
107       puts ("2nd attr_getguardsize failed");
108       exit (1);
109     }
110   if (g != 0)
111     {
112       printf ("guardsize set to zero but %zu returned\n", g);
113       exit (1);
114     }
115 
116   e = pthread_attr_setguardsize (&a, 1);
117   if (e != 0)
118     {
119       puts ("2nd attr_setguardsize failed");
120       exit (1);
121     }
122   if (pthread_attr_getguardsize (&a, &g) != 0)
123     {
124       puts ("3rd attr_getguardsize failed");
125       exit (1);
126     }
127   if (g != 1)
128     {
129       printf ("guardsize set to 1 but %zu returned\n", g);
130       exit (1);
131     }
132 
133 
134   if (pthread_attr_getinheritsched (&a, &s) != 0)
135     {
136       puts ("1st attr_getinheritsched failed");
137       exit (1);
138     }
139   /* XXX What is the correct default value.  */
140   if (s != PTHREAD_INHERIT_SCHED && s != PTHREAD_EXPLICIT_SCHED)
141     {
142       puts ("incorrect default value for inheritsched");
143       exit (1);
144     }
145 
146   e = pthread_attr_setinheritsched (&a, PTHREAD_EXPLICIT_SCHED);
147   if (e != 0)
148     {
149       puts ("1st attr_setinheritsched failed");
150       exit (1);
151     }
152   if (pthread_attr_getinheritsched (&a, &s) != 0)
153     {
154       puts ("2nd attr_getinheritsched failed");
155       exit (1);
156     }
157   if (s != PTHREAD_EXPLICIT_SCHED)
158     {
159       printf ("inheritsched set to PTHREAD_EXPLICIT_SCHED, but got %d\n", s);
160       exit (1);
161     }
162 
163   e = pthread_attr_setinheritsched (&a, PTHREAD_INHERIT_SCHED);
164   if (e != 0)
165     {
166       puts ("2nd attr_setinheritsched failed");
167       exit (1);
168     }
169   if (pthread_attr_getinheritsched (&a, &s) != 0)
170     {
171       puts ("3rd attr_getinheritsched failed");
172       exit (1);
173     }
174   if (s != PTHREAD_INHERIT_SCHED)
175     {
176       printf ("inheritsched set to PTHREAD_INHERIT_SCHED, but got %d\n", s);
177       exit (1);
178     }
179 
180 
181   if (pthread_attr_getschedpolicy (&a, &s) != 0)
182     {
183       puts ("1st attr_getschedpolicy failed");
184       exit (1);
185     }
186   /* XXX What is the correct default value.  */
187   if (s != SCHED_OTHER && s != SCHED_FIFO && s != SCHED_RR)
188     {
189       puts ("incorrect default value for schedpolicy");
190       exit (1);
191     }
192 
193   e = pthread_attr_setschedpolicy (&a, SCHED_RR);
194   if (e != 0)
195     {
196       puts ("1st attr_setschedpolicy failed");
197       exit (1);
198     }
199   if (pthread_attr_getschedpolicy (&a, &s) != 0)
200     {
201       puts ("2nd attr_getschedpolicy failed");
202       exit (1);
203     }
204   if (s != SCHED_RR)
205     {
206       printf ("schedpolicy set to SCHED_RR, but got %d\n", s);
207       exit (1);
208     }
209 
210   e = pthread_attr_setschedpolicy (&a, SCHED_FIFO);
211   if (e != 0)
212     {
213       puts ("2nd attr_setschedpolicy failed");
214       exit (1);
215     }
216   if (pthread_attr_getschedpolicy (&a, &s) != 0)
217     {
218       puts ("3rd attr_getschedpolicy failed");
219       exit (1);
220     }
221   if (s != SCHED_FIFO)
222     {
223       printf ("schedpolicy set to SCHED_FIFO, but got %d\n", s);
224       exit (1);
225     }
226 
227   e = pthread_attr_setschedpolicy (&a, SCHED_OTHER);
228   if (e != 0)
229     {
230       puts ("3rd attr_setschedpolicy failed");
231       exit (1);
232     }
233   if (pthread_attr_getschedpolicy (&a, &s) != 0)
234     {
235       puts ("4th attr_getschedpolicy failed");
236       exit (1);
237     }
238   if (s != SCHED_OTHER)
239     {
240       printf ("schedpolicy set to SCHED_OTHER, but got %d\n", s);
241       exit (1);
242     }
243 
244 
245   if (pthread_attr_getscope (&a, &s) != 0)
246     {
247       puts ("1st attr_getscope failed");
248       exit (1);
249     }
250   /* XXX What is the correct default value.  */
251   if (s != PTHREAD_SCOPE_SYSTEM && s != PTHREAD_SCOPE_PROCESS)
252     {
253       puts ("incorrect default value for contentionscope");
254       exit (1);
255     }
256 
257   e = pthread_attr_setscope (&a, PTHREAD_SCOPE_PROCESS);
258   if (e != ENOTSUP)
259     {
260       if (e != 0)
261 	{
262 	  puts ("1st attr_setscope failed");
263 	  exit (1);
264 	}
265       if (pthread_attr_getscope (&a, &s) != 0)
266 	{
267 	  puts ("2nd attr_getscope failed");
268 	  exit (1);
269 	}
270       if (s != PTHREAD_SCOPE_PROCESS)
271 	{
272 	  printf ("\
273 contentionscope set to PTHREAD_SCOPE_PROCESS, but got %d\n", s);
274 	  exit (1);
275 	}
276     }
277 
278   e = pthread_attr_setscope (&a, PTHREAD_SCOPE_SYSTEM);
279   if (e != 0)
280     {
281       puts ("2nd attr_setscope failed");
282       exit (1);
283     }
284   if (pthread_attr_getscope (&a, &s) != 0)
285     {
286       puts ("3rd attr_getscope failed");
287       exit (1);
288     }
289   if (s != PTHREAD_SCOPE_SYSTEM)
290     {
291       printf ("contentionscope set to PTHREAD_SCOPE_SYSTEM, but got %d\n", s);
292       exit (1);
293     }
294 
295   char buf[1];
296   e = pthread_attr_setstack (&a, buf, 1);
297   if (e != EINVAL)
298     {
299       puts ("setstack with size 1 did not produce EINVAL");
300       exit (1);
301     }
302 
303   e = pthread_attr_setstacksize (&a, 1);
304   if (e != EINVAL)
305     {
306       puts ("setstacksize with size 1 did not produce EINVAL");
307       exit (1);
308     }
309 
310   return 0;
311 }
312 
313 
314 #define TEST_FUNCTION do_test ()
315 #include "../test-skeleton.c"
316