summaryrefslogtreecommitdiff
path: root/pipe.c
blob: 7ef89e2ea3de83f523de1cd671498bde84ed7749 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "types.h"
#include "param.h"
#include "x86.h"
#include "mmu.h"
#include "proc.h"
#include "defs.h"
#include "file.h"
#include "spinlock.h"

#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;
  char data[PIPESIZE];
};

int
pipe_alloc(struct file **f0, struct file **f1)
{
  *f0 = *f1 = 0;
  struct pipe *p = 0;

  if((*f0 = filealloc()) == 0)
    goto oops;
  if((*f1 = filealloc()) == 0)
    goto oops;
  if((p = (struct pipe*) kalloc(PAGE)) == 0)
    goto oops;
  p->readopen = 1;
  p->writeopen = 1;
  p->writep = 0;
  p->readp = 0;
  initlock(&p->lock, "pipe");
  (*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;
  return 0;
 oops:
  if(p)
    kfree((char*) p, PAGE);
  if(*f0){
    (*f0)->type = FD_NONE;
    fileclose(*f0);
  }
  if(*f1){
    (*f1)->type = FD_NONE;
    fileclose(*f1);
  }
  return -1;
}

void
pipe_close(struct pipe *p, int writable)
{
  acquire(&p->lock);

  if(writable){
    p->writeopen = 0;
    wakeup(&p->readp);
  } else {
    p->readopen = 0;
    wakeup(&p->writep);
  }

  release(&p->lock);

  if(p->readopen == 0 && p->writeopen == 0)
    kfree((char*) p, PAGE);
}

int
pipe_write(struct pipe *p, char *addr, int n)
{
  int i;

  acquire(&p->lock);

  for(i = 0; i < n; i++){
    while(((p->writep + 1) % PIPESIZE) == p->readp){
      if(p->readopen == 0){
        release(&p->lock);
        return -1;
      }
      wakeup(&p->readp);
      sleep(&p->writep, &p->lock);
    }
    p->data[p->writep] = addr[i];
    p->writep = (p->writep + 1) % PIPESIZE;
  }

  release(&p->lock);
  wakeup(&p->readp);
  return i;
}

int
pipe_read(struct pipe *p, char *addr, int n)
{
  int i;

  acquire(&p->lock);

  while(p->readp == p->writep){
    if(p->writeopen == 0){
      release(&p->lock);
      return 0;
    }
    sleep(&p->readp, &p->lock);
  }

  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);
  return i;
}