summaryrefslogtreecommitdiff
path: root/kernel/sync/spinlock.h
diff options
context:
space:
mode:
authorRichard Qin <richard.qin.x@gmail.com>2026-07-10 11:31:46 +0800
committerRichard Qin <richard.qin.x@gmail.com>2026-07-10 11:31:46 +0800
commite90b72ec3082588b296956730bd2930cf8d5d671 (patch)
tree36ff6916af9b4176ea5a9662219d11e721f0b691 /kernel/sync/spinlock.h
parent1767d390fbf3d0c65389de46caf0b6d43ef3b6ad (diff)
downloadlume-e90b72ec3082588b296956730bd2930cf8d5d671.tar.gz
lume-e90b72ec3082588b296956730bd2930cf8d5d671.tar.xz
refactor: promote new architecture to repository root
Archive the deprecated implementation under lume-legacy, make the new kernel the default root-level build, and update repository entry points without changing kernel behavior.
Diffstat (limited to 'kernel/sync/spinlock.h')
-rw-r--r--kernel/sync/spinlock.h61
1 files changed, 61 insertions, 0 deletions
diff --git a/kernel/sync/spinlock.h b/kernel/sync/spinlock.h
new file mode 100644
index 0000000..a1369d1
--- /dev/null
+++ b/kernel/sync/spinlock.h
@@ -0,0 +1,61 @@
+/* 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/atomic.h>
+#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:
+ lume::atomic<uint32> locked_; // 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_;
+};