source: vendor/glibc-tests/glibc/nptl/tst-barrier3.c

Last change on this file was 2036, checked in by bird, 20 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 3.7 KB
Line 
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.
4
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.
9
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.
14
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. */
19
20/* Test of POSIX barriers. */
21
22#include <pthread.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26
27#define NTHREADS 20
28
29#define ROUNDS 20
30
31static pthread_barrier_t barriers[NTHREADS];
32
33static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
34static int counters[NTHREADS];
35static int serial[NTHREADS];
36
37static void *
38worker (void *arg)
39{
40 void *result = NULL;
41 int nr = (long int) arg;
42 int i;
43
44 for (i = 0; i < ROUNDS; ++i)
45 {
46 int j;
47 int retval;
48
49 if (nr == 0)
50 {
51 memset (counters, '\0', sizeof (counters));
52 memset (serial, '\0', sizeof (serial));
53 }
54
55 retval = pthread_barrier_wait (&barriers[NTHREADS - 1]);
56 if (retval != 0 && retval != PTHREAD_BARRIER_SERIAL_THREAD)
57 {
58 printf ("thread %d failed to wait for all the others\n", nr);
59 result = (void *) 1;
60 }
61
62 for (j = nr; j < NTHREADS; ++j)
63 {
64 /* Increment the counter for this round. */
65 pthread_mutex_lock (&lock);
66 ++counters[j];
67 pthread_mutex_unlock (&lock);
68
69 /* Wait for the rest. */
70 retval = pthread_barrier_wait (&barriers[j]);
71
72 /* Test the result. */
73 if (nr == 0 && counters[j] != j + 1)
74 {
75 printf ("barrier in round %d released but count is %d\n",
76 j, counters[j]);
77 result = (void *) 1;
78 }
79
80 if (retval != 0)
81 {
82 if (retval != PTHREAD_BARRIER_SERIAL_THREAD)
83 {
84 printf ("thread %d in round %d has nonzero return value != PTHREAD_BARRIER_SERIAL_THREAD\n",
85 nr, j);
86 result = (void *) 1;
87 }
88 else
89 {
90 pthread_mutex_lock (&lock);
91 ++serial[j];
92 pthread_mutex_unlock (&lock);
93 }
94 }
95
96 /* Wait for the rest again. */
97 retval = pthread_barrier_wait (&barriers[j]);
98
99 /* Now we can check whether exactly one thread was serializing. */
100 if (nr == 0 && serial[j] != 1)
101 {
102 printf ("not exactly one serial thread in round %d\n", j);
103 result = (void *) 1;
104 }
105 }
106 }
107
108 return result;
109}
110
111
112#define TEST_FUNCTION do_test ()
113#define TIMEOUT 60
114static int
115do_test (void)
116{
117 pthread_t threads[NTHREADS];
118 int i;
119 void *res;
120 int result = 0;
121
122 /* Initialized the barrier variables. */
123 for (i = 0; i < NTHREADS; ++i)
124 if (pthread_barrier_init (&barriers[i], NULL, i + 1) != 0)
125 {
126 printf ("Failed to initialize barrier %d\n", i);
127 exit (1);
128 }
129
130 /* Start the threads. */
131 for (i = 0; i < NTHREADS; ++i)
132 if (pthread_create (&threads[i], NULL, worker, (void *) (long int) i) != 0)
133 {
134 printf ("Failed to start thread %d\n", i);
135 exit (1);
136 }
137
138 /* And wait for them. */
139 for (i = 0; i < NTHREADS; ++i)
140 if (pthread_join (threads[i], &res) != 0 || res != NULL)
141 {
142 printf ("thread %d returned a failure\n", i);
143 result = 1;
144 }
145 else
146 printf ("joined threads %d\n", i);
147
148 if (result == 0)
149 puts ("all OK");
150
151 return result;
152}
153
154#include "../test-skeleton.c"
Note: See TracBrowser for help on using the repository browser.