xref: /netbsd-src/lib/libpthread/pthread_barrier.c (revision 990562bfef3015a6e18a209dde598d5588ea144c)
1 /*	$NetBSD: pthread_barrier.c,v 1.5 2003/02/15 00:52:18 nathanw 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 <errno.h>
40 #include <sys/cdefs.h>
41 
42 #include "pthread.h"
43 #include "pthread_int.h"
44 
45 #undef PTHREAD_BARRIER_DEBUG
46 
47 #ifdef PTHREAD_BARRIER_DEBUG
48 #define SDPRINTF(x) DPRINTF(x)
49 #else
50 #define SDPRINTF(x)
51 #endif
52 
53 int
54 pthread_barrier_init(pthread_barrier_t *barrier,
55     const pthread_barrierattr_t *attr, unsigned int count)
56 {
57 	pthread_t self;
58 
59 #ifdef ERRORCHECK
60 	if ((barrier == NULL) ||
61 	    (attr && (attr->ptba_magic != _PT_BARRIERATTR_MAGIC)))
62 		return EINVAL;
63 #endif
64 
65 	if (count == 0)
66 		return EINVAL;
67 
68 	self = pthread__self();
69 
70 	if (barrier->ptb_magic == _PT_BARRIER_MAGIC) {
71 		/*
72 		 * We're simply reinitializing the barrier to a
73 		 * new count.
74 		 */
75 		pthread_spinlock(self, &barrier->ptb_lock);
76 
77 		if (barrier->ptb_magic != _PT_BARRIER_MAGIC) {
78 			pthread_spinunlock(self, &barrier->ptb_lock);
79 			return EINVAL;
80 		}
81 
82 		if (!PTQ_EMPTY(&barrier->ptb_waiters)) {
83 			pthread_spinunlock(self, &barrier->ptb_lock);
84 			return EBUSY;
85 		}
86 
87 		barrier->ptb_initcount = count;
88 		barrier->ptb_curcount = 0;
89 		barrier->ptb_generation = 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 	barrier->ptb_generation = 0;
102 
103 	return 0;
104 }
105 
106 
107 int
108 pthread_barrier_destroy(pthread_barrier_t *barrier)
109 {
110 	pthread_t self;
111 
112 #ifdef ERRORCHECK
113 	if ((barrier == NULL) || (barrier->ptb_magic != _PT_BARRIER_MAGIC))
114 		return EINVAL;
115 #endif
116 
117 	self = pthread__self();
118 
119 	pthread_spinlock(self, &barrier->ptb_lock);
120 
121 	if (barrier->ptb_magic != _PT_BARRIER_MAGIC) {
122 		pthread_spinunlock(self, &barrier->ptb_lock);
123 		return EINVAL;
124 	}
125 
126 	if (!PTQ_EMPTY(&barrier->ptb_waiters)) {
127 		pthread_spinunlock(self, &barrier->ptb_lock);
128 		return EBUSY;
129 	}
130 
131 	barrier->ptb_magic = _PT_BARRIER_DEAD;
132 
133 	pthread_spinunlock(self, &barrier->ptb_lock);
134 
135 	return 0;
136 }
137 
138 
139 int
140 pthread_barrier_wait(pthread_barrier_t *barrier)
141 {
142 	pthread_t self;
143 	unsigned int gen;
144 
145 #ifdef ERRORCHECK
146 	if ((barrier == NULL) || (barrier->ptb_magic != _PT_BARRIER_MAGIC))
147 		return EINVAL;
148 #endif
149 	self = pthread__self();
150 
151 	pthread_spinlock(self, &barrier->ptb_lock);
152 
153 	/*
154 	 * A single arbitrary thread is supposed to return
155 	 * PTHREAD_BARRIER_SERIAL_THREAD, and everone else
156 	 * is supposed to return 0.  Since pthread_barrier_wait()
157 	 * is not a cancellation point, this is trivial; we
158 	 * simply elect that the thread that causes the barrier
159 	 * to be satisfied gets the special return value.  Note
160 	 * that this final thread does not actually need to block,
161 	 * but instead is responsible for waking everyone else up.
162 	 */
163 	if (barrier->ptb_curcount + 1 == barrier->ptb_initcount) {
164 		struct pthread_queue_t blockedq;
165 
166 		SDPRINTF(("(barrier wait %p) Satisfied %p\n",
167 		    self, barrier));
168 
169 		blockedq = barrier->ptb_waiters;
170 		PTQ_INIT(&barrier->ptb_waiters);
171 		barrier->ptb_curcount = 0;
172 		barrier->ptb_generation++;
173 
174 		pthread__sched_sleepers(self, &blockedq);
175 
176 		pthread_spinunlock(self, &barrier->ptb_lock);
177 
178 		return PTHREAD_BARRIER_SERIAL_THREAD;
179 	}
180 
181 	barrier->ptb_curcount++;
182 	gen = barrier->ptb_generation;
183 	while (gen == barrier->ptb_generation) {
184 		SDPRINTF(("(barrier wait %p) Waiting on %p\n",
185 		    self, barrier));
186 
187 		pthread_spinlock(self, &self->pt_statelock);
188 
189 		self->pt_state = PT_STATE_BLOCKED_QUEUE;
190 		self->pt_sleepobj = barrier;
191 		self->pt_sleepq = &barrier->ptb_waiters;
192 		self->pt_sleeplock = &barrier->ptb_lock;
193 
194 		pthread_spinunlock(self, &self->pt_statelock);
195 
196 		PTQ_INSERT_TAIL(&barrier->ptb_waiters, self, pt_sleep);
197 
198 		pthread__block(self, &barrier->ptb_lock);
199 		SDPRINTF(("(barrier wait %p) Woke up on %p\n",
200 		    self, barrier));
201 		/* Spinlock is unlocked on return */
202 		pthread_spinlock(self, &barrier->ptb_lock);
203 	}
204 	pthread_spinunlock(self, &barrier->ptb_lock);
205 
206 	return 0;
207 }
208 
209 
210 int
211 pthread_barrierattr_init(pthread_barrierattr_t *attr)
212 {
213 
214 #ifdef ERRORCHECK
215 	if (attr == NULL)
216 		return EINVAL;
217 #endif
218 
219 	attr->ptba_magic = _PT_BARRIERATTR_MAGIC;
220 
221 	return 0;
222 }
223 
224 
225 int
226 pthread_barrierattr_destroy(pthread_barrierattr_t *attr)
227 {
228 
229 #ifdef ERRORCHECK
230 	if ((attr == NULL) ||
231 	    (attr->ptba_magic != _PT_BARRIERATTR_MAGIC))
232 		return EINVAL;
233 #endif
234 
235 	attr->ptba_magic = _PT_BARRIERATTR_DEAD;
236 
237 	return 0;
238 }
239