From 7759eef8cdac79028dd5b468bd9ce0cf1c12b162 Mon Sep 17 00:00:00 2001 From: Richard Qin Date: Tue, 30 Dec 2025 14:21:40 +0800 Subject: dev(spinlock): refactoring the implementation of locks --- include/kernel/proc.h | 22 ++++++++++++++++--- include/kernel/spinlock.h | 54 ++++++++++++++++++++++++++++++++++++----------- 2 files changed, 61 insertions(+), 15 deletions(-) (limited to 'include') diff --git a/include/kernel/proc.h b/include/kernel/proc.h index b557267..e77fdc9 100644 --- a/include/kernel/proc.h +++ b/include/kernel/proc.h @@ -3,11 +3,13 @@ #include "kernel/trap.h" #include "kernel/spinlock.h" +// Context structure, used for context switching struct Context { uint64 ra; uint64 sp; + // Callee-saved registers uint64 s0; uint64 s1; uint64 s2; @@ -22,7 +24,9 @@ struct Context uint64 s11; }; -enum ProcState{ +// Process State Enumeration +enum ProcState +{ UNUSED, USED, SLEEPING, @@ -31,6 +35,7 @@ enum ProcState{ ZOMBIE, }; +// Process Control Block (PCB) struct Proc { struct Context context; @@ -41,15 +46,25 @@ struct Proc enum ProcState state; int pid; - void* kstack; + void *kstack; char name[16]; uint64 sz; struct Proc *parent; void *chan; int xstate; + + // Implement later }; +// CPU status structure +struct cpu +{ + struct Proc *proc; // Processes currently running on this CPU + struct Context context; // Save the scheduler context here + int noff; // Depth of push_off() + int intena; // Interrupt enable status before closing +}; namespace ProcManager { @@ -62,7 +77,8 @@ namespace ProcManager void user_init(); void exit(int status); int wait(uint64 addr); - void sleep(void *chan, struct spinlock *lk); + + void sleep(void *chan, Spinlock *lk); void wakeup(void *chan); } diff --git a/include/kernel/spinlock.h b/include/kernel/spinlock.h index aec4b6a..b3cc93e 100644 --- a/include/kernel/spinlock.h +++ b/include/kernel/spinlock.h @@ -1,20 +1,50 @@ #pragma once + #include "common/types.h" -struct spinlock +struct cpu; +struct Proc; + +// Spinlock +// Features: Non-interruptible, busy-wait loop, extremely short hold time. +// Scenario: Protecting core data such as scheduler, interrupt handling, PMM, etc. +class Spinlock { - uint locked; +public: + volatile uint32 locked; // Whether it is locked (0: unlocked, 1: locked) + const char *name; // Lock Name (for debugging) + struct cpu *cpu; // The CPU holding the lock + + // Constructor + Spinlock() : locked(0), name("uninit"), cpu(nullptr) {} + + // Initialize lock name + void init(const char *name); - char *name; - int cpu_id; + // Acquire lock + void acquire(); + + // Release lock + void release(); + + // Check if the current CPU holds the lock + bool holding(); }; -namespace Spinlock +// Mutex +// Features: Allows interruption, allows sleeping (yield CPU), can hold for a long time. +// Scenarios: File system IO, device driver waiting, long-term buffering operations. +class Mutex { - void init(struct spinlock *lk, char *name); - void acquire(struct spinlock *lk); - void release(struct spinlock *lk); - int holding(struct spinlock *lk); - void push_off(); - void pop_off(); -} \ No newline at end of file +public: + uint32 locked; // Whether it is locked + const char *name; // Lock Name + int pid; // PID of the process holding the lock (for debugging) + + Spinlock lk; // Internal spin lock, protects the state of the Mutex itself + + void init(const char *name); + void acquire(); + void release(); + bool holding(); +}; \ No newline at end of file -- cgit v1.2.3