feat: add threading to Scheduler, fix desktop background selection freeze issue

This commit is contained in:
2026-05-23 16:08:25 +02:00
parent 28aadd3195
commit 159fea1667
15 changed files with 895 additions and 117 deletions
+53 -6
View File
@@ -10,12 +10,18 @@
namespace Sched {
// Process and thread slots share the same table: the main thread of a
// process owns the per-process resources (PML4, IPC handles, args/cwd/user,
// redirected I/O), while sibling threads occupy additional slots whose
// `primarySlot` points back to the main thread. The scheduler treats
// every slot uniformly -- the primary/sibling distinction matters only
// for resource ownership on exit.
static constexpr int MaxProcesses = 256;
static constexpr uint64_t StackPages = 4; // 16 KiB kernel stack per process
static constexpr uint64_t StackPages = 4; // 16 KiB kernel stack per thread
static constexpr uint64_t StackSize = StackPages * 0x1000;
static constexpr uint64_t UserStackPages = 8; // 32 KiB user stack
static constexpr uint64_t UserStackPages = 8; // 32 KiB user stack (main thread)
static constexpr uint64_t UserStackSize = UserStackPages * 0x1000;
static constexpr uint64_t UserStackTop = 0x7FFFFFF000ULL; // User stack top VA
static constexpr uint64_t UserStackTop = 0x7FFFFFF000ULL; // Main-thread user stack top VA
static constexpr uint64_t UserHeapBase = 0x40000000ULL; // User heap start VA
static constexpr uint32_t UserReadDirSlots = 64; // rotating scratch pages for SYS_READDIR
static constexpr uint64_t UserReadDirBase =
@@ -57,6 +63,16 @@ namespace Sched {
bool reapReady = false; // Set once teardown is complete and BSP may free slot resources
bool startPending = false; // Spawned but not yet made runnable
// Threading. Every slot is a schedulable thread. The "main thread" of a
// process has primarySlot == own slot and owns per-process resources
// (pml4Phys, IPC handles, libs, heap, redirected I/O, cwd/user/args).
// Sibling threads share those resources via primarySlot.
int primarySlot = -1; // Index into processTable; -1 if Free
int joinerSlot = -1; // Slot blocked in JoinThread on this slot
int exitCode = 0; // Returned to joiner / accumulated for the process
bool joinable = true; // Sibling thread keeps Terminated state for the joiner
uint64_t threadArg = 0; // Argument passed into entryPoint on first dispatch
// I/O redirection for GUI terminal
bool redirected = false;
int parentPid = -1;
@@ -97,15 +113,46 @@ namespace Sched {
// tick interval. The BSP runs at 1 ms; APs may use a coarser interval.
void Tick(uint32_t elapsedMs = 1);
// Get the PID of the currently running process (-1 if idle)
// Get the PID of the currently running process (-1 if idle).
// For sibling threads this returns the primary slot's PID -- i.e. the
// process-level identifier, which is what userspace sees.
int GetCurrentPid();
// Get a pointer to the currently running process (nullptr if idle)
// Get the per-thread id of the currently running thread (-1 if idle).
// For the main thread this equals GetCurrentPid().
int GetCurrentTid();
// Get a pointer to the currently running process (primary slot, nullptr if idle).
// Always returns the slot that owns per-process state -- never a sibling thread.
Process* GetCurrentProcessPtr();
// Called by terminated processes to mark themselves done
// Get a pointer to the currently running thread's slot (may be a sibling).
Process* GetCurrentThreadPtr();
// Called by terminated processes to mark themselves done.
// Tears down the entire process (kills all sibling threads, frees address space).
void ExitProcess();
// ====================================================================
// Threading
// ====================================================================
// Spawn a sibling thread inside the current process.
// `entry` is a user-mode RIP, `arg` is delivered to entry in %rdi, and
// `userStackTop` points to the top-of-stack the caller (libc) allocated
// out of the user heap. Returns the new TID, or -1 on failure.
int SpawnThread(uint64_t entry, uint64_t arg, uint64_t userStackTop);
// Terminate the currently executing thread. If this is the main thread,
// the entire process exits (equivalent to ExitProcess).
[[noreturn]] void ExitCurrentThread(int exitCode);
// Block the current thread until `tid` (a sibling of the same process)
// terminates. On success, writes the exit code via outExitCode (may be
// null) and frees the joined thread's slot. Returns 0 on success, -1 if
// tid is invalid or not joinable by the caller.
int JoinThread(int tid, int* outExitCode);
// Check if a process is still alive (Ready, Running, or Blocked)
bool IsAlive(int pid);