xref: /onnv-gate/usr/src/lib/libc/port/threads/pthread.c (revision 6296:b2fd3d371933)
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
51885Sraf  * Common Development and Distribution License (the "License").
61885Sraf  * 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  */
211885Sraf 
220Sstevel@tonic-gate /*
236247Sraf  * 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_once related data
340Sstevel@tonic-gate  * This structure is exported as pthread_once_t in pthread.h.
350Sstevel@tonic-gate  * We export only the size of this structure. so check
360Sstevel@tonic-gate  * pthread_once_t in pthread.h before making a change here.
370Sstevel@tonic-gate  */
380Sstevel@tonic-gate typedef struct  __once {
390Sstevel@tonic-gate 	mutex_t	mlock;
400Sstevel@tonic-gate 	union {
410Sstevel@tonic-gate 		uint32_t	pad32_flag[2];
420Sstevel@tonic-gate 		uint64_t	pad64_flag;
430Sstevel@tonic-gate 	} oflag;
440Sstevel@tonic-gate } __once_t;
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #define	once_flag	oflag.pad32_flag[1]
470Sstevel@tonic-gate 
486247Sraf static int
496247Sraf _thr_setparam(pthread_t tid, int policy, int prio)
506247Sraf {
516247Sraf 	ulwp_t *ulwp;
526247Sraf 	id_t cid;
536247Sraf 	int error = 0;
546247Sraf 
556247Sraf 	if ((ulwp = find_lwp(tid)) == NULL) {
566247Sraf 		error = ESRCH;
576247Sraf 	} else {
586247Sraf 		if (policy == ulwp->ul_policy &&
596247Sraf 		    (policy == SCHED_FIFO || policy == SCHED_RR) &&
606247Sraf 		    ulwp->ul_epri != 0) {
616247Sraf 			/*
626247Sraf 			 * Don't change the ceiling priority,
636247Sraf 			 * just the base priority.
646247Sraf 			 */
656247Sraf 			if (prio > ulwp->ul_epri)
666247Sraf 				error = EPERM;
676247Sraf 			else
686247Sraf 				ulwp->ul_pri = prio;
696247Sraf 		} else if ((cid = setparam(P_LWPID, tid, policy, prio)) == -1) {
706247Sraf 			error = errno;
716247Sraf 		} else {
72*6296Sraf 			if (policy == SCHED_FIFO || policy == SCHED_RR)
73*6296Sraf 				ulwp->ul_rtclassid = cid;
746247Sraf 			ulwp->ul_cid = cid;
756247Sraf 			ulwp->ul_pri = prio;
766247Sraf 			_membar_producer();
776247Sraf 			ulwp->ul_policy = policy;
786247Sraf 		}
796247Sraf 		ulwp_unlock(ulwp, curthread->ul_uberdata);
806247Sraf 	}
816247Sraf 	return (error);
826247Sraf }
836247Sraf 
840Sstevel@tonic-gate /*
850Sstevel@tonic-gate  * pthread_create: creates a thread in the current process.
860Sstevel@tonic-gate  * calls common _thrp_create() after copying the attributes.
870Sstevel@tonic-gate  */
880Sstevel@tonic-gate #pragma weak	pthread_create			= _pthread_create
890Sstevel@tonic-gate int
900Sstevel@tonic-gate _pthread_create(pthread_t *thread, const pthread_attr_t *attr,
910Sstevel@tonic-gate 	void * (*start_routine)(void *), void *arg)
920Sstevel@tonic-gate {
930Sstevel@tonic-gate 	ulwp_t		*self = curthread;
941885Sraf 	const thrattr_t	*ap = attr? attr->__pthread_attrp : def_thrattr();
956247Sraf 	const pcclass_t	*pccp;
960Sstevel@tonic-gate 	long		flag;
970Sstevel@tonic-gate 	pthread_t	tid;
980Sstevel@tonic-gate 	int		error;
996247Sraf 
1006247Sraf 	update_sched(self);
1010Sstevel@tonic-gate 
1021885Sraf 	if (ap == NULL)
1030Sstevel@tonic-gate 		return (EINVAL);
1040Sstevel@tonic-gate 
1056247Sraf 	/* validate explicit scheduling attributes */
1066247Sraf 	if (ap->inherit == PTHREAD_EXPLICIT_SCHED &&
1076247Sraf 	    (ap->policy == SCHED_SYS ||
1086247Sraf 	    (pccp = get_info_by_policy(ap->policy)) == NULL ||
1096247Sraf 	    ap->prio < pccp->pcc_primin || ap->prio > pccp->pcc_primax))
1106247Sraf 		return (EINVAL);
1110Sstevel@tonic-gate 
1121885Sraf 	flag = ap->scope | ap->detachstate | ap->daemonstate | THR_SUSPENDED;
1130Sstevel@tonic-gate 	error = _thrp_create(ap->stkaddr, ap->stksize, start_routine, arg,
1146247Sraf 	    flag, &tid, ap->guardsize);
1150Sstevel@tonic-gate 	if (error == 0) {
1166247Sraf 		if (ap->inherit == PTHREAD_EXPLICIT_SCHED &&
1176247Sraf 		    (ap->policy != self->ul_policy ||
1186247Sraf 		    ap->prio != (self->ul_epri? self->ul_epri : self->ul_pri)))
1196247Sraf 			/*
1206247Sraf 			 * The SUSv3 specification requires pthread_create()
1216247Sraf 			 * to fail with EPERM if it cannot set the scheduling
1226247Sraf 			 * policy and parameters on the new thread.
1236247Sraf 			 */
1246247Sraf 			error = _thr_setparam(tid, ap->policy, ap->prio);
1256247Sraf 		if (error) {
1266247Sraf 			/*
1276247Sraf 			 * We couldn't determine this error before
1286247Sraf 			 * actually creating the thread.  To recover,
1296247Sraf 			 * mark the thread detached and cancel it.
1306247Sraf 			 * It is as though it was never created.
1316247Sraf 			 */
1320Sstevel@tonic-gate 			ulwp_t *ulwp = find_lwp(tid);
1336247Sraf 			if (ulwp->ul_detached == 0) {
1346247Sraf 				ulwp->ul_detached = 1;
1356247Sraf 				ulwp->ul_usropts |= THR_DETACHED;
1366247Sraf 				(void) __lwp_detach(tid);
1376247Sraf 			}
1386247Sraf 			ulwp->ul_cancel_pending = 2; /* cancelled on creation */
1396247Sraf 			ulwp->ul_cancel_disabled = 0;
1406247Sraf 			ulwp_unlock(ulwp, self->ul_uberdata);
1416247Sraf 		} else if (thread) {
1426247Sraf 			*thread = tid;
1430Sstevel@tonic-gate 		}
1440Sstevel@tonic-gate 		(void) _thr_continue(tid);
1450Sstevel@tonic-gate 	}
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	/* posix version expects EAGAIN for lack of memory */
1480Sstevel@tonic-gate 	if (error == ENOMEM)
1490Sstevel@tonic-gate 		error = EAGAIN;
1500Sstevel@tonic-gate 	return (error);
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate /*
1540Sstevel@tonic-gate  * pthread_once: calls given function only once.
1550Sstevel@tonic-gate  * it synchronizes via mutex in pthread_once_t structure
1560Sstevel@tonic-gate  */
1570Sstevel@tonic-gate #pragma weak	pthread_once			= _pthread_once
1580Sstevel@tonic-gate int
1590Sstevel@tonic-gate _pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate 	__once_t *once = (__once_t *)once_control;
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	if (once == NULL || init_routine == NULL)
1640Sstevel@tonic-gate 		return (EINVAL);
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	if (once->once_flag == PTHREAD_ONCE_NOTDONE) {
1670Sstevel@tonic-gate 		(void) _private_mutex_lock(&once->mlock);
1680Sstevel@tonic-gate 		if (once->once_flag == PTHREAD_ONCE_NOTDONE) {
1690Sstevel@tonic-gate 			pthread_cleanup_push(_private_mutex_unlock,
1700Sstevel@tonic-gate 			    &once->mlock);
1710Sstevel@tonic-gate 			(*init_routine)();
1720Sstevel@tonic-gate 			pthread_cleanup_pop(0);
1733864Sraf 			_membar_producer();
1740Sstevel@tonic-gate 			once->once_flag = PTHREAD_ONCE_DONE;
1750Sstevel@tonic-gate 		}
1760Sstevel@tonic-gate 		(void) _private_mutex_unlock(&once->mlock);
1770Sstevel@tonic-gate 	}
1783864Sraf 	_membar_consumer();
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	return (0);
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate /*
1840Sstevel@tonic-gate  * pthread_equal: equates two thread ids.
1850Sstevel@tonic-gate  */
1860Sstevel@tonic-gate #pragma weak	pthread_equal			= _pthread_equal
1870Sstevel@tonic-gate int
1880Sstevel@tonic-gate _pthread_equal(pthread_t t1, pthread_t t2)
1890Sstevel@tonic-gate {
1900Sstevel@tonic-gate 	return (t1 == t2);
1910Sstevel@tonic-gate }
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate /*
1946247Sraf  * pthread_getschedparam: get the thread's sched parameters.
1950Sstevel@tonic-gate  */
1960Sstevel@tonic-gate #pragma weak	pthread_getschedparam		= _pthread_getschedparam
1970Sstevel@tonic-gate int
1980Sstevel@tonic-gate _pthread_getschedparam(pthread_t tid, int *policy, struct sched_param *param)
1990Sstevel@tonic-gate {
2000Sstevel@tonic-gate 	ulwp_t *ulwp;
2016247Sraf 	id_t cid;
2020Sstevel@tonic-gate 	int error = 0;
2030Sstevel@tonic-gate 
2046247Sraf 	if ((ulwp = find_lwp(tid)) == NULL) {
2050Sstevel@tonic-gate 		error = ESRCH;
2066247Sraf 	} else {
2076247Sraf 		cid = getparam(P_LWPID, ulwp->ul_lwpid, policy, param);
2086247Sraf 		if (cid == -1) {
2096247Sraf 			error = errno;
2106247Sraf 		} else if (*policy == ulwp->ul_policy && cid == ulwp->ul_cid &&
2116247Sraf 		    (*policy == SCHED_FIFO || *policy == SCHED_RR)) {
2126247Sraf 			/*
2136247Sraf 			 * Return the defined priority, not the effective
2146247Sraf 			 * priority from priority ceiling mutexes.
2156247Sraf 			 */
2160Sstevel@tonic-gate 			param->sched_priority = ulwp->ul_pri;
2176247Sraf 		} else {
218*6296Sraf 			if (*policy == SCHED_FIFO || *policy == SCHED_RR)
219*6296Sraf 				ulwp->ul_rtclassid = cid;
2206247Sraf 			ulwp->ul_cid = cid;
2216247Sraf 			ulwp->ul_pri = param->sched_priority;
2226247Sraf 			_membar_producer();
2236247Sraf 			ulwp->ul_policy = *policy;
2246247Sraf 		}
2256247Sraf 		ulwp_unlock(ulwp, curthread->ul_uberdata);
2260Sstevel@tonic-gate 	}
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	return (error);
2290Sstevel@tonic-gate }
2300Sstevel@tonic-gate 
2316247Sraf #pragma weak thr_getprio = _thr_getprio
2320Sstevel@tonic-gate int
2336247Sraf _thr_getprio(thread_t tid, int *priority)
2340Sstevel@tonic-gate {
2356247Sraf 	struct sched_param param;
2366247Sraf 	int policy;
2376247Sraf 	int error;
2380Sstevel@tonic-gate 
2396247Sraf 	if ((error = _pthread_getschedparam(tid, &policy, &param)) == 0)
2406247Sraf 		*priority = param.sched_priority;
2410Sstevel@tonic-gate 	return (error);
2420Sstevel@tonic-gate }
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate /*
2450Sstevel@tonic-gate  * pthread_setschedparam: sets the sched parameters for a thread.
2460Sstevel@tonic-gate  */
2470Sstevel@tonic-gate #pragma weak	pthread_setschedparam		= _pthread_setschedparam
2480Sstevel@tonic-gate int
2490Sstevel@tonic-gate _pthread_setschedparam(pthread_t tid,
2500Sstevel@tonic-gate 	int policy, const struct sched_param *param)
2510Sstevel@tonic-gate {
2526247Sraf 	return (_thr_setparam(tid, policy, param->sched_priority));
2530Sstevel@tonic-gate }
2546247Sraf 
2556247Sraf #pragma weak thr_setprio = _thr_setprio
2566247Sraf #pragma weak pthread_setschedprio = _thr_setprio
2576247Sraf #pragma weak _pthread_setschedprio = _thr_setprio
2586247Sraf int
2596247Sraf _thr_setprio(thread_t tid, int prio)
2606247Sraf {
2616247Sraf 	struct sched_param param;
2626247Sraf 	int policy;
2636247Sraf 	int error;
2646247Sraf 
2656247Sraf 	/*
2666247Sraf 	 * _pthread_getschedparam() has the side-effect of setting
2676247Sraf 	 * the target thread's ul_policy, ul_pri and ul_cid correctly.
2686247Sraf 	 */
2696247Sraf 	if ((error = _pthread_getschedparam(tid, &policy, &param)) != 0)
2706247Sraf 		return (error);
2716247Sraf 	if (param.sched_priority == prio)	/* no change */
2726247Sraf 		return (0);
2736247Sraf 	return (_thr_setparam(tid, policy, prio));
2746247Sraf }
275