1 /* Copyright (C) 2002 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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 /* Test of POSIX barriers. */
30 static pthread_barrier_t barriers[NTHREADS];
32 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
33 static int counters[NTHREADS];
34 static int serial[NTHREADS];
40 int nr = (long int) arg;
43 for (i = 0; i < ROUNDS; ++i)
50 memset (counters, '\0', sizeof (counters));
51 memset (serial, '\0', sizeof (serial));
54 retval = pthread_barrier_wait (&barriers[NTHREADS - 1]);
55 if (retval != 0 && retval != PTHREAD_BARRIER_SERIAL_THREAD)
57 printf ("thread %d failed to wait for all the others\n", nr);
61 for (j = nr; j < NTHREADS; ++j)
63 /* Increment the counter for this round. */
64 pthread_mutex_lock (&lock);
66 pthread_mutex_unlock (&lock);
68 /* Wait for the rest. */
69 retval = pthread_barrier_wait (&barriers[j]);
71 /* Test the result. */
72 if (nr == 0 && counters[j] != j + 1)
74 printf ("barrier in round %d released but count is %d\n",
81 if (retval != PTHREAD_BARRIER_SERIAL_THREAD)
83 printf ("thread %d in round %d has nonzero return value != PTHREAD_BARRIER_SERIAL_THREAD\n",
89 pthread_mutex_lock (&lock);
91 pthread_mutex_unlock (&lock);
95 /* Wait for the rest again. */
96 retval = pthread_barrier_wait (&barriers[j]);
98 /* Now we can check whether exactly one thread was serializing. */
99 if (nr == 0 && serial[j] != 1)
101 printf ("not exactly one serial thread in round %d\n", j);
111 #define TEST_FUNCTION do_test ()
116 pthread_t threads[NTHREADS];
121 /* Initialized the barrier variables. */
122 for (i = 0; i < NTHREADS; ++i)
123 if (pthread_barrier_init (&barriers[i], NULL, i + 1) != 0)
125 printf ("Failed to initialize barrier %d\n", i);
129 /* Start the threads. */
130 for (i = 0; i < NTHREADS; ++i)
131 if (pthread_create (&threads[i], NULL, worker, (void *) (long int) i) != 0)
133 printf ("Failed to start thread %d\n", i);
137 /* And wait for them. */
138 for (i = 0; i < NTHREADS; ++i)
139 if (pthread_join (threads[i], &res) != 0 || res != NULL)
141 printf ("thread %d returned a failure\n", i);
145 printf ("joined threads %d\n", i);
153 #include "../test-skeleton.c"