changeset 380:8f2bc03fcbb2

impl ReadSyscall*.cbc
author anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Mon, 20 Jul 2020 14:14:24 +0900
parents a0d6f7124b9b
children 874577d7505f
files src/impl/ReadSyscallEntry.cbc src/impl/ReadSyscallImpl.cbc
diffstat 2 files changed, 81 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/src/impl/ReadSyscallEntry.cbc	Mon Jul 13 16:10:06 2020 +0900
+++ b/src/impl/ReadSyscallEntry.cbc	Mon Jul 20 14:14:24 2020 +0900
@@ -1,5 +1,7 @@
 #include "../context.h"
 #interface "SyscallEntry.h"
+#interface "ReadSyscall.h"
+
 
 // ----
 // typedef struct ReadSyscallEntry <Type, Isa> impl SyscallEntry {
@@ -14,8 +16,11 @@
     syscall_entry->exec = C_execReadSyscallEntry;
     return syscall_entry;
 }
-__code execReadSyscallEntry(struct ReadSyscallEntry* syscall_entry, __code next(...)) {
+
+extern ReadSyscall* createReadSyscallImpl(struct Context*);
 
-    goto next(...);
+__code execReadSyscallEntry(struct ReadSyscallEntry* syscall_entry, __code next(...)) {
+    ReadSyscall* read_syscall = createReadSyscallImpl(cbc_context);
+    goto read_syscall->fileread(next);
 }
 
--- a/src/impl/ReadSyscallImpl.cbc	Mon Jul 13 16:10:06 2020 +0900
+++ b/src/impl/ReadSyscallImpl.cbc	Mon Jul 20 14:14:24 2020 +0900
@@ -1,5 +1,6 @@
 #include "../context.h"
 #interface "ReadSyscall.h"
+#interface "KernelRet.h"
 
 // ----
 // typedef struct ReadSyscallImpl <Type, Isa> impl ReadSyscall {
@@ -22,19 +23,64 @@
     read_syscall->fileread1 = C_fileread1ReadSyscallImpl;
     return read_syscall;
 }
+
 __code consolereadReadSyscallImpl(struct ReadSyscallImpl* read_syscall, struct inode* ip, char* dst, int n, __code next(int ret, ...)) {
+    uint target;
+
+    iunlock(ip);
 
-    goto next(ret, ...);
+    //target = n;
+    acquire(&input.lock);
+
+    if (n > 0) {
+        goto read_syscall->consoleread2(...);
+    }
+
+    goto read_syscall->consoleread1(...);
 }
 
 __code consoleread1ReadSyscallImpl(struct ReadSyscallImpl* read_syscall,int n, int target, char* dst, struct inode* ip,  __code next(...)) {
+    int cont = 1;
+    int c = input.buf[input.r++ % INPUT_BUF];
 
-    goto next(...);
+    if (c == C('D')) {  // EOF
+        if (n < target) {
+            // Save ^D for next time, to make sure
+            // caller gets a 0-byte result.
+            input.r--;
+        }
+        cont = 0;
+    }
+
+    *dst++ = c;
+    --n;
+
+    if (c == '\n') {
+        cont = 0;
+    }
+
+    if (cont == 1) {
+        if (n > 0) {
+            goto cbc_sleep(&input.r, &input.lock, read_syscall->consoleread2);
+        }
+    }
+
+    release(&input.lock);
+    ilock(ip);
+    goto next(target - n);
 }
 
 __code consoleread2ReadSyscallImpl(struct ReadSyscallImpl* read_syscall,struct inode* ip, __code next(...)) {
-
-    goto next(...);
+    if (input.r == input.w) {
+        if (proc->killed) {
+            release(&input.lock);
+            ilock(ip);
+            KernelRet* kernel_ret = createKernelRetImpl(cbc_context);
+            goto kernel_ret->cbc_return(-1);
+        }
+       goto cbc_sleep(&input.r, &input.lock, cbc_consoleread2);
+    }
+    goto read_sycall->consoleread1();
 }
 
 __code pipereadReadSyscallImpl(struct ReadSyscallImpl* read_syscall, struct pipe* p,char* addr, int n,  __code next(int ret,...)) {
@@ -43,24 +89,38 @@
 }
 
 __code piperead1ReadSyscallImpl(struct ReadSyscallImpl* read_syscall, struct pipe* p, __code next(int ret,...)) {
-
-    goto next(ret,...);
+    if (p->nread == p->nwrite && p->writeopen){
+        if(proc->killed){
+            release(&p->lock);
+            KernelRet* kernel_ret = createKernelRetImpl(cbc_context);
+            goto kernel_ret->cbc_return(-1);
+        }
+        goto cbc_sleep(&p->nread, &p->lock, cbc_piperead1);
+    }
+    goto read_syscall->piperead2(...);
 }
 
-__code piperead2ReadSyscallImpl(struct ReadSyscallImpl* read_syscall, int i, int n, struct pipe* p, char* addr, __code next(int ret,...)) {
-
-    goto next(ret,...);
+__code piperead2ReadSyscallImpl(struct ReadSyscallImpl* read_syscall, int i, int n, struct pipe* p, char* addr, __code next(...)) {
+    if (i < n && !(p->nread == p->nwrite)) {
+        addr[i] = p->data[p->nread++ % PIPESIZE];
+        i ++;
+        goto read_syscall->piperead2(i,n, p, addr,next);
+    }
+    //proc->cbc_arg.cbc_console_arg.p = p;
+    goto cbc_wakeup(&p->nwrite, cbc_piperead3);  //DOC: piperead-wakeup
 }
 
 __code piperead3ReadSyscallImpl(struct ReadSyscallImpl* read_syscall, struct pipe* p, int i, __code next(int ret,...)) {
-
-    goto next(ret,...);
+    release(&p->lock);
+    KernelRet* kernel_ret = createKernelRetImpl(cbc_context);
+    goto kernel_ret->cbc_return(-1);
 }
 
 __code filereadReadSyscallImpl(struct ReadSyscallImpl* read_syscall, struct file* f, char* addr, int n, __code next(int ret, ...)) {
     if (f->readable == 0) {
         ret = -1;
-        goto next(ret);
+        KernelRet* kernel_ret = createKernelRetImpl(cbc_context);
+        goto kernel_ret->cbc_return(ret);
     }
 
     if (f->type == FD_PIPE) {
@@ -79,6 +139,7 @@
     if (ret > 0)
         f->off += ret;
     iunlock(f->ip);
-    goto next(ret);
+    KernelRet* kernel_ret = createKernelRetImpl(cbc_context);
+    goto kernel_ret->cbc_return(ret);
 }