1 /* $NetBSD: pthread_barrier.c,v 1.18 2008/05/25 17:05:28 ad Exp $ */
4 * Copyright (c) 2001, 2003, 2006, 2007, 2009 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Nathan J. Williams, by Jason R. Thorpe, and by Andrew Doran.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: pthread_barrier.c,v 1.18 2008/05/25 17:05:28 ad Exp $");
38 #include "pthread_int.h"
41 pthread_barrier_init(pthread_barrier_t
*barrier
,
42 const pthread_barrierattr_t
*attr
, unsigned int count
)
45 if (attr
!= NULL
&& attr
->ptba_magic
!= _PT_BARRIERATTR_MAGIC
)
50 barrier
->ptb_magic
= _PT_BARRIER_MAGIC
;
51 PTQ_INIT(&barrier
->ptb_waiters
);
52 barrier
->ptb_initcount
= count
;
53 barrier
->ptb_curcount
= 0;
54 barrier
->ptb_generation
= 0;
59 pthread_barrier_destroy(pthread_barrier_t
*barrier
)
62 if (barrier
->ptb_magic
!= _PT_BARRIER_MAGIC
)
64 if (barrier
->ptb_curcount
!= 0)
70 pthread_barrier_wait(pthread_barrier_t
*barrier
)
72 pthread_mutex_t
*interlock
;
76 if (barrier
->ptb_magic
!= _PT_BARRIER_MAGIC
)
80 * A single arbitrary thread is supposed to return
81 * PTHREAD_BARRIER_SERIAL_THREAD, and everone else
82 * is supposed to return 0. Since pthread_barrier_wait()
83 * is not a cancellation point, this is trivial; we
84 * simply elect that the thread that causes the barrier
85 * to be satisfied gets the special return value. Note
86 * that this final thread does not actually need to block,
87 * but instead is responsible for waking everyone else up.
89 self
= pthread__self();
90 interlock
= pthread__hashlock(barrier
);
91 pthread_mutex_lock(interlock
);
92 if (barrier
->ptb_curcount
+ 1 == barrier
->ptb_initcount
) {
93 barrier
->ptb_generation
++;
94 barrier
->ptb_curcount
= 0;
95 pthread__unpark_all(&barrier
->ptb_waiters
, self
,
97 pthread_mutex_unlock(interlock
);
98 return PTHREAD_BARRIER_SERIAL_THREAD
;
100 barrier
->ptb_curcount
++;
101 gen
= barrier
->ptb_generation
;
103 PTQ_INSERT_TAIL(&barrier
->ptb_waiters
, self
, pt_sleep
);
104 self
->pt_sleepobj
= &barrier
->ptb_waiters
;
105 (void)pthread__park(self
, interlock
, &barrier
->ptb_waiters
,
106 NULL
, 0, __UNVOLATILE(&interlock
->ptm_waiters
));
107 if (__predict_true(gen
!= barrier
->ptb_generation
)) {
110 pthread_mutex_lock(interlock
);
111 if (gen
!= barrier
->ptb_generation
) {
112 pthread_mutex_unlock(interlock
);
122 pthread_barrierattr_init(pthread_barrierattr_t
*attr
)
125 attr
->ptba_magic
= _PT_BARRIERATTR_MAGIC
;
130 pthread_barrierattr_destroy(pthread_barrierattr_t
*attr
)
133 if (attr
->ptba_magic
!= _PT_BARRIERATTR_MAGIC
)
135 attr
->ptba_magic
= _PT_BARRIERATTR_DEAD
;