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