xref: /onnv-gate/usr/src/lib/libc/port/threads/pthread.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include "lint.h"
30*0Sstevel@tonic-gate #include "thr_uberdata.h"
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate /*
33*0Sstevel@tonic-gate  * pthread_once related data
34*0Sstevel@tonic-gate  * This structure is exported as pthread_once_t in pthread.h.
35*0Sstevel@tonic-gate  * We export only the size of this structure. so check
36*0Sstevel@tonic-gate  * pthread_once_t in pthread.h before making a change here.
37*0Sstevel@tonic-gate  */
38*0Sstevel@tonic-gate typedef struct  __once {
39*0Sstevel@tonic-gate 	mutex_t	mlock;
40*0Sstevel@tonic-gate 	union {
41*0Sstevel@tonic-gate 		uint32_t	pad32_flag[2];
42*0Sstevel@tonic-gate 		uint64_t	pad64_flag;
43*0Sstevel@tonic-gate 	} oflag;
44*0Sstevel@tonic-gate } __once_t;
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate #define	once_flag	oflag.pad32_flag[1]
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate /*
49*0Sstevel@tonic-gate  * Default attribute object for pthread_create() with NULL attr pointer.
50*0Sstevel@tonic-gate  * Note that the 'guardsize' field is initialized on the first call
51*0Sstevel@tonic-gate  * to pthread_create() with a NULL attr pointer.
52*0Sstevel@tonic-gate  */
53*0Sstevel@tonic-gate static thrattr_t _defattr =
54*0Sstevel@tonic-gate 	{0, NULL, PTHREAD_CREATE_JOINABLE, PTHREAD_SCOPE_PROCESS,
55*0Sstevel@tonic-gate 	0, SCHED_OTHER, PTHREAD_EXPLICIT_SCHED, 0};
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate /*
58*0Sstevel@tonic-gate  * pthread_create: creates a thread in the current process.
59*0Sstevel@tonic-gate  * calls common _thrp_create() after copying the attributes.
60*0Sstevel@tonic-gate  */
61*0Sstevel@tonic-gate #pragma weak	pthread_create			= _pthread_create
62*0Sstevel@tonic-gate int
63*0Sstevel@tonic-gate _pthread_create(pthread_t *thread, const pthread_attr_t *attr,
64*0Sstevel@tonic-gate 	void * (*start_routine)(void *), void *arg)
65*0Sstevel@tonic-gate {
66*0Sstevel@tonic-gate 	ulwp_t		*self = curthread;
67*0Sstevel@tonic-gate 	uberdata_t	*udp = self->ul_uberdata;
68*0Sstevel@tonic-gate 	long		flag;
69*0Sstevel@tonic-gate 	thrattr_t	*ap;
70*0Sstevel@tonic-gate 	pthread_t	tid;
71*0Sstevel@tonic-gate 	int		policy;
72*0Sstevel@tonic-gate 	pri_t		priority;
73*0Sstevel@tonic-gate 	int		error;
74*0Sstevel@tonic-gate 	int		mapped = 0;
75*0Sstevel@tonic-gate 	int		mappedpri;
76*0Sstevel@tonic-gate 	int		rt = 0;
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate 	if (attr == NULL) {
79*0Sstevel@tonic-gate 		ap = &_defattr;
80*0Sstevel@tonic-gate 		if (ap->guardsize == 0) {
81*0Sstevel@tonic-gate 			if (_lpagesize == 0)
82*0Sstevel@tonic-gate 				_lpagesize = _sysconf(_SC_PAGESIZE);
83*0Sstevel@tonic-gate 			ap->guardsize = _lpagesize;
84*0Sstevel@tonic-gate 		}
85*0Sstevel@tonic-gate 	} else if ((ap = attr->__pthread_attrp) == NULL) {
86*0Sstevel@tonic-gate 		return (EINVAL);
87*0Sstevel@tonic-gate 	}
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate 	if (ap->inherit == PTHREAD_INHERIT_SCHED) {
90*0Sstevel@tonic-gate 		policy = self->ul_policy;
91*0Sstevel@tonic-gate 		priority = self->ul_pri;
92*0Sstevel@tonic-gate 		mapped = self->ul_pri_mapped;
93*0Sstevel@tonic-gate 		mappedpri = self->ul_mappedpri;
94*0Sstevel@tonic-gate 	} else {
95*0Sstevel@tonic-gate 		policy = ap->policy;
96*0Sstevel@tonic-gate 		priority = ap->prio;
97*0Sstevel@tonic-gate 		if (policy == SCHED_OTHER) {
98*0Sstevel@tonic-gate 			if (priority < THREAD_MIN_PRIORITY ||
99*0Sstevel@tonic-gate 			    priority > THREAD_MAX_PRIORITY) {
100*0Sstevel@tonic-gate 				if (_validate_rt_prio(policy, priority))
101*0Sstevel@tonic-gate 					return (EINVAL);
102*0Sstevel@tonic-gate 				mapped = 1;
103*0Sstevel@tonic-gate 				mappedpri = priority;
104*0Sstevel@tonic-gate 				priority = _map_rtpri_to_gp(priority);
105*0Sstevel@tonic-gate 				ASSERT(priority >= THREAD_MIN_PRIORITY &&
106*0Sstevel@tonic-gate 				    priority <= THREAD_MAX_PRIORITY);
107*0Sstevel@tonic-gate 			}
108*0Sstevel@tonic-gate 		} else if (policy == SCHED_FIFO || policy == SCHED_RR) {
109*0Sstevel@tonic-gate 			if (_validate_rt_prio(policy, priority))
110*0Sstevel@tonic-gate 				return (EINVAL);
111*0Sstevel@tonic-gate 			if (_private_geteuid() == 0)
112*0Sstevel@tonic-gate 				rt = 1;
113*0Sstevel@tonic-gate 		} else {
114*0Sstevel@tonic-gate 			return (EINVAL);
115*0Sstevel@tonic-gate 		}
116*0Sstevel@tonic-gate 	}
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate 	flag = ap->scope | ap->detachstate | THR_SUSPENDED;
119*0Sstevel@tonic-gate 	error = _thrp_create(ap->stkaddr, ap->stksize, start_routine, arg,
120*0Sstevel@tonic-gate 		flag, &tid, priority, policy, ap->guardsize);
121*0Sstevel@tonic-gate 	if (error == 0) {
122*0Sstevel@tonic-gate 		if (mapped) {
123*0Sstevel@tonic-gate 			ulwp_t *ulwp = find_lwp(tid);
124*0Sstevel@tonic-gate 			ulwp->ul_pri_mapped = 1;
125*0Sstevel@tonic-gate 			ulwp->ul_mappedpri = mappedpri;
126*0Sstevel@tonic-gate 			ulwp_unlock(ulwp, udp);
127*0Sstevel@tonic-gate 		}
128*0Sstevel@tonic-gate 		if (rt && _thrp_setlwpprio(tid, policy, priority))
129*0Sstevel@tonic-gate 			thr_panic("_thrp_setlwpprio() failed");
130*0Sstevel@tonic-gate 		if (thread)
131*0Sstevel@tonic-gate 			*thread = tid;
132*0Sstevel@tonic-gate 		(void) _thr_continue(tid);
133*0Sstevel@tonic-gate 	}
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate 	/* posix version expects EAGAIN for lack of memory */
136*0Sstevel@tonic-gate 	if (error == ENOMEM)
137*0Sstevel@tonic-gate 		error = EAGAIN;
138*0Sstevel@tonic-gate 	return (error);
139*0Sstevel@tonic-gate }
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate /*
142*0Sstevel@tonic-gate  * pthread_once: calls given function only once.
143*0Sstevel@tonic-gate  * it synchronizes via mutex in pthread_once_t structure
144*0Sstevel@tonic-gate  */
145*0Sstevel@tonic-gate #pragma weak	pthread_once			= _pthread_once
146*0Sstevel@tonic-gate int
147*0Sstevel@tonic-gate _pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
148*0Sstevel@tonic-gate {
149*0Sstevel@tonic-gate 	__once_t *once = (__once_t *)once_control;
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 	if (once == NULL || init_routine == NULL)
152*0Sstevel@tonic-gate 		return (EINVAL);
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate 	if (once->once_flag == PTHREAD_ONCE_NOTDONE) {
155*0Sstevel@tonic-gate 		(void) _private_mutex_lock(&once->mlock);
156*0Sstevel@tonic-gate 		if (once->once_flag == PTHREAD_ONCE_NOTDONE) {
157*0Sstevel@tonic-gate 			pthread_cleanup_push(_private_mutex_unlock,
158*0Sstevel@tonic-gate 			    &once->mlock);
159*0Sstevel@tonic-gate 			(*init_routine)();
160*0Sstevel@tonic-gate 			pthread_cleanup_pop(0);
161*0Sstevel@tonic-gate 			once->once_flag = PTHREAD_ONCE_DONE;
162*0Sstevel@tonic-gate 		}
163*0Sstevel@tonic-gate 		(void) _private_mutex_unlock(&once->mlock);
164*0Sstevel@tonic-gate 	}
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate 	return (0);
167*0Sstevel@tonic-gate }
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate /*
170*0Sstevel@tonic-gate  * pthread_equal: equates two thread ids.
171*0Sstevel@tonic-gate  */
172*0Sstevel@tonic-gate #pragma weak	pthread_equal			= _pthread_equal
173*0Sstevel@tonic-gate int
174*0Sstevel@tonic-gate _pthread_equal(pthread_t t1, pthread_t t2)
175*0Sstevel@tonic-gate {
176*0Sstevel@tonic-gate 	return (t1 == t2);
177*0Sstevel@tonic-gate }
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate /*
180*0Sstevel@tonic-gate  * pthread_getschedparam: gets the sched parameters in a struct.
181*0Sstevel@tonic-gate  */
182*0Sstevel@tonic-gate #pragma weak	pthread_getschedparam		= _pthread_getschedparam
183*0Sstevel@tonic-gate int
184*0Sstevel@tonic-gate _pthread_getschedparam(pthread_t tid, int *policy, struct sched_param *param)
185*0Sstevel@tonic-gate {
186*0Sstevel@tonic-gate 	uberdata_t *udp = curthread->ul_uberdata;
187*0Sstevel@tonic-gate 	ulwp_t *ulwp;
188*0Sstevel@tonic-gate 	int error = 0;
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate 	if (param == NULL || policy == NULL)
191*0Sstevel@tonic-gate 		error = EINVAL;
192*0Sstevel@tonic-gate 	else if ((ulwp = find_lwp(tid)) == NULL)
193*0Sstevel@tonic-gate 		error = ESRCH;
194*0Sstevel@tonic-gate 	else {
195*0Sstevel@tonic-gate 		if (ulwp->ul_pri_mapped)
196*0Sstevel@tonic-gate 			param->sched_priority = ulwp->ul_mappedpri;
197*0Sstevel@tonic-gate 		else
198*0Sstevel@tonic-gate 			param->sched_priority = ulwp->ul_pri;
199*0Sstevel@tonic-gate 		*policy = ulwp->ul_policy;
200*0Sstevel@tonic-gate 		ulwp_unlock(ulwp, udp);
201*0Sstevel@tonic-gate 	}
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate 	return (error);
204*0Sstevel@tonic-gate }
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate /*
207*0Sstevel@tonic-gate  * Besides the obvious arguments, the inheritflag needs to be explained:
208*0Sstevel@tonic-gate  * If set to PRIO_SET or PRIO_SET_PRIO, it does the normal, expected work
209*0Sstevel@tonic-gate  * of setting thread's assigned scheduling parameters and policy.
210*0Sstevel@tonic-gate  * If set to PRIO_INHERIT, it sets the thread's effective priority values
211*0Sstevel@tonic-gate  * (t_epri, t_empappedpri), and does not update the assigned priority values
212*0Sstevel@tonic-gate  * (t_pri, t_mappedpri).  If set to PRIO_DISINHERIT, it clears the thread's
213*0Sstevel@tonic-gate  * effective priority values, and reverts the thread, if necessary, back
214*0Sstevel@tonic-gate  * to the assigned priority values.
215*0Sstevel@tonic-gate  */
216*0Sstevel@tonic-gate int
217*0Sstevel@tonic-gate _thread_setschedparam_main(pthread_t tid, int policy,
218*0Sstevel@tonic-gate     const struct sched_param *param, int inheritflag)
219*0Sstevel@tonic-gate {
220*0Sstevel@tonic-gate 	uberdata_t *udp = curthread->ul_uberdata;
221*0Sstevel@tonic-gate 	ulwp_t	*ulwp;
222*0Sstevel@tonic-gate 	int	error = 0;
223*0Sstevel@tonic-gate 	int	prio;
224*0Sstevel@tonic-gate 	int	opolicy;
225*0Sstevel@tonic-gate 	int	mappedprio;
226*0Sstevel@tonic-gate 	int	mapped = 0;
227*0Sstevel@tonic-gate 	pri_t	*mappedprip;
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate 	if (param == NULL)
230*0Sstevel@tonic-gate 		return (EINVAL);
231*0Sstevel@tonic-gate 	if ((ulwp = find_lwp(tid)) == NULL)
232*0Sstevel@tonic-gate 		return (ESRCH);
233*0Sstevel@tonic-gate 	prio = param->sched_priority;
234*0Sstevel@tonic-gate 	opolicy = ulwp->ul_policy;
235*0Sstevel@tonic-gate 	if (inheritflag == PRIO_SET_PRIO) {	/* don't change policy */
236*0Sstevel@tonic-gate 		policy = opolicy;
237*0Sstevel@tonic-gate 		inheritflag = PRIO_SET;
238*0Sstevel@tonic-gate 	}
239*0Sstevel@tonic-gate 	ASSERT(inheritflag == PRIO_SET || opolicy == policy);
240*0Sstevel@tonic-gate 	if (inheritflag == PRIO_DISINHERIT) {
241*0Sstevel@tonic-gate 		ulwp->ul_emappedpri = 0;
242*0Sstevel@tonic-gate 		ulwp->ul_epri = 0;
243*0Sstevel@tonic-gate 		prio = ulwp->ul_pri;	/* ignore prio in sched_param */
244*0Sstevel@tonic-gate 	}
245*0Sstevel@tonic-gate 	if (policy == SCHED_OTHER) {
246*0Sstevel@tonic-gate 		/*
247*0Sstevel@tonic-gate 		 * Set thread's policy to OTHER
248*0Sstevel@tonic-gate 		 */
249*0Sstevel@tonic-gate 		if (prio < THREAD_MIN_PRIORITY || prio > THREAD_MAX_PRIORITY) {
250*0Sstevel@tonic-gate 			if (_validate_rt_prio(policy, prio)) {
251*0Sstevel@tonic-gate 				error = EINVAL;
252*0Sstevel@tonic-gate 				goto out;
253*0Sstevel@tonic-gate 			}
254*0Sstevel@tonic-gate 			mapped = 1;
255*0Sstevel@tonic-gate 			mappedprio = prio;
256*0Sstevel@tonic-gate 			prio = _map_rtpri_to_gp(prio);
257*0Sstevel@tonic-gate 			ASSERT(prio >= THREAD_MIN_PRIORITY &&
258*0Sstevel@tonic-gate 			    prio <= THREAD_MAX_PRIORITY);
259*0Sstevel@tonic-gate 		}
260*0Sstevel@tonic-gate 		/*
261*0Sstevel@tonic-gate 		 * Thread changing from FIFO/RR to OTHER
262*0Sstevel@tonic-gate 		 */
263*0Sstevel@tonic-gate 		if (opolicy == SCHED_FIFO || opolicy == SCHED_RR) {
264*0Sstevel@tonic-gate 			if ((error = _thrp_setlwpprio(tid, policy, prio)) != 0)
265*0Sstevel@tonic-gate 				goto out;
266*0Sstevel@tonic-gate 		}
267*0Sstevel@tonic-gate 		if (inheritflag != PRIO_DISINHERIT) {
268*0Sstevel@tonic-gate 			if (inheritflag == PRIO_INHERIT)
269*0Sstevel@tonic-gate 				mappedprip = &ulwp->ul_emappedpri;
270*0Sstevel@tonic-gate 			else
271*0Sstevel@tonic-gate 				mappedprip = &ulwp->ul_mappedpri;
272*0Sstevel@tonic-gate 			if (mapped) {
273*0Sstevel@tonic-gate 				ulwp->ul_pri_mapped = 1;
274*0Sstevel@tonic-gate 				*mappedprip = mappedprio;
275*0Sstevel@tonic-gate 			} else {
276*0Sstevel@tonic-gate 				ulwp->ul_pri_mapped = 0;
277*0Sstevel@tonic-gate 				*mappedprip = 0;
278*0Sstevel@tonic-gate 			}
279*0Sstevel@tonic-gate 		}
280*0Sstevel@tonic-gate 		ulwp->ul_policy = policy;
281*0Sstevel@tonic-gate 		if (inheritflag == PRIO_INHERIT)
282*0Sstevel@tonic-gate 			ulwp->ul_epri = prio;
283*0Sstevel@tonic-gate 		else
284*0Sstevel@tonic-gate 			ulwp->ul_pri = prio;
285*0Sstevel@tonic-gate 	} else if (policy == SCHED_FIFO || policy == SCHED_RR) {
286*0Sstevel@tonic-gate 		if (_validate_rt_prio(policy, prio))
287*0Sstevel@tonic-gate 			error = EINVAL;
288*0Sstevel@tonic-gate 		else {
289*0Sstevel@tonic-gate 			if (_private_geteuid() == 0 &&
290*0Sstevel@tonic-gate 			    _thrp_setlwpprio(tid, policy, prio))
291*0Sstevel@tonic-gate 				thr_panic("_thrp_setlwpprio failed");
292*0Sstevel@tonic-gate 			ulwp->ul_policy = policy;
293*0Sstevel@tonic-gate 			if (inheritflag == PRIO_INHERIT)
294*0Sstevel@tonic-gate 				ulwp->ul_epri = prio;
295*0Sstevel@tonic-gate 			else
296*0Sstevel@tonic-gate 				ulwp->ul_pri = prio;
297*0Sstevel@tonic-gate 		}
298*0Sstevel@tonic-gate 	} else {
299*0Sstevel@tonic-gate 		error = EINVAL;
300*0Sstevel@tonic-gate 	}
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate out:
303*0Sstevel@tonic-gate 	ulwp_unlock(ulwp, udp);
304*0Sstevel@tonic-gate 	return (error);
305*0Sstevel@tonic-gate }
306*0Sstevel@tonic-gate 
307*0Sstevel@tonic-gate /*
308*0Sstevel@tonic-gate  * pthread_setschedparam: sets the sched parameters for a thread.
309*0Sstevel@tonic-gate  */
310*0Sstevel@tonic-gate #pragma weak	pthread_setschedparam		= _pthread_setschedparam
311*0Sstevel@tonic-gate int
312*0Sstevel@tonic-gate _pthread_setschedparam(pthread_t tid,
313*0Sstevel@tonic-gate 	int policy, const struct sched_param *param)
314*0Sstevel@tonic-gate {
315*0Sstevel@tonic-gate 	return (_thread_setschedparam_main(tid, policy, param, PRIO_SET));
316*0Sstevel@tonic-gate }
317