libio: Remove unused pragma weak on vtable
[glibc.git] / sysdeps / pthread / tst-mutex4.c
blob531dbfc238da910b355b1800e46a8c1d05d7485e
1 /* Copyright (C) 2002-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
18 #include <errno.h>
19 #include <pthread.h>
20 #include <stdint.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <sys/mman.h>
26 #include <sys/wait.h>
29 static int
30 do_test (void)
32 size_t ps = sysconf (_SC_PAGESIZE);
33 char tmpfname[] = "/tmp/tst-mutex4.XXXXXX";
34 char data[ps];
35 void *mem;
36 int fd;
37 pthread_mutex_t *m;
38 pthread_mutexattr_t a;
39 pid_t pid;
40 char *p;
41 int err;
42 int s;
43 pthread_barrier_t *b;
44 pthread_barrierattr_t ba;
46 fd = mkstemp (tmpfname);
47 if (fd == -1)
49 printf ("cannot open temporary file: %m\n");
50 return 1;
53 /* Make sure it is always removed. */
54 unlink (tmpfname);
56 /* Create one page of data. */
57 memset (data, '\0', ps);
59 /* Write the data to the file. */
60 if (write (fd, data, ps) != (ssize_t) ps)
62 puts ("short write");
63 return 1;
66 mem = mmap (NULL, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
67 if (mem == MAP_FAILED)
69 printf ("mmap failed: %m\n");
70 return 1;
73 m = (pthread_mutex_t *) (((uintptr_t) mem + __alignof (pthread_mutex_t) - 1)
74 & ~(__alignof (pthread_mutex_t) - 1));
75 b = (pthread_barrier_t *) (((uintptr_t) (m + 1)
76 + __alignof (pthread_barrier_t) - 1)
77 & ~(__alignof (pthread_barrier_t) - 1));
78 p = (char *) (b + 1);
80 if (pthread_mutexattr_init (&a) != 0)
82 puts ("mutexattr_init failed");
83 return 1;
86 if (pthread_mutexattr_getpshared (&a, &s) != 0)
88 puts ("1st mutexattr_getpshared failed");
89 return 1;
92 if (s != PTHREAD_PROCESS_PRIVATE)
94 puts ("default pshared value wrong");
95 return 1;
98 if (pthread_mutexattr_setpshared (&a, PTHREAD_PROCESS_SHARED) != 0)
100 puts ("mutexattr_setpshared failed");
101 return 1;
104 if (pthread_mutexattr_getpshared (&a, &s) != 0)
106 puts ("2nd mutexattr_getpshared failed");
107 return 1;
110 if (s != PTHREAD_PROCESS_SHARED)
112 puts ("pshared value after setpshared call wrong");
113 return 1;
116 #ifdef ENABLE_PI
117 if (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_INHERIT) != 0)
119 puts ("pthread_mutexattr_setprotocol failed");
120 return 1;
122 #endif
124 if ((err = pthread_mutex_init (m, &a)) != 0)
126 #ifdef ENABLE_PI
127 if (err == ENOTSUP)
129 puts ("PI mutexes unsupported");
130 return 0;
132 #endif
133 puts ("mutex_init failed");
134 return 1;
137 if (pthread_mutex_lock (m) != 0)
139 puts ("mutex_lock failed");
140 return 1;
143 if (pthread_mutexattr_destroy (&a) != 0)
145 puts ("mutexattr_destroy failed");
146 return 1;
149 if (pthread_barrierattr_init (&ba) != 0)
151 puts ("barrierattr_init failed");
152 return 1;
155 if (pthread_barrierattr_setpshared (&ba, PTHREAD_PROCESS_SHARED) != 0)
157 puts ("barrierattr_setpshared failed");
158 return 1;
161 if (pthread_barrier_init (b, &ba, 2) != 0)
163 puts ("barrier_init failed");
164 return 1;
167 if (pthread_barrierattr_destroy (&ba) != 0)
169 puts ("barrierattr_destroy failed");
170 return 1;
173 err = pthread_mutex_trylock (m);
174 if (err == 0)
176 puts ("mutex_trylock succeeded");
177 return 1;
179 else if (err != EBUSY)
181 puts ("mutex_trylock didn't return EBUSY");
182 return 1;
185 *p = 0;
187 if (pthread_mutex_unlock (m) != 0)
189 puts ("parent: 1st mutex_unlock failed");
190 return 1;
193 puts ("going to fork now");
194 pid = fork ();
195 if (pid == -1)
197 puts ("fork failed");
198 return 1;
200 else if (pid == 0)
202 if (pthread_mutex_lock (m) != 0)
204 puts ("child: mutex_lock failed");
205 return 1;
208 int e = pthread_barrier_wait (b);
209 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
211 puts ("child: barrier_wait failed");
212 return 1;
215 if ((*p)++ != 0)
217 puts ("child: *p != 0");
218 return 1;
221 if (pthread_mutex_unlock (m) != 0)
223 puts ("child: mutex_unlock failed");
224 return 1;
227 puts ("child done");
229 else
231 int e = pthread_barrier_wait (b);
232 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
234 puts ("parent: barrier_wait failed");
235 return 1;
238 if (pthread_mutex_lock (m) != 0)
240 puts ("parent: 2nd mutex_lock failed");
241 return 1;
244 if (*p != 1)
246 puts ("*p != 1");
247 return 1;
250 if (pthread_mutex_unlock (m) != 0)
252 puts ("parent: 2nd mutex_unlock failed");
253 return 1;
256 if (pthread_mutex_destroy (m) != 0)
258 puts ("mutex_destroy failed");
259 return 1;
262 if (pthread_barrier_destroy (b) != 0)
264 puts ("barrier_destroy failed");
265 return 1;
268 puts ("parent done");
271 return 0;
274 #define TEST_FUNCTION do_test ()
275 #include "../test-skeleton.c"