Cixo Develop 8 miesięcy temu
rodzic
commit
fcc44e0a58

+ 9 - 0
headers/zivuos.h

@@ -0,0 +1,9 @@
+#ifndef CX_ZIVUOS_H_INCLUDED
+#define CX_ZIVUOS_H_INCLUDED
+
+#include "zivuos/status.h"
+#include "zivuos/models.h"
+#include "zivuos/process.h"
+#include "zivuos/kernel.h"
+
+#endif

+ 0 - 14
headers/zivuos/hash.h

@@ -1,14 +0,0 @@
-#ifndef CX_ZIVUOS_HASH_H_INCLUDED
-#define CX_ZIVUOS_HASH_H_INCLUDED
-
-#include <stdint.h>
-
-#include "zivu_types.h"
-
-typedef uint32_t zv_hash_t;
-
-#define ZV_HASH_MAX UINT32_MAX
-
-zv_hash_t zv_hash_calculate(void *, zv_size_t);
-
-#endif

+ 17 - 14
headers/zivuos/kernel.h

@@ -3,31 +3,34 @@
 
 #include <stdbool.h>
 
+#include "status.h"
+#include "models.h"
 #include "process.h"
-#include "zivu_types.h"
 
 typedef struct zv_kernel_s {
     zv_process_t *processes;
     zv_pid_t processes_count;
-    zv_pid_t current_process;
-    
-/* For dynamic also kernel. */
-#ifndef ZV_DISABLE_DYNAMIC
+    zv_pid_t current_max_pid;
+
+#ifndef ZIVUOS_USE_STATIC
     bool is_dynamic;
 #endif
 } zv_kernel_t;
 
-#define ZV_IDLE_PROCESS ZV_MAX_PID_T
+zv_kernel_t * zv_kernel_create();
 
-void zv_kernel_init(zv_kernel_t *, zv_process_t *, zv_pid_t); 
+void zv_kernel_close(zv_kernel_t *);
+static inline void zv_kernel_init(zv_kernel_t *, zv_process_t *);
+zv_process_t * zv_kernel_create_process(zv_kernel_t *);
+void zv_kernel_scheduler(zv_kernel_t *, bool);
 
-/* For dynamic also. */
-#ifndef ZV_DISABLE_DYNAMIC
+/* Static inline functions */
 
-zv_kernel_t * zv_kernel_create();
-#define ZV_PROCESSES_STEP 0x08
-#define ZV_PROCESSES_STEP_SIZE (ZV_PROCESSES_STEP * sizeof(zv_process_t))
+static inline void zv_kernel_init(
+    zv_kernel_t *self,
+    zv_process_t *processes
+) {
+    self->processes = processes;
+}
 
 #endif
-
-#endif

+ 0 - 22
headers/zivuos/latch.h

@@ -1,22 +0,0 @@
-#ifndef CX_ZIVUOS_LATCH_H_INCLUDED
-#define CX_ZIVUOS_LATCH_H_INCLUDED
-
-#include <stdint.h>
-#include <stdbool.h>
-
-#include "zivu_types.h"
-#include "hash.h"
-
-typedef struct zv_latch_s {
-    zv_universal_param_t data[1];
-    zv_hash_t hash;
-    zv_size_t size;
-    bool changed;
-
-#ifndef ZV_DISABLE_DYNAMIC
-    bool is_dynamic;
-#endif
-
-} zv_latch_t;
-
-#endif

+ 33 - 0
headers/zivuos/models.h

@@ -0,0 +1,33 @@
+#ifndef CX_ZIVUOS_MODELS_H_INCLUDED
+#define CX_ZIVUOS_MODELS_H_INCLUDED
+
+#include <stdint.h>
+#include <stdbool.h>
+
+#include "status.h"
+
+typedef union zv_universal_param_u {
+    intptr_t number;
+    uintptr_t unumber;
+    void *pointer;
+} zv_universal_param_t;
+
+#ifdef ZIVUOS_8BIT_NUMBERS
+typedef uint8_t zv_number_t;
+#elif ZIVUOS_32BIT_NUMBERS
+typedef uint32_t zv_number_t;
+#else
+typedef uint16_t zv_number_t;
+#endif
+
+typedef zv_number_t zv_pid_t;
+
+typedef zv_status_t (*zv_worker_t) (void *, void *);
+typedef zv_readiness_t (*zv_checker_t) (void *, void *);
+
+typedef union zv_updater_u {
+    zv_readiness_t non_dynamic;
+    zv_checker_t dynamic;
+} zv_updater_t;
+
+#endif

+ 81 - 16
headers/zivuos/process.h

@@ -1,25 +1,90 @@
 #ifndef CX_ZIVUOS_PROCESS_H_INCLUDED
 #define CX_ZIVUOS_PROCESS_H_INCLUDED
 
