File: tst-mutex8.c

package info (click to toggle)
glibc 2.19-18%2Bdeb8u4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-backports
  • size: 204,416 kB
  • ctags: 144,800
  • sloc: ansic: 970,361; asm: 241,207; sh: 10,069; makefile: 8,475; cpp: 3,595; perl: 2,077; pascal: 1,839; awk: 1,704; yacc: 317; sed: 73
file content (403 lines) | stat: -rw-r--r-- 8,476 bytes parent folder | download | duplicates (10)
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/* Copyright (C) 2003-2014 Free Software Foundation, Inc.
   This file is part of the GNU C Library.
   Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.

   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/>.  */

/* This test checks behavior not required by POSIX.  */
#include <errno.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>


static pthread_mutex_t *m;
static pthread_barrier_t b;
static pthread_cond_t c;
static bool done;


static void
cl (void *arg)
{
  if (pthread_mutex_unlock (m) != 0)
    {
      puts ("cl: mutex_unlocked failed");
      exit (1);
    }
}


static void *
tf (void *arg)
{
  if (pthread_mutex_lock (m) != 0)
    {
      puts ("tf: mutex_lock failed");
      return (void *) 1l;
    }

  int e = pthread_barrier_wait (&b);
  if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
    {
      puts ("barrier_wait failed");
      return (void *) 1l;
    }

  if (arg == NULL)
    do
      if (pthread_cond_wait (&c, m) != 0)
	{
	  puts ("tf: cond_wait failed");
	  return (void *) 1l;
	}
    while (! done);
  else
    do
      {
	pthread_cleanup_push (cl, NULL);

	if (pthread_cond_wait (&c, m) != 0)
	  {
	    puts ("tf: cond_wait failed");
	    return (void *) 1l;
	  }

	pthread_cleanup_pop (0);
      }
    while (! done);

  if (pthread_mutex_unlock (m) != 0)
    {
      puts ("tf: mutex_unlock failed");
      return (void *) 1l;
    }

  return NULL;
}


static int
check_type (const char *mas, pthread_mutexattr_t *ma)
{
  int e;

  e = pthread_mutex_init (m, ma);
  if (e != 0)
    {
#ifdef ENABLE_PI
      if (e == ENOTSUP)
	{
	  puts ("PI mutexes unsupported");
	  return 0;
	}
#endif
      printf ("1st mutex_init failed for %s\n", mas);
      return 1;
    }

  if (pthread_mutex_destroy (m) != 0)
    {
      printf ("immediate mutex_destroy failed for %s\n", mas);
      return 1;
    }

  if (pthread_mutex_init (m, ma) != 0)
    {
      printf ("2nd mutex_init failed for %s\n", mas);
      return 1;
    }

  if (pthread_mutex_lock (m) != 0)
    {
      printf ("1st mutex_lock failed for %s\n", mas);
      return 1;
    }

  /* Elided mutexes don't fail destroy. If elision is not explicitly disabled
     we don't know, so can also not check this.  */
#ifndef ENABLE_LOCK_ELISION
  e = pthread_mutex_destroy (m);
  if (e == 0)
    {
      printf ("mutex_destroy of self-locked mutex succeeded for %s\n", mas);
      return 1;
    }
  if (e != EBUSY)
    {
      printf ("mutex_destroy of self-locked mutex did not return EBUSY %s\n",
	      mas);
      return 1;
    }
#endif

  if (pthread_mutex_unlock (m) != 0)
    {
      printf ("1st mutex_unlock failed for %s\n", mas);
      return 1;
    }

  if (pthread_mutex_trylock (m) != 0)
    {
      printf ("mutex_trylock failed for %s\n", mas);
      return 1;
    }

  /* Elided mutexes don't fail destroy.  */
#ifndef ENABLE_LOCK_ELISION
  e = pthread_mutex_destroy (m);
  if (e == 0)
    {
      printf ("mutex_destroy of self-trylocked mutex succeeded for %s\n", mas);
      return 1;
    }
  if (e != EBUSY)
    {
      printf ("\
mutex_destroy of self-trylocked mutex did not return EBUSY %s\n",
	      mas);
      return 1;
    }
#endif

  if (pthread_mutex_unlock (m) != 0)
    {
      printf ("2nd mutex_unlock failed for %s\n", mas);
      return 1;
    }

  pthread_t th;
  if (pthread_create (&th, NULL, tf, NULL) != 0)
    {
      puts ("1st create failed");
      return 1;
    }
  done = false;

  e = pthread_barrier_wait (&b);
  if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
    {
      puts ("1st barrier_wait failed");
      return 1;
    }

  if (pthread_mutex_lock (m) != 0)
    {
      printf ("2nd mutex_lock failed for %s\n", mas);
      return 1;
    }

  if (pthread_mutex_unlock (m) != 0)
    {
      printf ("3rd mutex_unlock failed for %s\n", mas);
      return 1;
    }

  /* Elided mutexes don't fail destroy.  */
#ifndef ENABLE_LOCK_ELISION
  e = pthread_mutex_destroy (m);
  if (e == 0)
    {
      printf ("mutex_destroy of condvar-used mutex succeeded for %s\n", mas);
      return 1;
    }
  if (e != EBUSY)
    {
      printf ("\
mutex_destroy of condvar-used mutex did not return EBUSY for %s\n", mas);
      return 1;
    }
#endif

  done = true;
  if (pthread_cond_signal (&c) != 0)
    {
      puts ("cond_signal failed");
      return 1;
    }

  void *r;
  if (pthread_join (th, &r) != 0)
    {
      puts ("join failed");
      return 1;
    }
  if (r != NULL)
    {
      puts ("thread didn't return NULL");
      return 1;
    }

  if (pthread_mutex_destroy (m) != 0)
    {
      printf ("mutex_destroy after condvar-use failed for %s\n", mas);
      return 1;
    }

  if (pthread_mutex_init (m, ma) != 0)
    {
      printf ("3rd mutex_init failed for %s\n", mas);
      return 1;
    }

  if (pthread_create (&th, NULL, tf, (void *) 1) != 0)
    {
      puts ("2nd create failed");
      return 1;
    }
  done = false;

  e = pthread_barrier_wait (&b);
  if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
    {
      puts ("2nd barrier_wait failed");
      return 1;
    }

  if (pthread_mutex_lock (m) != 0)
    {
      printf ("3rd mutex_lock failed for %s\n", mas);
      return 1;
    }

  if (pthread_mutex_unlock (m) != 0)
    {
      printf ("4th mutex_unlock failed for %s\n", mas);
      return 1;
    }

  /* Elided mutexes don't fail destroy.  */
#ifndef ENABLE_LOCK_ELISION
  e = pthread_mutex_destroy (m);
  if (e == 0)
    {
      printf ("2nd mutex_destroy of condvar-used mutex succeeded for %s\n",
	      mas);
      return 1;
    }
  if (e != EBUSY)
    {
      printf ("\
2nd mutex_destroy of condvar-used mutex did not return EBUSY for %s\n",
	      mas);
      return 1;
    }
#endif

  if (pthread_cancel (th) != 0)
    {
      puts ("cond_cancel failed");
      return 1;
    }

  if (pthread_join (th, &r) != 0)
    {
      puts ("join failed");
      return 1;
    }
  if (r != PTHREAD_CANCELED)
    {
      puts ("thread not canceled");
      return 1;
    }

  if (pthread_mutex_destroy (m) != 0)
    {
      printf ("mutex_destroy after condvar-canceled failed for %s\n", mas);
      return 1;
    }

  return 0;
}


