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

块队列(ChunkQueue)用于存储和管理数据块 ChunkQueue is used to store and manage chunks of data More...

#include <chunk_queue.hpp>

Collaboration diagram for LibXR::ChunkQueue:

Public Types

typedef uint32_t BlockInfo
 记录块信息的类型 Type for storing block information
 

Public Member Functions

 ChunkQueue (size_t max_blocks, size_t data_buffer_size)
 构造一个块队列 Constructs a chunk queue
 
ErrorCode CreateNewBlock ()
 创建一个新的数据块(线程安全) Creates a new data block (thread-safe)
 
ErrorCode AppendToCurrentBlock (const void *data, size_t size)
 创建一个新的数据块(线程安全) Creates a new data block (thread-safe)
 
ErrorCode Pop (size_t size, void *data=nullptr)
 弹出指定大小的数据(线程安全) Pops the specified size of data (thread-safe)
 
ErrorCode PopFromCallback (size_t size, void *data, bool in_isr)
 从回调函数(ISR)中弹出数据 Pops data from an ISR (Interrupt Service Routine)
 
ErrorCode PopBlock (void *buffer, size_t *out_size)
 弹出整个数据块(线程安全) Pops an entire data block (thread-safe)
 
ErrorCode PopBlockFromCallback (void *buffer, size_t *out_size, bool in_isr)
 从回调函数(ISR)中弹出整个数据块 Pops an entire data block from an ISR
 
void Reset ()
 重置队列 Resets the queue
 
size_t Size ()
 获取当前数据大小 Gets the current size of the data
 
size_t BlockSize ()
 获取当前数据块数量 Gets the current number of data blocks
 
size_t SizeFromCallback (bool in_isr)
 从 ISR 中获取当前数据大小 Gets the current data size from an ISR
 
size_t BlockSizeFromCallback (bool in_isr)
 从 ISR 中获取当前数据块数量 Gets the current number of data blocks from an ISR
 
size_t EmptySize ()
 获取队列的剩余可用空间(线程安全) Gets the remaining available space in the queue (thread-safe)
 
size_t EmptyBlockSize ()
 获取块信息的剩余可用空间(线程安全) Gets the remaining available space in the block information (thread-safe)
 
size_t EmptySizeFromCallback (bool in_isr)
 从 ISR(中断服务例程)中获取队列的剩余可用空间 Gets the remaining available space in the queue from an ISR (Interrupt Service Routine)
 
size_t EmptyBlockSizeFromCallback (bool in_isr)
 从 ISR(中断服务例程)中获取块信息的剩余可用空间 Gets the remaining available space in the block information from an ISR (Interrupt Service Routine)
 
 ChunkQueue (const ChunkQueue &)=delete
 
ChunkQueue operator= (const ChunkQueue &)=delete
 
ChunkQueue operator= (ChunkQueue &)=delete
 
ChunkQueue operator= (const ChunkQueue &&)=delete
 
ChunkQueue operator= (ChunkQueue &&)=delete
 

Private Member Functions

ErrorCode CreateNewBlockNoLock ()
 
ErrorCode AppendToCurrentBlockNoLock (const void *data, size_t size)
 
ErrorCode PopNoLock (size_t size, void *data=nullptr)
 
ErrorCode PopBlockNoLock (void *buffer, size_t *out_size)
 

Private Attributes

BaseQueue block_queue_
 
BaseQueue data_queue_
 
size_t max_blocks_
 
Mutex mutex_
 

Detailed Description

块队列(ChunkQueue)用于存储和管理数据块 ChunkQueue is used to store and manage chunks of data

该类实现了一个支持多块数据存储的队列,提供线程安全的访问方式,并支持从 ISR(中断服务例程)中调用。 This class implements a multi-block data storage queue, provides thread-safe access, and supports calls from an ISR (Interrupt Service Routine).

Definition at line 21 of file chunk_queue.hpp.

Member Typedef Documentation

◆ BlockInfo

记录块信息的类型 Type for storing block information

Definition at line 24 of file chunk_queue.hpp.

