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