static int
do_test (void)
{
  pthread_mutex_t mm;
  m = &mm;

  if (pthread_barrier_init (&b, NULL, 2) != 0)
    {
      puts ("barrier_init failed");
      return 1;
    }

  if (pthread_cond_init (&c, NULL) != 0)
    {
      puts ("cond_init failed");
      return 1;
    }

  puts ("check normal mutex");
  int res = check_type ("normal", NULL);

  pthread_mutexattr_t ma;
  if (pthread_mutexattr_init (&ma) != 0)
    {
      puts ("1st mutexattr_init failed");
      return 1;
    }
  if (pthread_mutexattr_settype (&ma, PTHREAD_MUTEX_RECURSIVE) != 0)
    {
      puts ("1st mutexattr_settype failed");
      return 1;
    }
#ifdef ENABLE_PI
  if (pthread_mutexattr_setprotocol (&ma, PTHREAD_PRIO_INHERIT))
    {
      puts ("1st pthread_mutexattr_setprotocol failed");
      return 1;
    }
#endif
  puts ("check recursive mutex");
  res |= check_type ("recursive", &ma);
  if (pthread_mutexattr_destroy (&ma) != 0)
    {
      puts ("1st mutexattr_destroy failed");
      return 1;
    }

  if (pthread_mutexattr_init (&ma) != 0)
    {
      puts ("2nd mutexattr_init failed");
      return 1;
    }
  if (pthread_mutexattr_settype (&ma, PTHREAD_MUTEX_ERRORCHECK) != 0)
    {
      puts ("2nd mutexattr_settype failed");
      return 1;
    }
#ifdef ENABLE_PI
  if (pthread_mutexattr_setprotocol (&ma, PTHREAD_PRIO_INHERIT))
    {
      puts ("2nd pthread_mutexattr_setprotocol failed");
      return 1;
    }
#endif
  puts ("check error-checking mutex");
  res |= check_type ("error-checking", &ma);
  if (pthread_mutexattr_destroy (&ma) != 0)
    {
      puts ("2nd mutexattr_destroy failed");
      return 1;
    }

  return res;
}

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