-#include "zivu_types.h"
-#include "latch.h"
-
-typedef enum zv_state_e {
-    CLOSED,
-    SLEEP,
-    WORKING,
-    CHECKER_WAITING,
-    LATCH_WAITING
-} zv_state_t;
+#include "status.h"
+#include "models.h"
 
 typedef struct zv_process_s {
-    zv_priority_t priority;
-    zv_latch_t latch[1];
+    struct zv_process_s *next;
+    zv_pid_t pid;
+    zv_status_t status;
     zv_universal_param_t param[1];
-    zv_state_t state;
-
     zv_worker_t worker;
-    zv_checker_t checker;
+    zv_updater_t updater[1];
+
+#ifndef ZIVUOS_USE_STATIC
+    bool is_dynamic;
+#endif
 } zv_process_t;
 
-#endif
+zv_process_t * zv_process_create();
+zv_process_t * zv_process_remove(zv_process_t *);
+
+static inline zv_pid_t zv_process_get_pid(zv_process_t *self) {
+    return self->pid;
+}
+
+static inline void zv_process_set_worker(
+    zv_process_t *self, 
+    zv_worker_t worker
+) {
+    self->worker = worker;
+}
+
+static inline void zv_process_set_updater(
+    zv_process_t *self, 
+    zv_updater_t updater
+) {
+    *self->updater = updater;
+}
+
+static inline void zv_process_set_param(
+    zv_process_t *self,
+    zv_universal_param_t param
+) {
+    *self->param = param;
+}
+
+static inline void zv_process_set_status(
+    zv_process_t *self,
+    zv_status_t status
+) {
+    self->status = status;
+}
+
+static inline void zv_process_set_number_param(
+    zv_process_t *self,
+    intptr_t number
+) {
+    self->param->number = number;
+}
+
+static inline void zv_process_set_unumber_param(
+    zv_process_t *self,
+    uintptr_t unumber
+) {
+    self->param->unumber = unumber;
+}
+
+static inline void zv_process_set_pointer_param(
+    zv_process_t *self,
+    void *pointer
+) {
+    self->param->pointer = pointer;
+}
+
+static inline void zv_process_set_dynamic_updater(
+    zv_process_t *self,
+    zv_checker_t checker
+) {
+    self->updater->dynamic = checker;
+}
+
+static inline void zv_process_set_readiness(
+    zv_process_t *self,
+    zv_readiness_t readiness
+) {
+    self->updater->non_dynamic = readiness;
+}
+
+#endif

+ 18 - 0
headers/zivuos/status.h

@@ -0,0 +1,18 @@
+#ifndef CX_ZIVUOS_STATUS_H_INCLUDED
+#define CX_ZIVUOS_STATUS_H_INCLUDED
+
+typedef enum zv_status_e {
+    ZV_STATUS_NOT_EXISTS,
+    ZV_STATUS_SLEEP,
+    ZV_STATUS_READY,
+    ZV_STATUS_CHECK_UPDATER,
+    ZV_STATUS_RUNNING,
+    ZV_STATUS_CREATING
+} zv_status_t;
+
+typedef enum zv_readiness_e {
+    ZV_NOT_READY = 0x00,
+    ZV_READY = 0x01
+} zv_readiness_t;
+
+#endif

+ 0 - 40
headers/zivuos/zivu_types.h

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

+ 0 - 53
sources/hash.c

@@ -1,53 +0,0 @@
-#include <stdint.h>
-
-#include "hash.h"
-#include "zivu_types.h"
-
-inline static zv_hash_t zv_hash_calculate_intptr(intptr_t target) {
-    zv_hash_t left_part = (zv_hash_t) (target >> (sizeof(zv_hash_t) * 8));
-    zv_hash_t right_part = (zv_hash_t) (target & ZV_HASH_MAX);
-
-    /* Splited left part is left part of target, where bytes are fliped. */
-    /* Example: 0xAABBCCDD -> 0xCCDDAABB */
-
-    const uint8_t half_part = sizeof(zv_hash_t) * 8 / 2;
-    zv_hash_t splited_left_part;
-    splited_left_part = left_part >> half_part;
-    splited_left_part = splited_left_part | (left_part << half_part);
-
-    zv_hash_t result;
-    result = left_part ^ right_part;
-    result = result ^ splited_left_part;
-
-    return result;
-}
-
-inline static zv_hash_t zv_hash_calculate_array(
-    uint8_t *content, 
-    zv_size_t amount
-) {
-    zv_hash_t first = 1;
-    zv_hash_t second = 0;
-    
-    while (amount > 0) {
-        first = (first + *content) % 65521;
-        second = (first + second) % 65521;
-
-        ++content;
-        --amount;
-    }
-    
-    return (second << 16) | first;
-}
-
-zv_hash_t zv_hash_calculate(void *target, zv_size_t amount) {
-    if (amount == 0) {
-        if (sizeof(zv_hash_t) <= sizeof(intptr_t)) {
-            return zv_hash_calculate_intptr((intptr_t) target);
-        }
-
-        return (zv_hash_t) ((intptr_t) target);
-    }
-
-    return zv_hash_calculate_array((uint8_t *) target, amount);
-}

