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