فهرست منبع

Working on kernel and processe.s

Cixo Develop 8 ماه پیش
والد
کامیت
9ab534b428
9فایلهای تغییر یافته به همراه127 افزوده شده و 314 حذف شده
  1. 5 5
      headers/zivuos.h
  2. 6 19
      headers/zivuos/kernel.h
  3. 25 21
      headers/zivuos/models.h
  4. 10 76
      headers/zivuos/process.h
  5. 13 0
      headers/zivuos/state.h
  6. 0 18
      headers/zivuos/status.h
  7. 43 101
      sources/kernel.c
  8. 25 32
      sources/process.c
  9. 0 42
      sources/scheduler.c

+ 5 - 5
headers/zivuos.h

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

+ 6 - 19
headers/zivuos/kernel.h

@@ -3,34 +3,21 @@
 
 #include <stdbool.h>
 
-#include "status.h"
 #include "models.h"
 #include "process.h"
 
 typedef struct zv_kernel_s {
-    zv_process_t *processes;
-    zv_pid_t processes_count;
-    zv_pid_t current_max_pid;
+    zv_process_t **processes_stack;
+    zv_pid_t processes_stack_size;
 
-#ifndef ZIVUOS_USE_STATIC
+#ifndef ZIVUOS_DISABLE_DYNAMIC
     bool is_dynamic;
 #endif
+
 } zv_kernel_t;
 
 zv_kernel_t * zv_kernel_create();
-
-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);
-
-/* Static inline functions */
-
-static inline void zv_kernel_init(
-    zv_kernel_t *self,
-    zv_process_t *processes
-) {
-    self->processes = processes;
-}
+void zv_kernel_init(zv_kernel_t *, zv_process_t **, zv_pid_t);
+zv_pid_t zv_kernel_count_processes(zv_kernel_t *);
 
 #endif

+ 25 - 21
headers/zivuos/models.h

@@ -1,10 +1,27 @@
-#ifndef CX_ZIVUOS_MODELS_H_INCLUDED
-#define CX_ZIVUOS_MODELS_H_INCLUDED
+#ifndef CX_ZIVUOS_MODELS_H_INCLUDED 
+#define CX_ZIVUOS_MODELS_H_INCLUDED 
 
+#include <stddef.h>
 #include <stdint.h>
 #include <stdbool.h>
 
-#include "status.h"
+#include "state.h"
+
+typedef size_t zv_size_t;
+typedef int32_t zv_number_t;
+typedef uint32_t zv_unumber_t;
+typedef uint32_t zv_tick_t;
+typedef uint16_t zv_pid_t;
+
+#define ZV_ERROR_PID 0
+
+typedef enum zv_readiness_e {
+    ZV_NON_READY = 0,
+    ZV_READY = 1
+} zv_readiness_t;
+
+typedef zv_readiness_t (*zv_updater_t)(void *, void *);
+typedef zv_state_t (*zv_worker_t)(void *, void *); 
 
 typedef union zv_universal_param_u {
     intptr_t number;
@@ -12,22 +29,9 @@ typedef union zv_universal_param_u {
     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 union zv_restorer_u {
+    zv_tick_t time;
+    zv_updater_t updater;
+} zv_restorer_t;
 
-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
+#endif

+ 10 - 76
headers/zivuos/process.h

@@ -1,90 +1,24 @@
 #ifndef CX_ZIVUOS_PROCESS_H_INCLUDED
 #define CX_ZIVUOS_PROCESS_H_INCLUDED
 
-#include "status.h"
+#include <stdbool.h>
+
+#include "state.h"
 #include "models.h"
 
 typedef struct zv_process_s {
-    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_updater_t updater[1];
+    zv_restorer_t restorer[1];
+    zv_universal_param_t param[1];
 
-#ifndef ZIVUOS_USE_STATIC
+#ifndef ZIVUOS_DISABLE_DYNAMIC
     bool is_dynamic;
 #endif
-} zv_process_t;
-
-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;
-}
+} zv_process_t;
 
-static inline void zv_process_set_readiness(
-    zv_process_t *self,
-    zv_readiness_t readiness
-) {
-    self->updater->non_dynamic = readiness;
-}
+zv_process_t * zv_process_create(zv_worker_t);
+void zv_process_init(zv_process_t *, zv_worker_t);
 
 #endif

+ 13 - 0
headers/zivuos/state.h

@@ -0,0 +1,13 @@
+#ifndef CX_ZIVUOS_STATE_H_INCLUDED
+#define CX_ZIVUOS_STATE_H_INCLUDED
+
+typedef enum zv_state_e {
+    ZV_STATE_RUNNING,
+    ZV_STATE_READY,
+    ZV_STATE_REQUEST_WAITING,
+    ZV_STATE_UPDATER_WAITING,
+    ZV_STATE_TIME_WAITING,
+    ZV_STATE_CREATED
+} zv_state_t;
+
+#endif

+ 0 - 18
headers/zivuos/status.h

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

+ 43 - 101
sources/kernel.c

