Improve thread code

This commit is contained in:
Björn Gaier 2024-06-14 19:56:44 +02:00
parent 13390bff08
commit 96644c55b0
5 changed files with 36 additions and 36 deletions

View File

@ -1,12 +1,15 @@
#pragma once
#include <PSX/System/threads.hpp>
#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);
}
}
}

View File

@ -1,22 +1,21 @@
#pragma once
#include "syscalls.hpp"
#include <PSX/System/syscalls.hpp>
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<size_t N>
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();
}
};
}

View File

@ -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<void(*)()>(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();
}

View File

@ -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);
}
}
}

View File

@ -19,7 +19,7 @@ namespace JabyEngine {
/*if(VSyncCallback::callback) {
VSyncCallback::callback();
}*/
MainThread::restore();
SysCall::ChangeThread(MainThread::Handle);
}
}
}