From 9e42cb1a4e57ec1626879355cfc22786321fc96d Mon Sep 17 00:00:00 2001 From: antifallobst Date: Sun, 19 Feb 2023 09:13:17 +0100 Subject: [PATCH] fix (kernel): removed unneeded complexiticy of thread descriptors --- kernel/inc/proc/scheduler.h | 21 +++++++--------- kernel/inc/proc/thread.h | 26 ++++++++----------- kernel/src/kmain.c | 6 ++--- kernel/src/proc/scheduler.c | 50 +++++++++++++++++-------------------- kernel/src/proc/thread.c | 18 ++++++------- 5 files changed, 54 insertions(+), 67 deletions(-) diff --git a/kernel/inc/proc/scheduler.h b/kernel/inc/proc/scheduler.h index 9d688e9..cfba23d 100644 --- a/kernel/inc/proc/scheduler.h +++ b/kernel/inc/proc/scheduler.h @@ -19,27 +19,24 @@ #include "proc/thread.h" #include "utils/bitmap.h" -#define SCHEDULER_MAX_PROCESSES 32 -#define SCHEDULER_MAX_THREADS_PER_PROCESS 16 - typedef struct { - uint32_t num_threads; - thread_descriptor_T* running_thread; + uint32_t num_threads; + thread_T* running_thread; - bool blocked; - bool initialized; + bool blocked; + bool initialized; } scheduler_T; void scheduler_init (); cpu_state_T* scheduler_start (cpu_state_T* state); bool scheduler_is_initialized (); -thread_t scheduler_register_thread (thread_T* thread); -void scheduler_pause_thread (thread_t thread_descriptor); -void scheduler_start_thread (thread_t thread_descriptor); -void scheduler_kill_thread (thread_t thread_descriptor); +thread_T* scheduler_register_thread (thread_T* thread); +void scheduler_pause_thread (thread_T* thread); +void scheduler_start_thread (thread_T* thread); +void scheduler_kill_thread (thread_T* thread); -thread_t scheduler_get_current_thread (); +thread_T* scheduler_get_current_thread (); cpu_state_T* scheduler_switch_context (cpu_state_T* state); diff --git a/kernel/inc/proc/thread.h b/kernel/inc/proc/thread.h index aa8e9d7..3ce4103 100644 --- a/kernel/inc/proc/thread.h +++ b/kernel/inc/proc/thread.h @@ -19,27 +19,23 @@ #include "utils/stdtypes.h" #include "platform/cpu.h" -typedef struct thread_descriptor_T thread_descriptor_T; -typedef const thread_descriptor_T* thread_t; - -typedef struct { +typedef struct thread_T thread_T; +struct thread_T{ cpu_state_T state; uint64_t cpu_time; void* stack; uint32_t stack_size; - thread_descriptor_T* descriptor; -} thread_T; -struct thread_descriptor_T{ - thread_descriptor_T* prev; - thread_descriptor_T* next; - thread_T* thread; + // Scheduling data + thread_T* prev; + thread_T* next; }; -thread_t thread_spawn (void* function); -thread_t thread_spawn_from_state (cpu_state_T* state); -void thread_start (thread_t thread_descriptor); -void thread_pause (thread_t thread_descriptor); -void thread_kill (thread_t thread_descriptor); + +thread_T* thread_spawn (void* function); +thread_T* thread_spawn_from_state (cpu_state_T* state); +void thread_start (thread_T* thread); +void thread_pause (thread_T* thread); +void thread_kill (thread_T* thread); #endif //NOX_THREAD_H diff --git a/kernel/src/kmain.c b/kernel/src/kmain.c index 15b3187..e010a24 100644 --- a/kernel/src/kmain.c +++ b/kernel/src/kmain.c @@ -24,7 +24,7 @@ #include "proc/scheduler.h" #include "utils/io.h" -#include "platform/gdt.h" +#include "proc/thread.h" void limine_terminal_print(boot_info_T* boot_info, string_t string) { boot_info->terminal->write(boot_info->terminal->terminals[0], string, string_length(string)); @@ -72,10 +72,10 @@ void kmain(boot_info_T boot_info) { limine_terminal_print(&boot_info, "Kernel initialized\n"); log(LOG_INFO, "!=====[ Kernel Initialized ]=====!\n"); - thread_t thread = thread_spawn(test_b); + thread_T* thread = thread_spawn(test_b); thread_start(thread); - thread_t thread1 = thread_spawn(test_c); + thread_T* thread1 = thread_spawn(test_c); thread_start(thread1); test_a(); diff --git a/kernel/src/proc/scheduler.c b/kernel/src/proc/scheduler.c index 9f62285..28cedbb 100644 --- a/kernel/src/proc/scheduler.c +++ b/kernel/src/proc/scheduler.c @@ -30,7 +30,7 @@ void scheduler_init() { } cpu_state_T* scheduler_start(cpu_state_T* state) { - thread_descriptor_T* thread = thread_spawn_from_state(state); + thread_T* thread = thread_spawn_from_state(state); thread->prev = thread; thread->next = thread; g_scheduler.running_thread = thread; @@ -47,55 +47,51 @@ bool scheduler_is_initialized() { return g_scheduler.initialized; } -void scheduler_queue_add_thread_descriptor(thread_descriptor_T* descriptor) { +void scheduler_queue_add_thread(thread_T* thread) { if (g_scheduler.running_thread == NULL) { return; } - descriptor->prev = g_scheduler.running_thread; - descriptor->next = g_scheduler.running_thread->next; - g_scheduler.running_thread->next->prev = descriptor; - g_scheduler.running_thread->next = descriptor; + thread->prev = g_scheduler.running_thread; + thread->next = g_scheduler.running_thread->next; + g_scheduler.running_thread->next->prev = thread; + g_scheduler.running_thread->next = thread; } -void scheduler_queue_remove_thread_descriptor(thread_descriptor_T* descriptor) { - if (descriptor->prev == NULL || descriptor->next == NULL) { return; } +void scheduler_queue_remove_thread(thread_T* thread) { + if (thread->prev == NULL || thread->next == NULL) { return; } - descriptor->prev->next = descriptor->next; - descriptor->next->prev = descriptor->prev; - descriptor->prev = NULL; - descriptor->next = NULL; + thread->prev->next = thread->next; + thread->next->prev = thread->prev; + thread->prev = NULL; + thread->next = NULL; } -thread_t scheduler_register_thread(thread_T* thread) { - thread->descriptor = memory_allocate(sizeof(thread_descriptor_T)); - thread->descriptor->thread = thread; - +thread_T* scheduler_register_thread(thread_T* thread) { g_scheduler.num_threads++; log(LOG_INFO, " Registered thread"); - return thread->descriptor; + return thread; } -void scheduler_pause_thread(thread_t thread_descriptor) { - scheduler_queue_remove_thread_descriptor((thread_descriptor_T*)thread_descriptor); +void scheduler_pause_thread(thread_T* thread) { + scheduler_queue_remove_thread(thread); log(LOG_INFO, " Paused thread"); } -void scheduler_start_thread(thread_t thread_descriptor) { - scheduler_queue_add_thread_descriptor((thread_descriptor_T*)thread_descriptor); +void scheduler_start_thread(thread_T* thread) { + scheduler_queue_add_thread(thread); log(LOG_INFO, " Started thread"); } -void scheduler_kill_thread(thread_t thread_descriptor) { - scheduler_queue_remove_thread_descriptor((thread_descriptor_T*)thread_descriptor); +void scheduler_kill_thread(thread_T* thread) { + scheduler_queue_remove_thread(thread); - memory_free((void*)thread_descriptor); g_scheduler.num_threads--; log(LOG_INFO, " Killed thread"); } -thread_t scheduler_get_current_thread() { +thread_T* scheduler_get_current_thread() { return g_scheduler.running_thread; } @@ -107,8 +103,8 @@ cpu_state_T* scheduler_switch_context(cpu_state_T* state) { CORE_HALT_WHILE(g_scheduler.blocked) g_scheduler.blocked = true; - thread_T* old_thread = g_scheduler.running_thread->thread; - thread_T* new_thread = g_scheduler.running_thread->next->thread; + thread_T* old_thread = g_scheduler.running_thread; + thread_T* new_thread = g_scheduler.running_thread->next; if (old_thread->cpu_time > 0) { memory_copy(state, &old_thread->state, sizeof(cpu_state_T)); diff --git a/kernel/src/proc/thread.c b/kernel/src/proc/thread.c index 3fecdbf..1b7ada5 100644 --- a/kernel/src/proc/thread.c +++ b/kernel/src/proc/thread.c @@ -20,7 +20,7 @@ #include "platform/gdt.h" #include "utils/memory.h" -thread_t thread_spawn(void* function) { +thread_T* thread_spawn(void* function) { thread_T* thread = memory_allocate(sizeof(thread_T)); thread->stack_size = PFRAME_SIZE * 4; @@ -48,7 +48,7 @@ thread_t thread_spawn(void* function) { return scheduler_register_thread(thread); } -thread_t thread_spawn_from_state(cpu_state_T* state) { +thread_T* thread_spawn_from_state(cpu_state_T* state) { thread_T* thread = memory_allocate(sizeof(thread_T)); thread->stack_size = 0; @@ -60,18 +60,16 @@ thread_t thread_spawn_from_state(cpu_state_T* state) { return scheduler_register_thread(thread); } -void thread_start(thread_t thread_descriptor) { - scheduler_start_thread(thread_descriptor); +void thread_start(thread_T* thread) { + scheduler_start_thread(thread); } -void thread_pause(thread_t thread_descriptor) { - scheduler_pause_thread(thread_descriptor); +void thread_pause(thread_T* thread) { + scheduler_pause_thread(thread); } -void thread_kill(thread_t thread_descriptor) { - thread_T* thread = thread_descriptor->thread; - - scheduler_kill_thread(thread_descriptor); +void thread_kill(thread_T* thread) { + scheduler_kill_thread(thread); if (thread->stack != NULL) { memory_free(thread->stack);