summaryrefslogtreecommitdiffstats
path: root/src/pi_tests/tst-mutexpi10.c
blob: 0244b4a7a7c1782a916d6083cbc278eeb38ab584 (plain)
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
/*
   Classic Priority Inversion deadlock test case
  
   Copyright (C) 2006 Free Software Foundation, Inc.
   This file is part of the GNU C Library.
     Contributed by Clark Williams<williams@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; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.  */


/* This program tests Priority Inheritence mutexes and their ability
   to avoid Priority Inversion deadlocks
  
   The basic premise here is to set up a deadlock scenario and confirm that PI
   mutexes resolve the situation. Three worker threads will be created from the
   main thread: low, medium and high priority threads that use SCHED_FIFO as 
   their scheduling policy. The low priority thread claims a mutex and then
   starts "working". The medium priority thread starts and preempts the low 
   priority thread. Then the high priority thread runs and attempts to claim
   the mutex owned by the low priority thread. Without priority inheritance, 
   this will deadlock the program. With priority inheritance, the low priority 
   thread receives a priority boost, finishes it's "work" and releases the mutex,
   which allows the high priority thread to run and finish and then the medium
   priority thread finishes. 
   
   That's the theory, anyway... 
   
   CW - 2006  */

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

/* test timeout */
#define TIMEOUT 2

/* determine if the C library supports Priority Inheritance mutexes */
#if defined(_POSIX_THREAD_PRIO_INHERIT) && _POSIX_THREAD_PRIO_INHERIT != -1
#define HAVE_PI_MUTEX 1
#else
#define HAVE_PI_MUTEX 0
#endif

int use_pi_mutex = HAVE_PI_MUTEX;

#define SUCCESS 0
#define FAILURE 1

/* the number of times we cause a priority inversion situation */
int inversions = 1;

/* the file handle used by the error reporting routine */
FILE *errout;

#define MAIN do_test
#define TEST_FUNCTION do_test(argc, argv)
#define CMDLINE_OPTIONS { "verbose", no_argument, NULL, 1002 }, \
		        { "no-pi", no_argument, NULL, 1003},
#define CMDLINE_PROCESS  \
	case 1002: verbose=1; break;		\
	case 1003: use_pi_mutex = 0; break;
#define CLEANUP_HANDLER cleanup()
int verbose = 0;

/* define priorities for the threads */
#define SKEL_PRIO(x) (x)
#define MAIN_PRIO(x) (x - 1)
#define HIGH_PRIO(x) (x - 2)
#define MED_PRIO(x)  (x - 3)
#define LOW_PRIO(x)  (x - 4)

enum thread_names
{ LOW = 0, MEDIUM, HIGH, NUM_WORKER_THREADS };

pthread_mutex_t mutex;
pthread_mutexattr_t mutex_attr;

pthread_barrier_t all_threads_ready;
pthread_barrier_t all_threads_done;

/* state barriers */
pthread_barrier_t start_barrier;
pthread_barrier_t locked_barrier;
pthread_barrier_t elevate_barrier;
pthread_barrier_t finish_barrier;

volatile int deadlocked = 0;
volatile int high_has_run = 0;
volatile int low_unlocked = 0;

cpu_set_t cpu_mask;

struct thread_parameters
{
  pthread_t tid;
  int inversions;
} thread_parameters[NUM_WORKER_THREADS];

/* forward prototypes */
void *low_priority (void *arg);
void *med_priority (void *arg);
void *high_priority (void *arg);
int setup_thread_attr (pthread_attr_t * attr, int prio, cpu_set_t * mask);
int set_cpu_affinity (cpu_set_t * mask);
void error (char *, ...);
void info (char *, ...);

void
prepare (int argc, char **argv)
{
  struct sched_param thread_param;
  int max = sched_get_priority_max (SCHED_FIFO);
  int status;

  errout = stdout;

  /* boost test skeleton to max priority (so we keep running) :) */
  thread_param.sched_priority = SKEL_PRIO (max);
  status = pthread_setschedparam (pthread_self (), SCHED_FIFO, &thread_param);
  if (status)
    error ("main: boosting to max priority: 0x%x\n", status);
}

#define PREPARE prepare

