xref: /onnv-gate/usr/src/lib/libc/port/threads/pthread.c (revision 6247:ad4c702ff226)
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*6247Sraf  * 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 
48*6247Sraf static int
49*6247Sraf _thr_setparam(pthread_t tid, int policy, int prio)
50*6247Sraf {
51*6247Sraf 	ulwp_t *ulwp;
52*6247Sraf 	id_t cid;
53*6247Sraf 	int error = 0;
54*6247Sraf 
55*6247Sraf 	if ((ulwp = find_lwp(tid)) == NULL) {
56*6247Sraf 		error = ESRCH;
57*6247Sraf 	} else {
58*6247Sraf 		if (policy == ulwp->ul_policy &&
59*6247Sraf 		    (policy == SCHED_FIFO || policy == SCHED_RR) &&
60*6247Sraf 		    ulwp->ul_cid == ulwp->ul_rtclassid &&
61*6247Sraf 		    ulwp->ul_epri != 0) {
62*6247Sraf 			/*
63*6247Sraf 			 * Don't change the ceiling priority,
64*6247Sraf 			 * just the base priority.
65*6247Sraf 			 */
66*6247Sraf 			if (prio > ulwp->ul_epri)
67*6247Sraf 				error = EPERM;
68*6247Sraf 			else
69*6247Sraf 				ulwp->ul_pri = prio;
70*6247Sraf 		} else if ((cid = setparam(P_LWPID, tid, policy, prio)) == -1) {
71*6247Sraf 			error = errno;
72*6247Sraf 		} else {
73*6247Sraf 			ulwp->ul_cid = cid;
74*6247Sraf 			ulwp->ul_pri = prio;
75*6247Sraf 			_membar_producer();
76*6247Sraf 			ulwp->ul_policy = policy;
77*6247Sraf 		}
78*6247Sraf 		ulwp_unlock(ulwp, curthread->ul_uberdata);
79*6247Sraf 	}
80*6247Sraf 	return (error);
81*6247Sraf }
82*6247Sraf 
830Sstevel@tonic-gate /*
840Sstevel@tonic-gate  * pthread_create: creates a thread in the current process.
850Sstevel@tonic-gate  * calls common _thrp_create() after copying the attributes.
860Sstevel@tonic-gate  */
870Sstevel@tonic-gate #pragma weak	pthread_create			= _pthread_create
880Sstevel@tonic-gate int
890Sstevel@tonic-gate _pthread_create(pthread_t *thread, const pthread_attr_t *attr,
900Sstevel@tonic-gate 	void * (*start_routine)(void *), void *arg)
910Sstevel@tonic-gate {
920Sstevel@tonic-gate 	ulwp_t		*self = curthread;
931885Sraf 	const thrattr_t	*ap = attr? attr->__pthread_attrp : def_thrattr();
94*6247Sraf 	const pcclass_t	*pccp;
950Sstevel@tonic-gate 	long		flag;
960Sstevel@tonic-gate 	pthread_t	tid;
970Sstevel@tonic-gate 	int		error;
98*6247Sraf 
99*6247Sraf 	update_sched(self);
1000Sstevel@tonic-gate 
1011885Sraf 	if (ap == NULL)
1020Sstevel@tonic-gate 		return (EINVAL);
1030Sstevel@tonic-gate 
104*6247Sraf 	/* validate explicit scheduling attributes */
105*6247Sraf 	if (ap->inherit == PTHREAD_EXPLICIT_SCHED &&
106*6247Sraf 	    (ap->policy == SCHED_SYS ||
107*6247Sraf 	    (pccp = get_info_by_policy(ap->policy)) == NULL ||
108*6247Sraf 	    ap->prio < pccp->pcc_primin || ap->prio > pccp->pcc_primax))
109*6247Sraf 		return (EINVAL);
1100Sstevel@tonic-gate 
1111885Sraf 	flag = ap->scope | ap->detachstate | ap->daemonstate | THR_SUSPENDED;
1120Sstevel@tonic-gate 	error = _thrp_create(ap->stkaddr, ap->stksize, start_routine, arg,
113*6247Sraf 	    flag, &tid, ap->guardsize);
1140Sstevel@tonic-gate 	if (error == 0) {
115*6247Sraf 		if (ap->inherit == PTHREAD_EXPLICIT_SCHED &&
116*6247Sraf 		    (ap->policy != self->ul_policy ||
117*6247Sraf 		    ap->prio != (self->ul_epri? self->ul_epri : self->ul_pri)))
118*6247Sraf 			/*
119*6247Sraf 			 * The SUSv3 specification requires pthread_create()
120*6247Sraf 			 * to fail with EPERM if it cannot set the scheduling
121*6247Sraf 			 * policy and parameters on the new thread.
122*6247Sraf 			 */
123*6247Sraf 			error = _thr_setparam(tid, ap->policy, ap->prio);
124*6247Sraf 		if (error) {
125*6247Sraf 			/*
126*6247Sraf 			 * We couldn't determine this error before
127*6247Sraf 			 * actually creating the thread.  To recover,
128*6247Sraf 			 * mark the thread detached and cancel it.
129*6247Sraf 			 * It is as though it was never created.
130*6247Sraf 			 */
1310Sstevel@tonic-gate 			ulwp_t *ulwp = find_lwp(tid);
132*6247Sraf 			if (ulwp->ul_detached == 0) {
133*6247Sraf 				ulwp->ul_detached = 1;
134*6247Sraf 				ulwp->ul_usropts |= THR_DETACHED;
135*6247Sraf 				(void) __lwp_detach(tid);
136*6247Sraf 			}
137*6247Sraf 			ulwp->ul_cancel_pending = 2; /* cancelled on creation */
138*6247Sraf 			ulwp->ul_cancel_disabled = 0;
139*6247Sraf 			ulwp_unlock(ulwp, self->ul_uberdata);
140*6247Sraf 		} else if (thread) {
141*6247Sraf 			*thread = tid;
1420Sstevel@tonic-gate 		}
1430Sstevel@tonic-gate 		(void) _thr_continue(tid);
1440Sstevel@tonic-gate 	}
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	/* posix version expects EAGAIN for lack of memory */
1470Sstevel@tonic-gate 	if (error == ENOMEM)
1480Sstevel@tonic-gate 		error = EAGAIN;
1490Sstevel@tonic-gate 	return (error);
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate /*
1530Sstevel@tonic-gate  * pthread_once: calls given function only once.
1540Sstevel@tonic-gate  * it synchronizes via mutex in pthread_once_t structure
1550Sstevel@tonic-gate  */
1560Sstevel@tonic-gate #pragma weak	pthread_once			= _pthread_once
1570Sstevel@tonic-gate int
1580Sstevel@tonic-gate _pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
1590Sstevel@tonic-gate {
1600Sstevel@tonic-gate 	__once_t *once = (__once_t *)once_control;
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	if (once == NULL || init_routine == NULL)
1630Sstevel@tonic-gate 		return (EINVAL);
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	if (once->once_flag == PTHREAD_ONCE_NOTDONE) {
1660Sstevel@tonic-gate 		(void) _private_mutex_lock(&once->mlock);
1670Sstevel@tonic-gate 		if (once->once_flag == PTHREAD_ONCE_NOTDONE) {
1680Sstevel@tonic-gate 			pthread_cleanup_push(_private_mutex_unlock,
1690Sstevel@tonic-gate 			    &once->mlock);
1700Sstevel@tonic-gate 			(*init_routine)();
1710Sstevel@tonic-gate 			pthread_cleanup_pop(0);
1723864Sraf 			_membar_producer();
1730Sstevel@tonic-gate 			once->once_flag = PTHREAD_ONCE_DONE;
1740Sstevel@tonic-gate 		}
1750Sstevel@tonic-gate 		(void) _private_mutex_unlock(&once->mlock);
1760Sstevel@tonic-gate 	}
1773864Sraf 	_membar_consumer();
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	return (0);
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate /*
1830Sstevel@tonic-gate  * pthread_equal: equates two thread ids.
1840Sstevel@tonic-gate  */
1850Sstevel@tonic-gate #pragma weak	pthread_equal			= _pthread_equal
1860Sstevel@tonic-gate int
1870Sstevel@tonic-gate _pthread_equal(pthread_t t1, pthread_t t2)
1880Sstevel@tonic-gate {
1890Sstevel@tonic-gate 	return (t1 == t2);
1900Sstevel@tonic-gate }
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate /*
193*6247Sraf  * pthread_getschedparam: get the thread's sched parameters.
1940Sstevel@tonic-gate  */
1950Sstevel@tonic-gate #pragma weak	pthread_getschedparam		= _pthread_getschedparam
1960Sstevel@tonic-gate int
1970Sstevel@tonic-gate _pthread_getschedparam(pthread_t tid, int *policy, struct sched_param *param)
1980Sstevel@tonic-gate {
1990Sstevel@tonic-gate 	ulwp_t *ulwp;
200*6247Sraf 	id_t cid;
2010Sstevel@tonic-gate 	int error = 0;
2020Sstevel@tonic-gate 
203*6247Sraf 	if ((ulwp = find_lwp(tid)) == NULL) {
2040Sstevel@tonic-gate 		error = ESRCH;
205*6247Sraf 	} else {
206*6247Sraf 		cid = getparam(P_LWPID, ulwp->ul_lwpid, policy, param);
207*6247Sraf 		if (cid == -1) {
208*6247Sraf 			error = errno;
209*6247Sraf 		} else if (*policy == ulwp->ul_policy && cid == ulwp->ul_cid &&
210*6247Sraf 		    (*policy == SCHED_FIFO || *policy == SCHED_RR)) {
211*6247Sraf 			/*
212*6247Sraf 			 * Return the defined priority, not the effective
213*6247Sraf 			 * priority from priority ceiling mutexes.
214*6247Sraf 			 */
2150Sstevel@tonic-gate 			param->sched_priority = ulwp->ul_pri;
216*6247Sraf 		} else {
217*6247Sraf 			ulwp->ul_cid = cid;
218*6247Sraf 			ulwp->ul_pri = param->sched_priority;
219*6247Sraf 			_membar_producer();
220*6247Sraf 			ulwp->ul_policy = *policy;
221*6247Sraf 		}
222*6247Sraf 		ulwp_unlock(ulwp, curthread->ul_uberdata);
2230Sstevel@tonic-gate 	}
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	return (error);
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate 
228*6247Sraf #pragma weak thr_getprio = _thr_getprio
2290Sstevel@tonic-gate int
230*6247Sraf _thr_getprio(thread_t tid, int *priority)
2310Sstevel@tonic-gate {
232*6247Sraf 	struct sched_param param;
233*6247Sraf 	int policy;
234*6247Sraf 	int error;
2350Sstevel@tonic-gate 
236*6247Sraf 	if ((error = _pthread_getschedparam(tid, &policy, &param)) == 0)
237*6247Sraf 		*priority = param.sched_priority;
2380Sstevel@tonic-gate 	return (error);
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate /*
2420Sstevel@tonic-gate  * pthread_setschedparam: sets the sched parameters for a thread.
2430Sstevel@tonic-gate  */
2440Sstevel@tonic-gate #pragma weak	pthread_setschedparam		= _pthread_setschedparam
2450Sstevel@tonic-gate int
2460Sstevel@tonic-gate _pthread_setschedparam(pthread_t tid,
2470Sstevel@tonic-gate 	int policy, const struct sched_param *param)
2480Sstevel@tonic-gate {
249*6247Sraf 	return (_thr_setparam(tid, policy, param->sched_priority));
2500Sstevel@tonic-gate }
251*6247Sraf 
252*6247Sraf #pragma weak thr_setprio = _thr_setprio
253*6247Sraf #pragma weak pthread_setschedprio = _thr_setprio
254*6247Sraf #pragma weak _pthread_setschedprio = _thr_setprio
255*6247Sraf int
256*6247Sraf _thr_setprio(thread_t tid, int prio)
257*6247Sraf {
258*6247Sraf 	struct sched_param param;
259*6247Sraf 	int policy;
260*6247Sraf 	int error;
261*6247Sraf 
262*6247Sraf 	/*
263*6247Sraf 	 * _pthread_getschedparam() has the side-effect of setting
264*6247Sraf 	 * the target thread's ul_policy, ul_pri and ul_cid correctly.
265*6247Sraf 	 */
266*6247Sraf 	if ((error = _pthread_getschedparam(tid, &policy, &param)) != 0)
267*6247Sraf 		return (error);
268*6247Sraf 	if (param.sched_priority == prio)	/* no change */
269*6247Sraf 		return (0);
270*6247Sraf 	return (_thr_setparam(tid, policy, prio));
271*6247Sraf }
272