1 /* Copyright (C) 2004-2018 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Jakub Jelinek <jakub@redhat.com>, 2004.
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_cond_t cond2
= PTHREAD_COND_INITIALIZER
;
29 static pthread_mutex_t mut
= PTHREAD_MUTEX_INITIALIZER
;
30 static pthread_barrier_t b
;
37 for (i
= 0; i
< ROUNDS
; ++i
)
39 pthread_mutex_lock (&mut
);
42 pthread_cond_signal (&cond2
);
46 gettimeofday (&tv
, NULL
);
48 /* Wait three seconds. */
49 ts
.tv_sec
= tv
.tv_sec
+ 3;
50 ts
.tv_nsec
= tv
.tv_usec
* 1000;
51 pthread_cond_timedwait (&cond
, &mut
, &ts
);
53 pthread_cond_wait (&cond
, &mut
);
56 pthread_mutex_unlock (&mut
);
58 int err
= pthread_barrier_wait (&b
);
59 if (err
!= 0 && err
!= PTHREAD_BARRIER_SERIAL_THREAD
)
61 puts ("child: barrier_wait failed");
65 err
= pthread_barrier_wait (&b
);
66 if (err
!= 0 && err
!= PTHREAD_BARRIER_SERIAL_THREAD
)
68 puts ("child: barrier_wait failed");
80 if (pthread_barrier_init (&b
, NULL
, N
+ 1) != 0)
82 puts ("barrier_init failed");
86 pthread_mutex_lock (&mut
);
90 for (i
= 0; i
< N
; ++i
)
91 if ((err
= pthread_create (&th
[i
], NULL
, tf
, NULL
)) != 0)
93 printf ("cannot create thread %d: %s\n", i
, strerror (err
));
97 for (i
= 0; i
< ROUNDS
; ++i
)
99 /* Make sure we discard spurious wake-ups. */
101 pthread_cond_wait (&cond2
, &mut
);
105 pthread_mutex_unlock (&mut
);
108 pthread_cond_broadcast (&cond
);
110 for (j
= 0; j
< N
; ++j
)
111 pthread_cond_signal (&cond
);
114 for (j
= 0; j
< (i
/ 8) % N
; ++j
)
115 pthread_cond_signal (&cond
);
116 pthread_cond_broadcast (&cond
);
120 pthread_mutex_unlock (&mut
);
122 err
= pthread_cond_destroy (&cond
);
125 printf ("pthread_cond_destroy failed: %s\n", strerror (err
));
129 /* Now clobber the cond variable which has been successfully
131 memset (&cond
, (char) i
, sizeof (cond
));
133 err
= pthread_barrier_wait (&b
);
134 if (err
!= 0 && err
!= PTHREAD_BARRIER_SERIAL_THREAD
)
136 puts ("parent: barrier_wait failed");
140 pthread_mutex_lock (&mut
);
142 err
= pthread_barrier_wait (&b
);
143 if (err
!= 0 && err
!= PTHREAD_BARRIER_SERIAL_THREAD
)
145 puts ("parent: barrier_wait failed");
150 err
= pthread_cond_init (&cond
, NULL
);
153 printf ("pthread_cond_init failed: %s\n", strerror (err
));
158 for (i
= 0; i
< N
; ++i
)
159 if ((err
= pthread_join (th
[i
], NULL
)) != 0)
161 printf ("failed to join thread %d: %s\n", i
, strerror (err
));
171 #define TEST_FUNCTION do_test ()
172 #include "../test-skeleton.c"