xref: /onnv-gate/usr/src/lib/libc/port/threads/pthr_barrier.c (revision 7255:4f2e32b16726)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55891Sraf  * Common Development and Distribution License (the "License").
65891Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
215891Sraf 
220Sstevel@tonic-gate /*
235891Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include "lint.h"
300Sstevel@tonic-gate #include "thr_uberdata.h"
310Sstevel@tonic-gate #include <pthread.h>
320Sstevel@tonic-gate 
330Sstevel@tonic-gate /*
340Sstevel@tonic-gate  * Implementation-private attribute structure (for extensibility).
350Sstevel@tonic-gate  */
360Sstevel@tonic-gate typedef struct {
370Sstevel@tonic-gate 	int	pshared;
380Sstevel@tonic-gate } barrierattr_t;
390Sstevel@tonic-gate 
400Sstevel@tonic-gate int
pthread_barrierattr_init(pthread_barrierattr_t * attr)416812Sraf pthread_barrierattr_init(pthread_barrierattr_t *attr)
420Sstevel@tonic-gate {
430Sstevel@tonic-gate 	barrierattr_t *ap;
440Sstevel@tonic-gate 
450Sstevel@tonic-gate 	if ((ap = lmalloc(sizeof (barrierattr_t))) == NULL)
460Sstevel@tonic-gate 		return (ENOMEM);
470Sstevel@tonic-gate 	ap->pshared = PTHREAD_PROCESS_PRIVATE;
480Sstevel@tonic-gate 	attr->__pthread_barrierattrp = ap;
490Sstevel@tonic-gate 	return (0);
500Sstevel@tonic-gate }
510Sstevel@tonic-gate 
520Sstevel@tonic-gate int
pthread_barrierattr_destroy(pthread_barrierattr_t * attr)536812Sraf pthread_barrierattr_destroy(pthread_barrierattr_t *attr)
540Sstevel@tonic-gate {
550Sstevel@tonic-gate 	if (attr == NULL || attr->__pthread_barrierattrp == NULL)
560Sstevel@tonic-gate 		return (EINVAL);
570Sstevel@tonic-gate 	lfree(attr->__pthread_barrierattrp, sizeof (barrierattr_t));
580Sstevel@tonic-gate 	attr->__pthread_barrierattrp = NULL;
590Sstevel@tonic-gate 	return (0);
600Sstevel@tonic-gate }
610Sstevel@tonic-gate 
620Sstevel@tonic-gate int
pthread_barrierattr_setpshared(pthread_barrierattr_t * attr,int pshared)636812Sraf pthread_barrierattr_setpshared(pthread_barrierattr_t *attr, int pshared)
640Sstevel@tonic-gate {
650Sstevel@tonic-gate 	barrierattr_t *ap;
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 	if (attr == NULL || (ap = attr->__pthread_barrierattrp) == NULL ||
680Sstevel@tonic-gate 	    (pshared != PTHREAD_PROCESS_PRIVATE &&
690Sstevel@tonic-gate 	    pshared != PTHREAD_PROCESS_SHARED))
700Sstevel@tonic-gate 		return (EINVAL);
710Sstevel@tonic-gate 	ap->pshared = pshared;
720Sstevel@tonic-gate 	return (0);
730Sstevel@tonic-gate }
740Sstevel@tonic-gate 
750Sstevel@tonic-gate int
pthread_barrierattr_getpshared(const pthread_barrierattr_t * attr,int * pshared)766812Sraf pthread_barrierattr_getpshared(const pthread_barrierattr_t *attr, int *pshared)
770Sstevel@tonic-gate {
780Sstevel@tonic-gate 	barrierattr_t *ap;
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	if (attr == NULL || (ap = attr->__pthread_barrierattrp) == NULL ||
810Sstevel@tonic-gate 	    pshared == NULL)
820Sstevel@tonic-gate 		return (EINVAL);
830Sstevel@tonic-gate 	*pshared = ap->pshared;
840Sstevel@tonic-gate 	return (0);
850Sstevel@tonic-gate }
860Sstevel@tonic-gate 
870Sstevel@tonic-gate int
pthread_barrier_init(pthread_barrier_t * barrier,const pthread_barrierattr_t * attr,uint_t count)886812Sraf pthread_barrier_init(pthread_barrier_t *barrier,
89*7255Sraf     const pthread_barrierattr_t *attr, uint_t count)
900Sstevel@tonic-gate {
910Sstevel@tonic-gate 	mutex_t *mp = (mutex_t *)&barrier->__pthread_barrier_lock;
920Sstevel@tonic-gate 	cond_t *cvp = (cond_t *)&barrier->__pthread_barrier_cond;
930Sstevel@tonic-gate 	barrierattr_t *ap;
940Sstevel@tonic-gate 	int type;
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	if (attr == NULL)
970Sstevel@tonic-gate 		type = DEFAULT_TYPE;
980Sstevel@tonic-gate 	else if ((ap = attr->__pthread_barrierattrp) != NULL)
990Sstevel@tonic-gate 		type = ap->pshared;
1000Sstevel@tonic-gate 	else
1010Sstevel@tonic-gate 		type = -1;
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	if (count == 0 ||
1040Sstevel@tonic-gate 	    (type != PTHREAD_PROCESS_PRIVATE && type != PTHREAD_PROCESS_SHARED))
1050Sstevel@tonic-gate 		return (EINVAL);
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	barrier->__pthread_barrier_count = count;
1080Sstevel@tonic-gate 	barrier->__pthread_barrier_current = count;
1090Sstevel@tonic-gate 	barrier->__pthread_barrier_cycle = 0;
1100Sstevel@tonic-gate 	barrier->__pthread_barrier_reserved = 0;
1116812Sraf 	(void) mutex_init(mp, type, NULL);
1126812Sraf 	(void) cond_init(cvp, type, NULL);
113*7255Sraf 
114*7255Sraf 	/*
115*7255Sraf 	 * This should be at the beginning of the function,
116*7255Sraf 	 * but for the sake of old broken applications that
117*7255Sraf 	 * do not have proper alignment for their barriers
118*7255Sraf 	 * (and don't check the return code from pthread_barrier_init),
119*7255Sraf 	 * we put it here, after initializing the barrier regardless.
120*7255Sraf 	 */
121*7255Sraf 	if (((uintptr_t)barrier & (_LONG_LONG_ALIGNMENT - 1)) &&
122*7255Sraf 	    curthread->ul_misaligned == 0)
123*7255Sraf 		return (EINVAL);
124*7255Sraf 
1250Sstevel@tonic-gate 	return (0);
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate int
pthread_barrier_destroy(pthread_barrier_t * barrier)1296812Sraf pthread_barrier_destroy(pthread_barrier_t *barrier)
1300Sstevel@tonic-gate {
1310Sstevel@tonic-gate 	mutex_t *mp = (mutex_t *)&barrier->__pthread_barrier_lock;
1320Sstevel@tonic-gate 	cond_t *cvp = (cond_t *)&barrier->__pthread_barrier_cond;
1330Sstevel@tonic-gate 
1346812Sraf 	(void) mutex_destroy(mp);
1356812Sraf 	(void) cond_destroy(cvp);
1366515Sraf 	(void) memset(barrier, -1, sizeof (*barrier));
1370Sstevel@tonic-gate 	return (0);
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate /*
1410Sstevel@tonic-gate  * pthread_barrier_wait() is not a cancellation point;
1420Sstevel@tonic-gate  */
1430Sstevel@tonic-gate int
pthread_barrier_wait(pthread_barrier_t * barrier)1446812Sraf pthread_barrier_wait(pthread_barrier_t *barrier)
1450Sstevel@tonic-gate {
1460Sstevel@tonic-gate 	mutex_t *mp = (mutex_t *)&barrier->__pthread_barrier_lock;
1470Sstevel@tonic-gate 	cond_t *cvp = (cond_t *)&barrier->__pthread_barrier_cond;
1480Sstevel@tonic-gate 	uint64_t cycle;
1495891Sraf 	int cancel_state;
1500Sstevel@tonic-gate 
1516812Sraf 	(void) mutex_lock(mp);
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	if (--barrier->__pthread_barrier_current == 0) {
1540Sstevel@tonic-gate 		barrier->__pthread_barrier_cycle++;
1550Sstevel@tonic-gate 		barrier->__pthread_barrier_current =
1565891Sraf 		    barrier->__pthread_barrier_count;
1576812Sraf 		(void) mutex_unlock(mp);
1586812Sraf 		(void) cond_broadcast(cvp);
1590Sstevel@tonic-gate 		return (PTHREAD_BARRIER_SERIAL_THREAD);
1600Sstevel@tonic-gate 	}
1610Sstevel@tonic-gate 
1626812Sraf 	(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel_state);
1630Sstevel@tonic-gate 	cycle = barrier->__pthread_barrier_cycle;
1640Sstevel@tonic-gate 	do {
1656812Sraf 		(void) cond_wait(cvp, mp);
1660Sstevel@tonic-gate 	} while (cycle == barrier->__pthread_barrier_cycle);
1676812Sraf 	(void) pthread_setcancelstate(cancel_state, NULL);
1680Sstevel@tonic-gate 
1696812Sraf 	(void) mutex_unlock(mp);
1700Sstevel@tonic-gate 	return (0);
1710Sstevel@tonic-gate }
172