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 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 }
58
66 ErrorCode Pop(Data &data, uint32_t timeout)
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 }
85
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 }
102
109 ErrorCode Pop(uint32_t timeout)
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 }
128
136 ErrorCode PushFromCallback(const Data &data, bool in_isr)
137 {
138 UNUSED(in_isr);
139 return Push(data);
140 }
141
147 size_t Size()
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 }
157
163 size_t EmptySize()
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 }
173
174 private:
178};
179
180} // 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:16
void Unlock()
解锁互斥锁 (Unlock the mutex).
Definition mutex.cpp:40
基于 BaseQueue 的泛型队列模板类 (Generic queue template class based on BaseQueue).
Definition queue.hpp:180
信号量类,实现线程同步机制 Semaphore class implementing thread synchronization
Definition semaphore.hpp:23
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
LibXR 命名空间
Definition ch32_can.hpp:14
ErrorCode
定义错误码枚举
@ OK
操作成功 | Operation successful