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...
 

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).
 

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 10 of file mutex.cpp.

10: mutex_handle_(PTHREAD_MUTEX_INITIALIZER) {}
libxr_mutex_handle mutex_handle_
互斥锁句柄 (Handle for the mutex).
Definition mutex.hpp:85

◆ ~Mutex()

Mutex::~Mutex ( )

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

Definition at line 12 of file mutex.cpp.

12{ pthread_mutex_destroy(&mutex_handle_); }

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 14 of file mutex.cpp.

14 {
15 if (pthread_mutex_lock(&mutex_handle_) != 0) {
16 return ErrorCode::BUSY;
17 }
18 return ErrorCode::OK;
19}

◆ TryLock()

ErrorCode Mutex::TryLock ( )
nodiscard

尝试加锁,如果锁已被占用,则立即返回失败 (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 21 of file mutex.cpp.

21 {
22 if (pthread_mutex_trylock(&mutex_handle_) != 0) {
23 return ErrorCode::BUSY;
24 }
25 return ErrorCode::OK;
26}

◆ Unlock()

void Mutex::Unlock ( )

解锁互斥锁 (Unlock the mutex).

Definition at line 28 of file mutex.cpp.

28{ pthread_mutex_unlock(&mutex_handle_); }

Field Documentation

◆ mutex_handle_

libxr_mutex_handle LibXR::Mutex::mutex_handle_
private

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

Definition at line 85 of file mutex.hpp.


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