pub struct Mutex<T: ?Sized> { /* private fields */ }Expand description
互斥锁对象结构体
Implementations§
source§impl<T: ?Sized> Mutex<T>
impl<T: ?Sized> Mutex<T>
sourcepub fn lock(&self) -> Result<MutexGuard<'_, T>, MutexError>
pub fn lock(&self) -> Result<MutexGuard<'_, T>, MutexError>
获取互斥锁,若线程无法获取互斥锁,就阻塞等待,直到能获得锁为止
- 若成功获取互斥锁,将返回 RAII Guard :
MutexGuard,用于提供 Scoped Lock 机制。MutexGuard中包含Mutex的引用, 当MutexGuard生命周期结束时,会在drop()方法中自动解锁互斥锁。
- 若失败,将返回错误码
MutexError。
§上下文
- 线程
§错误码
MutexError::NotInit互斥锁未被初始化MutexError::Interrupt等待被中断MutexError::NotThreadContext不在线程上下文中MutexError::DisPmpt抢占被关闭MutexError::DisBh中断底半部被关闭MutexError::DisIrq中断被关闭
§示例
use xwrust::xwos::lock::mtx::*;
static GLOBAL_MUTEX: Mutex<u32> = Mutex::new(0);
pub fn xwrust_example_mutex() {
// ...省略...
GLOBAL_MUTEX.init();
match GLOBAL_MUTEX.lock() {
Ok(mut guard) => { // 上锁成功
*guard = 1; // 访问共享变量
} // guard 生命周期结束,自动解锁
Err(e) => {
// 上锁失败
}
}
}sourcepub fn trylock(&self) -> Result<MutexGuard<'_, T>, MutexError>
pub fn trylock(&self) -> Result<MutexGuard<'_, T>, MutexError>
尝试获取互斥锁,若线程无法获取互斥锁,立即返回错误
- 若成功获取互斥锁,将返回 RAII Guard :
MutexGuard,用于提供 Scoped Lock 机制。MutexGuard中包含Mutex的引用, 当MutexGuard生命周期结束时,会在drop()方法中自动解锁互斥锁。
- 若失败,将返回错误码
MutexError。
§上下文
- 线程
§错误码
MutexError::NotInit互斥锁未被初始化MutexError::WouldBlock尝试获取锁失败MutexError::NotThreadContext不在线程上下文中
§示例
use xwrust::xwos::lock::mtx::*;
static GLOBAL_MUTEX: Mutex<u32> = Mutex::new(0);
pub fn xwrust_example_mutex() {
// ...省略...
GLOBAL_MUTEX.init();
match GLOBAL_MUTEX.trylock() {
Ok(mut guard) => { // 上锁成功
*guard = 1; // 访问共享变量
} // guard 生命周期结束,自动解锁
Err(e) => {
// 上锁失败
}
}
}sourcepub fn lock_to(&self, to: XwTm) -> Result<MutexGuard<'_, T>, MutexError>
pub fn lock_to(&self, to: XwTm) -> Result<MutexGuard<'_, T>, MutexError>
获取互斥锁,若线程无法获取互斥锁,就限时阻塞等待
- 若成功获取互斥锁,将返回 RAII Guard :
MutexGuard,用于提供 Scoped Lock 机制。MutexGuard中包含Mutex的引用, 当MutexGuard生命周期结束时,会在drop()方法中自动解锁互斥锁。
- 若失败,将返回错误码
MutexError。
§参数说明
- to: 期望唤醒的时间点
§上下文
- 线程
§错误码
MutexError::NotInit互斥锁未被初始化MutexError::Interrupt等待被中断MutexError::Timedout等待超时MutexError::NotThreadContext不在线程上下文中MutexError::DisPmpt抢占被关闭MutexError::DisBh中断底半部被关闭MutexError::DisIrq中断被关闭
§示例
use xwrust::xwtm;
use xwrust::xwos::lock::mtx::*;
static GLOBAL_MUTEX: Mutex<u32> = Mutex::new(0);
pub fn xwrust_example_mutex() {
// ...省略...
GLOBAL_MUTEX.init();
match GLOBAL_MUTEX.lock_to(xwtm::ft(xwtm::s(10))) { // 最多等待10s
Ok(mut guard) => { // 上锁成功
*guard = 1; // 访问共享变量
} // guard 生命周期结束,自动解锁
Err(e) => {
// 上锁失败
}
}
}sourcepub fn lock_unintr(&self) -> Result<MutexGuard<'_, T>, MutexError>
pub fn lock_unintr(&self) -> Result<MutexGuard<'_, T>, MutexError>
获取互斥锁,若线程无法获取互斥锁,就阻塞等待,且不可中断,直到能获得锁为止
- 若成功获取互斥锁,将返回 RAII Guard :
MutexGuard,用于提供 Scoped Lock 机制。MutexGuard中包含Mutex的引用, 当MutexGuard生命周期结束时,会在drop()方法中自动解锁互斥锁。
- 若失败,将返回错误码
MutexError。
§上下文
- 线程
§错误码
MutexError::NotInit互斥锁未被初始化MutexError::NotThreadContext不在线程上下文中MutexError::DisPmpt抢占被关闭MutexError::DisBh中断底半部被关闭MutexError::DisIrq中断被关闭
§示例
use xwrust::xwos::lock::mtx::*;
static GLOBAL_MUTEX: Mutex<u32> = Mutex::new(0);
pub fn xwrust_example_mutex() {
// ...省略...
GLOBAL_MUTEX.init();
match GLOBAL_MUTEX.lock_unintr() {
Ok(mut guard) => { // 上锁成功
*guard = 1; // 访问共享变量
} // guard 生命周期结束,自动解锁
Err(e) => {
// 上锁失败
}
}
}sourcepub fn unlock(guard: MutexGuard<'_, T>)
pub fn unlock(guard: MutexGuard<'_, T>)
释放 MutexGuard,并在 drop() 方法中解锁互斥锁
§上下文
- 线程
§示例
use xwrust::xwos::lock::mtx::*;
static GLOBAL_MUTEX: Mutex<u32> = Mutex::new(0);
pub fn xwrust_example_mutex() {
// ...省略...
GLOBAL_MUTEX.init();
match GLOBAL_MUTEX.lock() {
Ok(mut guard) => { // 上锁成功
*guard = 1; // 访问共享变量
Mutex::unlock(guard); // 主动解锁
}
Err(e) => {
// 上锁失败
}
}
}