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