Clean up using killed()

This commit is contained in:
Frans Kaashoek 2022-08-11 07:23:17 -04:00
parent 4087a6e7fc
commit 975f3b31d3
6 changed files with 14 additions and 7 deletions

View file

@ -89,7 +89,7 @@ consoleread(int user_dst, uint64 dst, int n)
// wait until interrupt handler has put some // wait until interrupt handler has put some
// input into cons.buffer. // input into cons.buffer.
while(cons.r == cons.w){ while(cons.r == cons.w){
if(__sync_add_and_fetch(&(myproc()->killed), 0)){ if(killed(myproc())){
release(&cons.lock); release(&cons.lock);
return -1; return -1;
} }

View file

@ -90,6 +90,7 @@ void proc_mapstacks(pagetable_t);
pagetable_t proc_pagetable(struct proc *); pagetable_t proc_pagetable(struct proc *);
void proc_freepagetable(pagetable_t, uint64); void proc_freepagetable(pagetable_t, uint64);
int kill(int); int kill(int);
int killed(struct proc*);
struct cpu* mycpu(void); struct cpu* mycpu(void);
struct cpu* getmycpu(void); struct cpu* getmycpu(void);
struct proc* myproc(); struct proc* myproc();

View file

@ -81,7 +81,7 @@ pipewrite(struct pipe *pi, uint64 addr, int n)
acquire(&pi->lock); acquire(&pi->lock);
while(i < n){ while(i < n){
if(pi->readopen == 0 || __sync_add_and_fetch(&pr->killed,0)){ if(pi->readopen == 0 || killed(pr)){
release(&pi->lock); release(&pi->lock);
return -1; return -1;
} }
@ -111,7 +111,7 @@ piperead(struct pipe *pi, uint64 addr, int n)
acquire(&pi->lock); acquire(&pi->lock);
while(pi->nread == pi->nwrite && pi->writeopen){ //DOC: pipe-empty while(pi->nread == pi->nwrite && pi->writeopen){ //DOC: pipe-empty
if(__sync_add_and_fetch(&pr->killed,0)){ if(killed(pr)){
release(&pi->lock); release(&pi->lock);
return -1; return -1;
} }

View file

@ -422,7 +422,7 @@ wait(uint64 addr)
} }
// No point waiting if we don't have any children. // No point waiting if we don't have any children.
if(!havekids || __sync_add_and_fetch(&p->killed, 0)){ if(!havekids || killed(p)){
release(&wait_lock); release(&wait_lock);
return -1; return -1;
} }
@ -601,6 +601,12 @@ kill(int pid)
return -1; return -1;
} }
int
killed(struct proc *p)
{
return __sync_add_and_fetch(&p->killed, 0);
}
// Copy to either a user address, or kernel address, // Copy to either a user address, or kernel address,
// depending on usr_dst. // depending on usr_dst.
// Returns 0 on success, -1 on error. // Returns 0 on success, -1 on error.

View file

@ -63,7 +63,7 @@ sys_sleep(void)
acquire(&tickslock); acquire(&tickslock);
ticks0 = ticks; ticks0 = ticks;
while(ticks - ticks0 < n){ while(ticks - ticks0 < n){
if(__sync_add_and_fetch(&(myproc()->killed), 0)){ if(killed(myproc())){
release(&tickslock); release(&tickslock);
return -1; return -1;
} }

View file

@ -53,7 +53,7 @@ usertrap(void)
if(r_scause() == 8){ if(r_scause() == 8){
// system call // system call
if(__sync_add_and_fetch(&p->killed, 0)) if(killed(p))
exit(-1); exit(-1);
// sepc points to the ecall instruction, // sepc points to the ecall instruction,
@ -73,7 +73,7 @@ usertrap(void)
__sync_bool_compare_and_swap(&p->killed, 0, 1); __sync_bool_compare_and_swap(&p->killed, 0, 1);
} }
if(__sync_add_and_fetch(&p->killed, 0)) if(killed(p))
exit(-1); exit(-1);
// give up the CPU if this is a timer interrupt. // give up the CPU if this is a timer interrupt.