Add and use new glibc-internal futex API.
[glibc.git] / nptl / tst-cond8.c
blob9f8f03d421974149e7f535c5ef09b9d4c7339a94
1 /* Copyright (C) 2003-2015 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/>. */
19 #include <errno.h>
20 #include <pthread.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <time.h>
24 #include <sys/time.h>
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;
33 static void
34 ch (void *arg)
36 int e = pthread_mutex_lock (&mut);
37 if (e == 0)
39 puts ("mutex not locked at all by cond_wait");
40 exit (1);
43 if (e != EDEADLK)
45 puts ("no deadlock error signaled");
46 exit (1);
49 if (pthread_mutex_unlock (&mut) != 0)
51 puts ("ch: cannot unlock mutex");
52 exit (1);
55 puts ("ch done");
59 static void *
60 tf1 (void *p)
62 int err;
64 if (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL) != 0
65 || pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, NULL) != 0)
67 puts ("cannot set cancellation options");
68 exit (1);
71 err = pthread_mutex_lock (&mut);
72 if (err != 0)
74 puts ("child: cannot get mutex");
75 exit (1);
78 err = pthread_barrier_wait (&bar);
79 if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
81 printf ("barrier_wait returned %d\n", err);
82 exit (1);
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");
95 return NULL;
99 static void *
100 tf2 (void *p)
102 int err;
104 if (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL) != 0
105 || pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, NULL) != 0)
107 puts ("cannot set cancellation options");
108 exit (1);
111 err = pthread_mutex_lock (&mut);
112 if (err != 0)
114 puts ("child: cannot get mutex");
115 exit (1);
118 err = pthread_barrier_wait (&bar);
119 if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
121 printf ("barrier_wait returned %d\n", err);
122 exit (1);
125 puts ("child: got mutex; waiting");
127 pthread_cleanup_push (ch, NULL);
129 /* Current time. */
130 struct timeval tv;
131 (void) gettimeofday (&tv, NULL);
132 /* +1000 seconds in correct format. */
133 struct timespec ts;
134 TIMEVAL_TO_TIMESPEC (&tv, &ts);
135 ts.tv_sec += 1000;
137 pthread_cond_timedwait (&cond, &mut, &ts);
139 pthread_cleanup_pop (0);
141 puts ("child: cond_wait should not have returned");
143 return NULL;
147 static int
148 do_test (void)
150 pthread_t th;
151 int err;
153 printf ("&cond = %p\n&mut = %p\n", &cond, &mut);
155 puts ("parent: get mutex");
157 err = pthread_barrier_init (&bar, NULL, 2);
158 if (err != 0)
160 puts ("parent: cannot init barrier");
161 exit (1);
164 puts ("parent: create child");
166 err = pthread_create (&th, NULL, tf1, NULL);
167 if (err != 0)
169 puts ("parent: cannot create thread");
170 exit (1);
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");
179 exit (1);
182 err = pthread_mutex_lock (&mut);
183 if (err != 0)
185 puts ("parent: mutex_lock failed");
186 exit (1);
189 err = pthread_mutex_unlock (&mut);
190 if (err != 0)
192 puts ("parent: mutex_unlock failed");
193 exit (1);
196 if (pthread_cancel (th) != 0)
198 puts ("cannot cancel thread");
199 exit (1);
202 void *r;
203 err = pthread_join (th, &r);
204 if (err != 0)
206 puts ("parent: failed to join");
207 exit (1);
210 if (r != PTHREAD_CANCELED)
212 puts ("child hasn't been canceled");
213 exit (1);
218 puts ("parent: create 2nd child");
220 err = pthread_create (&th, NULL, tf2, NULL);
221 if (err != 0)
223 puts ("parent: cannot create thread");
224 exit (1);
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");
233 exit (1);
236 err = pthread_mutex_lock (&mut);
237 if (err != 0)
239 puts ("parent: mutex_lock failed");
240 exit (1);
243 err = pthread_mutex_unlock (&mut);
244 if (err != 0)
246 puts ("parent: mutex_unlock failed");
247 exit (1);
250 if (pthread_cancel (th) != 0)
252 puts ("cannot cancel thread");
253 exit (1);
256 err = pthread_join (th, &r);
257 if (err != 0)
259 puts ("parent: failed to join");
260 exit (1);
263 if (r != PTHREAD_CANCELED)
265 puts ("child hasn't been canceled");
266 exit (1);
269 puts ("done");
271 return 0;
275 #define TEST_FUNCTION do_test ()
276 #include "../test-skeleton.c"