xwrust::xwos::lock::mtx

Struct Mutex

source
pub struct Mutex<T: ?Sized> { /* private fields */ }
Expand description

互斥锁对象结构体

Implementations§

source§

impl<T> Mutex<T>

source

pub const fn new(t: T) -> Self

新建互斥锁对象

此方法是编译期方法,可用于新建 static 约束的全局变量。

§示例
use xwrust::xwos::lock::mtx::*;

static GLOBAL_MUTEX: Mutex<u32> = Mutex::new(0);
source§

impl<T: ?Sized> Mutex<T>

source

pub fn init(&self)

初始化互斥锁对象

互斥锁对象必须调用此方法一次,方可正常使用。

§上下文
  • 任意
§示例
use xwrust::xwos::lock::mtx::*;

static GLOBAL_MUTEX: Mutex<u32> = Mutex::new(0);

pub fn xwrust_example_mutex() {
    // ...省略...
    GLOBAL_MUTEX.init();
    // 从此处开始 GLOBAL_MUTEX 可正常使用
}
source

pub fn lock(&self) -> Result<MutexGuard<'_, T>, MutexError>

获取互斥锁,若线程无法获取互斥锁,就阻塞等待,直到能获得锁为止

  • 若成功获取互斥锁,将返回 RAII GuardMutexGuard ,用于提供 Scoped Lock 机制。
  • 若失败,将返回错误码 MutexError
§上下文
  • 线程
§错误码
§示例
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) => {
            // 上锁失败
        }
    }
}
source

pub fn trylock(&self) -> Result<MutexGuard<'_, T>, MutexError>

尝试获取互斥锁,若线程无法获取互斥锁,立即返回错误

  • 若成功获取互斥锁,将返回 RAII GuardMutexGuard ,用于提供 Scoped Lock 机制。
  • 若失败,将返回错误码 MutexError
§上下文
  • 线程
§错误码
§示例
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) => {
            // 上锁失败
        }
    }
}
source

pub fn lock_to(&self, to: XwTm) -> Result<MutexGuard<'_, T>, MutexError>

获取互斥锁,若线程无法获取互斥锁,就限时阻塞等待

  • 若成功获取互斥锁,将返回 RAII GuardMutexGuard ,用于提供 Scoped Lock 机制。
  • 若失败,将返回错误码 MutexError
§参数说明
  • to: 期望唤醒的时间点
§上下文
  • 线程
§错误码
§示例
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) => {
            // 上锁失败
        }
    }
}
source

pub fn lock_unintr(&self) -> Result<MutexGuard<'_, T>, MutexError>

获取互斥锁,若线程无法获取互斥锁,就阻塞等待,且不可中断,直到能获得锁为止

  • 若成功获取互斥锁,将返回 RAII GuardMutexGuard ,用于提供 Scoped Lock 机制。
  • 若失败,将返回错误码 MutexError
§上下文
  • 线程
§错误码
§示例
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) => {
            // 上锁失败
        }
    }
}
source

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) => {
            // 上锁失败
        }
    }
}

Trait Implementations§

source§

impl<T: ?Sized + Default> Default for Mutex<T>

source§

fn default() -> Mutex<T>

Returns the “default value” for a type. Read more
source§

impl<T: ?Sized> Drop for Mutex<T>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<T> From<T> for Mutex<T>

source§

fn from(t: T) -> Self

从数据新建互斥锁对象

此方法会将数据所有权转移到互斥锁对象的内部

等价于 Mutex::new

source§

impl<T: ?Sized + Send> Send for Mutex<T>

source§

impl<T: ?Sized + Send> Sync for Mutex<T>

Auto Trait Implementations§

§

impl<T> !Freeze for Mutex<T>

§

impl<T> !RefUnwindSafe for Mutex<T>

§

impl<T> Unpin for Mutex<T>
where T: Unpin + ?Sized,

§

impl<T> UnwindSafe for Mutex<T>
where T: UnwindSafe + ?Sized,

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<!> for T

§

fn from(t: !) -> T

Converts to this type from the input type.
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.