[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[tyndur-devel] [PATCH] libc: Synchrones Lesen in LIO



* libc: Anfaenge von Synchronem Lesen in LostIO brauchbar gemacht.
! vterm: lostio_dispatch() nur aufrufen wenn neue Eingaben da sind, und
         nicht nach jedem rpc.
---
 src/modules/lib/lostio/handler.c                 |   18 +++++---
 src/modules/lib/lostio/include/lostio_internal.h |    9 ++++
 src/modules/lib/lostio/sync.c                    |   49 +++++++++++-----------
 src/modules/lib/stdlibc/file.c                   |    4 ++
 src/modules/vterm/input.c                        |    3 +
 src/modules/vterm/vterm.c                        |    1 -
 6 files changed, 53 insertions(+), 31 deletions(-)

diff --git a/src/modules/lib/lostio/handler.c b/src/modules/lib/lostio/handler.c
index 0c9c1b6..5e8f361 100644
--- a/src/modules/lib/lostio/handler.c
+++ b/src/modules/lib/lostio/handler.c
@@ -191,7 +191,12 @@ void rpc_io_read(pid_t pid, dword correlation_id, size_t data_size, void* data)
         size_t size = typehandle->read(filehandle, buf,
             read_request->blocksize, read_request->blockcount);
 
-        rpc_send_response(pid, correlation_id, size, (char*) buf);
+        if (!size && (filehandle->flags & IO_OPEN_MODE_SYNC)) {
+            // Synchrones lesen
+            lostio_sync_read_wait(pid, correlation_id, read_request);
+        } else {
+            rpc_send_response(pid, correlation_id, size, (char*) buf);
+        }
         return;
     }
 
@@ -200,11 +205,12 @@ void rpc_io_read(pid_t pid, dword correlation_id, size_t data_size, void* data)
     size_t size = typehandle->read(filehandle, shm_ptr,
             read_request->blocksize, read_request->blockcount);
     close_shared_memory(read_request->shared_mem_id);
-
-/*            printf("size = %d, ptr = %p, value = %d\n",
-        sizeof(reply.size), &reply.size, reply.size);
-    fflush(stdout);*/
-    rpc_send_dword_response(pid, correlation_id, size);
+    if (!size && (filehandle->flags & IO_OPEN_MODE_SYNC)) {
+        // Synchrones lesen
+        lostio_sync_read_wait(pid, correlation_id, read_request);
+    } else {
+        rpc_send_dword_response(pid, correlation_id, size);
+    }
     //v();
 }
 
