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
5*6812Sraf * Common Development and Distribution License (the "License").
6*6812Sraf * 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 */
21*6812Sraf
220Sstevel@tonic-gate /*
23*6812Sraf * 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
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate * pthread_condattr_init: allocates the cond attribute object and
340Sstevel@tonic-gate * initializes it with the default values.
350Sstevel@tonic-gate */
36*6812Sraf #pragma weak _pthread_condattr_init = pthread_condattr_init
370Sstevel@tonic-gate int
pthread_condattr_init(pthread_condattr_t * attr)38*6812Sraf pthread_condattr_init(pthread_condattr_t *attr)
390Sstevel@tonic-gate {
400Sstevel@tonic-gate cvattr_t *ap;
410Sstevel@tonic-gate
420Sstevel@tonic-gate if ((ap = lmalloc(sizeof (cvattr_t))) == NULL)
430Sstevel@tonic-gate return (ENOMEM);
440Sstevel@tonic-gate ap->pshared = DEFAULT_TYPE;
450Sstevel@tonic-gate ap->clockid = CLOCK_REALTIME;
460Sstevel@tonic-gate attr->__pthread_condattrp = ap;
470Sstevel@tonic-gate return (0);
480Sstevel@tonic-gate }
490Sstevel@tonic-gate
500Sstevel@tonic-gate /*
510Sstevel@tonic-gate * pthread_condattr_destroy: frees the cond attribute object and
520Sstevel@tonic-gate * invalidates it with NULL value.
530Sstevel@tonic-gate */
540Sstevel@tonic-gate int
pthread_condattr_destroy(pthread_condattr_t * attr)55*6812Sraf pthread_condattr_destroy(pthread_condattr_t *attr)
560Sstevel@tonic-gate {
570Sstevel@tonic-gate if (attr == NULL || attr->__pthread_condattrp == NULL)
580Sstevel@tonic-gate return (EINVAL);
590Sstevel@tonic-gate lfree(attr->__pthread_condattrp, sizeof (cvattr_t));
600Sstevel@tonic-gate attr->__pthread_condattrp = NULL;
610Sstevel@tonic-gate return (0);
620Sstevel@tonic-gate }
630Sstevel@tonic-gate
640Sstevel@tonic-gate /*
650Sstevel@tonic-gate * pthread_condattr_setclock: sets the clockid attribute.
660Sstevel@tonic-gate */
670Sstevel@tonic-gate int
pthread_condattr_setclock(pthread_condattr_t * attr,clockid_t clock_id)68*6812Sraf pthread_condattr_setclock(pthread_condattr_t *attr, clockid_t clock_id)
690Sstevel@tonic-gate {
700Sstevel@tonic-gate cvattr_t *ap;
710Sstevel@tonic-gate
720Sstevel@tonic-gate if (attr != NULL && (ap = attr->__pthread_condattrp) != NULL &&
730Sstevel@tonic-gate (clock_id == CLOCK_REALTIME || clock_id == CLOCK_HIGHRES)) {
740Sstevel@tonic-gate ap->clockid = clock_id;
750Sstevel@tonic-gate return (0);
760Sstevel@tonic-gate }
770Sstevel@tonic-gate return (EINVAL);
780Sstevel@tonic-gate }
790Sstevel@tonic-gate
800Sstevel@tonic-gate /*
810Sstevel@tonic-gate * pthread_condattr_getclock: gets the shared attr.
820Sstevel@tonic-gate */
830Sstevel@tonic-gate int
pthread_condattr_getclock(const pthread_condattr_t * attr,clockid_t * clock_id)84*6812Sraf pthread_condattr_getclock(const pthread_condattr_t *attr, clockid_t *clock_id)
850Sstevel@tonic-gate {
860Sstevel@tonic-gate cvattr_t *ap;
870Sstevel@tonic-gate
880Sstevel@tonic-gate if (attr != NULL && (ap = attr->__pthread_condattrp) != NULL &&
890Sstevel@tonic-gate clock_id != NULL) {
900Sstevel@tonic-gate *clock_id = ap->clockid;
910Sstevel@tonic-gate return (0);
920Sstevel@tonic-gate }
930Sstevel@tonic-gate return (EINVAL);
940Sstevel@tonic-gate }
950Sstevel@tonic-gate
960Sstevel@tonic-gate
970Sstevel@tonic-gate /*
980Sstevel@tonic-gate * pthread_condattr_setpshared: sets the shared attr to PRIVATE or SHARED.
990Sstevel@tonic-gate * This is equivalent to setting USYNC_PROCESS/USYNC_THREAD flag in cond_init().
1000Sstevel@tonic-gate */
1010Sstevel@tonic-gate int
pthread_condattr_setpshared(pthread_condattr_t * attr,int pshared)102*6812Sraf pthread_condattr_setpshared(pthread_condattr_t *attr, int pshared)
1030Sstevel@tonic-gate {
1040Sstevel@tonic-gate cvattr_t *ap;
1050Sstevel@tonic-gate
1060Sstevel@tonic-gate if (attr != NULL && (ap = attr->__pthread_condattrp) != NULL &&
1070Sstevel@tonic-gate (pshared == PTHREAD_PROCESS_PRIVATE ||
1080Sstevel@tonic-gate pshared == PTHREAD_PROCESS_SHARED)) {
1090Sstevel@tonic-gate ap->pshared = pshared;
1100Sstevel@tonic-gate return (0);
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate return (EINVAL);
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate /*
1160Sstevel@tonic-gate * pthread_condattr_getpshared: gets the shared attr.
1170Sstevel@tonic-gate */
118*6812Sraf #pragma weak _pthread_condattr_getpshared = pthread_condattr_getpshared
1190Sstevel@tonic-gate int
pthread_condattr_getpshared(const pthread_condattr_t * attr,int * pshared)120*6812Sraf pthread_condattr_getpshared(const pthread_condattr_t *attr, int *pshared)
1210Sstevel@tonic-gate {
1220Sstevel@tonic-gate cvattr_t *ap;
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate if (attr != NULL && (ap = attr->__pthread_condattrp) != NULL &&
1250Sstevel@tonic-gate pshared != NULL) {
1260Sstevel@tonic-gate *pshared = ap->pshared;
1270Sstevel@tonic-gate return (0);
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate return (EINVAL);
1300Sstevel@tonic-gate }
1310Sstevel@tonic-gate
1320Sstevel@tonic-gate /*
1330Sstevel@tonic-gate * pthread_cond_init: Initializes the cond object. It copies the
1340Sstevel@tonic-gate * pshared attr into type argument and calls cond_init().
1350Sstevel@tonic-gate */
136*6812Sraf #pragma weak _pthread_cond_init = pthread_cond_init
1370Sstevel@tonic-gate int
pthread_cond_init(pthread_cond_t * cond,const pthread_condattr_t * attr)138*6812Sraf pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
1390Sstevel@tonic-gate {
1400Sstevel@tonic-gate cvattr_t *ap;
1410Sstevel@tonic-gate int type;
1420Sstevel@tonic-gate clockid_t clock_id;
1430Sstevel@tonic-gate int error;
1440Sstevel@tonic-gate
1450Sstevel@tonic-gate if (attr == NULL) {
1460Sstevel@tonic-gate type = DEFAULT_TYPE;
1470Sstevel@tonic-gate clock_id = CLOCK_REALTIME;
1480Sstevel@tonic-gate } else if ((ap = attr->__pthread_condattrp) != NULL) {
1490Sstevel@tonic-gate type = ap->pshared;
1500Sstevel@tonic-gate clock_id = ap->clockid;
1510Sstevel@tonic-gate } else {
1520Sstevel@tonic-gate return (EINVAL);
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate if (clock_id != CLOCK_REALTIME && clock_id != CLOCK_HIGHRES)
1560Sstevel@tonic-gate error = EINVAL;
157*6812Sraf else if ((error = cond_init((cond_t *)cond, type, NULL)) == 0)
1580Sstevel@tonic-gate ((cond_t *)cond)->cond_clockid = (uint8_t)clock_id;
1590Sstevel@tonic-gate
1600Sstevel@tonic-gate return (error);
1610Sstevel@tonic-gate }
162