Skip to content

Commit

Permalink
group locks into structs they protect.
Browse files Browse the repository at this point in the history
few naming nits.
  • Loading branch information
rsc committed May 31, 2009
1 parent 949e559 commit 34295f4
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 80 deletions.
2 changes: 1 addition & 1 deletion bio.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ binit(void)
{
struct buf *b;

initlock(&bcache.lock, "buf_table");
initlock(&bcache.lock, "bcache");

//PAGEBREAK!
// Create linked list of buffers
Expand Down
27 changes: 15 additions & 12 deletions console.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@

static ushort *crt = (ushort*)0xb8000; // CGA memory

static struct spinlock console_lock;
int panicked = 0;
volatile int use_console_lock = 0;
static struct {
struct spinlock lock;
int locking;
} cons;

static int panicked = 0;

static void
cgaputc(int c)
Expand Down Expand Up @@ -99,9 +102,9 @@ cprintf(char *fmt, ...)
uint *argp;
char *s;

locking = use_console_lock;
locking = cons.locking;
if(locking)
acquire(&console_lock);
acquire(&cons.lock);

argp = (uint*)(void*)&fmt + 1;
state = 0;
Expand Down Expand Up @@ -146,7 +149,7 @@ cprintf(char *fmt, ...)
}

if(locking)
release(&console_lock);
release(&cons.lock);
}

int
Expand All @@ -155,10 +158,10 @@ consolewrite(struct inode *ip, char *buf, int n)
int i;

iunlock(ip);
acquire(&console_lock);
acquire(&cons.lock);
for(i = 0; i < n; i++)
consputc(buf[i] & 0xff);
release(&console_lock);
release(&cons.lock);
ilock(ip);

return n;
Expand Down Expand Up @@ -255,12 +258,12 @@ consoleread(struct inode *ip, char *dst, int n)
void
consoleinit(void)
{
initlock(&console_lock, "console");
initlock(&input.lock, "console input");
initlock(&cons.lock, "console");
initlock(&input.lock, "input");

devsw[CONSOLE].write = consolewrite;
devsw[CONSOLE].read = consoleread;
use_console_lock = 1;
cons.locking = 1;

picenable(IRQ_KBD);
ioapicenable(IRQ_KBD, 0);
Expand All @@ -273,7 +276,7 @@ panic(char *s)
uint pcs[10];

cli();
use_console_lock = 0;
cons.locking = 0;
cprintf("cpu%d: panic: ", cpu());
cprintf(s);
cprintf("\n");
Expand Down
2 changes: 1 addition & 1 deletion file.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct {
void
fileinit(void)
{
initlock(&ftable.lock, "file_table");
initlock(&ftable.lock, "ftable");
}

// Allocate a file structure.
Expand Down
2 changes: 1 addition & 1 deletion fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ struct {
void
iinit(void)
{
initlock(&icache.lock, "icache.lock");
initlock(&icache.lock, "icache");
}

// Find the inode with number inum on device dev
Expand Down
4 changes: 2 additions & 2 deletions ide.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ static void idestart(struct buf*);

// Wait for IDE disk to become ready.
static int
idewait(int check_error)
idewait(int checkerr)
{
int r;

while(((r = inb(0x1f7)) & (IDE_BSY|IDE_DRDY)) != IDE_DRDY)
;
if(check_error && (r & (IDE_DF|IDE_ERR)) != 0)
if(checkerr && (r & (IDE_DF|IDE_ERR)) != 0)
return -1;
return 0;
}
Expand Down
4 changes: 2 additions & 2 deletions init.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "user.h"
#include "fcntl.h"

char *sh_args[] = { "sh", 0 };
char *argv[] = { "sh", 0 };

int
main(void)
Expand All @@ -27,7 +27,7 @@ main(void)
exit();
}
if(pid == 0){
exec("sh", sh_args);
exec("sh", argv);
printf(1, "init: exec sh failed\n");
exit();
}
Expand Down
26 changes: 14 additions & 12 deletions kalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
#include "param.h"
#include "spinlock.h"

struct spinlock kalloc_lock;

struct run {
struct run *next;
int len; // bytes
};
struct run *freelist;

struct {
struct spinlock lock;
struct run *freelist;
} kmem;

// Initialize free list of physical pages.
// This code cheats by just considering one megabyte of
Expand All @@ -29,7 +31,7 @@ kinit(void)
uint mem;
char *start;

initlock(&kalloc_lock, "kalloc");
initlock(&kmem.lock, "kmem");
start = (char*) &end;
start = (char*) (((uint)start + PAGE) & ~(PAGE-1));
mem = 256; // assume computer has 256 pages of RAM
Expand All @@ -52,10 +54,10 @@ kfree(char *v, int len)
// Fill with junk to catch dangling refs.
memset(v, 1, len);

acquire(&kalloc_lock);
acquire(&kmem.lock);
p = (struct run*)v;
pend = (struct run*)(v + len);
for(rp=&freelist; (r=*rp) != 0 && r <= pend; rp=&r->next){
for(rp=&kmem.freelist; (r=*rp) != 0 && r <= pend; rp=&r->next){
rend = (struct run*)((char*)r + r->len);
if(r <= p && p < rend)
panic("freeing free page");
Expand All @@ -80,7 +82,7 @@ kfree(char *v, int len)
*rp = p;

out:
release(&kalloc_lock);
release(&kmem.lock);
}

// Allocate n bytes of physical memory.
Expand All @@ -95,21 +97,21 @@ kalloc(int n)
if(n % PAGE || n <= 0)
panic("kalloc");

acquire(&kalloc_lock);
for(rp=&freelist; (r=*rp) != 0; rp=&r->next){
acquire(&kmem.lock);
for(rp=&kmem.freelist; (r=*rp) != 0; rp=&r->next){
if(r->len == n){
*rp = r->next;
release(&kalloc_lock);
release(&kmem.lock);
return (char*)r;
}
if(r->len > n){
r->len -= n;
p = (char*)r + r->len;
release(&kalloc_lock);
release(&kmem.lock);
return p;
}
}
release(&kalloc_lock);
release(&kmem.lock);

cprintf("kalloc: out of memory\n");
return 0;
Expand Down
Loading

0 comments on commit 34295f4

Please sign in to comment.