Add a maximum number of forward/backward time steps that can be queued

This commit is contained in:
AdenKoperczak 2025-04-15 13:12:49 -04:00
parent 3537a233ca
commit 97693fdace
No known key found for this signature in database
GPG key ID: 9843017036F62EE7
4 changed files with 133 additions and 4 deletions

View file

@ -0,0 +1,49 @@
#include <scwx/qt/util/queue_counter.hpp>
#include <atomic>
namespace scwx::qt::util
{
class QueueCounter::Impl
{
public:
explicit Impl(size_t maxCount) : maxCount_ {maxCount} {}
const size_t maxCount_;
std::atomic<size_t> count_ {0};
};
QueueCounter::QueueCounter(size_t maxCount) :
p {std::make_unique<Impl>(maxCount)}
{
}
QueueCounter::~QueueCounter() = default;
bool QueueCounter::add()
{
const size_t count = p->count_.fetch_add(1);
// Must be >= (not ==) to avoid race conditions
if (count >= p->maxCount_)
{
p->count_.fetch_sub(1);
return false;
}
else
{
return true;
}
}
void QueueCounter::remove()
{
p->count_.fetch_sub(1);
}
bool QueueCounter::is_lock_free()
{
return p->count_.is_lock_free();
}
} // namespace scwx::qt::util