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

53
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,34 +69,35 @@ bget(uint dev, uint sector)
acquire(&buf_table_lock); acquire(&buf_table_lock);
for(;;){ loop:
for(b = bufhead.next; b != &bufhead; b = b->next) // Try for cached block.
if((b->flags & (B_BUSY|B_VALID)) && for(b = bufhead.next; b != &bufhead; b = b->next)
b->dev == dev && b->sector == sector) if((b->flags & (B_BUSY|B_VALID)) &&
break; b->dev == dev && b->sector == sector)
break;
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_VALID; // Force reread from disk b->flags |= B_BUSY;
release(&buf_table_lock); // b->flags &= ~B_VALID; // Force reread from disk
return b; release(&buf_table_lock);
} return b;
} else { }
for(b = bufhead.prev; b != &bufhead; b = b->prev){
if((b->flags & B_BUSY) == 0){ // Allocate fresh block.
b->flags = B_BUSY; for(b = bufhead.prev; b != &bufhead; b = b->prev){
b->dev = dev; if((b->flags & B_BUSY) == 0){
b->sector = sector; b->flags = B_BUSY;
release(&buf_table_lock); b->dev = dev;
return b; b->sector = sector;
} release(&buf_table_lock);
} return b;
panic("bget: no buffers");
} }
} }
panic("bget: no buffers");
} }
// Read buf's contents from disk. // Read buf's contents from disk.