summaryrefslogtreecommitdiff
path: root/include/kernel/spinlock.h
diff options
context:
space:
mode:
authorRichard Qin <richard.qin.x@gmail.com>2026-01-03 02:19:57 +0800
committerRichard Qin <richard.qin.x@gmail.com>2026-01-03 02:19:57 +0800
commit210025560c80b48af4875af199f183ad709e3bf0 (patch)
treeeb11aeb7b641187f207e3b572c5184cf3526b463 /include/kernel/spinlock.h
parentbde87490d61135dcaa94d9b1489ff6c39876d169 (diff)
parent10614e04929a6d109a5e90524e446b637aaf0c13 (diff)
downloadlume-210025560c80b48af4875af199f183ad709e3bf0.tar.gz
lume-210025560c80b48af4875af199f183ad709e3bf0.tar.xz
Merge pull request #4 from Richard-Qin-001/dev/shell
Rebuilt the system's underlying structure; it is now stable enough to merge. The system now supports a read-only file system and has a simple shell implemented.
Diffstat (limited to 'include/kernel/spinlock.h')
-rw-r--r--include/kernel/spinlock.h52
1 files changed, 40 insertions, 12 deletions
diff --git a/include/kernel/spinlock.h b/include/kernel/spinlock.h
index aec4b6a..3102e68 100644
--- a/include/kernel/spinlock.h
+++ b/include/kernel/spinlock.h
@@ -1,20 +1,48 @@
#pragma once
#include "common/types.h"
-struct spinlock
+struct cpu;
+
+// 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);
+
+ // Acquire lock
+ void acquire();
- char *name;
- int cpu_id;
+ // 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