xref: /titanic_44/usr/src/lib/libc/port/threads/pthread.c (revision 8bd1bae7b80c19127d25a15440e161140bcaacea)
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 2008 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 static int
49 _thr_setparam(pthread_t tid, int policy, int prio)
50 {
51 	ulwp_t *ulwp;
52 	id_t cid;
53 	int error = 0;
54 
55 	if ((ulwp = find_lwp(tid)) == NULL) {
56 		error = ESRCH;
57 	} else {
58 		if (policy == ulwp->ul_policy &&
59 		    (policy == SCHED_FIFO || policy == SCHED_RR) &&
60 		    ulwp->ul_cid == ulwp->ul_rtclassid &&
61 		    ulwp->ul_epri != 0) {
62 			/*
63 			 * Don't change the ceiling priority,
64 			 * just the base priority.
65 			 */
66 			if (prio > ulwp->ul_epri)
67 				error = EPERM;
68 			else
69 				ulwp->ul_pri = prio;
70 		} else if ((cid = setparam(P_LWPID, tid, policy, prio)) == -1) {
71 			error = errno;
72 		} else {
73 			ulwp->ul_cid = cid;
74 			ulwp->ul_pri = prio;
75 			_membar_producer();
76 			ulwp->ul_policy = policy;
77 		}
78 		ulwp_unlock(ulwp, curthread->ul_uberdata);
79 	}
80 	return (error);
81 }
82 
83 /*
84  * pthread_create: creates a thread in the current process.
85  * calls common _thrp_create() after copying the attributes.
86  */
87 #pragma weak	pthread_create			= _pthread_create
88 int
89 _pthread_create(pthread_t *thread, const pthread_attr_t *attr,
90 	void * (*start_routine)(void *), void *arg)
91 {
92 	ulwp_t		*self = curthread;
93 	const thrattr_t	*ap = attr? attr->__pthread_attrp : def_thrattr();
94 	const pcclass_t	*pccp;
95 	long		flag;
96 	pthread_t	tid;
97 	int		error;
98 
99 	update_sched(self);
100 
101 	if (ap == NULL)
102 		return (EINVAL);
103 
104 	/* validate explicit scheduling attributes */
105 	if (ap->inherit == PTHREAD_EXPLICIT_SCHED &&
106 	    (ap->policy == SCHED_SYS ||
107 	    (pccp = get_info_by_policy(ap->policy)) == NULL ||
108 	    ap->prio < pccp->pcc_primin || ap->prio > pccp->pcc_primax))
109 		return (EINVAL);
110 
111 	flag = ap->scope | ap->detachstate | ap->daemonstate | THR_SUSPENDED;
112 	error = _thrp_create(ap->stkaddr, ap->stksize, start_routine, arg,
113 	    flag, &tid, ap->guardsize);
114 	if (error == 0) {
115 		if (ap->inherit == PTHREAD_EXPLICIT_SCHED &&
116 		    (ap->policy != self->ul_policy ||
117 		    ap->prio != (self->ul_epri? self->ul_epri : self->ul_pri)))
118 			/*
119 			 * The SUSv3 specification requires pthread_create()
120 			 * to fail with EPERM if it cannot set the scheduling
121 			 * policy and parameters on the new thread.
122 			 */
123 			error = _thr_setparam(tid, ap->policy, ap->prio);
124 		if (error) {
125 			/*
126 			 * We couldn't determine this error before
127 			 * actually creating the thread.  To recover,
128 			 * mark the thread detached and cancel it.
129 			 * It is as though it was never created.
130 			 */
131 			ulwp_t *ulwp = find_lwp(tid);
132 			if (ulwp->ul_detached == 0) {
133 				ulwp->ul_detached = 1;
134 				ulwp->ul_usropts |= THR_DETACHED;
135 				(void) __lwp_detach(tid);
136 			}
137 			ulwp->ul_cancel_pending = 2; /* cancelled on creation */
138 			ulwp->ul_cancel_disabled = 0;
139 			ulwp_unlock(ulwp, self->ul_uberdata);
140 		} else if (thread) {
141 			*thread = tid;
142 		}
143 		(void) _thr_continue(tid);
144 	}
145 
146 	/* posix version expects EAGAIN for lack of memory */
147 	if (error == ENOMEM)
148 		error = EAGAIN;
149 	return (error);
150 }
151 
152 /*
153  * pthread_once: calls given function only once.
154  * it synchronizes via mutex in pthread_once_t structure
155  */
156 #pragma weak	pthread_once			= _pthread_once
157 int
158 _pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
159 {
160 	__once_t *once = (__once_t *)once_control;
161 
162 	if (once == NULL || init_routine == NULL)
163 		return (EINVAL);
164 
165 	if (once->once_flag == PTHREAD_ONCE_NOTDONE) {
166 		(void) _private_mutex_lock(&once->mlock);
167 		if (once->once_flag == PTHREAD_ONCE_NOTDONE) {
168 			pthread_cleanup_push(_private_mutex_unlock,
169 			    &once->mlock);
170 			(*init_routine)();
171 			pthread_cleanup_pop(0);
172 			_membar_producer();
173 			once->once_flag = PTHREAD_ONCE_DONE;
174 		}
175 		(void) _private_mutex_unlock(&once->mlock);
176 	}
177 	_membar_consumer();
178 
179 	return (0);
180 }
181 
182 /*
183  * pthread_equal: equates two thread ids.
184  */
185 #pragma weak	pthread_equal			= _pthread_equal
186 int
187 _pthread_equal(pthread_t t1, pthread_t t2)
188 {
189 	return (t1 == t2);
190 }
191 
192 /*
193  * pthread_getschedparam: get the thread's sched parameters.
194  */
195 #pragma weak	pthread_getschedparam		= _pthread_getschedparam
196 int
197 _pthread_getschedparam(pthread_t tid, int *policy, struct sched_param *param)
198 {
199 	ulwp_t *ulwp;
200 	id_t cid;
201 	int error = 0;
202 
203 	if ((ulwp = find_lwp(tid)) == NULL) {
204 		error = ESRCH;
205 	} else {
206 		cid = getparam(P_LWPID, ulwp->ul_lwpid, policy, param);
207 		if (cid == -1) {
208 			error = errno;
209 		} else if (*policy == ulwp->ul_policy && cid == ulwp->ul_cid &&
210 		    (*policy == SCHED_FIFO || *policy == SCHED_RR)) {
211 			/*
212 			 * Return the defined priority, not the effective
213 			 * priority from priority ceiling mutexes.
214 			 */
215 			param->sched_priority = ulwp->ul_pri;
216 		} else {
217 			ulwp->ul_cid = cid;
218 			ulwp->ul_pri = param->sched_priority;
219 			_membar_producer();
220 			ulwp->ul_policy = *policy;
221 		}
222 		ulwp_unlock(ulwp, curthread->ul_uberdata);
223 	}
224 
225 	return (error);
226 }
227 
228 #pragma weak thr_getprio = _thr_getprio
229 int
230 _thr_getprio(thread_t tid, int *priority)
231 {
232 	struct sched_param param;
233 	int policy;
234 	int error;
235 
236 	if ((error = _pthread_getschedparam(tid, &policy, &param)) == 0)
237 		*priority = param.sched_priority;
238 	return (error);
239 }
240 
241 /*
242  * pthread_setschedparam: sets the sched parameters for a thread.
243  */
244 #pragma weak	pthread_setschedparam		= _pthread_setschedparam
245 int
246 _pthread_setschedparam(pthread_t tid,
247 	int policy, const struct sched_param *param)
248 {
249 	return (_thr_setparam(tid, policy, param->sched_priority));
250 }
251 
252 #pragma weak thr_setprio = _thr_setprio
253 #pragma weak pthread_setschedprio = _thr_setprio
254 #pragma weak _pthread_setschedprio = _thr_setprio
255 int
256 _thr_setprio(thread_t tid, int prio)
257 {
258 	struct sched_param param;
259 	int policy;
260 	int error;
261 
262 	/*
263 	 * _pthread_getschedparam() has the side-effect of setting
264 	 * the target thread's ul_policy, ul_pri and ul_cid correctly.
265 	 */
266 	if ((error = _pthread_getschedparam(tid, &policy, &param)) != 0)
267 		return (error);
268 	if (param.sched_priority == prio)	/* no change */
269 		return (0);
270 	return (_thr_setparam(tid, policy, prio));
271 }
272