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