Skip to content

Commit 34295f4

Browse files
author
rsc
committed
group locks into structs they protect. few naming nits.
1 parent 949e559 commit 34295f4

File tree

10 files changed

+82
-80
lines changed

10 files changed

+82
-80
lines changed

bio.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ binit(void)
4141
{
4242
struct buf *b;
4343

44-
initlock(&bcache.lock, "buf_table");
44+
initlock(&bcache.lock, "bcache");
4545

4646
//PAGEBREAK!
4747
// Create linked list of buffers

console.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717

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

20-
static struct spinlock console_lock;
21-
int panicked = 0;
22-
volatile int use_console_lock = 0;
20+
static struct {
21+
struct spinlock lock;
22+
int locking;
23+
} cons;
24+
25+
static int panicked = 0;
2326

2427
static void
2528
cgaputc(int c)
@@ -99,9 +102,9 @@ cprintf(char *fmt, ...)
99102
uint *argp;
100103
char *s;
101104

102-
locking = use_console_lock;
105+
locking = cons.locking;
103106
if(locking)
104-
acquire(&console_lock);
107+
acquire(&cons.lock);
105108

106109
argp = (uint*)(void*)&fmt + 1;
107110
state = 0;
@@ -146,7 +149,7 @@ cprintf(char *fmt, ...)
146149
}
147150

148151
if(locking)
149-
release(&console_lock);
152+
release(&cons.lock);
150153
}
151154

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

157160
iunlock(ip);
158-
acquire(&console_lock);
161+
acquire(&cons.lock);
159162
for(i = 0; i < n; i++)
160163
consputc(buf[i] & 0xff);
161-
release(&console_lock);
164+
release(&cons.lock);
162165
ilock(ip);
163166

164167
return n;
@@ -255,12 +258,12 @@ consoleread(struct inode *ip, char *dst, int n)
255258
void
256259
consoleinit(void)
257260
{
258-
initlock(&console_lock, "console");
259-
initlock(&input.lock, "console input");
261+
initlock(&cons.lock, "console");
262+
initlock(&input.lock, "input");
260263

261264
devsw[CONSOLE].write = consolewrite;
262265
devsw[CONSOLE].read = consoleread;
263-
use_console_lock = 1;
266+
cons.locking = 1;
264267

265268
picenable(IRQ_KBD);
266269
ioapicenable(IRQ_KBD, 0);
@@ -273,7 +276,7 @@ panic(char *s)
273276
uint pcs[10];
274277

275278
cli();
276-
use_console_lock = 0;
279+
cons.locking = 0;
277280
cprintf("cpu%d: panic: ", cpu());
278281
cprintf(s);
279282
cprintf("\n");

file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct {
1414
void
1515
fileinit(void)
1616
{
17-
initlock(&ftable.lock, "file_table");
17+
initlock(&ftable.lock, "ftable");
1818
}
1919

2020
// Allocate a file structure.

fs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ struct {
138138
void
139139
iinit(void)
140140
{
141-
initlock(&icache.lock, "icache.lock");
141+
initlock(&icache.lock, "icache");
142142
}
143143

144144
// Find the inode with number inum on device dev

ide.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ static void idestart(struct buf*);
3030

3131
// Wait for IDE disk to become ready.
3232
static int
33-
idewait(int check_error)
33+
idewait(int checkerr)
3434
{
3535
int r;
3636

3737
while(((r = inb(0x1f7)) & (IDE_BSY|IDE_DRDY)) != IDE_DRDY)
3838
;
39-
if(check_error && (r & (IDE_DF|IDE_ERR)) != 0)
39+
if(checkerr && (r & (IDE_DF|IDE_ERR)) != 0)
4040
return -1;
4141
return 0;
4242
}

init.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include "user.h"
66
#include "fcntl.h"
77

8-
char *sh_args[] = { "sh", 0 };
8+
char *argv[] = { "sh", 0 };
99

1010
int
1111
main(void)
@@ -27,7 +27,7 @@ main(void)
2727
exit();
2828
}
2929
if(pid == 0){
30-
exec("sh", sh_args);
30+
exec("sh", argv);
3131
printf(1, "init: exec sh failed\n");
3232
exit();
3333
}

kalloc.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
#include "param.h"
1111
#include "spinlock.h"
1212

13-
struct spinlock kalloc_lock;
14-
1513
struct run {
1614
struct run *next;
1715
int len; // bytes
1816
};
19-
struct run *freelist;
17+
18+
struct {
19+
struct spinlock lock;
20+
struct run *freelist;
21+
} kmem;
2022

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

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

55-
acquire(&kalloc_lock);
57+
acquire(&kmem.lock);
5658
p = (struct run*)v;
5759
pend = (struct run*)(v + len);
58-
for(rp=&freelist; (r=*rp) != 0 && r <= pend; rp=&r->next){
60+
for(rp=&kmem.freelist; (r=*rp) != 0 && r <= pend; rp=&r->next){
5961
rend = (struct run*)((char*)r + r->len);
6062
if(r <= p && p < rend)
6163
panic("freeing free page");
@@ -80,7 +82,7 @@ kfree(char *v, int len)
8082
*rp = p;
8183

8284
out:
83-
release(&kalloc_lock);
85+
release(&kmem.lock);
8486
}
8587

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

98-
acquire(&kalloc_lock);
99-
for(rp=&freelist; (r=*rp) != 0; rp=&r->next){
100+
acquire(&kmem.lock);
101+
for(rp=&kmem.freelist; (r=*rp) != 0; rp=&r->next){
100102
if(r->len == n){
101103
*rp = r->next;
102-
release(&kalloc_lock);
104+
release(&kmem.lock);
103105
return (char*)r;
104106
}
105107
if(r->len > n){
106108
r->len -= n;
107109
p = (char*)r + r->len;
108-
release(&kalloc_lock);
110+
release(&kmem.lock);
109111
return p;
110112
}
111113
}
112-
release(&kalloc_lock);
114+
release(&kmem.lock);
113115

114116
cprintf("kalloc: out of memory\n");
115117
return 0;

0 commit comments

Comments
 (0)