提交 2710b226 创建 作者: Nickolai Zeldovich's avatar Nickolai Zeldovich

name individual proc locks

上级 b11c28b9
...@@ -15,6 +15,8 @@ ...@@ -15,6 +15,8 @@
#include "proc.h" #include "proc.h"
#include "x86.h" #include "x86.h"
#include <stdarg.h>
static void consputc(int); static void consputc(int);
static int panicked = 0; static int panicked = 0;
...@@ -25,7 +27,8 @@ static struct { ...@@ -25,7 +27,8 @@ static struct {
} cons; } cons;
static void static void
printint(int xx, int base, int sign) printint(void (*putch) (void*, char), void *putarg,
int xx, int base, int sign)
{ {
static char digits[] = "0123456789abcdef"; static char digits[] = "0123456789abcdef";
char buf[16]; char buf[16];
...@@ -46,61 +49,112 @@ printint(int xx, int base, int sign) ...@@ -46,61 +49,112 @@ printint(int xx, int base, int sign)
buf[i++] = '-'; buf[i++] = '-';
while(--i >= 0) while(--i >= 0)
consputc(buf[i]); putch(putarg, buf[i]);
} }
//PAGEBREAK: 50 //PAGEBREAK: 50
// Print to the console. only understands %d, %x, %p, %s. // Only understands %d, %x, %p, %s.
void void
cprintf(char *fmt, ...) vprintfmt(void (*putch) (void*, char), void *putarg,
char *fmt, va_list ap)
{ {
int i, c, state, locking;
uint *argp;
char *s; char *s;
int c, i, state;
locking = cons.locking;
if(locking)
acquire(&cons.lock);
argp = (uint*)(void*)(&fmt + 1);
state = 0; state = 0;
for(i = 0; (c = fmt[i] & 0xff) != 0; i++){ for(i = 0; fmt[i]; i++){
if(c != '%'){ c = fmt[i] & 0xff;
consputc(c); if(state == 0){
continue; if(c == '%'){
} state = '%';
c = fmt[++i] & 0xff; } else {
if(c == 0) putch(putarg, c);
break; }
switch(c){ } else if(state == '%'){
case 'd': if(c == 'd'){
printint(*argp++, 10, 1); printint(putch, putarg, va_arg(ap, uint), 10, 1);
break; } else if(c == 'x' || c == 'p'){
case 'x': printint(putch, putarg, va_arg(ap, uint), 16, 0);
case 'p': } else if(c == 's'){
printint(*argp++, 16, 0); s = (char*) va_arg(ap, char*);
break; if(s == 0)
case 's': s = "(null)";
if((s = (char*)*argp++) == 0) while(*s != 0){
s = "(null)"; putch(putarg, *s);
for(; *s; s++) s++;
consputc(*s); }
break; } else if(c == 'c'){
case '%': putch(putarg, va_arg(ap, uint));
consputc('%'); } else if(c == '%'){
break; putch(putarg, c);
default: } else {
// Print unknown % sequence to draw attention. // Unknown % sequence. Print it to draw attention.
consputc('%'); putch(putarg, '%');
consputc(c); putch(putarg, c);
break; }
state = 0;
} }
} }
}
// Print to the console.
static void
writecons(void *arg, char c)
{
consputc(c);
}
void
cprintf(char *fmt, ...)
{
va_list ap;
int locking = cons.locking;
if(locking)
acquire(&cons.lock);
va_start(ap, fmt);
vprintfmt(writecons, 0, fmt, ap);
va_end(ap);
if(locking) if(locking)
release(&cons.lock); release(&cons.lock);
} }
// Print to a buffer.
struct bufstate {
char *p;
char *e;
};
static void
writebuf(void *arg, char c)
{
struct bufstate *bs = arg;
if (bs->p < bs->e) {
bs->p[0] = c;
bs->p++;
}
}
void
vsnprintf(char *buf, uint n, char *fmt, va_list ap)
{
struct bufstate bs = { buf, buf+n-1 };
vprintfmt(writebuf, (void*) &bs, fmt, ap);
bs.p[0] = '\0';
}
void
snprintf(char *buf, uint n, char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, n, fmt, ap);
va_end(ap);
}
void void
panic(char *s) panic(char *s)
{ {
......
#include <stdarg.h>
struct buf; struct buf;
struct context; struct context;
struct file; struct file;
...@@ -24,6 +26,8 @@ void cv_wakeup(struct condvar *cv); ...@@ -24,6 +26,8 @@ void cv_wakeup(struct condvar *cv);
// console.c // console.c
void consoleinit(void); void consoleinit(void);
void cprintf(char*, ...); void cprintf(char*, ...);
void vsnprintf(char *buf, uint n, char *fmt, va_list ap);
void snprintf(char *buf, uint n, char *fmt, ...);
void consoleintr(int(*)(void)); void consoleintr(int(*)(void));
void panic(char*) __attribute__((noreturn)); void panic(char*) __attribute__((noreturn));
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
#include "xv6-mtrace.h" #include "xv6-mtrace.h"
#define NCHILD 2 #define NCHILD 2
#define NDEPTH 6 #define NDEPTH 5
char* char*
strncpy(char *s, const char *t, int n) strncpy(char *s, const char *t, int n)
...@@ -27,7 +27,7 @@ forktree(void) ...@@ -27,7 +27,7 @@ forktree(void)
{ {
uint depth = 0; uint depth = 0;
printf(1, "fork tree\n"); printf(1, "%d: fork tree\n", getpid());
mtrace_enable_set(1, "xv6-forktree"); mtrace_enable_set(1, "xv6-forktree");
next_level: next_level:
...@@ -68,7 +68,7 @@ forktree(void) ...@@ -68,7 +68,7 @@ forktree(void)
mtrace_appdata_register(&entry); mtrace_appdata_register(&entry);
mtrace_enable_set(0, "xv6-forktree"); mtrace_enable_set(0, "xv6-forktree");
printf(1, "fork tree OK\n"); printf(1, "%d: fork tree OK\n", getpid());
halt(); halt();
} }
......
...@@ -2,14 +2,11 @@ ...@@ -2,14 +2,11 @@
#include "stat.h" #include "stat.h"
#include "user.h" #include "user.h"
static void #include <stdarg.h>
putc(int fd, char c)
{
write(fd, &c, 1);
}
static void static void
printint(int fd, int xx, int base, int sgn) printint(void (*putch) (void*, char), void *putarg,
int xx, int base, int sgn)
{ {
static char digits[] = "0123456789ABCDEF"; static char digits[] = "0123456789ABCDEF";
char buf[16]; char buf[16];
...@@ -32,54 +29,101 @@ printint(int fd, int xx, int base, int sgn) ...@@ -32,54 +29,101 @@ printint(int fd, int xx, int base, int sgn)
buf[i++] = '-'; buf[i++] = '-';
while(--i >= 0) while(--i >= 0)
putc(fd, buf[i]); putch(putarg, buf[i]);
} }
// Print to the given fd. Only understands %d, %x, %p, %s. // Only understands %d, %x, %p, %s.
void void
printf(int fd, char *fmt, ...) vprintfmt(void (*putch) (void*, char), void *putarg,
char *fmt, va_list ap)
{ {
char *s; char *s;
int c, i, state; int c, i, state;
uint *ap;
state = 0; state = 0;
ap = (uint*)(void*)&fmt + 1;
for(i = 0; fmt[i]; i++){ for(i = 0; fmt[i]; i++){
c = fmt[i] & 0xff; c = fmt[i] & 0xff;
if(state == 0){ if(state == 0){
if(c == '%'){ if(c == '%'){
state = '%'; state = '%';
} else { } else {
putc(fd, c); putch(putarg, c);
} }
} else if(state == '%'){ } else if(state == '%'){
if(c == 'd'){ if(c == 'd'){
printint(fd, *ap, 10, 1); printint(putch, putarg, va_arg(ap, uint), 10, 1);
ap++;
} else if(c == 'x' || c == 'p'){ } else if(c == 'x' || c == 'p'){
printint(fd, *ap, 16, 0); printint(putch, putarg, va_arg(ap, uint), 16, 0);
ap++;
} else if(c == 's'){ } else if(c == 's'){
s = (char*)*ap; s = (char*) va_arg(ap, char*);
ap++;
if(s == 0) if(s == 0)
s = "(null)"; s = "(null)";
while(*s != 0){ while(*s != 0){
putc(fd, *s); putch(putarg, *s);
s++; s++;
} }
} else if(c == 'c'){ } else if(c == 'c'){
putc(fd, *ap); putch(putarg, va_arg(ap, uint));
ap++;
} else if(c == '%'){ } else if(c == '%'){
putc(fd, c); putch(putarg, c);
} else { } else {
// Unknown % sequence. Print it to draw attention. // Unknown % sequence. Print it to draw attention.
putc(fd, '%'); putch(putarg, '%');
putc(fd, c); putch(putarg, c);
} }
state = 0; state = 0;
} }
} }
} }
// Print to the given fd.
static void
writec(void *arg, char c)
{
int fd = (int) arg;
write(fd, &c, 1);
}
void
printf(int fd, char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vprintfmt(writec, (void*) fd, fmt, ap);
va_end(ap);
}
// Print to a buffer.
struct bufstate {
char *p;
char *e;
};
static void
writebuf(void *arg, char c)
{
struct bufstate *bs = arg;
if (bs->p < bs->e) {
bs->p[0] = c;
bs->p++;
}
}
void
vsnprintf(char *buf, uint n, char *fmt, va_list ap)
{
struct bufstate bs = { buf, buf+n-1 };
vprintfmt(writebuf, (void*) &bs, fmt, ap);
bs.p[0] = '\0';
}
void
snprintf(char *buf, uint n, char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, n, fmt, ap);
va_end(ap);
}
...@@ -50,14 +50,16 @@ allocproc(void) ...@@ -50,14 +50,16 @@ allocproc(void)
if (p == 0) return 0; if (p == 0) return 0;
memset(p, 0, sizeof(*p)); memset(p, 0, sizeof(*p));
p->state = EMBRYO; p->state = EMBRYO;
initlock(&p->lock, "proc");
initcondvar(&p->cv, "proc");
p->state = EMBRYO; p->state = EMBRYO;
p->pid = ns_allockey(nspid); p->pid = ns_allockey(nspid);
p->epoch = INF; p->epoch = INF;
p->cpuid = cpu->id; p->cpuid = cpu->id;
snprintf(p->lockname, sizeof(p->lockname), "proc%d", p->pid);
initlock(&p->lock, p->lockname);
initcondvar(&p->cv, p->lockname);
if (ns_insert(nspid, p->pid, (void *) p) < 0) if (ns_insert(nspid, p->pid, (void *) p) < 0)
panic("allocproc: ns_insert"); panic("allocproc: ns_insert");
......
...@@ -89,6 +89,7 @@ struct proc { ...@@ -89,6 +89,7 @@ struct proc {
SLIST_ENTRY(proc) child_next; SLIST_ENTRY(proc) child_next;
struct condvar cv; struct condvar cv;
uint epoch; uint epoch;
char lockname[16];
}; };
// Process memory is laid out contiguously, low addresses first: // Process memory is laid out contiguously, low addresses first:
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论