-
Notifications
You must be signed in to change notification settings - Fork 3
refactor(util): replace lock-free SPSCQueue with mutex-guarded ring buffer #145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,115 +1,86 @@ | ||
| #pragma once | ||
|
|
||
| #include <algorithm> | ||
| #include <atomic> | ||
| #include <cstddef> | ||
| #include <cstdlib> | ||
| #include <memory> | ||
| #include <mutex> | ||
| #include <optional> | ||
| #include <stdexcept> | ||
| #include <type_traits> | ||
| #include <vector> | ||
|
|
||
| namespace goggles::util { | ||
|
|
||
| /// @brief Single-producer, single-consumer lock-free ring buffer. | ||
| /// @brief Single-producer, single-consumer queue with fixed capacity. | ||
| /// | ||
| /// `capacity` must be a power of two. Construction throws on invalid capacity or allocation | ||
| /// failure. | ||
| /// Capacity must be > 0. Construction throws `std::invalid_argument` on zero capacity. | ||
| template <typename T> | ||
| class SPSCQueue { | ||
| public: | ||
| explicit SPSCQueue(size_t capacity) | ||
| : m_capacity(capacity), m_buffer_size(capacity * 2), m_capacity_mask(m_buffer_size - 1), | ||
| m_buffer(nullptr) { | ||
| // Power-of-2 required for efficient modulo via bitwise AND | ||
| if (capacity == 0 || (capacity & (capacity - 1)) != 0) { | ||
| throw std::invalid_argument("SPSCQueue capacity must be power of 2"); | ||
| : m_capacity(capacity), m_buffer(capacity), m_head(0), m_tail(0), m_size(0) { | ||
| if (capacity == 0) { | ||
| throw std::invalid_argument("SPSCQueue capacity must be > 0"); | ||
| } | ||
|
|
||
| m_buffer = static_cast<T*>(std::aligned_alloc(alignof(T), sizeof(T) * m_buffer_size)); | ||
| if (m_buffer == nullptr) { | ||
| throw std::bad_alloc(); | ||
| } | ||
|
|
||
| m_head.store(0, std::memory_order_relaxed); | ||
| m_tail.store(0, std::memory_order_relaxed); | ||
| } | ||
|
|
||
| ~SPSCQueue() { | ||
| while (try_pop()) { | ||
| } | ||
| if (m_buffer) { | ||
| std::free(m_buffer); | ||
| } | ||
| } | ||
| ~SPSCQueue() = default; | ||
|
|
||
| SPSCQueue(const SPSCQueue&) = delete; | ||
| SPSCQueue& operator=(const SPSCQueue&) = delete; | ||
| SPSCQueue(SPSCQueue&&) = delete; | ||
| SPSCQueue& operator=(SPSCQueue&&) = delete; | ||
|
|
||
| auto try_push(const T& item) -> bool { | ||
| const size_t current_head = m_head.load(std::memory_order_relaxed); | ||
| const size_t current_tail = m_tail.load(std::memory_order_acquire); | ||
| const size_t current_size = (current_head - current_tail) & m_capacity_mask; | ||
| if (current_size >= m_capacity) { | ||
| std::lock_guard lock(m_mutex); | ||
| if (m_size >= m_capacity) { | ||
| return false; | ||
| } | ||
|
|
||
| new (&m_buffer[current_head]) T(item); | ||
| const size_t next_head = (current_head + 1) & m_capacity_mask; | ||
| m_head.store(next_head, std::memory_order_release); | ||
| m_buffer[m_head].emplace(item); | ||
| m_head = (m_head + 1) % m_capacity; | ||
| ++m_size; | ||
| return true; | ||
| } | ||
|
|
||
| auto try_push(T&& item) -> bool { | ||
| const size_t current_head = m_head.load(std::memory_order_relaxed); | ||
| const size_t current_tail = m_tail.load(std::memory_order_acquire); | ||
| const size_t current_size = (current_head - current_tail) & m_capacity_mask; | ||
| if (current_size >= m_capacity) { | ||
| std::lock_guard lock(m_mutex); | ||
| if (m_size >= m_capacity) { | ||
| return false; | ||
| } | ||
|
|
||
| new (&m_buffer[current_head]) T(std::move(item)); | ||
| const size_t next_head = (current_head + 1) & m_capacity_mask; | ||
| m_head.store(next_head, std::memory_order_release); | ||
| m_buffer[m_head].emplace(std::move(item)); | ||
| m_head = (m_head + 1) % m_capacity; | ||
| ++m_size; | ||
|
Comment on lines
42
to
+49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2. Missing include src/util/queues.hpp uses std::move but does not include <utility>, making the header non-self-contained and potentially failing to compile on some toolchains/stdlib implementations. Agent Prompt
|
||
| return true; | ||
| } | ||
|
|
||
| auto try_pop() -> std::optional<T> { | ||
| const size_t current_tail = m_tail.load(std::memory_order_relaxed); | ||
| if (current_tail == m_head.load(std::memory_order_acquire)) { | ||
| std::lock_guard lock(m_mutex); | ||
| if (m_size == 0) { | ||
| return std::nullopt; | ||
| } | ||
|
|
||
| T item = std::move(m_buffer[current_tail]); | ||
| m_buffer[current_tail].~T(); | ||
| const size_t next_tail = (current_tail + 1) & m_capacity_mask; | ||
| m_tail.store(next_tail, std::memory_order_release); | ||
| std::optional<T> item = std::move(m_buffer[m_tail]); | ||
| m_buffer[m_tail].reset(); | ||
| m_tail = (m_tail + 1) % m_capacity; | ||
| --m_size; | ||
| return item; | ||
| } | ||
|
|
||
| [[nodiscard]] auto size() const -> size_t { | ||
| const size_t current_head = m_head.load(std::memory_order_acquire); | ||
| const size_t current_tail = m_tail.load(std::memory_order_acquire); | ||
| return (current_head - current_tail) & m_capacity_mask; | ||
| std::lock_guard lock(m_mutex); | ||
| return m_size; | ||
| } | ||
|
|
||
| [[nodiscard]] auto empty() const -> bool { | ||
| const size_t current_tail = m_tail.load(std::memory_order_relaxed); | ||
| return current_tail == m_head.load(std::memory_order_acquire); | ||
| std::lock_guard lock(m_mutex); | ||
| return m_size == 0; | ||
| } | ||
|
|
||
| [[nodiscard]] auto capacity() const -> size_t { return m_capacity; } | ||
|
|
||
| private: | ||
| alignas(64) std::atomic<size_t> m_head; | ||
| alignas(64) std::atomic<size_t> m_tail; | ||
|
|
||
| const size_t m_capacity; | ||
| const size_t m_buffer_size; | ||
| const size_t m_capacity_mask; | ||
| T* m_buffer; | ||
| std::vector<std::optional<T>> m_buffer; | ||
| size_t m_head; | ||
| size_t m_tail; | ||
| size_t m_size; | ||
| mutable std::mutex m_mutex; | ||
| }; | ||
|
|
||
| } // namespace goggles::util | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1. Tests missing required includes
🐞 Bug✓ CorrectnessAgent Prompt
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools