xref: /illumos-gate/usr/src/lib/libc/port/threads/pthr_cond.c (revision a90d75b828f0c01a350c4ecfc3924fd6456fffaf)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57257d1b4Sraf  * Common Development and Distribution License (the "License").
67257d1b4Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217257d1b4Sraf 
227c478bd9Sstevel@tonic-gate /*
237257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include "lint.h"
287c478bd9Sstevel@tonic-gate #include "thr_uberdata.h"
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * pthread_condattr_init: allocates the cond attribute object and
327c478bd9Sstevel@tonic-gate  * initializes it with the default values.
337c478bd9Sstevel@tonic-gate  */
347257d1b4Sraf #pragma weak _pthread_condattr_init = pthread_condattr_init
357c478bd9Sstevel@tonic-gate int
pthread_condattr_init(pthread_condattr_t * attr)367257d1b4Sraf pthread_condattr_init(pthread_condattr_t *attr)
377c478bd9Sstevel@tonic-gate {
387c478bd9Sstevel@tonic-gate 	cvattr_t *ap;
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate 	if ((ap = lmalloc(sizeof (cvattr_t))) == NULL)
417c478bd9Sstevel@tonic-gate 		return (ENOMEM);
42*a90d75b8SRichard PALO 	ap->pshared = PTHREAD_PROCESS_PRIVATE;
437c478bd9Sstevel@tonic-gate 	ap->clockid = CLOCK_REALTIME;
447c478bd9Sstevel@tonic-gate 	attr->__pthread_condattrp = ap;
457c478bd9Sstevel@tonic-gate 	return (0);
467c478bd9Sstevel@tonic-gate }
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /*
497c478bd9Sstevel@tonic-gate  * pthread_condattr_destroy: frees the cond attribute object and
507c478bd9Sstevel@tonic-gate  * invalidates it with NULL value.
517c478bd9Sstevel@tonic-gate  */
527c478bd9Sstevel@tonic-gate int
pthread_condattr_destroy(pthread_condattr_t * attr)537257d1b4Sraf pthread_condattr_destroy(pthread_condattr_t *attr)
547c478bd9Sstevel@tonic-gate {
557c478bd9Sstevel@tonic-gate 	if (attr == NULL || attr->__pthread_condattrp == NULL)
567c478bd9Sstevel@tonic-gate 		return (EINVAL);
577c478bd9Sstevel@tonic-gate 	lfree(attr->__pthread_condattrp, sizeof (cvattr_t));
587c478bd9Sstevel@tonic-gate 	attr->__pthread_condattrp = NULL;
597c478bd9Sstevel@tonic-gate 	return (0);
607c478bd9Sstevel@tonic-gate }
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate /*
637c478bd9Sstevel@tonic-gate  * pthread_condattr_setclock: sets the clockid attribute.
647c478bd9Sstevel@tonic-gate  */
657c478bd9Sstevel@tonic-gate int
pthread_condattr_setclock(pthread_condattr_t * attr,clockid_t clock_id)667257d1b4Sraf pthread_condattr_setclock(pthread_condattr_t *attr, clockid_t clock_id)
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate 	cvattr_t *ap;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 	if (attr != NULL && (ap = attr->__pthread_condattrp) != NULL &&
717c478bd9Sstevel@tonic-gate 	    (clock_id == CLOCK_REALTIME || clock_id == CLOCK_HIGHRES)) {
727c478bd9Sstevel@tonic-gate 		ap->clockid = clock_id;
737c478bd9Sstevel@tonic-gate 		return (0);
747c478bd9Sstevel@tonic-gate 	}
757c478bd9Sstevel@tonic-gate 	return (EINVAL);
767c478bd9Sstevel@tonic-gate }
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate  * pthread_condattr_getclock: gets the shared attr.
807c478bd9Sstevel@tonic-gate  */
817c478bd9Sstevel@tonic-gate int
pthread_condattr_getclock(const pthread_condattr_t * attr,clockid_t * clock_id)827257d1b4Sraf pthread_condattr_getclock(const pthread_condattr_t *attr, clockid_t *clock_id)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate 	cvattr_t *ap;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	if (attr != NULL && (ap = attr->__pthread_condattrp) != NULL &&
877c478bd9Sstevel@tonic-gate 	    clock_id != NULL) {
887c478bd9Sstevel@tonic-gate 		*clock_id = ap->clockid;
897c478bd9Sstevel@tonic-gate 		return (0);
907c478bd9Sstevel@tonic-gate 	}
917c478bd9Sstevel@tonic-gate 	return (EINVAL);
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate  * pthread_condattr_setpshared: sets the shared attr to PRIVATE or SHARED.
977c478bd9Sstevel@tonic-gate  * This is equivalent to setting USYNC_PROCESS/USYNC_THREAD flag in cond_init().
987c478bd9Sstevel@tonic-gate  */
997c478bd9Sstevel@tonic-gate int
pthread_condattr_setpshared(pthread_condattr_t * attr,int pshared)1007257d1b4Sraf pthread_condattr_setpshared(pthread_condattr_t *attr, int pshared)
1017c478bd9Sstevel@tonic-gate {
1027c478bd9Sstevel@tonic-gate 	cvattr_t *ap;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	if (attr != NULL && (ap = attr->__pthread_condattrp) != NULL &&
1057c478bd9Sstevel@tonic-gate 	    (pshared == PTHREAD_PROCESS_PRIVATE ||
1067c478bd9Sstevel@tonic-gate 	    pshared == PTHREAD_PROCESS_SHARED)) {
1077c478bd9Sstevel@tonic-gate 		ap->pshared = pshared;
1087c478bd9Sstevel@tonic-gate 		return (0);
1097c478bd9Sstevel@tonic-gate 	}
1107c478bd9Sstevel@tonic-gate 	return (EINVAL);
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate /*
1147c478bd9Sstevel@tonic-gate  * pthread_condattr_getpshared: gets the shared attr.
1157c478bd9Sstevel@tonic-gate  */
1167257d1b4Sraf #pragma weak _pthread_condattr_getpshared = pthread_condattr_getpshared
1177c478bd9Sstevel@tonic-gate int
pthread_condattr_getpshared(const pthread_condattr_t * attr,int * pshared)1187257d1b4Sraf pthread_condattr_getpshared(const pthread_condattr_t *attr, int *pshared)
1197c478bd9Sstevel@tonic-gate {
1207c478bd9Sstevel@tonic-gate 	cvattr_t *ap;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	if (attr != NULL && (ap = attr->__pthread_condattrp) != NULL &&
1237c478bd9Sstevel@tonic-gate 	    pshared != NULL) {
1247c478bd9Sstevel@tonic-gate 		*pshared = ap->pshared;
1257c478bd9Sstevel@tonic-gate 		return (0);
1267c478bd9Sstevel@tonic-gate 	}
1277c478bd9Sstevel@tonic-gate 	return (EINVAL);
1287c478bd9Sstevel@tonic-gate }
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate /*
1317c478bd9Sstevel@tonic-gate  * pthread_cond_init: Initializes the cond object. It copies the
1327c478bd9Sstevel@tonic-gate  * pshared attr into type argument and calls cond_init().
1337c478bd9Sstevel@tonic-gate  */
1347257d1b4Sraf #pragma weak _pthread_cond_init = pthread_cond_init
1357c478bd9Sstevel@tonic-gate int
pthread_cond_init(pthread_cond_t * cond,const pthread_condattr_t * attr)1367257d1b4Sraf pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate 	cvattr_t *ap;
1397c478bd9Sstevel@tonic-gate 	int type;
1407c478bd9Sstevel@tonic-gate 	clockid_t clock_id;
1417c478bd9Sstevel@tonic-gate 	int error;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	if (attr == NULL) {
144*a90d75b8SRichard PALO 		type = PTHREAD_PROCESS_PRIVATE;
1457c478bd9Sstevel@tonic-gate 		clock_id = CLOCK_REALTIME;
1467c478bd9Sstevel@tonic-gate 	} else if ((ap = attr->__pthread_condattrp) != NULL) {
1477c478bd9Sstevel@tonic-gate 		type = ap->pshared;
1487c478bd9Sstevel@tonic-gate 		clock_id = ap->clockid;
1497c478bd9Sstevel@tonic-gate 	} else {
1507c478bd9Sstevel@tonic-gate 		return (EINVAL);
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	if (clock_id != CLOCK_REALTIME && clock_id != CLOCK_HIGHRES)
1547c478bd9Sstevel@tonic-gate 		error = EINVAL;
1557257d1b4Sraf 	else if ((error = cond_init((cond_t *)cond, type, NULL)) == 0)
1567c478bd9Sstevel@tonic-gate 		((cond_t *)cond)->cond_clockid = (uint8_t)clock_id;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	return (error);
1597c478bd9Sstevel@tonic-gate }
160