source: vendor/glibc-tests/glibc/nptl/tst-attr1.c

Last change on this file was 2036, checked in by bird, 20 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 6.6 KB
Line 
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
27int
28do_test (void)
29{
30 int i;
31 pthread_attr_t a;
32
33 if (pthread_attr_init (&a) != 0)
34 {
35 puts ("attr_init failed");
36 exit (1);
37 }
38
39 pthread_mutexattr_t ma;
40
41 if (pthread_mutexattr_init (&ma) != 0)
42 {
43 puts ("mutexattr_init failed");
44 exit (1);
45 }
46
47 pthread_rwlockattr_t rwa;
48
49 if (pthread_rwlockattr_init (&rwa) != 0)
50 {
51 puts ("rwlockattr_init failed");
52 exit (1);
53 }
54
55 /* XXX Remove if default value is clear. */
56 pthread_attr_setinheritsched (&a, PTHREAD_INHERIT_SCHED);
57 pthread_attr_setschedpolicy (&a, SCHED_OTHER);
58 pthread_attr_setscope (&a, PTHREAD_SCOPE_SYSTEM);
59
60 for (i = 0; i < 10000; ++i)
61 {
62 long int r = random ();
63
64 if (r != PTHREAD_CREATE_DETACHED && r != PTHREAD_CREATE_JOINABLE)
65 {
66 int e = pthread_attr_setdetachstate (&a, r);
67
68 if (e == 0)
69 {
70 printf ("attr_setdetachstate with value %ld succeeded\n", r);
71 exit (1);
72 }
73 if (e != EINVAL)
74 {
75 puts ("attr_setdetachstate didn't return EINVAL");
76 exit (1);
77 }
78
79 int s;
80 if (pthread_attr_getdetachstate (&a, &s) != 0)
81 {
82 puts ("attr_getdetachstate failed");
83 exit (1);
84 }
85
86 if (s != PTHREAD_CREATE_JOINABLE)
87 {
88 printf ("\
89detach state changed to %d by invalid setdetachstate call\n", s);
90 exit (1);
91 }
92 }
93
94 if (r != PTHREAD_INHERIT_SCHED && r != PTHREAD_EXPLICIT_SCHED)
95 {
96 int e = pthread_attr_setinheritsched (&a, r);
97
98 if (e == 0)
99 {
100 printf ("attr_setinheritsched with value %ld succeeded\n", r);
101 exit (1);
102 }
103 if (e != EINVAL)
104 {
105 puts ("attr_setinheritsched didn't return EINVAL");
106 exit (1);
107 }
108
109 int s;
110 if (pthread_attr_getinheritsched (&a, &s) != 0)
111 {
112 puts ("attr_getinheritsched failed");
113 exit (1);
114 }
115
116 if (s != PTHREAD_INHERIT_SCHED)
117 {
118 printf ("\
119inheritsched changed to %d by invalid setinheritsched call\n", s);
120 exit (1);
121 }
122 }
123
124 if (r != SCHED_OTHER && r != SCHED_RR && r != SCHED_FIFO)
125 {
126 int e = pthread_attr_setschedpolicy (&a, r);
127
128 if (e == 0)
129 {
130 printf ("attr_setschedpolicy with value %ld succeeded\n", r);
131 exit (1);
132 }
133 if (e != EINVAL)
134 {
135 puts ("attr_setschedpolicy didn't return EINVAL");
136 exit (1);
137 }
138
139 int s;
140 if (pthread_attr_getschedpolicy (&a, &s) != 0)
141 {
142 puts ("attr_getschedpolicy failed");
143 exit (1);
144 }
145
146 if (s != SCHED_OTHER)
147 {
148 printf ("\
149schedpolicy changed to %d by invalid setschedpolicy call\n", s);
150 exit (1);
151 }
152 }
153
154 if (r != PTHREAD_SCOPE_SYSTEM && r != PTHREAD_SCOPE_PROCESS)
155 {
156 int e = pthread_attr_setscope (&a, r);
157
158 if (e == 0)
159 {
160 printf ("attr_setscope with value %ld succeeded\n", r);
161 exit (1);
162 }
163 if (e != EINVAL)
164 {
165 puts ("attr_setscope didn't return EINVAL");
166 exit (1);
167 }
168
169 int s;
170 if (pthread_attr_getscope (&a, &s) != 0)
171 {
172 puts ("attr_getscope failed");
173 exit (1);
174 }
175
176 if (s != PTHREAD_SCOPE_SYSTEM)
177 {
178 printf ("\
179contentionscope changed to %d by invalid setscope call\n", s);
180 exit (1);
181 }
182 }
183
184 if (r != PTHREAD_PROCESS_PRIVATE && r != PTHREAD_PROCESS_SHARED)
185 {
186 int e = pthread_mutexattr_setpshared (&ma, r);
187
188 if (e == 0)
189 {
190 printf ("mutexattr_setpshared with value %ld succeeded\n", r);
191 exit (1);
192 }
193 if (e != EINVAL)
194 {
195 puts ("mutexattr_setpshared didn't return EINVAL");
196 exit (1);
197 }
198
199 int s;
200 if (pthread_mutexattr_getpshared (&ma, &s) != 0)
201 {
202 puts ("mutexattr_getpshared failed");
203 exit (1);
204 }
205
206 if (s != PTHREAD_PROCESS_PRIVATE)
207 {
208 printf ("\
209pshared changed to %d by invalid mutexattr_setpshared call\n", s);
210 exit (1);
211 }
212
213 e = pthread_rwlockattr_setpshared (&rwa, r);
214
215 if (e == 0)
216 {
217 printf ("rwlockattr_setpshared with value %ld succeeded\n", r);
218 exit (1);
219 }
220 if (e != EINVAL)
221 {
222 puts ("rwlockattr_setpshared didn't return EINVAL");
223 exit (1);
224 }
225
226 if (pthread_rwlockattr_getpshared (&rwa, &s) != 0)
227 {
228 puts ("rwlockattr_getpshared failed");
229 exit (1);
230 }
231
232 if (s != PTHREAD_PROCESS_PRIVATE)
233 {
234 printf ("\
235pshared changed to %d by invalid rwlockattr_setpshared call\n", s);
236 exit (1);
237 }
238 }
239
240 if (r != PTHREAD_CANCEL_ENABLE && r != PTHREAD_CANCEL_DISABLE)
241 {
242 int e = pthread_setcancelstate (r, NULL);
243
244 if (e == 0)
245 {
246 printf ("setcancelstate with value %ld succeeded\n", r);
247 exit (1);
248 }
249
250 if (e != EINVAL)
251 {
252 puts ("setcancelstate didn't return EINVAL");
253 exit (1);
254 }
255
256 int s;
257 if (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, &s) != 0)
258 {
259 puts ("setcancelstate failed for PTHREAD_CANCEL_ENABLE");
260 exit (1);
261 }
262
263 if (s != PTHREAD_CANCEL_ENABLE)
264 {
265 puts ("invalid setcancelstate changed state");
266 exit (1);
267 }
268 }
269
270 if (r != PTHREAD_CANCEL_DEFERRED && r != PTHREAD_CANCEL_ASYNCHRONOUS)
271 {
272 int e = pthread_setcanceltype (r, NULL);
273
274 if (e == 0)
275 {
276 printf ("setcanceltype with value %ld succeeded\n", r);
277 exit (1);
278 }
279
280 if (e != EINVAL)
281 {
282 puts ("setcanceltype didn't return EINVAL");
283 exit (1);
284 }
285
286 int s;
287 if (pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, &s) != 0)
288 {
289 puts ("setcanceltype failed for PTHREAD_CANCEL_DEFERRED");
290 exit (1);
291 }
292
293 if (s != PTHREAD_CANCEL_DEFERRED)
294 {
295 puts ("invalid setcanceltype changed state");
296 exit (1);
297 }
298 }
299 }
300
301 return 0;
302}
303
304
305#define TEST_FUNCTION do_test ()
306#include "../test-skeleton.c"
Note: See TracBrowser for help on using the repository browser.