refactor bget to be more like iget - make it clear that sleep loops

This commit is contained in:
rsc 2007-08-08 09:50:26 +00:00
parent 9ad44da676
commit 3f98d050e1

13
bio.c
View file

@ -59,7 +59,7 @@ binit(void)
} }
} }
// Look through buffer cache for block n on device dev. // Look through buffer cache for sector on device dev.
// If not found, allocate fresh block. // If not found, allocate fresh block.
// In either case, return locked buffer. // In either case, return locked buffer.
static struct buf* static struct buf*
@ -69,7 +69,8 @@ bget(uint dev, uint sector)
acquire(&buf_table_lock); acquire(&buf_table_lock);
for(;;){ loop:
// Try for cached block.
for(b = bufhead.next; b != &bufhead; b = b->next) for(b = bufhead.next; b != &bufhead; b = b->next)
if((b->flags & (B_BUSY|B_VALID)) && if((b->flags & (B_BUSY|B_VALID)) &&
b->dev == dev && b->sector == sector) b->dev == dev && b->sector == sector)
@ -78,13 +79,15 @@ bget(uint dev, uint sector)
if(b != &bufhead){ if(b != &bufhead){
if(b->flags & B_BUSY){ if(b->flags & B_BUSY){
sleep(buf, &buf_table_lock); sleep(buf, &buf_table_lock);
} else { goto loop;
}
b->flags |= B_BUSY; b->flags |= B_BUSY;
// b->flags &= ~B_VALID; // Force reread from disk // b->flags &= ~B_VALID; // Force reread from disk
release(&buf_table_lock); release(&buf_table_lock);
return b; return b;
} }
} else {
// Allocate fresh block.
for(b = bufhead.prev; b != &bufhead; b = b->prev){ for(b = bufhead.prev; b != &bufhead; b = b->prev){
if((b->flags & B_BUSY) == 0){ if((b->flags & B_BUSY) == 0){
b->flags = B_BUSY; b->flags = B_BUSY;
@ -96,8 +99,6 @@ bget(uint dev, uint sector)
} }
panic("bget: no buffers"); panic("bget: no buffers");
} }
}
}
// Read buf's contents from disk. // Read buf's contents from disk.
struct buf* struct buf*