Ver Fonte

Add kernel.c and fix headers.

Cixo Develop há 8 meses atrás
pai
commit
38248fe1d3

+ 21 - 11
headers/zivuos/kernel.h

@@ -1,22 +1,32 @@
-#ifndef ZIVUOS_KERNEL_H_INCLUDED
-#define ZIVUOS_KERNEL_H_INCLUDED
+#ifndef CX_ZIVUOS_KERNEL_H_INCLUDED
+#define CX_ZIVUOS_KERNEL_H_INCLUDED
+
+#include <stdbool.h>
 
-#include "porttypes.h"
 #include "process.h"
-#include "worker.h"
+#include "zivu_types.h"
 
 typedef struct zv_kernel_s {
     zv_process_t *processes;
-    zv_size_t processes_count;
-#ifdef STATIC_ONLY
-    zv_size_t processes_array_size;
+    zv_pid_t processes_count;
+    zv_pid_t current_process;
+    
+/* For dynamic also kernel. */
+#ifndef ZV_DISABLE_DYNAMIC
+    bool is_dynamic;
 #endif
 } zv_kernel_t;
 
-zv_kernel_t * zv_kernel_create();
-void zv_kernel_init(zv_kernel_t *);
-void zv_kernel_delete(zv_kernel_t *);
+#define ZV_IDLE_PROCESS ZV_MAX_PID_T
+
+void zv_kernel_init(zv_kernel_t *, zv_process_t *, zv_pid_t); 
 
-bool zv_kernel_new_process(zv_kernel_t *);
+/* For dynamic also. */
+#ifndef ZV_DISABLE_DYNAMIC
+
+zv_kernel_t * zv_kernel_create();
+#define ZV_DYNAMIC_PROCESSES_STEP 0x08
 
 #endif
+
+#endif

+ 22 - 0
headers/zivuos/latch.h

@@ -0,0 +1,22 @@
+#ifndef CX_ZIVUOS_LATCH_H_INCLUDED
+#define CX_ZIVUOS_LATCH_H_INCLUDED
+
+#include "zivu_types.h"
+
+typedef enum zv_latch_state_e {
+    SAME,
+    CHANGED
+} zv_latch_state_t;
+
+typedef union zv_latch_updater_u {
+    intptr_t exists;
+    zv_latch_state_t (*callback) (struct zv_latch_s *);
+} zv_latch_updater_t;
+
+typedef struct zv_latch_s {
+    zv_universal_param_t data;
+    zv_latch_state_t state;
+    zv_latch_updater_t updater;
+} zv_latch_t;
+
+#endif

+ 0 - 40
headers/zivuos/pipe.h

@@ -1,40 +0,0 @@
-#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

+ 0 - 18
headers/zivuos/porttypes.h

@@ -1,18 +0,0 @@
-#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

+ 17 - 50
headers/zivuos/process.h

@@ -1,58 +1,25 @@
-#ifndef ZIVUOS_PROCESS_H_INCLUDED
-#define ZIVUOS_PROCESS_H_INCLUDED
+#ifndef CX_ZIVUOS_PROCESS_H_INCLUDED
+#define CX_ZIVUOS_PROCESS_H_INCLUDED
 
