1 /* Copyright (C) 2004-2022 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
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.
8
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.
13
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/>. */
17
18 #include <errno.h>
19 #include <pthread.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <sys/time.h>
25
26
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;
30
31 static void *
tf(void * p)32 tf (void *p)
33 {
34 if (pthread_mutex_lock (&mut) != 0)
35 {
36 printf ("%s: 1st mutex_lock failed\n", __func__);
37 exit (1);
38 }
39 if (pthread_mutex_lock (&mut) != 0)
40 {
41 printf ("%s: 2nd mutex_lock failed\n", __func__);
42 exit (1);
43 }
44 if (pthread_mutex_lock (&mut) != 0)
45 {
46 printf ("%s: 3rd mutex_lock failed\n", __func__);
47 exit (1);
48 }
49
50 if (pthread_mutex_unlock (&mut2) != 0)
51 {
52 printf ("%s: mutex_unlock failed\n", __func__);
53 exit (1);
54 }
55
56 struct timeval tv;
57 gettimeofday (&tv, NULL);
58 struct timespec ts;
59 TIMEVAL_TO_TIMESPEC (&tv, &ts);
60 ts.tv_sec += p == NULL ? 100 : 1;
61
62 int err = pthread_cond_timedwait (&cond, &mut, &ts);
63 if ((err != 0 && p == NULL) || (err != ETIMEDOUT && p != NULL))
64 {
65 printf ("%s: cond_wait failed\n", __func__);
66 exit (1);
67 }
68
69 if (pthread_mutex_unlock (&mut) != 0)
70 {
71 printf ("%s: 1st mutex_unlock failed\n", __func__);
72 exit (1);
73 }
74 if (pthread_mutex_unlock (&mut) != 0)
75 {
76 printf ("%s: 2nd mutex_unlock failed\n", __func__);
77 exit (1);
78 }
79 if (pthread_mutex_unlock (&mut) != 0)
80 {
81 printf ("%s: 3rd mutex_unlock failed\n", __func__);
82 exit (1);
83 }
84
85 puts ("child: done");
86
87 return NULL;
88 }
89
90
91 static int
do_test(void)92 do_test (void)
93 {
94 if (pthread_mutex_lock (&mut2) != 0)
95 {
96 puts ("1st mutex_lock failed");
97 return 1;
98 }
99
100 puts ("parent: create 1st child");
101
102 pthread_t th;
103 int err = pthread_create (&th, NULL, tf, NULL);
104 if (err != 0)
105 {
106 printf ("parent: cannot 1st create thread: %s\n", strerror (err));
107 return 1;
108 }
109
110 /* We have to synchronize with the child. */
111 if (pthread_mutex_lock (&mut2) != 0)
112 {
113 puts ("2nd mutex_lock failed");
114 return 1;
115 }
116
117 /* Give the child to reach to pthread_cond_wait. */
118 sleep (1);
119
120 if (pthread_cond_signal (&cond) != 0)
121 {
122 puts ("cond_signal failed");
123 return 1;
124 }
125
126 err = pthread_join (th, NULL);
127 if (err != 0)
128 {
129 printf ("parent: failed to join: %s\n", strerror (err));
130 return 1;
131 }
132
133
134 puts ("parent: create 2nd child");
135
136 err = pthread_create (&th, NULL, tf, (void *) 1l);
137 if (err != 0)
138 {
139 printf ("parent: cannot 2nd create thread: %s\n", strerror (err));
140 return 1;
141 }
142
143 err = pthread_join (th, NULL);
144 if (err != 0)
145 {
146 printf ("parent: failed to join: %s\n", strerror (err));
147 return 1;
148 }
149
150 puts ("done");
151
152 return 0;
153 }
154
155
156 #define TEST_FUNCTION do_test ()
157 #include "../test-skeleton.c"
158