Debian Codegraph @

src/pi_tests/tst-mutexpi10.c

raw symbols SCIP
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /*
3 * Classic Priority Inversion deadlock test case
4 * Copyright (C) 2006 Free Software Foundation, Inc.
5 * This file is part of the GNU C Library.
6 * Contributed by Clark Williams<[email protected]>, 2006
7 */
8
9 /* This program tests Priority Inheritance mutexes and their ability
10 to avoid Priority Inversion deadlocks
11
12 The basic premise here is to set up a deadlock scenario and confirm that PI
13 mutexes resolve the situation. Three worker threads will be created from the
14 main thread: low, medium and high priority threads that use SCHED_FIFO as
15 their scheduling policy. The low priority thread claims a mutex and then
16 starts "working". The medium priority thread starts and preempts the low
17 priority thread. Then the high priority thread runs and attempts to claim
18 the mutex owned by the low priority thread. Without priority inheritance,
19 this will deadlock the program. With priority inheritance, the low priority
20 thread receives a priority boost, finishes it's "work" and releases the mutex,
21 which allows the high priority thread to run and finish and then the medium
22 priority thread finishes.
23
24 That's the theory, anyway...
25
26 CW - 2006 */
27
28 #include <stdio.h>
29 #include <errno.h>
30 #include <stdlib.h>
31 #include <stdarg.h>
32 #include <pthread.h>
33 #include <sched.h>
34 #include <unistd.h>
35 #include <string.h>
36 #include <signal.h>
37
38 /* test timeout */
39 #define TIMEOUT 2
40
41 /* determine if the C library supports Priority Inheritance mutexes */
42 #if defined(_POSIX_THREAD_PRIO_INHERIT) && _POSIX_THREAD_PRIO_INHERIT != -1
43 #define HAVE_PI_MUTEX 1
44 #else
45 #define HAVE_PI_MUTEX 0
46 #endif
47
48 int use_pi_mutex = HAVE_PI_MUTEX;
49
50 #define SUCCESS 0
51 #define FAILURE 1
52
53 /* the number of times we cause a priority inversion situation */
54 int inversions = 1;
55
56 /* the file handle used by the error reporting routine */
57 FILE *errout;
58
59 #define MAIN do_test
60 #define TEST_FUNCTION do_test(argc, argv)
61 #define CMDLINE_OPTIONS { "verbose", no_argument, NULL, 1002 }, \
62 { "no-pi", no_argument, NULL, 1003},
63 #define CMDLINE_PROCESS \
64 case 1002: verbose=1; break; \
65 case 1003: use_pi_mutex = 0; break;
66 #define CLEANUP_HANDLER cleanup()
67 int verbose = 0;
68
69 /* define priorities for the threads */
70 #define SKEL_PRIO(x) (x)
71 #define MAIN_PRIO(x) (x - 1)
72 #define HIGH_PRIO(x) (x - 2)
73 #define MED_PRIO(x) (x - 3)
74 #define LOW_PRIO(x) (x - 4)
75
76 enum thread_names
77 { LOW = 0, MEDIUM, HIGH, NUM_WORKER_THREADS };
78
79 pthread_mutex_t mutex;
80 pthread_mutexattr_t mutex_attr;
81
82 pthread_barrier_t all_threads_ready;
83 pthread_barrier_t all_threads_done;
84
85 /* state barriers */
86 pthread_barrier_t start_barrier;
87 pthread_barrier_t locked_barrier;
88 pthread_barrier_t elevate_barrier;
89 pthread_barrier_t finish_barrier;
90
91 volatile int deadlocked = 0;
92 volatile int high_has_run = 0;
93 volatile int low_unlocked = 0;
94
95 cpu_set_t cpu_mask;
96
97 struct thread_parameters
98 {
99 pthread_t tid;
100 int inversions;
101 } thread_parameters[NUM_WORKER_THREADS];
102
103 /* forward prototypes */
104 void *low_priority (void *arg);
105 void *med_priority (void *arg);
106 void *high_priority (void *arg);
107 int setup_thread_attr (pthread_attr_t * attr, int prio, cpu_set_t * mask);
108 int set_cpu_affinity (cpu_set_t * mask);
109 void error (char *, ...);
110 void info (char *, ...);
111
112 void
113 prepare (int argc, char **argv)
114 {
115 struct sched_param thread_param;
116 int max = sched_get_priority_max (SCHED_FIFO);
117 int status;
118
119 errout = stdout;
120
121 /* boost test skeleton to max priority (so we keep running) :) */
122 thread_param.sched_priority = SKEL_PRIO (max);
123 status = pthread_setschedparam (pthread_self (), SCHED_FIFO, &thread_param);
124 if (status)
125 error ("main: boosting to max priority: 0x%x\n", status);
126 }
127
128 #define PREPARE prepare
129
130 int
131 initialize_barriers (void)
132 {
133 int status;
134
135 status =
136 pthread_barrier_init (&all_threads_ready, NULL, NUM_WORKER_THREADS + 1);
137 if (status)
138 {
139 error ("initialize_barriers: failed to initialize all_threads_ready\n");
140 return FAILURE;
141 }
142 status =
143 pthread_barrier_init (&all_threads_done, NULL, NUM_WORKER_THREADS + 1);
144 if (status)
145 {
146 error ("initialize_barriers: failed to initialize all_threads_done\n");
147 return FAILURE;
148 }
149 status = pthread_barrier_init (&start_barrier, NULL, NUM_WORKER_THREADS);
150 if (status)
151 {
152 error ("initialize_barriers: failed to initialize start_barrier\n");
153 return FAILURE;
154 }
155 status = pthread_barrier_init (&locked_barrier, NULL, 2);
156 if (status)
157 {
158 error ("initializing_barriers: failed to intialize locked_barrier\n");
159 return FAILURE;
160 }
161 status = pthread_barrier_init (&elevate_barrier, NULL, 2);
162 if (status)
163 {
164 error ("initializing_barriers: failed to initialize elevate_barrier\n");
165 return FAILURE;
166 }
167 status = pthread_barrier_init (&finish_barrier, NULL, NUM_WORKER_THREADS);
168 if (status)
169 {
170 error ("initializing_barriers: failed to initialize finish_barrier\n");
171 return FAILURE;
172 }
173 return SUCCESS;
174 }
175
176 void
177 cleanup (void)
178 {
179 int i;
180 int status;
181 for (i = 0; i < NUM_WORKER_THREADS; i++)
182 {
183 status = pthread_kill (thread_parameters[i].tid, SIGQUIT);
184 if (status)
185 error ("cleanup: error sending SIGQUIT to thread %d\n",
186 thread_parameters[i].tid);
187 }
188 }
189
190 void
191 handler (int signal)
192 {
193 info ("handler: %s fired\n", sys_siglist[signal]);
194 cleanup ();
195 if (signal == SIGALRM)
196 {
197 error ("handler: DEADLOCK detected!\n");
198 deadlocked = 1;
199 }
200 }
201
202 int
203 MAIN (int argc, char **argv)
204 {
205 int status;
206 int prio_max;
207 pthread_attr_t thread_attr;
208 struct sched_param thread_param;
209
210 errout = stdout;
211
212 /* initialize default attributes for the mutex */
213 status = pthread_mutexattr_init (&mutex_attr);
214 if (status)
215 {
216 error ("main: initializing mutex attribute: 0x%x\n", status);
217 return FAILURE;
218 }
219
220 if (use_pi_mutex)
221 {
222 /* set priority inheritance attribute for mutex */
223 status = pthread_mutexattr_setprotocol (&mutex_attr,
224 PTHREAD_PRIO_INHERIT);
225 if (status)
226 {
227 error ("main: setting mutex attribute policy: 0x%x\n", status);
228 return FAILURE;
229 }
230 }
231 info ("main: Priority Inheritance turned %s\n",
232 use_pi_mutex ? "on" : "off");
233
234 /* initialize our mutex */
235 status = pthread_mutex_init (&mutex, &mutex_attr);
236 if (status)
237 {
238 error ("main: initializing mutex: 0x%x\n", status);
239 return FAILURE;
240 }
241
242 /* set up our barriers */
243 status = initialize_barriers ();
244 if (status)
245 return FAILURE;
246
247 /* set up CPU affinity so we only use one processor */
248 if (set_cpu_affinity (&cpu_mask))
249 return FAILURE;
250
251 /* boost us to max priority (so we keep running) :) */
252 prio_max = sched_get_priority_max (SCHED_FIFO);
253 thread_param.sched_priority = MAIN_PRIO (prio_max);
254 status = pthread_setschedparam (pthread_self (), SCHED_FIFO, &thread_param);
255 if (status)
256 {
257 error ("main: boosting to max priority: 0x%x\n", status);
258 /* Don't fail if we don't have the right privledges */
259 return SUCCESS;
260 }
261
262 /* start the low priority thread */
263 info ("main: creating low priority thread\n");
264 setup_thread_attr (&thread_attr, LOW_PRIO (prio_max), &cpu_mask);
265 thread_parameters[LOW].inversions = inversions;
266 status = pthread_create (&thread_parameters[LOW].tid,
267 &thread_attr,
268 low_priority, &thread_parameters[LOW]);
269 if (status != 0)
270 {
271 error ("main: creating low_priority thread: 0x%x\n", status);
272 return FAILURE;
273 }
274
275 /* create the medium priority thread */
276 info ("main: creating medium priority thread\n");
277 setup_thread_attr (&thread_attr, MED_PRIO (prio_max), &cpu_mask);
278 thread_parameters[MEDIUM].inversions = inversions;
279 status = pthread_create (&thread_parameters[MEDIUM].tid,
280 &thread_attr,
281 med_priority, &thread_parameters[MEDIUM]);
282 if (status != 0)
283 {
284 error ("main: creating med_priority thread: 0x%x\n", status);
285 return FAILURE;
286 }
287
288 /* create the high priority thread */
289 info ("main: creating high priority thread\n");
290 if (setup_thread_attr (&thread_attr, HIGH_PRIO (prio_max), &cpu_mask))
291 return FAILURE;
292 thread_parameters[HIGH].inversions = inversions;
293 status = pthread_create (&thread_parameters[HIGH].tid,
294 &thread_attr,
295 high_priority, &thread_parameters[HIGH]);
296 if (status != 0)
297 {
298 error ("main: creating high_priority thread: 0x%x\n", status);
299 cleanup ();
300 return FAILURE;
301 }
302
303 signal (SIGINT, handler);
304
305 info ("main: releasing all threads\n");
306 status = pthread_barrier_wait (&all_threads_ready);
307 if (status && status != PTHREAD_BARRIER_SERIAL_THREAD)
308 {
309 error ("main: pthread_barrier_wait(all_threads_ready): 0x%x\n", status);
310 cleanup ();
311 return FAILURE;
312 }
313 info ("main: all threads initialized\n");
314
315 info ("main: waiting for threads to finish\n");
316
317 status = pthread_barrier_wait (&all_threads_done);
318 if (status && status != PTHREAD_BARRIER_SERIAL_THREAD)
319 {
320 error ("main: pthread_barrier_wait(all_threads_done): 0x%x\n", status);
321 cleanup ();
322 return FAILURE;
323 }
324 info ("main: all threads terminated!\n");
325 if (deadlocked)
326 {
327 info ("main: test failed\n");
328 return FAILURE;
329 }
330 info ("main: test passed\n");
331 return SUCCESS;
332 }
333
334
335 int
336 setup_thread_attr (pthread_attr_t * attr, int prio, cpu_set_t * mask)
337 {
338 int status;
339 struct sched_param thread_param;
340
341 status = pthread_attr_init (attr);
342 if (status)
343 {
344 error ("setup_thread_attr: initializing thread attribute: 0x%x\n",
345 status);
346 return FAILURE;
347 }
348 status = pthread_attr_setschedpolicy (attr, SCHED_FIFO);
349 if (status)
350 {
351 error
352 ("setup_thread_attr: setting attribute policy to SCHED_FIFO: 0x%x\n",
353 status);
354 return FAILURE;
355 }
356 status = pthread_attr_setinheritsched (attr, PTHREAD_EXPLICIT_SCHED);
357 if (status)
358 {
359 error
360 ("setup_thread_attr: setting explicit scheduling inheritance: 0x%x\n",
361 status);
362 return FAILURE;
363 }
364 thread_param.sched_priority = prio;
365 status = pthread_attr_setschedparam (attr, &thread_param);
366 if (status)
367 {
368 error ("setup_thread_attr: setting scheduler param: 0x%x\n", status);
369 return FAILURE;
370 }
371 status = pthread_attr_setaffinity_np (attr, sizeof (cpu_set_t), mask);
372 if (status)
373 {
374 error ("setup_thread_attr: setting affinity attribute: 0x%x\n", status);
375 return FAILURE;
376 }
377 return SUCCESS;
378 }
379
380 int
381 set_cpu_affinity (cpu_set_t * cpu_set)
382 {
383 int status, i;
384 cpu_set_t current_mask, new_mask;
385
386 /* Now set our CPU affinity to only run one one processor */
387 status = sched_getaffinity (0, sizeof (cpu_set_t), &current_mask);
388 if (status)
389 {
390 error ("set_cpu_affinity: getting CPU affinity mask: 0x%x\n", status);
391 return FAILURE;
392 }
393 for (i = 0; i < sizeof (cpu_set_t) * 8; i++)
394 {
395 if (CPU_ISSET (i, &current_mask))
396 break;
397 }
398 if (i >= sizeof (cpu_set_t) * 8)
399 {
400 error ("set_cpu_affinity: No schedulable CPU found!\n");
401 return FAILURE;
402 }
403 CPU_ZERO (&new_mask);
404 CPU_SET (i, &new_mask);
405 status = sched_setaffinity (0, sizeof (cpu_set_t), &new_mask);
406 if (status)
407 {
408 error ("set_cpu_affinity: setting CPU affinity mask: 0x%x\n", status);
409 return FAILURE;
410 }
411 info ("set_cpu_affinity: using processr %d\n", i);
412 *cpu_set = new_mask;
413 return SUCCESS;
414 }
415
416 void
417 report_threadinfo (char *name)
418 {
419 int status;
420 struct sched_param thread_param;
421 int thread_policy;
422
423 status =
424 pthread_getschedparam (pthread_self (), &thread_policy, &thread_param);
425 if (status)
426 {
427 error ("report_threadinfo: failed to get scheduler param: 0x%x\n",
428 status);
429 pthread_mutex_unlock (&mutex);
430 exit (FAILURE);
431 }
432 info ("%s: running as %s thread at priority %d\n",
433 name, thread_policy == SCHED_FIFO ? "FIFO" :
434 thread_policy == SCHED_RR ? "RR" : "OTHER",
435 thread_param.sched_priority);
436 }
437
438 void *
439 low_priority (void *arg)
440 {
441 int status;
442 struct thread_parameters *p = (struct thread_parameters *) arg;
443
444 report_threadinfo ("low_priority");
445
446 info ("low_priority: entering ready state\n");
447
448 /* wait for all threads to be ready */
449 status = pthread_barrier_wait (&all_threads_ready);
450 if (status && status != PTHREAD_BARRIER_SERIAL_THREAD)
451 {
452 error ("low_priority: pthread_barrier_wait(all_threads_ready): %x",
453 status);
454 return NULL;
455 }
456
457 info ("low_priority: starting inversion loop (%d)\n", p->inversions);
458 while (p->inversions-- > 0)
459 {
460 /* initial state */
461 info ("low_priority: entering start wait (%d)\n", p->inversions + 1);
462 status = pthread_barrier_wait (&start_barrier);
463 if (status && status != PTHREAD_BARRIER_SERIAL_THREAD)
464 {
465 error ("low_priority: pthread_barrier_wait(start): %x\n", status);
466 return NULL;
467 }
468 info ("low_priority: claiming mutex\n");
469 pthread_mutex_lock (&mutex);
470 info ("low_priority: mutex locked\n");
471
472 info ("low_priority: entering locked wait\n");
473 status = pthread_barrier_wait (&locked_barrier);
474 if (status && status != PTHREAD_BARRIER_SERIAL_THREAD)
475 {
476 error ("low_priority: pthread_barrier_wait(locked): %x\n", status);
477 return NULL;
478 }
479
480 /* wait for priority boost */
481 info ("low_priority: entering elevated wait\n");
482 low_unlocked = 0; /* prevent race with med_priority */
483 status = pthread_barrier_wait (&elevate_barrier);
484 if (status && status != PTHREAD_BARRIER_SERIAL_THREAD)
485 {
486 error ("low_priority: pthread_barrier_wait(elevate): %x\n", status);
487 return NULL;
488 }
489 low_unlocked = 1;
490
491 /* release the mutex */
492 info ("low_priority: unlocking mutex\n");
493 pthread_mutex_unlock (&mutex);
494
495 /* finish state */
496 info ("low_priority: entering finish wait\n");
497 status = pthread_barrier_wait (&finish_barrier);
498 if (status && status != PTHREAD_BARRIER_SERIAL_THREAD)
499 {
500 error ("low_priority: pthread_barrier_wait(elevate): %x\n", status);
501 return NULL;
502 }
503
504 }
505 /* let main know we're done */
506 info ("low_priority: entering exit state\n");
507 status = pthread_barrier_wait (&all_threads_done);
508 if (status && status != PTHREAD_BARRIER_SERIAL_THREAD)
509 {
510 error ("low_priority: pthread_barrier_wait(all_threads_done): %x",
511 status);
512 return NULL;
513 }
514 info ("low_priority: exiting\n");
515 return NULL;
516 }
517
518 void *
519 med_priority (void *arg)
520 {
521 int status;
522 struct thread_parameters *p = (struct thread_parameters *) arg;
523
524 report_threadinfo ("med_priority");
525
526 info ("med_priority: entering ready state\n");
527 /* wait for all threads to be ready */
528 status = pthread_barrier_wait (&all_threads_ready);
529 if (status && status != PTHREAD_BARRIER_SERIAL_THREAD)
530 {
531 error ("med_priority: pthread_barrier_wait(all_threads_ready): %x",
532 status);
533 return NULL;
534 }
535
536 info ("med_priority: starting inversion loop (%d)\n", p->inversions);
537 while (p->inversions-- > 0)
538 {
539 /* start state */
540 info ("med_priority: entering start state (%d)\n", p->inversions + 1);
541 status = pthread_barrier_wait (&start_barrier);
542 if (status && status != PTHREAD_BARRIER_SERIAL_THREAD)
543 {
544 error ("med_priority: pthread_barrier_wait(start): %x", status);
545 return NULL;
546 }
547 info ("med_priority: entering elevate state\n");
548 do
549 {
550 status = pthread_barrier_wait (&elevate_barrier);
551 if (status && status != PTHREAD_BARRIER_SERIAL_THREAD)
552 {
553 error ("med_priority: pthread_barrier_wait(elevate): %x",
554 status);
555 return NULL;
556 }
557 }
558 while (!high_has_run && !low_unlocked);
559 info ("med_priority: entering finish state\n");
560 status = pthread_barrier_wait (&finish_barrier);
561 if (status && status != PTHREAD_BARRIER_SERIAL_THREAD)
562 {
563 error ("med_priority: pthread_barrier_wait(finished): %x", status);
564 return NULL;
565 }
566 }
567
568 info ("med_priority: entering exit state\n");
569 status = pthread_barrier_wait (&all_threads_done);
570 if (status && status != PTHREAD_BARRIER_SERIAL_THREAD)
571 {
572 error ("med_priority: pthread_barrier_wait(all_threads_done): %x",
573 status);
574 return NULL;
575 }
576 info ("med_priority: exiting\n");
577 return NULL;
578 }
579
580 void *
581 high_priority (void *arg)
582 {
583 int status;
584 struct thread_parameters *p = (struct thread_parameters *) arg;
585
586 report_threadinfo ("high_priority");
587
588 info ("high_priority: entering ready state\n");
589
590 /* wait for all threads to be ready */
591 status = pthread_barrier_wait (&all_threads_ready);
592 if (status && status != PTHREAD_BARRIER_SERIAL_THREAD)
593 {
594 error ("high_priority: pthread_barrier_wait(all_threads_ready): %x",
595 status);
596 return NULL;
597 }
598
599 info ("high_priority: starting inversion loop (%d)\n", p->inversions);
600 while (p->inversions-- > 0)
601 {
602 high_has_run = 0;
603 info ("high_priority: entering start state (%d)\n", p->inversions + 1);
604 status = pthread_barrier_wait (&start_barrier);
605 if (status && status != PTHREAD_BARRIER_SERIAL_THREAD)
606 {
607 error ("high_priority: pthread_barrier_wait(start): %x", status);
608 return NULL;
609 }
610 info ("high_priority: entering running state\n");
611 status = pthread_barrier_wait (&locked_barrier);
612 if (status && status != PTHREAD_BARRIER_SERIAL_THREAD)
613 {
614 error ("high_priority: pthread_barrier_wait(running): %x", status);
615 return NULL;
616 }
617 info ("high_priority: locking mutex\n");
618 pthread_mutex_lock (&mutex);
619 info ("high_priority: got mutex\n");
620 high_has_run = 1;
621 info ("high_priority: unlocking mutex\n");
622 pthread_mutex_unlock (&mutex);
623 info ("high_priority: entering finish state\n");
624 status = pthread_barrier_wait (&finish_barrier);
625 if (status && status != PTHREAD_BARRIER_SERIAL_THREAD)
626 {
627 error ("high_priority: pthread_barrier_wait(finish): %x", status);
628 return NULL;
629 }
630 }
631
632 info ("high_priority: entering exit state\n");
633 status = pthread_barrier_wait (&all_threads_done);
634 if (status && status != PTHREAD_BARRIER_SERIAL_THREAD)
635 {
636 error ("high_priority: pthread_barrier_wait(all_threads_done): %x",
637 status);
638 return NULL;
639 }
640 info ("high_priority: exiting\n");
641 return NULL;
642 }
643
644 void
645 error (char *fmt, ...)
646 {
647 va_list ap;
648 fputs ("ERROR: ", errout);
649 va_start (ap, fmt);
650 vfprintf (errout, fmt, ap);
651 va_end (ap);
652 }
653
654 void
655 info (char *fmt, ...)
656 {
657 if (verbose)
658 {
659 va_list ap;
660 va_start (ap, fmt);
661 vprintf (fmt, ap);
662 va_end (ap);
663 }
664 }
665 #include "../test-skeleton.c"