lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <errno.h> |
| 4 | #include <pthread.h> |
| 5 | #include <unistd.h> |
| 6 | #include <sys/time.h> |
| 7 | |
| 8 | |
| 9 | static pthread_mutex_t m; |
| 10 | |
| 11 | static void * |
| 12 | tf (void *data) |
| 13 | { |
| 14 | int err = pthread_mutex_lock (&m); |
| 15 | if (err == EOWNERDEAD) |
| 16 | { |
| 17 | err = pthread_mutex_consistent_np (&m); |
| 18 | if (err) |
| 19 | { |
| 20 | puts ("pthread_mutex_consistent_np"); |
| 21 | exit (1); |
| 22 | } |
| 23 | } |
| 24 | else if (err) |
| 25 | { |
| 26 | puts ("pthread_mutex_lock"); |
| 27 | exit (1); |
| 28 | } |
| 29 | printf ("thread%ld got the lock.\n", (long int) data); |
| 30 | sleep (1); |
| 31 | /* exit without unlock */ |
| 32 | return NULL; |
| 33 | } |
| 34 | |
| 35 | static int |
| 36 | do_test (void) |
| 37 | { |
| 38 | int err, i; |
| 39 | pthread_t t[3]; |
| 40 | pthread_mutexattr_t ma; |
| 41 | |
| 42 | pthread_mutexattr_init (&ma); |
| 43 | err = pthread_mutexattr_setrobust_np (&ma, PTHREAD_MUTEX_ROBUST_NP); |
| 44 | if (err) |
| 45 | { |
| 46 | puts ("pthread_mutexattr_setrobust_np"); |
| 47 | return 1; |
| 48 | } |
| 49 | #ifdef ENABLE_PI |
| 50 | if (pthread_mutexattr_setprotocol (&ma, PTHREAD_PRIO_INHERIT) != 0) |
| 51 | { |
| 52 | puts ("pthread_mutexattr_setprotocol failed"); |
| 53 | return 1; |
| 54 | } |
| 55 | #endif |
| 56 | err = pthread_mutex_init (&m, &ma); |
| 57 | #ifdef ENABLE_PI |
| 58 | if (err == ENOTSUP) |
| 59 | { |
| 60 | puts ("PI robust mutexes not supported"); |
| 61 | return 0; |
| 62 | } |
| 63 | #endif |
| 64 | if (err) |
| 65 | { |
| 66 | puts ("pthread_mutex_init"); |
| 67 | return 1; |
| 68 | } |
| 69 | |
| 70 | for (i = 0; i < sizeof (t) / sizeof (t[0]); i++) |
| 71 | { |
| 72 | err = pthread_create (&t[i], NULL, tf, (void *) (long int) i); |
| 73 | if (err) |
| 74 | { |
| 75 | puts ("pthread_create"); |
| 76 | return 1; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | for (i = 0; i < sizeof (t) / sizeof (t[0]); i++) |
| 81 | { |
| 82 | err = pthread_join (t[i], NULL); |
| 83 | if (err) |
| 84 | { |
| 85 | puts ("pthread_join"); |
| 86 | return 1; |
| 87 | } |
| 88 | } |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | #define TIMEOUT 5 |
| 93 | #define TEST_FUNCTION do_test () |
| 94 | #include "../test-skeleton.c" |