mirror of
https://github.com/shadps4-emu/shadPS4.git
synced 2025-08-02 15:32:52 +00:00
Make AvPlayerQueue size atomic
This commit is contained in:
parent
723068049e
commit
0be9ebcdce
@ -58,12 +58,13 @@ template <class T>
|
|||||||
class AvPlayerQueue {
|
class AvPlayerQueue {
|
||||||
public:
|
public:
|
||||||
size_t Size() {
|
size_t Size() {
|
||||||
return m_queue.size();
|
return m_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Push(T&& value) {
|
void Push(T&& value) {
|
||||||
std::lock_guard guard(m_mutex);
|
std::lock_guard guard(m_mutex);
|
||||||
m_queue.emplace(std::forward<T>(value));
|
m_queue.emplace(std::forward<T>(value));
|
||||||
|
++m_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::optional<T> Pop() {
|
std::optional<T> Pop() {
|
||||||
@ -71,18 +72,24 @@ public:
|
|||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
std::lock_guard guard(m_mutex);
|
std::lock_guard guard(m_mutex);
|
||||||
|
if (Size() == 0) { // in case size changes while the mutex is being locked
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
auto result = std::move(m_queue.front());
|
auto result = std::move(m_queue.front());
|
||||||
m_queue.pop();
|
m_queue.pop();
|
||||||
|
--m_size;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Clear() {
|
void Clear() {
|
||||||
std::lock_guard guard(m_mutex);
|
std::lock_guard guard(m_mutex);
|
||||||
m_queue = {};
|
m_queue = {};
|
||||||
|
m_size = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::mutex m_mutex{};
|
std::mutex m_mutex{};
|
||||||
|
std::atomic_size_t m_size{0};
|
||||||
std::queue<T> m_queue{};
|
std::queue<T> m_queue{};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user