Constructor & Destructor Documentation

◆ ChunkQueue()

LibXR::ChunkQueue::ChunkQueue ( size_t  max_blocks,
size_t  data_buffer_size 
)
inline

构造一个块队列 Constructs a chunk queue

Parameters
max_blocks最大块数 Maximum number of blocks
data_buffer_size数据缓冲区大小 Size of the data buffer

Definition at line 32 of file chunk_queue.hpp.

33 : block_queue_(sizeof(BlockInfo), max_blocks),
34 data_queue_(1, data_buffer_size),
35 max_blocks_(max_blocks)
36 {
38 }
ErrorCode CreateNewBlock()
创建一个新的数据块(线程安全) Creates a new data block (thread-safe)
uint32_t BlockInfo
记录块信息的类型 Type for storing block information
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值

Member Function Documentation

◆ AppendToCurrentBlock()

ErrorCode LibXR::ChunkQueue::AppendToCurrentBlock ( const void data,
size_t  size 
)
inline

创建一个新的数据块(线程安全) Creates a new data block (thread-safe)

Returns
操作结果 ErrorCode indicating success or failure

Definition at line 56 of file chunk_queue.hpp.

57 {
58 Mutex::LockGuard lock_guard(mutex_);
59 return AppendToCurrentBlockNoLock(data, size);
60 }

◆ AppendToCurrentBlockNoLock()

ErrorCode LibXR::ChunkQueue::AppendToCurrentBlockNoLock ( const void data,
size_t  size 
)
inlineprivate

Definition at line 325 of file chunk_queue.hpp.

326 {
327 if (!data)
328 {
329 return ErrorCode::PTR_NULL;
330 }
331 if (size == 0)
332 {
333 return ErrorCode::ARG_ERR;
334 }
335
336 if (block_queue_.Size() == 0)
337 {
338 if (CreateNewBlockNoLock() != ErrorCode::OK)
339 {
340 return ErrorCode::FULL;
341 }
342 }
343
344 auto index = block_queue_.GetLastElementIndex();
345
346 BlockInfo *last_block = static_cast<BlockInfo *>(block_queue_[index]);
347
348 if (size > data_queue_.EmptySize())
349 {
350 return ErrorCode::NO_BUFF;
351 }
352
353 if (data_queue_.PushBatch(reinterpret_cast<const uint8_t *>(data), size) !=
354 ErrorCode::OK)
355 {
356 return ErrorCode::FULL;
357 }
358
359 *last_block += size;
360 return ErrorCode::OK;
361 }
size_t Size()
获取队列中的元素个数 (Get the number of elements in the queue).
Definition queue.hpp:305
size_t EmptySize()
获取队列的空闲空间 (Get the available space in the queue).
Definition queue.hpp:325
ErrorCode PushBatch(const void *data, size_t size)
批量推入多个元素 (Push multiple elements into the queue).
Definition queue.hpp:175
int GetLastElementIndex()
获取队列中最后一个元素的索引 (Get the index of the last element in the queue).
Definition queue.hpp:138

◆ BlockSize()

size_t LibXR::ChunkQueue::BlockSize ( )
inline

获取当前数据块数量 Gets the current number of data blocks

Returns
当前数据块数量 Current number of data blocks

Definition at line 158 of file chunk_queue.hpp.

159 {
160 Mutex::LockGuard lock_guard(mutex_);
161
162 return block_queue_.Size();
163 }

◆ BlockSizeFromCallback()

size_t LibXR::ChunkQueue::BlockSizeFromCallback ( bool  in_isr)
inline

从 ISR 中获取当前数据块数量 Gets the current number of data blocks from an ISR

Parameters
in_isr是否在 ISR 中调用 Whether it is called from an ISR
Returns
size_t 当前数据块数量 Current number of data blocks

Definition at line 190 of file chunk_queue.hpp.

191 {
192 Mutex::LockGuardInCallback lock_guard(mutex_, in_isr);
193
194 if (!lock_guard.Locked())
195 {
196 return 0;
197 }
198
199 return block_queue_.Size();
200 }

