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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 169
diff changeset
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
a70c436936e4 impl cbc_sleep
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 165
diff changeset
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
a70c436936e4 impl cbc_sleep
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 165
diff changeset
59 __code cbc_sleep(struct pipe* p, unit* nread, struct spinlock* lock, __code next(...), __code pread(...)){
a70c436936e4 impl cbc_sleep
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 165
diff changeset
60 if(proc == 0) {
a70c436936e4 impl cbc_sleep
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 165
diff changeset
61 goto cbc_context->panic("sleep");
a70c436936e4 impl cbc_sleep
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 165
diff changeset
62 }
a70c436936e4 impl cbc_sleep
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 165
diff changeset
63
a70c436936e4 impl cbc_sleep
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 165
diff changeset
64 if(lk == 0) {
a70c436936e4 impl cbc_sleep
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 165
diff changeset
65 goto cbc_context->panic("sleep without lk");
a70c436936e4 impl cbc_sleep
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 165
diff changeset
66 }
a70c436936e4 impl cbc_sleep
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 165
diff changeset
67
a70c436936e4 impl cbc_sleep
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 165
diff changeset
68 if(lk != &ptable.lock){ //DOC: sleeplock0
a70c436936e4 impl cbc_sleep
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 165
diff changeset
69 acquire(&ptable.lock); //DOC: sleeplock1
a70c436936e4 impl cbc_sleep
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 165
diff changeset
70 release(lk);
a70c436936e4 impl cbc_sleep
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 165
diff changeset
71 }
a70c436936e4 impl cbc_sleep
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 165
diff changeset
72 goto cbc_sched(cbc_sleep1);
a70c436936e4 impl cbc_sleep
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 165
diff changeset
73 }
a70c436936e4 impl cbc_sleep
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 165
diff changeset
74
a70c436936e4 impl cbc_sleep
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 165
diff changeset
75 __code cbc_sched_stub(struct pipe* p, unit* nread, struct spinlock* lock, __code next(...), __code pread(...)){
a70c436936e4 impl cbc_sleep
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 165
diff changeset
76 proc->chan = chan;
a70c436936e4 impl cbc_sleep
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 165
diff changeset
77 proc->state = SLEEPING;
a70c436936e4 impl cbc_sleep
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 165
diff changeset
78 proc->lk = lk;
a70c436936e4 impl cbc_sleep
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 165
diff changeset
79 proc->cbc_next = next1;
a70c436936e4 impl cbc_sleep
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 165
diff changeset
80 }
a70c436936e4 impl cbc_sleep
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 165
diff changeset
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
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 166
diff changeset
87 __code closepipe(struct pipe* file,int fd,__code next(...)) {
170
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 169
diff changeset
88 proc->ofile[fd] = 0;
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 169
diff changeset
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
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 166
diff changeset
92
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 166
diff changeset
93 __code cbc_fileclose(struct file* file, __code next(...)) {
170
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 169
diff changeset
94 struct file ff;
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 169
diff changeset
95 acquire(*ftable.loc)
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 169
diff changeset
96
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 169
diff changeset
97 if (f->ref < 1) {
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 169
diff changeset
98 goto cbc_context->kernel_error->panic("file close");
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 169
diff changeset
99 }
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 169
diff changeset
100 goto cbc_fileclose2(f,ff,next);
169
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 166
diff changeset
101 }
170
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 169
diff changeset
102
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 169
diff changeset
103 __code cbc_fileclose2(struct file* file, struct file* ff,__code next(...)) {
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 169
diff changeset
104 if (--f->ref > 0) {
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 169
diff changeset
105 release(&ftable.lock);
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 169
diff changeset
106 goto cbc_context->return();
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 169
diff changeset
107 }
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 169
diff changeset
108 goto cbc_fileclose3(f,ff,next);
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 169
diff changeset
109 }
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 169
diff changeset
110
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 169
diff changeset
111 __code cbc_fileclose3(struct file* file, struct file* ff,__code next(...)) {
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 169
diff changeset
112 if (--f->ref > 0) {
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 169
diff changeset
113 release(&ftable.lock);
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 169
diff changeset
114 goto cbc_context->return();
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 169
diff changeset
115 }
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 169
diff changeset
116 goto cbc_fileclose3(f,ff,next);
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 169
diff changeset
117 }