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/>. */
34 size_t ps
= sysconf (_SC_PAGESIZE
);
35 char tmpfname
[] = "/tmp/tst-cond4.XXXXXX";
39 pthread_mutexattr_t ma
;
40 pthread_mutex_t
*mut1
;
41 pthread_mutex_t
*mut2
;
42 pthread_condattr_t ca
;
48 fd
= mkstemp (tmpfname
);
51 printf ("cannot open temporary file: %m\n");
55 /* Make sure it is always removed. */
58 /* Create one page of data. */
59 memset (data
, '\0', ps
);
61 /* Write the data to the file. */
62 if (write (fd
, data
, ps
) != (ssize_t
) ps
)
68 mem
= mmap (NULL
, ps
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
69 if (mem
== MAP_FAILED
)
71 printf ("mmap failed: %m\n");
75 mut1
= (pthread_mutex_t
*) (((uintptr_t) mem
76 + __alignof (pthread_mutex_t
))
77 & ~(__alignof (pthread_mutex_t
) - 1));
80 cond
= (pthread_cond_t
*) (((uintptr_t) (mut2
+ 1)
81 + __alignof (pthread_cond_t
))
82 & ~(__alignof (pthread_cond_t
) - 1));
84 condition
= (int *) (((uintptr_t) (cond
+ 1) + __alignof (int))
85 & ~(__alignof (int) - 1));
87 if (pthread_mutexattr_init (&ma
) != 0)
89 puts ("mutexattr_init failed");
93 if (pthread_mutexattr_getpshared (&ma
, &p
) != 0)
95 puts ("1st mutexattr_getpshared failed");
99 if (p
!= PTHREAD_PROCESS_PRIVATE
)
101 puts ("default pshared value wrong");
105 if (pthread_mutexattr_setpshared (&ma
, PTHREAD_PROCESS_SHARED
) != 0)
107 puts ("mutexattr_setpshared failed");
111 if (pthread_mutexattr_getpshared (&ma
, &p
) != 0)
113 puts ("2nd mutexattr_getpshared failed");
117 if (p
!= PTHREAD_PROCESS_SHARED
)
119 puts ("pshared value after setpshared call wrong");
123 if (pthread_mutex_init (mut1
, &ma
) != 0)
125 puts ("1st mutex_init failed");
129 if (pthread_mutex_init (mut2
, &ma
) != 0)
131 puts ("2nd mutex_init failed");
135 if (pthread_condattr_init (&ca
) != 0)
137 puts ("condattr_init failed");
141 if (pthread_condattr_getpshared (&ca
, &p
) != 0)
143 puts ("1st condattr_getpshared failed");
147 if (p
!= PTHREAD_PROCESS_PRIVATE
)
149 puts ("default value for pshared in condattr wrong");
153 if (pthread_condattr_setpshared (&ca
, PTHREAD_PROCESS_SHARED
) != 0)
155 puts ("condattr_setpshared failed");
159 if (pthread_condattr_getpshared (&ca
, &p
) != 0)
161 puts ("2nd condattr_getpshared failed");
165 if (p
!= PTHREAD_PROCESS_SHARED
)
167 puts ("pshared condattr still not set");
171 if (pthread_cond_init (cond
, &ca
) != 0)
173 puts ("cond_init failed");
177 if (pthread_mutex_lock (mut1
) != 0)
179 puts ("parent: 1st mutex_lock failed");
183 puts ("going to fork now");
187 puts ("fork failed");
192 if (pthread_mutex_lock (mut2
) != 0)
194 puts ("child: mutex_lock failed");
198 if (pthread_mutex_unlock (mut1
) != 0)
200 puts ("child: 1st mutex_unlock failed");
205 if (pthread_cond_wait (cond
, mut2
) != 0)
207 puts ("child: cond_wait failed");
210 while (*condition
== 0);
212 if (pthread_mutex_unlock (mut2
) != 0)
214 puts ("child: 2nd mutex_unlock failed");
224 if (pthread_mutex_lock (mut1
) != 0)
226 puts ("parent: 2nd mutex_lock failed");
230 if (pthread_mutex_lock (mut2
) != 0)
232 puts ("parent: 3rd mutex_lock failed");
236 if (pthread_cond_signal (cond
) != 0)
238 puts ("parent: cond_signal failed");
244 if (pthread_mutex_unlock (mut2
) != 0)
246 puts ("parent: mutex_unlock failed");
250 puts ("waiting for child");
252 waitpid (pid
, &status
, 0);
255 puts ("parent done");
261 #define TEST_FUNCTION do_test ()
262 #include "../test-skeleton.c"