libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::LockQueue< Data > Class Template Reference

线程安全的锁队列类,提供同步和异步操作支持 Thread-safe lock queue class with synchronous and asynchronous operation support More...

#include <lock_queue.hpp>

Collaboration diagram for LibXR::LockQueue< Data >:
[legend]

Public Member Functions

 LockQueue (size_t length)
 构造函数,初始化队列 Constructor to initialize the queue
 
 ~LockQueue ()
 析构函数,释放资源 Destructor to release resources
 
ErrorCode Push (const Data &data)
 向队列中推送数据 Pushes data into the queue
 
ErrorCode Pop (Data &data, uint32_t timeout)
 从队列中弹出数据(带超时) Pops data from the queue with timeout
 
ErrorCode Pop ()
 无参数弹出数据 Pops data from the queue without storing it
 
ErrorCode Pop (uint32_t timeout)
 带超时的弹出数据 Pops data from the queue with timeout
 
ErrorCode PushFromCallback (const Data &data, bool in_isr)
 从回调函数中推送数据 Pushes data into the queue from a callback function
 
size_t Size ()
 获取队列中的数据项数量 Gets the number of items in the queue
 
size_t EmptySize ()
 获取队列的剩余容量 Gets the remaining capacity of the queue
 

Private Attributes

Queue< Data > queue_handle_
 底层队列对象 Underlying queue object
 
Mutex mutex_
 互斥锁 Mutex for thread safety
 
Semaphore semaphore_handle_
 信号量 Semaphore for synchronization
 

Detailed Description

template<typename Data>
class LibXR::LockQueue< Data >

线程安全的锁队列类,提供同步和异步操作支持 Thread-safe lock queue class with synchronous and asynchronous operation support

Template Parameters
Data队列存储的数据类型 The type of data stored in the queue

Definition at line 20 of file lock_queue.hpp.

Constructor & Destructor Documentation

◆ LockQueue()

template<typename Data >
LibXR::LockQueue< Data >::LockQueue ( size_t length)
inline

构造函数,初始化队列 Constructor to initialize the queue

Parameters
length队列的最大长度 Maximum length of the queue

Definition at line 28 of file lock_queue.hpp.

28: queue_handle_(length) {}
Queue< Data > queue_handle_
底层队列对象 Underlying queue object

◆ ~LockQueue()

template<typename Data >
LibXR::LockQueue< Data >::~LockQueue ( )
inline

析构函数,释放资源 Destructor to release resources

Definition at line 34 of file lock_queue.hpp.

34{}

Member Function Documentation

◆ EmptySize()

template<typename Data >
size_t LibXR::LockQueue< Data >::EmptySize ( )
inline

获取队列的剩余容量 Gets the remaining capacity of the queue

Returns
剩余空间大小 Remaining space size

Definition at line 141 of file lock_queue.hpp.

142 {
143 mutex_.Lock();
144 auto ans = queue_handle_.EmptySize();
145 mutex_.Unlock();
146 return ans;
147 }
Mutex mutex_
互斥锁 Mutex for thread safety
ErrorCode Lock()
加锁,如果锁已被占用,则阻塞等待 (Lock the mutex, blocking if it is already locked).
Definition mutex.cpp:14
void Unlock()
解锁互斥锁 (Unlock the mutex).
Definition mutex.cpp:28

◆ Pop() [1/3]

template<typename Data >
ErrorCode LibXR::LockQueue< Data >::Pop ( )
inline

无参数弹出数据 Pops data from the queue without storing it

Returns
操作结果 ErrorCode indicating success or failure

Definition at line 81 of file lock_queue.hpp.

82 {
83 mutex_.Lock();
84 auto ans = queue_handle_.Pop();
85 mutex_.Unlock();
86 return ans;
87 }

◆ Pop() [2/3]

template<typename Data >
ErrorCode LibXR::LockQueue< Data >::Pop ( Data & data,
uint32_t timeout )
inline

