Mercurial > hg > CbC > CbC_xv6
annotate src/impl/file_impl_pipe.cbc @ 189:000e247dc51d
tweak
author | anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 21 Jan 2020 20:12:16 +0900 (2020-01-21) |
parents | 64a1b9b8f08e |
children | 14aa35b56347 |
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) { |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
37 struct file* file = new file(); |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
38 struct pipe* pipe = new pipe(); |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
39 file->file = (union Data*)pipe; |
188
64a1b9b8f08e
remove_interface_field
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
187
diff
changeset
|
40 pipe->file = (union Data*)file; |
164
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
41 pipe->lock = 0; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
42 pipe->spinlock = 0; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
43 pipe->data = 0; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
44 pipe->nread = 0; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
45 pipe->nwrite = 0; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
46 pipe->readopen = 0; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
47 pipe->writeopen = 0; |
189 | 48 pipe->ref = 0; |
49 pipe->readable = 0; | |
50 pipe->writable = 0; | |
188
64a1b9b8f08e
remove_interface_field
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
187
diff
changeset
|
51 pipe->off = 0; |
64a1b9b8f08e
remove_interface_field
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
187
diff
changeset
|
52 pipe->int = 0; |
185 | 53 pipe->n = 0; |
54 pipe->addr = NULL; | |
164
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
55 file->st = NULL; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
56 file->addr = NULL; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
57 file->n = 0; |
185 | 58 file->fd = 0; |
59 pipe->piperead1 = C_piperead1pipe; | |
60 pipe->piperead2 = C_piperead2pipe; | |
187
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
61 pipe->cbc_pipeclose = C_cbc_pipeclosepipe; |
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
62 pipe->cbc_pipeclose2 = C_cbc_pipeclose2pipe; |
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
63 pipe->cbc_pipeclose3 = C_cbc_pipeclose3pipe; |
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
64 pipe->cbc_pipeclose4 = C_cbc_pipeclose4pipe; |
185 | 65 pipe->cbc_pipe_close_writeopen = C_cbc_pipe_close_writeopenpipe; |
66 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
|
67 file->read = C_readpipe; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
68 file->write = C_writepipe; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
69 file->close = C_closepipe; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
70 return file; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
71 } |
184 | 72 |
189 | 73 |
179 | 74 __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
|
75 |
170 | 76 goto next(...); |
164
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
77 } |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
78 |
165
21e83548d738
def file_impl_pipe.cbc private code gears
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
164
diff
changeset
|
79 __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
|
80 acquire(&p->lock); |
21e83548d738
def file_impl_pipe.cbc private code gears
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
164
diff
changeset
|
81 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
|
82 } |
164
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
83 |
165
21e83548d738
def file_impl_pipe.cbc private code gears
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
164
diff
changeset
|
84 __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
|
85 if (p->nread == p->nwrite && p->writeopen){ |
177 | 86 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
|
87 } |
21e83548d738
def file_impl_pipe.cbc private code gears
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
164
diff
changeset
|
88 n = 0; |
177 | 89 goto cbc_piperead3(p,addr,n,next); |
90 } | |
91 | |
92 __code piperead2(struct pipe* p, char* addr, int n, __code next(...)) { | |
93 if(proc->killed){ | |
94 release(&p->lock); | |
95 goto cbc_context->error(); | |
96 } | |
97 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
|
98 } |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
99 |
179 | 100 |
101 __code cbc_sleep(struct pipe* p, unit* nread, struct spinlock* lock, __code next(...), __code pread(...)){ //:skip | |
166 | 102 if(proc == 0) { |
103 goto cbc_context->panic("sleep"); | |
104 } | |
105 | |
106 if(lk == 0) { | |
107 goto cbc_context->panic("sleep without lk"); | |
108 } | |
109 | |
110 if(lk != &ptable.lock){ //DOC: sleeplock0 | |
111 acquire(&ptable.lock); //DOC: sleeplock1 | |
112 release(lk); | |
113 } | |
114 goto cbc_sched(cbc_sleep1); | |
115 } | |
116 | |
179 | 117 __code cbc_sched_stub(struct pipe* p, unit* nread, struct spinlock* lock, __code next(...), __code pread(...)){ //:skip |
166 | 118 proc->chan = chan; |
119 proc->state = SLEEPING; | |
120 proc->lk = lk; | |
121 proc->cbc_next = next1; | |
122 } | |
123 | |
164
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
124 __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
|
125 |
176 | 126 goto next(...); |
164
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
127 } |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
128 |
186 | 129 __code pipeclose(struct pipe* file,int fd,__code next(...)) { |
170 | 130 proc->ofile[fd] = 0; |
186 | 131 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
|
132 } |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
133 |
169 | 134 |
186 | 135 __code cbc_pipeclose(struct pipe* pipe, struct file* file, __code next(...)) { |
170 | 136 struct file ff; |
137 acquire(*ftable.loc) | |
138 | |
139 if (f->ref < 1) { | |
140 goto cbc_context->kernel_error->panic("file close"); | |
141 } | |
186 | 142 goto pipe->cbc_pipeclose2(f,ff,next); |
169 | 143 } |
170 | 144 |
186 | 145 __code cbc_pipeclose2(struct pipe* pipe,struct file* file, struct file* ff,__code next(...)) { |
170 | 146 if (--f->ref > 0) { |
147 release(&ftable.lock); | |
148 goto cbc_context->return(); | |
149 } | |
186 | 150 goto pipe->cbc_pipeclose3(f,ff,next); |
170 | 151 } |
152 | |
186 | 153 __code cbc_pipeclose3(struct pipe* pipe,struct file* file, struct file* ff,__code next(...)) { |
176 | 154 *ff = *f; |
155 f->ref = 0; | |
156 f->type = FD_NONE; | |
157 relsease(&ftable.lock); | |
187
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
158 struct pipe* p = ff.pipe; |
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
159 int writable = ff.writable; |
176 | 160 |
187
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
161 goto pipe->cbc_pipeclose4(p,writable,next); |
176 | 162 } |
163 | |
187
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
164 __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
|
165 acquire(&pipe->lock); |
176 | 166 if (writable) { |
187
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
167 goto pipe->cbc_pipe_close_writeopen(next); |
170 | 168 } |
187
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
169 goto pipe->cbc_pipe_close_readopen(next); |
176 | 170 } |
171 | |
172 | |
187
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
173 __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
|
174 pipe->writeopen = 0; |
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
175 goto cbc_wakeup(&pipe->nread,pipe,cbc_pipe_release,next); |
176 | 176 } |
177 | |
187
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
178 __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
|
179 pipe->readopen = 0; |
9d385a07dbfc
aligned API at pipe close...
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
186
diff
changeset
|
180 goto cbc_context->wakeup(&pipe->nwrite,pipe,cbc_pipe_release,next); |
176 | 181 } |
182 |