Warning: That file was not part of the compilation database. It may have many parsing errors.

1/* Test of POSIX barriers.
2 Copyright (C) 2002-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
19
20#include <pthread.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24
25#define NTHREADS 20
26
27#define ROUNDS 20
28
29static pthread_barrier_t barriers[NTHREADS];
30
31static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
32static int counters[NTHREADS];
33static int serial[NTHREADS];
34
35static void *
36worker (void *arg)
37{
38 void *result = NULL;
39 int nr = (long int) arg;
40 int i;
41
42 for (i = 0; i < ROUNDS; ++i)
43 {
44 int j;
45 int retval;
46
47 if (nr == 0)
48 {
49 memset (counters, '\0', sizeof (counters));
50 memset (serial, '\0', sizeof (serial));
51 }
52
53 retval = pthread_barrier_wait (&barriers[NTHREADS - 1]);
54 if (retval != 0 && retval != PTHREAD_BARRIER_SERIAL_THREAD)
55 {
56 printf ("thread %d failed to wait for all the others\n", nr);
57 result = (void *) 1;
58 }
59
60 for (j = nr; j < NTHREADS; ++j)
61 {
62 /* Increment the counter for this round. */
63 pthread_mutex_lock (&lock);
64 ++counters[j];
65 pthread_mutex_unlock (&lock);
66
67 /* Wait for the rest. */
68 retval = pthread_barrier_wait (&barriers[j]);
69
70 /* Test the result. */
71 if (nr == 0 && counters[j] != j + 1)
72 {
73 printf ("barrier in round %d released but count is %d\n",
74 j, counters[j]);
75 result = (void *) 1;
76 }
77
78 if (retval != 0)
79 {
80 if (retval != PTHREAD_BARRIER_SERIAL_THREAD)
81 {
82 printf ("thread %d in round %d has nonzero return value != PTHREAD_BARRIER_SERIAL_THREAD\n",
83 nr, j);
84 result = (void *) 1;
85 }
86 else
87 {
88 pthread_mutex_lock (&lock);
89 ++serial[j];
90 pthread_mutex_unlock (&lock);
91 }
92 }
93
94 /* Wait for the rest again. */
95 retval = pthread_barrier_wait (&barriers[j]);
96
97 /* Now we can check whether exactly one thread was serializing. */
98 if (nr == 0 && serial[j] != 1)
99 {
100 printf ("not exactly one serial thread in round %d\n", j);
101 result = (void *) 1;
102 }
103 }
104 }
105
106 return result;
107}
108
109
110#define TEST_FUNCTION do_test ()
111#define TIMEOUT 60
112static int
113do_test (void)
114{
115 pthread_t threads[NTHREADS];
116 int i;
117 void *res;
118 int result = 0;
119
120 /* Initialized the barrier variables. */
121 for (i = 0; i < NTHREADS; ++i)
122 if (pthread_barrier_init (&barriers[i], NULL, i + 1) != 0)
123 {
124 printf ("Failed to initialize barrier %d\n", i);
125 exit (1);
126 }
127
128 /* Start the threads. */
129 for (i = 0; i < NTHREADS; ++i)
130 if (pthread_create (&threads[i], NULL, worker, (void *) (long int) i) != 0)
131 {
132 printf ("Failed to start thread %d\n", i);
133 exit (1);
134 }
135
136 /* And wait for them. */
137 for (i = 0; i < NTHREADS; ++i)
138 if (pthread_join (threads[i], &res) != 0 || res != NULL)
139 {
140 printf ("thread %d returned a failure\n", i);
141 result = 1;
142 }
143 else
144 printf ("joined threads %d\n", i);
145
146 if (result == 0)
147 puts ("all OK");
148
149 return result;
150}
151
152#include "../test-skeleton.c"
153

Warning: That file was not part of the compilation database. It may have many parsing errors.