从队列中弹出数据(带超时) Pops data from the queue with timeout

Parameters
data存储弹出数据的变量 Variable to store the popped data
timeout超时时间(毫秒) Timeout in milliseconds
Returns
操作结果 ErrorCode indicating success or failure

Definition at line 61 of file lock_queue.hpp.

62 {
63 if (semaphore_handle_.Wait(timeout) == ErrorCode::OK)
64 {
65 mutex_.Lock();
66 auto ans = queue_handle_.Pop(data);
67 mutex_.Unlock();
68 return ans;
69 }
70 else
71 {
72 return ErrorCode::TIMEOUT;
73 }
74 }
Semaphore semaphore_handle_
信号量 Semaphore for synchronization
ErrorCode Wait(uint32_t timeout=UINT32_MAX)
等待(减少)信号量 Waits (decrements) the semaphore
Definition semaphore.cpp:25

◆ Pop() [3/3]

template<typename Data >
ErrorCode LibXR::LockQueue< Data >::Pop ( uint32_t timeout)
inline

带超时的弹出数据 Pops data from the queue with timeout

Parameters
timeout超时时间(毫秒) Timeout in milliseconds
Returns
操作结果 ErrorCode indicating success or failure

Definition at line 95 of file lock_queue.hpp.

96 {
97 if (semaphore_handle_.Wait(timeout) == ErrorCode::OK)
98 {
99 mutex_.Lock();
100 auto ans = queue_handle_.Pop();
101 mutex_.Unlock();
102 return ans;
103 }
104 else
105 {
106 return ErrorCode::TIMEOUT;
107 }
108 }

◆ Push()

template<typename Data >
ErrorCode LibXR::LockQueue< Data >::Push ( const Data & data)
inline

向队列中推送数据 Pushes data into the queue

Parameters
data要推送的数据 The data to be pushed
Returns
操作结果 ErrorCode indicating success or failure

Definition at line 42 of file lock_queue.hpp.

43 {
44 mutex_.Lock();
45 auto ans = queue_handle_.Push(data);
46 if (ans == ErrorCode::OK)
47 {
49 }
50 mutex_.Unlock();
51 return ans;
52 }
void Post()
释放(增加)信号量 Releases (increments) the semaphore
Definition semaphore.cpp:23

◆ PushFromCallback()

template<typename Data >
ErrorCode LibXR::LockQueue< Data >::PushFromCallback ( const Data & data,
bool in_isr )
inline

从回调函数中推送数据 Pushes data into the queue from a callback function

Parameters
data要推送的数据 The data to be pushed
in_isr是否在中断上下文中 Whether the function is called from an ISR
Returns
操作结果 ErrorCode indicating success or failure

Definition at line 117 of file lock_queue.hpp.

118 {
119 UNUSED(in_isr);
120 return Push(data);
121 }
ErrorCode Push(const Data &data)
向队列中推送数据 Pushes data into the queue

◆ Size()

template<typename Data >
size_t LibXR::LockQueue< Data >::Size ( )
inline

获取队列中的数据项数量 Gets the number of items in the queue

Returns
当前队列大小 Current queue size

Definition at line 128 of file lock_queue.hpp.

129 {
130 mutex_.Lock();
131 auto ans = queue_handle_.Size();
132 mutex_.Unlock();
133 return ans;
134 }

Field Documentation

◆ mutex_

template<typename Data >
Mutex LibXR::LockQueue< Data >::mutex_
private

互斥锁 Mutex for thread safety

Definition at line 151 of file lock_queue.hpp.

◆ queue_handle_

template<typename Data >
Queue<Data> LibXR::LockQueue< Data >::queue_handle_
private

底层队列对象 Underlying queue object

Definition at line 150 of file lock_queue.hpp.

◆ semaphore_handle_

template<typename Data >
Semaphore LibXR::LockQueue< Data >::semaphore_handle_
private

信号量 Semaphore for synchronization

Definition at line 152 of file lock_queue.hpp.


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