SimpleKernel 1.17.0
Loading...
Searching...
No Matches
SpinLock Class Reference

自旋锁 More...

#include <spinlock.hpp>

Collaboration diagram for SpinLock:
Collaboration graph

Public Member Functions

__always_inline auto Lock () -> Expected< void >
 获得锁
 
__always_inline auto UnLock () -> Expected< void >
 释放锁
 
构造/析构函数
 SpinLock (const char *_name)
 构造函数
 
 SpinLock ()=default
 
 SpinLock (const SpinLock &)=delete
 
 SpinLock (SpinLock &&)=default
 
auto operator= (const SpinLock &) -> SpinLock &=delete
 
auto operator= (SpinLock &&) -> SpinLock &=default
 
 ~SpinLock ()=default
 

Public Attributes

const char * name {"unnamed"}
 自旋锁名称
 

Protected Member Functions

__always_inline auto IsLockedByCurrentCore () -> bool
 检查当前 core 是否获得此锁
 

Protected Attributes

std::atomic_flag locked_ {ATOMIC_FLAG_INIT}
 是否 Lock
 
std::atomic< size_t > core_id_ {std::numeric_limits<size_t>::max()}
 获得此锁的 core_id
 
bool saved_intr_enable_ {false}
 保存的中断状态
 

Detailed Description

自旋锁

Note
使用限制:
  1. 不可重入:不支持同一核心递归获取锁,会导致返回失败
  2. 关中断:获取锁时会自动关闭中断,释放锁时恢复之前的状态
  3. 必须配对:必须在获取锁的同一个核心释放锁
  4. 不可休眠:持有自旋锁期间不可执行休眠或调度操作
  5. 副作用:修改当前 CPU 的中断状态

Definition at line 27 of file spinlock.hpp.

Constructor & Destructor Documentation

◆ SpinLock() [1/4]

SpinLock::SpinLock ( const char *  _name)
inlineexplicit

构造函数

Parameters
_name锁名
Note
需要堆初始化后可用

Definition at line 92 of file spinlock.hpp.

92: name(_name) {}
const char * name
自旋锁名称
Definition spinlock.hpp:30

◆ SpinLock() [2/4]

SpinLock::SpinLock ( )
default

◆ SpinLock() [3/4]

SpinLock::SpinLock ( const SpinLock )
delete

◆ SpinLock() [4/4]

SpinLock::SpinLock ( SpinLock &&  )
default

◆ ~SpinLock()

SpinLock::~SpinLock ( )
default

Member Function Documentation

◆ IsLockedByCurrentCore()

__always_inline auto SpinLock::IsLockedByCurrentCore ( ) -> bool
inlineprotected

检查当前 core 是否获得此锁

Returns
true 是
false 否

Definition at line 115 of file spinlock.hpp.

115 {
116 return locked_.test(std::memory_order_acquire) &&
117 (core_id_.load(std::memory_order_acquire) ==
119 }
std::atomic_flag locked_
是否 Lock
Definition spinlock.hpp:104
std::atomic< size_t > core_id_
获得此锁的 core_id
Definition spinlock.hpp:106
auto GetCurrentCoreId() -> size_t
Definition cpu_io.h:26
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Lock()

__always_inline auto SpinLock::Lock ( ) -> Expected<void>
inline

获得锁

Returns
Expected<void> 成功返回空值,失败返回错误

Definition at line 36 of file spinlock.hpp.

36 {
37 auto intr_enable = cpu_io::GetInterruptStatus();
39
40 // 先尝试获取锁
41 while (locked_.test_and_set(std::memory_order_acquire)) {
42 // 在等待时检查是否是当前核心持有锁(递归锁检测)
43 if (core_id_.load(std::memory_order_acquire) ==
45 // 递归锁定,恢复中断状态并返回失败
46 if (intr_enable) {
48 }
49 // klog::Err("spinlock {}: {} recursive lock detected.",
50 // cpu_io::GetCurrentCoreId(), name);
51 return std::unexpected(Error{ErrorCode::kSpinLockRecursiveLock});
52 }
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 }
bool saved_intr_enable_
保存的中断状态
Definition spinlock.hpp:108
@ kSpinLockRecursiveLock
void DisableInterrupt()
Definition cpu_io.h:41
bool GetInterruptStatus()
Definition cpu_io.h:48
void Pause()
Definition cpu_io.h:20
void EnableInterrupt()
Definition cpu_io.h:34
错误类型,用于 std::expected
Definition expected.hpp:343
Here is the call graph for this function:

◆ operator=() [1/2]

auto SpinLock::operator= ( const SpinLock ) -> SpinLock &=delete
delete

◆ operator=() [2/2]

auto SpinLock::operator= ( SpinLock &&  ) -> SpinLock &=default
default

◆ UnLock()

__always_inline auto SpinLock::UnLock ( ) -> Expected<void>
inline

释放锁

Returns
Expected<void> 成功返回空值,失败返回错误

Definition at line 66 of file spinlock.hpp.

66 {
67 if (!IsLockedByCurrentCore()) {
68 // klog::Err("spinlock {}: {} unlock by non-owner detected.",
69 // cpu_io::GetCurrentCoreId(), name);
70 return std::unexpected(Error{ErrorCode::kSpinLockNotOwned});
71 }
72
73 // 先重置 core_id_,再释放锁
74 core_id_.store(std::numeric_limits<size_t>::max(),
75 std::memory_order_release);
76 locked_.clear(std::memory_order_release);
77
80 }
81 return {};
82 }
__always_inline auto IsLockedByCurrentCore() -> bool
检查当前 core 是否获得此锁
Definition spinlock.hpp:115
@ kSpinLockNotOwned
Here is the call graph for this function:

Member Data Documentation

◆ core_id_

std::atomic<size_t> SpinLock::core_id_ {std::numeric_limits<size_t>::max()}
protected

获得此锁的 core_id

Definition at line 106 of file spinlock.hpp.

106{std::numeric_limits<size_t>::max()};

◆ locked_

std::atomic_flag SpinLock::locked_ {ATOMIC_FLAG_INIT}
protected

是否 Lock

Definition at line 104 of file spinlock.hpp.

104{ATOMIC_FLAG_INIT};

◆ name

const char* SpinLock::name {"unnamed"}

自旋锁名称

Definition at line 30 of file spinlock.hpp.

30{"unnamed"};

◆ saved_intr_enable_

bool SpinLock::saved_intr_enable_ {false}
protected

保存的中断状态

Definition at line 108 of file spinlock.hpp.

108{false};

The documentation for this class was generated from the following file: