summaryrefslogtreecommitdiff
path: root/nptl/tst-robust8.c
blob: 19682e594f1cbdb40a541244729862d385863131 (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
#include <pthread.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/wait.h>




static void prepare (void);
#define PREPARE(argc, argv) prepare ()
static int do_test (void);
#define TEST_FUNCTION do_test ()
#define TIMEOUT 3
#include "../test-skeleton.c"


static int fd;
#define N 100

static void
prepare (void)
{
  fd = create_temp_file ("tst-robust8", NULL);
  if (fd == -1)
    exit (1);
}


#define THESIGNAL SIGKILL
#define ROUNDS 5
#define THREADS 9


static const struct timespec before = { 0, 0 };


static pthread_mutex_t *map;


static void *
tf (void *arg)
{
  long int nr = (long int) arg;
  int fct = nr % 3;

  uint8_t state[N];
  memset (state, '\0', sizeof (state));

  while (1)
    {
      int r = random () % N;
      if (state[r] == 0)
	{
	  int e;

	  switch (fct)
	    {
	    case 0:
	      e = pthread_mutex_lock (&map[r]);
	      if (e != 0)
		{
		  printf ("mutex_lock of %d in thread %ld failed with %d\n",
			  r, nr, e);
		  exit (1);
		}
	      state[r] = 1;
	      break;
	    case 1:
	      e = pthread_mutex_timedlock (&map[r], &before);
	      if (e != 0 && e != ETIMEDOUT)
		{
		  printf ("\
mutex_timedlock of %d in thread %ld failed with %d\n",
			  r, nr, e);
		  exit (1);
		}
	      break;
	    default:
	      e = pthread_mutex_trylock (&map[r]);
	      if (e != 0 && e != EBUSY)
		{
		  printf ("mutex_trylock of %d in thread %ld failed with %d\n",
			  r, nr, e);
		  exit (1);
		}
	      break;
	    }

	  if (e == EOWNERDEAD)
	    pthread_mutex_consistent_np (&map[r]);

	  if (e == 0 || e == EOWNERDEAD)
	    state[r] = 1;
	}
      else
	{
	  int e = pthread_mutex_unlock (&map[r]);
	  if (e != 0)
	    {
	      printf ("mutex_unlock of %d in thread %ld failed with %d\n",
		      r, nr, e);
	      exit (1);
	    }

	  state[r] = 0;
	}
    }
}


static void
child (int round)
{
  for (int thread = 1; thread <= THREADS; ++thread)
    {
      pthread_t th;
      if (pthread_create (&th, NULL, tf, (void *) (long int) thread) != 0)
	{
	  printf ("cannot create thread %d in round %d\n", thread, round);
	  exit (1);
	}
    }

  struct timespec ts;
  ts.tv_sec = 0;
  ts.tv_nsec = 1000000000 / ROUNDS;
  while (nanosleep (&ts, &ts) != 0)
    /* nothing */;

  /* Time to die.  */
  kill (getpid (), THESIGNAL);

  /* We better never get here.  */
  abort ();
}


static int
do_test (void)
{
  if (ftruncate (fd, N * sizeof (pthread_mutex_t)) != 0)
    {
      puts ("cannot size new file");
      return 1;
    }

  map = mmap (NULL, N * sizeof (pthread_mutex_t), PROT_READ | PROT_WRITE,
	      MAP_SHARED, fd, 0);
  if (map == MAP_FAILED)
    {
      puts ("mapping failed");
      return 1;
    }

  pthread_mutexattr_t ma;
  if (pthread_mutexattr_init (&ma) != 0)
    {
      puts ("mutexattr_init failed");
      return 0;
    }
  if (pthread_mutexattr_setrobust_np (&ma, PTHREAD_MUTEX_ROBUST_NP) != 0)
    {
      puts ("mutexattr_setrobust failed");
      return 1;
    }
  if (pthread_mutexattr_setpshared (&ma, PTHREAD_PROCESS_SHARED) != 0)
    {
      puts ("mutexattr_setpshared failed");
      return 1;
    }

  for (int round = 1; round <= ROUNDS; ++round)
    {
      for (int n = 0; n < N; ++n)
	{
	  int e = pthread_mutex_init (&map[n], &ma);
	  if (e == ENOTSUP)
	    {
	      puts ("cannot support pshared robust mutexes");
	      return 0;
	    }
	  if (e != 0)
	    {
	      printf ("mutex_init %d in round %d failed\n", n + 1, round);
	      return 1;
	    }
	}

      pid_t p = fork ();
      if (p == -1)
	{
	  printf ("fork in round %d failed\n", round);
	  return 1;
	}
      if (p == 0)
	child (round);

      int status;
      if (TEMP_FAILURE_RETRY (waitpid (p, &status, 0)) != p)
	{
	  printf ("waitpid in round %d failed\n", round);
	  return 1;
	}
      if (!WIFSIGNALED (status))
	{
	  printf ("child did not die of a signal in round %d\n", round);
	  return 1;
	}
      if (WTERMSIG (status) != THESIGNAL)
	{
	  printf ("child did not die of signal %d in round %d\n",
		  THESIGNAL, round);
	  return 1;
	}

      for (int n = 0; n < N; ++n)
	{
	  int e = pthread_mutex_lock (&map[n]);
	  if (e != 0 && e != EOWNERDEAD)
	    {
	      printf ("mutex_lock %d failed in round %d\n", n + 1, round);
	      return 1;
	    }
	}

      for (int n = 0; n < N; ++n)
	if (pthread_mutex_unlock (&map[n]) != 0)
	  {
	    printf ("mutex_unlock %d failed in round %d\n", n + 1, round);
	    return 1;
	  }

      for (int n = 0; n < N; ++n)
	{
	  int e = pthread_mutex_destroy (&map[n]);
	  if (e != 0)
	    {
	      printf ("mutex_destroy %d in round %d failed with %d\n",
		      n + 1, round, e);
	      printf("nusers = %d\n", (int) map[n].__data.__nusers);
	      return 1;
	    }
	}
    }

  if (pthread_mutexattr_destroy (&ma) != 0)
    {
      puts ("mutexattr_destroy failed");
      return 1;
    }

  if (munmap (map, N * sizeof (pthread_mutex_t)) != 0)
    {
      puts ("munmap failed");
      return 1;
    }

  return 0;
}