int
initialize_barriers (void)
{
  int status;

  status =
    pthread_barrier_init (&all_threads_ready, NULL, NUM_WORKER_THREADS + 1);
  if (status)
    {
      error ("initialize_barriers: failed to initialize all_threads_ready\n");
      return FAILURE;
    }
  status =
    pthread_barrier_init (&all_threads_done, NULL, NUM_WORKER_THREADS + 1);
  if (status)
    {
      error ("initialize_barriers: failed to initialize all_threads_done\n");
      return FAILURE;
    }
  status = pthread_barrier_init (&start_barrier, NULL, NUM_WORKER_THREADS);
  if (status)
    {
      error ("initialize_barriers: failed to initialize start_barrier\n");
      return FAILURE;
    }
  status = pthread_barrier_init (&locked_barrier, NULL, 2);
  if (status)
    {
      error ("initializing_barriers: failed to intialize locked_barrier\n");
      return FAILURE;
    }
  status = pthread_barrier_init (&elevate_barrier, NULL, 2);
  if (status)
    {
      error ("initializing_barriers: failed to initialize elevate_barrier\n");
      return FAILURE;
    }
  status = pthread_barrier_init (&finish_barrier, NULL, NUM_WORKER_THREADS);
  if (status)
    {
      error ("initializing_barriers: failed to initialize finish_barrier\n");
      return FAILURE;
    }
  return SUCCESS;
}

void
cleanup (void)
{
  int i;
  int status;
  for (i = 0; i < NUM_WORKER_THREADS; i++)
    {
      status = pthread_kill (thread_parameters[i].tid, SIGQUIT);
      if (status)
	error ("cleanup: error sending SIGQUIT to thread %d\n",
	       thread_parameters[i].tid);
    }
}

void
handler (int signal)
{
  info ("handler: %s fired\n", sys_siglist[signal]);
  cleanup ();
  if (signal == SIGALRM)
    {
      error ("handler: DEADLOCK detected!\n");
      deadlocked = 1;
    }
}

int
MAIN (int argc, char **argv)
{
  int status;
  int prio_max;
  pthread_attr_t thread_attr;
  struct sched_param thread_param;

  errout = stdout;

  /* initialize default attributes for the mutex */
  status = pthread_mutexattr_init (&mutex_attr);
  if (status)
    {
      error ("main: initializing mutex attribute: 0x%x\n", status);
      return FAILURE;
    }

  if (use_pi_mutex)
    {
      /* set priority inheritance attribute for mutex */
      status = pthread_mutexattr_setprotocol (&mutex_attr,
					      PTHREAD_PRIO_INHERIT);
      if (status)
	{
	  error ("main: setting mutex attribute policy: 0x%x\n", status);
	  return FAILURE;
	}
    }
  info ("main: Priority Inheritance turned %s\n",
	use_pi_mutex ? "on" : "off");

  /* initialize our mutex */
  status = pthread_mutex_init (&mutex, &mutex_attr);
  if (status)
    {
      error ("main: initializing mutex: 0x%x\n", status);
      return FAILURE;
    }

  /* set up our barriers */
  status = initialize_barriers ();
  if (status)
    return FAILURE;

  /* set up CPU affinity so we only use one processor */
  if (set_cpu_affinity (&cpu_mask))
    return FAILURE;

  /* boost us to max priority (so we keep running) :) */
  prio_max = sched_get_priority_max (SCHED_FIFO);
  thread_param.sched_priority = MAIN_PRIO (prio_max);
  status = pthread_setschedparam (pthread_self (), SCHED_FIFO, &thread_param);
  if (status)
    {
      error ("main: boosting to max priority: 0x%x\n", status);
      /* Don't fail if we don't have the right privledges */
      return SUCCESS;
    }

  /* start the low priority thread */
  info ("main: creating low priority thread\n");
  setup_thread_attr (&thread_attr, LOW_PRIO (prio_max), &cpu_mask);
  thread_parameters[LOW].inversions = inversions;
  status = pthread_create (&thread_parameters[LOW].tid,
			   &thread_attr,
			   low_priority, &thread_parameters[LOW]);
  if (status != 0)
    {
      error ("main: creating low_priority thread: 0x%x\n", status);
      return FAILURE;
    }

  /* create the medium priority thread */
  info ("main: creating medium priority thread\n");
  setup_thread_attr (&thread_attr, MED_PRIO (prio_max), &cpu_mask);
  thread_parameters[MEDIUM].inversions = inversions;
  status = pthread_create (&thread_parameters[MEDIUM].tid,
			   &thread_attr,
			   med_priority, &thread_parameters[MEDIUM]);
  if (status != 0)
    {
      error ("main: creating med_priority thread: 0x%x\n", status);
      return FAILURE;
    }

  /* create the high priority thread */
  info ("main: creating high priority thread\n");
  if (setup_thread_attr (&thread_attr, HIGH_PRIO (prio_max), &cpu_mask))
    return FAILURE;
  thread_parameters[HIGH].inversions = inversions;
  status = pthread_create (&thread_parameters[HIGH].tid,
			   &thread_attr,
			   high_priority, &thread_parameters[HIGH]);
  if (status != 0)
    {
      error ("main: creating high_priority thread: 0x%x\n", status);
      cleanup ();
      return FAILURE;
    }

  signal (SIGINT, handler);

  info ("main: releasing all threads\n");
  status = pthread_barrier_wait (&all_threads_ready);
  if (status && status != PTHREAD_BARRIER_SERIAL_THREAD)
    {
      error ("main: pthread_barrier_wait(all_threads_ready): 0x%x\n", status);
      cleanup ();
      return FAILURE;
    }
  info ("main: all threads initialized\n");

  info ("main: waiting for threads to finish\n");

  status = pthread_barrier_wait (&all_threads_done);
  if (status && status != PTHREAD_BARRIER_SERIAL_THREAD)
    {
      error ("main: pthread_barrier_wait(all_threads_done): 0x%x\n", status);
      cleanup ();
      return FAILURE;
    }
  info ("main: all threads terminated!\n");
  if (deadlocked)
    {
      info ("main: test failed\n");
      return FAILURE;
    }
  info ("main: test passed\n");
  return SUCCESS;
}


