summaryrefslogtreecommitdiff
path: root/Source/UnitTests/Common/SPSCQueueTest.cpp
diff options
context:
space:
mode:
authorJordan Woyak <jordan.woyak@gmail.com>2025-03-12 03:21:44 -0500
committerJordan Woyak <jordan.woyak@gmail.com>2025-04-22 23:49:32 -0500
commitaf960651e81edf9fd57aba8e19857a5aa48ca184 (patch)
tree9a93a6dd269a8a54dd8de359b96e3ec3a57afa5c /Source/UnitTests/Common/SPSCQueueTest.cpp
parentd04e9e79a6c8ab59f8d273664c95e35e30354de7 (diff)
Common: SPSCQueue cleanups and improvements.
Diffstat (limited to 'Source/UnitTests/Common/SPSCQueueTest.cpp')
-rw-r--r--Source/UnitTests/Common/SPSCQueueTest.cpp43
1 files changed, 32 insertions, 11 deletions
diff --git a/Source/UnitTests/Common/SPSCQueueTest.cpp b/Source/UnitTests/Common/SPSCQueueTest.cpp
index 1673395027..93b3e584fd 100644
--- a/Source/UnitTests/Common/SPSCQueueTest.cpp
+++ b/Source/UnitTests/Common/SPSCQueueTest.cpp
@@ -2,8 +2,11 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#include <gtest/gtest.h>
+
+#include <memory>
#include <thread>
+#include "Common/CommonTypes.h"
#include "Common/SPSCQueue.h"
TEST(SPSCQueue, Simple)
@@ -44,21 +47,36 @@ TEST(SPSCQueue, Simple)
TEST(SPSCQueue, MultiThreaded)
{
- Common::SPSCQueue<u32> q;
+ struct Foo
+ {
+ std::shared_ptr<int> ptr;
+ u32 i;
+ };
+
+ // A shared_ptr held by every element in the queue.
+ auto sptr = std::make_shared<int>(0);
- auto inserter = [&q]() {
- for (u32 i = 0; i < 100000; ++i)
- q.Push(i);
+ auto queue_ptr = std::make_unique<Common::WaitableSPSCQueue<Foo>>();
+ auto& q = *queue_ptr;
+
+ constexpr u32 reps = 100000;
+
+ auto inserter = [&]() {
+ for (u32 i = 0; i != reps; ++i)
+ q.Push({sptr, i});
+
+ q.WaitForEmpty();
+ EXPECT_EQ(sptr.use_count(), 1);
+ q.Push({sptr, 0});
+ EXPECT_EQ(sptr.use_count(), 2);
};
- auto popper = [&q]() {
- for (u32 i = 0; i < 100000; ++i)
+ auto popper = [&]() {
+ for (u32 i = 0; i != reps; ++i)
{
- while (q.Empty())
- ;
- u32 v;
- q.Pop(v);
- EXPECT_EQ(i, v);
+ q.WaitForData();
+ EXPECT_EQ(i, q.Front().i);
+ q.Pop();
}
};
@@ -67,4 +85,7 @@ TEST(SPSCQueue, MultiThreaded)
popper_thread.join();
inserter_thread.join();
+
+ queue_ptr.reset();
+ EXPECT_EQ(sptr.use_count(), 1);
}