2018-11-17 21:07:04 +00:00
|
|
|
#ifndef LLARP_QUEUE_HPP
|
|
|
|
#define LLARP_QUEUE_HPP
|
|
|
|
|
2019-09-01 13:26:16 +00:00
|
|
|
#include <util/thread/queue_manager.hpp>
|
|
|
|
#include <util/thread/threading.hpp>
|
2018-11-17 21:07:04 +00:00
|
|
|
|
2020-05-01 19:51:15 +00:00
|
|
|
#include <optional>
|
2018-11-17 21:07:04 +00:00
|
|
|
#include <atomic>
|
|
|
|
#include <tuple>
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace thread
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2018-11-17 21:07:04 +00:00
|
|
|
class QueuePushGuard;
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2018-11-17 21:07:04 +00:00
|
|
|
class QueuePopGuard;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2018-11-17 21:07:04 +00:00
|
|
|
class Queue
|
|
|
|
{
|
|
|
|
// This class provides a thread-safe, lock-free, fixed-size queue.
|
|
|
|
public:
|
|
|
|
static constexpr size_t Alignment = 64;
|
|
|
|
|
|
|
|
private:
|
2020-04-07 18:38:56 +00:00
|
|
|
Type* m_data;
|
|
|
|
const char m_dataPadding[Alignment - sizeof(Type*)];
|
2018-11-17 21:07:04 +00:00
|
|
|
|
|
|
|
QueueManager m_manager;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
std::atomic<std::uint32_t> m_waitingPoppers;
|
2018-11-17 21:07:04 +00:00
|
|
|
util::Semaphore m_popSemaphore;
|
2020-04-07 18:38:56 +00:00
|
|
|
const char m_popSemaphorePadding[(2u * Alignment) - sizeof(util::Semaphore)];
|
2018-11-17 21:07:04 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
std::atomic<std::uint32_t> m_waitingPushers;
|
2018-11-17 21:07:04 +00:00
|
|
|
util::Semaphore m_pushSemaphore;
|
2020-04-07 18:38:56 +00:00
|
|
|
const char m_pushSemaphorePadding[(2u * Alignment) - sizeof(util::Semaphore)];
|
2018-11-17 21:07:04 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
friend QueuePopGuard<Type>;
|
|
|
|
friend QueuePushGuard<Type>;
|
2018-11-17 21:07:04 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
explicit Queue(size_t capacity);
|
|
|
|
|
|
|
|
~Queue();
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
Queue(const Queue&) = delete;
|
|
|
|
Queue&
|
|
|
|
operator=(const Queue&) = delete;
|
2018-11-17 21:07:04 +00:00
|
|
|
|
|
|
|
// Push back to the queue, blocking until space is available (if
|
|
|
|
// required). Will fail if the queue is disabled (or becomes disabled
|
|
|
|
// while waiting for space on the queue).
|
|
|
|
QueueReturn
|
2020-04-07 18:38:56 +00:00
|
|
|
pushBack(const Type& value);
|
2018-11-17 21:07:04 +00:00
|
|
|
|
|
|
|
QueueReturn
|
2020-04-07 18:38:56 +00:00
|
|
|
pushBack(Type&& value);
|
2018-11-17 21:07:04 +00:00
|
|
|
|
|
|
|
// Try to push back to the queue. Return false if the queue is full or
|
|
|
|
// disabled.
|
|
|
|
QueueReturn
|
2020-04-07 18:38:56 +00:00
|
|
|
tryPushBack(const Type& value);
|
2018-11-17 21:07:04 +00:00
|
|
|
|
|
|
|
QueueReturn
|
2020-04-07 18:38:56 +00:00
|
|
|
tryPushBack(Type&& value);
|
2018-11-17 21:07:04 +00:00
|
|
|
|
|
|
|
// Remove an element from the queue. Block until an element is available
|
|
|
|
Type
|
|
|
|
popFront();
|
2019-02-03 02:13:31 +00:00
|
|
|
|
2020-02-28 04:01:14 +00:00
|
|
|
// Remove an element from the queue. Block until an element is available
|
|
|
|
// or until <timeout> microseconds have elapsed
|
2020-05-01 19:51:15 +00:00
|
|
|
std::optional<Type>
|
2020-02-28 04:01:14 +00:00
|
|
|
popFrontWithTimeout(std::chrono::microseconds timeout);
|
|
|
|
|
2020-05-01 19:51:15 +00:00
|
|
|
std::optional<Type>
|
2018-11-17 21:07:04 +00:00
|
|
|
tryPopFront();
|
|
|
|
|
|
|
|
// Remove all elements from the queue. Note this is not atomic, and if
|
|
|
|
// other threads `pushBack` onto the queue during this call, the `size` of
|
|
|
|
// the queue is not guaranteed to be 0.
|
|
|
|
void
|
|
|
|
removeAll();
|
|
|
|
|
|
|
|
// Disable the queue. All push operations will fail "fast" (including
|
|
|
|
// blocked operations). Calling this method on a disabled queue has no
|
|
|
|
// effect.
|
|
|
|
void
|
|
|
|
disable();
|
|
|
|
|
|
|
|
// Enable the queue. Calling this method on a disabled queue has no
|
|
|
|
// effect.
|
|
|
|
void
|
|
|
|
enable();
|
|
|
|
|
|
|
|
size_t
|
|
|
|
capacity() const;
|
|
|
|
|
|
|
|
size_t
|
|
|
|
size() const;
|
|
|
|
|
|
|
|
bool
|
|
|
|
enabled() const;
|
|
|
|
|
|
|
|
bool
|
|
|
|
full() const;
|
|
|
|
|
|
|
|
bool
|
|
|
|
empty() const;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Provide a guard class to provide exception safety for pushing to a queue.
|
|
|
|
// On destruction, unless the `release` method has been called, will remove
|
|
|
|
// and destroy all elements from the queue, putting the queue into an empty
|
|
|
|
// state.
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2018-11-17 21:07:04 +00:00
|
|
|
class QueuePushGuard
|
|
|
|
{
|
|
|
|
private:
|
2020-04-07 18:38:56 +00:00
|
|
|
Queue<Type>* m_queue;
|
2018-11-17 21:07:04 +00:00
|
|
|
uint32_t m_generation;
|
|
|
|
uint32_t m_index;
|
|
|
|
|
|
|
|
public:
|
2020-04-07 18:38:56 +00:00
|
|
|
QueuePushGuard(Queue<Type>& queue, uint32_t generation, uint32_t index)
|
2018-11-17 21:07:04 +00:00
|
|
|
: m_queue(&queue), m_generation(generation), m_index(index)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~QueuePushGuard();
|
|
|
|
|
|
|
|
void
|
|
|
|
release();
|
|
|
|
};
|
|
|
|
|
|
|
|
// Provide a guard class to provide exception safety for popping from a
|
|
|
|
// queue. On destruction, this will pop the the given element from the
|
|
|
|
// queue.
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2018-11-17 21:07:04 +00:00
|
|
|
class QueuePopGuard
|
|
|
|
{
|
|
|
|
private:
|
2020-04-07 18:38:56 +00:00
|
|
|
Queue<Type>& m_queue;
|
2018-11-17 21:07:04 +00:00
|
|
|
uint32_t m_generation;
|
|
|
|
uint32_t m_index;
|
|
|
|
|
|
|
|
public:
|
2020-04-07 18:38:56 +00:00
|
|
|
QueuePopGuard(Queue<Type>& queue, uint32_t generation, uint32_t index)
|
2018-11-17 21:07:04 +00:00
|
|
|
: m_queue(queue), m_generation(generation), m_index(index)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~QueuePopGuard();
|
|
|
|
};
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
|
|
|
Queue<Type>::Queue(size_t capacity)
|
2018-11-17 21:07:04 +00:00
|
|
|
: m_data(nullptr)
|
|
|
|
, m_dataPadding()
|
|
|
|
, m_manager(capacity)
|
|
|
|
, m_waitingPoppers(0)
|
|
|
|
, m_popSemaphore(0)
|
|
|
|
, m_popSemaphorePadding()
|
|
|
|
, m_waitingPushers(0)
|
|
|
|
, m_pushSemaphore(0)
|
|
|
|
, m_pushSemaphorePadding()
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
m_data = static_cast<Type*>(::operator new(capacity * sizeof(Type)));
|
2018-11-17 21:07:04 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
|
|
|
Queue<Type>::~Queue()
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
removeAll();
|
|
|
|
|
|
|
|
// We have already deleted the queue members above, free as (void *)
|
2020-04-07 18:38:56 +00:00
|
|
|
::operator delete(static_cast<void*>(m_data));
|
2018-11-17 21:07:04 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2018-11-17 21:07:04 +00:00
|
|
|
QueueReturn
|
2020-04-07 18:38:56 +00:00
|
|
|
Queue<Type>::tryPushBack(const Type& value)
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
uint32_t generation = 0;
|
2020-04-07 18:38:56 +00:00
|
|
|
uint32_t index = 0;
|
2018-11-17 21:07:04 +00:00
|
|
|
|
|
|
|
// Sync point A
|
|
|
|
//
|
|
|
|
// The next call writes with full sequential consistency to the push
|
|
|
|
// index, which guarantees that the relaxed read to the waiting poppers
|
|
|
|
// count sees any waiting poppers from Sync point B.
|
|
|
|
|
|
|
|
QueueReturn retVal = m_manager.reservePushIndex(generation, index);
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (retVal != QueueReturn::Success)
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy into the array. If the copy constructor throws, the pushGuard will
|
|
|
|
// roll the reserve back.
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
QueuePushGuard<Type> pushGuard(*this, generation, index);
|
2018-11-17 21:07:04 +00:00
|
|
|
|
|
|
|
// Construct in place.
|
2020-04-07 18:38:56 +00:00
|
|
|
::new (&m_data[index]) Type(value);
|
2018-11-17 21:07:04 +00:00
|
|
|
|
|
|
|
pushGuard.release();
|
|
|
|
|
|
|
|
m_manager.commitPushIndex(generation, index);
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_waitingPoppers > 0)
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
m_popSemaphore.notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
return QueueReturn::Success;
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2018-11-17 21:07:04 +00:00
|
|
|
QueueReturn
|
2020-04-07 18:38:56 +00:00
|
|
|
Queue<Type>::tryPushBack(Type&& value)
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
uint32_t generation = 0;
|
2020-04-07 18:38:56 +00:00
|
|
|
uint32_t index = 0;
|
2018-11-17 21:07:04 +00:00
|
|
|
|
|
|
|
// Sync point A
|
|
|
|
//
|
|
|
|
// The next call writes with full sequential consistency to the push
|
|
|
|
// index, which guarantees that the relaxed read to the waiting poppers
|
|
|
|
// count sees any waiting poppers from Sync point B.
|
|
|
|
|
|
|
|
QueueReturn retVal = m_manager.reservePushIndex(generation, index);
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (retVal != QueueReturn::Success)
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy into the array. If the copy constructor throws, the pushGuard will
|
|
|
|
// roll the reserve back.
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
QueuePushGuard<Type> pushGuard(*this, generation, index);
|
2018-11-17 21:07:04 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
Type& dummy = value;
|
2018-11-17 21:07:04 +00:00
|
|
|
|
|
|
|
// Construct in place.
|
2020-04-07 18:38:56 +00:00
|
|
|
::new (&m_data[index]) Type(std::move(dummy));
|
2018-11-17 21:07:04 +00:00
|
|
|
|
|
|
|
pushGuard.release();
|
|
|
|
|
|
|
|
m_manager.commitPushIndex(generation, index);
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_waitingPoppers > 0)
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
m_popSemaphore.notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
return QueueReturn::Success;
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2020-05-01 19:51:15 +00:00
|
|
|
std::optional<Type>
|
2020-04-07 18:38:56 +00:00
|
|
|
Queue<Type>::tryPopFront()
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
uint32_t generation;
|
|
|
|
uint32_t index;
|
|
|
|
|
|
|
|
// Sync Point C.
|
|
|
|
//
|
|
|
|
// The call to reservePopIndex writes with full *sequential* consistency,
|
|
|
|
// which guarantees the relaxed read to waiting poppers is synchronized
|
|
|
|
// with Sync Point D.
|
|
|
|
|
|
|
|
QueueReturn retVal = m_manager.reservePopIndex(generation, index);
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (retVal != QueueReturn::Success)
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pop guard will (even if the move/copy constructor throws)
|
|
|
|
// - destroy the original object
|
|
|
|
// - update the queue
|
|
|
|
// - notify any waiting pushers
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
QueuePopGuard<Type> popGuard(*this, generation, index);
|
2020-05-01 19:51:15 +00:00
|
|
|
return std::optional<Type>(std::move(m_data[index]));
|
2018-11-17 21:07:04 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2018-11-17 21:07:04 +00:00
|
|
|
QueueReturn
|
2020-04-07 18:38:56 +00:00
|
|
|
Queue<Type>::pushBack(const Type& value)
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
for (;;)
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
QueueReturn retVal = tryPushBack(value);
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
switch (retVal)
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
// Queue disabled.
|
|
|
|
case QueueReturn::QueueDisabled:
|
|
|
|
// We pushed the value back
|
|
|
|
case QueueReturn::Success:
|
|
|
|
return retVal;
|
|
|
|
default:
|
|
|
|
// continue on.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_waitingPushers.fetch_add(1, std::memory_order_relaxed);
|
|
|
|
|
|
|
|
// Sync Point B.
|
|
|
|
//
|
|
|
|
// The call to `full` below loads the push index with full *sequential*
|
|
|
|
// consistency, which gives visibility of the change above to
|
|
|
|
// waiting pushers in Synchronisation Point B.
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (full() && enabled())
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
m_pushSemaphore.wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_waitingPushers.fetch_add(-1, std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2018-11-17 21:07:04 +00:00
|
|
|
QueueReturn
|
2020-04-07 18:38:56 +00:00
|
|
|
Queue<Type>::pushBack(Type&& value)
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
for (;;)
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
QueueReturn retVal = tryPushBack(std::move(value));
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
switch (retVal)
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
// Queue disabled.
|
|
|
|
case QueueReturn::QueueDisabled:
|
|
|
|
// We pushed the value back
|
|
|
|
case QueueReturn::Success:
|
|
|
|
return retVal;
|
|
|
|
default:
|
|
|
|
// continue on.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_waitingPushers.fetch_add(1, std::memory_order_relaxed);
|
|
|
|
|
|
|
|
// Sync Point B.
|
|
|
|
//
|
|
|
|
// The call to `full` below loads the push index with full *sequential*
|
|
|
|
// consistency, which gives visibility of the change above to
|
|
|
|
// waiting pushers in Synchronisation Point C.
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (full() && enabled())
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
m_pushSemaphore.wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_waitingPushers.fetch_add(-1, std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2018-11-17 21:07:04 +00:00
|
|
|
Type
|
2020-04-07 18:38:56 +00:00
|
|
|
Queue<Type>::popFront()
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
uint32_t generation = 0;
|
2020-04-07 18:38:56 +00:00
|
|
|
uint32_t index = 0;
|
|
|
|
while (m_manager.reservePopIndex(generation, index) != QueueReturn::Success)
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
m_waitingPoppers.fetch_add(1, std::memory_order_relaxed);
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (empty())
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
m_popSemaphore.wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_waitingPoppers.fetch_sub(1, std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
QueuePopGuard<Type> popGuard(*this, generation, index);
|
2018-11-17 21:07:04 +00:00
|
|
|
return Type(std::move(m_data[index]));
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2020-05-01 19:51:15 +00:00
|
|
|
std::optional<Type>
|
2020-04-07 18:38:56 +00:00
|
|
|
Queue<Type>::popFrontWithTimeout(std::chrono::microseconds timeout)
|
2020-02-28 04:01:14 +00:00
|
|
|
{
|
|
|
|
uint32_t generation = 0;
|
2020-04-07 18:38:56 +00:00
|
|
|
uint32_t index = 0;
|
|
|
|
bool secondTry = false;
|
|
|
|
bool success = false;
|
|
|
|
for (;;)
|
2020-02-28 04:01:14 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
success = m_manager.reservePopIndex(generation, index) == QueueReturn::Success;
|
2020-02-28 04:01:14 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (secondTry || success)
|
2020-03-07 01:20:11 +00:00
|
|
|
break;
|
2020-02-28 04:01:14 +00:00
|
|
|
|
|
|
|
m_waitingPoppers.fetch_add(1, std::memory_order_relaxed);
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (empty())
|
2020-02-28 04:01:14 +00:00
|
|
|
{
|
|
|
|
m_popSemaphore.waitFor(timeout);
|
|
|
|
secondTry = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_waitingPoppers.fetch_sub(1, std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (success)
|
2020-02-28 04:01:14 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
QueuePopGuard<Type> popGuard(*this, generation, index);
|
2020-02-28 04:01:14 +00:00
|
|
|
return Type(std::move(m_data[index]));
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2018-11-17 21:07:04 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
Queue<Type>::removeAll()
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
size_t elemCount = size();
|
|
|
|
|
|
|
|
uint32_t poppedItems = 0;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
while (poppedItems++ < elemCount)
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
uint32_t generation = 0;
|
2020-04-07 18:38:56 +00:00
|
|
|
uint32_t index = 0;
|
2018-11-17 21:07:04 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_manager.reservePopIndex(generation, index) != QueueReturn::Success)
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_data[index].~Type();
|
|
|
|
m_manager.commitPopIndex(generation, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t wakeups = std::min(poppedItems, m_waitingPushers.load());
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
while (wakeups--)
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
m_pushSemaphore.notify();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2018-11-17 21:07:04 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
Queue<Type>::disable()
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
m_manager.disable();
|
|
|
|
|
|
|
|
uint32_t numWaiting = m_waitingPushers;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
while (numWaiting--)
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
m_pushSemaphore.notify();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2018-11-17 21:07:04 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
Queue<Type>::enable()
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
m_manager.enable();
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2018-11-17 21:07:04 +00:00
|
|
|
size_t
|
2020-04-07 18:38:56 +00:00
|
|
|
Queue<Type>::capacity() const
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
return m_manager.capacity();
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2018-11-17 21:07:04 +00:00
|
|
|
size_t
|
2020-04-07 18:38:56 +00:00
|
|
|
Queue<Type>::size() const
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
return m_manager.size();
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2018-11-17 21:07:04 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
Queue<Type>::enabled() const
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
return m_manager.enabled();
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2018-11-17 21:07:04 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
Queue<Type>::full() const
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
return (capacity() <= size());
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2018-11-17 21:07:04 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
Queue<Type>::empty() const
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
return (0 >= size());
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
|
|
|
QueuePushGuard<Type>::~QueuePushGuard()
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_queue)
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
// Thread currently has the cell at index/generation. Dispose of it.
|
|
|
|
|
|
|
|
uint32_t generation = 0;
|
2020-04-07 18:38:56 +00:00
|
|
|
uint32_t index = 0;
|
2018-11-17 21:07:04 +00:00
|
|
|
|
|
|
|
// We should always have at least one item to pop.
|
|
|
|
size_t poppedItems = 1;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
while (m_queue->m_manager.reservePopForClear(generation, index, m_generation, m_index))
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
m_queue->m_data[index].~Type();
|
|
|
|
|
|
|
|
poppedItems++;
|
|
|
|
|
|
|
|
m_queue->m_manager.commitPopIndex(generation, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
// And release
|
|
|
|
|
|
|
|
m_queue->m_manager.abortPushIndexReservation(m_generation, m_index);
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
while (poppedItems--)
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
m_queue->m_pushSemaphore.notify();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2018-11-17 21:07:04 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
QueuePushGuard<Type>::release()
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
m_queue = nullptr;
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
|
|
|
QueuePopGuard<Type>::~QueuePopGuard()
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
m_queue.m_data[m_index].~Type();
|
|
|
|
m_queue.m_manager.commitPopIndex(m_generation, m_index);
|
|
|
|
|
|
|
|
// Notify a pusher
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_queue.m_waitingPushers > 0)
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
m_queue.m_pushSemaphore.notify();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace thread
|
|
|
|
} // namespace llarp
|
|
|
|
|
|
|
|
#endif
|