2006-06-27 14:35:53 +00:00
|
|
|
#include "types.h"
|
2007-08-27 23:26:33 +00:00
|
|
|
#include "defs.h"
|
2006-06-27 14:35:53 +00:00
|
|
|
#include "param.h"
|
|
|
|
#include "mmu.h"
|
|
|
|
#include "proc.h"
|
2009-08-08 08:07:30 +00:00
|
|
|
#include "fs.h"
|
2011-08-26 14:08:29 +00:00
|
|
|
#include "file.h"
|
2011-08-29 21:18:40 +00:00
|
|
|
#include "spinlock.h"
|
2006-06-27 14:35:53 +00:00
|
|
|
|
|
|
|
#define PIPESIZE 512
|
|
|
|
|
|
|
|
struct pipe {
|
2006-07-12 01:48:35 +00:00
|
|
|
struct spinlock lock;
|
2006-06-27 14:35:53 +00:00
|
|
|
char data[PIPESIZE];
|
2009-07-13 01:33:37 +00:00
|
|
|
uint nread; // number of bytes read
|
|
|
|
uint nwrite; // number of bytes written
|
|
|
|
int readopen; // read fd is still open
|
|
|
|
int writeopen; // write fd is still open
|
2006-06-27 14:35:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
int
|
2007-08-28 04:22:35 +00:00
|
|
|
pipealloc(struct file **f0, struct file **f1)
|
2006-06-27 14:35:53 +00:00
|
|
|
{
|
2007-08-10 16:35:01 +00:00
|
|
|
struct pipe *p;
|
2006-06-27 14:35:53 +00:00
|
|
|
|
2007-08-10 16:35:01 +00:00
|
|
|
p = 0;
|
|
|
|
*f0 = *f1 = 0;
|
2007-08-27 16:06:19 +00:00
|
|
|
if((*f0 = filealloc()) == 0 || (*f1 = filealloc()) == 0)
|
|
|
|
goto bad;
|
2010-08-31 16:54:47 +00:00
|
|
|
if((p = (struct pipe*)kalloc()) == 0)
|
2007-08-27 16:06:19 +00:00
|
|
|
goto bad;
|
2006-07-01 21:26:01 +00:00
|
|
|
p->readopen = 1;
|
|
|
|
p->writeopen = 1;
|
2009-07-13 01:33:37 +00:00
|
|
|
p->nwrite = 0;
|
|
|
|
p->nread = 0;
|
2006-08-10 22:08:14 +00:00
|
|
|
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;
|
2007-08-14 18:42:34 +00:00
|
|
|
|
2007-08-28 05:00:53 +00:00
|
|
|
//PAGEBREAK: 20
|
2007-08-27 16:06:19 +00:00
|
|
|
bad:
|
2006-06-27 14:35:53 +00:00
|
|
|
if(p)
|
2010-08-31 16:54:47 +00:00
|
|
|
kfree((char*)p);
|
2009-05-31 02:07:51 +00:00
|
|
|
if(*f0)
|
2006-09-08 13:44:42 +00:00
|
|
|
fileclose(*f0);
|
2009-05-31 02:07:51 +00:00
|
|
|
if(*f1)
|
2006-09-08 13:44:42 +00:00
|
|
|
fileclose(*f1);
|
2006-06-27 14:35:53 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-08-28 04:22:35 +00:00
|
|
|
pipeclose(struct pipe *p, int writable)
|
2006-06-27 14:35:53 +00:00
|
|
|
{
|
2006-07-18 19:22:37 +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;
|
2009-07-13 01:33:37 +00:00
|
|
|
wakeup(&p->nread);
|
2007-08-28 18:37:41 +00:00
|
|
|
} else {
|
2006-06-27 14:35:53 +00:00
|
|
|
p->readopen = 0;
|
2009-07-13 01:33:37 +00:00
|
|
|
wakeup(&p->nwrite);
|
2006-07-01 21:26:01 +00:00
|
|
|
}
|
2011-01-11 18:01:13 +00:00
|
|
|
if(p->readopen == 0 && p->writeopen == 0){
|
2008-10-15 17:42:56 +00:00
|
|
|
release(&p->lock);
|
2010-08-31 16:54:47 +00:00
|
|
|
kfree((char*)p);
|
2008-10-15 17:42:56 +00:00
|
|
|
} else
|
|
|
|
release(&p->lock);
|
2006-06-27 14:35:53 +00:00
|
|
|
}
|
|
|
|
|
2009-08-08 08:07:30 +00:00
|
|
|
//PAGEBREAK: 40
|
2006-06-27 14:35:53 +00:00
|
|
|
int
|
2007-08-28 04:22:35 +00:00
|
|
|
pipewrite(struct pipe *p, char *addr, int n)
|
2006-06-27 14:35:53 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2006-07-12 01:48:35 +00:00
|
|
|
acquire(&p->lock);
|
2006-06-27 14:35:53 +00:00
|
|
|
for(i = 0; i < n; i++){
|
2011-01-11 18:01:13 +00:00
|
|
|
while(p->nwrite == p->nread + PIPESIZE){ //DOC: pipewrite-full
|
2009-08-31 06:02:08 +00:00
|
|
|
if(p->readopen == 0 || proc->killed){
|
2006-07-15 12:03:57 +00:00
|
|
|
release(&p->lock);
|
2006-06-27 14:35:53 +00:00
|
|
|
return -1;
|
2006-07-15 12:03:57 +00:00
|
|
|
}
|
2009-07-13 01:33:37 +00:00
|
|
|
wakeup(&p->nread);
|
|
|
|
sleep(&p->nwrite, &p->lock); //DOC: pipewrite-sleep
|
2006-06-27 14:35:53 +00:00
|
|
|
}
|
2009-07-13 01:33:37 +00:00
|
|
|
p->data[p->nwrite++ % PIPESIZE] = addr[i];
|
2006-06-27 14:35:53 +00:00
|
|
|
}
|
2009-07-13 01:33:37 +00:00
|
|
|
wakeup(&p->nread); //DOC: pipewrite-wakeup1
|
2007-08-14 19:10:57 +00:00
|
|
|
release(&p->lock);
|
2009-07-13 01:33:37 +00:00
|
|
|
return n;
|
2006-06-27 14:35:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-08-28 04:22:35 +00:00
|
|
|
piperead(struct pipe *p, char *addr, int n)
|
2006-06-27 14:35:53 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2006-07-15 12:03:57 +00:00
|
|
|
acquire(&p->lock);
|
2009-07-13 01:33:37 +00:00
|
|
|
while(p->nread == p->nwrite && p->writeopen){ //DOC: pipe-empty
|
2009-08-31 06:02:08 +00:00
|
|
|
if(proc->killed){
|
2006-07-15 12:03:57 +00:00
|
|
|
release(&p->lock);
|
2007-08-27 16:06:19 +00:00
|
|
|
return -1;
|
2006-07-15 12:03:57 +00:00
|
|
|
}
|
2009-07-13 01:33:37 +00:00
|
|
|
sleep(&p->nread, &p->lock); //DOC: piperead-sleep
|
2006-06-27 14:35:53 +00:00
|
|
|
}
|
2009-07-13 01:33:37 +00:00
|
|
|
for(i = 0; i < n; i++){ //DOC: piperead-copy
|
|
|
|
if(p->nread == p->nwrite)
|
2006-06-27 14:35:53 +00:00
|
|
|
break;
|
2009-07-13 01:33:37 +00:00
|
|
|
addr[i] = p->data[p->nread++ % PIPESIZE];
|
2006-06-27 14:35:53 +00:00
|
|
|
}
|
2009-07-13 01:33:37 +00:00
|
|
|
wakeup(&p->nwrite); //DOC: piperead-wakeup
|
2007-08-14 19:10:57 +00:00
|
|
|
release(&p->lock);
|
2006-06-27 14:35:53 +00:00
|
|
|
return i;
|
|
|
|
}
|