about summary refs log tree commit diff
path: root/nptl/tst-mutexpp10.c
blob: 1e8ba5bb1b5b01fad47f00e78d21485cdc087497 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/* Copyright (C) 2006-2018 Free Software Foundation, Inc.
   This file is part of the GNU C Library.
   Contributed by Jakub Jelinek <jakub@redhat.com>, 2006.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <http://www.gnu.org/licenses/>.  */

#include <errno.h>
#include <limits.h>
#include <pthread.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "tst-tpp.h"

static int
do_test (void)
{
  int ret = 0;

  init_tpp_test ();

  pthread_mutexattr_t ma;
  if (pthread_mutexattr_init (&ma))
    {
      puts ("mutexattr_init failed");
      return 1;
    }
  if (pthread_mutexattr_setprotocol (&ma, PTHREAD_PRIO_PROTECT))
    {
      puts ("mutexattr_setprotocol failed");
      return 1;
    }

  int prioceiling;
  if (pthread_mutexattr_getprioceiling (&ma, &prioceiling))
    {
      puts ("mutexattr_getprioceiling failed");
      return 1;
    }

  if (prioceiling < fifo_min || prioceiling > fifo_max)
    {
      printf ("prioceiling %d not in %d..%d range\n",
	      prioceiling, fifo_min, fifo_max);
      return 1;
    }

  if (fifo_max < INT_MAX
      && pthread_mutexattr_setprioceiling (&ma, fifo_max + 1) != EINVAL)
    {
      printf ("mutexattr_setprioceiling %d did not fail with EINVAL\n",
	      fifo_max + 1);
      return 1;
    }

  if (fifo_min > 0
      && pthread_mutexattr_setprioceiling (&ma, fifo_min - 1) != EINVAL)
    {
      printf ("mutexattr_setprioceiling %d did not fail with EINVAL\n",
	      fifo_min - 1);
      return 1;
    }

  if (pthread_mutexattr_setprioceiling (&ma, fifo_min))
    {
      puts ("mutexattr_setprioceiling failed");
      return 1;
    }

  if (pthread_mutexattr_setprioceiling (&ma, fifo_max))
    {
      puts ("mutexattr_setprioceiling failed");
      return 1;
    }

  if (pthread_mutexattr_setprioceiling (&ma, 6))
    {
      puts ("mutexattr_setprioceiling failed");
      return 1;
    }

  if (pthread_mutexattr_getprioceiling (&ma, &prioceiling))
    {
      puts ("mutexattr_getprioceiling failed");
      return 1;
    }

  if (prioceiling != 6)
    {
      printf ("mutexattr_getprioceiling returned %d != 6\n",
	      prioceiling);
      return 1;
    }

  pthread_mutex_t m1, m2, m3;
  int e = pthread_mutex_init (&m1, &ma);
  if (e == ENOTSUP)
    {
      puts ("cannot support selected type of mutexes");
      return 0;
    }
  else if (e != 0)
    {
      puts ("mutex_init failed");
      return 1;
    }

  if (pthread_mutexattr_setprioceiling (&ma, 8))
    {
      puts ("mutexattr_setprioceiling failed");
      return 1;
    }

  if (pthread_mutex_init (&m2, &ma))
    {
      puts ("mutex_init failed");
      return 1;
    }

  if (pthread_mutexattr_setprioceiling (&ma, 5))
    {
      puts ("mutexattr_setprioceiling failed");
      return 1;
    }

  if (pthread_mutex_init (&m3, &ma))
    {
      puts ("mutex_init failed");
      return 1;
    }

  CHECK_TPP_PRIORITY (4, 4);

  if (pthread_mutex_lock (&m1) != 0)
    {
      puts ("mutex_lock failed");
      return 1;
    }

  CHECK_TPP_PRIORITY (4, 6);

  if (pthread_mutex_trylock (&m2) != 0)
    {
      puts ("mutex_lock failed");
      return 1;
    }

  CHECK_TPP_PRIORITY (4, 8);

  if (pthread_mutex_lock (&m3) != 0)
    {
      puts ("mutex_lock failed");
      return 1;
    }

  CHECK_TPP_PRIORITY (4, 8);

  if (pthread_mutex_unlock (&m2) != 0)
    {
      puts ("mutex_unlock failed");
      return 1;
    }

  CHECK_TPP_PRIORITY (4, 6);

  if (pthread_mutex_unlock (&m1) != 0)
    {
      puts ("mutex_unlock failed");
      return 1;
    }

  CHECK_TPP_PRIORITY (4, 5);

  if (pthread_mutex_lock (&m2) != 0)
    {
      puts ("mutex_lock failed");
      return 1;
    }

  CHECK_TPP_PRIORITY (4, 8);

  if (pthread_mutex_unlock (&m2) != 0)
    {
      puts ("mutex_unlock failed");
      return 1;
    }

  CHECK_TPP_PRIORITY (4, 5);

  if (pthread_mutex_getprioceiling (&m1, &prioceiling))
    {
      puts ("mutex_getprioceiling m1 failed");
      return 1;
    }
  else if (prioceiling != 6)
    {
      printf ("unexpected m1 prioceiling %d != 6\n", prioceiling);
      return 1;
    }

  if (pthread_mutex_getprioceiling (&m2, &prioceiling))
    {
      puts ("mutex_getprioceiling m2 failed");
      return 1;
    }
  else if (prioceiling != 8)
    {
      printf ("unexpected m2 prioceiling %d != 8\n", prioceiling);
      return 1;
    }

  if (pthread_mutex_getprioceiling (&m3, &prioceiling))
    {
      puts ("mutex_getprioceiling m3 failed");
      return 1;
    }
  else if (prioceiling != 5)
    {
      printf ("unexpected m3 prioceiling %d != 5\n", prioceiling);
      return 1;
    }

  if (pthread_mutex_setprioceiling (&m1, 7, &prioceiling))
    {
      printf ("mutex_setprioceiling failed");
      return 1;
    }
  else if (prioceiling != 6)
    {
      printf ("unexpected m1 old prioceiling %d != 6\n", prioceiling);
      return 1;
    }

  if (pthread_mutex_getprioceiling (&m1, &prioceiling))
    {
      puts ("mutex_getprioceiling m1 failed");
      return 1;
    }
  else if (prioceiling != 7)
    {
      printf ("unexpected m1 prioceiling %d != 7\n", prioceiling);
      return 1;
    }

  CHECK_TPP_PRIORITY (4, 5);

  if (pthread_mutex_unlock (&m3) != 0)
    {
      puts ("mutex_unlock failed");
      return 1;
    }

  CHECK_TPP_PRIORITY (4, 4);

  if (pthread_mutex_trylock (&m1) != 0)
    {
      puts ("mutex_lock failed");
      return 1;
    }

  CHECK_TPP_PRIORITY (4, 7);

  struct sched_param sp;
  memset (&sp, 0, sizeof (sp));
  sp.sched_priority = 8;
  if (pthread_setschedparam (pthread_self (), SCHED_FIFO, &sp))
    {
      puts ("cannot set scheduling params");
      return 1;
    }

  CHECK_TPP_PRIORITY (8, 8);

  if (pthread_mutex_unlock (&m1) != 0)
    {
      puts ("mutex_unlock failed");
      return 1;
    }

  CHECK_TPP_PRIORITY (8, 8);

  if (pthread_mutex_lock (&m3) != EINVAL)
    {
      puts ("pthread_mutex_lock didn't fail with EINVAL");
      return 1;
    }

  CHECK_TPP_PRIORITY (8, 8);

  if (pthread_mutex_destroy (&m1) != 0)
    {
      puts ("mutex_destroy failed");
      return 1;
    }

  if (pthread_mutex_destroy (&m2) != 0)
    {
      puts ("mutex_destroy failed");
      return 1;
    }

  if (pthread_mutex_destroy (&m3) != 0)
    {
      puts ("mutex_destroy failed");
      return 1;
    }

  if (pthread_mutexattr_destroy (&ma) != 0)
    {
      puts ("mutexattr_destroy failed");
      return 1;
    }

  return ret;
}

#define TEST_FUNCTION do_test ()
#include "../test-skeleton.c"