[Kernel] Add idle thread

This commit is contained in:
wheremyfoodat 2023-04-18 20:42:17 +03:00
parent 94ea97a419
commit 0b46b92bb7
6 changed files with 88 additions and 3 deletions

View file

@ -1,8 +1,9 @@
#pragma once
#include <cstdint>
// Status register definitions
namespace CPSR {
enum : u32 {
enum : std::uint32_t {
// Privilege modes
UserMode = 16,
FIQMode = 17,
@ -26,7 +27,7 @@ namespace CPSR {
namespace FPSCR {
// FPSCR Flags
enum : u32 {
enum : std::uint32_t {
Sign = 1U << 31U, // Negative condition flag
Zero = 1 << 30, // Zero condition flag
Carry = 1 << 29, // Carry condition flag

View file

@ -1,5 +1,6 @@
#pragma once
#include <array>
#include <cassert>
#include <limits>
#include <string>
#include <vector>
@ -19,7 +20,16 @@ class Kernel {
// The handle number for the next kernel object to be created
u32 handleCounter;
std::array<Thread, appResourceLimits.maxThreads> threads;
// A list of our OS threads, the max number of which depends on the resource limit (hardcoded 32 per process on retail it seems).
// We have an extra thread for when no thread is capable of running. This thread is called the "idle thread" in our code
// This thread is set up in setupIdleThread and just yields in a loop to see if any other thread has woken up
std::array<Thread, appResourceLimits.maxThreads + 1> threads;
static constexpr int idleThreadIndex = appResourceLimits.maxThreads;
// Our waitlist system uses a bitfield of 64 bits to show which threads are waiting on an object.
// That means we can have a maximum of 63 threads + 1 idle thread. This assert should never trigger because the max thread # is 32
// But we have it here for safety purposes
static_assert(appResourceLimits.maxThreads <= 63, "The waitlist system is built on the premise that <= 63 threads max can be active");
std::vector<KernelObject> objects;
std::vector<Handle> portHandles;
@ -71,6 +81,7 @@ private:
s32 getCurrentResourceValue(const KernelObject* limit, u32 resourceName);
u32 getMaxForResource(const KernelObject* limit, u32 resourceName);
u32 getTLSPointer();
void setupIdleThread();
bool isWaitable(const KernelObject* object);

View file

@ -111,6 +111,7 @@ class Memory {
SharedMemoryBlock(0, 0x1000, KernelHandles::HIDSharedMemHandle) // HID shared memory
};
public:
static constexpr u32 pageShift = 12;
static constexpr u32 pageSize = 1 << pageShift;
static constexpr u32 pageMask = pageSize - 1;
@ -125,6 +126,7 @@ class Memory {
static constexpr u32 DSP_CODE_MEMORY_OFFSET = 0_KB;
static constexpr u32 DSP_DATA_MEMORY_OFFSET = 256_KB;
private:
std::bitset<FCRAM_PAGE_COUNT> usedFCRAMPages;
std::optional<u32> findPaddr(u32 size);
u64 timeSince3DSEpoch();