2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2003 David Xu <davidxu@freebsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 #include "namespace.h"
34 #include "un-namespace.h"
36 #include "thr_private.h"
38 _Static_assert(sizeof(struct pthread_barrier) <= THR_PAGE_SIZE_MIN,
39 "pthread_barrier is too large for off-page");
41 __weak_reference(_pthread_barrier_init, pthread_barrier_init);
42 __weak_reference(_pthread_barrier_wait, pthread_barrier_wait);
43 __weak_reference(_pthread_barrier_destroy, pthread_barrier_destroy);
46 _pthread_barrier_destroy(pthread_barrier_t *barrier)
48 pthread_barrier_t bar;
49 struct pthread *curthread;
52 if (barrier == NULL || *barrier == NULL)
55 if (*barrier == THR_PSHARED_PTR) {
56 bar = __thr_pshared_offpage(barrier, 0);
66 curthread = _get_curthread();
67 THR_UMUTEX_LOCK(curthread, &bar->b_lock);
68 if (bar->b_destroying) {
69 THR_UMUTEX_UNLOCK(curthread, &bar->b_lock);
72 bar->b_destroying = 1;
74 if (bar->b_waiters > 0) {
75 bar->b_destroying = 0;
76 THR_UMUTEX_UNLOCK(curthread, &bar->b_lock);
79 if (bar->b_refcount != 0) {
80 _thr_ucond_wait(&bar->b_cv, &bar->b_lock, NULL, 0);
81 THR_UMUTEX_LOCK(curthread, &bar->b_lock);
85 bar->b_destroying = 0;
86 THR_UMUTEX_UNLOCK(curthread, &bar->b_lock);
90 __thr_pshared_destroy(barrier);
97 _pthread_barrier_init(pthread_barrier_t * __restrict barrier,
98 const pthread_barrierattr_t * __restrict attr, unsigned count)
100 pthread_barrier_t bar;
103 if (barrier == NULL || count == 0 || count > INT_MAX)
106 if (attr == NULL || *attr == NULL ||
107 (*attr)->pshared == PTHREAD_PROCESS_PRIVATE) {
108 bar = calloc(1, sizeof(struct pthread_barrier));
114 bar = __thr_pshared_offpage(barrier, 1);
117 *barrier = THR_PSHARED_PTR;
121 _thr_umutex_init(&bar->b_lock);
122 _thr_ucond_init(&bar->b_cv);
124 bar->b_lock.m_flags |= USYNC_PROCESS_SHARED;
125 bar->b_cv.c_flags |= USYNC_PROCESS_SHARED;
127 bar->b_count = count;
132 _pthread_barrier_wait(pthread_barrier_t *barrier)
134 struct pthread *curthread;
135 pthread_barrier_t bar;
139 if (barrier == NULL || *barrier == NULL)
142 if (*barrier == THR_PSHARED_PTR) {
143 bar = __thr_pshared_offpage(barrier, 0);
149 curthread = _get_curthread();
150 THR_UMUTEX_LOCK(curthread, &bar->b_lock);
151 if (++bar->b_waiters == bar->b_count) {
152 /* Current thread is lastest thread */
155 _thr_ucond_broadcast(&bar->b_cv);
156 THR_UMUTEX_UNLOCK(curthread, &bar->b_lock);
157 ret = PTHREAD_BARRIER_SERIAL_THREAD;
159 cycle = bar->b_cycle;
162 _thr_ucond_wait(&bar->b_cv, &bar->b_lock, NULL, 0);
163 THR_UMUTEX_LOCK(curthread, &bar->b_lock);
164 /* test cycle to avoid bogus wakeup */
165 } while (cycle == bar->b_cycle);
166 if (--bar->b_refcount == 0 && bar->b_destroying)
167 _thr_ucond_broadcast(&bar->b_cv);
168 THR_UMUTEX_UNLOCK(curthread, &bar->b_lock);