1 /* Copyright (C) 2004-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/>. */
27 static pthread_cond_t cond
= PTHREAD_COND_INITIALIZER
;
28 static pthread_mutex_t mut
= PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
;
29 static pthread_mutex_t mut2
= PTHREAD_MUTEX_INITIALIZER
;
34 if (pthread_mutex_lock (&mut
) != 0)
36 printf ("%s: 1st mutex_lock failed\n", __func__
);
39 if (pthread_mutex_lock (&mut
) != 0)
41 printf ("%s: 2nd mutex_lock failed\n", __func__
);
44 if (pthread_mutex_lock (&mut
) != 0)
46 printf ("%s: 3rd mutex_lock failed\n", __func__
);
50 if (pthread_mutex_unlock (&mut2
) != 0)
52 printf ("%s: mutex_unlock failed\n", __func__
);
57 gettimeofday (&tv
, NULL
);
59 TIMEVAL_TO_TIMESPEC (&tv
, &ts
);
60 ts
.tv_sec
+= p
== NULL
? 100 : 1;
62 int err
= pthread_cond_timedwait (&cond
, &mut
, &ts
);
63 if ((err
!= 0 && p
== NULL
) || (err
!= ETIMEDOUT
&& p
!= NULL
))
65 printf ("%s: cond_wait failed\n", __func__
);
69 if (pthread_mutex_unlock (&mut
) != 0)
71 printf ("%s: 1st mutex_unlock failed\n", __func__
);
74 if (pthread_mutex_unlock (&mut
) != 0)
76 printf ("%s: 2nd mutex_unlock failed\n", __func__
);
79 if (pthread_mutex_unlock (&mut
) != 0)
81 printf ("%s: 3rd mutex_unlock failed\n", __func__
);
94 if (pthread_mutex_lock (&mut2
) != 0)
96 puts ("1st mutex_lock failed");
100 puts ("parent: create 1st child");
103 int err
= pthread_create (&th
, NULL
, tf
, NULL
);
106 printf ("parent: cannot 1st create thread: %s\n", strerror (err
));
110 /* We have to synchronize with the child. */
111 if (pthread_mutex_lock (&mut2
) != 0)
113 puts ("2nd mutex_lock failed");
117 /* Give the child to reach to pthread_cond_wait. */
120 if (pthread_cond_signal (&cond
) != 0)
122 puts ("cond_signal failed");
126 err
= pthread_join (th
, NULL
);
129 printf ("parent: failed to join: %s\n", strerror (err
));
134 puts ("parent: create 2nd child");
136 err
= pthread_create (&th
, NULL
, tf
, (void *) 1l);
139 printf ("parent: cannot 2nd create thread: %s\n", strerror (err
));
143 err
= pthread_join (th
, NULL
);
146 printf ("parent: failed to join: %s\n", strerror (err
));
156 #define TEST_FUNCTION do_test ()
157 #include "../test-skeleton.c"