提交 9fee0d5f 创建 作者: Frans Kaashoek's avatar Frans Kaashoek

queue of children

more condition vars queue.h
上级 b025a666
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "mmu.h" #include "mmu.h"
#include "spinlock.h" #include "spinlock.h"
#include "condvar.h" #include "condvar.h"
#include "queue.h"
#include "proc.h" #include "proc.h"
void void
...@@ -19,7 +20,7 @@ cv_sleep(struct condvar *cv, struct spinlock *lk) ...@@ -19,7 +20,7 @@ cv_sleep(struct condvar *cv, struct spinlock *lk)
// Must acquire cv_lock to avoid sleep/wakeup race // Must acquire cv_lock to avoid sleep/wakeup race
acquire(&cv->lock); acquire(&cv->lock);
cprintf("cv_sleep: 0x%x\n", cv); cprintf("cv_sleep: 0x%x %s\n", cv, cv->lock.name);
release(lk); release(lk);
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include "fs.h" #include "fs.h"
#include "file.h" #include "file.h"
#include "mmu.h" #include "mmu.h"
#include "queue.h"
#include "proc.h" #include "proc.h"
#include "x86.h" #include "x86.h"
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include "mmu.h" #include "mmu.h"
#include "spinlock.h" #include "spinlock.h"
#include "condvar.h" #include "condvar.h"
#include "queue.h"
#include "proc.h" #include "proc.h"
#include "defs.h" #include "defs.h"
#include "x86.h" #include "x86.h"
......
#include "types.h" #include "types.h"
#include "defs.h" #include "defs.h"
#include "param.h" #include "param.h"
#include "spinlock.h"
#include "condvar.h"
#include "fs.h" #include "fs.h"
#include "file.h" #include "file.h"
#include "spinlock.h"
struct devsw devsw[NDEV]; struct devsw devsw[NDEV];
struct { struct {
......
...@@ -16,6 +16,7 @@ struct inode { ...@@ -16,6 +16,7 @@ struct inode {
uint inum; // Inode number uint inum; // Inode number
int ref; // Reference count int ref; // Reference count
int flags; // I_BUSY, I_VALID int flags; // I_BUSY, I_VALID
struct condvar cv;
short type; // copy of disk inode short type; // copy of disk inode
short major; short major;
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include "mmu.h" #include "mmu.h"
#include "spinlock.h" #include "spinlock.h"
#include "condvar.h" #include "condvar.h"
#include "queue.h"
#include "proc.h" #include "proc.h"
#include "buf.h" #include "buf.h"
#include "fs.h" #include "fs.h"
...@@ -138,7 +139,11 @@ struct { ...@@ -138,7 +139,11 @@ struct {
void void
iinit(void) iinit(void)
{ {
int i;
initlock(&icache.lock, "icache"); initlock(&icache.lock, "icache");
for (i = 0; i < NINODE; i++) {
initlock(&icache.inode[i].cv.lock, "icache");
}
} }
static struct inode* iget(uint dev, uint inum); static struct inode* iget(uint dev, uint inum);
...@@ -246,7 +251,7 @@ ilock(struct inode *ip) ...@@ -246,7 +251,7 @@ ilock(struct inode *ip)
acquire(&icache.lock); acquire(&icache.lock);
while(ip->flags & I_BUSY) while(ip->flags & I_BUSY)
sleep(ip, &icache.lock); cv_sleep(&ip->cv, &icache.lock);
ip->flags |= I_BUSY; ip->flags |= I_BUSY;
release(&icache.lock); release(&icache.lock);
...@@ -275,7 +280,7 @@ iunlock(struct inode *ip) ...@@ -275,7 +280,7 @@ iunlock(struct inode *ip)
acquire(&icache.lock); acquire(&icache.lock);
ip->flags &= ~I_BUSY; ip->flags &= ~I_BUSY;
wakeup(ip); cv_wakeup(&ip->cv);
release(&icache.lock); release(&icache.lock);
} }
...@@ -295,7 +300,7 @@ iput(struct inode *ip) ...@@ -295,7 +300,7 @@ iput(struct inode *ip)
iupdate(ip); iupdate(ip);
acquire(&icache.lock); acquire(&icache.lock);
ip->flags = 0; ip->flags = 0;
wakeup(ip); cv_wakeup(&ip->cv);
} }
ip->ref--; ip->ref--;
release(&icache.lock); release(&icache.lock);
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include "mmu.h" #include "mmu.h"
#include "spinlock.h" #include "spinlock.h"
#include "condvar.h" #include "condvar.h"
#include "queue.h"
#include "proc.h" #include "proc.h"
#include "x86.h" #include "x86.h"
#include "traps.h" #include "traps.h"
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "mmu.h" #include "mmu.h"
#include "spinlock.h" #include "spinlock.h"
#include "condvar.h" #include "condvar.h"
#include "queue.h"
#include "proc.h" #include "proc.h"
#include "kalloc.h" #include "kalloc.h"
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "mmu.h" #include "mmu.h"
#include "spinlock.h" #include "spinlock.h"
#include "condvar.h" #include "condvar.h"
#include "queue.h"
#include "proc.h" #include "proc.h"
#include "x86.h" #include "x86.h"
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "mmu.h" #include "mmu.h"
#include "spinlock.h" #include "spinlock.h"
#include "condvar.h" #include "condvar.h"
#include "queue.h"
#include "proc.h" #include "proc.h"
struct cpu cpus[NCPU]; struct cpu cpus[NCPU];
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "mmu.h" #include "mmu.h"
#include "spinlock.h" #include "spinlock.h"
#include "condvar.h" #include "condvar.h"
#include "queue.h"
#include "proc.h" #include "proc.h"
#include "fs.h" #include "fs.h"
#include "file.h" #include "file.h"
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "x86.h" #include "x86.h"
#include "spinlock.h" #include "spinlock.h"
#include "condvar.h" #include "condvar.h"
#include "queue.h"
#include "proc.h" #include "proc.h"
struct ptable ptables[NCPU]; struct ptable ptables[NCPU];
...@@ -91,7 +92,7 @@ addrun1(struct runq *rq, struct proc *p) ...@@ -91,7 +92,7 @@ addrun1(struct runq *rq, struct proc *p)
{ {
struct proc *q; struct proc *q;
cprintf("%d: add to run %d\n", cpu->id, p->pid); cprintf("%d: add to run %d\n", cpu->id, p->pid);
for (q = rq->runq; q != 0; q = q->runq) { SLIST_FOREACH(q, &rq->runq, run_next) {
if (q == p) { if (q == p) {
cprintf("allready on q\n"); cprintf("allready on q\n");
p->state = RUNNABLE; p->state = RUNNABLE;
...@@ -99,8 +100,7 @@ addrun1(struct runq *rq, struct proc *p) ...@@ -99,8 +100,7 @@ addrun1(struct runq *rq, struct proc *p)
} }
} }
p->state = RUNNABLE; // race? p->state = RUNNABLE; // race?
p->runq = rq->runq; SLIST_INSERT_HEAD(&rq->runq, p, run_next);
rq->runq = p;
} }
void void
...@@ -113,23 +113,13 @@ addrun(struct proc *p) ...@@ -113,23 +113,13 @@ addrun(struct proc *p)
} }
static void static void
delrun1(struct runq *rq, struct proc *proc) delrun1(struct runq *rq, struct proc *p)
{ {
struct proc *p = 0; struct proc *q, *nq;
struct proc *n; SLIST_FOREACH_SAFE(q, &rq->runq, run_next, nq) {
n = rq->runq; if (q == p) {
while (n != 0) { SLIST_REMOVE(&rq->runq, q, proc, run_next);
if (n == proc) {
if (p == 0) {
rq->runq = n->runq;
} else {
p->runq = n->runq;
}
n->runq = 0;
return; return;
} else {
p = n;
n = n->runq;
} }
} }
} }
...@@ -224,10 +214,12 @@ fork(void) ...@@ -224,10 +214,12 @@ fork(void)
if(proc->ofile[i]) if(proc->ofile[i])
np->ofile[i] = filedup(proc->ofile[i]); np->ofile[i] = filedup(proc->ofile[i]);
np->cwd = idup(proc->cwd); np->cwd = idup(proc->cwd);
SLIST_INSERT_HEAD(&proc->childq, np, child_next); // XXX lock?
pid = np->pid; pid = np->pid;
addrun(np); addrun(np);
safestrcpy(np->name, proc->name, sizeof(proc->name)); safestrcpy(np->name, proc->name, sizeof(proc->name));
return pid; return pid;
} }
...@@ -293,22 +285,21 @@ exit(void) ...@@ -293,22 +285,21 @@ exit(void)
int int
wait(void) wait(void)
{ {
struct proc *p; struct proc *p, *np;
int havekids, pid; int havekids, pid;
int c;
cprintf("wait %d\n", proc->pid);
for(;;){ for(;;){
// Scan through table looking for zombie children. // Scan through table looking for zombie children.
havekids = 0; havekids = 0;
for (c = 0; c < NCPU; c++) { acquire(&proc->lock);
acquire(&ptables[c].lock); // XXX Unscalable SLIST_FOREACH_SAFE(p, &proc->childq, child_next, np) {
for(p = ptables[c].proc; p < &ptables[c].proc[NPROC]; p++){
if(p->parent != proc)
continue;
havekids = 1; havekids = 1;
acquire(&p->lock);
if(p->state == ZOMBIE){ if(p->state == ZOMBIE){
// Found one.
pid = p->pid; pid = p->pid;
SLIST_REMOVE(&proc->childq, p, proc, child_next);
kfree(p->kstack); kfree(p->kstack);
p->kstack = 0; p->kstack = 0;
freevm(p->pgdir); freevm(p->pgdir);
...@@ -317,15 +308,13 @@ wait(void) ...@@ -317,15 +308,13 @@ wait(void)
p->parent = 0; p->parent = 0;
p->name[0] = 0; p->name[0] = 0;
p->killed = 0; p->killed = 0;
release(&ptables[c].lock); release(&p->lock);
release(&proc->lock);
return pid; return pid;
} }
} release(&p->lock);
release(&ptables[c].lock);
} }
acquire(&proc->lock);
// No point waiting if we don't have any children. // No point waiting if we don't have any children.
if(!havekids || proc->killed){ if(!havekids || proc->killed){
release(&proc->lock); release(&proc->lock);
...@@ -350,7 +339,7 @@ steal(void) ...@@ -350,7 +339,7 @@ steal(void)
if (c == cpunum()) if (c == cpunum())
continue; continue;
acquire(&runqs[c].lock); acquire(&runqs[c].lock);
for(p = runqs[c].runq; p != 0; p = p->runq) { SLIST_FOREACH(p, &runqs[c].runq, run_next) {
if (p->state == RUNNABLE) { if (p->state == RUNNABLE) {
cprintf("%d: steal %d from %d\n", cpunum(), p->pid, c); cprintf("%d: steal %d from %d\n", cpunum(), p->pid, c);
delrun1(&runqs[c], p); delrun1(&runqs[c], p);
...@@ -386,7 +375,7 @@ scheduler(void) ...@@ -386,7 +375,7 @@ scheduler(void)
// Loop over process table looking for process to run. // Loop over process table looking for process to run.
acquire(&runq->lock); acquire(&runq->lock);
for(p = runq->runq; p != 0; p = p->runq) { SLIST_FOREACH(p, &runq->runq, run_next) {
if(p->state != RUNNABLE) if(p->state != RUNNABLE)
continue; continue;
...@@ -544,7 +533,7 @@ procdump(int c) ...@@ -544,7 +533,7 @@ procdump(int c)
cprintf("\n"); cprintf("\n");
} }
cprintf("runq: "); cprintf("runq: ");
for (q = runqs[c].runq; q != 0; q = q->runq) { SLIST_FOREACH(q, &runqs[c].runq, run_next) {
if(q->state >= 0 && q->state < NELEM(states) && states[q->state]) if(q->state >= 0 && q->state < NELEM(states) && states[q->state])
state = states[q->state]; state = states[q->state];
else else
......
...@@ -46,8 +46,9 @@ struct proc { ...@@ -46,8 +46,9 @@ struct proc {
struct inode *cwd; // Current directory struct inode *cwd; // Current directory
char name[16]; // Process name (debugging) char name[16]; // Process name (debugging)
struct spinlock lock; struct spinlock lock;
struct proc *runq; SLIST_ENTRY(proc) run_next;
struct proc *childq; SLIST_HEAD(childlist, proc) childq;
SLIST_ENTRY(proc) child_next;
struct condvar cv; struct condvar cv;
}; };
...@@ -75,11 +76,10 @@ struct cpu { ...@@ -75,11 +76,10 @@ struct cpu {
struct runq *runq; // The per-core proc table struct runq *runq; // The per-core proc table
}; };
struct runq { struct runq {
char name[MAXNAME]; char name[MAXNAME];
struct spinlock lock; struct spinlock lock;
struct proc *runq; SLIST_HEAD(runlist, proc) runq;
}; };
struct ptable { struct ptable {
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "mmu.h" #include "mmu.h"
#include "spinlock.h" #include "spinlock.h"
#include "condvar.h" #include "condvar.h"
#include "queue.h"
#include "proc.h" #include "proc.h"
void void
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "mmu.h" #include "mmu.h"
#include "spinlock.h" #include "spinlock.h"
#include "condvar.h" #include "condvar.h"
#include "queue.h"
#include "proc.h" #include "proc.h"
#include "x86.h" #include "x86.h"
#include "syscall.h" #include "syscall.h"
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "mmu.h" #include "mmu.h"
#include "spinlock.h" #include "spinlock.h"
#include "condvar.h" #include "condvar.h"
#include "queue.h"
#include "proc.h" #include "proc.h"
#include "fs.h" #include "fs.h"
#include "file.h" #include "file.h"
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "mmu.h" #include "mmu.h"
#include "spinlock.h" #include "spinlock.h"
#include "condvar.h" #include "condvar.h"
#include "queue.h"
#include "proc.h" #include "proc.h"
int int
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "mmu.h" #include "mmu.h"
#include "spinlock.h" #include "spinlock.h"
#include "condvar.h" #include "condvar.h"
#include "queue.h"
#include "proc.h" #include "proc.h"
#include "x86.h" #include "x86.h"
#include "traps.h" #include "traps.h"
......
...@@ -5,12 +5,13 @@ ...@@ -5,12 +5,13 @@
#include "param.h" #include "param.h"
#include "traps.h" #include "traps.h"
#include "spinlock.h" #include "spinlock.h"
#include "fs.h"
#include "file.h"
#include "mmu.h" #include "mmu.h"
#include "condvar.h" #include "condvar.h"
#include "queue.h"
#include "proc.h" #include "proc.h"
#include "x86.h" #include "x86.h"
#include "fs.h"
#include "file.h"
#define COM1 0x3f8 #define COM1 0x3f8
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "mmu.h" #include "mmu.h"
#include "spinlock.h" #include "spinlock.h"
#include "condvar.h" #include "condvar.h"
#include "queue.h"
#include "proc.h" #include "proc.h"
#include "elf.h" #include "elf.h"
#include "kalloc.h" #include "kalloc.h"
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论