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