.
[glibc/history.git] / nptl / tst-barrier3.c
blobb5478f8277b81ed56122f96579a40354b8c63b29
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
18 02111-1307 USA. */
20 /* Test of POSIX barriers. */
22 #include <pthread.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
27 #define NTHREADS 20
29 #define ROUNDS 20
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];
37 static void *
38 worker (void *arg)
40 void *result = NULL;
41 int nr = (long int) arg;
42 int i;
44 for (i = 0; i < ROUNDS; ++i)
46 int j;
47 int retval;
49 if (nr == 0)
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);
59 result = (void *) 1;
62 for (j = nr; j < NTHREADS; ++j)
64 /* Increment the counter for this round. */
65 pthread_mutex_lock (&lock);
66 ++counters[j];
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",
76 j, counters[j]);
77 result = (void *) 1;
80 if (retval != 0)
82 if (retval != PTHREAD_BARRIER_SERIAL_THREAD)
84 printf ("thread %d in round %d has nonzero return value != PTHREAD_BARRIER_SERIAL_THREAD\n",
85 nr, j);
86 result = (void *) 1;
88 else
90 pthread_mutex_lock (&lock);
91 ++serial[j];
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);
103 result = (void *) 1;
108 return result;
112 #define TEST_FUNCTION do_test ()
113 #define TIMEOUT 60
114 static int
115 do_test (void)
117 pthread_t threads[NTHREADS];
118 int i;
119 void *res;
120 int result = 0;
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);
127 exit (1);
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);
135 exit (1);
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);
143 result = 1;
145 else
146 printf ("joined threads %d\n", i);
148 if (result == 0)
149 puts ("all OK");
151 return result;
154 #include "../test-skeleton.c"