0
|
1 #include "types.h"
|
|
2 #include "defs.h"
|
|
3 #include "param.h"
|
|
4 #include "mmu.h"
|
|
5 #include "proc.h"
|
|
6 #include "fs.h"
|
|
7 #include "file.h"
|
|
8 #include "spinlock.h"
|
|
9
|
|
10 #define PIPESIZE 512
|
|
11
|
|
12 struct pipe {
|
|
13 struct spinlock lock;
|
|
14 char data[PIPESIZE];
|
|
15 uint nread; // number of bytes read
|
|
16 uint nwrite; // number of bytes written
|
|
17 int readopen; // read fd is still open
|
|
18 int writeopen; // write fd is still open
|
|
19 };
|
|
20
|
|
21 int pipealloc(struct file **f0, struct file **f1)
|
|
22 {
|
|
23 struct pipe *p;
|
|
24
|
|
25 p = 0;
|
|
26 *f0 = *f1 = 0;
|
|
27
|
|
28 if((*f0 = filealloc()) == 0 || (*f1 = filealloc()) == 0) {
|
|
29 goto bad;
|
|
30 }
|
|
31
|
|
32 if((p = kmalloc (get_order(sizeof(*p)))) == 0) {
|
|
33 goto bad;
|
|
34 }
|
|
35
|
|
36 p->readopen = 1;
|
|
37 p->writeopen = 1;
|
|
38 p->nwrite = 0;
|
|
39 p->nread = 0;
|
|
40
|
|
41 initlock(&p->lock, "pipe");
|
|
42
|
|
43 (*f0)->type = FD_PIPE;
|
|
44 (*f0)->readable = 1;
|
|
45 (*f0)->writable = 0;
|
|
46 (*f0)->pipe = p;
|
|
47 (*f1)->type = FD_PIPE;
|
|
48 (*f1)->readable = 0;
|
|
49 (*f1)->writable = 1;
|
|
50 (*f1)->pipe = p;
|
|
51
|
|
52 return 0;
|
|
53
|
|
54 //PAGEBREAK: 20
|
|
55 bad:
|
|
56 if(p) {
|
|
57 kfree (p, get_order(sizeof*p));
|
|
58 }
|
|
59
|
|
60 if(*f0) {
|
|
61 fileclose(*f0);
|
|
62 }
|
|
63
|
|
64 if(*f1) {
|
|
65 fileclose(*f1);
|
|
66 }
|
|
67
|
|
68 return -1;
|
|
69 }
|
|
70
|
|
71 void pipeclose(struct pipe *p, int writable)
|
|
72 {
|
|
73 acquire(&p->lock);
|
|
74
|
|
75 if(writable){
|
|
76 p->writeopen = 0;
|
|
77 wakeup(&p->nread);
|
|
78
|
|
79 } else {
|
|
80 p->readopen = 0;
|
|
81 wakeup(&p->nwrite);
|
|
82 }
|
|
83
|
|
84 if(p->readopen == 0 && p->writeopen == 0){
|
|
85 release(&p->lock);
|
|
86 kfree (p, get_order(sizeof(*p)));
|
|
87
|
|
88 } else {
|
|
89 release(&p->lock);
|
|
90 }
|
|
91 }
|
|
92
|
|
93 //PAGEBREAK: 40
|
|
94 int pipewrite(struct pipe *p, char *addr, int n)
|
|
95 {
|
|
96 int i;
|
|
97
|
|
98 acquire(&p->lock);
|
|
99
|
|
100 for(i = 0; i < n; i++){
|
|
101 while(p->nwrite == p->nread + PIPESIZE){ //DOC: pipewrite-full
|
|
102 if(p->readopen == 0 /*|| proc->killed*/){
|
|
103 release(&p->lock);
|
|
104 return -1;
|
|
105 }
|
|
106
|
|
107 wakeup(&p->nread);
|
|
108 sleep(&p->nwrite, &p->lock); //DOC: pipewrite-sleep
|
|
109 }
|
|
110
|
|
111 p->data[p->nwrite++ % PIPESIZE] = addr[i];
|
|
112 }
|
|
113
|
|
114 wakeup(&p->nread); //DOC: pipewrite-wakeup1
|
|
115 release(&p->lock);
|
|
116 return n;
|
|
117 }
|
|
118
|
36
|
119 __code cbc_piperead3(){
|
|
120 struct pipe *p = proc->cbc_arg.cbc_console_arg.p;
|
|
121 int i = proc->cbc_arg.cbc_console_arg.i;
|
37
|
122 __code(*next)(int ret) = proc->cbc_arg.cbc_console_arg.next;
|
36
|
123 release(&p->lock);
|
|
124
|
|
125 goto next(i);
|
|
126 }
|
35
|
127
|
36
|
128 __code cbc_piperead2(){
|
|
129 int i = proc->cbc_arg.cbc_console_arg.i;
|
|
130 int n = proc->cbc_arg.cbc_console_arg.n;
|
|
131 struct pipe *p = proc->cbc_arg.cbc_console_arg.p;
|
|
132 char *addr = proc->cbc_arg.cbc_console_arg.addr;
|
37
|
133 if (i < n && !(p->nread == p->nwrite)) {
|
36
|
134 addr[i] = p->data[p->nread++ % PIPESIZE];
|
|
135 i ++;
|
|
136 proc->cbc_arg.cbc_console_arg.i = i;
|
|
137 proc->cbc_arg.cbc_console_arg.p = p;
|
|
138 proc->cbc_arg.cbc_console_arg.addr = addr;
|
|
139 goto cbc_piperead2();
|
|
140 }
|
|
141 proc->cbc_arg.cbc_console_arg.p = p;
|
|
142 goto cbc_wakeup(&p->nwrite, cbc_piperead3); //DOC: piperead-wakeup
|
|
143 }
|
35
|
144
|
36
|
145 __code cbc_piperead1(){
|
|
146 struct pipe *p = proc->cbc_arg.cbc_console_arg.p;
|
37
|
147 __code(*next)(int ret) = proc->cbc_arg.cbc_console_arg.next;
|
36
|
148 if (p->nread == p->nwrite && p->writeopen){
|
35
|
149 if(proc->killed){
|
|
150 release(&p->lock);
|
|
151 goto next(-1);
|
|
152 }
|
36
|
153 proc->cbc_arg.cbc_console_arg.p = p;
|
|
154 goto cbc_sleep(&p->nread, &p->lock, cbc_piperead1);
|
35
|
155 }
|
36
|
156 int i = 0;
|
|
157 proc->cbc_arg.cbc_console_arg.i = i;
|
|
158 proc->cbc_arg.cbc_console_arg.p = p;
|
|
159 goto cbc_piperead2();
|
|
160 }
|
35
|
161
|
36
|
162 __code cbc_piperead(struct pipe *p, char *addr, int n, __code (*next)(int ret))
|
|
163 {
|
|
164 acquire(&p->lock);
|
|
165 proc->cbc_arg.cbc_console_arg.n = n;
|
|
166 proc->cbc_arg.cbc_console_arg.p = p;
|
|
167 proc->cbc_arg.cbc_console_arg.addr = addr;
|
37
|
168 proc->cbc_arg.cbc_console_arg.next = next;
|
36
|
169 goto cbc_piperead1();
|
|
170 }
|
35
|
171
|
0
|
172 int piperead(struct pipe *p, char *addr, int n)
|
|
173 {
|
|
174 int i;
|
|
175
|
|
176 acquire(&p->lock);
|
|
177
|
|
178 while(p->nread == p->nwrite && p->writeopen){ //DOC: pipe-empty
|
|
179 if(proc->killed){
|
|
180 release(&p->lock);
|
|
181 return -1;
|
|
182 }
|
|
183
|
|
184 sleep(&p->nread, &p->lock); //DOC: piperead-sleep*/
|
|
185 }
|
|
186
|
|
187 for(i = 0; i < n; i++){ //DOC: piperead-copy
|
|
188 if(p->nread == p->nwrite) {
|
|
189 break;
|
|
190 }
|
|
191
|
|
192 addr[i] = p->data[p->nread++ % PIPESIZE];
|
|
193 }
|
|
194
|
|
195 wakeup(&p->nwrite); //DOC: piperead-wakeup
|
|
196 release(&p->lock);
|
|
197
|
|
198 return i;
|
|
199 }
|