Wrap proc state setting and getting with functions

上级 693cfef8
......@@ -38,7 +38,7 @@ struct proc {
struct vmap *vmap; // va -> vma
uptr brk; // Top of heap
char *kstack; // Bottom of kernel stack for this process
enum procstate state; // Process state
enum procstate _state; // Process state
volatile int pid; // Process ID
struct proc *parent; // Parent process
struct trapframe *tf; // Trap frame for current syscall
......@@ -72,3 +72,16 @@ struct proc {
LIST_ENTRY(proc) cv_waiters; // Linked list of processes waiting for oncv
LIST_ENTRY(proc) cv_sleep; // Linked list of processes sleeping on a cv
};
static inline void
set_proc_state(struct proc *p, enum procstate s)
{
p->_state = s;
}
static inline enum procstate
get_proc_state(struct proc *p)
{
return p->_state;
}
......@@ -82,7 +82,7 @@ void cv_sleepto(struct condvar *cv, struct spinlock *lk, u64 timeout)
LIST_INSERT_HEAD(&cv->waiters, myproc(), cv_waiters);
myproc()->oncv = cv;
myproc()->state = SLEEPING;
set_proc_state(myproc(), SLEEPING);
if (timeout) {
acquire(&sleepers_lock);
......@@ -115,9 +115,9 @@ cv_wakeup(struct condvar *cv)
acquire(&cv->lock);
LIST_FOREACH_SAFE(p, &cv->waiters, cv_waiters, tmp) {
acquire(&p->lock);
if (p->state != SLEEPING)
if (get_proc_state(p) != SLEEPING)
panic("cv_wakeup: pid %u name %s state %u",
p->pid, p->name, p->state);
p->pid, p->name, get_proc_state(p));
if (p->oncv != cv)
panic("cv_wakeup: pid %u name %s p->cv %p cv %p",
p->pid, p->name, p->oncv, cv);
......
......@@ -38,7 +38,7 @@ idleloop(void)
struct proc *p = schednext();
if (p) {
acquire(&p->lock);
if (p->state != RUNNABLE) {
if (get_proc_state(p) != RUNNABLE) {
panic("Huh?");
release(&p->lock);
} else {
......@@ -50,7 +50,7 @@ idleloop(void)
// before jumping back to us.
mycpu()->proc = p;
switchuvm(p);
p->state = RUNNING;
set_proc_state(p, RUNNING);
p->tsc = rdtsc();
mtpause(idlep);
......@@ -69,7 +69,7 @@ idleloop(void)
// Process is done running for now.
// It should have changed its p->state before coming back.
mycpu()->proc = idlep;
if (p->state == RUNNABLE)
if (get_proc_state(p) == RUNNABLE)
addrun(p);
release(&p->lock);
}
......
......@@ -41,13 +41,13 @@ sched(void)
#endif
if(mycpu()->ncli != 1)
panic("sched locks");
if(myproc()->state == RUNNING)
if(get_proc_state(myproc()) == RUNNING)
panic("sched running");
if(readrflags()&FL_IF)
panic("sched interruptible");
intena = mycpu()->intena;
myproc()->curcycles += rdtsc() - myproc()->tsc;
if (myproc()->state == ZOMBIE)
if (get_proc_state(myproc()) == ZOMBIE)
mtstop(myproc());
else
mtpause(myproc());
......@@ -62,7 +62,7 @@ void
yield(void)
{
acquire(&myproc()->lock); //DOC: yieldlock
myproc()->state = RUNNABLE;
set_proc_state(myproc(), RUNNABLE);
sched();
release(&myproc()->lock);
}
......@@ -120,7 +120,7 @@ exit(void)
SLIST_FOREACH_SAFE(p, &(myproc()->childq), child_next, np) {
acquire(&p->lock);
p->parent = bootproc;
if(p->state == ZOMBIE)
if(get_proc_state(p) == ZOMBIE)
wakeupinit = 1;
SLIST_REMOVE(&(myproc()->childq), p, proc, child_next);
release(&p->lock);
......@@ -139,7 +139,7 @@ exit(void)
cv_wakeup(&bootproc->cv);
// Jump into the scheduler, never to return.
myproc()->state = ZOMBIE;
set_proc_state(myproc(), ZOMBIE);
sched();
panic("zombie exit");
}
......@@ -179,7 +179,7 @@ allocproc(void)
if (p == 0) return 0;
memset(p, 0, sizeof(*p));
p->state = EMBRYO;
set_proc_state(p, EMBRYO);
p->pid = xnspid->allockey();
p->cpuid = mycpu()->id;
p->on_runq = -1;
......@@ -353,7 +353,7 @@ kill(int pid)
}
acquire(&p->lock);
p->killed = 1;
if(p->state == SLEEPING){
if(get_proc_state(p) == SLEEPING){
// XXX
// we need to wake p up if it is cv_sleep()ing.
// can't change p from SLEEPING to RUNNABLE since that
......@@ -388,8 +388,9 @@ procdumpall(void)
uptr pc[10];
for (proc *p : xnspid) {
if(p->state >= 0 && p->state < NELEM(states) && states[p->state])
state = states[p->state];
if(get_proc_state(p) >= 0 && get_proc_state(p) < NELEM(states) &&
states[get_proc_state(p)])
state = states[get_proc_state(p)];
else
state = "???";
......@@ -399,7 +400,7 @@ procdumpall(void)
cprintf("\n%-3d %-10s %8s %2u %lu\n",
p->pid, name, state, p->cpuid, p->tsc);
if(p->state == SLEEPING){
if(get_proc_state(p) == SLEEPING){
getcallerpcs((void*)p->context->rbp, pc, NELEM(pc));
for(int i=0; i<10 && pc[i] != 0; i++)
cprintf(" %lx\n", pc[i]);
......@@ -428,7 +429,7 @@ fork(int flags)
if((np->vmap = myproc()->vmap->copy(cow)) == 0){
ksfree(slab_stack, np->kstack);
np->kstack = 0;
np->state = UNUSED;
set_proc_state(np, UNUSED);
if (!xnspid->remove(np->pid, &np))
panic("fork: ns_remove");
freeproc(np);
......@@ -489,7 +490,7 @@ wait(void)
SLIST_FOREACH_SAFE(p, &myproc()->childq, child_next, np) {
havekids = 1;
acquire(&p->lock);
if(p->state == ZOMBIE){
if(get_proc_state(p) == ZOMBIE){
release(&p->lock); // noone else better be trying to lock p
pid = p->pid;
SLIST_REMOVE(&myproc()->childq, p, proc, child_next);
......@@ -497,7 +498,7 @@ wait(void)
ksfree(slab_stack, p->kstack);
p->kstack = 0;
p->vmap->decref();
p->state = UNUSED;
set_proc_state(p, UNUSED);
if (!xnspid->remove(p->pid, &p))
panic("wait: ns_remove");
p->pid = 0;
......@@ -568,7 +569,7 @@ threadpin(void (*fn)(void*), void *arg, const char *name, int cpu)
p->cpuid = cpu;
p->cpu_pin = 1;
acquire(&p->lock);
p->state = RUNNABLE;
set_proc_state(p, RUNNABLE);
addrun(p);
release(&p->lock);
}
......@@ -29,7 +29,7 @@ addrun(struct proc *p)
// Always called with p->lock held
struct runq *q;
p->state = RUNNABLE;
set_proc_state(p, RUNNABLE);
q = &runq[p->cpuid];
acquire(&q->lock);
......@@ -71,7 +71,7 @@ steal(void)
if (tryacquire(&q->lock) == 0)
continue;
STAILQ_FOREACH(p, &q->q, runqlink) {
if (p->state == RUNNABLE && !p->cpu_pin &&
if (get_proc_state(p) == RUNNABLE && !p->cpu_pin &&
p->curcycles != 0 && p->curcycles > VICTIMAGE)
{
STAILQ_REMOVE(&q->q, p, proc, runqlink);
......@@ -83,7 +83,7 @@ steal(void)
if (steal) {
acquire(&steal->lock);
if (steal->state == RUNNABLE && !steal->cpu_pin &&
if (get_proc_state(steal) == RUNNABLE && !steal->cpu_pin &&
steal->curcycles != 0 && steal->curcycles > VICTIMAGE)
{
//delrun(steal);
......@@ -94,7 +94,7 @@ steal(void)
r = 1;
break;
}
if (steal->state == RUNNABLE)
if (get_proc_state(steal) == RUNNABLE)
addrun(steal);
release(&steal->lock);
}
......
......@@ -152,7 +152,7 @@ trap(struct trapframe *tf)
// Force process to give up CPU on clock tick.
// If interrupts were on while locks held, would need to check nlock.
if(myproc() && myproc()->state == RUNNING && tf->trapno == T_IRQ0+IRQ_TIMER)
if(myproc() && get_proc_state(myproc()) == RUNNING && tf->trapno == T_IRQ0+IRQ_TIMER)
yield();
// Check if the process has been killed since we yielded
......
......@@ -220,7 +220,7 @@ sys_thread_new(const char *name, lwip_thread_fn thread, void *arg,
safestrcpy(p->name, name, sizeof(p->name));
acquire(&p->lock);
p->state = RUNNABLE;
set_proc_state(p, RUNNABLE);
addrun(p);
release(&p->lock);
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论