/* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License (the "License"). * You may not use this file except in compliance with the License. * * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at usr/src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright 2008 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ #pragma ident "%Z%%M% %I% %E% SMI" #include "lint.h" #include "thr_uberdata.h" /* * pthread_once related data * This structure is exported as pthread_once_t in pthread.h. * We export only the size of this structure. so check * pthread_once_t in pthread.h before making a change here. */ typedef struct __once { mutex_t mlock; union { uint32_t pad32_flag[2]; uint64_t pad64_flag; } oflag; } __once_t; #define once_flag oflag.pad32_flag[1] static int _thr_setparam(pthread_t tid, int policy, int prio) { ulwp_t *ulwp; id_t cid; int error = 0; if ((ulwp = find_lwp(tid)) == NULL) { error = ESRCH; } else { if (policy == ulwp->ul_policy && (policy == SCHED_FIFO || policy == SCHED_RR) && ulwp->ul_cid == ulwp->ul_rtclassid && ulwp->ul_epri != 0) { /* * Don't change the ceiling priority, * just the base priority. */ if (prio > ulwp->ul_epri) error = EPERM; else ulwp->ul_pri = prio; } else if ((cid = setparam(P_LWPID, tid, policy, prio)) == -1) { error = errno; } else { ulwp->ul_cid = cid; ulwp->ul_pri = prio; _membar_producer(); ulwp->ul_policy = policy; } ulwp_unlock(ulwp, curthread->ul_uberdata); } return (error); } /* * pthread_create: creates a thread in the current process. * calls common _thrp_create() after copying the attributes. */ #pragma weak pthread_create = _pthread_create int _pthread_create(pthread_t *thread, const pthread_attr_t *attr, void * (*start_routine)(void *), void *arg) { ulwp_t *self = curthread; const thrattr_t *ap = attr? attr->__pthread_attrp : def_thrattr(); const pcclass_t *pccp; long flag; pthread_t tid; int error; update_sched(self); if (ap == NULL) return (EINVAL); /* validate explicit scheduling attributes */ if (ap->inherit == PTHREAD_EXPLICIT_SCHED && (ap->policy == SCHED_SYS || (pccp = get_info_by_policy(ap->policy)) == NULL || ap->prio < pccp->pcc_primin || ap->prio > pccp->pcc_primax)) return (EINVAL); flag = ap->scope | ap->detachstate | ap->daemonstate | THR_SUSPENDED; error = _thrp_create(ap->stkaddr, ap->stksize, start_routine, arg, flag, &tid, ap->guardsize); if (error == 0) { if (ap->inherit == PTHREAD_EXPLICIT_SCHED && (ap->policy != self->ul_policy || ap->prio != (self->ul_epri? self->ul_epri : self->ul_pri))) /* * The SUSv3 specification requires pthread_create() * to fail with EPERM if it cannot set the scheduling * policy and parameters on the new thread. */ error = _thr_setparam(tid, ap->policy, ap->prio); if (error) { /* * We couldn't determine this error before * actually creating the thread. To recover, * mark the thread detached and cancel it. * It is as though it was never created. */ ulwp_t *ulwp = find_lwp(tid); if (ulwp->ul_detached == 0) { ulwp->ul_detached = 1; ulwp->ul_usropts |= THR_DETACHED; (void) __lwp_detach(tid); } ulwp->ul_cancel_pending = 2; /* cancelled on creation */ ulwp->ul_cancel_disabled = 0; ulwp_unlock(ulwp, self->ul_uberdata); } else if (thread) { *thread = tid; } (void) _thr_continue(tid); } /* posix version expects EAGAIN for lack of memory */ if (error == ENOMEM) error = EAGAIN; return (error); } /* * pthread_once: calls given function only once. * it synchronizes via mutex in pthread_once_t structure */ #pragma weak pthread_once = _pthread_once int _pthread_once(pthread_once_t *once_control, void (*init_routine)(void)) { __once_t *once = (__once_t *)once_control; if (once == NULL || init_routine == NULL) return (EINVAL); if (once->once_flag == PTHREAD_ONCE_NOTDONE) { (void) _private_mutex_lock(&once->mlock); if (once->once_flag == PTHREAD_ONCE_NOTDONE) { pthread_cleanup_push(_private_mutex_unlock, &once->mlock); (*init_routine)(); pthread_cleanup_pop(0); _membar_producer(); once->once_flag = PTHREAD_ONCE_DONE; } (void) _private_mutex_unlock(&once->mlock); } _membar_consumer(); return (0); } /* * pthread_equal: equates two thread ids. */ #pragma weak pthread_equal = _pthread_equal int _pthread_equal(pthread_t t1, pthread_t t2) { return (t1 == t2); } /* * pthread_getschedparam: get the thread's sched parameters. */ #pragma weak pthread_getschedparam = _pthread_getschedparam int _pthread_getschedparam(pthread_t tid, int *policy, struct sched_param *param) { ulwp_t *ulwp; id_t cid; int error = 0; if ((ulwp = find_lwp(tid)) == NULL) { error = ESRCH; } else { cid = getparam(P_LWPID, ulwp->ul_lwpid, policy, param); if (cid == -1) { error = errno; } else if (*policy == ulwp->ul_policy && cid == ulwp->ul_cid && (*policy == SCHED_FIFO || *policy == SCHED_RR)) { /* * Return the defined priority, not the effective * priority from priority ceiling mutexes. */ param->sched_priority = ulwp->ul_pri; } else { ulwp->ul_cid = cid; ulwp->ul_pri = param->sched_priority; _membar_producer(); ulwp->ul_policy = *policy; } ulwp_unlock(ulwp, curthread->ul_uberdata); } return (error); } #pragma weak thr_getprio = _thr_getprio int _thr_getprio(thread_t tid, int *priority) { struct sched_param param; int policy; int error; if ((error = _pthread_getschedparam(tid, &policy, ¶m)) == 0) *priority = param.sched_priority; return (error); } /* * pthread_setschedparam: sets the sched parameters for a thread. */ #pragma weak pthread_setschedparam = _pthread_setschedparam int _pthread_setschedparam(pthread_t tid, int policy, const struct sched_param *param) { return (_thr_setparam(tid, policy, param->sched_priority)); } #pragma weak thr_setprio = _thr_setprio #pragma weak pthread_setschedprio = _thr_setprio #pragma weak _pthread_setschedprio = _thr_setprio int _thr_setprio(thread_t tid, int prio) { struct sched_param param; int policy; int error; /* * _pthread_getschedparam() has the side-effect of setting * the target thread's ul_policy, ul_pri and ul_cid correctly. */ if ((error = _pthread_getschedparam(tid, &policy, ¶m)) != 0) return (error); if (param.sched_priority == prio) /* no change */ return (0); return (_thr_setparam(tid, policy, prio)); }