stdlib: Undo post review change to 16adc58e73f3 [BZ #27749]
[glibc.git] / nptl / tst-mutexpp10.c
blob80f148fce100b3265f0f50c217ed8f9baf558b70
1 /* Copyright (C) 2006-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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/>. */
18 #include <errno.h>
19 #include <limits.h>
20 #include <pthread.h>
21 #include <sched.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
27 #include "tst-tpp.h"
29 static int
30 do_test (void)
32 int ret = 0;
34 init_tpp_test ();
36 pthread_mutexattr_t ma;
37 if (pthread_mutexattr_init (&ma))
39 puts ("mutexattr_init failed");
40 return 1;
42 if (pthread_mutexattr_setprotocol (&ma, PTHREAD_PRIO_PROTECT))
44 puts ("mutexattr_setprotocol failed");
45 return 1;
48 int prioceiling;
49 if (pthread_mutexattr_getprioceiling (&ma, &prioceiling))
51 puts ("mutexattr_getprioceiling failed");
52 return 1;
55 if (prioceiling < fifo_min || prioceiling > fifo_max)
57 printf ("prioceiling %d not in %d..%d range\n",
58 prioceiling, fifo_min, fifo_max);
59 return 1;
62 if (fifo_max < INT_MAX
63 && pthread_mutexattr_setprioceiling (&ma, fifo_max + 1) != EINVAL)
65 printf ("mutexattr_setprioceiling %d did not fail with EINVAL\n",
66 fifo_max + 1);
67 return 1;
70 if (fifo_min > 0
71 && pthread_mutexattr_setprioceiling (&ma, fifo_min - 1) != EINVAL)
73 printf ("mutexattr_setprioceiling %d did not fail with EINVAL\n",
74 fifo_min - 1);
75 return 1;
78 if (pthread_mutexattr_setprioceiling (&ma, fifo_min))
80 puts ("mutexattr_setprioceiling failed");
81 return 1;
84 if (pthread_mutexattr_setprioceiling (&ma, fifo_max))
86 puts ("mutexattr_setprioceiling failed");
87 return 1;
90 if (pthread_mutexattr_setprioceiling (&ma, 6))
92 puts ("mutexattr_setprioceiling failed");
93 return 1;
96 if (pthread_mutexattr_getprioceiling (&ma, &prioceiling))
98 puts ("mutexattr_getprioceiling failed");
99 return 1;
102 if (prioceiling != 6)
104 printf ("mutexattr_getprioceiling returned %d != 6\n",
105 prioceiling);
106 return 1;
109 pthread_mutex_t m1, m2, m3;
110 int e = pthread_mutex_init (&m1, &ma);
111 if (e == ENOTSUP)
113 puts ("cannot support selected type of mutexes");
114 return 0;
116 else if (e != 0)
118 puts ("mutex_init failed");
119 return 1;
122 if (pthread_mutexattr_setprioceiling (&ma, 8))
124 puts ("mutexattr_setprioceiling failed");
125 return 1;
128 if (pthread_mutex_init (&m2, &ma))
130 puts ("mutex_init failed");
131 return 1;
134 if (pthread_mutexattr_setprioceiling (&ma, 5))
136 puts ("mutexattr_setprioceiling failed");
137 return 1;
140 if (pthread_mutex_init (&m3, &ma))
142 puts ("mutex_init failed");
143 return 1;
146 CHECK_TPP_PRIORITY (4, 4);
148 if (pthread_mutex_lock (&m1) != 0)
150 puts ("mutex_lock failed");
151 return 1;
154 CHECK_TPP_PRIORITY (4, 6);
156 if (pthread_mutex_trylock (&m2) != 0)
158 puts ("mutex_lock failed");
159 return 1;
162 CHECK_TPP_PRIORITY (4, 8);
164 if (pthread_mutex_lock (&m3) != 0)
166 puts ("mutex_lock failed");
167 return 1;
170 CHECK_TPP_PRIORITY (4, 8);
172 if (pthread_mutex_unlock (&m2) != 0)
174 puts ("mutex_unlock failed");
175 return 1;
178 CHECK_TPP_PRIORITY (4, 6);
180 if (pthread_mutex_unlock (&m1) != 0)
182 puts ("mutex_unlock failed");
183 return 1;
186 CHECK_TPP_PRIORITY (4, 5);
188 if (pthread_mutex_lock (&m2) != 0)
190 puts ("mutex_lock failed");
191 return 1;
194 CHECK_TPP_PRIORITY (4, 8);
196 if (pthread_mutex_unlock (&m2) != 0)
198 puts ("mutex_unlock failed");
199 return 1;
202 CHECK_TPP_PRIORITY (4, 5);
204 if (pthread_mutex_getprioceiling (&m1, &prioceiling))
206 puts ("mutex_getprioceiling m1 failed");
207 return 1;
209 else if (prioceiling != 6)
211 printf ("unexpected m1 prioceiling %d != 6\n", prioceiling);
212 return 1;
215 if (pthread_mutex_getprioceiling (&m2, &prioceiling))
217 puts ("mutex_getprioceiling m2 failed");
218 return 1;
220 else if (prioceiling != 8)
222 printf ("unexpected m2 prioceiling %d != 8\n", prioceiling);
223 return 1;
226 if (pthread_mutex_getprioceiling (&m3, &prioceiling))
228 puts ("mutex_getprioceiling m3 failed");
229 return 1;
231 else if (prioceiling != 5)
233 printf ("unexpected m3 prioceiling %d != 5\n", prioceiling);
234 return 1;
237 if (pthread_mutex_setprioceiling (&m1, 7, &prioceiling))
239 printf ("mutex_setprioceiling failed");
240 return 1;
242 else if (prioceiling != 6)
244 printf ("unexpected m1 old prioceiling %d != 6\n", prioceiling);
245 return 1;
248 if (pthread_mutex_getprioceiling (&m1, &prioceiling))
250 puts ("mutex_getprioceiling m1 failed");
251 return 1;
253 else if (prioceiling != 7)
255 printf ("unexpected m1 prioceiling %d != 7\n", prioceiling);
256 return 1;
259 CHECK_TPP_PRIORITY (4, 5);
261 if (pthread_mutex_unlock (&m3) != 0)
263 puts ("mutex_unlock failed");
264 return 1;
267 CHECK_TPP_PRIORITY (4, 4);
269 if (pthread_mutex_trylock (&m1) != 0)
271 puts ("mutex_lock failed");
272 return 1;
275 CHECK_TPP_PRIORITY (4, 7);
277 struct sched_param sp;
278 memset (&sp, 0, sizeof (sp));
279 sp.sched_priority = 8;
280 if (pthread_setschedparam (pthread_self (), SCHED_FIFO, &sp))
282 puts ("cannot set scheduling params");
283 return 1;
286 CHECK_TPP_PRIORITY (8, 8);
288 if (pthread_mutex_unlock (&m1) != 0)
290 puts ("mutex_unlock failed");
291 return 1;
294 CHECK_TPP_PRIORITY (8, 8);
296 if (pthread_mutex_lock (&m3) != EINVAL)
298 puts ("pthread_mutex_lock didn't fail with EINVAL");
299 return 1;
302 CHECK_TPP_PRIORITY (8, 8);
304 if (pthread_mutex_destroy (&m1) != 0)
306 puts ("mutex_destroy failed");
307 return 1;
310 if (pthread_mutex_destroy (&m2) != 0)
312 puts ("mutex_destroy failed");
313 return 1;
316 if (pthread_mutex_destroy (&m3) != 0)
318 puts ("mutex_destroy failed");
319 return 1;
322 if (pthread_mutexattr_destroy (&ma) != 0)
324 puts ("mutexattr_destroy failed");
325 return 1;
328 return ret;
331 #define TEST_FUNCTION do_test ()
332 #include "../test-skeleton.c"