10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51885Sraf * Common Development and Distribution License (the "License").
61885Sraf * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
211885Sraf
220Sstevel@tonic-gate /*
236247Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
280Sstevel@tonic-gate
290Sstevel@tonic-gate #include "lint.h"
300Sstevel@tonic-gate #include "thr_uberdata.h"
310Sstevel@tonic-gate
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate * pthread_once related data
340Sstevel@tonic-gate * This structure is exported as pthread_once_t in pthread.h.
350Sstevel@tonic-gate * We export only the size of this structure. so check
360Sstevel@tonic-gate * pthread_once_t in pthread.h before making a change here.
370Sstevel@tonic-gate */
380Sstevel@tonic-gate typedef struct __once {
390Sstevel@tonic-gate mutex_t mlock;
400Sstevel@tonic-gate union {
410Sstevel@tonic-gate uint32_t pad32_flag[2];
420Sstevel@tonic-gate uint64_t pad64_flag;
430Sstevel@tonic-gate } oflag;
440Sstevel@tonic-gate } __once_t;
450Sstevel@tonic-gate
460Sstevel@tonic-gate #define once_flag oflag.pad32_flag[1]
470Sstevel@tonic-gate
486247Sraf static int
_thr_setparam(pthread_t tid,int policy,int prio)496247Sraf _thr_setparam(pthread_t tid, int policy, int prio)
506247Sraf {
516247Sraf ulwp_t *ulwp;
526247Sraf id_t cid;
536247Sraf int error = 0;
546247Sraf
556247Sraf if ((ulwp = find_lwp(tid)) == NULL) {
566247Sraf error = ESRCH;
576247Sraf } else {
586247Sraf if (policy == ulwp->ul_policy &&
596247Sraf (policy == SCHED_FIFO || policy == SCHED_RR) &&
606247Sraf ulwp->ul_epri != 0) {
616247Sraf /*
626247Sraf * Don't change the ceiling priority,
636247Sraf * just the base priority.
646247Sraf */
656247Sraf if (prio > ulwp->ul_epri)
666247Sraf error = EPERM;
676247Sraf else
686247Sraf ulwp->ul_pri = prio;
696247Sraf } else if ((cid = setparam(P_LWPID, tid, policy, prio)) == -1) {
706247Sraf error = errno;
716247Sraf } else {
726296Sraf if (policy == SCHED_FIFO || policy == SCHED_RR)
736296Sraf ulwp->ul_rtclassid = cid;
746247Sraf ulwp->ul_cid = cid;
756247Sraf ulwp->ul_pri = prio;
76*6812Sraf membar_producer();
776247Sraf ulwp->ul_policy = policy;
786247Sraf }
796247Sraf ulwp_unlock(ulwp, curthread->ul_uberdata);
806247Sraf }
816247Sraf return (error);
826247Sraf }
836247Sraf
840Sstevel@tonic-gate /*
850Sstevel@tonic-gate * pthread_create: creates a thread in the current process.
860Sstevel@tonic-gate * calls common _thrp_create() after copying the attributes.
870Sstevel@tonic-gate */
88*6812Sraf #pragma weak _pthread_create = pthread_create
890Sstevel@tonic-gate int
pthread_create(pthread_t * thread,const pthread_attr_t * attr,void * (* start_routine)(void *),void * arg)90*6812Sraf pthread_create(pthread_t *thread, const pthread_attr_t *attr,
910Sstevel@tonic-gate void * (*start_routine)(void *), void *arg)
920Sstevel@tonic-gate {
930Sstevel@tonic-gate ulwp_t *self = curthread;
941885Sraf const thrattr_t *ap = attr? attr->__pthread_attrp : def_thrattr();
956247Sraf const pcclass_t *pccp;
960Sstevel@tonic-gate long flag;
970Sstevel@tonic-gate pthread_t tid;
980Sstevel@tonic-gate int error;
996247Sraf
1006247Sraf update_sched(self);
1010Sstevel@tonic-gate
1021885Sraf if (ap == NULL)
1030Sstevel@tonic-gate return (EINVAL);
1040Sstevel@tonic-gate
1056247Sraf /* validate explicit scheduling attributes */
1066247Sraf if (ap->inherit == PTHREAD_EXPLICIT_SCHED &&
1076247Sraf (ap->policy == SCHED_SYS ||
1086247Sraf (pccp = get_info_by_policy(ap->policy)) == NULL ||
1096247Sraf ap->prio < pccp->pcc_primin || ap->prio > pccp->pcc_primax))
1106247Sraf return (EINVAL);
1110Sstevel@tonic-gate
1121885Sraf flag = ap->scope | ap->detachstate | ap->daemonstate | THR_SUSPENDED;
1130Sstevel@tonic-gate error = _thrp_create(ap->stkaddr, ap->stksize, start_routine, arg,
1146247Sraf flag, &tid, ap->guardsize);
1150Sstevel@tonic-gate if (error == 0) {
1166247Sraf if (ap->inherit == PTHREAD_EXPLICIT_SCHED &&
1176247Sraf (ap->policy != self->ul_policy ||
1186247Sraf ap->prio != (self->ul_epri? self->ul_epri : self->ul_pri)))
1196247Sraf /*
1206247Sraf * The SUSv3 specification requires pthread_create()
1216247Sraf * to fail with EPERM if it cannot set the scheduling
1226247Sraf * policy and parameters on the new thread.
1236247Sraf */
1246247Sraf error = _thr_setparam(tid, ap->policy, ap->prio);
1256247Sraf if (error) {
1266247Sraf /*
1276247Sraf * We couldn't determine this error before
1286247Sraf * actually creating the thread. To recover,
1296247Sraf * mark the thread detached and cancel it.
1306247Sraf * It is as though it was never created.
1316247Sraf */
1320Sstevel@tonic-gate ulwp_t *ulwp = find_lwp(tid);
1336247Sraf if (ulwp->ul_detached == 0) {
1346247Sraf ulwp->ul_detached = 1;
1356247Sraf ulwp->ul_usropts |= THR_DETACHED;
1366247Sraf (void) __lwp_detach(tid);
1376247Sraf }
1386247Sraf ulwp->ul_cancel_pending = 2; /* cancelled on creation */
1396247Sraf ulwp->ul_cancel_disabled = 0;
1406247Sraf ulwp_unlock(ulwp, self->ul_uberdata);
1416247Sraf } else if (thread) {
1426247Sraf *thread = tid;
1430Sstevel@tonic-gate }
144*6812Sraf (void) thr_continue(tid);
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate /* posix version expects EAGAIN for lack of memory */
1480Sstevel@tonic-gate if (error == ENOMEM)
1490Sstevel@tonic-gate error = EAGAIN;
1500Sstevel@tonic-gate return (error);
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate
1530Sstevel@tonic-gate /*
1540Sstevel@tonic-gate * pthread_once: calls given function only once.
1550Sstevel@tonic-gate * it synchronizes via mutex in pthread_once_t structure
1560Sstevel@tonic-gate */
1570Sstevel@tonic-gate int
pthread_once(pthread_once_t * once_control,void (* init_routine)(void))158*6812Sraf pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
1590Sstevel@tonic-gate {
1600Sstevel@tonic-gate __once_t *once = (__once_t *)once_control;
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate if (once == NULL || init_routine == NULL)
1630Sstevel@tonic-gate return (EINVAL);
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate if (once->once_flag == PTHREAD_ONCE_NOTDONE) {
1666515Sraf (void) mutex_lock(&once->mlock);
1670Sstevel@tonic-gate if (once->once_flag == PTHREAD_ONCE_NOTDONE) {
1686515Sraf pthread_cleanup_push(mutex_unlock, &once->mlock);
1690Sstevel@tonic-gate (*init_routine)();
1700Sstevel@tonic-gate pthread_cleanup_pop(0);
171*6812Sraf membar_producer();
1720Sstevel@tonic-gate once->once_flag = PTHREAD_ONCE_DONE;
1730Sstevel@tonic-gate }
1746515Sraf (void) mutex_unlock(&once->mlock);
1750Sstevel@tonic-gate }
176*6812Sraf membar_consumer();
1770Sstevel@tonic-gate
1780Sstevel@tonic-gate return (0);
1790Sstevel@tonic-gate }
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate /*
1820Sstevel@tonic-gate * pthread_equal: equates two thread ids.
1830Sstevel@tonic-gate */
1840Sstevel@tonic-gate int
pthread_equal(pthread_t t1,pthread_t t2)185*6812Sraf pthread_equal(pthread_t t1, pthread_t t2)
1860Sstevel@tonic-gate {
1870Sstevel@tonic-gate return (t1 == t2);
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate
1900Sstevel@tonic-gate /*
1916247Sraf * pthread_getschedparam: get the thread's sched parameters.
1920Sstevel@tonic-gate */
193*6812Sraf #pragma weak _pthread_getschedparam = pthread_getschedparam
1940Sstevel@tonic-gate int
pthread_getschedparam(pthread_t tid,int * policy,struct sched_param * param)195*6812Sraf pthread_getschedparam(pthread_t tid, int *policy, struct sched_param *param)
1960Sstevel@tonic-gate {
1970Sstevel@tonic-gate ulwp_t *ulwp;
1986247Sraf id_t cid;
1990Sstevel@tonic-gate int error = 0;
2000Sstevel@tonic-gate
2016247Sraf if ((ulwp = find_lwp(tid)) == NULL) {
2020Sstevel@tonic-gate error = ESRCH;
2036247Sraf } else {
2046247Sraf cid = getparam(P_LWPID, ulwp->ul_lwpid, policy, param);
2056247Sraf if (cid == -1) {
2066247Sraf error = errno;
2076247Sraf } else if (*policy == ulwp->ul_policy && cid == ulwp->ul_cid &&
2086247Sraf (*policy == SCHED_FIFO || *policy == SCHED_RR)) {
2096247Sraf /*
2106247Sraf * Return the defined priority, not the effective
2116247Sraf * priority from priority ceiling mutexes.
2126247Sraf */
2130Sstevel@tonic-gate param->sched_priority = ulwp->ul_pri;
2146247Sraf } else {
2156296Sraf if (*policy == SCHED_FIFO || *policy == SCHED_RR)
2166296Sraf ulwp->ul_rtclassid = cid;
2176247Sraf ulwp->ul_cid = cid;
2186247Sraf ulwp->ul_pri = param->sched_priority;
219*6812Sraf membar_producer();
2206247Sraf ulwp->ul_policy = *policy;
2216247Sraf }
2226247Sraf ulwp_unlock(ulwp, curthread->ul_uberdata);
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate return (error);
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate
228*6812Sraf #pragma weak _thr_getprio = thr_getprio
2290Sstevel@tonic-gate int
thr_getprio(thread_t tid,int * priority)230*6812Sraf thr_getprio(thread_t tid, int *priority)
2310Sstevel@tonic-gate {
2326247Sraf struct sched_param param;
2336247Sraf int policy;
2346247Sraf int error;
2350Sstevel@tonic-gate
236*6812Sraf if ((error = pthread_getschedparam(tid, &policy, ¶m)) == 0)
2376247Sraf *priority = param.sched_priority;
2380Sstevel@tonic-gate return (error);
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate
2410Sstevel@tonic-gate /*
2420Sstevel@tonic-gate * pthread_setschedparam: sets the sched parameters for a thread.
2430Sstevel@tonic-gate */
2440Sstevel@tonic-gate int
pthread_setschedparam(pthread_t tid,int policy,const struct sched_param * param)245*6812Sraf pthread_setschedparam(pthread_t tid,
2460Sstevel@tonic-gate int policy, const struct sched_param *param)
2470Sstevel@tonic-gate {
2486247Sraf return (_thr_setparam(tid, policy, param->sched_priority));
2490Sstevel@tonic-gate }
2506247Sraf
251*6812Sraf #pragma weak pthread_setschedprio = thr_setprio
2526247Sraf int
thr_setprio(thread_t tid,int prio)253*6812Sraf thr_setprio(thread_t tid, int prio)
2546247Sraf {
2556247Sraf struct sched_param param;
2566247Sraf int policy;
2576247Sraf int error;
2586247Sraf
2596247Sraf /*
260*6812Sraf * pthread_getschedparam() has the side-effect of setting
2616247Sraf * the target thread's ul_policy, ul_pri and ul_cid correctly.
2626247Sraf */
263*6812Sraf if ((error = pthread_getschedparam(tid, &policy, ¶m)) != 0)
2646247Sraf return (error);
2656247Sraf if (param.sched_priority == prio) /* no change */
2666247Sraf return (0);
2676247Sraf return (_thr_setparam(tid, policy, prio));
2686247Sraf }
269