#include "lint.h"
#include "thr_uberdata.h"
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_setinherit(pthread_t tid, int inherit)
{
ulwp_t *ulwp;
int error = 0;
if ((ulwp = find_lwp(tid)) == NULL) {
error = ESRCH;
} else {
ulwp->ul_ptinherit = inherit;
ulwp_unlock(ulwp, curthread->ul_uberdata);
}
return (error);
}
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_epri != 0) {
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 {
if (policy == SCHED_FIFO || policy == SCHED_RR)
ulwp->ul_rtclassid = cid;
ulwp->ul_cid = cid;
ulwp->ul_pri = prio;
membar_producer();
ulwp->ul_policy = policy;
}
ulwp_unlock(ulwp, curthread->ul_uberdata);
}
return (error);
}
#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);
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, ap->name);
if (error == 0) {
(void) _thr_setinherit(tid, ap->inherit);
if (ap->inherit == PTHREAD_EXPLICIT_SCHED &&
(ap->policy != self->ul_policy ||
ap->prio != (self->ul_epri ? self->ul_epri :
self->ul_pri))) {
error = _thr_setparam(tid, ap->policy, ap->prio);
}
if (error) {
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;
ulwp->ul_cancel_disabled = 0;
ulwp_unlock(ulwp, self->ul_uberdata);
} else if (thread) {
*thread = tid;
}
(void) thr_continue(tid);
}
if (error == ENOMEM)
error = EAGAIN;
return (error);
}
static void
_mutex_unlock_wrap(void *ptr)
{
mutex_t *mp = ptr;
(void) mutex_unlock(mp);
}
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) mutex_lock(&once->mlock);
if (once->once_flag == PTHREAD_ONCE_NOTDONE) {
pthread_cleanup_push(_mutex_unlock_wrap, &once->mlock);
(*init_routine)();
pthread_cleanup_pop(0);
membar_producer();
once->once_flag = PTHREAD_ONCE_DONE;
}
(void) mutex_unlock(&once->mlock);
}
membar_consumer();
return (0);
}
int
pthread_equal(pthread_t t1, pthread_t t2)
{
return (t1 == t2);
}
#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)) {
param->sched_priority = ulwp->ul_pri;
} else {
if (*policy == SCHED_FIFO || *policy == SCHED_RR)
ulwp->ul_rtclassid = cid;
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);
}
int
pthread_setschedparam(pthread_t tid,
int policy, const struct sched_param *param)
{
return (_thr_setparam(tid, policy, param->sched_priority));
}
#pragma weak pthread_setschedprio = thr_setprio
int
thr_setprio(thread_t tid, int prio)
{
struct sched_param param;
int policy;
int error;
if ((error = pthread_getschedparam(tid, &policy, ¶m)) != 0)
return (error);
if (param.sched_priority == prio)
return (0);
return (_thr_setparam(tid, policy, prio));
}