Skip to content

Truly independent web browser

AK: Remove unused SingleProducerCircularQueue

Andreas Kling6527546f41a2
diff --git a/AK/SingleProducerCircularQueue.h b/AK/SingleProducerCircularQueue.h
deleted file mode 100644
index 118fbd8fba5..00000000000
--- a/AK/SingleProducerCircularQueue.h
+++ /dev/null
@@ -1,177 +0,0 @@
-/*
- * Copyright (c) 2022, kleines Filmröllchen <[email protected]>
- * Copyright (c) 2024, stasoid <[email protected]>
- * Copyright (c) 2026-present, the Ladybird developers.
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <AK/Array.h>
-#include <AK/Assertions.h>
-#include <AK/Atomic.h>
-#include <AK/BuiltinWrappers.h>
-#include <AK/Error.h>
-#include <AK/Function.h>
-#include <AK/StdLibExtras.h>
-#include <AK/Types.h>
-
-namespace AK {
-
-// The shared state of a single-producer single-consumer circular queue: the head/tail counters and
-// the slot storage, owned by a SingleProducerCircularQueueBase subclass.
-template<typename T, size_t Size>
-// Size must be a power of two, which speeds up the modulus operations for indexing.
-requires(popcount(Size) == 1)
-struct SPCQData {
- SPCQData() = default;
- SPCQData(SPCQData const&) = delete;
- SPCQData(SPCQData&&) = delete;
- ~SPCQData() = default;
-
- // Invariant: tail >= head
- // Invariant: head and tail are monotonically increasing
- // Invariant: tail always points to the next free location where an enqueue can happen.
- // Invariant: head always points to the element to be dequeued next.
- // Invariant: tail is only modified by enqueue functions.
- // Invariant: head is only modified by dequeue functions.
- // An empty queue is signalled with: tail = head
- // A full queue is signalled with: tail - head = size
- AK_CACHE_ALIGNED Atomic<size_t> m_tail { 0 };
- AK_CACHE_ALIGNED Atomic<size_t> m_head { 0 };
-
- alignas(T) Array<T, Size> m_data;
-};
-
-template<typename T, size_t Size>
-class SingleProducerCircularQueueBase {
-public:
- using ValueType = T;
-
- enum class QueueStatus : u8 {
- Invalid = 0,
- Full,
- Empty,
- };
-
- constexpr size_t size() const { return Size; }
-
- // These are provably inconsistent and should only be used as hints to the actual capacity/used count.
- ALWAYS_INLINE size_t weak_remaining_capacity() const { return Size - weak_used(); }
- ALWAYS_INLINE size_t weak_used() const
- {
- auto volatile tail = m_data->m_tail.load(AK::MemoryOrder::memory_order_relaxed);
- auto volatile head = m_data->m_head.load(AK::MemoryOrder::memory_order_relaxed);
- return tail - head;
- }
- ALWAYS_INLINE size_t weak_head() const { return m_data->m_head.load(AK::MemoryOrder::memory_order_relaxed); }
- ALWAYS_INLINE size_t weak_tail() const { return m_data->m_tail.load(AK::MemoryOrder::memory_order_relaxed); }
-
- template<typename WriteToSlot>
- ErrorOr<void, QueueStatus> enqueue_in_place(WriteToSlot write_to_slot)
- {
- VERIFY(m_data);
- auto tail = m_data->m_tail.load(AK::MemoryOrder::memory_order_relaxed);
- if (tail - m_data->m_head.load(AK::MemoryOrder::memory_order_acquire) == Size)
- return QueueStatus::Full;
- write_to_slot(m_data->m_data[tail % Size]);
- m_data->m_tail.store(tail + 1, AK::MemoryOrder::memory_order_release);
-
- return {};
- }
-
- ErrorOr<void, QueueStatus> enqueue(ValueType to_insert)
- {
- return enqueue_in_place([&](ValueType& slot) {
- slot = move(to_insert);
- });
- }
-
- ALWAYS_INLINE bool can_enqueue() const
- {
- return m_data->m_tail.load(AK::MemoryOrder::memory_order_relaxed) - m_data->m_head.load(AK::MemoryOrder::memory_order_relaxed) < Size;
- }
-
- // Repeatedly try to enqueue, calling wait_function to block while the queue is full.
- ErrorOr<void> blocking_enqueue(ValueType to_insert, Function<void()> wait_function)
- {
- while (true) {
- auto result = enqueue(to_insert);
- if (!result.is_error())
- return {};
- if (result.error() != QueueStatus::Full)
- return Error::from_string_literal("Unexpected error while enqueuing");
-
- wait_function();
- }
- }
-
- Optional<ValueType> peek() const
- {
- VERIFY(m_data);
- auto head = m_data->m_head.load(AK::MemoryOrder::memory_order_relaxed);
- if (head == m_data->m_tail.load(AK::MemoryOrder::memory_order_acquire))
- return {};
- return m_data->m_data[head % Size];
- }
-
- // Only the consumer advances head, so the newest slot cannot be overwritten during a consumer-side read.
- Optional<ValueType> peek_newest() const
- {
- VERIFY(m_data);
- auto tail = m_data->m_tail.load(AK::MemoryOrder::memory_order_acquire);
- if (m_data->m_head.load(AK::MemoryOrder::memory_order_relaxed) == tail)
- return {};
- return m_data->m_data[(tail - 1) % Size];
- }
-
- ErrorOr<ValueType, QueueStatus> dequeue()
- {
- VERIFY(m_data);
- auto head = m_data->m_head.load(AK::MemoryOrder::memory_order_relaxed);
- if (head == m_data->m_tail.load(AK::MemoryOrder::memory_order_acquire))
- return QueueStatus::Empty;
- auto data = move(m_data->m_data[head % Size]);
- m_data->m_head.store(head + 1, AK::MemoryOrder::memory_order_release);
- return { move(data) };
- }
-
- size_t head() const { return m_data->m_head.load(AK::MemoryOrder::memory_order_relaxed); }
-
-protected:
- SingleProducerCircularQueueBase() = default;
- explicit SingleProducerCircularQueueBase(SPCQData<T, Size>& data)
- : m_data(&data)
- {
- }
-
- void set_data(SPCQData<T, Size>* data) { m_data = data; }
-
-private:
- SPCQData<T, Size>* m_data { nullptr };
-};
-
-// An lock-free SPSC queue that owns its slot storage inline.
-template<typename T, size_t Size = 32>
-class SingleProducerCircularQueue final : public SingleProducerCircularQueueBase<T, Size> {
- AK_MAKE_NONCOPYABLE(SingleProducerCircularQueue);
- AK_MAKE_NONMOVABLE(SingleProducerCircularQueue);
-
-public:
- SingleProducerCircularQueue()
- : SingleProducerCircularQueueBase<T, Size>(m_storage)
- {
- }
-
-private:
- SPCQData<T, Size> m_storage;
-};
-
-}
-
-#if USING_AK_GLOBALLY
-using AK::SingleProducerCircularQueue;
-using AK::SingleProducerCircularQueueBase;
-using AK::SPCQData;
-#endif
diff --git a/Libraries/LibAudio/AudioFrameRingBase.h b/Libraries/LibAudio/AudioFrameRingBase.h
index a0d85d7e350..0b27f4edf5a 100644
--- a/Libraries/LibAudio/AudioFrameRingBase.h
+++ b/Libraries/LibAudio/AudioFrameRingBase.h
@@ -14,7 +14,7 @@
namespace Audio {
-// Invariants (see AK/SingleProducerCircularQueue.h):
+// Invariants:
// - head and tail are monotonically increasing frame counters, with tail >= head.
// - tail is only written by the producer; head is only written by the consumer.
// - tail - head is the number of frames in the ring, at most frame_capacity.
@@ -26,7 +26,7 @@ struct AudioFrameRingHeader {
};
// The lock-free single-producer/single-consumer discipline of a ring of interleaved float32 frames, over a header
-// and sample storage owned by the derived class, in the manner of AK/SingleProducerCircularQueueBase.
+// and sample storage owned by the derived class.
class AUDIO_API AudioFrameRingBase {
public:
u32 channel_count() const { return m_channel_count; }
diff --git a/Libraries/LibWeb/WebAudio/Rendering/AudioWorkletPipe.h b/Libraries/LibWeb/WebAudio/Rendering/AudioWorkletPipe.h
index 0c56a4e3b6f..43b13c2cb3f 100644
--- a/Libraries/LibWeb/WebAudio/Rendering/AudioWorkletPipe.h
+++ b/Libraries/LibWeb/WebAudio/Rendering/AudioWorkletPipe.h
@@ -22,8 +22,8 @@
namespace Web::WebAudio::Rendering {
// A lock-free bridge between the realtime render node and the main-thread processor. Two SPSC rings share a
-// preallocated arena; underruns render silence and a full input ring drops quanta. Its acquire/release head-tail
-// discipline follows AK/SingleProducerCircularQueue.h, but the slot stride is configured at run time.
+// preallocated arena; underruns render silence and a full input ring drops quanta. Each ring uses acquire/release
+// head-tail discipline, and the slot stride is configured at run time.
//
// Per-slot layout; strides are rounded up to 64 bytes so slots start cache-line-aligned relative to the arena:
//
diff --git a/Tests/AK/CMakeLists.txt b/Tests/AK/CMakeLists.txt
index 84081805451..38b2d5966a3 100644
--- a/Tests/AK/CMakeLists.txt
+++ b/Tests/AK/CMakeLists.txt
@@ -65,7 +65,6 @@ set(AK_TEST_SOURCES
TestSeqLock.cpp
TestSetUnion.cpp
TestSharedMemoryValue.cpp
- TestSingleProducerCircularQueue.cpp
TestSinglyLinkedList.cpp
TestSourceGenerator.cpp
TestSourceLocation.cpp
diff --git a/Tests/AK/TestSingleProducerCircularQueue.cpp b/Tests/AK/TestSingleProducerCircularQueue.cpp
deleted file mode 100644
index e89dc234b4c..00000000000
--- a/Tests/AK/TestSingleProducerCircularQueue.cpp
+++ /dev/null
@@ -1,142 +0,0 @@
-/*
- * Copyright (c) 2026-present, the Ladybird developers.
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <AK/RefCounted.h>
-#include <AK/RefPtr.h>
-#include <AK/SingleProducerCircularQueue.h>
-#include <LibTest/TestCase.h>
-
-using IntQueue = AK::SingleProducerCircularQueue<int, 4>;
-
-TEST_CASE(fifo_order)
-{
- IntQueue queue;
- for (int i = 0; i < 4; ++i)
- MUST(queue.enqueue(i));
- for (int i = 0; i < 4; ++i)
- EXPECT_EQ(MUST(queue.dequeue()), i);
-}
-
-TEST_CASE(full_and_empty)
-{
- IntQueue queue;
-
- auto empty = queue.dequeue();
- EXPECT(empty.is_error());
- EXPECT_EQ(empty.release_error(), IntQueue::QueueStatus::Empty);
-
- for (int i = 0; i < 4; ++i)
- MUST(queue.enqueue(i));
-
- auto full = queue.enqueue(99);
- EXPECT(full.is_error());
- EXPECT_EQ(full.release_error(), IntQueue::QueueStatus::Full);
-}
-
-TEST_CASE(enqueue_in_place)
-{
- IntQueue queue;
- for (int i = 0; i < 4; ++i)
- MUST(queue.enqueue_in_place([&](int& slot) { slot = i; }));
- for (int i = 0; i < 4; ++i)
- EXPECT_EQ(MUST(queue.dequeue()), i);
-}
-
-TEST_CASE(peek_does_not_consume)
-{
- IntQueue queue;
- EXPECT(!queue.peek().has_value());
-
- MUST(queue.enqueue(7));
- MUST(queue.enqueue(8));
- EXPECT_EQ(queue.peek().value(), 7);
- EXPECT_EQ(queue.peek().value(), 7);
-
- EXPECT_EQ(MUST(queue.dequeue()), 7);
- EXPECT_EQ(queue.peek().value(), 8);
-}
-
-namespace {
-
-struct Tracked : public RefCounted<Tracked> {
- explicit Tracked(int value)
- : value(value)
- {
- ++s_live;
- }
- ~Tracked() { --s_live; }
-
- int value { 0 };
- static inline int s_live { 0 };
-};
-
-}
-
-// The shared-memory queue forbids non-trivially-copyable elements; the in-process queue must
-// construct references on enqueue and destroy them on dequeue and teardown, with no leaks.
-TEST_CASE(non_pod_items_are_held_then_moved_out)
-{
- EXPECT_EQ(Tracked::s_live, 0);
- {
- AK::SingleProducerCircularQueue<RefPtr<Tracked>, 4> queue;
-
- auto item = adopt_ref(*new Tracked(7));
- EXPECT_EQ(Tracked::s_live, 1);
- EXPECT_EQ(item->ref_count(), 1u);
-
- MUST(queue.enqueue(item));
- // Enqueuing takes the queue's own reference into the slot.
- EXPECT_EQ(item->ref_count(), 2u);
- EXPECT_EQ(Tracked::s_live, 1);
-
- auto dequeued = MUST(queue.dequeue());
- // Dequeue moves the slot's reference out rather than copying it, so the count is unchanged.
- EXPECT_EQ(dequeued->value, 7);
- EXPECT_EQ(item->ref_count(), 2u);
- }
- EXPECT_EQ(Tracked::s_live, 0);
-}
-
-TEST_CASE(non_pod_items_are_released_when_the_queue_is_destroyed)
-{
- EXPECT_EQ(Tracked::s_live, 0);
- {
- AK::SingleProducerCircularQueue<RefPtr<Tracked>, 4> queue;
- for (int i = 0; i < 3; ++i)
- MUST(queue.enqueue(adopt_ref(*new Tracked(i))));
- EXPECT_EQ(Tracked::s_live, 3);
- // The queue goes out of scope with items still enqueued; its slots must release them.
- }
- EXPECT_EQ(Tracked::s_live, 0);
-}
-
-namespace {
-
-struct MoveOnly {
- MoveOnly() = default;
- explicit MoveOnly(int value)
- : value(value)
- {
- }
- MoveOnly(MoveOnly&&) = default;
- MoveOnly& operator=(MoveOnly&&) = default;
- MoveOnly(MoveOnly const&) = delete;
- MoveOnly& operator=(MoveOnly const&) = delete;
-
- int value { 0 };
-};
-
-}
-
-// A move-only element type is likewise unusable in the shared version; it must round-trip here.
-TEST_CASE(move_only_items)
-{
- AK::SingleProducerCircularQueue<MoveOnly, 4> queue;
- for (int i = 0; i < 4; ++i)
- MUST(queue.enqueue(MoveOnly(i)));
- for (int i = 0; i < 4; ++i)
- EXPECT_EQ(MUST(queue.dequeue()).value, i);
-}