fix (kernel): removed unneeded complexiticy of thread descriptors

This commit is contained in:
antifallobst 2023-02-19 09:13:17 +01:00
parent fa014b5b5c
commit 9e42cb1a4e
5 changed files with 54 additions and 67 deletions

View File

@ -19,12 +19,9 @@
#include "proc/thread.h" #include "proc/thread.h"
#include "utils/bitmap.h" #include "utils/bitmap.h"
#define SCHEDULER_MAX_PROCESSES 32
#define SCHEDULER_MAX_THREADS_PER_PROCESS 16
typedef struct { typedef struct {
uint32_t num_threads; uint32_t num_threads;
thread_descriptor_T* running_thread; thread_T* running_thread;
bool blocked; bool blocked;
bool initialized; bool initialized;
@ -34,12 +31,12 @@ void scheduler_init ();
cpu_state_T* scheduler_start (cpu_state_T* state); cpu_state_T* scheduler_start (cpu_state_T* state);
bool scheduler_is_initialized (); bool scheduler_is_initialized ();
thread_t scheduler_register_thread (thread_T* thread); thread_T* scheduler_register_thread (thread_T* thread);
void scheduler_pause_thread (thread_t thread_descriptor); void scheduler_pause_thread (thread_T* thread);
void scheduler_start_thread (thread_t thread_descriptor); void scheduler_start_thread (thread_T* thread);
void scheduler_kill_thread (thread_t thread_descriptor); 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); cpu_state_T* scheduler_switch_context (cpu_state_T* state);

View File

@ -19,27 +19,23 @@
#include "utils/stdtypes.h" #include "utils/stdtypes.h"
#include "platform/cpu.h" #include "platform/cpu.h"
typedef struct thread_descriptor_T thread_descriptor_T; typedef struct thread_T thread_T;
typedef const thread_descriptor_T* thread_t; struct thread_T{
typedef struct {
cpu_state_T state; cpu_state_T state;
uint64_t cpu_time; uint64_t cpu_time;
void* stack; void* stack;
uint32_t stack_size; uint32_t stack_size;
thread_descriptor_T* descriptor;
} thread_T;
struct thread_descriptor_T{ // Scheduling data
thread_descriptor_T* prev; thread_T* prev;
thread_descriptor_T* next; thread_T* next;
thread_T* thread;
}; };
thread_t thread_spawn (void* function);
thread_t thread_spawn_from_state (cpu_state_T* state); thread_T* thread_spawn (void* function);
void thread_start (thread_t thread_descriptor); thread_T* thread_spawn_from_state (cpu_state_T* state);
void thread_pause (thread_t thread_descriptor); void thread_start (thread_T* thread);
void thread_kill (thread_t thread_descriptor); void thread_pause (thread_T* thread);
void thread_kill (thread_T* thread);
#endif //NOX_THREAD_H #endif //NOX_THREAD_H

View File

@ -24,7 +24,7 @@
#include "proc/scheduler.h" #include "proc/scheduler.h"
#include "utils/io.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) { 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)); 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"); limine_terminal_print(&boot_info, "Kernel initialized\n");
log(LOG_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_start(thread);
thread_t thread1 = thread_spawn(test_c); thread_T* thread1 = thread_spawn(test_c);
thread_start(thread1); thread_start(thread1);
test_a(); test_a();

View File

@ -30,7 +30,7 @@ void scheduler_init() {
} }
cpu_state_T* scheduler_start(cpu_state_T* state) { 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->prev = thread;
thread->next = thread; thread->next = thread;
g_scheduler.running_thread = thread; g_scheduler.running_thread = thread;
@ -47,55 +47,51 @@ bool scheduler_is_initialized() {
return g_scheduler.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; } if (g_scheduler.running_thread == NULL) { return; }
descriptor->prev = g_scheduler.running_thread; thread->prev = g_scheduler.running_thread;
descriptor->next = g_scheduler.running_thread->next; thread->next = g_scheduler.running_thread->next;
g_scheduler.running_thread->next->prev = descriptor; g_scheduler.running_thread->next->prev = thread;
g_scheduler.running_thread->next = descriptor; g_scheduler.running_thread->next = thread;
} }
void scheduler_queue_remove_thread_descriptor(thread_descriptor_T* descriptor) { void scheduler_queue_remove_thread(thread_T* thread) {
if (descriptor->prev == NULL || descriptor->next == NULL) { return; } if (thread->prev == NULL || thread->next == NULL) { return; }
descriptor->prev->next = descriptor->next; thread->prev->next = thread->next;
descriptor->next->prev = descriptor->prev; thread->next->prev = thread->prev;
descriptor->prev = NULL; thread->prev = NULL;
descriptor->next = NULL; thread->next = NULL;
} }
thread_t scheduler_register_thread(thread_T* thread) { thread_T* scheduler_register_thread(thread_T* thread) {
thread->descriptor = memory_allocate(sizeof(thread_descriptor_T));
thread->descriptor->thread = thread;
g_scheduler.num_threads++; g_scheduler.num_threads++;
log(LOG_INFO, "<Scheduler> Registered thread"); log(LOG_INFO, "<Scheduler> Registered thread");
return thread->descriptor; return thread;
} }
void scheduler_pause_thread(thread_t thread_descriptor) { void scheduler_pause_thread(thread_T* thread) {
scheduler_queue_remove_thread_descriptor((thread_descriptor_T*)thread_descriptor); scheduler_queue_remove_thread(thread);
log(LOG_INFO, "<Scheduler> Paused thread"); log(LOG_INFO, "<Scheduler> Paused thread");
} }
void scheduler_start_thread(thread_t thread_descriptor) { void scheduler_start_thread(thread_T* thread) {
scheduler_queue_add_thread_descriptor((thread_descriptor_T*)thread_descriptor); scheduler_queue_add_thread(thread);
log(LOG_INFO, "<Scheduler> Started thread"); log(LOG_INFO, "<Scheduler> Started thread");
} }
void scheduler_kill_thread(thread_t thread_descriptor) { void scheduler_kill_thread(thread_T* thread) {
scheduler_queue_remove_thread_descriptor((thread_descriptor_T*)thread_descriptor); scheduler_queue_remove_thread(thread);
memory_free((void*)thread_descriptor);
g_scheduler.num_threads--; g_scheduler.num_threads--;
log(LOG_INFO, "<Scheduler> Killed thread"); log(LOG_INFO, "<Scheduler> Killed thread");
} }
thread_t scheduler_get_current_thread() { thread_T* scheduler_get_current_thread() {
return g_scheduler.running_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) CORE_HALT_WHILE(g_scheduler.blocked)
g_scheduler.blocked = true; g_scheduler.blocked = true;
thread_T* old_thread = g_scheduler.running_thread->thread; thread_T* old_thread = g_scheduler.running_thread;
thread_T* new_thread = g_scheduler.running_thread->next->thread; thread_T* new_thread = g_scheduler.running_thread->next;
if (old_thread->cpu_time > 0) { if (old_thread->cpu_time > 0) {
memory_copy(state, &old_thread->state, sizeof(cpu_state_T)); memory_copy(state, &old_thread->state, sizeof(cpu_state_T));

View File

@ -20,7 +20,7 @@
#include "platform/gdt.h" #include "platform/gdt.h"
#include "utils/memory.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_T* thread = memory_allocate(sizeof(thread_T));
thread->stack_size = PFRAME_SIZE * 4; thread->stack_size = PFRAME_SIZE * 4;
@ -48,7 +48,7 @@ thread_t thread_spawn(void* function) {
return scheduler_register_thread(thread); 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_T* thread = memory_allocate(sizeof(thread_T));
thread->stack_size = 0; thread->stack_size = 0;
@ -60,18 +60,16 @@ thread_t thread_spawn_from_state(cpu_state_T* state) {
return scheduler_register_thread(thread); return scheduler_register_thread(thread);
} }
void thread_start(thread_t thread_descriptor) { void thread_start(thread_T* thread) {
scheduler_start_thread(thread_descriptor); scheduler_start_thread(thread);
} }
void thread_pause(thread_t thread_descriptor) { void thread_pause(thread_T* thread) {
scheduler_pause_thread(thread_descriptor); scheduler_pause_thread(thread);
} }
void thread_kill(thread_t thread_descriptor) { void thread_kill(thread_T* thread) {
thread_T* thread = thread_descriptor->thread; scheduler_kill_thread(thread);
scheduler_kill_thread(thread_descriptor);
if (thread->stack != NULL) { if (thread->stack != NULL) {
memory_free(thread->stack); memory_free(thread->stack);