forked from zillevdr/vdr-plugin-softhddevice-drm
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathqueue.h
More file actions
133 lines (108 loc) · 2.5 KB
/
queue.h
File metadata and controls
133 lines (108 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// SPDX-License-Identifier: AGPL-3.0-or-later
/**
* @file queue.h
* Thread-safe Queue
*
* @license{AGPL-3.0-or-later}
*/
#ifndef __SOFTHDQUEUE_H
#define __SOFTHDQUEUE_H
#include <cstdint>
#include <deque>
#include <mutex>
/**
* Thread-safe Queue
*
* This class provides a thread-safe queue implementation using std::deque
* and std::mutex for synchronization. It supports both FIFO queue operations
* and bounded capacity management.
*
* @tparam T The type of elements stored in the queue
*
* @ingroup misc
*/
template <typename T>
class cQueue {
public:
cQueue(size_t maxSize) : m_maxSize(maxSize) {};
~cQueue(void) {};
/**
* Push an element to the front of the queue
*
* @param element The element to push
* @return true if successfully pushed, false if queue is full
*/
bool Push(T *element) {
std::lock_guard<std::mutex> lock(m_mutex);
if (m_deque.size() >= m_maxSize) {
return false;
}
m_deque.push_front(element);
return true;
}
/**
* Pop an element from the back of the queue
*
* @return T The popped element, or nullptr if queue is empty
*/
T *Pop(void) {
std::lock_guard<std::mutex> lock(m_mutex);
if (m_deque.empty()) {
return nullptr;
}
T *element = m_deque.back();
m_deque.pop_back();
return element;
}
/**
* Get a reference to the back element
*
* @return T Element at the back (next to be popped), or nullptr if queue is empty
*/
T *Peek(void) {
std::lock_guard<std::mutex> lock(m_mutex);
if (m_deque.empty()) {
return nullptr;
}
return m_deque.back();
}
/**
* Remove all elements from the queue
*/
void Clear(void) {
std::lock_guard<std::mutex> lock(m_mutex);
m_deque.clear();
}
/**
* Check if the queue is empty
*
* @return true if queue is empty, false otherwise
*/
bool IsEmpty(void) {
std::lock_guard<std::mutex> lock(m_mutex);
return m_deque.empty();
}
/**
* Check if the queue is full
*
* @return true if queue is full, false otherwise
*/
bool IsFull(void) {
std::lock_guard<std::mutex> lock(m_mutex);
return m_deque.size() >= m_maxSize;
}
/**
* Get the current size of the queue
*
* @return Number of elements currently in the queue
*/
size_t Size(void) {
std::lock_guard<std::mutex> lock(m_mutex);
return m_deque.size();
}
private:
std::deque<T*> m_deque; ///< Underlying deque container
std::mutex m_mutex; ///< Mutex for thread-safe access
size_t m_maxSize; ///< Maximum queue capacity
};
#endif