◆ CreateNewBlock()

ErrorCode LibXR::ChunkQueue::CreateNewBlock ( )
inline

创建一个新的数据块(线程安全) Creates a new data block (thread-safe)

Returns
操作结果 ErrorCode indicating success or failure

Definition at line 45 of file chunk_queue.hpp.

46 {
47 Mutex::LockGuard lock_guard(mutex_);
48 return CreateNewBlockNoLock();
49 }

◆ CreateNewBlockNoLock()

ErrorCode LibXR::ChunkQueue::CreateNewBlockNoLock ( )
inlineprivate

Definition at line 304 of file chunk_queue.hpp.

305 {
306 auto index = block_queue_.GetLastElementIndex();
307
308 if (index >= 0)
309 {
310 BlockInfo *last_block = reinterpret_cast<BlockInfo *>(block_queue_[index]);
311 if (*last_block == 0)
312 {
313 return ErrorCode::OK;
314 }
315 }
316
317 if (block_queue_.Size() >= max_blocks_)
318 {
319 return ErrorCode::FULL;
320 }
322 return block_queue_.Push(&new_block);
323 }
ErrorCode Push(const void *data)
向队列中添加一个元素 (Push an element into the queue).
Definition queue.hpp:66

◆ EmptyBlockSize()

size_t LibXR::ChunkQueue::EmptyBlockSize ( )
inline

获取块信息的剩余可用空间(线程安全) Gets the remaining available space in the block information (thread-safe)

Returns
size_t

Definition at line 236 of file chunk_queue.hpp.

237 {
238 Mutex::LockGuard lock_guard(mutex_);
239 return block_queue_.EmptySize();
240 }

◆ EmptyBlockSizeFromCallback()

size_t LibXR::ChunkQueue::EmptyBlockSizeFromCallback ( bool  in_isr)
inline

从 ISR(中断服务例程)中获取块信息的剩余可用空间 Gets the remaining available space in the block information from an ISR (Interrupt Service Routine)

Parameters
in_isr是否在 ISR 中调用 Whether it is called from an ISR
Returns
size_t 剩余可用空间 The remaining available space

Definition at line 285 of file chunk_queue.hpp.

286 {
287 Mutex::LockGuardInCallback lock_guard(mutex_, in_isr);
288
289 if (!lock_guard.Locked())
290 {
291 return 0;
292 }
293
294 return block_queue_.EmptySize();
295 }

◆ EmptySize()

size_t LibXR::ChunkQueue::EmptySize ( )
inline

获取队列的剩余可用空间(线程安全) Gets the remaining available space in the queue (thread-safe)

Returns
可用空间大小 The size of available space

该方法通过互斥锁保证线程安全,检查 block_queue_ 是否还有空余块, 若有,则返回 data_queue_ 的剩余空间,否则返回 0。

This method ensures thread safety using a mutex lock. It checks whether block_queue_ has available blocks. If available, it returns the remaining space in data_queue_, otherwise, it returns 0.

Definition at line 216 of file chunk_queue.hpp.

217 {
218 Mutex::LockGuard lock_guard(mutex_);
219
220 if (block_queue_.EmptySize() > 0)
221 {
222 return data_queue_.EmptySize();
223 }
224 else
225 {
226 return 0;
227 }
228 }

◆ EmptySizeFromCallback()

size_t LibXR::ChunkQueue::EmptySizeFromCallback ( bool  in_isr)
inline

从 ISR(中断服务例程)中获取队列的剩余可用空间 Gets the remaining available space in the queue from an ISR (Interrupt Service Routine)

Parameters
in_isr是否在 ISR 中调用 Whether it is called from an ISR
Returns
可用空间大小 The size of available space

该方法在中断服务例程(ISR)中安全使用,通过 Mutex::LockGuardInCallback 进行锁保护。 若 block_queue_ 仍有可用块,则返回 data_queue_ 的剩余空间,否则返回 0。

This method is safe to use in an ISR (Interrupt Service Routine), protected by Mutex::LockGuardInCallback. If block_queue_ has available blocks, it returns the remaining space in data_queue_, otherwise, it returns 0.

