xref: /onnv-gate/usr/src/lib/libc/port/threads/pthread.c (revision 3864:2ae506652d11)
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 /*
23*3864Sraf  * Copyright 2007 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 
480Sstevel@tonic-gate /*
490Sstevel@tonic-gate  * pthread_create: creates a thread in the current process.
500Sstevel@tonic-gate  * calls common _thrp_create() after copying the attributes.
510Sstevel@tonic-gate  */
520Sstevel@tonic-gate #pragma weak	pthread_create			= _pthread_create
530Sstevel@tonic-gate int
540Sstevel@tonic-gate _pthread_create(pthread_t *thread, const pthread_attr_t *attr,
550Sstevel@tonic-gate 	void * (*start_routine)(void *), void *arg)
560Sstevel@tonic-gate {
570Sstevel@tonic-gate 	ulwp_t		*self = curthread;
580Sstevel@tonic-gate 	uberdata_t	*udp = self->ul_uberdata;
591885Sraf 	const thrattr_t	*ap = attr? attr->__pthread_attrp : def_thrattr();
600Sstevel@tonic-gate 	long		flag;
610Sstevel@tonic-gate 	pthread_t	tid;
620Sstevel@tonic-gate 	int		policy;
630Sstevel@tonic-gate 	pri_t		priority;
640Sstevel@tonic-gate 	int		error;
650Sstevel@tonic-gate 	int		mapped = 0;
660Sstevel@tonic-gate 	int		mappedpri;
670Sstevel@tonic-gate 	int		rt = 0;
680Sstevel@tonic-gate 
691885Sraf 	if (ap == NULL)
700Sstevel@tonic-gate 		return (EINVAL);
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	if (ap->inherit == PTHREAD_INHERIT_SCHED) {
730Sstevel@tonic-gate 		policy = self->ul_policy;
740Sstevel@tonic-gate 		priority = self->ul_pri;
750Sstevel@tonic-gate 		mapped = self->ul_pri_mapped;
760Sstevel@tonic-gate 		mappedpri = self->ul_mappedpri;
770Sstevel@tonic-gate 	} else {
780Sstevel@tonic-gate 		policy = ap->policy;
790Sstevel@tonic-gate 		priority = ap->prio;
800Sstevel@tonic-gate 		if (policy == SCHED_OTHER) {
810Sstevel@tonic-gate 			if (priority < THREAD_MIN_PRIORITY ||
820Sstevel@tonic-gate 			    priority > THREAD_MAX_PRIORITY) {
830Sstevel@tonic-gate 				if (_validate_rt_prio(policy, priority))
840Sstevel@tonic-gate 					return (EINVAL);
850Sstevel@tonic-gate 				mapped = 1;
860Sstevel@tonic-gate 				mappedpri = priority;
872248Sraf 				priority = map_rtpri_to_gp(priority);
880Sstevel@tonic-gate 				ASSERT(priority >= THREAD_MIN_PRIORITY &&
890Sstevel@tonic-gate 				    priority <= THREAD_MAX_PRIORITY);
900Sstevel@tonic-gate 			}
910Sstevel@tonic-gate 		} else if (policy == SCHED_FIFO || policy == SCHED_RR) {
920Sstevel@tonic-gate 			if (_validate_rt_prio(policy, priority))
930Sstevel@tonic-gate 				return (EINVAL);
940Sstevel@tonic-gate 			if (_private_geteuid() == 0)
950Sstevel@tonic-gate 				rt = 1;
960Sstevel@tonic-gate 		} else {
970Sstevel@tonic-gate 			return (EINVAL);
980Sstevel@tonic-gate 		}
990Sstevel@tonic-gate 	}
1000Sstevel@tonic-gate 
1011885Sraf 	flag = ap->scope | ap->detachstate | ap->daemonstate | THR_SUSPENDED;
1020Sstevel@tonic-gate 	error = _thrp_create(ap->stkaddr, ap->stksize, start_routine, arg,
1030Sstevel@tonic-gate 		flag, &tid, priority, policy, ap->guardsize);
1040Sstevel@tonic-gate 	if (error == 0) {
1052712Snn35248 		int prio_err;
1062712Snn35248 
1070Sstevel@tonic-gate 		if (mapped) {
1080Sstevel@tonic-gate 			ulwp_t *ulwp = find_lwp(tid);
1090Sstevel@tonic-gate 			ulwp->ul_pri_mapped = 1;
1100Sstevel@tonic-gate 			ulwp->ul_mappedpri = mappedpri;
1110Sstevel@tonic-gate 			ulwp_unlock(ulwp, udp);
1120Sstevel@tonic-gate 		}
1132712Snn35248 
1142712Snn35248 		if (rt && (prio_err = _thrp_setlwpprio(tid, policy, priority)))
1152712Snn35248 			return (prio_err);
1162712Snn35248 
1170Sstevel@tonic-gate 		if (thread)
1180Sstevel@tonic-gate 			*thread = tid;
1190Sstevel@tonic-gate 		(void) _thr_continue(tid);
1200Sstevel@tonic-gate 	}
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	/* posix version expects EAGAIN for lack of memory */
1230Sstevel@tonic-gate 	if (error == ENOMEM)
1240Sstevel@tonic-gate 		error = EAGAIN;
1250Sstevel@tonic-gate 	return (error);
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate /*
1290Sstevel@tonic-gate  * pthread_once: calls given function only once.
1300Sstevel@tonic-gate  * it synchronizes via mutex in pthread_once_t structure
1310Sstevel@tonic-gate  */
1320Sstevel@tonic-gate #pragma weak	pthread_once			= _pthread_once
1330Sstevel@tonic-gate int
1340Sstevel@tonic-gate _pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
1350Sstevel@tonic-gate {
1360Sstevel@tonic-gate 	__once_t *once = (__once_t *)once_control;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	if (once == NULL || init_routine == NULL)
1390Sstevel@tonic-gate 		return (EINVAL);
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	if (once->once_flag == PTHREAD_ONCE_NOTDONE) {
1420Sstevel@tonic-gate 		(void) _private_mutex_lock(&once->mlock);
1430Sstevel@tonic-gate 		if (once->once_flag == PTHREAD_ONCE_NOTDONE) {
1440Sstevel@tonic-gate 			pthread_cleanup_push(_private_mutex_unlock,
1450Sstevel@tonic-gate 			    &once->mlock);
1460Sstevel@tonic-gate 			(*init_routine)();
1470Sstevel@tonic-gate 			pthread_cleanup_pop(0);
148*3864Sraf 			_membar_producer();
1490Sstevel@tonic-gate 			once->once_flag = PTHREAD_ONCE_DONE;
1500Sstevel@tonic-gate 		}
1510Sstevel@tonic-gate 		(void) _private_mutex_unlock(&once->mlock);
1520Sstevel@tonic-gate 	}
153*3864Sraf 	_membar_consumer();
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	return (0);
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate /*
1590Sstevel@tonic-gate  * pthread_equal: equates two thread ids.
1600Sstevel@tonic-gate  */
1610Sstevel@tonic-gate #pragma weak	pthread_equal			= _pthread_equal
1620Sstevel@tonic-gate int
1630Sstevel@tonic-gate _pthread_equal(pthread_t t1, pthread_t t2)
1640Sstevel@tonic-gate {
1650Sstevel@tonic-gate 	return (t1 == t2);
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate /*
1690Sstevel@tonic-gate  * pthread_getschedparam: gets the sched parameters in a struct.
1700Sstevel@tonic-gate  */
1710Sstevel@tonic-gate #pragma weak	pthread_getschedparam		= _pthread_getschedparam
1720Sstevel@tonic-gate int
1730Sstevel@tonic-gate _pthread_getschedparam(pthread_t tid, int *policy, struct sched_param *param)
1740Sstevel@tonic-gate {
1750Sstevel@tonic-gate 	uberdata_t *udp = curthread->ul_uberdata;
1760Sstevel@tonic-gate 	ulwp_t *ulwp;
1770Sstevel@tonic-gate 	int error = 0;
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	if (param == NULL || policy == NULL)
1800Sstevel@tonic-gate 		error = EINVAL;
1810Sstevel@tonic-gate 	else if ((ulwp = find_lwp(tid)) == NULL)
1820Sstevel@tonic-gate 		error = ESRCH;
1830Sstevel@tonic-gate 	else {
1840Sstevel@tonic-gate 		if (ulwp->ul_pri_mapped)
1850Sstevel@tonic-gate 			param->sched_priority = ulwp->ul_mappedpri;
1860Sstevel@tonic-gate 		else
1870Sstevel@tonic-gate 			param->sched_priority = ulwp->ul_pri;
1880Sstevel@tonic-gate 		*policy = ulwp->ul_policy;
1890Sstevel@tonic-gate 		ulwp_unlock(ulwp, udp);
1900Sstevel@tonic-gate 	}
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	return (error);
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate /*
1960Sstevel@tonic-gate  * Besides the obvious arguments, the inheritflag needs to be explained:
1970Sstevel@tonic-gate  * If set to PRIO_SET or PRIO_SET_PRIO, it does the normal, expected work
1980Sstevel@tonic-gate  * of setting thread's assigned scheduling parameters and policy.
1990Sstevel@tonic-gate  * If set to PRIO_INHERIT, it sets the thread's effective priority values
2000Sstevel@tonic-gate  * (t_epri, t_empappedpri), and does not update the assigned priority values
2010Sstevel@tonic-gate  * (t_pri, t_mappedpri).  If set to PRIO_DISINHERIT, it clears the thread's
2020Sstevel@tonic-gate  * effective priority values, and reverts the thread, if necessary, back
2030Sstevel@tonic-gate  * to the assigned priority values.
2040Sstevel@tonic-gate  */
2050Sstevel@tonic-gate int
2060Sstevel@tonic-gate _thread_setschedparam_main(pthread_t tid, int policy,
2070Sstevel@tonic-gate     const struct sched_param *param, int inheritflag)
2080Sstevel@tonic-gate {
2090Sstevel@tonic-gate 	uberdata_t *udp = curthread->ul_uberdata;
2100Sstevel@tonic-gate 	ulwp_t	*ulwp;
2110Sstevel@tonic-gate 	int	error = 0;
2120Sstevel@tonic-gate 	int	prio;
2130Sstevel@tonic-gate 	int	opolicy;
2140Sstevel@tonic-gate 	int	mappedprio;
2150Sstevel@tonic-gate 	int	mapped = 0;
2160Sstevel@tonic-gate 	pri_t	*mappedprip;
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	if (param == NULL)
2190Sstevel@tonic-gate 		return (EINVAL);
2200Sstevel@tonic-gate 	if ((ulwp = find_lwp(tid)) == NULL)
2210Sstevel@tonic-gate 		return (ESRCH);
2220Sstevel@tonic-gate 	prio = param->sched_priority;
2230Sstevel@tonic-gate 	opolicy = ulwp->ul_policy;
2240Sstevel@tonic-gate 	if (inheritflag == PRIO_SET_PRIO) {	/* don't change policy */
2250Sstevel@tonic-gate 		policy = opolicy;
2260Sstevel@tonic-gate 		inheritflag = PRIO_SET;
2270Sstevel@tonic-gate 	}
2280Sstevel@tonic-gate 	ASSERT(inheritflag == PRIO_SET || opolicy == policy);
2290Sstevel@tonic-gate 	if (inheritflag == PRIO_DISINHERIT) {
2300Sstevel@tonic-gate 		ulwp->ul_emappedpri = 0;
2310Sstevel@tonic-gate 		ulwp->ul_epri = 0;
2320Sstevel@tonic-gate 		prio = ulwp->ul_pri;	/* ignore prio in sched_param */
2330Sstevel@tonic-gate 	}
2340Sstevel@tonic-gate 	if (policy == SCHED_OTHER) {
2350Sstevel@tonic-gate 		/*
2360Sstevel@tonic-gate 		 * Set thread's policy to OTHER
2370Sstevel@tonic-gate 		 */
2380Sstevel@tonic-gate 		if (prio < THREAD_MIN_PRIORITY || prio > THREAD_MAX_PRIORITY) {
2390Sstevel@tonic-gate 			if (_validate_rt_prio(policy, prio)) {
2400Sstevel@tonic-gate 				error = EINVAL;
2410Sstevel@tonic-gate 				goto out;
2420Sstevel@tonic-gate 			}
2430Sstevel@tonic-gate 			mapped = 1;
2440Sstevel@tonic-gate 			mappedprio = prio;
2452248Sraf 			prio = map_rtpri_to_gp(prio);
2460Sstevel@tonic-gate 			ASSERT(prio >= THREAD_MIN_PRIORITY &&
2470Sstevel@tonic-gate 			    prio <= THREAD_MAX_PRIORITY);
2480Sstevel@tonic-gate 		}
2490Sstevel@tonic-gate 		/*
2500Sstevel@tonic-gate 		 * Thread changing from FIFO/RR to OTHER
2510Sstevel@tonic-gate 		 */
2520Sstevel@tonic-gate 		if (opolicy == SCHED_FIFO || opolicy == SCHED_RR) {
2530Sstevel@tonic-gate 			if ((error = _thrp_setlwpprio(tid, policy, prio)) != 0)
2540Sstevel@tonic-gate 				goto out;
2550Sstevel@tonic-gate 		}
2560Sstevel@tonic-gate 		if (inheritflag != PRIO_DISINHERIT) {
2570Sstevel@tonic-gate 			if (inheritflag == PRIO_INHERIT)
2580Sstevel@tonic-gate 				mappedprip = &ulwp->ul_emappedpri;
2590Sstevel@tonic-gate 			else
2600Sstevel@tonic-gate 				mappedprip = &ulwp->ul_mappedpri;
2610Sstevel@tonic-gate 			if (mapped) {
2620Sstevel@tonic-gate 				ulwp->ul_pri_mapped = 1;
2630Sstevel@tonic-gate 				*mappedprip = mappedprio;
2640Sstevel@tonic-gate 			} else {
2650Sstevel@tonic-gate 				ulwp->ul_pri_mapped = 0;
2660Sstevel@tonic-gate 				*mappedprip = 0;
2670Sstevel@tonic-gate 			}
2680Sstevel@tonic-gate 		}
2690Sstevel@tonic-gate 		ulwp->ul_policy = policy;
2700Sstevel@tonic-gate 		if (inheritflag == PRIO_INHERIT)
2710Sstevel@tonic-gate 			ulwp->ul_epri = prio;
2720Sstevel@tonic-gate 		else
2730Sstevel@tonic-gate 			ulwp->ul_pri = prio;
2740Sstevel@tonic-gate 	} else if (policy == SCHED_FIFO || policy == SCHED_RR) {
2750Sstevel@tonic-gate 		if (_validate_rt_prio(policy, prio))
2760Sstevel@tonic-gate 			error = EINVAL;
2770Sstevel@tonic-gate 		else {
2782712Snn35248 			int prio_err;
2792712Snn35248 
2800Sstevel@tonic-gate 			if (_private_geteuid() == 0 &&
2812712Snn35248 			    (prio_err = _thrp_setlwpprio(tid, policy, prio))) {
2822712Snn35248 				error = prio_err;
2832712Snn35248 				goto out;
2842712Snn35248 			}
2852712Snn35248 
2860Sstevel@tonic-gate 			ulwp->ul_policy = policy;
2870Sstevel@tonic-gate 			if (inheritflag == PRIO_INHERIT)
2880Sstevel@tonic-gate 				ulwp->ul_epri = prio;
2890Sstevel@tonic-gate 			else
2900Sstevel@tonic-gate 				ulwp->ul_pri = prio;
2910Sstevel@tonic-gate 		}
2920Sstevel@tonic-gate 	} else {
2930Sstevel@tonic-gate 		error = EINVAL;
2940Sstevel@tonic-gate 	}
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate out:
2970Sstevel@tonic-gate 	ulwp_unlock(ulwp, udp);
2980Sstevel@tonic-gate 	return (error);
2990Sstevel@tonic-gate }
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate /*
3020Sstevel@tonic-gate  * pthread_setschedparam: sets the sched parameters for a thread.
3030Sstevel@tonic-gate  */
3040Sstevel@tonic-gate #pragma weak	pthread_setschedparam		= _pthread_setschedparam
3050Sstevel@tonic-gate int
3060Sstevel@tonic-gate _pthread_setschedparam(pthread_t tid,
3070Sstevel@tonic-gate 	int policy, const struct sched_param *param)
3080Sstevel@tonic-gate {
3090Sstevel@tonic-gate 	return (_thread_setschedparam_main(tid, policy, param, PRIO_SET));
3100Sstevel@tonic-gate }
311