1 /* Copyright (C) 2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
28 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
29 static pthread_mutex_t mut = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
31 static pthread_barrier_t bar;
37 int e = pthread_mutex_lock (&mut);
40 puts ("mutex not locked at all by cond_wait");
46 puts ("no deadlock error signaled");
50 if (pthread_mutex_unlock (&mut) != 0)
52 puts ("ch: cannot unlock mutex");
65 if (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL) != 0
66 || pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, NULL) != 0)
68 puts ("cannot set cancellation options");
72 err = pthread_mutex_lock (&mut);
75 puts ("child: cannot get mutex");
79 err = pthread_barrier_wait (&bar);
80 if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
82 printf ("barrier_wait returned %d\n", err);
86 puts ("child: got mutex; waiting");
88 pthread_cleanup_push (ch, NULL);
90 pthread_cond_wait (&cond, &mut);
92 pthread_cleanup_pop (0);
94 puts ("child: cond_wait should not have returned");
105 if (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL) != 0
106 || pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, NULL) != 0)
108 puts ("cannot set cancellation options");
112 err = pthread_mutex_lock (&mut);
115 puts ("child: cannot get mutex");
119 err = pthread_barrier_wait (&bar);
120 if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
122 printf ("barrier_wait returned %d\n", err);
126 puts ("child: got mutex; waiting");
128 pthread_cleanup_push (ch, NULL);
132 (void) gettimeofday (&tv, NULL);
133 /* +1000 seconds in correct format. */
135 TIMEVAL_TO_TIMESPEC (&tv, &ts);
138 pthread_cond_timedwait (&cond, &mut, &ts);
140 pthread_cleanup_pop (0);
142 puts ("child: cond_wait should not have returned");
154 printf ("&cond = %p\n&mut = %p\n", &cond, &mut);
156 puts ("parent: get mutex");
158 err = pthread_barrier_init (&bar, NULL, 2);
161 puts ("parent: cannot init barrier");
165 puts ("parent: create child");
167 err = pthread_create (&th, NULL, tf1, NULL);
170 puts ("parent: cannot create thread");
174 puts ("parent: wait for child to lock mutex");
176 err = pthread_barrier_wait (&bar);
177 if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
179 puts ("parent: cannot wait for barrier");
183 err = pthread_mutex_lock (&mut);
186 puts ("parent: mutex_lock failed");
190 err = pthread_mutex_unlock (&mut);
193 puts ("parent: mutex_unlock failed");
197 if (pthread_cancel (th) != 0)
199 puts ("cannot cancel thread");
204 err = pthread_join (th, &r);
207 puts ("parent: failed to join");
211 if (r != PTHREAD_CANCELED)
213 puts ("child hasn't been canceled");
219 puts ("parent: create 2nd child");
221 err = pthread_create (&th, NULL, tf2, NULL);
224 puts ("parent: cannot create thread");
228 puts ("parent: wait for child to lock mutex");
230 err = pthread_barrier_wait (&bar);
231 if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
233 puts ("parent: cannot wait for barrier");
237 err = pthread_mutex_lock (&mut);
240 puts ("parent: mutex_lock failed");
244 err = pthread_mutex_unlock (&mut);
247 puts ("parent: mutex_unlock failed");
251 if (pthread_cancel (th) != 0)
253 puts ("cannot cancel thread");
257 err = pthread_join (th, &r);
260 puts ("parent: failed to join");
264 if (r != PTHREAD_CANCELED)
266 puts ("child hasn't been canceled");
276 #define TEST_FUNCTION do_test ()
277 #include "../test-skeleton.c"