diff --git a/src/modules/lib/lostio/include/lostio_internal.h b/src/modules/lib/lostio/include/lostio_internal.h
index ba7a691..49fa280 100644
--- a/src/modules/lib/lostio/include/lostio_internal.h
+++ b/src/modules/lib/lostio/include/lostio_internal.h
@@ -76,6 +76,15 @@ void    rpc_io_unlink(pid_t pid, dword correlation_id, size_t data_size, void* d
 //bool vfstree_delete_node(char* path);
 
 
+/**
+ * Schiebt eine Anfrage fuer synchrones Lesen auf, falls keine Daten vorhanden
+ * sind.
+ *
+ * @param read_request Gesendete anfrage
+ */
+void lostio_sync_read_wait(pid_t pid, dword correlation_id,
+    io_read_request_t* read_request);
+
 
 ///Filehandle aus der Liste holen
 lostio_filehandle_t* get_filehandle(pid_t pid, dword id);
diff --git a/src/modules/lib/lostio/sync.c b/src/modules/lib/lostio/sync.c
index d57e7bd..d6e126f 100644
--- a/src/modules/lib/lostio/sync.c
+++ b/src/modules/lib/lostio/sync.c
@@ -36,11 +36,11 @@
 #include "stdlib.h"
 #include "string.h"
 #include "stdio.h"
+#include <syscall.h>
 
 /// In dieser Struktur werden die Informationen zu aufgeschobenen reads
 /// gespeichert
 struct sync_read_s {
-    lostio_filehandle_t* filehandle;
     io_read_request_t read_request;
     dword correlation_id;
     pid_t pid;
@@ -48,32 +48,33 @@ struct sync_read_s {
 
 static list_t* sync_read_list = NULL;
 
+
 /**
  * Verarbeitet eventuell vorhandene aufgeschobene Anfragen fuer synchrones
  * Lesen.
  */
-void lostio_sync_dispatch()
+void lostio_sync_dispatch(void)
 {
-    int i = 0;
-    struct sync_read_s* sync_read;
+    int i;
+    size_t sz;
 
     // Wenn noch keine Anfragen vorhanden sind, wird abgebrochen.
     if (sync_read_list == NULL) {
         return;
     }
 
-    while ((sync_read = list_get_element_at(sync_read_list, i)) != NULL) {
-        // Pruefen ob jetzt genug Daten vorhanden sind.
-        if ((sync_read->filehandle->node->size - sync_read->filehandle->pos) >=
-            (sync_read->read_request.blocksize * sync_read->read_request.
-            blockcount))
-        {
-            rpc_io_read(sync_read->pid, sync_read->correlation_id,
-                sizeof(io_read_request_t), &sync_read->read_request);
+    p();
+    sz = list_size(sync_read_list);
+    {
+        struct sync_read_s* reads[sz];
+        i = 0;
+        while ((reads[i++] = list_pop(sync_read_list))) { }
+        v();
 
-            list_remove(sync_read_list, i);
-        } else {
-            i++;
+        for (i = 0; i < sz; i++) {
+            rpc_io_read(reads[i]->pid, reads[i]->correlation_id,
+                sizeof(reads[i]->read_request), &reads[i]->read_request);
+            free(reads[i]);
         }
     }
 }
@@ -82,27 +83,27 @@ void lostio_sync_dispatch()
  * Schiebt eine Anfrage fuer synchrones Lesen auf, falls keine Daten vorhanden
  * sind.
  *
- * @param filehandle Filehandle von dem gelesen werden soll
  * @param read_request Gesendete anfrage
  */
-void lostio_sync_read_wait(lostio_filehandle_t* filehandle, pid_t pid,
-    dword correlation_id, io_read_request_t* read_request)
+void lostio_sync_read_wait(pid_t pid, dword correlation_id,
+    io_read_request_t* read_request)
 {
-    struct sync_read_s* sync_read = malloc(sizeof(struct sync_read_s));
+    struct sync_read_s* sync_read = malloc(sizeof(*sync_read));
 
+    p();
     if (sync_read_list == NULL) {
         sync_read_list = list_create();
     }
 
-    sync_read->pid = pid;
-    sync_read->correlation_id = correlation_id;
-
     // Read-Request kopieren, da er beim zurueckspringen aus dem rpc-Handler
     // zerstoert wird.
-    memcpy(&sync_read->read_request, read_request, sizeof(io_read_request_t));
+    memcpy(&sync_read->read_request, read_request,
+        sizeof(sync_read->read_request));
 
-    sync_read->filehandle = filehandle;
+    sync_read->pid = pid;
+    sync_read->correlation_id = correlation_id;
 
     sync_read_list = list_push(sync_read_list, sync_read);
+    v();
 }
 
diff --git a/src/modules/lib/stdlibc/file.c b/src/modules/lib/stdlibc/file.c
index 6441fcd..4b1fdbe 100644
--- a/src/modules/lib/stdlibc/file.c
+++ b/src/modules/lib/stdlibc/file.c
@@ -116,6 +116,10 @@ FILE* fopen (const char* filename, const char* mode)
                 *attr |= IO_OPEN_MODE_LINK;
                 break;
 
+            case 's':
+                *attr |= IO_OPEN_MODE_SYNC;
+                break;
+
         }
     }
 
diff --git a/src/modules/vterm/input.c b/src/modules/vterm/input.c
index 78e5f91..0f38f55 100644
--- a/src/modules/vterm/input.c
+++ b/src/modules/vterm/input.c
@@ -105,6 +105,9 @@ static void rpc_kbd_callback(pid_t pid, dword cid, size_t data_size,
     p();
     send_key_to_vterm(vterm, kbd_data[0], !kbd_data[1]);
     v();
+
+    // Synchrones Lesen benachrichtigen
+    lostio_dispatch();
 }
 
 /**
diff --git a/src/modules/vterm/vterm.c b/src/modules/vterm/vterm.c
index d3fcd7f..345406b 100644
--- a/src/modules/vterm/vterm.c
+++ b/src/modules/vterm/vterm.c
@@ -70,7 +70,6 @@ int main(int argc, char* argv[])
     // Auf Anfragen warten
     while(TRUE) {
         wait_for_rpc();
-        lostio_dispatch();
 	}
 }
 
-- 
1.6.0.6