libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
queue.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <queue>
12#include <promeki/core/error.h>
13#include <promeki/core/list.h>
14#include <promeki/core/mutex.h>
16
18
44template <typename T>
45class Queue {
46 public:
47 Queue() { };
48 ~Queue() { };
49
54 void push(const T &val) {
55 Mutex::Locker locker(_mutex);
56 _queue.push(val);
57 _cv.wakeOne();
58 return;
59 }
60
65 void push(T &&val) {
66 Mutex::Locker locker(_mutex);
67 _queue.push(std::move(val));
68 _cv.wakeOne();
69 return;
70 }
71
77 template <typename... Args>
78 void emplace(Args&&... args) {
79 Mutex::Locker locker(_mutex);
80 _queue.emplace(std::forward<Args>(args)...);
81 _cv.wakeOne();
82 return;
83 }
84
89 void push(const List<T> &list) {
90 Mutex::Locker locker(_mutex);
91 for(const auto &item : list) _queue.push(item);
92 _cv.wakeAll();
93 return;
94 }
95
104 std::pair<T, Error> pop(unsigned int timeoutMs = 0) {
105 Mutex::Locker locker(_mutex);
106 Error err = _cv.wait(_mutex,
107 [this] { return !_queue.empty(); }, timeoutMs);
108 if(err != Error::Ok) return {T{}, err};
109 T ret = std::move(_queue.front());
110 _queue.pop();
111 if(_queue.empty()) _cv.wakeAll();
112 return {std::move(ret), Error::Ok};
113 }
114
120 bool popOrFail(T &val) {
121 Mutex::Locker locker(_mutex);
122 if(_queue.empty()) return false;
123 val = std::move(_queue.front());
124 _queue.pop();
125 if(_queue.empty()) _cv.wakeAll();
126 return true;
127 }
128
137 std::pair<T, Error> peek(unsigned int timeoutMs = 0) {
138 Mutex::Locker locker(_mutex);
139 Error err = _cv.wait(_mutex,
140 [this] { return !_queue.empty(); }, timeoutMs);
141 if(err != Error::Ok) return {T{}, err};
142 T ret = _queue.front();
143 return {std::move(ret), Error::Ok};
144 }
145
151 bool peekOrFail(T &val) {
152 Mutex::Locker locker(_mutex);
153 if(_queue.empty()) return false;
154 val = _queue.front();
155 return true;
156 }
157
171 Error waitForEmpty(unsigned int timeoutMs = 0) {
172 Mutex::Locker locker(_mutex);
173 return _cv.wait(_mutex,
174 [this] { return _queue.empty(); }, timeoutMs);
175 }
176
181 bool isEmpty() const {
182 Mutex::Locker locker(_mutex);
183 return _queue.empty();
184 }
185
190 size_t size() const {
191 Mutex::Locker locker(_mutex);
192 return _queue.size();
193 }
194
198 void clear() {
199 Mutex::Locker locker(_mutex);
200 std::queue<T> empty;
201 std::swap(_queue, empty);
202 return;
203 }
204
205 private:
206 mutable Mutex _mutex;
207 WaitCondition _cv;
208 std::queue<T> _queue;
209};
210
Lightweight error code wrapper for the promeki library.
Definition error.h:39
@ Ok
No error.
Definition error.h:51
Dynamic array container wrapping std::vector.
Definition list.h:40
RAII scoped locker for Mutex.
Definition mutex.h:41
Mutual exclusion lock wrapping std::mutex.
Definition mutex.h:33
Thread-safe FIFO queue.
Definition queue.h:45
std::pair< T, Error > pop(unsigned int timeoutMs=0)
Removes and returns the front element, blocking until one is available or the timeout expires.
Definition queue.h:104
bool isEmpty() const
Returns true if the queue has no elements.
Definition queue.h:181
void push(T &&val)
Moves val onto the back of the queue.
Definition queue.h:65
size_t size() const
Returns the number of elements currently in the queue.
Definition queue.h:190
void emplace(Args &&... args)
Constructs an element in-place at the back of the queue.
Definition queue.h:78
bool popOrFail(T &val)
Tries to pop the front element without blocking.
Definition queue.h:120
Error waitForEmpty(unsigned int timeoutMs=0)
Blocks until the queue is empty.
Definition queue.h:171
void push(const T &val)
Pushes a copy of val onto the back of the queue.
Definition queue.h:54
void clear()
Removes all elements from the queue.
Definition queue.h:198
void push(const List< T > &list)
Pushes every element in list onto the back of the queue.
Definition queue.h:89
std::pair< T, Error > peek(unsigned int timeoutMs=0)
Returns a copy of the front element without removing it, blocking until one is available or the timeo...
Definition queue.h:137
bool peekOrFail(T &val)
Tries to copy the front element without blocking or removing it.
Definition queue.h:151
Condition variable wrapping std::condition_variable.
Definition waitcondition.h:26
void wakeAll()
Wakes all waiting threads.
Definition waitcondition.h:99
Error wait(Mutex &mutex, unsigned int timeoutMs=0)
Waits until woken or the timeout expires.
Definition waitcondition.h:50
void wakeOne()
Wakes one waiting thread.
Definition waitcondition.h:94
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19