-#include "porttypes.h"
-#include "worker.h"
-#include "pipe.h"
+#include "zivu_types.h"
+#include "latch.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 enum zv_state_e {
+    CLOSED,
+    SLEEP,
+    WORKING,
+    CHECKER_WAITING,
+    LATCH_WAITING
+} zv_state_t;
 
 typedef struct zv_process_s {
-    /* Current state of process */
-    zv_process_state_t state;
-
-    /* Pipe for processes communication */
-    zv_pipe_t pipe;
+    zv_priority_t priority;
+    zv_latch_t latch[1];
+    zv_universal_param_t param[1];
+    zv_state_t state;
 
-    /* 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_checker_t checker;
 } 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
+#endif

+ 0 - 10
headers/zivuos/worker.h

@@ -1,10 +0,0 @@
-#ifndef ZIVUOS_WORKER_H_INCLUDED
-#define ZIVUOS_WORKER_H_INCLUDED
-
-typedef void (*zv_worker_t)(void *, void *);
-
-#ifdef USING_NAMESPACE_ZV
-typedef zv_worker_t worker_t;
-#endif
-
-#endif

+ 40 - 0
headers/zivuos/zivu_types.h

@@ -0,0 +1,40 @@
+#ifndef CX_ZIVUOS_ZIVU_TYPES_H_INCLUDED
+#define CX_ZIVUOS_ZIVU_TYPES_H_INCLUDED
+
+#include <stdint.h>
+#include <stddef.h>
+#include <stdbool.h>
+
+/* Inform to deny warnings. */
+struct zv_kernel_s;
+struct zv_process_s;
+struct zv_latch_s;
+
+typedef uint32_t zv_pid_t;
+typedef size_t zv_size_t;
+typedef uint8_t zv_priority_t;
+
+#define ZV_MAX_SIZE_T SIZE_MAX
+#define ZV_MAX_PID_T UINT32_MAX
+
+typedef enum zv_checker_result_e {
+    NOT_READY,
+    READY
+} zv_checker_result_t;
+
+typedef enum zv_state_e (* zv_worker_t) (
+    struct zv_kernel_s *, 
+    struct zv_process_s *
+);
+
+typedef zv_checker_result_t (*zv_checker_t) (
+    struct zv_kernel_s *, 
+    struct zv_process_s *
+);
+
+typedef union zv_universal_param_u {
+    void *pointer;
+    intptr_t number;
+} zv_universal_param_t;
+
+#endif

+ 59 - 0
sources/kernel.c

@@ -1,3 +1,62 @@
+#ifndef ZV_DISABLE_DYNAMIC
+#include <malloc.h>
+#endif
+
+#include "zivu_types.h"
 #include "kernel.h"
 #include "process.h"
 
+zv_kernel_t * zv_kernel_create();
+
+zv_kernel_t * zv_kernel_create() {
+    zv_kernel_t *self = (zv_kernel_t *) malloc(sizeof(zv_kernel_t));
+    
+    if (self != NULL) {
+        zv_kernel_init(self, NULL, 0x00);
+    }
+
+    return self;
+}
+
+/* For dynamic also kernel. */
+#ifndef ZV_DISABLE_DYNAMIC
+
+void zv_kernel_init(
+    zv_kernel_t *self, 
+    zv_process_t *container,
+    zv_pid_t processes_count
+) {
+    self->current_process = ZV_IDLE_PROCESS;
+
+    if (container != NULL) { 
+        self->processes = container;
+        self->processes_count = processes_count;
+        self->is_dynamic = false;
+        
+        return;
+    }
+
+    self->processes_count = ZV_DYNAMIC_PROCESSES_STEP * 1;
+    self->processes = (zv_process_t *) malloc(ZV_DYNAMIC_PROCESSES_STEP * 1);
+    self->is_dynamic = true;
+
+    /* When cannot alloc, make kernel unuseable. */
+    if (self->processes == NULL) {
+        self->processes_count = 0x00;
+        self->is_dynamic = false;
+    }
+}
+
+#else 
+
+void zv_kernel_init(
+    zv_kernel_t *self, 
+    zv_process_t *container,
+    zv_pid_t processes_count
+) {
+    self->current_process = ZV_IDLE_PROCESS;
+    self->processes = container;
+    self->processes_count = processes_count;
+}
+
+#endif

+ 0 - 0
sources/latch.c


+ 0 - 57
sources/pipe.c

@@ -1,57 +0,0 @@
-#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;
-}

+ 0 - 0
sources/process.c


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

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

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

@@ -1,60 +1,5 @@
-#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;
 }