From 4e95d7b6b2850be0b85cc3da7270354320e6b771 Mon Sep 17 00:00:00 2001 From: jaby Date: Fri, 14 Jun 2024 19:56:44 +0200 Subject: [PATCH] Improve thread code --- .../System/callbacks_internal.hpp | 15 +++--- .../internal-include}/System/threads.hpp | 47 +++++++++---------- src/Library/src/BootLoader/callbacks_boot.cpp | 6 +-- src/Library/src/CD/cd.cpp | 2 +- src/Library/src/System/callbacks.cpp | 2 +- 5 files changed, 36 insertions(+), 36 deletions(-) rename {include/PSX => src/Library/internal-include}/System/threads.hpp (51%) diff --git a/src/Library/internal-include/System/callbacks_internal.hpp b/src/Library/internal-include/System/callbacks_internal.hpp index 8074bec6..b3250b4a 100644 --- a/src/Library/internal-include/System/callbacks_internal.hpp +++ b/src/Library/internal-include/System/callbacks_internal.hpp @@ -1,12 +1,15 @@ #pragma once -#include +#include "threads.hpp" namespace JabyEngine { namespace Callback { namespace internal { - static void execute_thread(SysCall::ThreadHandle thread_handle) { - MainThread::prepare_if_main(thread_handle); - Thread::execute_next(); + static void execute_callback(Thread::Handle thread_handle) { + if(CurrentThread::is_me(MainThread::Handle)) { + CurrentThread::replace_with(thread_handle); + } + + SysCall::ReturnFromException(); } namespace VSync { @@ -17,7 +20,7 @@ namespace JabyEngine { void routine(); static void [[deprecated("Currently not in use")]] execute() { - execute_thread(VSync::thread_handle); + execute_callback(VSync::thread_handle); } } @@ -28,7 +31,7 @@ namespace JabyEngine { extern uint32_t stack[StackSize]; static void execute() { - execute_thread(CD::thread_handle); + execute_callback(CD::thread_handle); } } } diff --git a/include/PSX/System/threads.hpp b/src/Library/internal-include/System/threads.hpp similarity index 51% rename from include/PSX/System/threads.hpp rename to src/Library/internal-include/System/threads.hpp index bf2d7a55..155ecbdd 100644 --- a/include/PSX/System/threads.hpp +++ b/src/Library/internal-include/System/threads.hpp @@ -1,22 +1,21 @@ #pragma once -#include "syscalls.hpp" +#include namespace JabyEngine { struct Thread { - static uintptr_t get_pic() { - return table_of_tables.processes->current_tcb->epc; - } + using Handle = SysCall::ThreadHandle; static constexpr uint32_t idx_from_handle(SysCall::ThreadHandle thread) { return thread & 0xFFFF; } - static void prepare_next(SysCall::ThreadHandle thread) { - table_of_tables.processes->current_tcb = &table_of_tables.threads[idx_from_handle(thread)]; + static uintptr_t get_pic_of(Handle handle) { + return table_of_tables.threads[idx_from_handle(handle)].epc; } - static void execute_next() { - SysCall::ReturnFromException(); + template + static Handle create(void(*function)(), uint32_t(&stack)[N]) { + return SysCall::OpenThread(function, &stack[N - 1], SysCall::get_gp()); } static void set_kernel_mode_for(SysCall::ThreadHandle handle) { @@ -28,25 +27,25 @@ namespace JabyEngine { } }; + struct CurrentThread { + static uintptr_t get_pic() { + return table_of_tables.processes->current_tcb->epc; + } + + static bool is_me(Thread::Handle handle) { + return table_of_tables.processes->current_tcb == &table_of_tables.threads[Thread::idx_from_handle(handle)]; + } + + static void replace_with(Thread::Handle handle) { + table_of_tables.processes->current_tcb = &table_of_tables.threads[Thread::idx_from_handle(handle)]; + } + }; + struct MainThread { + static constexpr const Thread::Handle Handle = 0xFF000000; + static uintptr_t get_pic() { return table_of_tables.threads[0].epc; } - - static void prepare_if_main(SysCall::ThreadHandle handle) { - if(table_of_tables.processes->current_tcb == &table_of_tables.threads[0]) { - Thread::prepare_next(handle); - } - } - - static void prepare_restore() { - Thread::prepare_next(0); - } - - static void restore() { - SysCall::EnterCriticalSection(); - MainThread::prepare_restore(); - Thread::execute_next(); - } }; } \ No newline at end of file diff --git a/src/Library/src/BootLoader/callbacks_boot.cpp b/src/Library/src/BootLoader/callbacks_boot.cpp index 10b9e3f0..f9a421dd 100644 --- a/src/Library/src/BootLoader/callbacks_boot.cpp +++ b/src/Library/src/BootLoader/callbacks_boot.cpp @@ -16,11 +16,9 @@ namespace JabyEngine { ); Thread::set_user_mode_for(InternalCallback::VSync::thread_handle);*/ - InternalCallback::CD::thread_handle = SysCall::OpenThread( + InternalCallback::CD::thread_handle = Thread::create( reinterpret_cast(JabyEngine::CD::internal::IRQ::data_ready_handler), - &InternalCallback::CD::stack[InternalCallback::CD::StackSize - 1], - SysCall::get_gp() - ); + InternalCallback::CD::stack); Thread::set_user_mode_for(InternalCallback::CD::thread_handle); SysCall::ExitCriticalSection(); } diff --git a/src/Library/src/CD/cd.cpp b/src/Library/src/CD/cd.cpp index da421793..a4bef6b3 100644 --- a/src/Library/src/CD/cd.cpp +++ b/src/Library/src/CD/cd.cpp @@ -157,7 +157,7 @@ namespace JabyEngine { void data_ready_handler(uint32_t data) { while(true) { printf("Data: %i\n", data); - SysCall::ChangeThread(0xFF000000); + SysCall::ChangeThread(MainThread::Handle); } } } diff --git a/src/Library/src/System/callbacks.cpp b/src/Library/src/System/callbacks.cpp index 1f66b6c4..69f027a6 100644 --- a/src/Library/src/System/callbacks.cpp +++ b/src/Library/src/System/callbacks.cpp @@ -19,7 +19,7 @@ namespace JabyEngine { /*if(VSyncCallback::callback) { VSyncCallback::callback(); }*/ - MainThread::restore(); + SysCall::ChangeThread(MainThread::Handle); } } }