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