@@ -1,131 +1,73 @@
-#include <stdint.h>
-#include <stddef.h>
 #include <stdbool.h>
+#include <stddef.h>
 
-#ifndef ZIVUOS_USE_STATIC
+#ifndef ZIVUOS_DISABLE_DYNAMIC
 #include <malloc.h>
 #endif
 
-#include "status.h"
-#include "models.h"
 #include "process.h"
 #include "kernel.h"
+#include "models.h"
+#include "state.h"
 
-/* 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) {
-        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 NULL;
-}
-
-void zv_kernel_close(zv_kernel_t *self) {
-    /* Not require to free not alloced resources. */
-}
-
+#ifndef ZV_ALLOC_STEP
+#define ZV_ALLOC_STEP 0x08
 #endif
 
-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 **processes, 
+    zv_pid_t processes_count
+) {
+    self->processes_stack = processes;
+    self->processes_stack_size = processes_count;
+#ifndef ZIVUOS_DISABLE_DYNAMIC
+    self->is_dynamic = false;
+#endif
+}
 
-    if (current == NULL || max == 0) {
-        return NULL;
-    }
+zv_pid_t zv_kernel_count_processes(zv_kernel_t *self) {
+    zv_pid_t result = 0;
 
-    while (current->next != NULL) {
-        current = current->next;
-        
-        if (++count >= max) {
-            return current;
+    for (zv_pid_t count = 0; count < self->processes_stack_size; ++count) {
+        if (self->processes_stack[count] == NULL) {
+            continue;
         }
+
+        ++result;
     }
 
-    return current;
+    return result;
 }
 
-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;
+#ifndef ZIVUOS_DISABLE_DYNAMIC
 
-    while (true) {
-        if (current == NULL || count == 0) {
-            return NULL;
-        }
-
-        if (current->pid == target) {
-            return current;
-        }
-
-        --count;
-        current = current->next;
-    }
+static inline size_t zv_get_stack_size(zv_pid_t count) {
+    return (count * sizeof(zv_process_t *));
 }   
 
-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;
-
-    target->pid = self->current_max_pid;
-    oldest->next = target;
+zv_kernel_t * zv_kernel_create() {
+    zv_kernel_t *self = (zv_kernel_t *) malloc(sizeof(zv_kernel_t));
     
-    return target->pid;
-}
-
-/* Dynamic process creating. */
-#ifndef ZIVUOS_USE_STATIC
+    if (self == NULL) {
+        return NULL;
+    }
 
-zv_process_t * zv_kernel_create_process(zv_kernel_t *self) {
-    zv_process_t *target = zv_process_create();
+#define STACK_SIZE zv_get_stack_size(ZV_ALLOC_STEP)
+    zv_process_t **stack = (zv_process_t **) malloc(STACK_SIZE);
+#undef STACK_SIZE
 
-    if (target == NULL) {
+    if (stack == NULL) {
+        free(self);
         return NULL;
     }
 
-    target->status = ZV_STATUS_CREATING;
-    zv_kernel_attach_process(self, target);
-    
-    return target;
-}
-
-#else
+    zv_kernel_init(self, stack, ZV_ALLOC_STEP);
+    self->is_dynamic = true;
 
-zv_process_t * zv_kernel_create_process(zv_kernel_t *self) {
-    return NULL;
+    return self;
 }
 
 #endif
+
+

+ 25 - 32
sources/process.c

@@ -1,50 +1,43 @@
-#include <stdint.h>
-#include <stddef.h>
 #include <stdbool.h>
+#include <stddef.h>
+
+#ifndef ZIVUOS_DISABLE_DYNAMIC
+#include <malloc.h>
+#endif
 
-#include "models.h"
-#include "status.h"
 #include "process.h"
+#include "kernel.h"
+#include "models.h"
+#include "state.h"
 
-#ifndef ZIVUOS_USE_STATIC
-#include <malloc.h>
+#ifndef ZV_ALLOC_STEP
+#define ZV_ALLOC_STEP 0x08
+#endif
+
+void zv_process_init(zv_process_t *self, zv_worker_t worker) {
+    self->worker = worker;
+    self->state = ZV_STATE_CREATED;
+    self->restorer->updater = (zv_updater_t) 0x00;
+    self->param->pointer = (void *) 0x00;
+
+#ifndef ZIVUOS_DISABLE_DYNAMIC
+    self->is_dynamic = false;
 #endif
+}
 
-#ifndef ZIVUOS_USE_STATIC
+#ifndef ZIVUOS_DISABLE_DYNAMIC
 
-zv_process_t * zv_process_create() {
+zv_process_t * zv_process_create(zv_worker_t worker) {
     zv_process_t *self = (zv_process_t *) malloc(sizeof(zv_process_t));
 
     if (self == NULL) {
         return NULL;
     }
 
+    zv_process_init(self, worker);
     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

+ 0 - 42
sources/scheduler.c

@@ -1,42 +0,0 @@
-#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) {
-        
-    }
-}