Partial ILP32 support for aarch64.
[glibc.git] / nptl / tst-mutex4.c
blobc75bffbfb29bb094a90340b82f1da0f26d7c7d00
1 /* Copyright (C) 2002-2016 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <pthread.h>
21 #include <stdint.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/mman.h>
27 #include <sys/wait.h>
30 static int
31 do_test (void)
33 size_t ps = sysconf (_SC_PAGESIZE);
34 char tmpfname[] = "/tmp/tst-mutex4.XXXXXX";
35 char data[ps];
36 void *mem;
37 int fd;
38 pthread_mutex_t *m;
39 pthread_mutexattr_t a;
40 pid_t pid;
41 char *p;
42 int err;
43 int s;
44 pthread_barrier_t *b;
45 pthread_barrierattr_t ba;
47 fd = mkstemp (tmpfname);
48 if (fd == -1)
50 printf ("cannot open temporary file: %m\n");
51 return 1;
54 /* Make sure it is always removed. */
55 unlink (tmpfname);
57 /* Create one page of data. */
58 memset (data, '\0', ps);
60 /* Write the data to the file. */
61 if (write (fd, data, ps) != (ssize_t) ps)
63 puts ("short write");
64 return 1;
67 mem = mmap (NULL, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
68 if (mem == MAP_FAILED)
70 printf ("mmap failed: %m\n");
71 return 1;
74 m = (pthread_mutex_t *) (((uintptr_t) mem + __alignof (pthread_mutex_t) - 1)
75 & ~(__alignof (pthread_mutex_t) - 1));
76 b = (pthread_barrier_t *) (((uintptr_t) (m + 1)
77 + __alignof (pthread_barrier_t) - 1)
78 & ~(__alignof (pthread_barrier_t) - 1));
79 p = (char *) (b + 1);
81 if (pthread_mutexattr_init (&a) != 0)
83 puts ("mutexattr_init failed");
84 return 1;
87 if (pthread_mutexattr_getpshared (&a, &s) != 0)
89 puts ("1st mutexattr_getpshared failed");
90 return 1;
93 if (s != PTHREAD_PROCESS_PRIVATE)
95 puts ("default pshared value wrong");
96 return 1;
99 if (pthread_mutexattr_setpshared (&a, PTHREAD_PROCESS_SHARED) != 0)
101 puts ("mutexattr_setpshared failed");
102 return 1;
105 if (pthread_mutexattr_getpshared (&a, &s) != 0)
107 puts ("2nd mutexattr_getpshared failed");
108 return 1;
111 if (s != PTHREAD_PROCESS_SHARED)
113 puts ("pshared value after setpshared call wrong");
114 return 1;
117 #ifdef ENABLE_PI
118 if (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_INHERIT) != 0)
120 puts ("pthread_mutexattr_setprotocol failed");
121 return 1;
123 #endif
125 if ((err = pthread_mutex_init (m, &a)) != 0)
127 #ifdef ENABLE_PI
128 if (err == ENOTSUP)
130 puts ("PI mutexes unsupported");
131 return 0;
133 #endif
134 puts ("mutex_init failed");
135 return 1;
138 if (pthread_mutex_lock (m) != 0)
140 puts ("mutex_lock failed");
141 return 1;
144 if (pthread_mutexattr_destroy (&a) != 0)
146 puts ("mutexattr_destroy failed");
147 return 1;
150 if (pthread_barrierattr_init (&ba) != 0)
152 puts ("barrierattr_init failed");
153 return 1;
156 if (pthread_barrierattr_setpshared (&ba, PTHREAD_PROCESS_SHARED) != 0)
158 puts ("barrierattr_setpshared failed");
159 return 1;
162 if (pthread_barrier_init (b, &ba, 2) != 0)
164 puts ("barrier_init failed");
165 return 1;
168 if (pthread_barrierattr_destroy (&ba) != 0)
170 puts ("barrierattr_destroy failed");
171 return 1;
174 err = pthread_mutex_trylock (m);
175 if (err == 0)
177 puts ("mutex_trylock succeeded");
178 return 1;
180 else if (err != EBUSY)
182 puts ("mutex_trylock didn't return EBUSY");
183 return 1;
186 *p = 0;
188 if (pthread_mutex_unlock (m) != 0)
190 puts ("parent: 1st mutex_unlock failed");
191 return 1;
194 puts ("going to fork now");
195 pid = fork ();
196 if (pid == -1)
198 puts ("fork failed");
199 return 1;
201 else if (pid == 0)
203 if (pthread_mutex_lock (m) != 0)
205 puts ("child: mutex_lock failed");
206 return 1;
209 int e = pthread_barrier_wait (b);
210 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
212 puts ("child: barrier_wait failed");
213 return 1;
216 if ((*p)++ != 0)
218 puts ("child: *p != 0");
219 return 1;
222 if (pthread_mutex_unlock (m) != 0)
224 puts ("child: mutex_unlock failed");
225 return 1;
228 puts ("child done");
230 else
232 int e = pthread_barrier_wait (b);
233 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
235 puts ("parent: barrier_wait failed");
236 return 1;
239 if (pthread_mutex_lock (m) != 0)
241 puts ("parent: 2nd mutex_lock failed");
242 return 1;
245 if (*p != 1)
247 puts ("*p != 1");
248 return 1;
251 if (pthread_mutex_unlock (m) != 0)
253 puts ("parent: 2nd mutex_unlock failed");
254 return 1;
257 if (pthread_mutex_destroy (m) != 0)
259 puts ("mutex_destroy failed");
260 return 1;
263 if (pthread_barrier_destroy (b) != 0)
265 puts ("barrier_destroy failed");
266 return 1;
269 puts ("parent done");
272 return 0;
275 #define TIMEOUT 4
276 #define TEST_FUNCTION do_test ()
277 #include "../test-skeleton.c"