pthread_barrier.c revision 1.13.2.2 1 1.13.2.2 ad /* $NetBSD: pthread_barrier.c,v 1.13.2.2 2007/08/04 13:37:50 ad Exp $ */
2 1.13.2.2 ad
3 1.13.2.2 ad /*-
4 1.13.2.2 ad * Copyright (c) 2001, 2003, 2006, 2007 The NetBSD Foundation, Inc.
5 1.13.2.2 ad * All rights reserved.
6 1.13.2.2 ad *
7 1.13.2.2 ad * This code is derived from software contributed to The NetBSD Foundation
8 1.13.2.2 ad * by Nathan J. Williams, by Jason R. Thorpe, and by Andrew Doran.
9 1.13.2.2 ad *
10 1.13.2.2 ad * Redistribution and use in source and binary forms, with or without
11 1.13.2.2 ad * modification, are permitted provided that the following conditions
12 1.13.2.2 ad * are met:
13 1.13.2.2 ad * 1. Redistributions of source code must retain the above copyright
14 1.13.2.2 ad * notice, this list of conditions and the following disclaimer.
15 1.13.2.2 ad * 2. Redistributions in binary form must reproduce the above copyright
16 1.13.2.2 ad * notice, this list of conditions and the following disclaimer in the
17 1.13.2.2 ad * documentation and/or other materials provided with the distribution.
18 1.13.2.2 ad * 3. All advertising materials mentioning features or use of this software
19 1.13.2.2 ad * must display the following acknowledgement:
20 1.13.2.2 ad * This product includes software developed by the NetBSD
21 1.13.2.2 ad * Foundation, Inc. and its contributors.
22 1.13.2.2 ad * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.13.2.2 ad * contributors may be used to endorse or promote products derived
24 1.13.2.2 ad * from this software without specific prior written permission.
25 1.13.2.2 ad *
26 1.13.2.2 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.13.2.2 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.13.2.2 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.13.2.2 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.13.2.2 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.13.2.2 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.13.2.2 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.13.2.2 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.13.2.2 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.13.2.2 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.13.2.2 ad * POSSIBILITY OF SUCH DAMAGE.
37 1.13.2.2 ad */
38 1.13.2.2 ad
39 1.13.2.2 ad #include <sys/cdefs.h>
40 1.13.2.2 ad __RCSID("$NetBSD: pthread_barrier.c,v 1.13.2.2 2007/08/04 13:37:50 ad Exp $");
41 1.13.2.2 ad
42 1.13.2.2 ad #include <errno.h>
43 1.13.2.2 ad
44 1.13.2.2 ad #include "pthread.h"
45 1.13.2.2 ad #include "pthread_int.h"
46 1.13.2.2 ad
47 1.13.2.2 ad #undef PTHREAD_BARRIER_DEBUG
48 1.13.2.2 ad
49 1.13.2.2 ad #ifdef PTHREAD_BARRIER_DEBUG
50 1.13.2.2 ad #define SDPRINTF(x) DPRINTF(x)
51 1.13.2.2 ad #else
52 1.13.2.2 ad #define SDPRINTF(x)
53 1.13.2.2 ad #endif
54 1.13.2.2 ad
55 1.13.2.2 ad int
56 1.13.2.2 ad pthread_barrier_init(pthread_barrier_t *barrier,
57 1.13.2.2 ad const pthread_barrierattr_t *attr, unsigned int count)
58 1.13.2.2 ad {
59 1.13.2.2 ad pthread_t self;
60 1.13.2.2 ad
61 1.13.2.2 ad #ifdef ERRORCHECK
62 1.13.2.2 ad if ((barrier == NULL) ||
63 1.13.2.2 ad (attr && (attr->ptba_magic != _PT_BARRIERATTR_MAGIC)))
64 1.13.2.2 ad return EINVAL;
65 1.13.2.2 ad #endif
66 1.13.2.2 ad
67 1.13.2.2 ad if (count == 0)
68 1.13.2.2 ad return EINVAL;
69 1.13.2.2 ad
70 1.13.2.2 ad self = pthread__self();
71 1.13.2.2 ad
72 1.13.2.2 ad if (barrier->ptb_magic == _PT_BARRIER_MAGIC) {
73 1.13.2.2 ad /*
74 1.13.2.2 ad * We're simply reinitializing the barrier to a
75 1.13.2.2 ad * new count.
76 1.13.2.2 ad */
77 1.13.2.2 ad pthread_spinlock(self, &barrier->ptb_lock);
78 1.13.2.2 ad
79 1.13.2.2 ad if (barrier->ptb_magic != _PT_BARRIER_MAGIC) {
80 1.13.2.2 ad pthread_spinunlock(self, &barrier->ptb_lock);
81 1.13.2.2 ad return EINVAL;
82 1.13.2.2 ad }
83 1.13.2.2 ad
84 1.13.2.2 ad if (!PTQ_EMPTY(&barrier->ptb_waiters)) {
85 1.13.2.2 ad pthread_spinunlock(self, &barrier->ptb_lock);
86 1.13.2.2 ad return EBUSY;
87 1.13.2.2 ad }
88 1.13.2.2 ad
89 1.13.2.2 ad barrier->ptb_initcount = count;
90 1.13.2.2 ad barrier->ptb_curcount = 0;
91 1.13.2.2 ad barrier->ptb_generation = 0;
92 1.13.2.2 ad
93 1.13.2.2 ad pthread_spinunlock(self, &barrier->ptb_lock);
94 1.13.2.2 ad
95 1.13.2.2 ad return 0;
96 1.13.2.2 ad }
97 1.13.2.2 ad
98 1.13.2.2 ad barrier->ptb_magic = _PT_BARRIER_MAGIC;
99 1.13.2.2 ad pthread_lockinit(&barrier->ptb_lock);
100 1.13.2.2 ad PTQ_INIT(&barrier->ptb_waiters);
101 1.13.2.2 ad barrier->ptb_initcount = count;
102 1.13.2.2 ad barrier->ptb_curcount = 0;
103 1.13.2.2 ad barrier->ptb_generation = 0;
104 1.13.2.2 ad
105 1.13.2.2 ad return 0;
106 1.13.2.2 ad }
107 1.13.2.2 ad
108 1.13.2.2 ad
109 1.13.2.2 ad int
110 1.13.2.2 ad pthread_barrier_destroy(pthread_barrier_t *barrier)
111 1.13.2.2 ad {
112 1.13.2.2 ad pthread_t self;
113 1.13.2.2 ad
114 1.13.2.2 ad #ifdef ERRORCHECK
115 1.13.2.2 ad if ((barrier == NULL) || (barrier->ptb_magic != _PT_BARRIER_MAGIC))
116 1.13.2.2 ad return EINVAL;
117 1.13.2.2 ad #endif
118 1.13.2.2 ad
119 1.13.2.2 ad self = pthread__self();
120 1.13.2.2 ad
121 1.13.2.2 ad pthread_spinlock(self, &barrier->ptb_lock);
122 1.13.2.2 ad
123 1.13.2.2 ad if (barrier->ptb_magic != _PT_BARRIER_MAGIC) {
124 1.13.2.2 ad pthread_spinunlock(self, &barrier->ptb_lock);
125 1.13.2.2 ad return EINVAL;
126 1.13.2.2 ad }
127 1.13.2.2 ad
128 1.13.2.2 ad if (!PTQ_EMPTY(&barrier->ptb_waiters)) {
129 1.13.2.2 ad pthread_spinunlock(self, &barrier->ptb_lock);
130 1.13.2.2 ad return EBUSY;
131 1.13.2.2 ad }
132 1.13.2.2 ad
133 1.13.2.2 ad barrier->ptb_magic = _PT_BARRIER_DEAD;
134 1.13.2.2 ad
135 1.13.2.2 ad pthread_spinunlock(self, &barrier->ptb_lock);
136 1.13.2.2 ad
137 1.13.2.2 ad return 0;
138 1.13.2.2 ad }
139 1.13.2.2 ad
140 1.13.2.2 ad
141 1.13.2.2 ad int
142 1.13.2.2 ad pthread_barrier_wait(pthread_barrier_t *barrier)
143 1.13.2.2 ad {
144 1.13.2.2 ad pthread_t self;
145 1.13.2.2 ad unsigned int gen;
146 1.13.2.2 ad
147 1.13.2.2 ad #ifdef ERRORCHECK
148 1.13.2.2 ad if ((barrier == NULL) || (barrier->ptb_magic != _PT_BARRIER_MAGIC))
149 1.13.2.2 ad return EINVAL;
150 1.13.2.2 ad #endif
151 1.13.2.2 ad self = pthread__self();
152 1.13.2.2 ad
153 1.13.2.2 ad pthread_spinlock(self, &barrier->ptb_lock);
154 1.13.2.2 ad
155 1.13.2.2 ad /*
156 1.13.2.2 ad * A single arbitrary thread is supposed to return
157 1.13.2.2 ad * PTHREAD_BARRIER_SERIAL_THREAD, and everone else
158 1.13.2.2 ad * is supposed to return 0. Since pthread_barrier_wait()
159 1.13.2.2 ad * is not a cancellation point, this is trivial; we
160 1.13.2.2 ad * simply elect that the thread that causes the barrier
161 1.13.2.2 ad * to be satisfied gets the special return value. Note
162 1.13.2.2 ad * that this final thread does not actually need to block,
163 1.13.2.2 ad * but instead is responsible for waking everyone else up.
164 1.13.2.2 ad */
165 1.13.2.2 ad if (barrier->ptb_curcount + 1 == barrier->ptb_initcount) {
166 1.13.2.2 ad SDPRINTF(("(barrier wait %p) Satisfied %p\n",
167 1.13.2.2 ad self, barrier));
168 1.13.2.2 ad
169 1.13.2.2 ad barrier->ptb_generation++;
170 1.13.2.2 ad pthread__unpark_all(self, &barrier->ptb_lock,
171 1.13.2.2 ad &barrier->ptb_waiters);
172 1.13.2.2 ad return PTHREAD_BARRIER_SERIAL_THREAD;
173 1.13.2.2 ad }
174 1.13.2.2 ad
175 1.13.2.2 ad barrier->ptb_curcount++;
176 1.13.2.2 ad gen = barrier->ptb_generation;
177 1.13.2.2 ad while (gen == barrier->ptb_generation) {
178 1.13.2.2 ad SDPRINTF(("(barrier wait %p) Waiting on %p\n",
179 1.13.2.2 ad self, barrier));
180 1.13.2.2 ad PTQ_INSERT_TAIL(&barrier->ptb_waiters, self, pt_sleep);
181 1.13.2.2 ad self->pt_sleeponq = 1;
182 1.13.2.2 ad self->pt_sleepobj = &barrier->ptb_waiters;
183 1.13.2.2 ad pthread_spinunlock(self, &barrier->ptb_lock);
184 1.13.2.2 ad (void)pthread__park(self, &barrier->ptb_lock,
185 1.13.2.2 ad &barrier->ptb_waiters, NULL, 0,
186 1.13.2.2 ad &barrier->ptb_waiters);
187 1.13.2.2 ad SDPRINTF(("(barrier wait %p) Woke up on %p\n",
188 1.13.2.2 ad self, barrier));
189 1.13.2.2 ad pthread_spinlock(self, &barrier->ptb_lock);
190 1.13.2.2 ad }
191 1.13.2.2 ad pthread_spinunlock(self, &barrier->ptb_lock);
192 1.13.2.2 ad
193 1.13.2.2 ad return 0;
194 1.13.2.2 ad }
195 1.13.2.2 ad
196 1.13.2.2 ad
197 1.13.2.2 ad int
198 1.13.2.2 ad pthread_barrierattr_init(pthread_barrierattr_t *attr)
199 1.13.2.2 ad {
200 1.13.2.2 ad
201 1.13.2.2 ad #ifdef ERRORCHECK
202 1.13.2.2 ad if (attr == NULL)
203 1.13.2.2 ad return EINVAL;
204 1.13.2.2 ad #endif
205 1.13.2.2 ad
206 1.13.2.2 ad attr->ptba_magic = _PT_BARRIERATTR_MAGIC;
207 1.13.2.2 ad
208 1.13.2.2 ad return 0;
209 1.13.2.2 ad }
210 1.13.2.2 ad
211 1.13.2.2 ad
212 1.13.2.2 ad int
213 1.13.2.2 ad pthread_barrierattr_destroy(pthread_barrierattr_t *attr)
214 1.13.2.2 ad {
215 1.13.2.2 ad
216 1.13.2.2 ad #ifdef ERRORCHECK
217 1.13.2.2 ad if ((attr == NULL) ||
218 1.13.2.2 ad (attr->ptba_magic != _PT_BARRIERATTR_MAGIC))
219 1.13.2.2 ad return EINVAL;
220 1.13.2.2 ad #endif
221 1.13.2.2 ad
222 1.13.2.2 ad attr->ptba_magic = _PT_BARRIERATTR_DEAD;
223 1.13.2.2 ad
224 1.13.2.2 ad return 0;
225 1.13.2.2 ad }
226