Przeglądaj źródła

Working on kernel and pipe.

Cixo Develop 8 miesięcy temu
rodzic
commit
6aca3deaaf

+ 14 - 10
headers/zivuos/kernel.h

@@ -1,18 +1,22 @@
 #ifndef ZIVUOS_KERNEL_H_INCLUDED
 #define ZIVUOS_KERNEL_H_INCLUDED
 
-#include <stdbool.h>
-
+#include "porttypes.h"
 #include "process.h"
+#include "worker.h"
+
+typedef struct zv_kernel_s {
+    zv_process_t *processes;
+    zv_size_t processes_count;
+#ifdef STATIC_ONLY
+    zv_size_t processes_array_size;
+#endif
+} zv_kernel_t;
 
-typedef struct zivu_kernel_s {
-    bool is_static;
-    size_t processes_count;
-    zivu_process_t *processes;
-} zivu_kernel_t;
+zv_kernel_t * zv_kernel_create();
+void zv_kernel_init(zv_kernel_t *);
+void zv_kernel_delete(zv_kernel_t *);
 
-zivu_kernel_t* zivu_kernel_init_dynamic();
-void zivu_kernel_init_static(zivu_kernel_t*, zivu_process_t*, size_t);
-void zivu_kernel_remove(zivu_kernel_t*);
+bool zv_kernel_new_process(zv_kernel_t *);
 
 #endif

+ 40 - 0
headers/zivuos/pipe.h

@@ -0,0 +1,40 @@
+#ifndef ZIVUOS_PIPE_H_INCLUDED
+#define ZIVUOS_PIPE_H_INCLUDED
+
+#include <stdint.h>
+#include <stdbool.h>
+
+#include "porttypes.h"
+
+typedef struct zv_pipe_s {
+    void *content;
+    bool changed;
+} zv_pipe_t;
+
+zv_pipe_t * zv_pipe_create();
+void zv_pipe_init(zv_pipe_t *);
+void zv_pipe_delete(zv_pipe_t *);
+
+bool zv_pipe_is_changed(zv_pipe_t *);
+intptr_t zv_pipe_read_number(zv_pipe_t *);
+void * zv_pipe_read_content(zv_pipe_t *);
+void zv_pipe_write_number(zv_pipe_t *, intptr_t);
+void zv_pipe_write_content(zv_pipe_t *, void *);
+
+#ifdef USING_NAMESPACE_ZV
+
+#define pipe_is_changed zv_pipe_is_changed
+#define pipe_create zv_pipe_create
+#define pipe_init zv_pipe_init
+#define pipe_delete zv_pipe_delete
+
+#define pipe_read_number zv_pipe_read_number
+#define pipe_read_content zv_pipe_read_content
+#define pipe_write_number zv_pipe_write_number
+#define pipe_write_content zv_pipe_write_content
+
+typedef zv_pipe_t pipe_t;
+
+#endif
+
+#endif

+ 18 - 0
headers/zivuos/porttypes.h

@@ -0,0 +1,18 @@
+#ifndef ZIVUOS_PORTTYPES_H_INCLUDED
+#define ZIVUOS_PORTTYPES_H_INCLUDED
+
+#include <stdint.h>
+
+#ifdef SMALL_INT
+typedef uint8_t zv_uint_t;
+typedef int8_t zv_int_t;
+typedef uint16_t zv_ticks_t;
+typedef uint8_t zv_irq_t;
+#else
+typedef uint16_t zv_uint_t;
+typedef int16_t zv_int_t;
+typedef uint32_t zv_ticks_t;
+typedef uint16_t zv_irq_t;
+#endif
+
+#endif

+ 53 - 16
headers/zivuos/process.h

@@ -1,21 +1,58 @@
 #ifndef ZIVUOS_PROCESS_H_INCLUDED
 #define ZIVUOS_PROCESS_H_INCLUDED
 
