libxr 1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::Mutex Class Reference

互斥锁类,提供线程同步机制 (Mutex class providing thread synchronization mechanisms). More...

#include <mutex.hpp>

Data Structures

class  LockGuard
 互斥锁的 RAII 机制封装 (RAII-style mechanism for automatic mutex management). More...
 
class  LockGuardInCallback
 回调(ISR)上下文中的互斥锁管理类 (Lock management class for ISR context). More...
 

Public Member Functions

 Mutex ()
 构造函数,初始化互斥锁 (Constructor to initialize the mutex).
 
 ~Mutex ()
 析构函数,销毁互斥锁 (Destructor to destroy the mutex).
 
ErrorCode Lock ()
 加锁,如果锁已被占用,则阻塞等待 (Lock the mutex, blocking if it is already locked).
 
ErrorCode TryLock ()
 尝试加锁,如果锁已被占用,则立即返回失败 (Attempt to lock the mutex, returning immediately if already locked).
 
void Unlock ()
 解锁互斥锁 (Unlock the mutex).
 
ErrorCode TryLockInCallback (bool in_isr)
 在回调(ISR)中尝试加锁 (Attempt to lock the mutex inside an interrupt service routine (ISR)).
 
void UnlockFromCallback (bool in_isr)
 在回调(ISR)中解锁 (Unlock the mutex inside an interrupt service routine (ISR)).
 

Private Attributes

libxr_mutex_handle mutex_handle_
 互斥锁句柄 (Handle for the mutex).
 

Detailed Description

互斥锁类,提供线程同步机制 (Mutex class providing thread synchronization mechanisms).

This class implements a mutex for thread-safe operations, supporting locking, unlocking, and special handling for interrupt service routines (ISR). 该类实现了一个互斥锁,用于确保多线程环境下的线程安全,支持加锁、解锁,并对中断服务程序(ISR)进行特殊处理。

Definition at line 17 of file mutex.hpp.

Constructor & Destructor Documentation

◆ Mutex()

Mutex::Mutex ( )

构造函数,初始化互斥锁 (Constructor to initialize the mutex).

Definition at line 9 of file mutex.cpp.

libxr_mutex_handle mutex_handle_
互斥锁句柄 (Handle for the mutex).
Definition mutex.hpp:164
void Unlock()
解锁互斥锁 (Unlock the mutex).
Definition mutex.cpp:27
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值

◆ ~Mutex()

Mutex::~Mutex ( )

析构函数,销毁互斥锁 (Destructor to destroy the mutex).

Definition at line 11 of file mutex.cpp.

Member Function Documentation

◆ Lock()

ErrorCode Mutex::Lock ( )

加锁,如果锁已被占用,则阻塞等待 (Lock the mutex, blocking if it is already locked).

Returns
操作结果 (Operation result):
  • ErrorCode::OK 表示成功 (ErrorCode::OK on success).

Definition at line 13 of file mutex.cpp.

13 {
15 return ErrorCode::BUSY;
16 }
17 return ErrorCode::OK;
18}

◆ TryLock()

ErrorCode Mutex::TryLock ( )

尝试加锁,如果锁已被占用,则立即返回失败 (Attempt to lock the mutex, returning immediately if already locked).

Returns
操作结果 (Operation result):
  • ErrorCode::OK 表示成功 (ErrorCode::OK on success).
  • ErrorCode::BUSY 表示锁已被占用 (ErrorCode::BUSY if the mutex is already locked).

Definition at line 20 of file mutex.cpp.

20 {
22 return ErrorCode::BUSY;
23 }
24 return ErrorCode::OK;
25}

◆ TryLockInCallback()

ErrorCode Mutex::TryLockInCallback ( bool  in_isr)

在回调(ISR)中尝试加锁 (Attempt to lock the mutex inside an interrupt service routine (ISR)).

Parameters
in_isr指示当前是否处于中断上下文 (Indicates whether the function is called inside an ISR).
Returns
操作结果 (Operation result):
  • ErrorCode::OK 表示成功 (ErrorCode::OK on success).
  • ErrorCode::BUSY 表示锁已被占用 (ErrorCode::BUSY if the mutex is already locked).

Definition at line 29 of file mutex.cpp.

29 {
30 if (in_isr) {
34
35 if (ans == pdPASS) {
37 portYIELD(); // NOLINT
38 }
39
40 return ErrorCode::OK;
41 } else {
42 return ErrorCode::BUSY;
43 }
44 } else {
45 return TryLock();
46 }
47}
ErrorCode TryLock()
尝试加锁,如果锁已被占用,则立即返回失败 (Attempt to lock the mutex, returning immediately if already locked).
Definition mutex.cpp:20

◆ Unlock()

void Mutex::Unlock ( )

解锁互斥锁 (Unlock the mutex).

Definition at line 27 of file mutex.cpp.

◆ UnlockFromCallback()

void Mutex::UnlockFromCallback ( bool  in_isr)

在回调(ISR)中解锁 (Unlock the mutex inside an interrupt service routine (ISR)).

Parameters
in_isr指示当前是否处于中断上下文 (Indicates whether the function is called inside an ISR).

Definition at line 49 of file mutex.cpp.

Field Documentation

◆ mutex_handle_

libxr_mutex_handle LibXR::Mutex::mutex_handle_
private

互斥锁句柄 (Handle for the mutex).

Definition at line 164 of file mutex.hpp.


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