xv6-65oo2/kernel/pipe.c

131 lines
2.5 KiB
C
Raw Normal View History

2006-06-27 14:35:53 +00:00
#include "types.h"
2019-05-31 15:45:42 +00:00
#include "riscv.h"
2007-08-27 23:26:33 +00:00
#include "defs.h"
2006-06-27 14:35:53 +00:00
#include "param.h"
#include "spinlock.h"
2006-06-27 14:35:53 +00:00
#include "proc.h"
#include "fs.h"
2016-09-12 00:59:57 +00:00
#include "sleeplock.h"
#include "file.h"
2006-06-27 14:35:53 +00:00
#define PIPESIZE 512
struct pipe {
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
{
struct pipe *pi;
2006-06-27 14:35:53 +00:00
pi = 0;
2007-08-10 16:35:01 +00:00
*f0 = *f1 = 0;
2007-08-27 16:06:19 +00:00
if((*f0 = filealloc()) == 0 || (*f1 = filealloc()) == 0)
goto bad;
if((pi = (struct pipe*)kalloc()) == 0)
2007-08-27 16:06:19 +00:00
goto bad;
pi->readopen = 1;
pi->writeopen = 1;
pi->nwrite = 0;
pi->nread = 0;
initlock(&pi->lock, "pipe");
2006-09-08 13:44:42 +00:00
(*f0)->type = FD_PIPE;
(*f0)->readable = 1;
(*f0)->writable = 0;
(*f0)->pipe = pi;
2006-09-08 13:44:42 +00:00
(*f1)->type = FD_PIPE;
(*f1)->readable = 0;
(*f1)->writable = 1;
(*f1)->pipe = pi;
2006-06-27 14:35:53 +00:00
return 0;
2007-08-14 18:42:34 +00:00
2007-08-27 16:06:19 +00:00
bad:
if(pi)
kfree((char*)pi);
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
pipeclose(struct pipe *pi, int writable)
2006-06-27 14:35:53 +00:00
{
acquire(&pi->lock);
2006-09-06 18:06:04 +00:00
if(writable){
pi->writeopen = 0;
wakeup(&pi->nread);
2007-08-28 18:37:41 +00:00
} else {
pi->readopen = 0;
wakeup(&pi->nwrite);
}
if(pi->readopen == 0 && pi->writeopen == 0){
release(&pi->lock);
kfree((char*)pi);
} else
release(&pi->lock);
2006-06-27 14:35:53 +00:00
}
int
pipewrite(struct pipe *pi, uint64 addr, int n)
2006-06-27 14:35:53 +00:00
{
int i = 0;
struct proc *pr = myproc();
2006-06-27 14:35:53 +00:00
acquire(&pi->lock);
while(i < n){
if(pi->readopen == 0 || pr->killed){
release(&pi->lock);
return -1;
}
if(pi->nwrite == pi->nread + PIPESIZE){ //DOC: pipewrite-full
wakeup(&pi->nread);
2019-08-19 22:12:19 +00:00
sleep(&pi->nwrite, &pi->lock);
} else {
char ch;
if(copyin(pr->pagetable, &ch, addr + i, 1) == -1)
break;
pi->data[pi->nwrite++ % PIPESIZE] = ch;
i++;
2006-06-27 14:35:53 +00:00
}
}
2019-08-19 22:12:19 +00:00
wakeup(&pi->nread);
release(&pi->lock);
return i;
2006-06-27 14:35:53 +00:00
}
int
piperead(struct pipe *pi, uint64 addr, int n)
2006-06-27 14:35:53 +00:00
{
int i;
struct proc *pr = myproc();
char ch;
2006-06-27 14:35:53 +00:00
acquire(&pi->lock);
while(pi->nread == pi->nwrite && pi->writeopen){ //DOC: pipe-empty
2019-10-16 16:27:08 +00:00
if(pr->killed){
release(&pi->lock);
2007-08-27 16:06:19 +00:00
return -1;
}
sleep(&pi->nread, &pi->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(pi->nread == pi->nwrite)
2006-06-27 14:35:53 +00:00
break;
ch = pi->data[pi->nread++ % PIPESIZE];
if(copyout(pr->pagetable, addr + i, &ch, 1) == -1)
break;
2006-06-27 14:35:53 +00:00
}
wakeup(&pi->nwrite); //DOC: piperead-wakeup
release(&pi->lock);
2006-06-27 14:35:53 +00:00
return i;
}