early-access version 3466

This commit is contained in:
pineappleEA
2023-03-19 22:38:32 +01:00
parent 587f4c8b2f
commit 6cdaf33559
6 changed files with 224 additions and 127 deletions

View File

@@ -24,112 +24,53 @@ class SPSCQueue {
public:
bool TryPush(T&& t) {
const size_t write_index = m_write_index.load();
// Check if we have free slots to write to.
if ((write_index - m_read_index.load()) == Capacity) {
return false;
}
// Determine the position to write to.
const size_t pos = write_index % Capacity;
// Push into the queue.
m_data[pos] = std::move(t);
// Increment the write index.
++m_write_index;
// Notify the consumer that we have pushed into the queue.
std::scoped_lock lock{cv_mutex};
cv.notify_one();
return true;
return Push<PushMode::Try>(std::move(t));
}
template <typename... Args>
bool TryPush(Args&&... args) {
const size_t write_index = m_write_index.load();
// Check if we have free slots to write to.
if ((write_index - m_read_index.load()) == Capacity) {
return false;
}
// Determine the position to write to.
const size_t pos = write_index % Capacity;
// Emplace into the queue.
std::construct_at(std::addressof(m_data[pos]), std::forward<Args>(args)...);
// Increment the write index.
++m_write_index;
// Notify the consumer that we have pushed into the queue.
std::scoped_lock lock{cv_mutex};
cv.notify_one();
return true;
bool TryEmplace(Args&&... args) {
return Emplace<PushMode::Try>(std::forward<Args>(args)...);
}
void Push(T&& t) {
const size_t write_index = m_write_index.load();
// Wait until we have free slots to write to.
while ((write_index - m_read_index.load()) == Capacity) {
std::this_thread::yield();
}
// Determine the position to write to.
const size_t pos = write_index % Capacity;
// Push into the queue.
m_data[pos] = std::move(t);
// Increment the write index.
++m_write_index;
// Notify the consumer that we have pushed into the queue.
std::scoped_lock lock{cv_mutex};
cv.notify_one();
void PushWait(T&& t) {
Push<PushMode::Wait>(std::move(t));
}
template <typename... Args>
void Push(Args&&... args) {
const size_t write_index = m_write_index.load();
void EmplaceWait(Args&&... args) {
Emplace<PushMode::Wait>(std::forward<Args>(args)...);
}
// Wait until we have free slots to write to.
while ((write_index - m_read_index.load()) == Capacity) {
std::this_thread::yield();
}
void PushOverwrite(T&& t) {
Push<PushMode::Overwrite>(std::move(t));
}
// Determine the position to write to.
const size_t pos = write_index % Capacity;
// Emplace into the queue.
std::construct_at(std::addressof(m_data[pos]), std::forward<Args>(args)...);
// Increment the write index.
++m_write_index;
// Notify the consumer that we have pushed into the queue.
std::scoped_lock lock{cv_mutex};
cv.notify_one();
template <typename... Args>
void EmplaceOverwrite(Args&&... args) {
Emplace<PushMode::Overwrite>(std::forward<Args>(args)...);
}
bool TryPop(T& t) {
return Pop(t);
return Pop<PopMode::Try>(t);
}
void PopWait(T& t) {
Pop<PopMode::Wait>(t);
}
void PopWait(T& t, std::stop_token stop_token) {
Wait(stop_token);
Pop(t);
Pop<PopMode::WaitWithStopToken>(t, stop_token);
}
T PopWait() {
T t;
Pop<PopMode::Wait>(t);
return t;
}
T PopWait(std::stop_token stop_token) {
Wait(stop_token);
T t;
Pop(t);
Pop<PopMode::WaitWithStopToken>(t, stop_token);
return t;
}
@@ -148,6 +89,102 @@ public:
}
private:
enum class PushMode {
Try,
Wait,
Overwrite,
Count,
};
enum class PopMode {
Try,
Wait,
WaitWithStopToken,
Count,
};
template <PushMode Mode>
bool Push(T&& t) {
const size_t write_index = m_write_index.load();
if constexpr (Mode == PushMode::Try) {
// Check if we have free slots to write to.
if ((write_index - m_read_index.load()) == Capacity) {
return false;
}
} else if constexpr (Mode == PushMode::Wait) {
// Wait until we have free slots to write to.
std::unique_lock lock{producer_cv_mutex};
producer_cv.wait(lock, [this, write_index] {
return (write_index - m_read_index.load()) < Capacity;
});
} else if constexpr (Mode == PushMode::Overwrite) {
// Check if we have free slots to write to.
if ((write_index - m_read_index.load()) == Capacity) {
// If we don't, increment the read index. This is effectively a pop operation.
++m_read_index;
}
} else {
static_assert(Mode < PushMode::Count, "Invalid PushMode.");
}
// Determine the position to write to.
const size_t pos = write_index % Capacity;
// Push into the queue.
m_data[pos] = std::move(t);
// Increment the write index.
++m_write_index;
// Notify the consumer that we have pushed into the queue.
std::scoped_lock lock{consumer_cv_mutex};
consumer_cv.notify_one();
return true;
}
template <PushMode Mode, typename... Args>
bool Emplace(Args&&... args) {
const size_t write_index = m_write_index.load();
if constexpr (Mode == PushMode::Try) {
// Check if we have free slots to write to.
if ((write_index - m_read_index.load()) == Capacity) {
return false;
}
} else if constexpr (Mode == PushMode::Wait) {
// Wait until we have free slots to write to.
std::unique_lock lock{producer_cv_mutex};
producer_cv.wait(lock, [this, write_index] {
return (write_index - m_read_index.load()) < Capacity;
});
} else if constexpr (Mode == PushMode::Overwrite) {
// Check if we have free slots to write to.
if ((write_index - m_read_index.load()) == Capacity) {
// If we don't, increment the read index. This is effectively a pop operation.
++m_read_index;
}
} else {
static_assert(Mode < PushMode::Count, "Invalid PushMode.");
}
// Determine the position to write to.
const size_t pos = write_index % Capacity;
// Emplace into the queue.
std::construct_at(std::addressof(m_data[pos]), std::forward<Args>(args)...);
// Increment the write index.
++m_write_index;
// Notify the consumer that we have pushed into the queue.
std::scoped_lock lock{consumer_cv_mutex};
consumer_cv.notify_one();
return true;
}
void Pop() {
const size_t read_index = m_read_index.load();
@@ -164,14 +201,33 @@ private:
// Increment the read index.
++m_read_index;
// Notify the producer that we have popped off the queue.
std::unique_lock lock{producer_cv_mutex};
producer_cv.notify_one();
}
bool Pop(T& t) {
template <PopMode Mode>
bool Pop(T& t, [[maybe_unused]] std::stop_token stop_token = {}) {
const size_t read_index = m_read_index.load();
// Check if the queue is empty.
if (read_index == m_write_index.load()) {
return false;
if constexpr (Mode == PopMode::Try) {
// Check if the queue is empty.
if (read_index == m_write_index.load()) {
return false;
}
} else if constexpr (Mode == PopMode::Wait) {
// Wait until the queue is not empty.
std::unique_lock lock{consumer_cv_mutex};
consumer_cv.wait(lock,
[this, read_index] { return read_index != m_write_index.load(); });
} else if constexpr (Mode == PopMode::WaitWithStopToken) {
// Wait until the queue is not empty.
std::unique_lock lock{consumer_cv_mutex};
Common::CondvarWait(consumer_cv, lock, stop_token,
[this, read_index] { return read_index != m_write_index.load(); });
} else {
static_assert(Mode < PopMode::Count, "Invalid PopMode.");
}
// Determine the position to read from.
@@ -183,12 +239,11 @@ private:
// Increment the read index.
++m_read_index;
return true;
}
// Notify the producer that we have popped off the queue.
std::unique_lock lock{producer_cv_mutex};
producer_cv.notify_one();
void Wait(std::stop_token stop_token) {
std::unique_lock lock{cv_mutex};
Common::CondvarWait(cv, lock, stop_token, [this] { return !Empty(); });
return true;
}
#ifdef __cpp_lib_hardware_interference_size
@@ -201,43 +256,64 @@ private:
std::array<T, Capacity> m_data;
std::condition_variable_any cv;
std::mutex cv_mutex;
std::condition_variable_any producer_cv;
std::mutex producer_cv_mutex;
std::condition_variable_any consumer_cv;
std::mutex consumer_cv_mutex;
};
template <typename T, size_t Capacity = detail::DefaultCapacity>
class MPSCQueue {
public:
void TryPush(T&& t) {
bool TryPush(T&& t) {
std::scoped_lock lock{write_mutex};
spsc_queue.TryPush(std::move(t));
return spsc_queue.TryPush(std::move(t));
}
template <typename... Args>
void TryPush(Args&&... args) {
bool TryEmplace(Args&&... args) {
std::scoped_lock lock{write_mutex};
spsc_queue.TryPush(std::forward<Args>(args)...);
return spsc_queue.TryEmplace(std::forward<Args>(args)...);
}
void Push(T&& t) {
void PushWait(T&& t) {
std::scoped_lock lock{write_mutex};
spsc_queue.Push(std::move(t));
spsc_queue.PushWait(std::move(t));
}
template <typename... Args>
void Push(Args&&... args) {
void EmplaceWait(Args&&... args) {
std::scoped_lock lock{write_mutex};
spsc_queue.Push(std::forward<Args>(args)...);
spsc_queue.EmplaceWait(std::forward<Args>(args)...);
}
void PushOverwrite(T&& t) {
std::scoped_lock lock{write_mutex};
spsc_queue.PushOverwrite(std::move(t));
}
template <typename... Args>
void EmplaceOverwrite(Args&&... args) {
std::scoped_lock lock{write_mutex};
spsc_queue.EmplaceOverwrite(std::forward<Args>(args)...);
}
bool TryPop(T& t) {
return spsc_queue.TryPop(t);
}
void PopWait(T& t) {
spsc_queue.PopWait(t);
}
void PopWait(T& t, std::stop_token stop_token) {
spsc_queue.PopWait(t, stop_token);
}
T PopWait() {
return spsc_queue.PopWait();
}
T PopWait(std::stop_token stop_token) {
return spsc_queue.PopWait(stop_token);
}
@@ -262,26 +338,37 @@ private:
template <typename T, size_t Capacity = detail::DefaultCapacity>
class MPMCQueue {
public:
void TryPush(T&& t) {
bool TryPush(T&& t) {
std::scoped_lock lock{write_mutex};
spsc_queue.TryPush(std::move(t));
return spsc_queue.TryPush(std::move(t));
}
template <typename... Args>
void TryPush(Args&&... args) {
bool TryEmplace(Args&&... args) {
std::scoped_lock lock{write_mutex};
spsc_queue.TryPush(std::forward<Args>(args)...);
return spsc_queue.TryEmplace(std::forward<Args>(args)...);
}
void Push(T&& t) {
void PushWait(T&& t) {
std::scoped_lock lock{write_mutex};
spsc_queue.Push(std::move(t));
spsc_queue.PushWait(std::move(t));
}
template <typename... Args>
void Push(Args&&... args) {
void EmplaceWait(Args&&... args) {
std::scoped_lock lock{write_mutex};
spsc_queue.Push(std::forward<Args>(args)...);
spsc_queue.EmplaceWait(std::forward<Args>(args)...);
}
void PushOverwrite(T&& t) {
std::scoped_lock lock{write_mutex};
spsc_queue.PushOverwrite(std::move(t));
}
template <typename... Args>
void EmplaceOverwrite(Args&&... args) {
std::scoped_lock lock{write_mutex};
spsc_queue.EmplaceOverwrite(std::forward<Args>(args)...);
}
bool TryPop(T& t) {
@@ -289,11 +376,21 @@ public:
return spsc_queue.TryPop(t);
}
void PopWait(T& t) {
std::scoped_lock lock{read_mutex};
spsc_queue.PopWait(t);
}
void PopWait(T& t, std::stop_token stop_token) {
std::scoped_lock lock{read_mutex};
spsc_queue.PopWait(t, stop_token);
}
T PopWait() {
std::scoped_lock lock{read_mutex};
return spsc_queue.PopWait();
}
T PopWait(std::stop_token stop_token) {
std::scoped_lock lock{read_mutex};
return spsc_queue.PopWait(stop_token);