changeset 36:d4e5846ddb48

cbc_piperead
author mir3636
date Fri, 22 Feb 2019 22:15:33 +0900
parents ad1d3b268e2d
children fb3e5a2f76c1
files src/pipe.c src/proc.c
diffstat 2 files changed, 64 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/src/pipe.c	Fri Feb 22 16:32:51 2019 +0900
+++ b/src/pipe.c	Fri Feb 22 22:15:33 2019 +0900
@@ -116,34 +116,59 @@
     return n;
 }
 
-int cbc_piperead(struct pipe *p, char *addr, int n, __code (*next)(int ret))
-{
-    int i;
+__code cbc_piperead3(){
+    struct pipe *p = proc->cbc_arg.cbc_console_arg.p;
+    int i = proc->cbc_arg.cbc_console_arg.i;
+    release(&p->lock);
+
+    goto next(i);
+}
 
-    acquire(&p->lock);
+__code cbc_piperead2(){
+    int i = proc->cbc_arg.cbc_console_arg.i;
+    int n = proc->cbc_arg.cbc_console_arg.n;
+    struct pipe *p = proc->cbc_arg.cbc_console_arg.p;
+    char *addr = proc->cbc_arg.cbc_console_arg.addr;
+    if (i < n) {
+        if(p->nread == p->nwrite) {
+            break;
+        }
+        addr[i] = p->data[p->nread++ % PIPESIZE];
+        i ++;
+        proc->cbc_arg.cbc_console_arg.i = i;
+        proc->cbc_arg.cbc_console_arg.p = p;
+        proc->cbc_arg.cbc_console_arg.addr = addr;
+        goto cbc_piperead2();
+    }
+    proc->cbc_arg.cbc_console_arg.p = p;
+    goto cbc_wakeup(&p->nwrite, cbc_piperead3);  //DOC: piperead-wakeup
+}
 
-    while(p->nread == p->nwrite && p->writeopen){  //DOC: pipe-empty
+__code cbc_piperead1(){
+    struct pipe *p = proc->cbc_arg.cbc_console_arg.p;
+    if (p->nread == p->nwrite && p->writeopen){
         if(proc->killed){
             release(&p->lock);
             goto next(-1);
         }
-
-        sleep(&p->nread, &p->lock); //DOC: piperead-sleep*/
+        proc->cbc_arg.cbc_console_arg.p = p;
+        goto cbc_sleep(&p->nread, &p->lock, cbc_piperead1);        
     }
-
-    for(i = 0; i < n; i++){  //DOC: piperead-copy
-        if(p->nread == p->nwrite) {
-            break;
-        }
+    int i = 0;
+    proc->cbc_arg.cbc_console_arg.i = i;
+    proc->cbc_arg.cbc_console_arg.p = p;
+    goto cbc_piperead2();
+}
 
-        addr[i] = p->data[p->nread++ % PIPESIZE];
-    }
+__code cbc_piperead(struct pipe *p, char *addr, int n, __code (*next)(int ret))
+{
+    acquire(&p->lock);
+    proc->cbc_arg.cbc_console_arg.n = n;
+    proc->cbc_arg.cbc_console_arg.p = p;
+    proc->cbc_arg.cbc_console_arg.addr = addr;
+    goto cbc_piperead1();
+}
 
-    goto cbc_wakeup(&p->nwrite);  //DOC: piperead-wakeup
-    release(&p->lock);
-
-    return i;
-}
 int piperead(struct pipe *p, char *addr, int n)
 {
     int i;
--- a/src/proc.c	Fri Feb 22 16:32:51 2019 +0900
+++ b/src/proc.c	Fri Feb 22 22:15:33 2019 +0900
@@ -528,6 +528,26 @@
     }
 }
 
+__code cbc_wakeup1(void *chan)
+{
+    struct proc *p;
+
+    for(p = ptable.proc; p < &ptable.proc[NPROC]; p++) {
+        if(p->state == SLEEPING && p->chan == chan) {
+            p->state = RUNNABLE;
+        }
+    }
+
+    release(&ptable.lock);
+    goto next();
+}
+
+__code cbc_wakeup(void *chan) 
+{
+    acquire(&ptable.lock);
+    cbc_wakeup1(chan);
+}
+
 // Wake up all processes sleeping on chan.
 void wakeup(void *chan)
 {