lab lock has been finished

This commit is contained in:
whatever 2024-11-25 19:11:58 +08:00
parent 39d5972dbd
commit 80fbb50f09
2 changed files with 10 additions and 8 deletions

View File

@ -52,7 +52,7 @@ uint hasher(uint dev,uint blockno){
int hash_get(uint dev,uint blockno){
const uint hash_pos=hasher(dev, blockno);
int pos=-1;
acquire(&bcache.hashlock[hash_pos]);
// acquire(&bcache.hashlock[hash_pos]);
for(int cur=bcache.hash_head[hash_pos];cur!=-1;cur=bcache.nxt[cur])
if(bcache.buf[cur].dev==dev && bcache.buf[cur].blockno==blockno){
pos=cur;
@ -61,17 +61,17 @@ int hash_get(uint dev,uint blockno){
RET:
release(&bcache.hashlock[hash_pos]);
// release(&bcache.hashlock[hash_pos]);
// printf("%u\n",pos);
return pos;
}
void hash_add(uint dev,uint blockno,uint buf_pos){
const uint hash_pos=hasher(dev, blockno);
acquire(&bcache.hashlock[hash_pos]);
// acquire(&bcache.hashlock[hash_pos]);
bcache.nxt[buf_pos]=bcache.hash_head[hash_pos];
bcache.hash_head[hash_pos]=buf_pos;
release(&bcache.hashlock[hash_pos]);
// release(&bcache.hashlock[hash_pos]);
}
void hash_del(uint dev,uint blockno){
@ -157,6 +157,8 @@ bget(uint dev, uint blockno)
{
struct buf *b;
// acquire(&bcache.lock);
const uint hash_pos=hasher(dev, blockno);
acquire(&bcache.hashlock[hash_pos]);
const int pos=hash_get(dev, blockno);
// Is the block already cached?
// for(b = bcache.head.next; b != &bcache.head; b = b->next){
@ -169,8 +171,7 @@ bget(uint dev, uint blockno)
// }
if(pos>=0){
b=&bcache.buf[pos];
const uint hash_pos=hasher(dev, blockno);
acquire(&bcache.hashlock[hash_pos]);
// acquire(&bcache.hashlock[hash_pos]);
++bcache.buf[pos].refcnt;
release(&bcache.hashlock[hash_pos]);
acquiresleep(&b->lock);
@ -214,6 +215,7 @@ bget(uint dev, uint blockno)
const int ori=bcache.freelist_head[i];
bcache.freelist_head[i]=bcache.nxt[bcache.freelist_head[i]];
hash_add(dev,blockno,ori);
release(&bcache.hashlock[hash_pos]);
b->dev=dev;
b->blockno=blockno;
b->valid=0;

View File

@ -93,8 +93,8 @@ bfree(int dev, uint b)
{
struct buf *bp;
int bi, m;
DEBUG();
printf("%d %d\n",dev,b);
// DEBUG();
// printf("%d %d\n",dev,b);
bp = bread(dev, BBLOCK(b, sb));
bi = b % BPB;
m = 1 << (bi % 8);