-#include <stdint.h>
-#include <stdbool.h>
-
-typedef uint8_t zivu_priority_t;
-typedef size_t zivu_pid_t;
-
-typedef struct zivu_pipe_s {
-    void *data;
-    bool avairable;
-} zivu_pipe_t;
-
-typedef struct zivu_process_s {
-    void (*worker)(void*, void*);
-    zivu_pipe_t pipe[1];
-    zivu_priority_t priority;
-} zivu_process_t;
+#include "porttypes.h"
+#include "worker.h"
+#include "pipe.h"
+
+typedef enum zv_process_state_e {
+    /* Process waiting for free processor time */
+    WAITING,
+    
+    /* Process current executing by processor */
+    RUNNING,
+
+    /* Process waiting for IRQ (param) */
+    IRQ_WAITING,
+
+    /* Process waiting for new data in PIPE */
+    PIPE_WAITING,
+
+    /* Process waiting for ticks (param) */
+    TIME_WAITING
+} zv_process_state_t;
+
+typedef union zv_process_param_u {
+    /* Required for TIME_WAITING */
+    zv_ticks_t time;
+    
+    /* Required for IRQ_WAITING */
+    zv_irq_t irq;
+} zv_process_param_t;
+
+typedef struct zv_process_s {
+    /* Current state of process */
+    zv_process_state_t state;
+
+    /* Pipe for processes communication */
+    zv_pipe_t pipe;
+
+    /* Priority of process (lower number -> higher priority) */
+    zv_uint_t priority;
+    
+    /* Param for the process */
+    zv_process_param_t param;
+
+    /* Worker of the process */
+    zv_worker_t worker;
+} zv_process_t;
+
+#ifdef USING_NAMESPACE_ZV
+
+typedef zv_process_t process_t;
+typedef zv_process_param_t process_param_t;
+typedef zv_process_state_t process_state_t;
+
+#endif
 
 #endif

+ 4 - 3
headers/zivuos/worker.h

@@ -1,9 +1,10 @@
 #ifndef ZIVUOS_WORKER_H_INCLUDED
 #define ZIVUOS_WORKER_H_INCLUDED
 
-#include "kernel.h"
-#include "process.h"
+typedef void (*zv_worker_t)(void *, void *);
 
-typedef void (*zivu_worker_t)(zivu_kernel_t*, zivu_process_t*);
+#ifdef USING_NAMESPACE_ZV
+typedef zv_worker_t worker_t;
+#endif
 
 #endif

+ 0 - 35
sources/kernel.c

@@ -1,38 +1,3 @@
-#include <malloc.h>
-#include <stdbool.h>
-
 #include "kernel.h"
 #include "process.h"
-#include "worker.h"
-
-zivu_kernel_t* zivu_kernel_init_dynamic() {
-    zivu_kernel_t *current = (zivu_kernel_t*) malloc(sizeof(zivu_kernel_t)); 
-
-    current->processes = NULL;
-    current->is_static = false;
-    current->processes_count = 0;
-
-    return current;
-}
-
-void zivu_kernel_init_static(
-    zivu_kernel_t *self, 
-    zivu_process_t *processes_table,
-    zivu_pid_t processes_count
-) {
-    self->is_static = true;
-    self->processes = processes_table;
-    self->processes_count = processes_count;
-}
-
-void zivu_kernel_remove(zivu_kernel_t *self) {
-    if (self->is_static) {
-        self->processes = NULL;
-        self->processes_count = 0;
-        
-        return;
-    }
 
-    free((void*) self);
-    free((void*) self->processes);
-}   

+ 57 - 0
sources/pipe.c

