14 static void prepare (void);
15 #define PREPARE(argc, argv) prepare ()
16 static int do_test (void);
17 #define TEST_FUNCTION do_test ()
18 #include "../test-skeleton.c"
27 fd
= create_temp_file ("tst-robust8", NULL
);
33 #define THESIGNAL SIGKILL
38 static const struct timespec before
= { 0, 0 };
41 static pthread_mutex_t
*map
;
47 long int nr
= (long int) arg
;
51 memset (state
, '\0', sizeof (state
));
55 int r
= random () % N
;
63 e
= pthread_mutex_lock (&map
[r
]);
66 printf ("mutex_lock of %d in thread %ld failed with %d\n",
73 e
= pthread_mutex_timedlock (&map
[r
], &before
);
74 if (e
!= 0 && e
!= ETIMEDOUT
)
77 mutex_timedlock of %d in thread %ld failed with %d\n",
83 e
= pthread_mutex_trylock (&map
[r
]);
84 if (e
!= 0 && e
!= EBUSY
)
86 printf ("mutex_trylock of %d in thread %ld failed with %d\n",
94 pthread_mutex_consistent_np (&map
[r
]);
96 if (e
== 0 || e
== EOWNERDEAD
)
101 int e
= pthread_mutex_unlock (&map
[r
]);
104 printf ("mutex_unlock of %d in thread %ld failed with %d\n",
118 for (int thread
= 1; thread
<= THREADS
; ++thread
)
121 if (pthread_create (&th
, NULL
, tf
, (void *) (long int) thread
) != 0)
123 printf ("cannot create thread %d in round %d\n", thread
, round
);
130 ts
.tv_nsec
= 1000000000 / ROUNDS
;
131 while (nanosleep (&ts
, &ts
) != 0)
135 kill (getpid (), THESIGNAL
);
137 /* We better never get here. */
145 if (ftruncate (fd
, N
* sizeof (pthread_mutex_t
)) != 0)
147 puts ("cannot size new file");
151 map
= mmap (NULL
, N
* sizeof (pthread_mutex_t
), PROT_READ
| PROT_WRITE
,
153 if (map
== MAP_FAILED
)
155 puts ("mapping failed");
159 pthread_mutexattr_t ma
;
160 if (pthread_mutexattr_init (&ma
) != 0)
162 puts ("mutexattr_init failed");
165 if (pthread_mutexattr_setrobust_np (&ma
, PTHREAD_MUTEX_ROBUST_NP
) != 0)
167 puts ("mutexattr_setrobust failed");
170 if (pthread_mutexattr_setpshared (&ma
, PTHREAD_PROCESS_SHARED
) != 0)
172 puts ("mutexattr_setpshared failed");
176 if (pthread_mutexattr_setprotocol (&ma
, PTHREAD_PRIO_INHERIT
) != 0)
178 puts ("pthread_mutexattr_setprotocol failed");
183 for (int round
= 1; round
<= ROUNDS
; ++round
)
185 for (int n
= 0; n
< N
; ++n
)
187 int e
= pthread_mutex_init (&map
[n
], &ma
);
191 puts ("cannot support pshared robust PI mutexes");
193 puts ("cannot support pshared robust mutexes");
199 printf ("mutex_init %d in round %d failed\n", n
+ 1, round
);
207 printf ("fork in round %d failed\n", round
);
214 if (TEMP_FAILURE_RETRY (waitpid (p
, &status
, 0)) != p
)
216 printf ("waitpid in round %d failed\n", round
);
219 if (!WIFSIGNALED (status
))
221 printf ("child did not die of a signal in round %d\n", round
);
224 if (WTERMSIG (status
) != THESIGNAL
)
226 printf ("child did not die of signal %d in round %d\n",
231 for (int n
= 0; n
< N
; ++n
)
233 int e
= pthread_mutex_lock (&map
[n
]);
234 if (e
!= 0 && e
!= EOWNERDEAD
)
236 printf ("mutex_lock %d failed in round %d\n", n
+ 1, round
);
241 for (int n
= 0; n
< N
; ++n
)
242 if (pthread_mutex_unlock (&map
[n
]) != 0)
244 printf ("mutex_unlock %d failed in round %d\n", n
+ 1, round
);
248 for (int n
= 0; n
< N
; ++n
)
250 int e
= pthread_mutex_destroy (&map
[n
]);
253 printf ("mutex_destroy %d in round %d failed with %d\n",
255 printf("nusers = %d\n", (int) map
[n
].__data
.__nusers
);
261 if (pthread_mutexattr_destroy (&ma
) != 0)
263 puts ("mutexattr_destroy failed");
267 if (munmap (map
, N
* sizeof (pthread_mutex_t
)) != 0)
269 puts ("munmap failed");