Definition at line 258 of file chunk_queue.hpp.

259 {
260 Mutex::LockGuardInCallback lock_guard(mutex_, in_isr);
261
262 if (!lock_guard.Locked())
263 {
264 return 0;
265 }
266
267 if (block_queue_.EmptySize() > 0)
268 {
269 return data_queue_.EmptySize();
270 }
271 else
272 {
273 return 0;
274 }
275 }

◆ Pop()

ErrorCode LibXR::ChunkQueue::Pop ( size_t  size,
void data = nullptr 
)
inline

弹出指定大小的数据(线程安全) Pops the specified size of data (thread-safe)

Parameters
size要弹出的数据大小 Size of the data to pop
data存储弹出数据的缓冲区(可选) Buffer to store popped data (optional)
Returns
操作结果 ErrorCode indicating success or failure

Definition at line 69 of file chunk_queue.hpp.

70 {
71 Mutex::LockGuard lock_guard(mutex_);
72
73 return PopNoLock(size, data);
74 }

◆ PopBlock()

ErrorCode LibXR::ChunkQueue::PopBlock ( void buffer,
size_t out_size 
)
inline

弹出整个数据块(线程安全) Pops an entire data block (thread-safe)

Parameters
buffer存储数据的缓冲区 Buffer to store popped data
out_size返回的块大小 Output parameter for block size
Returns
操作结果 ErrorCode indicating success or failure

Definition at line 103 of file chunk_queue.hpp.

104 {
105 Mutex::LockGuard lock_guard(mutex_);
106
107 return PopBlockNoLock(buffer, out_size);
108 }

◆ PopBlockFromCallback()

ErrorCode LibXR::ChunkQueue::PopBlockFromCallback ( void buffer,
size_t out_size,
bool  in_isr 
)
inline

从回调函数(ISR)中弹出整个数据块 Pops an entire data block from an ISR

Parameters
buffer存储数据的缓冲区 Buffer to store popped data
out_size返回的块大小 Output parameter for block size
in_isr是否在 ISR 中调用 Whether it is called from an ISR
Returns
操作结果 ErrorCode indicating success or failure

Definition at line 118 of file chunk_queue.hpp.

119 {
120 Mutex::LockGuardInCallback lock_guard(mutex_, in_isr);
121
122 if (!lock_guard.Locked())
123 {
124 return ErrorCode::TIMEOUT;
125 }
126
127 return PopBlockNoLock(buffer, out_size);
128 }

◆ PopBlockNoLock()

ErrorCode LibXR::ChunkQueue::PopBlockNoLock ( void buffer,
size_t out_size 
)
inlineprivate

Definition at line 409 of file chunk_queue.hpp.

410 {
411 auto index = block_queue_.GetFirstElementIndex();
412
413 if (index >= 0)
414 {
415 BlockInfo *last_block = static_cast<BlockInfo *>(block_queue_[index]);
416 if (*last_block == 0)
417 {
418 return ErrorCode::EMPTY;
419 }
420 }
421 else
422 {
423 return ErrorCode::EMPTY;
424 }
425
426 BlockInfo block; // NOLINT
427 if (block_queue_.Pop(&block) != ErrorCode::OK)
428 {
429 ASSERT(false);
430 return ErrorCode::EMPTY;
431 }
432
433 if (data_queue_.PopBatch(buffer, block) != ErrorCode::OK)
434 {
435 ASSERT(false);
436 return ErrorCode::CHECK_ERR;
437 }
438 *out_size = block;
439 return ErrorCode::OK;
440 }
ErrorCode Pop(void *data=nullptr)
移除队列头部的元素 (Pop the front element from the queue).
Definition queue.hpp:114
ErrorCode PopBatch(void *data, size_t size)
批量移除多个元素 (Pop multiple elements from the queue).
Definition queue.hpp:208
int GetFirstElementIndex()
获取队列中第一个元素的索引 (Get the index of the first element in the queue).
Definition queue.hpp:156

◆ PopFromCallback()

