1 /* Copyright (C) 2003-2018 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, see
17 <http://www.gnu.org/licenses/>. */
27 static pthread_cond_t cond
= PTHREAD_COND_INITIALIZER
;
28 static pthread_mutex_t mut
= PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
;
30 static pthread_barrier_t bar
;
36 int e
= pthread_mutex_lock (&mut
);
39 puts ("mutex not locked at all by cond_wait");
45 puts ("no deadlock error signaled");
49 if (pthread_mutex_unlock (&mut
) != 0)
51 puts ("ch: cannot unlock mutex");
64 if (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE
, NULL
) != 0
65 || pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED
, NULL
) != 0)
67 puts ("cannot set cancellation options");
71 err
= pthread_mutex_lock (&mut
);
74 puts ("child: cannot get mutex");
78 err
= pthread_barrier_wait (&bar
);
79 if (err
!= 0 && err
!= PTHREAD_BARRIER_SERIAL_THREAD
)
81 printf ("barrier_wait returned %d\n", err
);
85 puts ("child: got mutex; waiting");
87 pthread_cleanup_push (ch
, NULL
);
89 pthread_cond_wait (&cond
, &mut
);
91 pthread_cleanup_pop (0);
93 puts ("child: cond_wait should not have returned");
104 if (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE
, NULL
) != 0
105 || pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED
, NULL
) != 0)
107 puts ("cannot set cancellation options");
111 err
= pthread_mutex_lock (&mut
);
114 puts ("child: cannot get mutex");
118 err
= pthread_barrier_wait (&bar
);
119 if (err
!= 0 && err
!= PTHREAD_BARRIER_SERIAL_THREAD
)
121 printf ("barrier_wait returned %d\n", err
);
125 puts ("child: got mutex; waiting");
127 pthread_cleanup_push (ch
, NULL
);
131 (void) gettimeofday (&tv
, NULL
);
132 /* +1000 seconds in correct format. */
134 TIMEVAL_TO_TIMESPEC (&tv
, &ts
);
137 pthread_cond_timedwait (&cond
, &mut
, &ts
);
139 pthread_cleanup_pop (0);
141 puts ("child: cond_wait should not have returned");
153 printf ("&cond = %p\n&mut = %p\n", &cond
, &mut
);
155 puts ("parent: get mutex");
157 err
= pthread_barrier_init (&bar
, NULL
, 2);
160 puts ("parent: cannot init barrier");
164 puts ("parent: create child");
166 err
= pthread_create (&th
, NULL
, tf1
, NULL
);
169 puts ("parent: cannot create thread");
173 puts ("parent: wait for child to lock mutex");
175 err
= pthread_barrier_wait (&bar
);
176 if (err
!= 0 && err
!= PTHREAD_BARRIER_SERIAL_THREAD
)
178 puts ("parent: cannot wait for barrier");
182 err
= pthread_mutex_lock (&mut
);
185 puts ("parent: mutex_lock failed");
189 err
= pthread_mutex_unlock (&mut
);
192 puts ("parent: mutex_unlock failed");
196 if (pthread_cancel (th
) != 0)
198 puts ("cannot cancel thread");
203 err
= pthread_join (th
, &r
);
206 puts ("parent: failed to join");
210 if (r
!= PTHREAD_CANCELED
)
212 puts ("child hasn't been canceled");
218 puts ("parent: create 2nd child");
220 err
= pthread_create (&th
, NULL
, tf2
, NULL
);
223 puts ("parent: cannot create thread");
227 puts ("parent: wait for child to lock mutex");
229 err
= pthread_barrier_wait (&bar
);
230 if (err
!= 0 && err
!= PTHREAD_BARRIER_SERIAL_THREAD
)
232 puts ("parent: cannot wait for barrier");
236 err
= pthread_mutex_lock (&mut
);
239 puts ("parent: mutex_lock failed");
243 err
= pthread_mutex_unlock (&mut
);
246 puts ("parent: mutex_unlock failed");
250 if (pthread_cancel (th
) != 0)
252 puts ("cannot cancel thread");
256 err
= pthread_join (th
, &r
);
259 puts ("parent: failed to join");
263 if (r
!= PTHREAD_CANCELED
)
265 puts ("child hasn't been canceled");
275 #define TEST_FUNCTION do_test ()
276 #include "../test-skeleton.c"