int
setup_thread_attr (pthread_attr_t * attr, int prio, cpu_set_t * mask)
{
  int status;
  struct sched_param thread_param;

  status = pthread_attr_init (attr);
  if (status)
    {
      error ("setup_thread_attr: initializing thread attribute: 0x%x\n",
	     status);
      return FAILURE;
    }
  status = pthread_attr_setschedpolicy (attr, SCHED_FIFO);
  if (status)
    {
      error
	("setup_thread_attr: setting attribute policy to SCHED_FIFO: 0x%x\n",
	 status);
      return FAILURE;
    }
  status = pthread_attr_setinheritsched (attr, PTHREAD_EXPLICIT_SCHED);
  if (status)
    {
      error
	("setup_thread_attr: setting explicit scheduling inheritance: 0x%x\n",
	 status);
      return FAILURE;
    }
  thread_param.sched_priority = prio;
  status = pthread_attr_setschedparam (attr, &thread_param);
  if (status)
    {
      error ("setup_thread_attr: setting scheduler param: 0x%x\n", status);
      return FAILURE;
    }
  status = pthread_attr_setaffinity_np (attr, sizeof (cpu_set_t), mask);
  if (status)
    {
      error ("setup_thread_attr: setting affinity attribute: 0x%x\n", status);
      return FAILURE;
    }
  return SUCCESS;
}

int
set_cpu_affinity (cpu_set_t * cpu_set)
{
  int status, i;
  cpu_set_t current_mask, new_mask;

  /* Now set our CPU affinity to only run one one processor */
  status = sched_getaffinity (0, sizeof (cpu_set_t), &current_mask);
  if (status)
    {
      error ("set_cpu_affinity: getting CPU affinity mask: 0x%x\n", status);
      return FAILURE;
    }
  for (i = 0; i < sizeof (cpu_set_t) * 8; i++)
    {
      if (CPU_ISSET (i, &current_mask))
	break;
    }
  if (i >= sizeof (cpu_set_t) * 8)
    {
      error ("set_cpu_affinity: No schedulable CPU found!\n");
      return FAILURE;
    }
  CPU_ZERO (&new_mask);
  CPU_SET (i, &new_mask);
  status = sched_setaffinity (0, sizeof (cpu_set_t), &new_mask);
  if (status)
    {
      error ("set_cpu_affinity: setting CPU affinity mask: 0x%x\n", status);
      return FAILURE;
    }
  info ("set_cpu_affinity: using processr %d\n", i);
  *cpu_set = new_mask;
  return SUCCESS;
}

