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 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; 766247Sraf _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 */ 880Sstevel@tonic-gate #pragma weak pthread_create = _pthread_create 890Sstevel@tonic-gate int 900Sstevel@tonic-gate _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 } 1440Sstevel@tonic-gate (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 #pragma weak pthread_once = _pthread_once 1580Sstevel@tonic-gate int 1590Sstevel@tonic-gate _pthread_once(pthread_once_t *once_control, void (*init_routine)(void)) 1600Sstevel@tonic-gate { 1610Sstevel@tonic-gate __once_t *once = (__once_t *)once_control; 1620Sstevel@tonic-gate 1630Sstevel@tonic-gate if (once == NULL || init_routine == NULL) 1640Sstevel@tonic-gate return (EINVAL); 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate if (once->once_flag == PTHREAD_ONCE_NOTDONE) { 167*6515Sraf (void) mutex_lock(&once->mlock); 1680Sstevel@tonic-gate if (once->once_flag == PTHREAD_ONCE_NOTDONE) { 169*6515Sraf pthread_cleanup_push(mutex_unlock, &once->mlock); 1700Sstevel@tonic-gate (*init_routine)(); 1710Sstevel@tonic-gate pthread_cleanup_pop(0); 1723864Sraf _membar_producer(); 1730Sstevel@tonic-gate once->once_flag = PTHREAD_ONCE_DONE; 1740Sstevel@tonic-gate } 175*6515Sraf (void) mutex_unlock(&once->mlock); 1760Sstevel@tonic-gate } 1773864Sraf _membar_consumer(); 1780Sstevel@tonic-gate 1790Sstevel@tonic-gate return (0); 1800Sstevel@tonic-gate } 1810Sstevel@tonic-gate 1820Sstevel@tonic-gate /* 1830Sstevel@tonic-gate * pthread_equal: equates two thread ids. 1840Sstevel@tonic-gate */ 1850Sstevel@tonic-gate #pragma weak pthread_equal = _pthread_equal 1860Sstevel@tonic-gate int 1870Sstevel@tonic-gate _pthread_equal(pthread_t t1, pthread_t t2) 1880Sstevel@tonic-gate { 1890Sstevel@tonic-gate return (t1 == t2); 1900Sstevel@tonic-gate } 1910Sstevel@tonic-gate 1920Sstevel@tonic-gate /* 1936247Sraf * pthread_getschedparam: get the thread's sched parameters. 1940Sstevel@tonic-gate */ 1950Sstevel@tonic-gate #pragma weak pthread_getschedparam = _pthread_getschedparam 1960Sstevel@tonic-gate int 1970Sstevel@tonic-gate _pthread_getschedparam(pthread_t tid, int *policy, struct sched_param *param) 1980Sstevel@tonic-gate { 1990Sstevel@tonic-gate ulwp_t *ulwp; 2006247Sraf id_t cid; 2010Sstevel@tonic-gate int error = 0; 2020Sstevel@tonic-gate 2036247Sraf if ((ulwp = find_lwp(tid)) == NULL) { 2040Sstevel@tonic-gate error = ESRCH; 2056247Sraf } else { 2066247Sraf cid = getparam(P_LWPID, ulwp->ul_lwpid, policy, param); 2076247Sraf if (cid == -1) { 2086247Sraf error = errno; 2096247Sraf } else if (*policy == ulwp->ul_policy && cid == ulwp->ul_cid && 2106247Sraf (*policy == SCHED_FIFO || *policy == SCHED_RR)) { 2116247Sraf /* 2126247Sraf * Return the defined priority, not the effective 2136247Sraf * priority from priority ceiling mutexes. 2146247Sraf */ 2150Sstevel@tonic-gate param->sched_priority = ulwp->ul_pri; 2166247Sraf } else { 2176296Sraf if (*policy == SCHED_FIFO || *policy == SCHED_RR) 2186296Sraf ulwp->ul_rtclassid = cid; 2196247Sraf ulwp->ul_cid = cid; 2206247Sraf ulwp->ul_pri = param->sched_priority; 2216247Sraf _membar_producer(); 2226247Sraf ulwp->ul_policy = *policy; 2236247Sraf } 2246247Sraf ulwp_unlock(ulwp, curthread->ul_uberdata); 2250Sstevel@tonic-gate } 2260Sstevel@tonic-gate 2270Sstevel@tonic-gate return (error); 2280Sstevel@tonic-gate } 2290Sstevel@tonic-gate 2306247Sraf #pragma weak thr_getprio = _thr_getprio 2310Sstevel@tonic-gate int 2326247Sraf _thr_getprio(thread_t tid, int *priority) 2330Sstevel@tonic-gate { 2346247Sraf struct sched_param param; 2356247Sraf int policy; 2366247Sraf int error; 2370Sstevel@tonic-gate 2386247Sraf if ((error = _pthread_getschedparam(tid, &policy, ¶m)) == 0) 2396247Sraf *priority = param.sched_priority; 2400Sstevel@tonic-gate return (error); 2410Sstevel@tonic-gate } 2420Sstevel@tonic-gate 2430Sstevel@tonic-gate /* 2440Sstevel@tonic-gate * pthread_setschedparam: sets the sched parameters for a thread. 2450Sstevel@tonic-gate */ 2460Sstevel@tonic-gate #pragma weak pthread_setschedparam = _pthread_setschedparam 2470Sstevel@tonic-gate int 2480Sstevel@tonic-gate _pthread_setschedparam(pthread_t tid, 2490Sstevel@tonic-gate int policy, const struct sched_param *param) 2500Sstevel@tonic-gate { 2516247Sraf return (_thr_setparam(tid, policy, param->sched_priority)); 2520Sstevel@tonic-gate } 2536247Sraf 2546247Sraf #pragma weak thr_setprio = _thr_setprio 2556247Sraf #pragma weak pthread_setschedprio = _thr_setprio 2566247Sraf #pragma weak _pthread_setschedprio = _thr_setprio 2576247Sraf int 2586247Sraf _thr_setprio(thread_t tid, int prio) 2596247Sraf { 2606247Sraf struct sched_param param; 2616247Sraf int policy; 2626247Sraf int error; 2636247Sraf 2646247Sraf /* 2656247Sraf * _pthread_getschedparam() has the side-effect of setting 2666247Sraf * the target thread's ul_policy, ul_pri and ul_cid correctly. 2676247Sraf */ 2686247Sraf if ((error = _pthread_getschedparam(tid, &policy, ¶m)) != 0) 2696247Sraf return (error); 2706247Sraf if (param.sched_priority == prio) /* no change */ 2716247Sraf return (0); 2726247Sraf return (_thr_setparam(tid, policy, prio)); 2736247Sraf } 274