summaryrefslogtreecommitdiff
path: root/na/kernel/sync/spinlock.h
diff options
context:
space:
mode:
authorRichard Qin <richard.qin.x@gmail.com>2026-04-17 01:05:31 +0800
committerRichard Qin <richard.qin.x@gmail.com>2026-04-17 01:05:31 +0800
commite2564d112ba03b432f9f44f445cbc4a3ed85751a (patch)
tree1541ee010bfd9c0e614fc91c2de5c9aa741418ae /na/kernel/sync/spinlock.h
parent81563cb415d8214f1d1e34f73d49a404424f8f2c (diff)
downloadlume-e2564d112ba03b432f9f44f445cbc4a3ed85751a.tar.gz
lume-e2564d112ba03b432f9f44f445cbc4a3ed85751a.tar.xz
feat: add initial RISC-V architecture support and core kernel functionality
- Implement MMU hardware primitives for RISC-V SV39 in mmu.h - Create trap vector control primitives in trap.h - Define trap frame structure for RISC-V in trapframe.h - Add assembly routines for context switching in swtch.S - Implement trap vector entry/exit in trap_vector.S - Introduce basic error number definitions in errno.h - Provide placement new/delete operators in new.h - Define kernel panic handler in panic.h and implement in panic.cc - Establish basic type definitions in types.h - Create the kernel main entry point in main.cc with global initialization - Add synchronization primitives using spinlocks in spinlock.h and spinlock.cc - Define per-CPU state tracking for interrupts in cpu_state.h - Create a linker script for the kernel in linker.ld - Update module makefiles for kernel and library components
Diffstat (limited to 'na/kernel/sync/spinlock.h')
-rw-r--r--na/kernel/sync/spinlock.h60
1 files changed, 60 insertions, 0 deletions
diff --git a/na/kernel/sync/spinlock.h b/na/kernel/sync/spinlock.h
new file mode 100644
index 0000000..b3e3393
--- /dev/null
+++ b/na/kernel/sync/spinlock.h
@@ -0,0 +1,60 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/* Copyright (C) 2026 Richard Qin */
+
+#pragma once
+
+/*
+ * Spinlock & LockGuard
+ *
+ * Provides mutual exclusion for SMP kernels using TTAS
+ * (Test-and-Test-and-Set) with acquire/release memory ordering.
+ * Interrupt disable/enable is managed automatically via push_intr/pop_intr.
+ *
+ * Reference: docs/specs/spinlock.md
+ */
+
+#include <lume/types.h>
+#include "cpu_state.h"
+
+class Spinlock {
+public:
+ // Default: BSS zero is safe. Call init() to give the lock a name.
+ Spinlock() = default;
+
+ // Explicit initialization (for global objects or Placement New)
+ void init(const char* name);
+
+ // Non-copyable, non-movable (lock identity is its address)
+ Spinlock(const Spinlock&) = delete;
+ Spinlock& operator=(const Spinlock&) = delete;
+ Spinlock(Spinlock&&) = delete;
+ Spinlock& operator=(Spinlock&&) = delete;
+
+ void acquire();
+ void release();
+ bool is_held_by_current_cpu() const;
+
+private:
+ uint32 locked_ = 0; // 0 = free, 1 = held
+ const char* name_ = "uninit"; // Lock name (for panic messages)
+ uint64 cpu_id_ = sync::kInvalidCpuId; // Holder CPU ID
+};
+
+class LockGuard {
+public:
+ explicit LockGuard(Spinlock& lock) : lock_(lock) {
+ lock_.acquire();
+ }
+
+ ~LockGuard() {
+ lock_.release();
+ }
+
+ LockGuard(const LockGuard&) = delete;
+ LockGuard& operator=(const LockGuard&) = delete;
+ LockGuard(LockGuard&&) = delete;
+ LockGuard& operator=(LockGuard&&) = delete;
+
+private:
+ Spinlock& lock_;
+};