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