blob: 34b4b4c73c51d1f30c1a01c817e55d1430d10fc3 [file] [log] [blame]
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
raf34709572006-04-25 19:36:56 -07005 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07007 *
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 */
raf34709572006-04-25 19:36:56 -070021
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -070022/*
rafd4204c82008-03-20 14:44:26 -070023 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -070024 * Use is subject to license terms.
25 */
Robert Mustacchie56998e2016-04-11 08:27:29 -070026/*
John Levonab618542018-10-08 15:34:11 +010027 * Copyright 2018 Joyent, Inc.
Robert Mustacchie56998e2016-04-11 08:27:29 -070028 */
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -070029
30#include "lint.h"
31#include "thr_uberdata.h"
32
33/*
34 * pthread_once related data
35 * This structure is exported as pthread_once_t in pthread.h.
36 * We export only the size of this structure. so check
37 * pthread_once_t in pthread.h before making a change here.
38 */
39typedef struct __once {
40 mutex_t mlock;
41 union {
42 uint32_t pad32_flag[2];
43 uint64_t pad64_flag;
44 } oflag;
45} __once_t;
46
47#define once_flag oflag.pad32_flag[1]
48
rafd4204c82008-03-20 14:44:26 -070049static int
Robert Mustacchie56998e2016-04-11 08:27:29 -070050_thr_setinherit(pthread_t tid, int inherit)
51{
52 ulwp_t *ulwp;
53 int error = 0;
54
55 if ((ulwp = find_lwp(tid)) == NULL) {
56 error = ESRCH;
57 } else {
58 ulwp->ul_ptinherit = inherit;
59 ulwp_unlock(ulwp, curthread->ul_uberdata);
60 }
61
62 return (error);
63}
64
65static int
rafd4204c82008-03-20 14:44:26 -070066_thr_setparam(pthread_t tid, int policy, int prio)
67{
68 ulwp_t *ulwp;
69 id_t cid;
70 int error = 0;
71
72 if ((ulwp = find_lwp(tid)) == NULL) {
73 error = ESRCH;
74 } else {
75 if (policy == ulwp->ul_policy &&
76 (policy == SCHED_FIFO || policy == SCHED_RR) &&
rafd4204c82008-03-20 14:44:26 -070077 ulwp->ul_epri != 0) {
78 /*
79 * Don't change the ceiling priority,
80 * just the base priority.
81 */
82 if (prio > ulwp->ul_epri)
83 error = EPERM;
84 else
85 ulwp->ul_pri = prio;
86 } else if ((cid = setparam(P_LWPID, tid, policy, prio)) == -1) {
87 error = errno;
88 } else {
rafe84487a2008-03-26 17:37:28 -070089 if (policy == SCHED_FIFO || policy == SCHED_RR)
90 ulwp->ul_rtclassid = cid;
rafd4204c82008-03-20 14:44:26 -070091 ulwp->ul_cid = cid;
92 ulwp->ul_pri = prio;
raf7257d1b2008-06-06 14:02:15 -070093 membar_producer();
rafd4204c82008-03-20 14:44:26 -070094 ulwp->ul_policy = policy;
95 }
96 ulwp_unlock(ulwp, curthread->ul_uberdata);
97 }
98 return (error);
99}
100
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700101/*
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700102 * pthread_create: creates a thread in the current process.
103 * calls common _thrp_create() after copying the attributes.
104 */
raf7257d1b2008-06-06 14:02:15 -0700105#pragma weak _pthread_create = pthread_create
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700106int
raf7257d1b2008-06-06 14:02:15 -0700107pthread_create(pthread_t *thread, const pthread_attr_t *attr,
Robert Mustacchie56998e2016-04-11 08:27:29 -0700108 void * (*start_routine)(void *), void *arg)
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700109{
110 ulwp_t *self = curthread;
raf34709572006-04-25 19:36:56 -0700111 const thrattr_t *ap = attr? attr->__pthread_attrp : def_thrattr();
rafd4204c82008-03-20 14:44:26 -0700112 const pcclass_t *pccp;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700113 long flag;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700114 pthread_t tid;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700115 int error;
rafd4204c82008-03-20 14:44:26 -0700116
117 update_sched(self);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700118
raf34709572006-04-25 19:36:56 -0700119 if (ap == NULL)
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700120 return (EINVAL);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700121
rafd4204c82008-03-20 14:44:26 -0700122 /* validate explicit scheduling attributes */
123 if (ap->inherit == PTHREAD_EXPLICIT_SCHED &&
124 (ap->policy == SCHED_SYS ||
125 (pccp = get_info_by_policy(ap->policy)) == NULL ||
126 ap->prio < pccp->pcc_primin || ap->prio > pccp->pcc_primax))
127 return (EINVAL);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700128
raf34709572006-04-25 19:36:56 -0700129 flag = ap->scope | ap->detachstate | ap->daemonstate | THR_SUSPENDED;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700130 error = _thrp_create(ap->stkaddr, ap->stksize, start_routine, arg,
John Levonab618542018-10-08 15:34:11 +0100131 flag, &tid, ap->guardsize, ap->name);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700132 if (error == 0) {
Robert Mustacchie56998e2016-04-11 08:27:29 -0700133 /*
134 * Record the original inheritence value for
135 * pthread_getattr_np(). We should always be able to find the
136 * thread.
137 */
138 (void) _thr_setinherit(tid, ap->inherit);
139
rafd4204c82008-03-20 14:44:26 -0700140 if (ap->inherit == PTHREAD_EXPLICIT_SCHED &&
141 (ap->policy != self->ul_policy ||
Robert Mustacchie56998e2016-04-11 08:27:29 -0700142 ap->prio != (self->ul_epri ? self->ul_epri :
143 self->ul_pri))) {
rafd4204c82008-03-20 14:44:26 -0700144 /*
145 * The SUSv3 specification requires pthread_create()
146 * to fail with EPERM if it cannot set the scheduling
147 * policy and parameters on the new thread.
148 */
149 error = _thr_setparam(tid, ap->policy, ap->prio);
Robert Mustacchie56998e2016-04-11 08:27:29 -0700150 }
151
rafd4204c82008-03-20 14:44:26 -0700152 if (error) {
153 /*
154 * We couldn't determine this error before
155 * actually creating the thread. To recover,
156 * mark the thread detached and cancel it.
157 * It is as though it was never created.
158 */
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700159 ulwp_t *ulwp = find_lwp(tid);
rafd4204c82008-03-20 14:44:26 -0700160 if (ulwp->ul_detached == 0) {
161 ulwp->ul_detached = 1;
162 ulwp->ul_usropts |= THR_DETACHED;
163 (void) __lwp_detach(tid);
164 }
165 ulwp->ul_cancel_pending = 2; /* cancelled on creation */
166 ulwp->ul_cancel_disabled = 0;
167 ulwp_unlock(ulwp, self->ul_uberdata);
168 } else if (thread) {
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700169 *thread = tid;
rafd4204c82008-03-20 14:44:26 -0700170 }
raf7257d1b2008-06-06 14:02:15 -0700171 (void) thr_continue(tid);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700172 }
173
174 /* posix version expects EAGAIN for lack of memory */
175 if (error == ENOMEM)
176 error = EAGAIN;
177 return (error);
178}
179
180/*
181 * pthread_once: calls given function only once.
182 * it synchronizes via mutex in pthread_once_t structure
183 */
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700184int
raf7257d1b2008-06-06 14:02:15 -0700185pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700186{
187 __once_t *once = (__once_t *)once_control;
188
189 if (once == NULL || init_routine == NULL)
190 return (EINVAL);
191
192 if (once->once_flag == PTHREAD_ONCE_NOTDONE) {
raf8cd45542008-04-29 14:12:45 -0700193 (void) mutex_lock(&once->mlock);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700194 if (once->once_flag == PTHREAD_ONCE_NOTDONE) {
raf8cd45542008-04-29 14:12:45 -0700195 pthread_cleanup_push(mutex_unlock, &once->mlock);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700196 (*init_routine)();
197 pthread_cleanup_pop(0);
raf7257d1b2008-06-06 14:02:15 -0700198 membar_producer();
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700199 once->once_flag = PTHREAD_ONCE_DONE;
200 }
raf8cd45542008-04-29 14:12:45 -0700201 (void) mutex_unlock(&once->mlock);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700202 }
raf7257d1b2008-06-06 14:02:15 -0700203 membar_consumer();
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700204
205 return (0);
206}
207
208/*
209 * pthread_equal: equates two thread ids.
210 */
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700211int
raf7257d1b2008-06-06 14:02:15 -0700212pthread_equal(pthread_t t1, pthread_t t2)
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700213{
214 return (t1 == t2);
215}
216
217/*
rafd4204c82008-03-20 14:44:26 -0700218 * pthread_getschedparam: get the thread's sched parameters.
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700219 */
raf7257d1b2008-06-06 14:02:15 -0700220#pragma weak _pthread_getschedparam = pthread_getschedparam
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700221int
raf7257d1b2008-06-06 14:02:15 -0700222pthread_getschedparam(pthread_t tid, int *policy, struct sched_param *param)
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700223{
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700224 ulwp_t *ulwp;
rafd4204c82008-03-20 14:44:26 -0700225 id_t cid;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700226 int error = 0;
227
rafd4204c82008-03-20 14:44:26 -0700228 if ((ulwp = find_lwp(tid)) == NULL) {
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700229 error = ESRCH;
rafd4204c82008-03-20 14:44:26 -0700230 } else {
231 cid = getparam(P_LWPID, ulwp->ul_lwpid, policy, param);
232 if (cid == -1) {
233 error = errno;
234 } else if (*policy == ulwp->ul_policy && cid == ulwp->ul_cid &&
235 (*policy == SCHED_FIFO || *policy == SCHED_RR)) {
236 /*
237 * Return the defined priority, not the effective
238 * priority from priority ceiling mutexes.
239 */
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700240 param->sched_priority = ulwp->ul_pri;
rafd4204c82008-03-20 14:44:26 -0700241 } else {
rafe84487a2008-03-26 17:37:28 -0700242 if (*policy == SCHED_FIFO || *policy == SCHED_RR)
243 ulwp->ul_rtclassid = cid;
rafd4204c82008-03-20 14:44:26 -0700244 ulwp->ul_cid = cid;
245 ulwp->ul_pri = param->sched_priority;
raf7257d1b2008-06-06 14:02:15 -0700246 membar_producer();
rafd4204c82008-03-20 14:44:26 -0700247 ulwp->ul_policy = *policy;
248 }
249 ulwp_unlock(ulwp, curthread->ul_uberdata);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700250 }
251
252 return (error);
253}
254
raf7257d1b2008-06-06 14:02:15 -0700255#pragma weak _thr_getprio = thr_getprio
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700256int
raf7257d1b2008-06-06 14:02:15 -0700257thr_getprio(thread_t tid, int *priority)
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700258{
rafd4204c82008-03-20 14:44:26 -0700259 struct sched_param param;
260 int policy;
261 int error;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700262
raf7257d1b2008-06-06 14:02:15 -0700263 if ((error = pthread_getschedparam(tid, &policy, &param)) == 0)
rafd4204c82008-03-20 14:44:26 -0700264 *priority = param.sched_priority;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700265 return (error);
266}
267
268/*
269 * pthread_setschedparam: sets the sched parameters for a thread.
270 */
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700271int
raf7257d1b2008-06-06 14:02:15 -0700272pthread_setschedparam(pthread_t tid,
Robert Mustacchie56998e2016-04-11 08:27:29 -0700273 int policy, const struct sched_param *param)
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700274{
rafd4204c82008-03-20 14:44:26 -0700275 return (_thr_setparam(tid, policy, param->sched_priority));
276}
277
raf7257d1b2008-06-06 14:02:15 -0700278#pragma weak pthread_setschedprio = thr_setprio
rafd4204c82008-03-20 14:44:26 -0700279int
raf7257d1b2008-06-06 14:02:15 -0700280thr_setprio(thread_t tid, int prio)
rafd4204c82008-03-20 14:44:26 -0700281{
282 struct sched_param param;
283 int policy;
284 int error;
285
286 /*
raf7257d1b2008-06-06 14:02:15 -0700287 * pthread_getschedparam() has the side-effect of setting
rafd4204c82008-03-20 14:44:26 -0700288 * the target thread's ul_policy, ul_pri and ul_cid correctly.
289 */
raf7257d1b2008-06-06 14:02:15 -0700290 if ((error = pthread_getschedparam(tid, &policy, &param)) != 0)
rafd4204c82008-03-20 14:44:26 -0700291 return (error);
292 if (param.sched_priority == prio) /* no change */
293 return (0);
294 return (_thr_setparam(tid, policy, prio));
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700295}