libxr 1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
lock_queue.hpp
1#pragma once
2
3#include <cstdint>
4
5#include "libxr_def.hpp"
6#include "mutex.hpp"
7#include "queue.hpp"
8#include "semaphore.hpp"
9
10namespace LibXR
11{
12
19template <typename Data>
20class LockQueue
21{
22 public:
29
35
42 ErrorCode Push(const Data &data)
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 }
53
61 ErrorCode Pop(Data &data, uint32_t timeout)
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 }
75
81 ErrorCode Pop()
82 {
83 mutex_.Lock();
84 auto ans = queue_handle_.Pop();
85 mutex_.Unlock();
86 return ans;
87 }
88
95 ErrorCode PopFromCallback(bool in_isr)
96 {
97 UNUSED(in_isr);
98 return Pop();
99 }
100
107 ErrorCode Pop(uint32_t timeout)
108 {
109 if (semaphore_handle_.Wait(timeout) == ErrorCode::OK)
110 {
111 mutex_.Lock();
112 auto ans = queue_handle_.Pop();
113 mutex_.Unlock();
114 return ans;
115 }
116 else
117 {
118 return ErrorCode::TIMEOUT;
119 }
120 }
121
128 ErrorCode Overwrite(const Data &data)
129 {
130 mutex_.Lock();
131 while (semaphore_handle_.Wait(0) != ErrorCode::OK)
132 {
133 }
134 auto ans = queue_handle_.Overwrite(data);
136 mutex_.Unlock();
137 return ans;
138 }
139
147 ErrorCode PushFromCallback(const Data &data, bool in_isr)
148 {
149 UNUSED(in_isr);
150 return Push(data);
151 }
152
160 ErrorCode PopFromCallback(Data &data, bool in_isr)
161 {
162 UNUSED(in_isr);
163 return Pop(data, 0);
164 }
165
173 ErrorCode OverwriteFromCallback(const Data &data, bool in_isr)
174 {
175 UNUSED(in_isr);
176 return Overwrite(data);
177 }
178
185 ErrorCode Peek(Data &item)
186 {
187 mutex_.Lock();
188 auto ans = queue_handle_.Peek(item);
189 mutex_.Unlock();
190 return ans;
191 }
192
201 {
202 UNUSED(in_isr);
203 return Peek(item);
204 }
205
210 void Reset()
211 {
212 mutex_.Lock();
213 while (semaphore_handle_.Wait(0) == ErrorCode::OK)
214 {
215 };
216 queue_handle_.Reset();
217 mutex_.Unlock();
218 }
219
225 size_t Size()
226 {
227 mutex_.Lock();
228 auto ans = queue_handle_.Size();
229 mutex_.Unlock();
230 return ans;
231 }
232
238 size_t EmptySize()
239 {
240 mutex_.Lock();
241 auto ans = queue_handle_.EmptySize();
242 mutex_.Unlock();
243 return ans;
244 }
245
253 {
254 UNUSED(in_isr);
255 return Size();
256 }
257
265 {
266 UNUSED(in_isr);
267 return EmptySize();
268 }
269
270 private:
274};
275
276} // namespace LibXR
ErrorCode Pop(Data &data, uint32_t timeout)
从队列中弹出数据(带超时) Pops data from the queue with timeout
~LockQueue()
析构函数,释放资源 Destructor to release resources
size_t Size()
获取队列中的数据项数量 Gets the number of items in the queue
ErrorCode Pop()
无参数弹出数据 Pops data from the queue without storing it
Queue< Data > queue_handle_
底层队列对象 Underlying queue object
ErrorCode Peek(Data &item)
查看队列中的数据(不弹出) Peeks at the data in the queue without popping it
ErrorCode Push(const Data &data)
向队列中推送数据 Pushes data into the queue
void Reset()
重置队列 Resets the queue
ErrorCode Peek(Data &data)
查看队列中的数据(非阻塞) Peeks at the data in the queue (non-blocking)
ErrorCode PopFromCallback(Data &data, bool in_isr)
从回调函数中弹出数据 Pops data from the queue in a callback function
size_t SizeFromCallback(bool in_isr)
从回调函数中获取队列大小 Gets the queue size from a callback function
size_t EmptySize()
获取队列的剩余容量 Gets the remaining capacity of the queue
Mutex mutex_
互斥锁 Mutex for thread safety
ErrorCode OverwriteFromCallback(const Data &data, bool in_isr)
从回调函数中覆盖数据 Overwrites data in the queue from a callback function
Semaphore semaphore_handle_
信号量 Semaphore for synchronization
ErrorCode PopFromCallback(bool in_isr)
从回调函数中弹出数据 Pops data from the queue in a callback function
ErrorCode PeekFromCallback(Data &item, bool in_isr)
从回调函数中查看数据 Peeks at the data in the queue from a callback function
QueueHandle_t queue_handle_
FreeRTOS 队列句柄 FreeRTOS queue handle.
size_t EmptySizeFromCallback(bool in_isr)
从回调函数中获取队列的剩余容量 Gets the remaining capacity of the queue from a callback function
ErrorCode PushFromCallback(const Data &data, bool in_isr)
从回调函数中推送数据 Pushes data into the queue from a callback function
ErrorCode Pop(uint32_t timeout)
带超时的弹出数据 Pops data from the queue with timeout
LockQueue(size_t length)
构造函数,初始化队列 Constructor to initialize the queue
ErrorCode Overwrite(const Data &data)
覆盖队列中的数据 Overwrites data in the queue
互斥锁类,提供线程同步机制 (Mutex class providing thread synchronization mechanisms).
Definition mutex.hpp:18
ErrorCode Lock()
加锁,如果锁已被占用,则阻塞等待 (Lock the mutex, blocking if it is already locked).
Definition mutex.cpp:13
void Unlock()
解锁互斥锁 (Unlock the mutex).
Definition mutex.cpp:27
基于 BaseQueue 的泛型队列模板类 (Generic queue template class based on BaseQueue).
Definition queue.hpp:354
信号量类,实现线程同步机制 Semaphore class implementing thread synchronization
Definition semaphore.hpp:23
void Post()
释放(增加)信号量 Releases (increments) the semaphore
Definition semaphore.cpp:13
ErrorCode Wait(uint32_t timeout=UINT32_MAX)
等待(减少)信号量 Waits (decrements) the semaphore
Definition semaphore.cpp:15
LibXR Color Control Library / LibXR终端颜色控制库
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值