#include <sys/types.h>
#include <sys/param.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/cmn_err.h>
#include <sys/debug.h>
#include <sys/inline.h>
#include <sys/disp.h>
#include <sys/kmem.h>
#include <sys/cpuvar.h>
#include <sys/vtrace.h>
#include <sys/lockstat.h>
#include <sys/spl.h>
#include <sys/atomic.h>
#include <sys/cpu.h>
static void
disp_onintr_panic(void)
{
panic("dispatcher invoked from high-level interrupt handler");
}
void
disp_lock_init(disp_lock_t *lp, char *name)
{
DISP_LOCK_INIT(lp);
}
void
disp_lock_destroy(disp_lock_t *lp)
{
DISP_LOCK_DESTROY(lp);
}
void
disp_lock_enter_high(disp_lock_t *lp)
{
lock_set(lp);
}
void
disp_lock_exit_high(disp_lock_t *lp)
{
if (CPU_ON_INTR(CPU) != 0)
disp_onintr_panic();
ASSERT(DISP_LOCK_HELD(lp));
lock_clear(lp);
}
void
disp_lock_enter(disp_lock_t *lp)
{
lock_set_spl(lp, ipltospl(DISP_LEVEL), &curthread->t_oldspl);
}
void
disp_lock_exit(disp_lock_t *lp)
{
if (CPU_ON_INTR(CPU) != 0)
disp_onintr_panic();
ASSERT(DISP_LOCK_HELD(lp));
if (CPU->cpu_kprunrun) {
lock_clear_splx(lp, curthread->t_oldspl);
kpreempt(KPREEMPT_SYNC);
} else {
lock_clear_splx(lp, curthread->t_oldspl);
}
}
void
disp_lock_exit_nopreempt(disp_lock_t *lp)
{
if (CPU_ON_INTR(CPU) != 0)
disp_onintr_panic();
ASSERT(DISP_LOCK_HELD(lp));
lock_clear_splx(lp, curthread->t_oldspl);
}
void
thread_lock(kthread_id_t t)
{
int s = splhigh();
if (CPU_ON_INTR(CPU) != 0)
disp_onintr_panic();
for (;;) {
lock_t *volatile *tlpp = &t->t_lockp;
lock_t *lp = *tlpp;
if (lock_try(lp)) {
if (lp == *tlpp) {
curthread->t_oldspl = (ushort_t)s;
return;
}
lock_clear(lp);
} else {
hrtime_t spin_time =
LOCKSTAT_START_TIME(LS_THREAD_LOCK_SPIN);
splx(s);
while (lp == *tlpp && LOCK_HELD(lp)) {
if (panicstr) {
curthread->t_oldspl = splhigh();
return;
}
SMT_PAUSE();
}
LOCKSTAT_RECORD_TIME(LS_THREAD_LOCK_SPIN,
lp, spin_time);
s = splhigh();
}
}
}
void
thread_lock_high(kthread_id_t t)
{
if (CPU_ON_INTR(CPU) != 0)
disp_onintr_panic();
for (;;) {
lock_t *volatile *tlpp = &t->t_lockp;
lock_t *lp = *tlpp;
if (lock_try(lp)) {
if (lp == *tlpp)
return;
lock_clear(lp);
} else {
hrtime_t spin_time =
LOCKSTAT_START_TIME(LS_THREAD_LOCK_HIGH_SPIN);
while (lp == *tlpp && LOCK_HELD(lp)) {
if (panicstr)
return;
SMT_PAUSE();
}
LOCKSTAT_RECORD_TIME(LS_THREAD_LOCK_HIGH_SPIN,
lp, spin_time);
}
}
}
void
thread_transition(kthread_id_t t)
{
disp_lock_t *lp;
ASSERT(THREAD_LOCK_HELD(t));
ASSERT(t->t_lockp != &transition_lock);
lp = t->t_lockp;
t->t_lockp = &transition_lock;
disp_lock_exit_high(lp);
}
void
thread_stop(kthread_id_t t)
{
disp_lock_t *lp;
ASSERT(THREAD_LOCK_HELD(t));
ASSERT(t->t_lockp != &stop_lock);
lp = t->t_lockp;
t->t_state = TS_STOPPED;
membar_producer();
t->t_lockp = &stop_lock;
disp_lock_exit(lp);
}