mirror of
https://github.com/RPCS3/rpcs3.git
synced 2025-07-02 21:11:25 +12:00
lf_queue: add range-for support (endless loop with waiting)
This commit is contained in:
parent
16dd72b3e3
commit
5624b001ae
2 changed files with 66 additions and 9 deletions
|
@ -401,6 +401,64 @@ public:
|
|||
|
||||
return count;
|
||||
}
|
||||
|
||||
// Iterator that enables direct endless range-for loop: for (auto* ptr : queue) ...
|
||||
class iterator
|
||||
{
|
||||
lf_queue* _this = nullptr;
|
||||
|
||||
lf_queue_slice<T> m_data;
|
||||
|
||||
public:
|
||||
constexpr iterator() = default;
|
||||
|
||||
explicit iterator(lf_queue* _this)
|
||||
: _this(_this)
|
||||
{
|
||||
m_data = _this->pop_all();
|
||||
}
|
||||
|
||||
bool operator !=(const iterator& rhs) const
|
||||
{
|
||||
return _this != rhs._this;
|
||||
}
|
||||
|
||||
T* operator *() const
|
||||
{
|
||||
return m_data ? m_data.get() : nullptr;
|
||||
}
|
||||
|
||||
iterator& operator ++()
|
||||
{
|
||||
if (m_data)
|
||||
{
|
||||
m_data.pop_front();
|
||||
}
|
||||
|
||||
if (!m_data)
|
||||
{
|
||||
m_data = _this->pop_all();
|
||||
|
||||
if (!m_data)
|
||||
{
|
||||
_this->wait();
|
||||
m_data = _this->pop_all();
|
||||
}
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
iterator begin()
|
||||
{
|
||||
return iterator{this};
|
||||
}
|
||||
|
||||
iterator end()
|
||||
{
|
||||
return iterator{};
|
||||
}
|
||||
};
|
||||
|
||||
// Assignable lock-free thread-safe value of any type (memory-inefficient)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue