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 /* 231885Sraf * Copyright 2006 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 480Sstevel@tonic-gate /* 490Sstevel@tonic-gate * pthread_create: creates a thread in the current process. 500Sstevel@tonic-gate * calls common _thrp_create() after copying the attributes. 510Sstevel@tonic-gate */ 520Sstevel@tonic-gate #pragma weak pthread_create = _pthread_create 530Sstevel@tonic-gate int 540Sstevel@tonic-gate _pthread_create(pthread_t *thread, const pthread_attr_t *attr, 550Sstevel@tonic-gate void * (*start_routine)(void *), void *arg) 560Sstevel@tonic-gate { 570Sstevel@tonic-gate ulwp_t *self = curthread; 580Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 591885Sraf const thrattr_t *ap = attr? attr->__pthread_attrp : def_thrattr(); 600Sstevel@tonic-gate long flag; 610Sstevel@tonic-gate pthread_t tid; 620Sstevel@tonic-gate int policy; 630Sstevel@tonic-gate pri_t priority; 640Sstevel@tonic-gate int error; 650Sstevel@tonic-gate int mapped = 0; 660Sstevel@tonic-gate int mappedpri; 670Sstevel@tonic-gate int rt = 0; 680Sstevel@tonic-gate 691885Sraf if (ap == NULL) 700Sstevel@tonic-gate return (EINVAL); 710Sstevel@tonic-gate 720Sstevel@tonic-gate if (ap->inherit == PTHREAD_INHERIT_SCHED) { 730Sstevel@tonic-gate policy = self->ul_policy; 740Sstevel@tonic-gate priority = self->ul_pri; 750Sstevel@tonic-gate mapped = self->ul_pri_mapped; 760Sstevel@tonic-gate mappedpri = self->ul_mappedpri; 770Sstevel@tonic-gate } else { 780Sstevel@tonic-gate policy = ap->policy; 790Sstevel@tonic-gate priority = ap->prio; 800Sstevel@tonic-gate if (policy == SCHED_OTHER) { 810Sstevel@tonic-gate if (priority < THREAD_MIN_PRIORITY || 820Sstevel@tonic-gate priority > THREAD_MAX_PRIORITY) { 830Sstevel@tonic-gate if (_validate_rt_prio(policy, priority)) 840Sstevel@tonic-gate return (EINVAL); 850Sstevel@tonic-gate mapped = 1; 860Sstevel@tonic-gate mappedpri = priority; 87*2248Sraf priority = map_rtpri_to_gp(priority); 880Sstevel@tonic-gate ASSERT(priority >= THREAD_MIN_PRIORITY && 890Sstevel@tonic-gate priority <= THREAD_MAX_PRIORITY); 900Sstevel@tonic-gate } 910Sstevel@tonic-gate } else if (policy == SCHED_FIFO || policy == SCHED_RR) { 920Sstevel@tonic-gate if (_validate_rt_prio(policy, priority)) 930Sstevel@tonic-gate return (EINVAL); 940Sstevel@tonic-gate if (_private_geteuid() == 0) 950Sstevel@tonic-gate rt = 1; 960Sstevel@tonic-gate } else { 970Sstevel@tonic-gate return (EINVAL); 980Sstevel@tonic-gate } 990Sstevel@tonic-gate } 1000Sstevel@tonic-gate 1011885Sraf flag = ap->scope | ap->detachstate | ap->daemonstate | THR_SUSPENDED; 1020Sstevel@tonic-gate error = _thrp_create(ap->stkaddr, ap->stksize, start_routine, arg, 1030Sstevel@tonic-gate flag, &tid, priority, policy, ap->guardsize); 1040Sstevel@tonic-gate if (error == 0) { 1050Sstevel@tonic-gate if (mapped) { 1060Sstevel@tonic-gate ulwp_t *ulwp = find_lwp(tid); 1070Sstevel@tonic-gate ulwp->ul_pri_mapped = 1; 1080Sstevel@tonic-gate ulwp->ul_mappedpri = mappedpri; 1090Sstevel@tonic-gate ulwp_unlock(ulwp, udp); 1100Sstevel@tonic-gate } 1110Sstevel@tonic-gate if (rt && _thrp_setlwpprio(tid, policy, priority)) 1120Sstevel@tonic-gate thr_panic("_thrp_setlwpprio() failed"); 1130Sstevel@tonic-gate if (thread) 1140Sstevel@tonic-gate *thread = tid; 1150Sstevel@tonic-gate (void) _thr_continue(tid); 1160Sstevel@tonic-gate } 1170Sstevel@tonic-gate 1180Sstevel@tonic-gate /* posix version expects EAGAIN for lack of memory */ 1190Sstevel@tonic-gate if (error == ENOMEM) 1200Sstevel@tonic-gate error = EAGAIN; 1210Sstevel@tonic-gate return (error); 1220Sstevel@tonic-gate } 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate /* 1250Sstevel@tonic-gate * pthread_once: calls given function only once. 1260Sstevel@tonic-gate * it synchronizes via mutex in pthread_once_t structure 1270Sstevel@tonic-gate */ 1280Sstevel@tonic-gate #pragma weak pthread_once = _pthread_once 1290Sstevel@tonic-gate int 1300Sstevel@tonic-gate _pthread_once(pthread_once_t *once_control, void (*init_routine)(void)) 1310Sstevel@tonic-gate { 1320Sstevel@tonic-gate __once_t *once = (__once_t *)once_control; 1330Sstevel@tonic-gate 1340Sstevel@tonic-gate if (once == NULL || init_routine == NULL) 1350Sstevel@tonic-gate return (EINVAL); 1360Sstevel@tonic-gate 1370Sstevel@tonic-gate if (once->once_flag == PTHREAD_ONCE_NOTDONE) { 1380Sstevel@tonic-gate (void) _private_mutex_lock(&once->mlock); 1390Sstevel@tonic-gate if (once->once_flag == PTHREAD_ONCE_NOTDONE) { 1400Sstevel@tonic-gate pthread_cleanup_push(_private_mutex_unlock, 1410Sstevel@tonic-gate &once->mlock); 1420Sstevel@tonic-gate (*init_routine)(); 1430Sstevel@tonic-gate pthread_cleanup_pop(0); 1440Sstevel@tonic-gate once->once_flag = PTHREAD_ONCE_DONE; 1450Sstevel@tonic-gate } 1460Sstevel@tonic-gate (void) _private_mutex_unlock(&once->mlock); 1470Sstevel@tonic-gate } 1480Sstevel@tonic-gate 1490Sstevel@tonic-gate return (0); 1500Sstevel@tonic-gate } 1510Sstevel@tonic-gate 1520Sstevel@tonic-gate /* 1530Sstevel@tonic-gate * pthread_equal: equates two thread ids. 1540Sstevel@tonic-gate */ 1550Sstevel@tonic-gate #pragma weak pthread_equal = _pthread_equal 1560Sstevel@tonic-gate int 1570Sstevel@tonic-gate _pthread_equal(pthread_t t1, pthread_t t2) 1580Sstevel@tonic-gate { 1590Sstevel@tonic-gate return (t1 == t2); 1600Sstevel@tonic-gate } 1610Sstevel@tonic-gate 1620Sstevel@tonic-gate /* 1630Sstevel@tonic-gate * pthread_getschedparam: gets the sched parameters in a struct. 1640Sstevel@tonic-gate */ 1650Sstevel@tonic-gate #pragma weak pthread_getschedparam = _pthread_getschedparam 1660Sstevel@tonic-gate int 1670Sstevel@tonic-gate _pthread_getschedparam(pthread_t tid, int *policy, struct sched_param *param) 1680Sstevel@tonic-gate { 1690Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata; 1700Sstevel@tonic-gate ulwp_t *ulwp; 1710Sstevel@tonic-gate int error = 0; 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate if (param == NULL || policy == NULL) 1740Sstevel@tonic-gate error = EINVAL; 1750Sstevel@tonic-gate else if ((ulwp = find_lwp(tid)) == NULL) 1760Sstevel@tonic-gate error = ESRCH; 1770Sstevel@tonic-gate else { 1780Sstevel@tonic-gate if (ulwp->ul_pri_mapped) 1790Sstevel@tonic-gate param->sched_priority = ulwp->ul_mappedpri; 1800Sstevel@tonic-gate else 1810Sstevel@tonic-gate param->sched_priority = ulwp->ul_pri; 1820Sstevel@tonic-gate *policy = ulwp->ul_policy; 1830Sstevel@tonic-gate ulwp_unlock(ulwp, udp); 1840Sstevel@tonic-gate } 1850Sstevel@tonic-gate 1860Sstevel@tonic-gate return (error); 1870Sstevel@tonic-gate } 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate /* 1900Sstevel@tonic-gate * Besides the obvious arguments, the inheritflag needs to be explained: 1910Sstevel@tonic-gate * If set to PRIO_SET or PRIO_SET_PRIO, it does the normal, expected work 1920Sstevel@tonic-gate * of setting thread's assigned scheduling parameters and policy. 1930Sstevel@tonic-gate * If set to PRIO_INHERIT, it sets the thread's effective priority values 1940Sstevel@tonic-gate * (t_epri, t_empappedpri), and does not update the assigned priority values 1950Sstevel@tonic-gate * (t_pri, t_mappedpri). If set to PRIO_DISINHERIT, it clears the thread's 1960Sstevel@tonic-gate * effective priority values, and reverts the thread, if necessary, back 1970Sstevel@tonic-gate * to the assigned priority values. 1980Sstevel@tonic-gate */ 1990Sstevel@tonic-gate int 2000Sstevel@tonic-gate _thread_setschedparam_main(pthread_t tid, int policy, 2010Sstevel@tonic-gate const struct sched_param *param, int inheritflag) 2020Sstevel@tonic-gate { 2030Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata; 2040Sstevel@tonic-gate ulwp_t *ulwp; 2050Sstevel@tonic-gate int error = 0; 2060Sstevel@tonic-gate int prio; 2070Sstevel@tonic-gate int opolicy; 2080Sstevel@tonic-gate int mappedprio; 2090Sstevel@tonic-gate int mapped = 0; 2100Sstevel@tonic-gate pri_t *mappedprip; 2110Sstevel@tonic-gate 2120Sstevel@tonic-gate if (param == NULL) 2130Sstevel@tonic-gate return (EINVAL); 2140Sstevel@tonic-gate if ((ulwp = find_lwp(tid)) == NULL) 2150Sstevel@tonic-gate return (ESRCH); 2160Sstevel@tonic-gate prio = param->sched_priority; 2170Sstevel@tonic-gate opolicy = ulwp->ul_policy; 2180Sstevel@tonic-gate if (inheritflag == PRIO_SET_PRIO) { /* don't change policy */ 2190Sstevel@tonic-gate policy = opolicy; 2200Sstevel@tonic-gate inheritflag = PRIO_SET; 2210Sstevel@tonic-gate } 2220Sstevel@tonic-gate ASSERT(inheritflag == PRIO_SET || opolicy == policy); 2230Sstevel@tonic-gate if (inheritflag == PRIO_DISINHERIT) { 2240Sstevel@tonic-gate ulwp->ul_emappedpri = 0; 2250Sstevel@tonic-gate ulwp->ul_epri = 0; 2260Sstevel@tonic-gate prio = ulwp->ul_pri; /* ignore prio in sched_param */ 2270Sstevel@tonic-gate } 2280Sstevel@tonic-gate if (policy == SCHED_OTHER) { 2290Sstevel@tonic-gate /* 2300Sstevel@tonic-gate * Set thread's policy to OTHER 2310Sstevel@tonic-gate */ 2320Sstevel@tonic-gate if (prio < THREAD_MIN_PRIORITY || prio > THREAD_MAX_PRIORITY) { 2330Sstevel@tonic-gate if (_validate_rt_prio(policy, prio)) { 2340Sstevel@tonic-gate error = EINVAL; 2350Sstevel@tonic-gate goto out; 2360Sstevel@tonic-gate } 2370Sstevel@tonic-gate mapped = 1; 2380Sstevel@tonic-gate mappedprio = prio; 239*2248Sraf prio = map_rtpri_to_gp(prio); 2400Sstevel@tonic-gate ASSERT(prio >= THREAD_MIN_PRIORITY && 2410Sstevel@tonic-gate prio <= THREAD_MAX_PRIORITY); 2420Sstevel@tonic-gate } 2430Sstevel@tonic-gate /* 2440Sstevel@tonic-gate * Thread changing from FIFO/RR to OTHER 2450Sstevel@tonic-gate */ 2460Sstevel@tonic-gate if (opolicy == SCHED_FIFO || opolicy == SCHED_RR) { 2470Sstevel@tonic-gate if ((error = _thrp_setlwpprio(tid, policy, prio)) != 0) 2480Sstevel@tonic-gate goto out; 2490Sstevel@tonic-gate } 2500Sstevel@tonic-gate if (inheritflag != PRIO_DISINHERIT) { 2510Sstevel@tonic-gate if (inheritflag == PRIO_INHERIT) 2520Sstevel@tonic-gate mappedprip = &ulwp->ul_emappedpri; 2530Sstevel@tonic-gate else 2540Sstevel@tonic-gate mappedprip = &ulwp->ul_mappedpri; 2550Sstevel@tonic-gate if (mapped) { 2560Sstevel@tonic-gate ulwp->ul_pri_mapped = 1; 2570Sstevel@tonic-gate *mappedprip = mappedprio; 2580Sstevel@tonic-gate } else { 2590Sstevel@tonic-gate ulwp->ul_pri_mapped = 0; 2600Sstevel@tonic-gate *mappedprip = 0; 2610Sstevel@tonic-gate } 2620Sstevel@tonic-gate } 2630Sstevel@tonic-gate ulwp->ul_policy = policy; 2640Sstevel@tonic-gate if (inheritflag == PRIO_INHERIT) 2650Sstevel@tonic-gate ulwp->ul_epri = prio; 2660Sstevel@tonic-gate else 2670Sstevel@tonic-gate ulwp->ul_pri = prio; 2680Sstevel@tonic-gate } else if (policy == SCHED_FIFO || policy == SCHED_RR) { 2690Sstevel@tonic-gate if (_validate_rt_prio(policy, prio)) 2700Sstevel@tonic-gate error = EINVAL; 2710Sstevel@tonic-gate else { 2720Sstevel@tonic-gate if (_private_geteuid() == 0 && 2730Sstevel@tonic-gate _thrp_setlwpprio(tid, policy, prio)) 2740Sstevel@tonic-gate thr_panic("_thrp_setlwpprio failed"); 2750Sstevel@tonic-gate ulwp->ul_policy = policy; 2760Sstevel@tonic-gate if (inheritflag == PRIO_INHERIT) 2770Sstevel@tonic-gate ulwp->ul_epri = prio; 2780Sstevel@tonic-gate else 2790Sstevel@tonic-gate ulwp->ul_pri = prio; 2800Sstevel@tonic-gate } 2810Sstevel@tonic-gate } else { 2820Sstevel@tonic-gate error = EINVAL; 2830Sstevel@tonic-gate } 2840Sstevel@tonic-gate 2850Sstevel@tonic-gate out: 2860Sstevel@tonic-gate ulwp_unlock(ulwp, udp); 2870Sstevel@tonic-gate return (error); 2880Sstevel@tonic-gate } 2890Sstevel@tonic-gate 2900Sstevel@tonic-gate /* 2910Sstevel@tonic-gate * pthread_setschedparam: sets the sched parameters for a thread. 2920Sstevel@tonic-gate */ 2930Sstevel@tonic-gate #pragma weak pthread_setschedparam = _pthread_setschedparam 2940Sstevel@tonic-gate int 2950Sstevel@tonic-gate _pthread_setschedparam(pthread_t tid, 2960Sstevel@tonic-gate int policy, const struct sched_param *param) 2970Sstevel@tonic-gate { 2980Sstevel@tonic-gate return (_thread_setschedparam_main(tid, policy, param, PRIO_SET)); 2990Sstevel@tonic-gate } 300