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>
21{
22 public:
28 LockQueue(size_t length) : queue_handle_(length) {}
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 Pop(uint32_t timeout)
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 }
109
117 ErrorCode PushFromCallback(const Data &data, bool in_isr)
118 {
119 UNUSED(in_isr);
120 return Push(data);
121 }
122
128 size_t Size()
129 {
130 mutex_.Lock();
131 auto ans = queue_handle_.Size();
132 mutex_.Unlock();
133 return ans;
134 }
135
141 size_t EmptySize()
142 {
143 mutex_.Lock();
144 auto ans = queue_handle_.EmptySize();
145 mutex_.Unlock();
146 return ans;
147 }
148
149 private:
153};
154
155} // namespace LibXR
线程安全的锁队列类,提供同步和异步操作支持 Thread-safe lock queue class with synchronous and asynchronous operation suppor...
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 Push(const Data &data)
向队列中推送数据 Pushes data into the queue
size_t EmptySize()
获取队列的剩余容量 Gets the remaining capacity of the queue
Mutex mutex_
互斥锁 Mutex for thread safety
Semaphore semaphore_handle_
信号量 Semaphore for synchronization
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
互斥锁类,提供线程同步机制 (Mutex class providing thread synchronization mechanisms).
Definition mutex.hpp:18
ErrorCode Lock()
加锁,如果锁已被占用,则阻塞等待 (Lock the mutex, blocking if it is already locked).
Definition mutex.cpp:14
void Unlock()
解锁互斥锁 (Unlock the mutex).
Definition mutex.cpp:28
基于 BaseQueue 的泛型队列模板类 (Generic queue template class based on BaseQueue).
Definition queue.hpp:362
信号量类,实现线程同步机制 Semaphore class implementing thread synchronization
Definition semaphore.hpp:23
void Post()
释放(增加)信号量 Releases (increments) the semaphore
Definition semaphore.cpp:23
ErrorCode Wait(uint32_t timeout=UINT32_MAX)
等待(减少)信号量 Waits (decrements) the semaphore
Definition semaphore.cpp:25
LibXR 命名空间
Definition ch32_gpio.hpp:9