Skip to content
Snippets Groups Projects
Commit 34295f46 authored by rsc's avatar rsc
Browse files

group locks into structs they protect.
few naming nits.
parent 949e5590
No related branches found
No related tags found
No related merge requests found
...@@ -41,7 +41,7 @@ binit(void) ...@@ -41,7 +41,7 @@ binit(void)
{ {
struct buf *b; struct buf *b;
initlock(&bcache.lock, "buf_table"); initlock(&bcache.lock, "bcache");
//PAGEBREAK! //PAGEBREAK!
// Create linked list of buffers // Create linked list of buffers
......
...@@ -17,9 +17,12 @@ ...@@ -17,9 +17,12 @@
static ushort *crt = (ushort*)0xb8000; // CGA memory static ushort *crt = (ushort*)0xb8000; // CGA memory
static struct spinlock console_lock; static struct {
int panicked = 0; struct spinlock lock;
volatile int use_console_lock = 0; int locking;
} cons;
static int panicked = 0;
static void static void
cgaputc(int c) cgaputc(int c)
...@@ -99,9 +102,9 @@ cprintf(char *fmt, ...) ...@@ -99,9 +102,9 @@ cprintf(char *fmt, ...)
uint *argp; uint *argp;
char *s; char *s;
locking = use_console_lock; locking = cons.locking;
if(locking) if(locking)
acquire(&console_lock); acquire(&cons.lock);
argp = (uint*)(void*)&fmt + 1; argp = (uint*)(void*)&fmt + 1;
state = 0; state = 0;
...@@ -146,7 +149,7 @@ cprintf(char *fmt, ...) ...@@ -146,7 +149,7 @@ cprintf(char *fmt, ...)
} }
if(locking) if(locking)
release(&console_lock); release(&cons.lock);
} }
int int
...@@ -155,10 +158,10 @@ consolewrite(struct inode *ip, char *buf, int n) ...@@ -155,10 +158,10 @@ consolewrite(struct inode *ip, char *buf, int n)
int i; int i;
iunlock(ip); iunlock(ip);
acquire(&console_lock); acquire(&cons.lock);
for(i = 0; i < n; i++) for(i = 0; i < n; i++)
consputc(buf[i] & 0xff); consputc(buf[i] & 0xff);
release(&console_lock); release(&cons.lock);
ilock(ip); ilock(ip);
return n; return n;
...@@ -255,12 +258,12 @@ consoleread(struct inode *ip, char *dst, int n) ...@@ -255,12 +258,12 @@ consoleread(struct inode *ip, char *dst, int n)
void void
consoleinit(void) consoleinit(void)
{ {
initlock(&console_lock, "console"); initlock(&cons.lock, "console");
initlock(&input.lock, "console input"); initlock(&input.lock, "input");
devsw[CONSOLE].write = consolewrite; devsw[CONSOLE].write = consolewrite;
devsw[CONSOLE].read = consoleread; devsw[CONSOLE].read = consoleread;
use_console_lock = 1; cons.locking = 1;
picenable(IRQ_KBD); picenable(IRQ_KBD);
ioapicenable(IRQ_KBD, 0); ioapicenable(IRQ_KBD, 0);
...@@ -273,7 +276,7 @@ panic(char *s) ...@@ -273,7 +276,7 @@ panic(char *s)
uint pcs[10]; uint pcs[10];
cli(); cli();
use_console_lock = 0; cons.locking = 0;
cprintf("cpu%d: panic: ", cpu()); cprintf("cpu%d: panic: ", cpu());
cprintf(s); cprintf(s);
cprintf("\n"); cprintf("\n");
......
...@@ -14,7 +14,7 @@ struct { ...@@ -14,7 +14,7 @@ struct {
void void
fileinit(void) fileinit(void)
{ {
initlock(&ftable.lock, "file_table"); initlock(&ftable.lock, "ftable");
} }
// Allocate a file structure. // Allocate a file structure.
......
...@@ -138,7 +138,7 @@ struct { ...@@ -138,7 +138,7 @@ struct {
void void
iinit(void) iinit(void)
{ {
initlock(&icache.lock, "icache.lock"); initlock(&icache.lock, "icache");
} }
// Find the inode with number inum on device dev // Find the inode with number inum on device dev
......
...@@ -30,13 +30,13 @@ static void idestart(struct buf*); ...@@ -30,13 +30,13 @@ static void idestart(struct buf*);
// Wait for IDE disk to become ready. // Wait for IDE disk to become ready.
static int static int
idewait(int check_error) idewait(int checkerr)
{ {
int r; int r;
while(((r = inb(0x1f7)) & (IDE_BSY|IDE_DRDY)) != IDE_DRDY) 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 -1;
return 0; return 0;
} }
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
#include "user.h" #include "user.h"
#include "fcntl.h" #include "fcntl.h"
char *sh_args[] = { "sh", 0 }; char *argv[] = { "sh", 0 };
int int
main(void) main(void)
...@@ -27,7 +27,7 @@ main(void) ...@@ -27,7 +27,7 @@ main(void)
exit(); exit();
} }
if(pid == 0){ if(pid == 0){
exec("sh", sh_args); exec("sh", argv);
printf(1, "init: exec sh failed\n"); printf(1, "init: exec sh failed\n");
exit(); exit();
} }
......
...@@ -10,13 +10,15 @@ ...@@ -10,13 +10,15 @@
#include "param.h" #include "param.h"
#include "spinlock.h" #include "spinlock.h"
struct spinlock kalloc_lock;
struct run { struct run {
struct run *next; struct run *next;
int len; // bytes int len; // bytes
}; };
struct run *freelist;
struct {
struct spinlock lock;
struct run *freelist;
} kmem;
// Initialize free list of physical pages. // Initialize free list of physical pages.
// This code cheats by just considering one megabyte of // This code cheats by just considering one megabyte of
...@@ -29,7 +31,7 @@ kinit(void) ...@@ -29,7 +31,7 @@ kinit(void)
uint mem; uint mem;
char *start; char *start;
initlock(&kalloc_lock, "kalloc"); initlock(&kmem.lock, "kmem");
start = (char*) &end; start = (char*) &end;
start = (char*) (((uint)start + PAGE) & ~(PAGE-1)); start = (char*) (((uint)start + PAGE) & ~(PAGE-1));
mem = 256; // assume computer has 256 pages of RAM mem = 256; // assume computer has 256 pages of RAM
...@@ -52,10 +54,10 @@ kfree(char *v, int len) ...@@ -52,10 +54,10 @@ kfree(char *v, int len)
// Fill with junk to catch dangling refs. // Fill with junk to catch dangling refs.
memset(v, 1, len); memset(v, 1, len);
acquire(&kalloc_lock); acquire(&kmem.lock);
p = (struct run*)v; p = (struct run*)v;
pend = (struct run*)(v + len); 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); rend = (struct run*)((char*)r + r->len);
if(r <= p && p < rend) if(r <= p && p < rend)
panic("freeing free page"); panic("freeing free page");
...@@ -80,7 +82,7 @@ kfree(char *v, int len) ...@@ -80,7 +82,7 @@ kfree(char *v, int len)
*rp = p; *rp = p;
out: out:
release(&kalloc_lock); release(&kmem.lock);
} }
// Allocate n bytes of physical memory. // Allocate n bytes of physical memory.
...@@ -95,21 +97,21 @@ kalloc(int n) ...@@ -95,21 +97,21 @@ kalloc(int n)
if(n % PAGE || n <= 0) if(n % PAGE || n <= 0)
panic("kalloc"); panic("kalloc");
acquire(&kalloc_lock); acquire(&kmem.lock);
for(rp=&freelist; (r=*rp) != 0; rp=&r->next){ for(rp=&kmem.freelist; (r=*rp) != 0; rp=&r->next){
if(r->len == n){ if(r->len == n){
*rp = r->next; *rp = r->next;
release(&kalloc_lock); release(&kmem.lock);
return (char*)r; return (char*)r;
} }
if(r->len > n){ if(r->len > n){
r->len -= n; r->len -= n;
p = (char*)r + r->len; p = (char*)r + r->len;
release(&kalloc_lock); release(&kmem.lock);
return p; return p;
} }
} }
release(&kalloc_lock); release(&kmem.lock);
cprintf("kalloc: out of memory\n"); cprintf("kalloc: out of memory\n");
return 0; return 0;
......
...@@ -6,9 +6,11 @@ ...@@ -6,9 +6,11 @@
#include "proc.h" #include "proc.h"
#include "spinlock.h" #include "spinlock.h"
struct spinlock proc_table_lock; struct {
struct spinlock lock;
struct proc proc[NPROC];
} ptable;
struct proc proc[NPROC];
static struct proc *initproc; static struct proc *initproc;
int nextpid = 1; int nextpid = 1;
...@@ -18,7 +20,7 @@ extern void forkret1(struct trapframe*); ...@@ -18,7 +20,7 @@ extern void forkret1(struct trapframe*);
void void
pinit(void) pinit(void)
{ {
initlock(&proc_table_lock, "proc_table"); initlock(&ptable.lock, "ptable");
} }
// Look in the process table for an UNUSED proc. // Look in the process table for an UNUSED proc.
...@@ -30,20 +32,19 @@ allocproc(void) ...@@ -30,20 +32,19 @@ allocproc(void)
int i; int i;
struct proc *p; struct proc *p;
acquire(&proc_table_lock); acquire(&ptable.lock);
for(i = 0; i < NPROC; i++){ for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
p = &proc[i];
if(p->state == UNUSED){ if(p->state == UNUSED){
p->state = EMBRYO; p->state = EMBRYO;
p->pid = nextpid++; p->pid = nextpid++;
goto found; goto found;
} }
} }
release(&proc_table_lock); release(&ptable.lock);
return 0; return 0;
found: found:
release(&proc_table_lock); release(&ptable.lock);
// Allocate kernel stack if necessary. // Allocate kernel stack if necessary.
if((p->kstack = kalloc(KSTACKSIZE)) == 0){ if((p->kstack = kalloc(KSTACKSIZE)) == 0){
...@@ -215,14 +216,13 @@ scheduler(void) ...@@ -215,14 +216,13 @@ scheduler(void)
sti(); sti();
// Loop over process table looking for process to run. // Loop over process table looking for process to run.
acquire(&proc_table_lock); acquire(&ptable.lock);
for(i = 0; i < NPROC; i++){ for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
p = &proc[i];
if(p->state != RUNNABLE) if(p->state != RUNNABLE)
continue; continue;
// Switch to chosen process. It is the process's job // Switch to chosen process. It is the process's job
// to release proc_table_lock and then reacquire it // to release ptable.lock and then reacquire it
// before jumping back to us. // before jumping back to us.
cp = p; cp = p;
usegment(); usegment();
...@@ -234,12 +234,12 @@ scheduler(void) ...@@ -234,12 +234,12 @@ scheduler(void)
cp = 0; cp = 0;
usegment(); usegment();
} }
release(&proc_table_lock); release(&ptable.lock);
} }
} }
// Enter scheduler. Must already hold proc_table_lock // Enter scheduler. Must already hold ptable.lock
// and have changed cp->state. // and have changed cp->state.
void void
sched(void) sched(void)
...@@ -250,8 +250,8 @@ sched(void) ...@@ -250,8 +250,8 @@ sched(void)
panic("sched interruptible"); panic("sched interruptible");
if(cp->state == RUNNING) if(cp->state == RUNNING)
panic("sched running"); panic("sched running");
if(!holding(&proc_table_lock)) if(!holding(&ptable.lock))
panic("sched proc_table_lock"); panic("sched ptable.lock");
if(c->ncli != 1) if(c->ncli != 1)
panic("sched locks"); panic("sched locks");
...@@ -264,10 +264,10 @@ sched(void) ...@@ -264,10 +264,10 @@ sched(void)
void void
yield(void) yield(void)
{ {
acquire(&proc_table_lock); acquire(&ptable.lock);
cp->state = RUNNABLE; cp->state = RUNNABLE;
sched(); sched();
release(&proc_table_lock); release(&ptable.lock);
} }
// A fork child's very first scheduling by scheduler() // A fork child's very first scheduling by scheduler()
...@@ -275,8 +275,8 @@ yield(void) ...@@ -275,8 +275,8 @@ yield(void)
void void
forkret(void) forkret(void)
{ {
// Still holding proc_table_lock from scheduler. // Still holding ptable.lock from scheduler.
release(&proc_table_lock); release(&ptable.lock);
// Jump into assembly, never to return. // Jump into assembly, never to return.
forkret1(cp->tf); forkret1(cp->tf);
...@@ -293,14 +293,14 @@ sleep(void *chan, struct spinlock *lk) ...@@ -293,14 +293,14 @@ sleep(void *chan, struct spinlock *lk)
if(lk == 0) if(lk == 0)
panic("sleep without lk"); panic("sleep without lk");
// Must acquire proc_table_lock in order to // Must acquire ptable.lock in order to
// change p->state and then call sched. // change p->state and then call sched.
// Once we hold proc_table_lock, we can be // Once we hold ptable.lock, we can be
// guaranteed that we won't miss any wakeup // guaranteed that we won't miss any wakeup
// (wakeup runs with proc_table_lock locked), // (wakeup runs with ptable.lock locked),
// so it's okay to release lk. // so it's okay to release lk.
if(lk != &proc_table_lock){ if(lk != &ptable.lock){
acquire(&proc_table_lock); acquire(&ptable.lock);
release(lk); release(lk);
} }
...@@ -313,15 +313,15 @@ sleep(void *chan, struct spinlock *lk) ...@@ -313,15 +313,15 @@ sleep(void *chan, struct spinlock *lk)
cp->chan = 0; cp->chan = 0;
// Reacquire original lock. // Reacquire original lock.
if(lk != &proc_table_lock){ if(lk != &ptable.lock){
release(&proc_table_lock); release(&ptable.lock);
acquire(lk); acquire(lk);
} }
} }
//PAGEBREAK! //PAGEBREAK!
// Wake up all processes sleeping on chan. // Wake up all processes sleeping on chan.
// Proc_table_lock must be held. // The ptable lock must be held.
static void static void
wakeup1(void *chan) wakeup1(void *chan)
{ {
...@@ -336,9 +336,9 @@ wakeup1(void *chan) ...@@ -336,9 +336,9 @@ wakeup1(void *chan)
void void
wakeup(void *chan) wakeup(void *chan)
{ {
acquire(&proc_table_lock); acquire(&ptable.lock);
wakeup1(chan); wakeup1(chan);
release(&proc_table_lock); release(&ptable.lock);
} }
// Kill the process with the given pid. // Kill the process with the given pid.
...@@ -349,18 +349,18 @@ kill(int pid) ...@@ -349,18 +349,18 @@ kill(int pid)
{ {
struct proc *p; struct proc *p;
acquire(&proc_table_lock); acquire(&ptable.lock);
for(p = proc; p < &proc[NPROC]; p++){ for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
if(p->pid == pid){ if(p->pid == pid){
p->killed = 1; p->killed = 1;
// Wake process from sleep if necessary. // Wake process from sleep if necessary.
if(p->state == SLEEPING) if(p->state == SLEEPING)
p->state = RUNNABLE; p->state = RUNNABLE;
release(&proc_table_lock); release(&ptable.lock);
return 0; return 0;
} }
} }
release(&proc_table_lock); release(&ptable.lock);
return -1; return -1;
} }
...@@ -387,13 +387,13 @@ exit(void) ...@@ -387,13 +387,13 @@ exit(void)
iput(cp->cwd); iput(cp->cwd);
cp->cwd = 0; cp->cwd = 0;
acquire(&proc_table_lock); acquire(&ptable.lock);
// Parent might be sleeping in wait(). // Parent might be sleeping in wait().
wakeup1(cp->parent); wakeup1(cp->parent);
// Pass abandoned children to init. // Pass abandoned children to init.
for(p = proc; p < &proc[NPROC]; p++){ for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
if(p->parent == cp){ if(p->parent == cp){
p->parent = initproc; p->parent = initproc;
if(p->state == ZOMBIE) if(p->state == ZOMBIE)
...@@ -416,12 +416,11 @@ wait(void) ...@@ -416,12 +416,11 @@ wait(void)
struct proc *p; struct proc *p;
int i, havekids, pid; int i, havekids, pid;
acquire(&proc_table_lock); acquire(&ptable.lock);
for(;;){ for(;;){
// Scan through table looking for zombie children. // Scan through table looking for zombie children.
havekids = 0; havekids = 0;
for(i = 0; i < NPROC; i++){ for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
p = &proc[i];
if(p->state == UNUSED) if(p->state == UNUSED)
continue; continue;
if(p->parent == cp){ if(p->parent == cp){
...@@ -435,7 +434,7 @@ wait(void) ...@@ -435,7 +434,7 @@ wait(void)
p->pid = 0; p->pid = 0;
p->parent = 0; p->parent = 0;
p->name[0] = 0; p->name[0] = 0;
release(&proc_table_lock); release(&ptable.lock);
return pid; return pid;
} }
} }
...@@ -443,12 +442,12 @@ wait(void) ...@@ -443,12 +442,12 @@ wait(void)
// No point waiting if we don't have any children. // No point waiting if we don't have any children.
if(!havekids || cp->killed){ if(!havekids || cp->killed){
release(&proc_table_lock); release(&ptable.lock);
return -1; return -1;
} }
// Wait for children to exit. (See wakeup1 call in proc_exit.) // Wait for children to exit. (See wakeup1 call in proc_exit.)
sleep(cp, &proc_table_lock); sleep(cp, &ptable.lock);
} }
} }
...@@ -471,8 +470,7 @@ procdump(void) ...@@ -471,8 +470,7 @@ procdump(void)
char *state; char *state;
uint pc[10]; uint pc[10];
for(i = 0; i < NPROC; i++){ for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
p = &proc[i];
if(p->state == UNUSED) if(p->state == UNUSED)
continue; continue;
if(p->state >= 0 && p->state < NELEM(states) && states[p->state]) if(p->state >= 0 && p->state < NELEM(states) && states[p->state])
......
...@@ -24,14 +24,14 @@ struct context { ...@@ -24,14 +24,14 @@ struct context {
uint eip; uint eip;
}; };
enum proc_state { UNUSED, EMBRYO, SLEEPING, RUNNABLE, RUNNING, ZOMBIE }; enum procstate { UNUSED, EMBRYO, SLEEPING, RUNNABLE, RUNNING, ZOMBIE };
// Per-process state // Per-process state
struct proc { struct proc {
char *mem; // Start of process memory (kernel address) char *mem; // Start of process memory (kernel address)
uint sz; // Size of process memory (bytes) uint sz; // Size of process memory (bytes)
char *kstack; // Bottom of kernel stack for this process char *kstack; // Bottom of kernel stack for this process
enum proc_state state; // Process state enum procstate state; // Process state
volatile int pid; // Process ID volatile int pid; // Process ID
struct proc *parent; // Parent process struct proc *parent; // Parent process
struct trapframe *tf; // Trap frame for current syscall struct trapframe *tf; // Trap frame for current syscall
......
...@@ -6,8 +6,7 @@ ...@@ -6,8 +6,7 @@
char buf[2048]; char buf[2048];
char name[3]; char name[3];
char *echo_args[] = { "echo", "ALL", "TESTS", "PASSED", 0 }; char *echoargv[] = { "echo", "ALL", "TESTS", "PASSED", 0 };
char *cat_args[] = { "cat", "README", 0 };
int stdout = 1; int stdout = 1;
// simple file system tests // simple file system tests
...@@ -191,7 +190,7 @@ void ...@@ -191,7 +190,7 @@ void
exectest(void) exectest(void)
{ {
printf(stdout, "exec test\n"); printf(stdout, "exec test\n");
if(exec("echo", echo_args) < 0) { if(exec("echo", echoargv) < 0) {
printf(stdout, "exec echo failed\n"); printf(stdout, "exec echo failed\n");
exit(); exit();
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment