-
-
Notifications
You must be signed in to change notification settings - Fork 392
/
Copy pathqueue.hpp
128 lines (102 loc) · 3.02 KB
/
queue.hpp
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
/**
* Copyright (c) 2019 Paul-Louis Ageneau
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#ifndef RTC_IMPL_QUEUE_H
#define RTC_IMPL_QUEUE_H
#include "common.hpp"
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <mutex>
#include <queue>
namespace rtc::impl {
template <typename T> class Queue {
public:
using amount_function = std::function<size_t(const T &element)>;
Queue(size_t limit = 0, // elements (0 means no limit)
amount_function func = nullptr);
~Queue();
void stop();
bool running() const;
bool empty() const;
bool full() const;
size_t size() const; // elements
size_t amount() const; // amount
void push(T element);
optional<T> pop();
optional<T> peek();
optional<T> exchange(T element);
private:
const size_t mLimit;
size_t mAmount;
std::queue<T> mQueue;
std::condition_variable mPushCondition;
amount_function mAmountFunction;
bool mStopping = false;
mutable std::mutex mMutex;
};
template <typename T>
Queue<T>::Queue(size_t limit, amount_function func) : mLimit(limit), mAmount(0) {
mAmountFunction = func ? func : []([[maybe_unused]] const T &element) -> size_t { return 1; };
}
template <typename T> Queue<T>::~Queue() { stop(); }
template <typename T> void Queue<T>::stop() {
std::lock_guard lock(mMutex);
mStopping = true;
mPushCondition.notify_all();
}
template <typename T> bool Queue<T>::running() const {
std::lock_guard lock(mMutex);
return !mQueue.empty() || !mStopping;
}
template <typename T> bool Queue<T>::empty() const {
std::lock_guard lock(mMutex);
return mQueue.empty();
}
template <typename T> bool Queue<T>::full() const {
std::lock_guard lock(mMutex);
return mLimit > 0 && mQueue.size() >= mLimit;
}
template <typename T> size_t Queue<T>::size() const {
std::lock_guard lock(mMutex);
return mQueue.size();
}
template <typename T> size_t Queue<T>::amount() const {
std::lock_guard lock(mMutex);
return mAmount;
}
template <typename T> void Queue<T>::push(T element) {
std::unique_lock lock(mMutex);
mPushCondition.wait(lock, [this]() { return mLimit == 0 || mQueue.size() < mLimit || mStopping; });
if (mStopping)
return;
mAmount += mAmountFunction(element);
mQueue.emplace(std::move(element));
}
template <typename T> optional<T> Queue<T>::pop() {
std::unique_lock lock(mMutex);
if (mQueue.empty())
return nullopt;
mAmount -= mAmountFunction(mQueue.front());
optional<T> element{std::move(mQueue.front())};
mQueue.pop();
mPushCondition.notify_one();
return element;
}
template <typename T> optional<T> Queue<T>::peek() {
std::unique_lock lock(mMutex);
return !mQueue.empty() ? std::make_optional(mQueue.front()) : nullopt;
}
template <typename T> optional<T> Queue<T>::exchange(T element) {
std::unique_lock lock(mMutex);
if (mQueue.empty())
return nullopt;
std::swap(mQueue.front(), element);
return std::make_optional(std::move(element));
}
} // namespace rtc::impl
#endif