Partially based on a patch by Petr Baudis <pasky@suse.cz>.
[glibc.git] / nptl / tst-mutexpp10.c
blob78281a4d3b6ea5af2feb5a93a4c051d0f33a5956
1 /* Copyright (C) 2006 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Jakub Jelinek <jakub@redhat.com>, 2006.
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <limits.h>
22 #include <pthread.h>
23 #include <sched.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
29 #include "tst-tpp.h"
31 static int
32 do_test (void)
34 int ret = 0;
36 init_tpp_test ();
38 pthread_mutexattr_t ma;
39 if (pthread_mutexattr_init (&ma))
41 puts ("mutexattr_init failed");
42 return 1;
44 if (pthread_mutexattr_setprotocol (&ma, PTHREAD_PRIO_PROTECT))
46 puts ("mutexattr_setprotocol failed");
47 return 1;
50 int prioceiling;
51 if (pthread_mutexattr_getprioceiling (&ma, &prioceiling))
53 puts ("mutexattr_getprioceiling failed");
54 return 1;
57 if (prioceiling < fifo_min || prioceiling > fifo_max)
59 printf ("prioceiling %d not in %d..%d range\n",
60 prioceiling, fifo_min, fifo_max);
61 return 1;
64 if (fifo_max < INT_MAX
65 && pthread_mutexattr_setprioceiling (&ma, fifo_max + 1) != EINVAL)
67 printf ("mutexattr_setprioceiling %d did not fail with EINVAL\n",
68 fifo_max + 1);
69 return 1;
72 if (fifo_min > 0
73 && pthread_mutexattr_setprioceiling (&ma, fifo_min - 1) != EINVAL)
75 printf ("mutexattr_setprioceiling %d did not fail with EINVAL\n",
76 fifo_min - 1);
77 return 1;
80 if (pthread_mutexattr_setprioceiling (&ma, fifo_min))
82 puts ("mutexattr_setprioceiling failed");
83 return 1;
86 if (pthread_mutexattr_setprioceiling (&ma, fifo_max))
88 puts ("mutexattr_setprioceiling failed");
89 return 1;
92 if (pthread_mutexattr_setprioceiling (&ma, 6))
94 puts ("mutexattr_setprioceiling failed");
95 return 1;
98 if (pthread_mutexattr_getprioceiling (&ma, &prioceiling))
100 puts ("mutexattr_getprioceiling failed");
101 return 1;
104 if (prioceiling != 6)
106 printf ("mutexattr_getprioceiling returned %d != 6\n",
107 prioceiling);
108 return 1;
111 pthread_mutex_t m1, m2, m3;
112 int e = pthread_mutex_init (&m1, &ma);
113 if (e == ENOTSUP)
115 puts ("cannot support selected type of mutexes");
116 return 0;
118 else if (e != 0)
120 puts ("mutex_init failed");
121 return 1;
124 if (pthread_mutexattr_setprioceiling (&ma, 8))
126 puts ("mutexattr_setprioceiling failed");
127 return 1;
130 if (pthread_mutex_init (&m2, &ma))
132 puts ("mutex_init failed");
133 return 1;
136 if (pthread_mutexattr_setprioceiling (&ma, 5))
138 puts ("mutexattr_setprioceiling failed");
139 return 1;
142 if (pthread_mutex_init (&m3, &ma))
144 puts ("mutex_init failed");
145 return 1;
148 CHECK_TPP_PRIORITY (4, 4);
150 if (pthread_mutex_lock (&m1) != 0)
152 puts ("mutex_lock failed");
153 return 1;
156 CHECK_TPP_PRIORITY (4, 6);
158 if (pthread_mutex_trylock (&m2) != 0)
160 puts ("mutex_lock failed");
161 return 1;
164 CHECK_TPP_PRIORITY (4, 8);
166 if (pthread_mutex_lock (&m3) != 0)
168 puts ("mutex_lock failed");
169 return 1;
172 CHECK_TPP_PRIORITY (4, 8);
174 if (pthread_mutex_unlock (&m2) != 0)
176 puts ("mutex_unlock failed");
177 return 1;
180 CHECK_TPP_PRIORITY (4, 6);
182 if (pthread_mutex_unlock (&m1) != 0)
184 puts ("mutex_unlock failed");
185 return 1;
188 CHECK_TPP_PRIORITY (4, 5);
190 if (pthread_mutex_lock (&m2) != 0)
192 puts ("mutex_lock failed");
193 return 1;
196 CHECK_TPP_PRIORITY (4, 8);
198 if (pthread_mutex_unlock (&m2) != 0)
200 puts ("mutex_unlock failed");
201 return 1;
204 CHECK_TPP_PRIORITY (4, 5);
206 if (pthread_mutex_getprioceiling (&m1, &prioceiling))
208 puts ("mutex_getprioceiling m1 failed");
209 return 1;
211 else if (prioceiling != 6)
213 printf ("unexpected m1 prioceiling %d != 6\n", prioceiling);
214 return 1;
217 if (pthread_mutex_getprioceiling (&m2, &prioceiling))
219 puts ("mutex_getprioceiling m2 failed");
220 return 1;
222 else if (prioceiling != 8)
224 printf ("unexpected m2 prioceiling %d != 8\n", prioceiling);
225 return 1;
228 if (pthread_mutex_getprioceiling (&m3, &prioceiling))
230 puts ("mutex_getprioceiling m3 failed");
231 return 1;
233 else if (prioceiling != 5)
235 printf ("unexpected m3 prioceiling %d != 5\n", prioceiling);
236 return 1;
239 if (pthread_mutex_setprioceiling (&m1, 7, &prioceiling))
241 printf ("mutex_setprioceiling failed");
242 return 1;
244 else if (prioceiling != 6)
246 printf ("unexpected m1 old prioceiling %d != 6\n", prioceiling);
247 return 1;
250 if (pthread_mutex_getprioceiling (&m1, &prioceiling))
252 puts ("mutex_getprioceiling m1 failed");
253 return 1;
255 else if (prioceiling != 7)
257 printf ("unexpected m1 prioceiling %d != 7\n", prioceiling);
258 return 1;
261 CHECK_TPP_PRIORITY (4, 5);
263 if (pthread_mutex_unlock (&m3) != 0)
265 puts ("mutex_unlock failed");
266 return 1;
269 CHECK_TPP_PRIORITY (4, 4);
271 if (pthread_mutex_trylock (&m1) != 0)
273 puts ("mutex_lock failed");
274 return 1;
277 CHECK_TPP_PRIORITY (4, 7);
279 struct sched_param sp;
280 memset (&sp, 0, sizeof (sp));
281 sp.sched_priority = 8;
282 if (pthread_setschedparam (pthread_self (), SCHED_FIFO, &sp))
284 puts ("cannot set scheduling params");
285 return 1;
288 CHECK_TPP_PRIORITY (8, 8);
290 if (pthread_mutex_unlock (&m1) != 0)
292 puts ("mutex_unlock failed");
293 return 1;
296 CHECK_TPP_PRIORITY (8, 8);
298 if (pthread_mutex_lock (&m3) != EINVAL)
300 puts ("pthread_mutex_lock didn't fail with EINVAL");
301 return 1;
304 CHECK_TPP_PRIORITY (8, 8);
306 if (pthread_mutex_destroy (&m1) != 0)
308 puts ("mutex_destroy failed");
309 return 1;
312 if (pthread_mutex_destroy (&m2) != 0)
314 puts ("mutex_destroy failed");
315 return 1;
318 if (pthread_mutex_destroy (&m3) != 0)
320 puts ("mutex_destroy failed");
321 return 1;
324 if (pthread_mutexattr_destroy (&ma) != 0)
326 puts ("mutexattr_destroy failed");
327 return 1;
330 return ret;
333 #define TEST_FUNCTION do_test ()
334 #include "../test-skeleton.c"