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