2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
32 #include "namespace.h"
36 #include "un-namespace.h"
38 #include "thr_private.h"
40 _Static_assert(sizeof(struct pthread_barrier) <= PAGE_SIZE,
41 "pthread_barrier is too large for off-page");
43 __weak_reference(_pthread_barrier_init, pthread_barrier_init);
44 __weak_reference(_pthread_barrier_wait, pthread_barrier_wait);
45 __weak_reference(_pthread_barrier_destroy, pthread_barrier_destroy);
48 _pthread_barrier_destroy(pthread_barrier_t *barrier)
50 pthread_barrier_t bar;
51 struct pthread *curthread;
54 if (barrier == NULL || *barrier == NULL)
57 if (*barrier == THR_PSHARED_PTR) {
58 bar = __thr_pshared_offpage(barrier, 0);
68 curthread = _get_curthread();
69 THR_UMUTEX_LOCK(curthread, &bar->b_lock);
70 if (bar->b_destroying) {
71 THR_UMUTEX_UNLOCK(curthread, &bar->b_lock);
74 bar->b_destroying = 1;
76 if (bar->b_waiters > 0) {
77 bar->b_destroying = 0;
78 THR_UMUTEX_UNLOCK(curthread, &bar->b_lock);
81 if (bar->b_refcount != 0) {
82 _thr_ucond_wait(&bar->b_cv, &bar->b_lock, NULL, 0);
83 THR_UMUTEX_LOCK(curthread, &bar->b_lock);
87 bar->b_destroying = 0;
88 THR_UMUTEX_UNLOCK(curthread, &bar->b_lock);
92 __thr_pshared_destroy(barrier);
99 _pthread_barrier_init(pthread_barrier_t * __restrict barrier,
100 const pthread_barrierattr_t * __restrict attr, unsigned count)
102 pthread_barrier_t bar;
105 if (barrier == NULL || count == 0 || count > INT_MAX)
108 if (attr == NULL || *attr == NULL ||
109 (*attr)->pshared == PTHREAD_PROCESS_PRIVATE) {
110 bar = calloc(1, sizeof(struct pthread_barrier));
116 bar = __thr_pshared_offpage(barrier, 1);
119 *barrier = THR_PSHARED_PTR;
123 _thr_umutex_init(&bar->b_lock);
124 _thr_ucond_init(&bar->b_cv);
126 bar->b_lock.m_flags |= USYNC_PROCESS_SHARED;
127 bar->b_cv.c_flags |= USYNC_PROCESS_SHARED;
129 bar->b_count = count;
134 _pthread_barrier_wait(pthread_barrier_t *barrier)
136 struct pthread *curthread;
137 pthread_barrier_t bar;
141 if (barrier == NULL || *barrier == NULL)
144 if (*barrier == THR_PSHARED_PTR) {
145 bar = __thr_pshared_offpage(barrier, 0);
151 curthread = _get_curthread();
152 THR_UMUTEX_LOCK(curthread, &bar->b_lock);
153 if (++bar->b_waiters == bar->b_count) {
154 /* Current thread is lastest thread */
157 _thr_ucond_broadcast(&bar->b_cv);
158 THR_UMUTEX_UNLOCK(curthread, &bar->b_lock);
159 ret = PTHREAD_BARRIER_SERIAL_THREAD;
161 cycle = bar->b_cycle;
164 _thr_ucond_wait(&bar->b_cv, &bar->b_lock, NULL, 0);
165 THR_UMUTEX_LOCK(curthread, &bar->b_lock);
166 /* test cycle to avoid bogus wakeup */
167 } while (cycle == bar->b_cycle);
168 if (--bar->b_refcount == 0 && bar->b_destroying)
169 _thr_ucond_broadcast(&bar->b_cv);
170 THR_UMUTEX_UNLOCK(curthread, &bar->b_lock);