MCPcopy Create free account
hub / github.com/Simple-XX/SimpleKernel / Lock

Method Lock

src/include/spinlock.hpp:36–60  ·  view source on GitHub ↗

* @brief 获得锁 * @return Expected 成功返回空值,失败返回错误 */

Source from the content-addressed store, hash-verified

34 * @return Expected<void> 成功返回空值,失败返回错误
35 */
36 [[nodiscard]] __always_inline auto Lock() -> Expected<void> {
37 auto intr_enable = cpu_io::GetInterruptStatus();
38 cpu_io::DisableInterrupt();
39
40 // 先尝试获取锁
41 while (locked_.test_and_set(std::memory_order_acquire)) {
42 // 在等待时检查是否是当前核心持有锁(递归锁检测)
43 if (core_id_.load(std::memory_order_acquire) ==
44 cpu_io::GetCurrentCoreId()) {
45 // 递归锁定,恢复中断状态并返回失败
46 if (intr_enable) {
47 cpu_io::EnableInterrupt();
48 }
49 // klog::Err("spinlock {}: {} recursive lock detected.",
50 // cpu_io::GetCurrentCoreId(), name);
51 return std::unexpected(Error{ErrorCode::kSpinLockRecursiveLock});
52 }
53 cpu_io::Pause();
54 }
55
56 // 获取锁成功后立即设置 core_id_
57 core_id_.store(cpu_io::GetCurrentCoreId(), std::memory_order_release);
58 saved_intr_enable_ = intr_enable;
59 return {};
60 }
61
62 /**
63 * @brief 释放锁

Callers 2

spinlock.hppFile · 0.45
LockGuardMethod · 0.45

Calls 5

GetInterruptStatusFunction · 0.85
DisableInterruptFunction · 0.85
GetCurrentCoreIdFunction · 0.85
EnableInterruptFunction · 0.85
PauseFunction · 0.85

Tested by

no test coverage detected