void
report_threadinfo (char *name)
{
  int status;
  struct sched_param thread_param;
  int thread_policy;

  status =
    pthread_getschedparam (pthread_self (), &thread_policy, &thread_param);
  if (status)
    {
      error ("report_threadinfo: failed to get scheduler param: 0x%x\n",
	     status);
      pthread_mutex_unlock (&mutex);
      exit (FAILURE);
    }
  info ("%s: running as %s thread at priority %d\n",
	name, thread_policy == SCHED_FIFO ? "FIFO" :
	thread_policy == SCHED_RR ? "RR" : "OTHER",
	thread_param.sched_priority);
}

void *
low_priority (void *arg)
{
  int status;
  struct thread_parameters *p = (struct thread_parameters *) arg;

  report_threadinfo ("low_priority");

  info ("low_priority: entering ready state\n");

  /* wait for all threads to be ready */
  status = pthread_barrier_wait (&all_threads_ready);
  if (status && status != PTHREAD_BARRIER_SERIAL_THREAD)
    {
      error ("low_priority: pthread_barrier_wait(all_threads_ready): %x",
	     status);
      return NULL;
    }

  info ("low_priority: starting inversion loop (%d)\n", p->inversions);
  while (p->inversions-- > 0)
    {
      /* initial state */
      info ("low_priority: entering start wait (%d)\n", p->inversions + 1);
      status = pthread_barrier_wait (&start_barrier);
      if (status && status != PTHREAD_BARRIER_SERIAL_THREAD)
	{
	  error ("low_priority: pthread_barrier_wait(start): %x\n", status);
	  return NULL;
	}
      info ("low_priority: claiming mutex\n");
      pthread_mutex_lock (&mutex);
      info ("low_priority: mutex locked\n");

      info ("low_priority: entering locked wait\n");
      status = pthread_barrier_wait (&locked_barrier);
      if (status && status != PTHREAD_BARRIER_SERIAL_THREAD)
	{
	  error ("low_priority: pthread_barrier_wait(locked): %x\n", status);
	  return NULL;
	}

      /* wait for priority boost */
      info ("low_priority: entering elevated wait\n");
      low_unlocked = 0;		/* prevent race with med_priority */
      status = pthread_barrier_wait (&elevate_barrier);
      if (status && status != PTHREAD_BARRIER_SERIAL_THREAD)
	{
	  error ("low_priority: pthread_barrier_wait(elevate): %x\n", status);
	  return NULL;
	}
      low_unlocked = 1;

      /* release the mutex */
      info ("low_priority: unlocking mutex\n");
      pthread_mutex_unlock (&mutex);

      /* finish state */
      info ("low_priority: entering finish wait\n");
      status = pthread_barrier_wait (&finish_barrier);
      if (status && status != PTHREAD_BARRIER_SERIAL_THREAD)
	{
	  error ("low_priority: pthread_barrier_wait(elevate): %x\n", status);
	  return NULL;
	}

    }
  /* let main know we're done */
  info ("low_priority: entering exit state\n");
  status = pthread_barrier_wait (&all_threads_done);
  if (status && status != PTHREAD_BARRIER_SERIAL_THREAD)
    {
      error ("low_priority: pthread_barrier_wait(all_threads_done): %x",
	     status);
      return NULL;
    }
  info ("low_priority: exiting\n");
  return NULL;
}

