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

name individual proc locks

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