14 static void prepare (void);
15 #define PREPARE(argc, argv) prepare ()
16 static int do_test (void);
17 #define TEST_FUNCTION do_test ()
19 #include "../test-skeleton.c"
28 fd = create_temp_file ("tst-robust8", NULL);
34 #define THESIGNAL SIGKILL
39 static const struct timespec before = { 0, 0 };
42 static pthread_mutex_t *map;
48 long int nr = (long int) arg;
52 memset (state, '\0', sizeof (state));
56 int r = random () % N;
64 e = pthread_mutex_lock (&map[r]);
67 printf ("mutex_lock of %d in thread %ld failed with %d\n",
74 e = pthread_mutex_timedlock (&map[r], &before);
75 if (e != 0 && e != ETIMEDOUT)
78 mutex_timedlock of %d in thread %ld failed with %d\n",
84 e = pthread_mutex_trylock (&map[r]);
85 if (e != 0 && e != EBUSY)
87 printf ("mutex_trylock of %d in thread %ld failed with %d\n",
95 pthread_mutex_consistent_np (&map[r]);
97 if (e == 0 || e == EOWNERDEAD)
102 int e = pthread_mutex_unlock (&map[r]);
105 printf ("mutex_unlock of %d in thread %ld failed with %d\n",
119 for (int thread = 1; thread <= THREADS; ++thread)
122 if (pthread_create (&th, NULL, tf, (void *) (long int) thread) != 0)
124 printf ("cannot create thread %d in round %d\n", thread, round);
131 ts.tv_nsec = 1000000000 / ROUNDS;
132 while (nanosleep (&ts, &ts) != 0)
136 kill (getpid (), THESIGNAL);
138 /* We better never get here. */
146 if (ftruncate (fd, N * sizeof (pthread_mutex_t)) != 0)
148 puts ("cannot size new file");
152 map = mmap (NULL, N * sizeof (pthread_mutex_t), PROT_READ | PROT_WRITE,
154 if (map == MAP_FAILED)
156 puts ("mapping failed");
160 pthread_mutexattr_t ma;
161 if (pthread_mutexattr_init (&ma) != 0)
163 puts ("mutexattr_init failed");
166 if (pthread_mutexattr_setrobust_np (&ma, PTHREAD_MUTEX_ROBUST_NP) != 0)
168 puts ("mutexattr_setrobust failed");
171 if (pthread_mutexattr_setpshared (&ma, PTHREAD_PROCESS_SHARED) != 0)
173 puts ("mutexattr_setpshared failed");
177 if (pthread_mutexattr_setprotocol (&ma, PTHREAD_PRIO_INHERIT) != 0)
179 puts ("pthread_mutexattr_setprotocol failed");
184 for (int round = 1; round <= ROUNDS; ++round)
186 for (int n = 0; n < N; ++n)
188 int e = pthread_mutex_init (&map[n], &ma);
192 puts ("cannot support pshared robust PI mutexes");
194 puts ("cannot support pshared robust mutexes");
200 printf ("mutex_init %d in round %d failed\n", n + 1, round);
208 printf ("fork in round %d failed\n", round);
215 if (TEMP_FAILURE_RETRY (waitpid (p, &status, 0)) != p)
217 printf ("waitpid in round %d failed\n", round);
220 if (!WIFSIGNALED (status))
222 printf ("child did not die of a signal in round %d\n", round);
225 if (WTERMSIG (status) != THESIGNAL)
227 printf ("child did not die of signal %d in round %d\n",
232 for (int n = 0; n < N; ++n)
234 int e = pthread_mutex_lock (&map[n]);
235 if (e != 0 && e != EOWNERDEAD)
237 printf ("mutex_lock %d failed in round %d\n", n + 1, round);
242 for (int n = 0; n < N; ++n)
243 if (pthread_mutex_unlock (&map[n]) != 0)
245 printf ("mutex_unlock %d failed in round %d\n", n + 1, round);
249 for (int n = 0; n < N; ++n)
251 int e = pthread_mutex_destroy (&map[n]);
254 printf ("mutex_destroy %d in round %d failed with %d\n",
256 printf("nusers = %d\n", (int) map[n].__data.__nusers);
262 if (pthread_mutexattr_destroy (&ma) != 0)
264 puts ("mutexattr_destroy failed");
268 if (munmap (map, N * sizeof (pthread_mutex_t)) != 0)
270 puts ("munmap failed");