#include <sys/param.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/user.h>
#include <sys/file.h>
#include <sys/vnode.h>
#include <sys/kernel.h>
#include <sys/buf.h>
#include <machine/spl.h>
#include <kern/queue.h>
#include <sys/lock.h>
#include <kern/thread.h>
#include <kern/ast.h>
#include <kern/cpu_number.h>
#include <vm/vm_kern.h>
#include <kern/task.h>
#include <mach/time_value.h>
_sleep_continue()
{
register struct proc *p;
register thread_t thread = current_thread();
thread_act_t th_act;
struct uthread * ut;
int sig, catch;
int error = 0;
th_act = current_act();
ut = get_bsdthread_info(th_act);
catch = ut->uu_pri & PCATCH;
p = current_proc();
#if FIXME
thread->wait_mesg = NULL;
#endif
switch (get_thread_waitresult(thread)) {
case THREAD_TIMED_OUT:
error = EWOULDBLOCK;
break;
case THREAD_AWAKENED:
if (!catch)
break;
case THREAD_INTERRUPTED:
if (catch) {
unix_master();
if (thread_should_abort(current_thread())) {
error = EINTR;
} else if (SHOULDissignal(p,ut)) {
if (sig = CURSIG(p)) {
if (p->p_sigacts->ps_sigintr & sigmask(sig))
error = EINTR;
else
error = ERESTART;
}
if (thread_should_abort(current_thread())) {
error = EINTR;
}
}
unix_release();
} else
error = EINTR;
break;
}
if ((error == EINTR) || (error == ERESTART)) {
#ifdef BSD_USE_APC
thread_apc_set(th_act, bsd_ast);
#else
thread_ast_set(th_act, AST_BSD);
ast_on(AST_BSD);
#endif
}
if (ut->uu_timo)
thread_cancel_timer();
#if 0
thread_funnel_set(kernel_flock, TRUE);
#endif
unix_syscall_return((*ut->uu_continuation)(error));
}
#if FIXME
static __inline__
#endif
int
_sleep(chan, pri, wmsg, timo, continuation)
caddr_t chan;
int pri;
char *wmsg;
int timo;
int (*continuation)();
{
register struct proc *p;
register thread_t thread = current_thread();
thread_act_t th_act;
struct uthread * ut;
int sig, catch = pri & PCATCH;
int sigttblock = pri & PTTYBLOCK;
int error = 0;
spl_t s;
s = splhigh();
th_act = current_act();
ut = get_bsdthread_info(th_act);
p = current_proc();
#if KTRACE
if (KTRPOINT(p, KTR_CSW))
ktrcsw(p->p_tracep, 1, 0);
#endif
p->p_priority = pri & PRIMASK;
if (chan)
assert_wait(chan, (catch) ? THREAD_ABORTSAFE : THREAD_UNINT);
if (timo)
thread_set_timer(timo, NSEC_PER_SEC / hz);
if (catch) {
unix_master();
if (SHOULDissignal(p,ut)) {
if (sig = CURSIG(p)) {
clear_wait(thread, THREAD_INTERRUPTED);
if (sigttblock && ((sig == SIGTTOU) || (sig == SIGTTIN))) {
p->p_flag |= P_TTYSLEEP;
clear_sigbits(p, sig);
assert_wait(&p->p_siglist, THREAD_ABORTSAFE);
if (p->p_flag & P_TTYSLEEP)
thread_block(0);
error = 0;
goto out;
}
if (p->p_sigacts->ps_sigintr & sigmask(sig))
error = EINTR;
else
error = ERESTART;
unix_release();
goto out;
}
}
if (thread_should_abort(current_thread())) {
clear_wait(thread, THREAD_INTERRUPTED);
error = EINTR;
unix_release();
goto out;
}
if (get_thread_waitevent(thread) == 0) {
unix_release();
goto out;
}
unix_release();
}
#if FIXME
thread->wait_mesg = wmsg;
#endif
splx(s);
p->p_stats->p_ru.ru_nvcsw++;
if (continuation != (int (*)()) 0 ) {
ut->uu_continuation = continuation;
ut->uu_pri = pri;
ut->uu_timo = timo;
thread_block(_sleep_continue);
}
thread_block(0);
#if FIXME
thread->wait_mesg = NULL;
#endif
switch (get_thread_waitresult(thread)) {
case THREAD_TIMED_OUT:
error = EWOULDBLOCK;
break;
case THREAD_AWAKENED:
if (!catch)
break;
case THREAD_INTERRUPTED:
if (catch) {
unix_master();
if (thread_should_abort(current_thread())) {
error = EINTR;
} else if (SHOULDissignal(p,ut)) {
if (sig = CURSIG(p)) {
if (p->p_sigacts->ps_sigintr & sigmask(sig))
error = EINTR;
else
error = ERESTART;
}
if (thread_should_abort(current_thread())) {
error = EINTR;
}
}
unix_release();
} else
error = EINTR;
break;
}
out:
if ((error == EINTR) || (error == ERESTART)) {
#ifdef BSD_USE_APC
thread_apc_set(th_act, bsd_ast);
#else
thread_ast_set(th_act, AST_BSD);
ast_on(AST_BSD);
#endif
}
if (timo)
thread_cancel_timer();
(void) splx(s);
return (error);
}
int sleep(chan, pri)
void *chan;
int pri;
{
return (_sleep((caddr_t)chan, pri, (char *)NULL, 0, (void (*)())0 ));
}
int tsleep(chan, pri, wmsg, timo)
void *chan;
int pri;
char * wmsg;
int timo;
{
return(_sleep((caddr_t)chan, pri, wmsg, timo, (void (*)())0 ));
}
int tsleep0(chan, pri, wmsg, timo, continuation)
void *chan;
int pri;
char * wmsg;
int timo;
int (*continuation)();
{
#if defined (__i386__)
return(_sleep((caddr_t)chan, pri, wmsg, timo, (void (*)())0 ));
#else
return(_sleep((caddr_t)chan, pri, wmsg, timo, continuation));
#endif
}
void
wakeup(chan)
register void *chan;
{
thread_wakeup_prim((caddr_t)chan,FALSE, THREAD_AWAKENED);
}
wakeup_one(chan)
register caddr_t chan;
{
thread_wakeup_prim((caddr_t)chan, TRUE, THREAD_AWAKENED);
}
void
resetpriority(p)
register struct proc *p;
{
int newpri;
#if FIXME
if (p->p_nice < 0)
newpri = BASEPRI_USER +
(p->p_nice * (MAXPRI_USER - BASEPRI_USER)) / PRIO_MIN;
else
newpri = BASEPRI_USER -
(p->p_nice * BASEPRI_USER) / PRIO_MAX;
(void)task_priority(p->task, newpri, TRUE);
#endif
}