1 /* Copyright (C) 2004-2022 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
26 static pthread_cond_t cond
= PTHREAD_COND_INITIALIZER
;
27 static pthread_cond_t cond2
= PTHREAD_COND_INITIALIZER
;
28 static pthread_mutex_t mut
= PTHREAD_MUTEX_INITIALIZER
;
29 static pthread_barrier_t b
;
36 for (i
= 0; i
< ROUNDS
; ++i
)
38 pthread_mutex_lock (&mut
);
41 pthread_cond_signal (&cond2
);
45 gettimeofday (&tv
, NULL
);
47 /* Wait three seconds. */
48 ts
.tv_sec
= tv
.tv_sec
+ 3;
49 ts
.tv_nsec
= tv
.tv_usec
* 1000;
50 pthread_cond_timedwait (&cond
, &mut
, &ts
);
52 pthread_cond_wait (&cond
, &mut
);
55 pthread_mutex_unlock (&mut
);
57 int err
= pthread_barrier_wait (&b
);
58 if (err
!= 0 && err
!= PTHREAD_BARRIER_SERIAL_THREAD
)
60 puts ("child: barrier_wait failed");
64 err
= pthread_barrier_wait (&b
);
65 if (err
!= 0 && err
!= PTHREAD_BARRIER_SERIAL_THREAD
)
67 puts ("child: barrier_wait failed");
79 if (pthread_barrier_init (&b
, NULL
, N
+ 1) != 0)
81 puts ("barrier_init failed");
85 pthread_mutex_lock (&mut
);
89 for (i
= 0; i
< N
; ++i
)
90 if ((err
= pthread_create (&th
[i
], NULL
, tf
, NULL
)) != 0)
92 printf ("cannot create thread %d: %s\n", i
, strerror (err
));
96 for (i
= 0; i
< ROUNDS
; ++i
)
98 /* Make sure we discard spurious wake-ups. */
100 pthread_cond_wait (&cond2
, &mut
);
104 pthread_mutex_unlock (&mut
);
107 pthread_cond_broadcast (&cond
);
109 for (j
= 0; j
< N
; ++j
)
110 pthread_cond_signal (&cond
);
113 for (j
= 0; j
< (i
/ 8) % N
; ++j
)
114 pthread_cond_signal (&cond
);
115 pthread_cond_broadcast (&cond
);
119 pthread_mutex_unlock (&mut
);
121 err
= pthread_cond_destroy (&cond
);
124 printf ("pthread_cond_destroy failed: %s\n", strerror (err
));
128 /* Now clobber the cond variable which has been successfully
130 memset (&cond
, (char) i
, sizeof (cond
));
132 err
= pthread_barrier_wait (&b
);
133 if (err
!= 0 && err
!= PTHREAD_BARRIER_SERIAL_THREAD
)
135 puts ("parent: barrier_wait failed");
139 pthread_mutex_lock (&mut
);
141 err
= pthread_barrier_wait (&b
);
142 if (err
!= 0 && err
!= PTHREAD_BARRIER_SERIAL_THREAD
)
144 puts ("parent: barrier_wait failed");
149 err
= pthread_cond_init (&cond
, NULL
);
152 printf ("pthread_cond_init failed: %s\n", strerror (err
));
157 for (i
= 0; i
< N
; ++i
)
158 if ((err
= pthread_join (th
[i
], NULL
)) != 0)
160 printf ("failed to join thread %d: %s\n", i
, strerror (err
));
170 #define TEST_FUNCTION do_test ()
171 #include "../test-skeleton.c"