1 /* Copyright (C) 2002-2024 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/>. */
32 size_t ps
= sysconf (_SC_PAGESIZE
);
33 char tmpfname
[] = "/tmp/tst-mutex4.XXXXXX";
38 pthread_mutexattr_t a
;
44 pthread_barrierattr_t ba
;
46 fd
= mkstemp (tmpfname
);
49 printf ("cannot open temporary file: %m\n");
53 /* Make sure it is always removed. */
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
)
66 mem
= mmap (NULL
, ps
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
67 if (mem
== MAP_FAILED
)
69 printf ("mmap failed: %m\n");
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));
80 if (pthread_mutexattr_init (&a
) != 0)
82 puts ("mutexattr_init failed");
86 if (pthread_mutexattr_getpshared (&a
, &s
) != 0)
88 puts ("1st mutexattr_getpshared failed");
92 if (s
!= PTHREAD_PROCESS_PRIVATE
)
94 puts ("default pshared value wrong");
98 if (pthread_mutexattr_setpshared (&a
, PTHREAD_PROCESS_SHARED
) != 0)
100 puts ("mutexattr_setpshared failed");
104 if (pthread_mutexattr_getpshared (&a
, &s
) != 0)
106 puts ("2nd mutexattr_getpshared failed");
110 if (s
!= PTHREAD_PROCESS_SHARED
)
112 puts ("pshared value after setpshared call wrong");
117 if (pthread_mutexattr_setprotocol (&a
, PTHREAD_PRIO_INHERIT
) != 0)
119 puts ("pthread_mutexattr_setprotocol failed");
124 if ((err
= pthread_mutex_init (m
, &a
)) != 0)
129 puts ("PI mutexes unsupported");
133 puts ("mutex_init failed");
137 if (pthread_mutex_lock (m
) != 0)
139 puts ("mutex_lock failed");
143 if (pthread_mutexattr_destroy (&a
) != 0)
145 puts ("mutexattr_destroy failed");
149 if (pthread_barrierattr_init (&ba
) != 0)
151 puts ("barrierattr_init failed");
155 if (pthread_barrierattr_setpshared (&ba
, PTHREAD_PROCESS_SHARED
) != 0)
157 puts ("barrierattr_setpshared failed");
161 if (pthread_barrier_init (b
, &ba
, 2) != 0)
163 puts ("barrier_init failed");
167 if (pthread_barrierattr_destroy (&ba
) != 0)
169 puts ("barrierattr_destroy failed");
173 err
= pthread_mutex_trylock (m
);
176 puts ("mutex_trylock succeeded");
179 else if (err
!= EBUSY
)
181 puts ("mutex_trylock didn't return EBUSY");
187 if (pthread_mutex_unlock (m
) != 0)
189 puts ("parent: 1st mutex_unlock failed");
193 puts ("going to fork now");
197 puts ("fork failed");
202 if (pthread_mutex_lock (m
) != 0)
204 puts ("child: mutex_lock failed");
208 int e
= pthread_barrier_wait (b
);
209 if (e
!= 0 && e
!= PTHREAD_BARRIER_SERIAL_THREAD
)
211 puts ("child: barrier_wait failed");
217 puts ("child: *p != 0");
221 if (pthread_mutex_unlock (m
) != 0)
223 puts ("child: mutex_unlock failed");
231 int e
= pthread_barrier_wait (b
);
232 if (e
!= 0 && e
!= PTHREAD_BARRIER_SERIAL_THREAD
)
234 puts ("parent: barrier_wait failed");
238 if (pthread_mutex_lock (m
) != 0)
240 puts ("parent: 2nd mutex_lock failed");
250 if (pthread_mutex_unlock (m
) != 0)
252 puts ("parent: 2nd mutex_unlock failed");
256 if (pthread_mutex_destroy (m
) != 0)
258 puts ("mutex_destroy failed");
262 if (pthread_barrier_destroy (b
) != 0)
264 puts ("barrier_destroy failed");
268 puts ("parent done");
274 #define TEST_FUNCTION do_test ()
275 #include "../test-skeleton.c"