xv6-65oo2/pipe.c

131 lines
2.2 KiB
C
Raw Normal View History

2006-06-27 14:35:53 +00:00
#include "types.h"
#include "param.h"
#include "x86.h"
#include "mmu.h"
#include "proc.h"
#include "defs.h"
2006-09-06 18:40:28 +00:00
#include "file.h"
#include "spinlock.h"
2006-06-27 14:35:53 +00:00
#define PIPESIZE 512
struct pipe {
int readopen; // read fd is still open
int writeopen; // write fd is still open
int writep; // next index to write
int readp; // next index to read
struct spinlock lock;
2006-06-27 14:35:53 +00:00
char data[PIPESIZE];
};
int
2006-09-08 13:44:42 +00:00
pipe_alloc(struct file **f0, struct file **f1)
2006-06-27 14:35:53 +00:00
{
2006-09-08 13:44:42 +00:00
*f0 = *f1 = 0;
2006-06-27 14:35:53 +00:00
struct pipe *p = 0;
2006-09-08 13:44:42 +00:00
if((*f0 = filealloc()) == 0)
2006-06-27 14:35:53 +00:00
goto oops;
2006-09-08 13:44:42 +00:00
if((*f1 = filealloc()) == 0)
2006-06-27 14:35:53 +00:00
goto oops;
2006-09-06 17:27:19 +00:00
if((p = (struct pipe*) kalloc(PAGE)) == 0)
2006-06-27 14:35:53 +00:00
goto oops;
p->readopen = 1;
p->writeopen = 1;
p->writep = 0;
p->readp = 0;
initlock(&p->lock, "pipe");
2006-09-08 13:44:42 +00:00
(*f0)->type = FD_PIPE;
(*f0)->readable = 1;
(*f0)->writable = 0;
(*f0)->pipe = p;
(*f1)->type = FD_PIPE;
(*f1)->readable = 0;
(*f1)->writable = 1;
(*f1)->pipe = p;
2006-06-27 14:35:53 +00:00
return 0;
oops:
if(p)
2006-09-06 17:27:19 +00:00
kfree((char*) p, PAGE);
2006-09-08 13:44:42 +00:00
if(*f0){
(*f0)->type = FD_NONE;
fileclose(*f0);
2006-06-27 14:35:53 +00:00
}
2006-09-08 13:44:42 +00:00
if(*f1){
(*f1)->type = FD_NONE;
fileclose(*f1);
2006-06-27 14:35:53 +00:00
}
return -1;
}
void
2006-09-06 18:06:04 +00:00
pipe_close(struct pipe *p, int writable)
2006-06-27 14:35:53 +00:00
{
acquire(&p->lock);
2006-09-06 18:06:04 +00:00
if(writable){
2006-06-27 14:35:53 +00:00
p->writeopen = 0;
wakeup(&p->readp);
} else {
2006-06-27 14:35:53 +00:00
p->readopen = 0;
wakeup(&p->writep);
}
2006-09-06 17:27:19 +00:00
release(&p->lock);
2006-06-27 14:35:53 +00:00
if(p->readopen == 0 && p->writeopen == 0)
2006-09-06 17:27:19 +00:00
kfree((char*) p, PAGE);
2006-06-27 14:35:53 +00:00
}
int
pipe_write(struct pipe *p, char *addr, int n)
{
int i;
acquire(&p->lock);
2006-06-27 14:35:53 +00:00
for(i = 0; i < n; i++){
while(((p->writep + 1) % PIPESIZE) == p->readp){
if(p->readopen == 0){
release(&p->lock);
2006-06-27 14:35:53 +00:00
return -1;
}
wakeup(&p->readp);
sleep(&p->writep, &p->lock);
2006-06-27 14:35:53 +00:00
}
p->data[p->writep] = addr[i];
p->writep = (p->writep + 1) % PIPESIZE;
}
release(&p->lock);
wakeup(&p->readp);
2006-06-27 14:35:53 +00:00
return i;
}
int
pipe_read(struct pipe *p, char *addr, int n)
{
int i;
acquire(&p->lock);
2006-06-27 14:35:53 +00:00
while(p->readp == p->writep){
if(p->writeopen == 0){
release(&p->lock);
2006-06-27 14:35:53 +00:00
return 0;
}
sleep(&p->readp, &p->lock);
2006-06-27 14:35:53 +00:00
}
for(i = 0; i < n; i++){
if(p->readp == p->writep)
break;
addr[i] = p->data[p->readp];
p->readp = (p->readp + 1) % PIPESIZE;
}
release(&p->lock);
wakeup(&p->writep);
2006-06-27 14:35:53 +00:00
return i;
}