Mercurial > hg > Members > menikon > CbC_xv6
annotate src/impl/file_impl_pipe.cbc @ 184:8c39a36878b1
fix_include_context_file_at_file_impl
author | anatofuz |
---|---|
date | Mon, 20 Jan 2020 17:45:25 +0900 |
parents | 2842d9e65751 |
children | 00e5213ebabe |
rev | line source |
---|---|
184 | 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 |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
7 // struct spinlock lock; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
8 // char data[PIPESIZE]; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
9 // uint nread; // number of bytes read |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
10 // uint nwrite; // number of bytes written |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
11 // int readopen; // read fd is still open |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
12 // int writeopen; // write fd is still open |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
13 // } pipe; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
14 // ---- |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
15 |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
16 file* createpipe(struct Context* cbc_context) { |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
17 struct file* file = new file(); |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
18 struct pipe* pipe = new pipe(); |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
19 file->file = (union Data*)pipe; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
20 pipe->lock = 0; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
21 pipe->spinlock = 0; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
22 pipe->data = 0; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
23 pipe->nread = 0; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
24 pipe->nwrite = 0; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
25 pipe->readopen = 0; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
26 pipe->writeopen = 0; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
27 file->off = 0; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
28 file->st = NULL; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
29 file->addr = NULL; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
30 file->n = 0; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
31 file->stat = C_statpipe; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
32 file->read = C_readpipe; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
33 file->write = C_writepipe; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
34 file->close = C_closepipe; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
35 return file; |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
36 } |
179 | 37 //Skip: generate_context |
38 __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
|
39 |
170 | 40 goto next(...); |
164
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
41 } |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
42 |
165
21e83548d738
def file_impl_pipe.cbc private code gears
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
164
diff
changeset
|
43 __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
|
44 acquire(&p->lock); |
21e83548d738
def file_impl_pipe.cbc private code gears
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
164
diff
changeset
|
45 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
|
46 } |
164
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
47 |
165
21e83548d738
def file_impl_pipe.cbc private code gears
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
164
diff
changeset
|
48 __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
|
49 if (p->nread == p->nwrite && p->writeopen){ |
177 | 50 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
|
51 } |
21e83548d738
def file_impl_pipe.cbc private code gears
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
164
diff
changeset
|
52 n = 0; |
177 | 53 goto cbc_piperead3(p,addr,n,next); |
54 } | |
55 | |
56 __code piperead2(struct pipe* p, char* addr, int n, __code next(...)) { | |
57 if(proc->killed){ | |
58 release(&p->lock); | |
59 goto cbc_context->error(); | |
60 } | |
61 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
|
62 } |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
63 |
179 | 64 |
65 //Skip: generate_context | |
66 __code cbc_sleep(struct pipe* p, unit* nread, struct spinlock* lock, __code next(...), __code pread(...)){ //:skip | |
166 | 67 if(proc == 0) { |
68 goto cbc_context->panic("sleep"); | |
69 } | |
70 | |
71 if(lk == 0) { | |
72 goto cbc_context->panic("sleep without lk"); | |
73 } | |
74 | |
75 if(lk != &ptable.lock){ //DOC: sleeplock0 | |
76 acquire(&ptable.lock); //DOC: sleeplock1 | |
77 release(lk); | |
78 } | |
79 goto cbc_sched(cbc_sleep1); | |
80 } | |
81 | |
179 | 82 //Skip: generate_context |
83 __code cbc_sched_stub(struct pipe* p, unit* nread, struct spinlock* lock, __code next(...), __code pread(...)){ //:skip | |
166 | 84 proc->chan = chan; |
85 proc->state = SLEEPING; | |
86 proc->lk = lk; | |
87 proc->cbc_next = next1; | |
88 } | |
89 | |
164
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
90 __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
|
91 |
176 | 92 goto next(...); |
164
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
93 } |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
94 |
169 | 95 __code closepipe(struct pipe* file,int fd,__code next(...)) { |
170 | 96 proc->ofile[fd] = 0; |
97 goto cbc_fileclose(f,next); | |
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 |
169 | 100 |
101 __code cbc_fileclose(struct file* file, __code next(...)) { | |
170 | 102 struct file ff; |
103 acquire(*ftable.loc) | |
104 | |
105 if (f->ref < 1) { | |
106 goto cbc_context->kernel_error->panic("file close"); | |
107 } | |
108 goto cbc_fileclose2(f,ff,next); | |
169 | 109 } |
170 | 110 |
111 __code cbc_fileclose2(struct file* file, struct file* ff,__code next(...)) { | |
112 if (--f->ref > 0) { | |
113 release(&ftable.lock); | |
114 goto cbc_context->return(); | |
115 } | |
116 goto cbc_fileclose3(f,ff,next); | |
117 } | |
118 | |
119 __code cbc_fileclose3(struct file* file, struct file* ff,__code next(...)) { | |
176 | 120 *ff = *f; |
121 f->ref = 0; | |
122 f->type = FD_NONE; | |
123 relsease(&ftable.lock); | |
124 | |
125 goto cbc_pipe_close(ff.pipe,ff.writable,next); | |
126 } | |
127 | |
128 __code cbc_pipe_close(struct pipe* p, int writable, __code next(...)) { | |
129 acquire(&p->lock); | |
130 if (writable) { | |
131 goto cbc_pipe_close_writeopen(p,next); | |
170 | 132 } |
176 | 133 goto cbc_pipe_close_readopen(p,next); |
134 } | |
135 | |
136 | |
137 __code cbc_pipe_close_writeopen(struct pipe* p, __code next(...)) { | |
138 p->writeopen = 0; | |
139 goto cbc_wakeup(&p->nread,p,cbc_pipe_release,next); | |
140 } | |
141 | |
142 __code cbc_pipe_close_readopen(struct pipe* p, __code next(...)) { | |
143 p->writeopen = 0; | |
144 goto cbc_->wakeup(&p->nwrite,p,cbc_pipe_release,next); | |
145 } | |
146 |