xref: /illumos-gate/usr/src/lib/libc/port/threads/pthread.c (revision 9acbbeaf2a1ffe5c14b244867d427714fab43c5c)
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
534709573Sraf  * Common Development and Distribution License (the "License").
634709573Sraf  * 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  */
2134709573Sraf 
227c478bd9Sstevel@tonic-gate /*
2334709573Sraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include "lint.h"
307c478bd9Sstevel@tonic-gate #include "thr_uberdata.h"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * pthread_once related data
347c478bd9Sstevel@tonic-gate  * This structure is exported as pthread_once_t in pthread.h.
357c478bd9Sstevel@tonic-gate  * We export only the size of this structure. so check
367c478bd9Sstevel@tonic-gate  * pthread_once_t in pthread.h before making a change here.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate typedef struct  __once {
397c478bd9Sstevel@tonic-gate 	mutex_t	mlock;
407c478bd9Sstevel@tonic-gate 	union {
417c478bd9Sstevel@tonic-gate 		uint32_t	pad32_flag[2];
427c478bd9Sstevel@tonic-gate 		uint64_t	pad64_flag;
437c478bd9Sstevel@tonic-gate 	} oflag;
447c478bd9Sstevel@tonic-gate } __once_t;
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #define	once_flag	oflag.pad32_flag[1]
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /*
497c478bd9Sstevel@tonic-gate  * pthread_create: creates a thread in the current process.
507c478bd9Sstevel@tonic-gate  * calls common _thrp_create() after copying the attributes.
517c478bd9Sstevel@tonic-gate  */
527c478bd9Sstevel@tonic-gate #pragma weak	pthread_create			= _pthread_create
537c478bd9Sstevel@tonic-gate int
547c478bd9Sstevel@tonic-gate _pthread_create(pthread_t *thread, const pthread_attr_t *attr,
557c478bd9Sstevel@tonic-gate 	void * (*start_routine)(void *), void *arg)
567c478bd9Sstevel@tonic-gate {
577c478bd9Sstevel@tonic-gate 	ulwp_t		*self = curthread;
587c478bd9Sstevel@tonic-gate 	uberdata_t	*udp = self->ul_uberdata;
5934709573Sraf 	const thrattr_t	*ap = attr? attr->__pthread_attrp : def_thrattr();
607c478bd9Sstevel@tonic-gate 	long		flag;
617c478bd9Sstevel@tonic-gate 	pthread_t	tid;
627c478bd9Sstevel@tonic-gate 	int		policy;
637c478bd9Sstevel@tonic-gate 	pri_t		priority;
647c478bd9Sstevel@tonic-gate 	int		error;
657c478bd9Sstevel@tonic-gate 	int		mapped = 0;
667c478bd9Sstevel@tonic-gate 	int		mappedpri;
677c478bd9Sstevel@tonic-gate 	int		rt = 0;
687c478bd9Sstevel@tonic-gate 
6934709573Sraf 	if (ap == NULL)
707c478bd9Sstevel@tonic-gate 		return (EINVAL);
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	if (ap->inherit == PTHREAD_INHERIT_SCHED) {
737c478bd9Sstevel@tonic-gate 		policy = self->ul_policy;
747c478bd9Sstevel@tonic-gate 		priority = self->ul_pri;
757c478bd9Sstevel@tonic-gate 		mapped = self->ul_pri_mapped;
767c478bd9Sstevel@tonic-gate 		mappedpri = self->ul_mappedpri;
777c478bd9Sstevel@tonic-gate 	} else {
787c478bd9Sstevel@tonic-gate 		policy = ap->policy;
797c478bd9Sstevel@tonic-gate 		priority = ap->prio;
807c478bd9Sstevel@tonic-gate 		if (policy == SCHED_OTHER) {
817c478bd9Sstevel@tonic-gate 			if (priority < THREAD_MIN_PRIORITY ||
827c478bd9Sstevel@tonic-gate 			    priority > THREAD_MAX_PRIORITY) {
837c478bd9Sstevel@tonic-gate 				if (_validate_rt_prio(policy, priority))
847c478bd9Sstevel@tonic-gate 					return (EINVAL);
857c478bd9Sstevel@tonic-gate 				mapped = 1;
867c478bd9Sstevel@tonic-gate 				mappedpri = priority;
87f841f6adSraf 				priority = map_rtpri_to_gp(priority);
887c478bd9Sstevel@tonic-gate 				ASSERT(priority >= THREAD_MIN_PRIORITY &&
897c478bd9Sstevel@tonic-gate 				    priority <= THREAD_MAX_PRIORITY);
907c478bd9Sstevel@tonic-gate 			}
917c478bd9Sstevel@tonic-gate 		} else if (policy == SCHED_FIFO || policy == SCHED_RR) {
927c478bd9Sstevel@tonic-gate 			if (_validate_rt_prio(policy, priority))
937c478bd9Sstevel@tonic-gate 				return (EINVAL);
947c478bd9Sstevel@tonic-gate 			if (_private_geteuid() == 0)
957c478bd9Sstevel@tonic-gate 				rt = 1;
967c478bd9Sstevel@tonic-gate 		} else {
977c478bd9Sstevel@tonic-gate 			return (EINVAL);
987c478bd9Sstevel@tonic-gate 		}
997c478bd9Sstevel@tonic-gate 	}
1007c478bd9Sstevel@tonic-gate 
10134709573Sraf 	flag = ap->scope | ap->detachstate | ap->daemonstate | THR_SUSPENDED;
1027c478bd9Sstevel@tonic-gate 	error = _thrp_create(ap->stkaddr, ap->stksize, start_routine, arg,
1037c478bd9Sstevel@tonic-gate 		flag, &tid, priority, policy, ap->guardsize);
1047c478bd9Sstevel@tonic-gate 	if (error == 0) {
105*9acbbeafSnn 		int prio_err;
106*9acbbeafSnn 
1077c478bd9Sstevel@tonic-gate 		if (mapped) {
1087c478bd9Sstevel@tonic-gate 			ulwp_t *ulwp = find_lwp(tid);
1097c478bd9Sstevel@tonic-gate 			ulwp->ul_pri_mapped = 1;
1107c478bd9Sstevel@tonic-gate 			ulwp->ul_mappedpri = mappedpri;
1117c478bd9Sstevel@tonic-gate 			ulwp_unlock(ulwp, udp);
1127c478bd9Sstevel@tonic-gate 		}
113*9acbbeafSnn 
114*9acbbeafSnn 		if (rt && (prio_err = _thrp_setlwpprio(tid, policy, priority)))
115*9acbbeafSnn 			return (prio_err);
116*9acbbeafSnn 
1177c478bd9Sstevel@tonic-gate 		if (thread)
1187c478bd9Sstevel@tonic-gate 			*thread = tid;
1197c478bd9Sstevel@tonic-gate 		(void) _thr_continue(tid);
1207c478bd9Sstevel@tonic-gate 	}
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	/* posix version expects EAGAIN for lack of memory */
1237c478bd9Sstevel@tonic-gate 	if (error == ENOMEM)
1247c478bd9Sstevel@tonic-gate 		error = EAGAIN;
1257c478bd9Sstevel@tonic-gate 	return (error);
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate /*
1297c478bd9Sstevel@tonic-gate  * pthread_once: calls given function only once.
1307c478bd9Sstevel@tonic-gate  * it synchronizes via mutex in pthread_once_t structure
1317c478bd9Sstevel@tonic-gate  */
1327c478bd9Sstevel@tonic-gate #pragma weak	pthread_once			= _pthread_once
1337c478bd9Sstevel@tonic-gate int
1347c478bd9Sstevel@tonic-gate _pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
1357c478bd9Sstevel@tonic-gate {
1367c478bd9Sstevel@tonic-gate 	__once_t *once = (__once_t *)once_control;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	if (once == NULL || init_routine == NULL)
1397c478bd9Sstevel@tonic-gate 		return (EINVAL);
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	if (once->once_flag == PTHREAD_ONCE_NOTDONE) {
1427c478bd9Sstevel@tonic-gate 		(void) _private_mutex_lock(&once->mlock);
1437c478bd9Sstevel@tonic-gate 		if (once->once_flag == PTHREAD_ONCE_NOTDONE) {
1447c478bd9Sstevel@tonic-gate 			pthread_cleanup_push(_private_mutex_unlock,
1457c478bd9Sstevel@tonic-gate 			    &once->mlock);
1467c478bd9Sstevel@tonic-gate 			(*init_routine)();
1477c478bd9Sstevel@tonic-gate 			pthread_cleanup_pop(0);
1487c478bd9Sstevel@tonic-gate 			once->once_flag = PTHREAD_ONCE_DONE;
1497c478bd9Sstevel@tonic-gate 		}
1507c478bd9Sstevel@tonic-gate 		(void) _private_mutex_unlock(&once->mlock);
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	return (0);
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate /*
1577c478bd9Sstevel@tonic-gate  * pthread_equal: equates two thread ids.
1587c478bd9Sstevel@tonic-gate  */
1597c478bd9Sstevel@tonic-gate #pragma weak	pthread_equal			= _pthread_equal
1607c478bd9Sstevel@tonic-gate int
1617c478bd9Sstevel@tonic-gate _pthread_equal(pthread_t t1, pthread_t t2)
1627c478bd9Sstevel@tonic-gate {
1637c478bd9Sstevel@tonic-gate 	return (t1 == t2);
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate /*
1677c478bd9Sstevel@tonic-gate  * pthread_getschedparam: gets the sched parameters in a struct.
1687c478bd9Sstevel@tonic-gate  */
1697c478bd9Sstevel@tonic-gate #pragma weak	pthread_getschedparam		= _pthread_getschedparam
1707c478bd9Sstevel@tonic-gate int
1717c478bd9Sstevel@tonic-gate _pthread_getschedparam(pthread_t tid, int *policy, struct sched_param *param)
1727c478bd9Sstevel@tonic-gate {
1737c478bd9Sstevel@tonic-gate 	uberdata_t *udp = curthread->ul_uberdata;
1747c478bd9Sstevel@tonic-gate 	ulwp_t *ulwp;
1757c478bd9Sstevel@tonic-gate 	int error = 0;
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	if (param == NULL || policy == NULL)
1787c478bd9Sstevel@tonic-gate 		error = EINVAL;
1797c478bd9Sstevel@tonic-gate 	else if ((ulwp = find_lwp(tid)) == NULL)
1807c478bd9Sstevel@tonic-gate 		error = ESRCH;
1817c478bd9Sstevel@tonic-gate 	else {
1827c478bd9Sstevel@tonic-gate 		if (ulwp->ul_pri_mapped)
1837c478bd9Sstevel@tonic-gate 			param->sched_priority = ulwp->ul_mappedpri;
1847c478bd9Sstevel@tonic-gate 		else
1857c478bd9Sstevel@tonic-gate 			param->sched_priority = ulwp->ul_pri;
1867c478bd9Sstevel@tonic-gate 		*policy = ulwp->ul_policy;
1877c478bd9Sstevel@tonic-gate 		ulwp_unlock(ulwp, udp);
1887c478bd9Sstevel@tonic-gate 	}
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	return (error);
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate /*
1947c478bd9Sstevel@tonic-gate  * Besides the obvious arguments, the inheritflag needs to be explained:
1957c478bd9Sstevel@tonic-gate  * If set to PRIO_SET or PRIO_SET_PRIO, it does the normal, expected work
1967c478bd9Sstevel@tonic-gate  * of setting thread's assigned scheduling parameters and policy.
1977c478bd9Sstevel@tonic-gate  * If set to PRIO_INHERIT, it sets the thread's effective priority values
1987c478bd9Sstevel@tonic-gate  * (t_epri, t_empappedpri), and does not update the assigned priority values
1997c478bd9Sstevel@tonic-gate  * (t_pri, t_mappedpri).  If set to PRIO_DISINHERIT, it clears the thread's
2007c478bd9Sstevel@tonic-gate  * effective priority values, and reverts the thread, if necessary, back
2017c478bd9Sstevel@tonic-gate  * to the assigned priority values.
2027c478bd9Sstevel@tonic-gate  */
2037c478bd9Sstevel@tonic-gate int
2047c478bd9Sstevel@tonic-gate _thread_setschedparam_main(pthread_t tid, int policy,
2057c478bd9Sstevel@tonic-gate     const struct sched_param *param, int inheritflag)
2067c478bd9Sstevel@tonic-gate {
2077c478bd9Sstevel@tonic-gate 	uberdata_t *udp = curthread->ul_uberdata;
2087c478bd9Sstevel@tonic-gate 	ulwp_t	*ulwp;
2097c478bd9Sstevel@tonic-gate 	int	error = 0;
2107c478bd9Sstevel@tonic-gate 	int	prio;
2117c478bd9Sstevel@tonic-gate 	int	opolicy;
2127c478bd9Sstevel@tonic-gate 	int	mappedprio;
2137c478bd9Sstevel@tonic-gate 	int	mapped = 0;
2147c478bd9Sstevel@tonic-gate 	pri_t	*mappedprip;
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	if (param == NULL)
2177c478bd9Sstevel@tonic-gate 		return (EINVAL);
2187c478bd9Sstevel@tonic-gate 	if ((ulwp = find_lwp(tid)) == NULL)
2197c478bd9Sstevel@tonic-gate 		return (ESRCH);
2207c478bd9Sstevel@tonic-gate 	prio = param->sched_priority;
2217c478bd9Sstevel@tonic-gate 	opolicy = ulwp->ul_policy;
2227c478bd9Sstevel@tonic-gate 	if (inheritflag == PRIO_SET_PRIO) {	/* don't change policy */
2237c478bd9Sstevel@tonic-gate 		policy = opolicy;
2247c478bd9Sstevel@tonic-gate 		inheritflag = PRIO_SET;
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 	ASSERT(inheritflag == PRIO_SET || opolicy == policy);
2277c478bd9Sstevel@tonic-gate 	if (inheritflag == PRIO_DISINHERIT) {
2287c478bd9Sstevel@tonic-gate 		ulwp->ul_emappedpri = 0;
2297c478bd9Sstevel@tonic-gate 		ulwp->ul_epri = 0;
2307c478bd9Sstevel@tonic-gate 		prio = ulwp->ul_pri;	/* ignore prio in sched_param */
2317c478bd9Sstevel@tonic-gate 	}
2327c478bd9Sstevel@tonic-gate 	if (policy == SCHED_OTHER) {
2337c478bd9Sstevel@tonic-gate 		/*
2347c478bd9Sstevel@tonic-gate 		 * Set thread's policy to OTHER
2357c478bd9Sstevel@tonic-gate 		 */
2367c478bd9Sstevel@tonic-gate 		if (prio < THREAD_MIN_PRIORITY || prio > THREAD_MAX_PRIORITY) {
2377c478bd9Sstevel@tonic-gate 			if (_validate_rt_prio(policy, prio)) {
2387c478bd9Sstevel@tonic-gate 				error = EINVAL;
2397c478bd9Sstevel@tonic-gate 				goto out;
2407c478bd9Sstevel@tonic-gate 			}
2417c478bd9Sstevel@tonic-gate 			mapped = 1;
2427c478bd9Sstevel@tonic-gate 			mappedprio = prio;
243f841f6adSraf 			prio = map_rtpri_to_gp(prio);
2447c478bd9Sstevel@tonic-gate 			ASSERT(prio >= THREAD_MIN_PRIORITY &&
2457c478bd9Sstevel@tonic-gate 			    prio <= THREAD_MAX_PRIORITY);
2467c478bd9Sstevel@tonic-gate 		}
2477c478bd9Sstevel@tonic-gate 		/*
2487c478bd9Sstevel@tonic-gate 		 * Thread changing from FIFO/RR to OTHER
2497c478bd9Sstevel@tonic-gate 		 */
2507c478bd9Sstevel@tonic-gate 		if (opolicy == SCHED_FIFO || opolicy == SCHED_RR) {
2517c478bd9Sstevel@tonic-gate 			if ((error = _thrp_setlwpprio(tid, policy, prio)) != 0)
2527c478bd9Sstevel@tonic-gate 				goto out;
2537c478bd9Sstevel@tonic-gate 		}
2547c478bd9Sstevel@tonic-gate 		if (inheritflag != PRIO_DISINHERIT) {
2557c478bd9Sstevel@tonic-gate 			if (inheritflag == PRIO_INHERIT)
2567c478bd9Sstevel@tonic-gate 				mappedprip = &ulwp->ul_emappedpri;
2577c478bd9Sstevel@tonic-gate 			else
2587c478bd9Sstevel@tonic-gate 				mappedprip = &ulwp->ul_mappedpri;
2597c478bd9Sstevel@tonic-gate 			if (mapped) {
2607c478bd9Sstevel@tonic-gate 				ulwp->ul_pri_mapped = 1;
2617c478bd9Sstevel@tonic-gate 				*mappedprip = mappedprio;
2627c478bd9Sstevel@tonic-gate 			} else {
2637c478bd9Sstevel@tonic-gate 				ulwp->ul_pri_mapped = 0;
2647c478bd9Sstevel@tonic-gate 				*mappedprip = 0;
2657c478bd9Sstevel@tonic-gate 			}
2667c478bd9Sstevel@tonic-gate 		}
2677c478bd9Sstevel@tonic-gate 		ulwp->ul_policy = policy;
2687c478bd9Sstevel@tonic-gate 		if (inheritflag == PRIO_INHERIT)
2697c478bd9Sstevel@tonic-gate 			ulwp->ul_epri = prio;
2707c478bd9Sstevel@tonic-gate 		else
2717c478bd9Sstevel@tonic-gate 			ulwp->ul_pri = prio;
2727c478bd9Sstevel@tonic-gate 	} else if (policy == SCHED_FIFO || policy == SCHED_RR) {
2737c478bd9Sstevel@tonic-gate 		if (_validate_rt_prio(policy, prio))
2747c478bd9Sstevel@tonic-gate 			error = EINVAL;
2757c478bd9Sstevel@tonic-gate 		else {
276*9acbbeafSnn 			int prio_err;
277*9acbbeafSnn 
2787c478bd9Sstevel@tonic-gate 			if (_private_geteuid() == 0 &&
279*9acbbeafSnn 			    (prio_err = _thrp_setlwpprio(tid, policy, prio))) {
280*9acbbeafSnn 				error = prio_err;
281*9acbbeafSnn 				goto out;
282*9acbbeafSnn 			}
283*9acbbeafSnn 
2847c478bd9Sstevel@tonic-gate 			ulwp->ul_policy = policy;
2857c478bd9Sstevel@tonic-gate 			if (inheritflag == PRIO_INHERIT)
2867c478bd9Sstevel@tonic-gate 				ulwp->ul_epri = prio;
2877c478bd9Sstevel@tonic-gate 			else
2887c478bd9Sstevel@tonic-gate 				ulwp->ul_pri = prio;
2897c478bd9Sstevel@tonic-gate 		}
2907c478bd9Sstevel@tonic-gate 	} else {
2917c478bd9Sstevel@tonic-gate 		error = EINVAL;
2927c478bd9Sstevel@tonic-gate 	}
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate out:
2957c478bd9Sstevel@tonic-gate 	ulwp_unlock(ulwp, udp);
2967c478bd9Sstevel@tonic-gate 	return (error);
2977c478bd9Sstevel@tonic-gate }
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate /*
3007c478bd9Sstevel@tonic-gate  * pthread_setschedparam: sets the sched parameters for a thread.
3017c478bd9Sstevel@tonic-gate  */
3027c478bd9Sstevel@tonic-gate #pragma weak	pthread_setschedparam		= _pthread_setschedparam
3037c478bd9Sstevel@tonic-gate int
3047c478bd9Sstevel@tonic-gate _pthread_setschedparam(pthread_t tid,
3057c478bd9Sstevel@tonic-gate 	int policy, const struct sched_param *param)
3067c478bd9Sstevel@tonic-gate {
3077c478bd9Sstevel@tonic-gate 	return (_thread_setschedparam_main(tid, policy, param, PRIO_SET));
3087c478bd9Sstevel@tonic-gate }
309