From 192b756a6999a0637fcc72f3fd2f9f7099e32543 Mon Sep 17 00:00:00 2001
From: Marvin Borner
Date: Fri, 2 Apr 2021 01:27:36 +0200
Subject: Huge scheduling/proc-management improvements

---
 kernel/inc/fs.h   |  4 ++--
 kernel/inc/proc.h | 34 +++++++++++++++++++++-------------
 2 files changed, 23 insertions(+), 15 deletions(-)

(limited to 'kernel/inc')

diff --git a/kernel/inc/fs.h b/kernel/inc/fs.h
index b6c30a2..1a78072 100644
--- a/kernel/inc/fs.h
+++ b/kernel/inc/fs.h
@@ -47,7 +47,7 @@ struct vfs {
 	res (*ioctl)(const char *path, u32 request, void *arg1, void *arg2, void *arg3,
 		     struct device *dev) ATTR((nonnull(1, 6)));
 	res (*stat)(const char *path, struct stat *buf, struct device *dev) NONNULL;
-	res (*wait)(const char *path, u32 func_ptr, struct device *dev) NONNULL;
+	res (*block)(const char *path, u32 func_ptr, struct device *dev) NONNULL;
 	res (*ready)(const char *path, struct device *dev) NONNULL;
 	res (*perm)(const char *path, enum vfs_perm perm, struct device *dev) NONNULL;
 };
@@ -68,7 +68,7 @@ res vfs_read(const char *path, void *buf, u32 offset, u32 count) NONNULL;
 res vfs_write(const char *path, void *buf, u32 offset, u32 count) NONNULL;
 res vfs_ioctl(const char *path, u32 request, void *arg1, void *arg2, void *arg3) ATTR((nonnull(1)));
 res vfs_stat(const char *path, struct stat *buf) NONNULL;
-res vfs_wait(const char *path, u32 func_ptr) NONNULL;
+res vfs_block(const char *path, u32 func_ptr) NONNULL;
 res vfs_poll(const char **files) NONNULL;
 res vfs_ready(const char *path) NONNULL;
 
diff --git a/kernel/inc/proc.h b/kernel/inc/proc.h
index 8a68131..c9dadaa 100644
--- a/kernel/inc/proc.h
+++ b/kernel/inc/proc.h
@@ -19,25 +19,25 @@
 #define GDT_USER_CODE_OFFSET 0x1b // User code segment offset in GDT (with ring3 mask)
 #define GDT_USER_DATA_OFFSET 0x23 // User data segment offset in GDT (with ring3 mask)
 
-#define PROC_MAX_WAIT_IDS 16
-#define PROC_WAIT_MAGIC 0x00528491
+#define PROC_MAX_BLOCK_IDS 16
+#define PROC_BLOCK_MAGIC 0x00528491
 
 #define STREAM_MAX_SIZE 4096
 enum stream_defaults { STREAM_IN, STREAM_OUT, STREAM_ERR, STREAM_LOG, STREAM_UNKNOWN = -1 };
 
 enum proc_priv { PROC_PRIV_NONE, PROC_PRIV_ROOT, PROC_PRIV_KERNEL };
-enum proc_state { PROC_RUNNING, PROC_SLEEPING };
-enum proc_wait_type { PROC_WAIT_DEV, PROC_WAIT_MSG };
+enum proc_state { PROC_RUNNING, PROC_BLOCKED };
+enum proc_block_type { PROC_BLOCK_DEV, PROC_BLOCK_MSG };
 
-struct proc_wait_identifier {
+struct proc_block_identifier {
 	u32 magic;
 	u32 id;
-	enum proc_wait_type type;
+	enum proc_block_type type;
 	u32 func_ptr;
 };
 
-struct proc_wait {
-	struct proc_wait_identifier ids[PROC_MAX_WAIT_IDS];
+struct proc_block {
+	struct proc_block_identifier ids[PROC_MAX_BLOCK_IDS];
 	u32 id_cnt;
 };
 
@@ -54,11 +54,17 @@ struct proc {
 	struct stream streams[4];
 	struct page_dir *page_dir;
 	struct regs regs;
-	struct proc_wait wait; // dev_id
+	struct proc_block block; // dev_id
 	enum proc_priv priv;
 	enum proc_state state;
 	struct stack *messages;
 	struct list *memory;
+
+	u32 ticks;
+	struct {
+		u8 val;
+		u8 cnt;
+	} quantum;
 };
 
 void scheduler(struct regs *regs) NONNULL;
@@ -67,11 +73,13 @@ void proc_print(void);
 struct proc *proc_current(void);
 u8 proc_super(void);
 struct proc *proc_from_pid(u32 pid);
-void proc_exit(struct proc *proc, s32 status) NONNULL;
+void proc_exit(struct proc *proc, struct regs *r, s32 status) NONNULL;
 void proc_yield(struct regs *r) NONNULL;
-void proc_clear_quantum(void);
-void proc_enable_waiting(u32 id, enum proc_wait_type type);
-void proc_wait_for(u32 id, enum proc_wait_type type, u32 func_ptr);
+void proc_set_quantum(struct proc *proc, u32 value);
+void proc_reset_quantum(struct proc *proc);
+void proc_state(struct proc *proc, enum proc_state state);
+void proc_block(u32 id, enum proc_block_type type, u32 func_ptr);
+void proc_unblock(u32 id, enum proc_block_type type);
 struct proc *proc_make(enum proc_priv priv);
 void proc_stack_push(struct proc *proc, u32 data) NONNULL;
 
-- 
cgit v1.2.3