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

信号量类,实现线程同步机制 Semaphore class implementing thread synchronization More...

#include <semaphore.hpp>

Public Member Functions

 Semaphore (uint32_t init_count=0)
 构造一个信号量对象 Constructs a semaphore object
 
 ~Semaphore ()
 析构信号量对象,释放资源 Destroys the semaphore object and releases resources
 
void Post ()
 释放(增加)信号量 Releases (increments) the semaphore
 
void PostFromCallback (bool in_isr)
 从中断回调中释放(增加)信号量 Releases (increments) the semaphore from an ISR (Interrupt Service Routine)
 
ErrorCode Wait (uint32_t timeout=UINT32_MAX)
 等待(减少)信号量 Waits (decrements) the semaphore
 
size_t Value ()
 获取当前信号量的值 Gets the current value of the semaphore
 

Private Attributes

libxr_semaphore_handle semaphore_handle_
 信号量句柄 Semaphore handle
 

Detailed Description

信号量类,实现线程同步机制 Semaphore class implementing thread synchronization

信号量(Semaphore)用于线程间同步,支持普通等待和中断回调模式。 它允许多个线程协调访问共享资源,并支持从 ISR(中断服务例程)中安全调用。

The semaphore is used for thread synchronization, supporting both regular wait operations and ISR (Interrupt Service Routine) callback mode. It enables multiple threads to coordinate access to shared resources and provides safe operations from an ISR.

Definition at line 22 of file semaphore.hpp.

Constructor & Destructor Documentation

◆ Semaphore()

Semaphore::Semaphore ( uint32_t  init_count = 0)

构造一个信号量对象 Constructs a semaphore object

Parameters
init_count信号量的初始值 Initial count of the semaphore

信号量的初始值 init_count 定义了可用资源的初始数量。 该信号量可用于控制多个线程对共享资源的访问。

The initial count init_count defines the initial number of available resources. This semaphore can be used to control access to shared resources by multiple threads.

Definition at line 8 of file semaphore.cpp.

libxr_semaphore_handle semaphore_handle_
信号量句柄 Semaphore handle
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值

◆ ~Semaphore()

Semaphore::~Semaphore ( )

析构信号量对象,释放资源 Destroys the semaphore object and releases resources

Definition at line 11 of file semaphore.cpp.

Member Function Documentation

◆ Post()

void Semaphore::Post ( )

释放(增加)信号量 Releases (increments) the semaphore

该方法增加信号量的值,表示增加了一个可用资源。 若有等待的线程,该方法可能会解除其中一个线程的阻塞状态。

This method increments the semaphore count, indicating that a resource has been made available. If there are waiting threads, one of them may be unblocked.

Definition at line 13 of file semaphore.cpp.

◆ PostFromCallback()

void Semaphore::PostFromCallback ( bool  in_isr)

从中断回调中释放(增加)信号量 Releases (increments) the semaphore from an ISR (Interrupt Service Routine)

Parameters
in_isr是否在 ISR(中断服务例程)中调用 Whether it is called from an ISR

该方法在中断服务程序(ISR)中使用,确保安全地增加信号量。 若 in_isrtrue,则执行适用于中断环境的信号量释放操作。

This method is used in an ISR (Interrupt Service Routine) to safely increment the semaphore. If in_isr is true, an ISR-safe semaphore release operation is performed.

Definition at line 23 of file semaphore.cpp.

23 {
24 if (in_isr) {
28 portYIELD(); // NOLINT
29 }
30 } else {
31 Post();
32 }
33}
void Post()
释放(增加)信号量 Releases (increments) the semaphore
Definition semaphore.cpp:13

◆ Value()

size_t Semaphore::Value ( )

获取当前信号量的值 Gets the current value of the semaphore

Returns
当前信号量值 The current semaphore value

该方法返回信号量当前的计数值,表示可用资源的数量。

This method returns the current semaphore count, representing the number of available resources.

Definition at line 35 of file semaphore.cpp.

35 {
37 return value;
38}

◆ Wait()

ErrorCode Semaphore::Wait ( uint32_t  timeout = UINT32_MAX)

等待(减少)信号量 Waits (decrements) the semaphore

Parameters
timeout超时时间(默认无限等待) Timeout period (default is infinite wait)
Returns
操作结果 ErrorCode indicating success or timeout

该方法尝试减少信号量的值,表示线程正在占用一个资源。 如果信号量的值为 0,调用线程将进入阻塞状态,直到信号量可用或超时。

This method attempts to decrement the semaphore count, indicating that a resource is being used by the calling thread. If the semaphore count is 0, the calling thread is blocked until the semaphore becomes available or the timeout expires.

Definition at line 15 of file semaphore.cpp.

15 {
16 if (xSemaphoreTake(semaphore_handle_, timeout) == pdTRUE) {
17 return ErrorCode::OK;
18 } else {
19 return ErrorCode::TIMEOUT;
20 }
21}

Field Documentation

◆ semaphore_handle_

libxr_semaphore_handle LibXR::Semaphore::semaphore_handle_
private

信号量句柄 Semaphore handle

Definition at line 104 of file semaphore.hpp.


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