2006-06-27 16:35:53 +02:00
|
|
|
#include "types.h"
|
|
|
|
#include "param.h"
|
|
|
|
#include "x86.h"
|
|
|
|
#include "mmu.h"
|
|
|
|
#include "proc.h"
|
|
|
|
#include "defs.h"
|
2006-09-06 20:40:28 +02:00
|
|
|
#include "file.h"
|
2006-07-12 03:48:35 +02:00
|
|
|
#include "spinlock.h"
|
2006-06-27 16:35:53 +02: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
|
2006-07-12 03:48:35 +02:00
|
|
|
struct spinlock lock;
|
2006-06-27 16:35:53 +02:00
|
|
|
char data[PIPESIZE];
|
|
|
|
};
|
|
|
|
|
|
|
|
int
|
2006-09-06 20:38:56 +02:00
|
|
|
pipe_alloc(struct file **fd1, struct file **fd2)
|
2006-06-27 16:35:53 +02:00
|
|
|
{
|
|
|
|
*fd1 = *fd2 = 0;
|
|
|
|
struct pipe *p = 0;
|
|
|
|
|
2006-09-06 20:43:45 +02:00
|
|
|
if((*fd1 = filealloc()) == 0)
|
2006-06-27 16:35:53 +02:00
|
|
|
goto oops;
|
2006-09-06 20:43:45 +02:00
|
|
|
if((*fd2 = filealloc()) == 0)
|
2006-06-27 16:35:53 +02:00
|
|
|
goto oops;
|
2006-09-06 19:27:19 +02:00
|
|
|
if((p = (struct pipe*) kalloc(PAGE)) == 0)
|
2006-06-27 16:35:53 +02:00
|
|
|
goto oops;
|
2006-07-01 23:26:01 +02:00
|
|
|
p->readopen = 1;
|
|
|
|
p->writeopen = 1;
|
|
|
|
p->writep = 0;
|
|
|
|
p->readp = 0;
|
2006-08-11 00:08:14 +02:00
|
|
|
initlock(&p->lock, "pipe");
|
2006-06-27 16:35:53 +02:00
|
|
|
(*fd1)->type = FD_PIPE;
|
|
|
|
(*fd1)->readable = 1;
|
2006-09-06 20:06:04 +02:00
|
|
|
(*fd1)->writable = 0;
|
2006-06-27 16:35:53 +02:00
|
|
|
(*fd1)->pipe = p;
|
|
|
|
(*fd2)->type = FD_PIPE;
|
|
|
|
(*fd2)->readable = 0;
|
2006-09-06 20:06:04 +02:00
|
|
|
(*fd2)->writable = 1;
|
2006-06-27 16:35:53 +02:00
|
|
|
(*fd2)->pipe = p;
|
|
|
|
return 0;
|
|
|
|
oops:
|
|
|
|
if(p)
|
2006-09-06 19:27:19 +02:00
|
|
|
kfree((char*) p, PAGE);
|
2006-06-27 16:35:53 +02:00
|
|
|
if(*fd1){
|
|
|
|
(*fd1)->type = FD_NONE;
|
2006-09-06 20:43:45 +02:00
|
|
|
fileclose(*fd1);
|
2006-06-27 16:35:53 +02:00
|
|
|
}
|
|
|
|
if(*fd2){
|
|
|
|
(*fd2)->type = FD_NONE;
|
2006-09-06 20:43:45 +02:00
|
|
|
fileclose(*fd2);
|
2006-06-27 16:35:53 +02:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2006-09-06 20:06:04 +02:00
|
|
|
pipe_close(struct pipe *p, int writable)
|
2006-06-27 16:35:53 +02:00
|
|
|
{
|
2006-07-18 21:22:37 +02:00
|
|
|
acquire(&p->lock);
|
|
|
|
|
2006-09-06 20:06:04 +02:00
|
|
|
if(writable){
|
2006-06-27 16:35:53 +02:00
|
|
|
p->writeopen = 0;
|
2006-07-01 23:26:01 +02:00
|
|
|
wakeup(&p->readp);
|
|
|
|
} else {
|
2006-06-27 16:35:53 +02:00
|
|
|
p->readopen = 0;
|
2006-07-01 23:26:01 +02:00
|
|
|
wakeup(&p->writep);
|
|
|
|
}
|
2006-09-06 19:27:19 +02:00
|
|
|
|
2006-07-18 21:22:37 +02:00
|
|
|
release(&p->lock);
|
|
|
|
|
2006-06-27 16:35:53 +02:00
|
|
|
if(p->readopen == 0 && p->writeopen == 0)
|
2006-09-06 19:27:19 +02:00
|
|
|
kfree((char*) p, PAGE);
|
2006-06-27 16:35:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
pipe_write(struct pipe *p, char *addr, int n)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2006-07-12 03:48:35 +02:00
|
|
|
acquire(&p->lock);
|
|
|
|
|
2006-06-27 16:35:53 +02:00
|
|
|
for(i = 0; i < n; i++){
|
|
|
|
while(((p->writep + 1) % PIPESIZE) == p->readp){
|
2006-07-15 14:03:57 +02:00
|
|
|
if(p->readopen == 0){
|
|
|
|
release(&p->lock);
|
2006-06-27 16:35:53 +02:00
|
|
|
return -1;
|
2006-07-15 14:03:57 +02:00
|
|
|
}
|
2006-07-01 23:26:01 +02:00
|
|
|
wakeup(&p->readp);
|
2006-07-15 14:03:57 +02:00
|
|
|
sleep(&p->writep, &p->lock);
|
2006-06-27 16:35:53 +02:00
|
|
|
}
|
|
|
|
p->data[p->writep] = addr[i];
|
|
|
|
p->writep = (p->writep + 1) % PIPESIZE;
|
|
|
|
}
|
2006-07-15 14:03:57 +02:00
|
|
|
|
2006-07-12 03:48:35 +02:00
|
|
|
release(&p->lock);
|
2006-07-01 23:26:01 +02:00
|
|
|
wakeup(&p->readp);
|
2006-06-27 16:35:53 +02:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
pipe_read(struct pipe *p, char *addr, int n)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2006-07-15 14:03:57 +02:00
|
|
|
acquire(&p->lock);
|
|
|
|
|
2006-06-27 16:35:53 +02:00
|
|
|
while(p->readp == p->writep){
|
2006-07-15 14:03:57 +02:00
|
|
|
if(p->writeopen == 0){
|
|
|
|
release(&p->lock);
|
2006-06-27 16:35:53 +02:00
|
|
|
return 0;
|
2006-07-15 14:03:57 +02:00
|
|
|
}
|
|
|
|
sleep(&p->readp, &p->lock);
|
2006-06-27 16:35:53 +02: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;
|
|
|
|
}
|
2006-07-15 14:03:57 +02:00
|
|
|
|
2006-07-12 03:48:35 +02:00
|
|
|
release(&p->lock);
|
2006-07-01 23:26:01 +02:00
|
|
|
wakeup(&p->writep);
|
2006-06-27 16:35:53 +02:00
|
|
|
return i;
|
|
|
|
}
|