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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 /* Test of POSIX barriers. */
31 static pthread_barrier_t barriers
[NTHREADS
];
33 static pthread_mutex_t lock
= PTHREAD_MUTEX_INITIALIZER
;
34 static int counters
[NTHREADS
];
35 static int serial
[NTHREADS
];
41 int nr
= (long int) arg
;
44 for (i
= 0; i
< ROUNDS
; ++i
)
51 memset (counters
, '\0', sizeof (counters
));
52 memset (serial
, '\0', sizeof (serial
));
55 retval
= pthread_barrier_wait (&barriers
[NTHREADS
- 1]);
56 if (retval
!= 0 && retval
!= PTHREAD_BARRIER_SERIAL_THREAD
)
58 printf ("thread %d failed to wait for all the others\n", nr
);
62 for (j
= nr
; j
< NTHREADS
; ++j
)
64 /* Increment the counter for this round. */
65 pthread_mutex_lock (&lock
);
67 pthread_mutex_unlock (&lock
);
69 /* Wait for the rest. */
70 retval
= pthread_barrier_wait (&barriers
[j
]);
72 /* Test the result. */
73 if (nr
== 0 && counters
[j
] != j
+ 1)
75 printf ("barrier in round %d released but count is %d\n",
82 if (retval
!= PTHREAD_BARRIER_SERIAL_THREAD
)
84 printf ("thread %d in round %d has nonzero return value != PTHREAD_BARRIER_SERIAL_THREAD\n",
90 pthread_mutex_lock (&lock
);
92 pthread_mutex_unlock (&lock
);
96 /* Wait for the rest again. */
97 retval
= pthread_barrier_wait (&barriers
[j
]);
99 /* Now we can check whether exactly one thread was serializing. */
100 if (nr
== 0 && serial
[j
] != 1)
102 printf ("not exactly one serial thread in round %d\n", j
);
112 #define TEST_FUNCTION do_test ()
117 pthread_t threads
[NTHREADS
];
122 /* Initialized the barrier variables. */
123 for (i
= 0; i
< NTHREADS
; ++i
)
124 if (pthread_barrier_init (&barriers
[i
], NULL
, i
+ 1) != 0)
126 printf ("Failed to initialize barrier %d\n", i
);
130 /* Start the threads. */
131 for (i
= 0; i
< NTHREADS
; ++i
)
132 if (pthread_create (&threads
[i
], NULL
, worker
, (void *) (long int) i
) != 0)
134 printf ("Failed to start thread %d\n", i
);
138 /* And wait for them. */
139 for (i
= 0; i
< NTHREADS
; ++i
)
140 if (pthread_join (threads
[i
], &res
) != 0 || res
!= NULL
)
142 printf ("thread %d returned a failure\n", i
);
146 printf ("joined threads %d\n", i
);
154 #include "../test-skeleton.c"