Mercurial > hg > Members > menikon > CbC_xv6
annotate src/impl/file_impl_pipe.cbc @ 170:9b0f4b421288
tweak
author | anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 16 Jan 2020 20:34:18 +0900 |
parents | e0255e66e646 |
children | da4c83ae7ada |
rev | line source |
---|---|
164
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
1 #include "../context.h" |
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 } |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
37 __code statpipe(struct pipe* file, struct stat* st, __code next(...)) { |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
38 |
170 | 39 goto next(...); |
164
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
40 } |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
41 |
165
21e83548d738
def file_impl_pipe.cbc private code gears
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
164
diff
changeset
|
42 __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
|
43 acquire(&p->lock); |
21e83548d738
def file_impl_pipe.cbc private code gears
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
164
diff
changeset
|
44 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
|
45 } |
164
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
46 |
165
21e83548d738
def file_impl_pipe.cbc private code gears
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
164
diff
changeset
|
47 __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
|
48 if (p->nread == p->nwrite && p->writeopen){ |
21e83548d738
def file_impl_pipe.cbc private code gears
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
164
diff
changeset
|
49 if(proc->killed){ |
21e83548d738
def file_impl_pipe.cbc private code gears
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
164
diff
changeset
|
50 release(&p->lock); |
21e83548d738
def file_impl_pipe.cbc private code gears
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
164
diff
changeset
|
51 goto cbc_context->error(); |
21e83548d738
def file_impl_pipe.cbc private code gears
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
164
diff
changeset
|
52 } |
166 | 53 goto cbc_sleep(p,&p->nread, &p->lock, next,cbc_piperead1); |
165
21e83548d738
def file_impl_pipe.cbc private code gears
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
164
diff
changeset
|
54 } |
21e83548d738
def file_impl_pipe.cbc private code gears
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
164
diff
changeset
|
55 n = 0; |
21e83548d738
def file_impl_pipe.cbc private code gears
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
164
diff
changeset
|
56 goto cbc_piperead2(p,n); |
164
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
57 } |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
58 |
166 | 59 __code cbc_sleep(struct pipe* p, unit* nread, struct spinlock* lock, __code next(...), __code pread(...)){ |
60 if(proc == 0) { | |
61 goto cbc_context->panic("sleep"); | |
62 } | |
63 | |
64 if(lk == 0) { | |
65 goto cbc_context->panic("sleep without lk"); | |
66 } | |
67 | |
68 if(lk != &ptable.lock){ //DOC: sleeplock0 | |
69 acquire(&ptable.lock); //DOC: sleeplock1 | |
70 release(lk); | |
71 } | |
72 goto cbc_sched(cbc_sleep1); | |
73 } | |
74 | |
75 __code cbc_sched_stub(struct pipe* p, unit* nread, struct spinlock* lock, __code next(...), __code pread(...)){ | |
76 proc->chan = chan; | |
77 proc->state = SLEEPING; | |
78 proc->lk = lk; | |
79 proc->cbc_next = next1; | |
80 } | |
81 | |
164
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
82 __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
|
83 |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
84 goto next(...); |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
85 } |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
86 |
169 | 87 __code closepipe(struct pipe* file,int fd,__code next(...)) { |
170 | 88 proc->ofile[fd] = 0; |
89 goto cbc_fileclose(f,next); | |
164
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
90 } |
9c501dca25e3
add file_impl_pipe.cbc
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
91 |
169 | 92 |
93 __code cbc_fileclose(struct file* file, __code next(...)) { | |
170 | 94 struct file ff; |
95 acquire(*ftable.loc) | |
96 | |
97 if (f->ref < 1) { | |
98 goto cbc_context->kernel_error->panic("file close"); | |
99 } | |
100 goto cbc_fileclose2(f,ff,next); | |
169 | 101 } |
170 | 102 |
103 __code cbc_fileclose2(struct file* file, struct file* ff,__code next(...)) { | |
104 if (--f->ref > 0) { | |
105 release(&ftable.lock); | |
106 goto cbc_context->return(); | |
107 } | |
108 goto cbc_fileclose3(f,ff,next); | |
109 } | |
110 | |
111 __code cbc_fileclose3(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 } |