2006-06-27 16:35:53 +02:00
|
|
|
#include "types.h"
|
2007-08-28 01:26:33 +02:00
|
|
|
#include "defs.h"
|
2006-06-27 16:35:53 +02:00
|
|
|
#include "param.h"
|
|
|
|
#include "mmu.h"
|
|
|
|
#include "proc.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 {
|
2007-08-09 18:56:40 +02:00
|
|
|
int readopen; // read fd is still open
|
|
|
|
int writeopen; // write fd is still open
|
2008-08-22 02:26:22 +02:00
|
|
|
uint writep; // next index to write
|
|
|
|
uint 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
|
2007-08-28 06:22:35 +02:00
|
|
|
pipealloc(struct file **f0, struct file **f1)
|
2006-06-27 16:35:53 +02:00
|
|
|
{
|
2007-08-10 18:35:01 +02:00
|
|
|
struct pipe *p;
|
2006-06-27 16:35:53 +02:00
|
|
|
|
2007-08-10 18:35:01 +02:00
|
|
|
p = 0;
|
|
|
|
*f0 = *f1 = 0;
|
2007-08-27 18:06:19 +02:00
|
|
|
if((*f0 = filealloc()) == 0 || (*f1 = filealloc()) == 0)
|
|
|
|
goto bad;
|
|
|
|
if((p = (struct pipe*)kalloc(PAGE)) == 0)
|
|
|
|
goto bad;
|
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-09-08 15:44:42 +02: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 16:35:53 +02:00
|
|
|
return 0;
|
2007-08-14 20:42:34 +02:00
|
|
|
|
2007-08-28 07:00:53 +02:00
|
|
|
//PAGEBREAK: 20
|
2007-08-27 18:06:19 +02:00
|
|
|
bad:
|
2006-06-27 16:35:53 +02:00
|
|
|
if(p)
|
2007-08-27 18:06:19 +02:00
|
|
|
kfree((char*)p, PAGE);
|
2006-09-08 15:44:42 +02:00
|
|
|
if(*f0){
|
|
|
|
(*f0)->type = FD_NONE;
|
|
|
|
fileclose(*f0);
|
2006-06-27 16:35:53 +02:00
|
|
|
}
|
2006-09-08 15:44:42 +02:00
|
|
|
if(*f1){
|
|
|
|
(*f1)->type = FD_NONE;
|
|
|
|
fileclose(*f1);
|
2006-06-27 16:35:53 +02:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-08-28 06:22:35 +02:00
|
|
|
pipeclose(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);
|
2007-08-28 20:37:41 +02:00
|
|
|
} else {
|
2006-06-27 16:35:53 +02:00
|
|
|
p->readopen = 0;
|
2006-07-01 23:26:01 +02:00
|
|
|
wakeup(&p->writep);
|
|
|
|
}
|
2008-10-15 19:42:56 +02:00
|
|
|
if(p->readopen == 0 && p->writeopen == 0) {
|
|
|
|
release(&p->lock);
|
2007-08-27 18:06:19 +02:00
|
|
|
kfree((char*)p, PAGE);
|
2008-10-15 19:42:56 +02:00
|
|
|
} else
|
|
|
|
release(&p->lock);
|
2006-06-27 16:35:53 +02:00
|
|
|
}
|
|
|
|
|
2007-08-28 07:00:53 +02:00
|
|
|
//PAGEBREAK: 30
|
2006-06-27 16:35:53 +02:00
|
|
|
int
|
2007-08-28 06:22:35 +02:00
|
|
|
pipewrite(struct pipe *p, char *addr, int n)
|
2006-06-27 16:35:53 +02:00
|
|
|
{
|
|
|
|
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++){
|
2008-08-22 02:26:22 +02:00
|
|
|
while(p->writep == p->readp + PIPESIZE) {
|
2007-08-10 18:37:27 +02:00
|
|
|
if(p->readopen == 0 || cp->killed){
|
2006-07-15 14:03:57 +02:00
|
|
|
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
|
|
|
}
|
2008-08-22 02:26:22 +02:00
|
|
|
p->data[p->writep++ % PIPESIZE] = addr[i];
|
2006-06-27 16:35:53 +02:00
|
|
|
}
|
2006-07-01 23:26:01 +02:00
|
|
|
wakeup(&p->readp);
|
2007-08-14 21:10:57 +02:00
|
|
|
release(&p->lock);
|
2006-06-27 16:35:53 +02:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-08-28 06:22:35 +02:00
|
|
|
piperead(struct pipe *p, char *addr, int n)
|
2006-06-27 16:35:53 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2006-07-15 14:03:57 +02:00
|
|
|
acquire(&p->lock);
|
2007-08-27 18:06:19 +02:00
|
|
|
while(p->readp == p->writep && p->writeopen){
|
|
|
|
if(cp->killed){
|
2006-07-15 14:03:57 +02:00
|
|
|
release(&p->lock);
|
2007-08-27 18:06:19 +02:00
|
|
|
return -1;
|
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;
|
2008-08-22 02:26:22 +02:00
|
|
|
addr[i] = p->data[p->readp++ % PIPESIZE];
|
2006-06-27 16:35:53 +02:00
|
|
|
}
|
2006-07-01 23:26:01 +02:00
|
|
|
wakeup(&p->writep);
|
2007-08-14 21:10:57 +02:00
|
|
|
release(&p->lock);
|
2006-06-27 16:35:53 +02:00
|
|
|
return i;
|
|
|
|
}
|