clean up circular buffers, so pipe can queue 512 bytes rather than 511
This commit is contained in:
parent
5c5470a2fa
commit
02cc595f28
2 changed files with 12 additions and 14 deletions
14
console.c
14
console.c
|
@ -186,9 +186,9 @@ console_write(struct inode *ip, char *buf, int n)
|
||||||
struct {
|
struct {
|
||||||
struct spinlock lock;
|
struct spinlock lock;
|
||||||
char buf[INPUT_BUF];
|
char buf[INPUT_BUF];
|
||||||
int r; // Read index
|
uint r; // Read index
|
||||||
int w; // Write index
|
uint w; // Write index
|
||||||
int e; // Edit index
|
uint e; // Edit index
|
||||||
} input;
|
} input;
|
||||||
|
|
||||||
#define C(x) ((x)-'@') // Control-x
|
#define C(x) ((x)-'@') // Control-x
|
||||||
|
@ -205,20 +205,20 @@ console_intr(int (*getc)(void))
|
||||||
procdump();
|
procdump();
|
||||||
break;
|
break;
|
||||||
case C('U'): // Kill line.
|
case C('U'): // Kill line.
|
||||||
while(input.e > input.w &&
|
while(input.e != input.w &&
|
||||||
input.buf[(input.e-1) % INPUT_BUF] != '\n'){
|
input.buf[(input.e-1) % INPUT_BUF] != '\n'){
|
||||||
input.e--;
|
input.e--;
|
||||||
cons_putc(BACKSPACE);
|
cons_putc(BACKSPACE);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case C('H'): // Backspace
|
case C('H'): // Backspace
|
||||||
if(input.e > input.w){
|
if(input.e != input.w){
|
||||||
input.e--;
|
input.e--;
|
||||||
cons_putc(BACKSPACE);
|
cons_putc(BACKSPACE);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if(c != 0 && input.e < input.r+INPUT_BUF){
|
if(c != 0 && input.e-input.r < INPUT_BUF){
|
||||||
input.buf[input.e++ % INPUT_BUF] = c;
|
input.buf[input.e++ % INPUT_BUF] = c;
|
||||||
cons_putc(c);
|
cons_putc(c);
|
||||||
if(c == '\n' || c == C('D') || input.e == input.r+INPUT_BUF){
|
if(c == '\n' || c == C('D') || input.e == input.r+INPUT_BUF){
|
||||||
|
@ -293,7 +293,7 @@ panic(char *s)
|
||||||
__asm __volatile("cli");
|
__asm __volatile("cli");
|
||||||
use_console_lock = 0;
|
use_console_lock = 0;
|
||||||
cprintf("cpu%d: panic: ", cpu());
|
cprintf("cpu%d: panic: ", cpu());
|
||||||
cprintf(s, 0);
|
cprintf(s);
|
||||||
cprintf("\n");
|
cprintf("\n");
|
||||||
getcallerpcs(&s, pcs);
|
getcallerpcs(&s, pcs);
|
||||||
for(i=0; i<10; i++)
|
for(i=0; i<10; i++)
|
||||||
|
|
12
pipe.c
12
pipe.c
|
@ -11,8 +11,8 @@
|
||||||
struct pipe {
|
struct pipe {
|
||||||
int readopen; // read fd is still open
|
int readopen; // read fd is still open
|
||||||
int writeopen; // write fd is still open
|
int writeopen; // write fd is still open
|
||||||
int writep; // next index to write
|
uint writep; // next index to write
|
||||||
int readp; // next index to read
|
uint readp; // next index to read
|
||||||
struct spinlock lock;
|
struct spinlock lock;
|
||||||
char data[PIPESIZE];
|
char data[PIPESIZE];
|
||||||
};
|
};
|
||||||
|
@ -83,7 +83,7 @@ pipewrite(struct pipe *p, char *addr, int n)
|
||||||
|
|
||||||
acquire(&p->lock);
|
acquire(&p->lock);
|
||||||
for(i = 0; i < n; i++){
|
for(i = 0; i < n; i++){
|
||||||
while(((p->writep + 1) % PIPESIZE) == p->readp){
|
while(p->writep == p->readp + PIPESIZE) {
|
||||||
if(p->readopen == 0 || cp->killed){
|
if(p->readopen == 0 || cp->killed){
|
||||||
release(&p->lock);
|
release(&p->lock);
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -91,8 +91,7 @@ pipewrite(struct pipe *p, char *addr, int n)
|
||||||
wakeup(&p->readp);
|
wakeup(&p->readp);
|
||||||
sleep(&p->writep, &p->lock);
|
sleep(&p->writep, &p->lock);
|
||||||
}
|
}
|
||||||
p->data[p->writep] = addr[i];
|
p->data[p->writep++ % PIPESIZE] = addr[i];
|
||||||
p->writep = (p->writep + 1) % PIPESIZE;
|
|
||||||
}
|
}
|
||||||
wakeup(&p->readp);
|
wakeup(&p->readp);
|
||||||
release(&p->lock);
|
release(&p->lock);
|
||||||
|
@ -115,8 +114,7 @@ piperead(struct pipe *p, char *addr, int n)
|
||||||
for(i = 0; i < n; i++){
|
for(i = 0; i < n; i++){
|
||||||
if(p->readp == p->writep)
|
if(p->readp == p->writep)
|
||||||
break;
|
break;
|
||||||
addr[i] = p->data[p->readp];
|
addr[i] = p->data[p->readp++ % PIPESIZE];
|
||||||
p->readp = (p->readp + 1) % PIPESIZE;
|
|
||||||
}
|
}
|
||||||
wakeup(&p->writep);
|
wakeup(&p->writep);
|
||||||
release(&p->lock);
|
release(&p->lock);
|
||||||
|
|
Loading…
Reference in a new issue