void *
med_priority (void *arg)
{
  int status;
  struct thread_parameters *p = (struct thread_parameters *) arg;

  report_threadinfo ("med_priority");

  info ("med_priority: entering ready state\n");
  /* wait for all threads to be ready */
  status = pthread_barrier_wait (&all_threads_ready);
  if (status && status != PTHREAD_BARRIER_SERIAL_THREAD)
    {
      error ("med_priority: pthread_barrier_wait(all_threads_ready): %x",
	     status);
      return NULL;
    }

  info ("med_priority: starting inversion loop (%d)\n", p->inversions);
  while (p->inversions-- > 0)
    {
      /* start state */
      info ("med_priority: entering start state (%d)\n", p->inversions + 1);
      status = pthread_barrier_wait (&start_barrier);
      if (status && status != PTHREAD_BARRIER_SERIAL_THREAD)
	{
	  error ("med_priority: pthread_barrier_wait(start): %x", status);
	  return NULL;
	}
      info ("med_priority: entering elevate state\n");
      do
	{
	  status = pthread_barrier_wait (&elevate_barrier);
	  if (status && status != PTHREAD_BARRIER_SERIAL_THREAD)
	    {
	      error ("med_priority: pthread_barrier_wait(elevate): %x",
		     status);
	      return NULL;
	    }
	}
      while (!high_has_run && !low_unlocked);
      info ("med_priority: entering finish state\n");
      status = pthread_barrier_wait (&finish_barrier);
      if (status && status != PTHREAD_BARRIER_SERIAL_THREAD)
	{
	  error ("med_priority: pthread_barrier_wait(finished): %x", status);
	  return NULL;
	}
    }

  info ("med_priority: entering exit state\n");
  status = pthread_barrier_wait (&all_threads_done);
  if (status && status != PTHREAD_BARRIER_SERIAL_THREAD)
    {
      error ("med_priority: pthread_barrier_wait(all_threads_done): %x",
	     status);
      return NULL;
    }
  info ("med_priority: exiting\n");
  return NULL;
}

void *
high_priority (void *arg)
{
  int status;
  struct thread_parameters *p = (struct thread_parameters *) arg;

  report_threadinfo ("high_priority");

  info ("high_priority: entering ready state\n");

  /* wait for all threads to be ready */
  status = pthread_barrier_wait (&all_threads_ready);
  if (status && status != PTHREAD_BARRIER_SERIAL_THREAD)
    {
      error ("high_priority: pthread_barrier_wait(all_threads_ready): %x",
	     status);
      return NULL;
    }

  info ("high_priority: starting inversion loop (%d)\n", p->inversions);
  while (p->inversions-- > 0)
    {
      high_has_run = 0;
      info ("high_priority: entering start state (%d)\n", p->inversions + 1);
      status = pthread_barrier_wait (&start_barrier);
      if (status && status != PTHREAD_BARRIER_SERIAL_THREAD)
	{
	  error ("high_priority: pthread_barrier_wait(start): %x", status);
	  return NULL;
	}
      info ("high_priority: entering running state\n");
      status = pthread_barrier_wait (&locked_barrier);
      if (status && status != PTHREAD_BARRIER_SERIAL_THREAD)
	{
	  error ("high_priority: pthread_barrier_wait(running): %x", status);
	  return NULL;
	}
      info ("high_priority: locking mutex\n");
      pthread_mutex_lock (&mutex);
      info ("high_priority: got mutex\n");
      high_has_run = 1;
      info ("high_priority: unlocking mutex\n");
      pthread_mutex_unlock (&mutex);
      info ("high_priority: entering finish state\n");
      status = pthread_barrier_wait (&finish_barrier);
      if (status && status != PTHREAD_BARRIER_SERIAL_THREAD)
	{
	  error ("high_priority: pthread_barrier_wait(finish): %x", status);
	  return NULL;
	}
    }

  info ("high_priority: entering exit state\n");
  status = pthread_barrier_wait (&all_threads_done);
  if (status && status != PTHREAD_BARRIER_SERIAL_THREAD)
    {
      error ("high_priority: pthread_barrier_wait(all_threads_done): %x",
	     status);
      return NULL;
    }
  info ("high_priority: exiting\n");
  return NULL;
}

void
error (char *fmt, ...)
{
  va_list ap;
  fputs ("ERROR: ", errout);
  va_start (ap, fmt);
  vfprintf (errout, fmt, ap);
  va_end (ap);
}

void
info (char *fmt, ...)
{
  if (verbose)
    {
      va_list ap;
      va_start (ap, fmt);
      vprintf (fmt, ap);
      va_end (ap);
    }
}
#include "../test-skeleton.c"