1 /* Copyright (C) 2002, 2003, 2004, 2006 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/>. */
33 size_t ps
= sysconf (_SC_PAGESIZE
);
34 char tmpfname
[] = "/tmp/tst-mutex4.XXXXXX";
39 pthread_mutexattr_t a
;
45 pthread_barrierattr_t ba
;
47 fd
= mkstemp (tmpfname
);
50 printf ("cannot open temporary file: %m\n");
54 /* Make sure it is always removed. */
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
)
67 mem
= mmap (NULL
, ps
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
68 if (mem
== MAP_FAILED
)
70 printf ("mmap failed: %m\n");
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));
81 if (pthread_mutexattr_init (&a
) != 0)
83 puts ("mutexattr_init failed");
87 if (pthread_mutexattr_getpshared (&a
, &s
) != 0)
89 puts ("1st mutexattr_getpshared failed");
93 if (s
!= PTHREAD_PROCESS_PRIVATE
)
95 puts ("default pshared value wrong");
99 if (pthread_mutexattr_setpshared (&a
, PTHREAD_PROCESS_SHARED
) != 0)
101 puts ("mutexattr_setpshared failed");
105 if (pthread_mutexattr_getpshared (&a
, &s
) != 0)
107 puts ("2nd mutexattr_getpshared failed");
111 if (s
!= PTHREAD_PROCESS_SHARED
)
113 puts ("pshared value after setpshared call wrong");
118 if (pthread_mutexattr_setprotocol (&a
, PTHREAD_PRIO_INHERIT
) != 0)
120 puts ("pthread_mutexattr_setprotocol failed");
125 if ((err
= pthread_mutex_init (m
, &a
)) != 0)
130 puts ("PI mutexes unsupported");
134 puts ("mutex_init failed");
138 if (pthread_mutex_lock (m
) != 0)
140 puts ("mutex_lock failed");
144 if (pthread_mutexattr_destroy (&a
) != 0)
146 puts ("mutexattr_destroy failed");
150 if (pthread_barrierattr_init (&ba
) != 0)
152 puts ("barrierattr_init failed");
156 if (pthread_barrierattr_setpshared (&ba
, PTHREAD_PROCESS_SHARED
) != 0)
158 puts ("barrierattr_setpshared failed");
162 if (pthread_barrier_init (b
, &ba
, 2) != 0)
164 puts ("barrier_init failed");
168 if (pthread_barrierattr_destroy (&ba
) != 0)
170 puts ("barrierattr_destroy failed");
174 err
= pthread_mutex_trylock (m
);
177 puts ("mutex_trylock succeeded");
180 else if (err
!= EBUSY
)
182 puts ("mutex_trylock didn't return EBUSY");
188 if (pthread_mutex_unlock (m
) != 0)
190 puts ("parent: 1st mutex_unlock failed");
194 puts ("going to fork now");
198 puts ("fork failed");
203 if (pthread_mutex_lock (m
) != 0)
205 puts ("child: mutex_lock failed");
209 int e
= pthread_barrier_wait (b
);
210 if (e
!= 0 && e
!= PTHREAD_BARRIER_SERIAL_THREAD
)
212 puts ("child: barrier_wait failed");
218 puts ("child: *p != 0");
222 if (pthread_mutex_unlock (m
) != 0)
224 puts ("child: mutex_unlock failed");
232 int e
= pthread_barrier_wait (b
);
233 if (e
!= 0 && e
!= PTHREAD_BARRIER_SERIAL_THREAD
)
235 puts ("parent: barrier_wait failed");
239 if (pthread_mutex_lock (m
) != 0)
241 puts ("parent: 2nd mutex_lock failed");
251 if (pthread_mutex_unlock (m
) != 0)
253 puts ("parent: 2nd mutex_unlock failed");
257 if (pthread_mutex_destroy (m
) != 0)
259 puts ("mutex_destroy failed");
263 if (pthread_barrier_destroy (b
) != 0)
265 puts ("barrier_destroy failed");
269 puts ("parent done");
276 #define TEST_FUNCTION do_test ()
277 #include "../test-skeleton.c"