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