@@ -0,0 +1,57 @@
+#include "pipe.h"
+
+#ifndef STATIC_ONLY
+#include <malloc.h>
+#endif
+
+zv_pipe_t * zv_pipe_create() {
+#ifdef STATIC_ONLY
+    return (zv_pipe_t *) NULL;
+#else
+    zv_pipe_t *self = (zv_pipe_t *) malloc(sizeof(zv_pipe_t));
+
+    if (self == NULL) {
+        return self;
+    }
+
+    zv_pipe_init(self);
+    return self;
+#endif
+}
+
+void zv_pipe_init(zv_pipe_t *self) {
+    self->content = (zv_pipe_t *) NULL;
+    self->changed = false;
+}
+
+void zv_pipe_delete(zv_pipe_t *self) {
+#ifndef STATIC_ONLY
+    if (self != NULL) {
+        free(self);
+    }
+#endif
+}
+
+bool zv_pipe_is_changed(zv_pipe_t *self) {
+    return self->changed;
+}
+
+intptr_t zv_pipe_read_number(zv_pipe_t *self) {
+    self->changed = false;
+    return (intptr_t) self->content;
+}
+
+void * zv_pipe_read_content(zv_pipe_t *self) {
+    self->changed = false;
+    return self->content;
+}
+
+void zv_pipe_write_number(zv_pipe_t *self, intptr_t content) {
+    self->changed = true;
+    self->content = (void *) content;
+}
+
+void zv_pipe_write_content(zv_pipe_t *self, void *content) {
+    self->changed = true;
+    self->content = content;
+}

+ 3 - 2
tests/000-default/testing.c

@@ -1,6 +1,7 @@
-#include <zivuos/kernel.h>
+#include <zivuos/process.h>
+#include <zivuos/porttypes.h>
+#include <zivuos/worker.h>
 
 int main() {
-    kernel_init();
     return 0;
 }

+ 24 - 0
tests/001-pipe/run.sh

@@ -0,0 +1,24 @@
+#!/bin/bash
+
+CC=gcc
+CC_FLAGS="-O2"
+
+OWN=$(dirname $(realpath $0))
+TEST_SOURCE=$OWN/testing.c
+TEST_OUTPUT=$OWN/testing.app
+HEADERS=$OWN/../../headers
+LIBRARY=$OWN/../../build-generic
+LIBRARY_MAKE=$OWN/../../build-generic/make.sh
+
+rm -f $TEST_OUTPUT
+
+echo "Rebuilding..."
+$LIBRARY_MAKE
+
+echo ""
+echo "Build test..."
+$CC $CC_FLAGS $TEST_SOURCE -o $TEST_OUTPUT -I$HEADERS -L$LIBRARY -lzivuos 
+
+echo "Running..."
+echo ""
+$TEST_OUTPUT

+ 60 - 0
tests/001-pipe/testing.c

@@ -0,0 +1,60 @@
+#include <zivuos/process.h>
+#include <zivuos/porttypes.h>
+#include <zivuos/worker.h>
+#include <zivuos/pipe.h>
+
+#include <stdio.h>
+
+int main() {
+    puts("Creating pipe...");
+    zv_pipe_t *pipe = zv_pipe_create();
+
+    if (pipe == NULL) {
+        puts("Can not create pipe!");
+        return -1;
+    }
+
+    puts("Pipe was created.");
+
+    if (zv_pipe_is_changed(pipe)) {
+        puts("Pipe created as changed. FAIL.");
+    } else {
+        puts("Pipe created sucess!");
+    }
+
+    zv_pipe_write_number(pipe, 100);
+
+    if (zv_pipe_is_changed(pipe)) {
+        puts("Pipe write success!");
+    } else {
+        puts("Pipe write fail. Not mark as changed.");
+    }
+
+    if (zv_pipe_read_number(pipe) == 100) {
+        puts("Pipe read success!");
+    } else {
+        puts("Pipe return ivalid value.");
+    }
+
+    if (!zv_pipe_is_changed(pipe)) {
+        puts("Pipe read mark success!");
+    } else {
+        puts("Pipe read fail. Not unmarked as changed.");
+    }
+
+    int array[] = {0x01, 0x02};
+
+    zv_pipe_write_content(pipe, (void *) array);
+
+    if (zv_pipe_is_changed(pipe)) {
+        if (*((int *) zv_pipe_read_content(pipe)) == array[0]) {
+            puts("Pipe as pointer work propertly!");
+        } else {
+            puts("Pipe marked as changed, but pointer not correct.");
+        }
+    } else {
+        puts("Pipe not marked as changed from pointer storage.");
+    }
+
+    return 0;
+}