libxr
Embedded C++ framework for bare-metal, RTOS and Linux
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
 

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 35 of file semaphore.cpp.

36 : semaphore_handle_(new libxr_linux_futex_semaphore)
37{
38 semaphore_handle_->count.store(init_count, std::memory_order_release);
39}

◆ ~Semaphore()

Semaphore::~Semaphore ( )

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

Definition at line 41 of file semaphore.cpp.

41{ delete semaphore_handle_; }

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 43 of file semaphore.cpp.

44{
45 (void)semaphore_handle_->count.fetch_add(1, std::memory_order_release);
46 (void)FutexWake(&semaphore_handle_->count, 1);
47}

◆ 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_isr 为 true,则执行适用于中断环境的信号量释放操作。

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 96 of file semaphore.cpp.

97{
98 UNUSED(in_isr);
99 Post();
100}
void Post()
释放(增加)信号量 Releases (increments) the semaphore
Definition semaphore.cpp:43

◆ 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 102 of file semaphore.cpp.

103{
104 return semaphore_handle_->count.load(std::memory_order_acquire);
105}

◆ Wait()

ErrorCode Semaphore::Wait ( uint32_t timeout = UINT32_MAX)

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

Parameters
timeout超时时间(毫秒),默认 UINT32_MAX / Timeout in milliseconds, default UINT32_MAX
Returns
获得信号量返回 ErrorCode::OK,超时返回 ErrorCode::TIMEOUT; ThreadX 后端出错时返回 ErrorCode::FAILED / ErrorCode::OK when acquired, ErrorCode::TIMEOUT on timeout; ErrorCode::FAILED on a ThreadX error

该方法尝试减少信号量的值,表示线程正在占用一个资源。 如果信号量的值为 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 49 of file semaphore.cpp.

50{
51 const uint64_t deadline_ms =
52 (timeout == UINT32_MAX) ? UINT64_MAX : (MonotonicTime::NowMilliseconds() + timeout);
53
54 while (true)
55 {
56 uint32_t current = semaphore_handle_->count.load(std::memory_order_acquire);
57
58 while (current > 0)
59 {
60 if (semaphore_handle_->count.compare_exchange_weak(current, current - 1U,
61 std::memory_order_acq_rel,
62 std::memory_order_relaxed))
63 {
64 return ErrorCode::OK;
65 }
66 }
67
68 timespec ts = {};
69 timespec* ts_ptr = nullptr;
70 if (timeout != UINT32_MAX)
71 {
72 const uint32_t remaining_ms = MonotonicTime::RemainingMilliseconds(deadline_ms);
73 if (remaining_ms == 0)
74 {
75 return ErrorCode::TIMEOUT;
76 }
77 ts = MonotonicTime::RelativeFromMilliseconds(remaining_ms);
78 ts_ptr = &ts;
79 }
80
81 const int ans = FutexWait(&semaphore_handle_->count, 0, ts_ptr);
82 if (ans == 0 || errno == EAGAIN || errno == EINTR)
83 {
84 continue;
85 }
86
87 if (errno == ETIMEDOUT)
88 {
89 return ErrorCode::TIMEOUT;
90 }
91
92 return ErrorCode::FAILED;
93 }
94}
@ TIMEOUT
超时 / Timeout
@ FAILED
操作失败 / Operation failed
@ OK
操作成功 / Operation successful

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