ErrorCode LibXR::ChunkQueue::PopFromCallback ( size_t  size,
void data,
bool  in_isr 
)
inline

从回调函数(ISR)中弹出数据 Pops data from an ISR (Interrupt Service Routine)

Parameters
size要弹出的数据大小 Size of the data to pop
data存储弹出数据的缓冲区 Buffer to store popped data
in_isr是否在中断服务程序(ISR)中调用 Whether it is called from an ISR
Returns
操作结果 ErrorCode indicating success or failure

Definition at line 84 of file chunk_queue.hpp.

85 {
86 Mutex::LockGuardInCallback lock_guard(mutex_, in_isr);
87
88 if (!lock_guard.Locked())
89 {
90 return ErrorCode::TIMEOUT;
91 }
92
93 return PopNoLock(size, data);
94 }

◆ PopNoLock()

ErrorCode LibXR::ChunkQueue::PopNoLock ( size_t  size,
void data = nullptr 
)
inlineprivate

Definition at line 363 of file chunk_queue.hpp.

364 {
365 if (data_queue_.Size() < size)
366 {
367 return ErrorCode::EMPTY;
368 }
369
370 size_t remaining_size = size;
371
372 while (remaining_size > 0)
373 {
374 auto index = block_queue_.GetFirstElementIndex();
375 if (index < 0)
376 {
377 return ErrorCode::CHECK_ERR;
378 }
379
380 BlockInfo *block = static_cast<BlockInfo *>(block_queue_[index]);
381
382 if (remaining_size < *block)
383 {
384 if (data_queue_.PopBatch(data, remaining_size) != ErrorCode::OK)
385 {
386 ASSERT(false);
387 return ErrorCode::CHECK_ERR;
388 }
389
391 remaining_size = 0;
392 }
393 else
394 {
395 if (data_queue_.PopBatch(data, *block) != ErrorCode::OK)
396 {
397 ASSERT(false);
398 return ErrorCode::CHECK_ERR;
399 }
401 data = static_cast<uint8_t *>(data) + *block;
402 block_queue_.Pop();
403 }
404 }
405
406 return ErrorCode::OK;
407 }

◆ Reset()

void LibXR::ChunkQueue::Reset ( )
inline

重置队列 Resets the queue

Definition at line 134 of file chunk_queue.hpp.

135 {
136 block_queue_.Reset();
137 data_queue_.Reset();
138 }
void Reset()
重置队列,清空所有数据 (Reset the queue and clear all data).
Definition queue.hpp:295

◆ Size()

size_t LibXR::ChunkQueue::Size ( )
inline

获取当前数据大小 Gets the current size of the data

Returns
当前数据大小 Current data size

Definition at line 145 of file chunk_queue.hpp.

146 {
147 Mutex::LockGuard lock_guard(mutex_);
148
149 return data_queue_.Size();
150 }

◆ SizeFromCallback()

size_t LibXR::ChunkQueue::SizeFromCallback ( bool  in_isr)
inline

从 ISR 中获取当前数据大小 Gets the current data size from an ISR

Parameters
in_isr是否在 ISR 中调用 Whether it is called from an ISR
Returns
当前数据大小 Current data size

Definition at line 171 of file chunk_queue.hpp.

172 {
173 Mutex::LockGuardInCallback lock_guard(mutex_, in_isr);
174
175 if (!lock_guard.Locked())
176 {
177 return 0;
178 }
179
180 return data_queue_.Size();
181 }

Field Documentation

◆ block_queue_

BaseQueue LibXR::ChunkQueue::block_queue_
private

Definition at line 442 of file chunk_queue.hpp.

◆ data_queue_

BaseQueue LibXR::ChunkQueue::data_queue_
private

Definition at line 443 of file chunk_queue.hpp.

◆ max_blocks_

size_t LibXR::ChunkQueue::max_blocks_
private

Definition at line 444 of file chunk_queue.hpp.

◆ mutex_

Mutex LibXR::ChunkQueue::mutex_
private

Definition at line 445 of file chunk_queue.hpp.


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