+ 99 - 38
sources/kernel.c

@@ -1,70 +1,131 @@
-#ifndef ZV_DISABLE_DYNAMIC
+#include <stdint.h>
+#include <stddef.h>
+#include <stdbool.h>
+
+#ifndef ZIVUOS_USE_STATIC
 #include <malloc.h>
 #endif
 
-#include "zivu_types.h"
-#include "kernel.h"
+#include "status.h"
+#include "models.h"
 #include "process.h"
+#include "kernel.h"
 
-#ifndef ZV_DISABLE_DYNAMIC
+/* Dynamic kernel creating. */
+#ifndef ZIVUOS_USE_STATIC
 
 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);
+
+    if (self == NULL) {
+        return NULL;
     }
 
+    self->is_dynamic = true;
+    self->processes = NULL;
+    self->processes_count = 0;
+    self->current_max_pid = 0;
+
     return self;
 }
 
+void zv_kernel_close(zv_kernel_t *self) {
+    if (!self->is_dynamic) {
+        return;
+    }
+
+    zv_process_t *current = self->processes;
+
+    while (current->next != NULL) {
+        current = zv_process_remove(current);
+    }
+
+    free(self);
+}
+
+/* Static kernel creating. */
 #else
 
 zv_kernel_t * zv_kernel_create() {
-    return (zv_kernel_t *) NULL;
+    return NULL;
+}
+
+void zv_kernel_close(zv_kernel_t *self) {
+    /* Not require to free not alloced resources. */
 }
 
 #endif
 
