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