Mercurial > hg > CbC > CbC_xv6
annotate src/impl/file_impl_pipe.cbc @ 309:c6cbe4711e02
tweak
author | menikon |
---|---|
date | Mon, 03 Feb 2020 15:29:54 +0900 |
parents | 14aa35b56347 |
children |
rev | line source |
---|---|
183 | 1 #include "../../context.h" |
164
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
2 #interface "file.h" |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
3 |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
4 // ---- |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
5 // typedef struct pipe<Impl, Isa> impl file { |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
6 // #define PIPESIZE 512 |
187
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
7 // union Data* file; |
164
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
8 // struct spinlock lock; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
9 // char data[PIPESIZE]; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
10 // uint nread; // number of bytes read |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
11 // uint nwrite; // number of bytes written |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
12 // int readopen; // read fd is still open |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
13 // int writeopen; // write fd is still open |
187
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
14 // |
188
64a1b9b8f08e
remove_interface_field
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
187
diff
changeset
|
15 // int ref; // reference count |
64a1b9b8f08e
remove_interface_field
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
187
diff
changeset
|
16 // char readable; |
64a1b9b8f08e
remove_interface_field
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
187
diff
changeset
|
17 // char writable; |
64a1b9b8f08e
remove_interface_field
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
187
diff
changeset
|
18 // unsigned int off; |
64a1b9b8f08e
remove_interface_field
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
187
diff
changeset
|
19 // |
187
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
20 // // interface field |
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
21 // int n; |
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
22 // char* addr; |
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
23 // |
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
24 // // private code gear |
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
25 // __code piperead1(Impl* pipe, char* addr, int n, __code next(...)); |
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
26 // __code piperead2(Impl* pipe, char* addr, int n, __code next(...)); |
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
27 // __code cbc_pipeclose(Impl* pipe, Isa* file, __code next(...)); |
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
28 // __code cbc_pipeclose2(Impl* pipe, Isa* file, Isa* ff, __code next(...)); |
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
29 // __code cbc_pipeclose3(Impl* pipe, Isa* file, Isa* ff, __code next(...)); |
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
30 // __code cbc_pipeclose4(Impl* pipe, int writable, __code next(...)); |
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
31 // __code cbc_pipe_close_writeopen(Impl* pipe, __code next(...)); |
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
32 // __code cbc_pipe_close_readopen(Impl* pipe, __code next(...)); |
164
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
33 // } pipe; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
34 // ---- |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
35 |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
36 file* createpipe(struct Context* cbc_context) { |
190 | 37 //initialize |
164
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
38 struct file* file = new file(); |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
39 struct pipe* pipe = new pipe(); |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
40 file->file = (union Data*)pipe; |
188
64a1b9b8f08e
remove_interface_field
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
187
diff
changeset
|
41 pipe->file = (union Data*)file; |
190 | 42 |
43 //assign | |
164
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
44 pipe->lock = 0; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
45 pipe->spinlock = 0; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
46 pipe->data = 0; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
47 pipe->nread = 0; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
48 pipe->nwrite = 0; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
49 pipe->readopen = 0; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
50 pipe->writeopen = 0; |
189 | 51 pipe->ref = 0; |
52 pipe->readable = 0; | |
53 pipe->writable = 0; | |
188
64a1b9b8f08e
remove_interface_field
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
187
diff
changeset
|
54 pipe->off = 0; |
64a1b9b8f08e
remove_interface_field
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
187
diff
changeset
|
55 pipe->int = 0; |
185 | 56 pipe->n = 0; |
57 pipe->addr = NULL; | |
190 | 58 |
59 /* | |
60 API | |
164
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
61 file->st = NULL; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
62 file->addr = NULL; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
63 file->n = 0; |
185 | 64 file->fd = 0; |
190 | 65 */ |
66 | |
67 // CodeGear Init | |
185 | 68 pipe->piperead1 = C_piperead1pipe; |
69 pipe->piperead2 = C_piperead2pipe; | |
187
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
70 pipe->cbc_pipeclose = C_cbc_pipeclosepipe; |
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
71 pipe->cbc_pipeclose2 = C_cbc_pipeclose2pipe; |
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
72 pipe->cbc_pipeclose3 = C_cbc_pipeclose3pipe; |
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
73 pipe->cbc_pipeclose4 = C_cbc_pipeclose4pipe; |
185 | 74 pipe->cbc_pipe_close_writeopen = C_cbc_pipe_close_writeopenpipe; |
75 pipe->cbc_pipe_close_readopen = C_cbc_pipe_close_readopenpipe; | |
164
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
76 file->read = C_readpipe; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
77 file->write = C_writepipe; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
78 file->close = C_closepipe; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
79 return file; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
80 } |
184 | 81 |
189 | 82 |
179 | 83 __code statpipe(struct pipe* file, struct stat* st, __code next(...)) { //:skip |
164
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
84 |
170 | 85 goto next(...); |
164
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
86 } |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
87 |
165
21e83548d738
def file_impl_pipe.cbc private code gears
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
164
diff
changeset
|
88 __code readpipe(struct pipe* file, char* addr, int n, __code next(...)) { |
21e83548d738
def file_impl_pipe.cbc private code gears
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
164
diff
changeset
|
89 acquire(&p->lock); |
21e83548d738
def file_impl_pipe.cbc private code gears
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
164
diff
changeset
|
90 goto cbc_piperead1(file,addr,n,next); |
21e83548d738
def file_impl_pipe.cbc private code gears
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
164
diff
changeset
|
91 } |
164
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
92 |
165
21e83548d738
def file_impl_pipe.cbc private code gears
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
164
diff
changeset
|
93 __code piperead1(struct pipe* p, char* addr, int n, __code next(...)) { |
21e83548d738
def file_impl_pipe.cbc private code gears
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
164
diff
changeset
|
94 if (p->nread == p->nwrite && p->writeopen){ |
177 | 95 goto cbc_piperead2(p,addr,n,next); |
165
21e83548d738
def file_impl_pipe.cbc private code gears
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
164
diff
changeset
|
96 } |
21e83548d738
def file_impl_pipe.cbc private code gears
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
164
diff
changeset
|
97 n = 0; |
177 | 98 goto cbc_piperead3(p,addr,n,next); |
99 } | |
100 | |
101 __code piperead2(struct pipe* p, char* addr, int n, __code next(...)) { | |
102 if(proc->killed){ | |
103 release(&p->lock); | |
104 goto cbc_context->error(); | |
105 } | |
106 goto cbc_sleep(p,&p->nread, &p->lock, next,cbc_piperead1); | |
164
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
107 } |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
108 |
179 | 109 |
110 __code cbc_sleep(struct pipe* p, unit* nread, struct spinlock* lock, __code next(...), __code pread(...)){ //:skip | |
166 | 111 if(proc == 0) { |
112 goto cbc_context->panic("sleep"); | |
113 } | |
114 | |
115 if(lk == 0) { | |
116 goto cbc_context->panic("sleep without lk"); | |
117 } | |
118 | |
119 if(lk != &ptable.lock){ //DOC: sleeplock0 | |
120 acquire(&ptable.lock); //DOC: sleeplock1 | |
121 release(lk); | |
122 } | |
123 goto cbc_sched(cbc_sleep1); | |
124 } | |
125 | |
179 | 126 __code cbc_sched_stub(struct pipe* p, unit* nread, struct spinlock* lock, __code next(...), __code pread(...)){ //:skip |
166 | 127 proc->chan = chan; |
128 proc->state = SLEEPING; | |
129 proc->lk = lk; | |
130 proc->cbc_next = next1; | |
131 } | |
132 | |
164
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
133 __code writepipe(struct pipe* file, char* addr, int n, __code next(...)) { |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
134 |
176 | 135 goto next(...); |
164
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
136 } |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
137 |
186 | 138 __code pipeclose(struct pipe* file,int fd,__code next(...)) { |
170 | 139 proc->ofile[fd] = 0; |
186 | 140 goto file->cbc_pipeclose(file->file, next); |
164
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
141 } |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
142 |
169 | 143 |
186 | 144 __code cbc_pipeclose(struct pipe* pipe, struct file* file, __code next(...)) { |
170 | 145 struct file ff; |
146 acquire(*ftable.loc) | |
147 | |
148 if (f->ref < 1) { | |
149 goto cbc_context->kernel_error->panic("file close"); | |
150 } | |
186 | 151 goto pipe->cbc_pipeclose2(f,ff,next); |
169 | 152 } |
170 | 153 |
186 | 154 __code cbc_pipeclose2(struct pipe* pipe,struct file* file, struct file* ff,__code next(...)) { |
170 | 155 if (--f->ref > 0) { |
156 release(&ftable.lock); | |
157 goto cbc_context->return(); | |
158 } | |
186 | 159 goto pipe->cbc_pipeclose3(f,ff,next); |
170 | 160 } |
161 | |
186 | 162 __code cbc_pipeclose3(struct pipe* pipe,struct file* file, struct file* ff,__code next(...)) { |
176 | 163 *ff = *f; |
164 f->ref = 0; | |
165 f->type = FD_NONE; | |
166 relsease(&ftable.lock); | |
187
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
167 struct pipe* p = ff.pipe; |
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
168 int writable = ff.writable; |
176 | 169 |
187
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
170 goto pipe->cbc_pipeclose4(p,writable,next); |
176 | 171 } |
172 | |
187
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
173 __code cbc_pipeclose4(struct pipe* pipe, int writable, __code next(...)) { |
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
174 acquire(&pipe->lock); |
176 | 175 if (writable) { |
187
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
176 goto pipe->cbc_pipe_close_writeopen(next); |
170 | 177 } |
187
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
178 goto pipe->cbc_pipe_close_readopen(next); |
176 | 179 } |
180 | |
181 | |
187
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
182 __code cbc_pipe_close_writeopen(struct pipe* pipe, __code next(...)) { |
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
183 pipe->writeopen = 0; |
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
184 goto cbc_wakeup(&pipe->nread,pipe,cbc_pipe_release,next); |
176 | 185 } |
186 | |
187
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
187 __code cbc_pipe_close_readopen(struct pipe* pipe, __code next(...)) { |
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
188 pipe->readopen = 0; |
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
189 goto cbc_context->wakeup(&pipe->nwrite,pipe,cbc_pipe_release,next); |
176 | 190 } |
191 |