1 /* Copyright (C) 2004 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, 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_cond_t cond2
= PTHREAD_COND_INITIALIZER
;
30 static pthread_mutex_t mut
= PTHREAD_MUTEX_INITIALIZER
;
31 static pthread_barrier_t b
;
38 for (i
= 0; i
< ROUNDS
; ++i
)
40 pthread_mutex_lock (&mut
);
43 pthread_cond_signal (&cond2
);
47 gettimeofday (&tv
, NULL
);
49 /* Wait three seconds. */
50 ts
.tv_sec
= tv
.tv_sec
+ 3;
51 ts
.tv_nsec
= tv
.tv_usec
* 1000;
52 pthread_cond_timedwait (&cond
, &mut
, &ts
);
54 pthread_cond_wait (&cond
, &mut
);
57 pthread_mutex_unlock (&mut
);
59 int err
= pthread_barrier_wait (&b
);
60 if (err
!= 0 && err
!= PTHREAD_BARRIER_SERIAL_THREAD
)
62 puts ("child: barrier_wait failed");
66 err
= pthread_barrier_wait (&b
);
67 if (err
!= 0 && err
!= PTHREAD_BARRIER_SERIAL_THREAD
)
69 puts ("child: barrier_wait failed");
81 if (pthread_barrier_init (&b
, NULL
, N
+ 1) != 0)
83 puts ("barrier_init failed");
87 pthread_mutex_lock (&mut
);
91 for (i
= 0; i
< N
; ++i
)
92 if ((err
= pthread_create (&th
[i
], NULL
, tf
, NULL
)) != 0)
94 printf ("cannot create thread %d: %s\n", i
, strerror (err
));
98 for (i
= 0; i
< ROUNDS
; ++i
)
100 pthread_cond_wait (&cond2
, &mut
);
103 pthread_mutex_unlock (&mut
);
106 pthread_cond_broadcast (&cond
);
108 for (j
= 0; j
< N
; ++j
)
109 pthread_cond_signal (&cond
);
112 for (j
= 0; j
< (i
/ 8) % N
; ++j
)
113 pthread_cond_signal (&cond
);
114 pthread_cond_broadcast (&cond
);
118 pthread_mutex_unlock (&mut
);
120 err
= pthread_cond_destroy (&cond
);
123 printf ("pthread_cond_destroy failed: %s\n", strerror (err
));
127 /* Now clobber the cond variable which has been successfully
129 memset (&cond
, (char) i
, sizeof (cond
));
131 err
= pthread_barrier_wait (&b
);
132 if (err
!= 0 && err
!= PTHREAD_BARRIER_SERIAL_THREAD
)
134 puts ("parent: barrier_wait failed");
138 pthread_mutex_lock (&mut
);
140 err
= pthread_barrier_wait (&b
);
141 if (err
!= 0 && err
!= PTHREAD_BARRIER_SERIAL_THREAD
)
143 puts ("parent: barrier_wait failed");
148 err
= pthread_cond_init (&cond
, NULL
);
151 printf ("pthread_cond_init failed: %s\n", strerror (err
));
156 for (i
= 0; i
< N
; ++i
)
157 if ((err
= pthread_join (th
[i
], NULL
)) != 0)
159 printf ("failed to join thread %d: %s\n", i
, strerror (err
));
169 #define TEST_FUNCTION do_test ()
170 #include "../test-skeleton.c"