-/* For dynamic also kernel. */
-#ifndef ZV_DISABLE_DYNAMIC
+zv_process_t * zv_kernel_get_oldest(zv_kernel_t *self) {
+    zv_process_t *current = self->processes;
+    zv_pid_t count = 0;
+    zv_pid_t max = self->processes_count;
 
-void zv_kernel_init(
-    zv_kernel_t *self, 
-    zv_process_t *container,
-    zv_pid_t processes_count
-) {
-    self->current_process = ZV_IDLE_PROCESS;
+    if (current == NULL || max == 0) {
+        return NULL;
+    }
 
-    if (container != NULL) { 
-        self->processes = container;
-        self->processes_count = processes_count;
-        self->is_dynamic = false;
+    while (current->next != NULL) {
+        current = current->next;
         
-        return;
+        if (++count >= max) {
+            return current;
+        }
     }
 
-    self->processes_count = ZV_PROCESSES_STEP * 1;
-    self->processes = (zv_process_t *) malloc(ZV_PROCESSES_STEP_SIZE * 1);
-    self->is_dynamic = true;
+    return current;
+}
+
+zv_process_t * zv_kernel_select_process(zv_kernel_t *self, zv_pid_t target) {
+    zv_process_t *current = self->processes;
+    zv_pid_t count = self->processes_count;
+
+    while (true) {
+        if (current == NULL || count == 0) {
+            return NULL;
+        }
+
+        if (current->pid == target) {
+            return current;
+        }
+
+        --count;
+        current = current->next;
+    }
+}   
+
+zv_pid_t zv_kernel_attach_process(zv_kernel_t *self, zv_process_t *target) {
+    zv_process_t *oldest = zv_kernel_get_oldest(self); 
+    
+    ++self->processes_count;
+    ++self->current_max_pid;
 
-    /* When cannot alloc, make kernel unuseable. */
-    if (self->processes == NULL) {
-        self->processes_count = 0x00;
-        self->is_dynamic = false;
+    target->pid = self->current_max_pid;
+    oldest->next = target;
+    
+    return target->pid;
+}
+
+/* Dynamic process creating. */
+#ifndef ZIVUOS_USE_STATIC
+
+zv_process_t * zv_kernel_create_process(zv_kernel_t *self) {
+    zv_process_t *target = zv_process_create();
+
+    if (target == NULL) {
+        return NULL;
     }
+
+    target->status = ZV_STATUS_CREATING;
+    zv_kernel_attach_process(self, target);
+    
+    return target;
 }
 
-#else 
+#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;
+zv_process_t * zv_kernel_create_process(zv_kernel_t *self) {
+    return NULL;
 }
 
-#endif
+#endif

+ 0 - 76
sources/latch.c

@@ -1,76 +0,0 @@
-#include "hash.h"
-#include "latch.h"
-#include "zivu_types.h"
-
-#ifndef ZV_DISABLE_DYNAMIC
-#include <malloc.h>
-#endif
-
-static inline zv_hash_t zv_latch_calculate_hash(zv_latch_t *self) {
-    return zv_hash_calculate(self->data->pointer, self->size);
-}
-
-static inline void zv_latch_regenerate_hash(zv_latch_t *self) {
-    self->hash = zv_latch_calculate_hash(self);
-}
-
-bool zv_latch_changed(zv_latch_t *self) {
-    if (self->changed) {
-        return true;
-    }
-
-    if (self->hash == zv_latch_calculate_hash(self)) {
-        return false;
-    }
-
-    return self->changed = true;
-}
-
-zv_universal_param_t zv_latch_read(zv_latch_t *self) {
-    self->changed = false;
-    self->hash = zv_latch_calculate_hash(self);
-    
-    return *self->data;
-}
-
-void zv_latch_write(zv_latch_t *self, zv_universal_param_t content) {
-    self->changed = true;
-    *self->data = content;
-}
-
-#ifndef ZV_DISABLE_DYNAMIC
-
-zv_latch_t * zv_latch_create(zv_size_t size) {
-    zv_latch_t *self = (zv_latch_t *) malloc(sizeof(zv_latch_t));
-    
-    if (self == NULL) {
-        return NULL;
-    }
-
-    self->size = size;
-    self->is_dynamic = true;
-    self->changed = false;
-    self->data->pointer = (void *) malloc(size);
-
-    if (self->data->pointer == NULL) {
-        free(self);
-        return NULL;
-    }
-
-    zv_latch_regenerate_hash(self);
-
-    return self;
-}
-
-void zv_latch_remove(zv_latch_t *self) {
-    if (!self->is_dynamic) {
-        return;
-    }
-    
-    free(self->data->pointer);
-    free(self);
-}
-
-#endif
-
-

+ 50 - 0
sources/process.c

@@ -0,0 +1,50 @@
+#include <stdint.h>
+#include <stddef.h>
+#include <stdbool.h>
+
+#include "models.h"
+#include "status.h"
+#include "process.h"
+
+#ifndef ZIVUOS_USE_STATIC
+#include <malloc.h>
+#endif
+
+#ifndef ZIVUOS_USE_STATIC
+
+zv_process_t * zv_process_create() {
+    zv_process_t *self = (zv_process_t *) malloc(sizeof(zv_process_t));
+
+    if (self == NULL) {
+        return NULL;
+    }
+
+    self->is_dynamic = true;
+    self->status = ZV_STATUS_CREATING;
+    self->next = NULL;
+
+    return self;
+}
+
+
+zv_process_t * zv_process_remove(zv_process_t *self) {
+    zv_process_t *next = self->next;
+
+    if (self->is_dynamic) {
+        free(self);
+    }
+
+    return next;
+}
+
+#else
+
+zv_process_t * zv_process_create() { 
+    return NULL;
+}
+
+zv_process_t * zv_process_remove(zv_process_t *self) {
+    return self->next;
+}
+
+#endif

+ 42 - 0
sources/scheduler.c

@@ -0,0 +1,42 @@
+#include "kernel.h"
+
+static inline bool zv_kernel_is_process_ready(
+    zv_kernel_t *self, 
+    zv_process_t *target
+) {
+    zv_status_t status = target->status;
+
+    if (status == ZV_STATUS_READY) {
+        return true;
+    }
+}
+
+static inline zv_process_t * zv_kernel_select_next_process(
+    zv_kernel_t *self
+) {
+    zv_process_t *current = self->processes;
+    zv_pid_t step = self->processes_count;
+
+    while (true) {
+        if (step == 0) {
+            return NULL;
+        }
+
+        if (current == NULL) {
+            return NULL;
+        }
+
+        if (zv_kernel_is_process_ready(self, current)) {
+            return current;
+        }
+
+        current = current->next;
+        step = step - 1;
+    }
+}
+
+void zv_kernel_scheduler(zv_kernel_t *self, bool exitable) {
+    while (!exitable) {
+        
+    }
+}

+ 4 - 4
tests/000-default/testing.c

@@ -1,8 +1,8 @@
-#include <zivuos/kernel.h>
-#include <zivuos/latch.h>
-#include <zivuos/process.h>
-#include <zivuos/zivu_types.h>
+#include <stdio.h>
+#include <zivuos.h>
 
 int main() {
+    puts("ZivuOS default test unit.");
+
     return 0;
 }

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

@@ -1,24 +0,0 @@
-#!/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

+ 0 - 5
tests/001-latch/testing.c

@@ -1,5 +0,0 @@
-#include <stdio.h>
-
-int main() {
-    return 0;
-}