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");
63 if (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE
, NULL
) != 0
64 || pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED
, NULL
) != 0)
66 puts ("cannot set cancellation options");
70 err
= pthread_mutex_lock (&mut
);
73 puts ("child: cannot get mutex");
77 err
= pthread_barrier_wait (&bar
);
78 if (err
!= 0 && err
!= PTHREAD_BARRIER_SERIAL_THREAD
)
80 printf ("barrier_wait returned %d\n", err
);
84 puts ("child: got mutex; waiting");
86 pthread_cleanup_push (ch
, NULL
);
88 pthread_cond_wait (&cond
, &mut
);
90 pthread_cleanup_pop (0);
92 puts ("child: cond_wait should not have returned");
103 if (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE
, NULL
) != 0
104 || pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED
, NULL
) != 0)
106 puts ("cannot set cancellation options");
110 err
= pthread_mutex_lock (&mut
);
113 puts ("child: cannot get mutex");
117 err
= pthread_barrier_wait (&bar
);
118 if (err
!= 0 && err
!= PTHREAD_BARRIER_SERIAL_THREAD
)
120 printf ("barrier_wait returned %d\n", err
);
124 puts ("child: got mutex; waiting");
126 pthread_cleanup_push (ch
, NULL
);
130 (void) gettimeofday (&tv
, NULL
);
131 /* +1000 seconds in correct format. */
133 TIMEVAL_TO_TIMESPEC (&tv
, &ts
);
136 pthread_cond_timedwait (&cond
, &mut
, &ts
);
138 pthread_cleanup_pop (0);
140 puts ("child: cond_wait should not have returned");
152 printf ("&cond = %p\n&mut = %p\n", &cond
, &mut
);
154 puts ("parent: get mutex");
156 err
= pthread_barrier_init (&bar
, NULL
, 2);
159 puts ("parent: cannot init barrier");
163 puts ("parent: create child");
165 err
= pthread_create (&th
, NULL
, tf1
, NULL
);
168 puts ("parent: cannot create thread");
172 puts ("parent: wait for child to lock mutex");
174 err
= pthread_barrier_wait (&bar
);
175 if (err
!= 0 && err
!= PTHREAD_BARRIER_SERIAL_THREAD
)
177 puts ("parent: cannot wait for barrier");
181 err
= pthread_mutex_lock (&mut
);
184 puts ("parent: mutex_lock failed");
188 err
= pthread_mutex_unlock (&mut
);
191 puts ("parent: mutex_unlock failed");
195 if (pthread_cancel (th
) != 0)
197 puts ("cannot cancel thread");
202 err
= pthread_join (th
, &r
);
205 puts ("parent: failed to join");
209 if (r
!= PTHREAD_CANCELED
)
211 puts ("child hasn't been canceled");
217 puts ("parent: create 2nd child");
219 err
= pthread_create (&th
, NULL
, tf2
, NULL
);
222 puts ("parent: cannot create thread");
226 puts ("parent: wait for child to lock mutex");
228 err
= pthread_barrier_wait (&bar
);
229 if (err
!= 0 && err
!= PTHREAD_BARRIER_SERIAL_THREAD
)
231 puts ("parent: cannot wait for barrier");
235 err
= pthread_mutex_lock (&mut
);
238 puts ("parent: mutex_lock failed");
242 err
= pthread_mutex_unlock (&mut
);
245 puts ("parent: mutex_unlock failed");
249 if (pthread_cancel (th
) != 0)
251 puts ("cannot cancel thread");
255 err
= pthread_join (th
, &r
);
258 puts ("parent: failed to join");
262 if (r
!= PTHREAD_CANCELED
)
264 puts ("child hasn't been canceled");
274 #define TEST_FUNCTION do_test ()
275 #include "../test-skeleton.c"