1 /* Copyright (C) 2006-2022 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/>. */
36 pthread_mutexattr_t ma
;
37 if (pthread_mutexattr_init (&ma
))
39 puts ("mutexattr_init failed");
42 if (pthread_mutexattr_setprotocol (&ma
, PTHREAD_PRIO_PROTECT
))
44 puts ("mutexattr_setprotocol failed");
49 if (pthread_mutexattr_getprioceiling (&ma
, &prioceiling
))
51 puts ("mutexattr_getprioceiling failed");
55 if (prioceiling
< fifo_min
|| prioceiling
> fifo_max
)
57 printf ("prioceiling %d not in %d..%d range\n",
58 prioceiling
, fifo_min
, fifo_max
);
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",
71 && pthread_mutexattr_setprioceiling (&ma
, fifo_min
- 1) != EINVAL
)
73 printf ("mutexattr_setprioceiling %d did not fail with EINVAL\n",
78 if (pthread_mutexattr_setprioceiling (&ma
, fifo_min
))
80 puts ("mutexattr_setprioceiling failed");
84 if (pthread_mutexattr_setprioceiling (&ma
, fifo_max
))
86 puts ("mutexattr_setprioceiling failed");
90 if (pthread_mutexattr_setprioceiling (&ma
, 6))
92 puts ("mutexattr_setprioceiling failed");
96 if (pthread_mutexattr_getprioceiling (&ma
, &prioceiling
))
98 puts ("mutexattr_getprioceiling failed");
102 if (prioceiling
!= 6)
104 printf ("mutexattr_getprioceiling returned %d != 6\n",
109 pthread_mutex_t m1
, m2
, m3
;
110 int e
= pthread_mutex_init (&m1
, &ma
);
113 puts ("cannot support selected type of mutexes");
118 puts ("mutex_init failed");
122 if (pthread_mutexattr_setprioceiling (&ma
, 8))
124 puts ("mutexattr_setprioceiling failed");
128 if (pthread_mutex_init (&m2
, &ma
))
130 puts ("mutex_init failed");
134 if (pthread_mutexattr_setprioceiling (&ma
, 5))
136 puts ("mutexattr_setprioceiling failed");
140 if (pthread_mutex_init (&m3
, &ma
))
142 puts ("mutex_init failed");
146 CHECK_TPP_PRIORITY (4, 4);
148 if (pthread_mutex_lock (&m1
) != 0)
150 puts ("mutex_lock failed");
154 CHECK_TPP_PRIORITY (4, 6);
156 if (pthread_mutex_trylock (&m2
) != 0)
158 puts ("mutex_lock failed");
162 CHECK_TPP_PRIORITY (4, 8);
164 if (pthread_mutex_lock (&m3
) != 0)
166 puts ("mutex_lock failed");
170 CHECK_TPP_PRIORITY (4, 8);
172 if (pthread_mutex_unlock (&m2
) != 0)
174 puts ("mutex_unlock failed");
178 CHECK_TPP_PRIORITY (4, 6);
180 if (pthread_mutex_unlock (&m1
) != 0)
182 puts ("mutex_unlock failed");
186 CHECK_TPP_PRIORITY (4, 5);
188 if (pthread_mutex_lock (&m2
) != 0)
190 puts ("mutex_lock failed");
194 CHECK_TPP_PRIORITY (4, 8);
196 if (pthread_mutex_unlock (&m2
) != 0)
198 puts ("mutex_unlock failed");
202 CHECK_TPP_PRIORITY (4, 5);
204 if (pthread_mutex_getprioceiling (&m1
, &prioceiling
))
206 puts ("mutex_getprioceiling m1 failed");
209 else if (prioceiling
!= 6)
211 printf ("unexpected m1 prioceiling %d != 6\n", prioceiling
);
215 if (pthread_mutex_getprioceiling (&m2
, &prioceiling
))
217 puts ("mutex_getprioceiling m2 failed");
220 else if (prioceiling
!= 8)
222 printf ("unexpected m2 prioceiling %d != 8\n", prioceiling
);
226 if (pthread_mutex_getprioceiling (&m3
, &prioceiling
))
228 puts ("mutex_getprioceiling m3 failed");
231 else if (prioceiling
!= 5)
233 printf ("unexpected m3 prioceiling %d != 5\n", prioceiling
);
237 if (pthread_mutex_setprioceiling (&m1
, 7, &prioceiling
))
239 printf ("mutex_setprioceiling failed");
242 else if (prioceiling
!= 6)
244 printf ("unexpected m1 old prioceiling %d != 6\n", prioceiling
);
248 if (pthread_mutex_getprioceiling (&m1
, &prioceiling
))
250 puts ("mutex_getprioceiling m1 failed");
253 else if (prioceiling
!= 7)
255 printf ("unexpected m1 prioceiling %d != 7\n", prioceiling
);
259 CHECK_TPP_PRIORITY (4, 5);
261 if (pthread_mutex_unlock (&m3
) != 0)
263 puts ("mutex_unlock failed");
267 CHECK_TPP_PRIORITY (4, 4);
269 if (pthread_mutex_trylock (&m1
) != 0)
271 puts ("mutex_lock failed");
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");
286 CHECK_TPP_PRIORITY (8, 8);
288 if (pthread_mutex_unlock (&m1
) != 0)
290 puts ("mutex_unlock failed");
294 CHECK_TPP_PRIORITY (8, 8);
296 if (pthread_mutex_lock (&m3
) != EINVAL
)
298 puts ("pthread_mutex_lock didn't fail with EINVAL");
302 CHECK_TPP_PRIORITY (8, 8);
304 if (pthread_mutex_destroy (&m1
) != 0)
306 puts ("mutex_destroy failed");
310 if (pthread_mutex_destroy (&m2
) != 0)
312 puts ("mutex_destroy failed");
316 if (pthread_mutex_destroy (&m3
) != 0)
318 puts ("mutex_destroy failed");
322 if (pthread_mutexattr_destroy (&ma
) != 0)
324 puts ("mutexattr_destroy failed");
331 #define TEST_FUNCTION do_test ()
332 #include "../test-skeleton.c"