diff options
| author | Richard Qin <richard.qin.x@gmail.com> | 2026-04-21 23:21:23 +0800 |
|---|---|---|
| committer | Richard Qin <richard.qin.x@gmail.com> | 2026-04-21 23:21:23 +0800 |
| commit | 1768a553c054b3e4774daaeda6c44f511ca7077a (patch) | |
| tree | 8d027233f3e04fdcc7b34679efef53a742ba20f6 /na/kernel | |
| parent | 92ac3931ec7c2ee7746d38499edd9179c943c5ac (diff) | |
| download | lume-1768a553c054b3e4774daaeda6c44f511ca7077a.tar.gz lume-1768a553c054b3e4774daaeda6c44f511ca7077a.tar.xz | |
feat: refactor Spinlock implementation to use atomic operations for lock management
Diffstat (limited to 'na/kernel')
| -rw-r--r-- | na/kernel/sync/spinlock.cc | 10 | ||||
| -rw-r--r-- | na/kernel/sync/spinlock.h | 3 |
2 files changed, 7 insertions, 6 deletions
diff --git a/na/kernel/sync/spinlock.cc b/na/kernel/sync/spinlock.cc index a03130d..b8c1435 100644 --- a/na/kernel/sync/spinlock.cc +++ b/na/kernel/sync/spinlock.cc @@ -23,7 +23,7 @@ // ============================================================ namespace sync { -CpuSyncState g_cpu_sync_states[kMaxCPUs]; +CpuSyncState g_cpu_sync_states[kMaxCpus]; void push_intr() { bool was_on = arch::cpu::intr_enabled(); @@ -53,7 +53,7 @@ void pop_intr() { void Spinlock::init(const char* name) { name_ = name; - locked_ = 0; + locked_.store(0, __ATOMIC_RELAXED); cpu_id_ = sync::kInvalidCpuId; } @@ -79,10 +79,10 @@ void Spinlock::acquire() { // Step 3: TTAS spin loop while (true) { // Outer: spin on local cache copy (no bus traffic) - while (__atomic_load_n(&locked_, __ATOMIC_RELAXED)) + while (locked_.load(__ATOMIC_RELAXED)) ; // Inner: attempt atomic exchange with acquire semantics - if (__atomic_exchange_n(&locked_, 1, __ATOMIC_ACQUIRE) == 0) + if (locked_.exchange(1, __ATOMIC_ACQUIRE) == 0) break; } @@ -100,7 +100,7 @@ void Spinlock::release() { cpu_id_ = sync::kInvalidCpuId; // Step 3: release the lock with release semantics - __atomic_store_n(&locked_, 0, __ATOMIC_RELEASE); + locked_.store(0, __ATOMIC_RELEASE); // Step 4: restore interrupt state if this was the last lock sync::pop_intr(); diff --git a/na/kernel/sync/spinlock.h b/na/kernel/sync/spinlock.h index b3e3393..cf70a30 100644 --- a/na/kernel/sync/spinlock.h +++ b/na/kernel/sync/spinlock.h @@ -14,6 +14,7 @@ */ #include <lume/types.h> +#include <lume/atomic.h> #include "cpu_state.h" class Spinlock { @@ -35,7 +36,7 @@ public: bool is_held_by_current_cpu() const; private: - uint32 locked_ = 0; // 0 = free, 1 = held + 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 }; |
