1 /* Copyright (C) 2006-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 <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>
26
27 #include "tst-tpp.h"
28
29 static int
do_test(void)30 do_test (void)
31 {
32 int ret = 0;
33
34 init_tpp_test ();
35
36 pthread_mutexattr_t ma;
37 if (pthread_mutexattr_init (&ma))
38 {
39 puts ("mutexattr_init failed");
40 return 1;
41 }
42 if (pthread_mutexattr_setprotocol (&ma, PTHREAD_PRIO_PROTECT))
43 {
44 puts ("mutexattr_setprotocol failed");
45 return 1;
46 }
47
48 int prioceiling;
49 if (pthread_mutexattr_getprioceiling (&ma, &prioceiling))
50 {
51 puts ("mutexattr_getprioceiling failed");
52 return 1;
53 }
54
55 if (prioceiling < fifo_min || prioceiling > fifo_max)
56 {
57 printf ("prioceiling %d not in %d..%d range\n",
58 prioceiling, fifo_min, fifo_max);
59 return 1;
60 }
61
62 if (fifo_max < INT_MAX
63 && pthread_mutexattr_setprioceiling (&ma, fifo_max + 1) != EINVAL)
64 {
65 printf ("mutexattr_setprioceiling %d did not fail with EINVAL\n",
66 fifo_max + 1);
67 return 1;
68 }
69
70 if (fifo_min > 0
71 && pthread_mutexattr_setprioceiling (&ma, fifo_min - 1) != EINVAL)
72 {
73 printf ("mutexattr_setprioceiling %d did not fail with EINVAL\n",
74 fifo_min - 1);
75 return 1;
76 }
77
78 if (pthread_mutexattr_setprioceiling (&ma, fifo_min))
79 {
80 puts ("mutexattr_setprioceiling failed");
81 return 1;
82 }
83
84 if (pthread_mutexattr_setprioceiling (&ma, fifo_max))
85 {
86 puts ("mutexattr_setprioceiling failed");
87 return 1;
88 }
89
90 if (pthread_mutexattr_setprioceiling (&ma, 6))
91 {
92 puts ("mutexattr_setprioceiling failed");
93 return 1;
94 }
95
96 if (pthread_mutexattr_getprioceiling (&ma, &prioceiling))
97 {
98 puts ("mutexattr_getprioceiling failed");
99 return 1;
100 }
101
102 if (prioceiling != 6)
103 {
104 printf ("mutexattr_getprioceiling returned %d != 6\n",
105 prioceiling);
106 return 1;
107 }
108
109 pthread_mutex_t m1, m2, m3;
110 int e = pthread_mutex_init (&m1, &ma);
111 if (e == ENOTSUP)
112 {
113 puts ("cannot support selected type of mutexes");
114 return 0;
115 }
116 else if (e != 0)
117 {
118 puts ("mutex_init failed");
119 return 1;
120 }
121
122 if (pthread_mutexattr_setprioceiling (&ma, 8))
123 {
124 puts ("mutexattr_setprioceiling failed");
125 return 1;
126 }
127
128 if (pthread_mutex_init (&m2, &ma))
129 {
130 puts ("mutex_init failed");
131 return 1;
132 }
133
134 if (pthread_mutexattr_setprioceiling (&ma, 5))
135 {
136 puts ("mutexattr_setprioceiling failed");
137 return 1;
138 }
139
140 if (pthread_mutex_init (&m3, &ma))
141 {
142 puts ("mutex_init failed");
143 return 1;
144 }
145
146 CHECK_TPP_PRIORITY (4, 4);
147
148 if (pthread_mutex_lock (&m1) != 0)
149 {
150 puts ("mutex_lock failed");
151 return 1;
152 }
153
154 CHECK_TPP_PRIORITY (4, 6);
155
156 if (pthread_mutex_trylock (&m2) != 0)
157 {
158 puts ("mutex_lock failed");
159 return 1;
160 }
161
162 CHECK_TPP_PRIORITY (4, 8);
163
164 if (pthread_mutex_lock (&m3) != 0)
165 {
166 puts ("mutex_lock failed");
167 return 1;
168 }
169
170 CHECK_TPP_PRIORITY (4, 8);
171
172 if (pthread_mutex_unlock (&m2) != 0)
173 {
174 puts ("mutex_unlock failed");
175 return 1;
176 }
177
178 CHECK_TPP_PRIORITY (4, 6);
179
180 if (pthread_mutex_unlock (&m1) != 0)
181 {
182 puts ("mutex_unlock failed");
183 return 1;
184 }
185
186 CHECK_TPP_PRIORITY (4, 5);
187
188 if (pthread_mutex_lock (&m2) != 0)
189 {
190 puts ("mutex_lock failed");
191 return 1;
192 }
193
194 CHECK_TPP_PRIORITY (4, 8);
195
196 if (pthread_mutex_unlock (&m2) != 0)
197 {
198 puts ("mutex_unlock failed");
199 return 1;
200 }
201
202 CHECK_TPP_PRIORITY (4, 5);
203
204 if (pthread_mutex_getprioceiling (&m1, &prioceiling))
205 {
206 puts ("mutex_getprioceiling m1 failed");
207 return 1;
208 }
209 else if (prioceiling != 6)
210 {
211 printf ("unexpected m1 prioceiling %d != 6\n", prioceiling);
212 return 1;
213 }
214
215 if (pthread_mutex_getprioceiling (&m2, &prioceiling))
216 {
217 puts ("mutex_getprioceiling m2 failed");
218 return 1;
219 }
220 else if (prioceiling != 8)
221 {
222 printf ("unexpected m2 prioceiling %d != 8\n", prioceiling);
223 return 1;
224 }
225
226 if (pthread_mutex_getprioceiling (&m3, &prioceiling))
227 {
228 puts ("mutex_getprioceiling m3 failed");
229 return 1;
230 }
231 else if (prioceiling != 5)
232 {
233 printf ("unexpected m3 prioceiling %d != 5\n", prioceiling);
234 return 1;
235 }
236
237 if (pthread_mutex_setprioceiling (&m1, 7, &prioceiling))
238 {
239 printf ("mutex_setprioceiling failed");
240 return 1;
241 }
242 else if (prioceiling != 6)
243 {
244 printf ("unexpected m1 old prioceiling %d != 6\n", prioceiling);
245 return 1;
246 }
247
248 if (pthread_mutex_getprioceiling (&m1, &prioceiling))
249 {
250 puts ("mutex_getprioceiling m1 failed");
251 return 1;
252 }
253 else if (prioceiling != 7)
254 {
255 printf ("unexpected m1 prioceiling %d != 7\n", prioceiling);
256 return 1;
257 }
258
259 CHECK_TPP_PRIORITY (4, 5);
260
261 if (pthread_mutex_unlock (&m3) != 0)
262 {
263 puts ("mutex_unlock failed");
264 return 1;
265 }
266
267 CHECK_TPP_PRIORITY (4, 4);
268
269 if (pthread_mutex_trylock (&m1) != 0)
270 {
271 puts ("mutex_lock failed");
272 return 1;
273 }
274
275 CHECK_TPP_PRIORITY (4, 7);
276
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))
281 {
282 puts ("cannot set scheduling params");
283 return 1;
284 }
285
286 CHECK_TPP_PRIORITY (8, 8);
287
288 if (pthread_mutex_unlock (&m1) != 0)
289 {
290 puts ("mutex_unlock failed");
291 return 1;
292 }
293
294 CHECK_TPP_PRIORITY (8, 8);
295
296 if (pthread_mutex_lock (&m3) != EINVAL)
297 {
298 puts ("pthread_mutex_lock didn't fail with EINVAL");
299 return 1;
300 }
301
302 CHECK_TPP_PRIORITY (8, 8);
303
304 if (pthread_mutex_destroy (&m1) != 0)
305 {
306 puts ("mutex_destroy failed");
307 return 1;
308 }
309
310 if (pthread_mutex_destroy (&m2) != 0)
311 {
312 puts ("mutex_destroy failed");
313 return 1;
314 }
315
316 if (pthread_mutex_destroy (&m3) != 0)
317 {
318 puts ("mutex_destroy failed");
319 return 1;
320 }
321
322 if (pthread_mutexattr_destroy (&ma) != 0)
323 {
324 puts ("mutexattr_destroy failed");
325 return 1;
326 }
327
328 return ret;
329 }
330
331 #define TEST_FUNCTION do_test ()
332 #include "../test-skeleton.c"
333