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 163 of file lock_queue.hpp.

164 {
165 if (mutex_.Lock() != ErrorCode::OK)
166 {
167 return 0;
168 }
169 auto ans = queue_handle_.EmptySize();
170 mutex_.Unlock();
171 return ans;
172 }
Mutex mutex_
互斥锁 Mutex for thread safety
ErrorCode Lock()
加锁,如果锁已被占用,则阻塞等待 (Lock the mutex, blocking if it is already locked).
Definition mutex.cpp:16
void Unlock()
解锁互斥锁 (Unlock the mutex).
Definition mutex.cpp:40
@ OK
操作成功 | Operation successful

◆ 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 91 of file lock_queue.hpp.

92 {
93 const ErrorCode lock_ans = mutex_.Lock();
94 if (lock_ans != ErrorCode::OK)
95 {
96 return lock_ans;
97 }
98 auto ans = queue_handle_.Pop();
99 mutex_.Unlock();
100 return ans;
101 }
ErrorCode
定义错误码枚举
Definition libxr_def.hpp:64

◆ 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 66 of file lock_queue.hpp.

67 {
68 const ErrorCode wait_ans = semaphore_handle_.Wait(timeout);
69 if (wait_ans == ErrorCode::OK)
70 {
71 const ErrorCode lock_ans = mutex_.Lock();
72 if (lock_ans != ErrorCode::OK)
73 {
75 return lock_ans;
76 }
77
78 auto ans = queue_handle_.Pop(data);
79 mutex_.Unlock();
80 return ans;
81 }
82
83 return wait_ans;
84 }
Semaphore semaphore_handle_
信号量 Semaphore for synchronization
void Post()
释放(增加)信号量 Releases (increments) the semaphore
Definition semaphore.cpp:44
ErrorCode Wait(uint32_t timeout=UINT32_MAX)
等待(减少)信号量 Waits (decrements) the semaphore
Definition semaphore.cpp:53

◆ 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 109 of file lock_queue.hpp.

110 {
111 const ErrorCode wait_ans = semaphore_handle_.Wait(timeout);
112 if (wait_ans == ErrorCode::OK)
113 {
114 const ErrorCode lock_ans = mutex_.Lock();
115 if (lock_ans != ErrorCode::OK)
116 {
118 return lock_ans;
119 }
120
121 auto ans = queue_handle_.Pop();
122 mutex_.Unlock();
123 return ans;
124 }
125
126 return wait_ans;
127 }

◆ 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 const ErrorCode lock_ans = mutex_.Lock();
45 if (lock_ans != ErrorCode::OK)
46 {
47 return lock_ans;
48 }
49
50 auto ans = queue_handle_.Push(data);
51 if (ans == ErrorCode::OK)
52 {
54 }
55 mutex_.Unlock();
56 return ans;
57 }

◆ 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 136 of file lock_queue.hpp.

137 {
138 UNUSED(in_isr);
139 return Push(data);
140 }
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 147 of file lock_queue.hpp.

148 {
149 if (mutex_.Lock() != ErrorCode::OK)
150 {
151 return 0;
152 }
153 auto ans = queue_handle_.Size();
154 mutex_.Unlock();
155 return ans;
156 }

Field Documentation

◆ mutex_

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

互斥锁 Mutex for thread safety

Definition at line 176 of file lock_queue.hpp.

◆ queue_handle_

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

底层队列对象 Underlying queue object

Definition at line 175 of file lock_queue.hpp.

◆ semaphore_handle_

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

信号量 Semaphore for synchronization

Definition at line 177 of file lock_queue.hpp.


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