S390: Fix sem.h conformance test failures.
[glibc.git] / nptl / tst-barrier3.c
blob4fb8b6a5766d2d794805fb5e0fffe8fc5e7f876b
1 /* Copyright (C) 2002-2015 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. */
21 #include <pthread.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
26 #define NTHREADS 20
28 #define ROUNDS 20
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];
36 static void *
37 worker (void *arg)
39 void *result = NULL;
40 int nr = (long int) arg;
41 int i;
43 for (i = 0; i < ROUNDS; ++i)
45 int j;
46 int retval;
48 if (nr == 0)
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);
58 result = (void *) 1;
61 for (j = nr; j < NTHREADS; ++j)
63 /* Increment the counter for this round. */
64 pthread_mutex_lock (&lock);
65 ++counters[j];
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",
75 j, counters[j]);
76 result = (void *) 1;
79 if (retval != 0)
81 if (retval != PTHREAD_BARRIER_SERIAL_THREAD)
83 printf ("thread %d in round %d has nonzero return value != PTHREAD_BARRIER_SERIAL_THREAD\n",
84 nr, j);
85 result = (void *) 1;
87 else
89 pthread_mutex_lock (&lock);
90 ++serial[j];
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);
102 result = (void *) 1;
107 return result;
111 #define TEST_FUNCTION do_test ()
112 #define TIMEOUT 60
113 static int
114 do_test (void)
116 pthread_t threads[NTHREADS];
117 int i;
118 void *res;
119 int result = 0;
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);
126 exit (1);
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);
134 exit (1);
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);
142 result = 1;
144 else
145 printf ("joined threads %d\n", i);
147 if (result == 0)
148 puts ("all OK");
150 return result;
153 #include "../test-skeleton.c"