diff --git a/src/MPMCQueueTest.cpp b/src/MPMCQueueTest.cpp index ccdf38f..72ab5e3 100644 --- a/src/MPMCQueueTest.cpp +++ b/src/MPMCQueueTest.cpp @@ -25,6 +25,10 @@ } \ } while (false) +namespace { + +using rigtorp::MPMCQueue; + // TestType tracks correct usage of constructors and destructors struct TestType { static std::set constructed; @@ -100,12 +104,7 @@ template struct TestAllocator { AllocationState &state; }; -int main(int argc, char *argv[]) { - (void)argc; - (void)argv; - - using namespace rigtorp; - +void testElementLifetime() { { MPMCQueue q(11); CHECK(q.size() == 0 && q.empty()); @@ -126,129 +125,161 @@ int main(int argc, char *argv[]) { CHECK(TestType::constructed.size() == 10); } CHECK(TestType::constructed.size() == 0); +} - { - MPMCQueue q(1); - int t = 0; - CHECK(q.try_push(1) == true); - CHECK(q.size() == 1 && !q.empty()); - CHECK(q.try_push(2) == false); - CHECK(q.size() == 1 && !q.empty()); - CHECK(q.try_pop(t) == true && t == 1); - CHECK(q.size() == 0 && q.empty()); - CHECK(q.try_pop(t) == false && t == 1); - CHECK(q.size() == 0 && q.empty()); +void testFullAndEmpty() { + MPMCQueue q(1); + int t = 0; + CHECK(q.try_push(1) == true); + CHECK(q.size() == 1 && !q.empty()); + CHECK(q.try_push(2) == false); + CHECK(q.size() == 1 && !q.empty()); + CHECK(q.try_pop(t) == true && t == 1); + CHECK(q.size() == 0 && q.empty()); + CHECK(q.try_pop(t) == false && t == 1); + CHECK(q.size() == 0 && q.empty()); +} + +void testCopyOnlyElements() { + struct alignas(512) Test { + Test() = default; + Test(const Test &) noexcept {} + Test &operator=(const Test &) noexcept { return *this; } + Test(Test &&) = delete; + Test &operator=(Test &&) = delete; + }; + MPMCQueue q(16); + // lvalue + Test v; + q.emplace(v); + CHECK(q.try_emplace(v)); + q.push(v); + CHECK(q.try_push(v)); + // xvalue + q.push(Test()); + CHECK(q.try_push(Test())); + q.pop(v); + CHECK(q.try_pop(v)); +} + +void testMoveOnlyElements() { + MPMCQueue> q(16); + q.emplace(std::make_unique(1)); + CHECK(q.try_emplace(std::make_unique(2))); + q.push(std::make_unique(3)); + CHECK(q.try_push(std::make_unique(4))); + std::unique_ptr v; + q.pop(v); + CHECK(v && *v == 1); + CHECK(q.try_pop(v) && *v == 2); + CHECK(q.try_consume([&v](std::unique_ptr &&w) noexcept { + v = std::move(w); + }) == true); + CHECK(*v == 3); +} + +template +concept CanTryPop = + requires(MPMCQueue &queue, T &item) { queue.try_pop(item); }; + +void testImmovableElements() { + struct Item { + int value; + int &destructions; + + Item(int itemValue, int &destructionCount) noexcept + : value(itemValue), destructions(destructionCount) {} + Item(const Item &) = delete; + Item(Item &&) = delete; + Item &operator=(const Item &) = delete; + Item &operator=(Item &&) = delete; + ~Item() noexcept { ++destructions; } + }; + + static_assert(!CanTryPop); + + int destructions = 0; + MPMCQueue q(1); + for (int value = 1; value <= 2; ++value) { + CHECK(q.try_emplace(value, destructions)); + CHECK(q.try_consume([&](Item &&item) noexcept { + CHECK(item.value == value); + CHECK(destructions == value - 1); + })); + CHECK(destructions == value); + CHECK(q.empty()); } +} + +void testMemberPointerConsumers() { + struct Item { + bool *consumed; + void consume() && noexcept { *consumed = true; } + }; + bool consumed = false; + MPMCQueue q(1); + CHECK(!q.try_consume(&Item::consume)); + q.emplace(&consumed); + CHECK(q.try_consume(&Item::consume)); + CHECK(consumed && q.empty()); - // Copyable only type + q.emplace(&consumed); + CHECK(q.try_consume(&Item::consumed)); + CHECK(q.empty()); +} + +void testMovedPointerIdentity() { + MPMCQueue> q(1); { - struct alignas(512) Test { - Test() = default; - Test(const Test &) noexcept {} - Test &operator=(const Test &) noexcept { return *this; } - Test(Test &&) = delete; - Test &operator=(Test &&) = delete; - }; - MPMCQueue q(16); - // lvalue - Test v; - q.emplace(v); - CHECK(q.try_emplace(v)); - q.push(v); - CHECK(q.try_push(v)); - // xvalue - q.push(Test()); - CHECK(q.try_push(Test())); - q.pop(v); - CHECK(q.try_pop(v)); + auto up = std::make_unique(1); + auto *rp = up.get(); + std::unique_ptr up2; + CHECK(q.try_emplace(std::move(up)) == true); + CHECK(q.try_consume([&](std::unique_ptr v) noexcept { + up2 = std::move(v); + }) == true); + CHECK(rp == up2.get()); + CHECK(*up2 == 1); } - // Movable only type { - MPMCQueue> q(16); - q.emplace(std::make_unique(1)); - CHECK(q.try_emplace(std::make_unique(2))); - q.push(std::make_unique(3)); - CHECK(q.try_push(std::make_unique(4))); - std::unique_ptr v; - q.pop(v); - CHECK(v && *v == 1); - CHECK(q.try_pop(v) && *v == 2); - CHECK(q.try_consume([&v](std::unique_ptr &&w) noexcept { - v = std::move(w); + auto up = std::make_unique(1); + auto *rp = up.get(); + std::unique_ptr up2; + q.emplace(std::move(up)); + CHECK(q.try_consume([&](std::unique_ptr v) noexcept { + up2 = std::move(v); }) == true); - CHECK(*v == 3); + CHECK(rp == up2.get()); + CHECK(*up2 == 1); } { - struct Item { - bool *consumed; - void consume() && noexcept { *consumed = true; } - }; - bool consumed = false; - MPMCQueue q(1); - CHECK(!q.try_consume(&Item::consume)); - q.emplace(&consumed); - CHECK(q.try_consume(&Item::consume)); - CHECK(consumed && q.empty()); - - q.emplace(&consumed); - CHECK(q.try_consume(&Item::consumed)); - CHECK(q.empty()); + auto up = std::make_unique(1); + auto *rp = up.get(); + std::unique_ptr up2; + q.push(std::move(up)); + CHECK(q.try_consume([&](std::unique_ptr v) noexcept { + up2 = std::move(v); + }) == true); + CHECK(rp == up2.get()); + CHECK(*up2 == 1); } - // Check that pointer have same address and value after moving through queue { - MPMCQueue> q(1); - { - auto up = std::make_unique(1); - auto *rp = up.get(); - std::unique_ptr up2; - CHECK(q.try_emplace(std::move(up)) == true); - CHECK(q.try_consume([&](std::unique_ptr v) noexcept { - up2 = std::move(v); - }) == true); - CHECK(rp == up2.get()); - CHECK(*up2 == 1); - } - - { - auto up = std::make_unique(1); - auto *rp = up.get(); - std::unique_ptr up2; - q.emplace(std::move(up)); - CHECK(q.try_consume([&](std::unique_ptr v) noexcept { - up2 = std::move(v); - }) == true); - CHECK(rp == up2.get()); - CHECK(*up2 == 1); - } - - { - auto up = std::make_unique(1); - auto *rp = up.get(); - std::unique_ptr up2; - q.push(std::move(up)); - CHECK(q.try_consume([&](std::unique_ptr v) noexcept { - up2 = std::move(v); - }) == true); - CHECK(rp == up2.get()); - CHECK(*up2 == 1); - } - - { - auto up = std::make_unique(1); - auto *rp = up.get(); - std::unique_ptr up2; - CHECK(q.try_push(std::move(up)) == true); - CHECK(q.try_consume([&](std::unique_ptr v) noexcept { - up2 = std::move(v); - }) == true); - CHECK(rp == up2.get()); - CHECK(*up2 == 1); - } + auto up = std::make_unique(1); + auto *rp = up.get(); + std::unique_ptr up2; + CHECK(q.try_push(std::move(up)) == true); + CHECK(q.try_consume([&](std::unique_ptr v) noexcept { + up2 = std::move(v); + }) == true); + CHECK(rp == up2.get()); + CHECK(*up2 == 1); } +} +void testCapacityBounds() { { bool throws = false; try { @@ -259,27 +290,6 @@ int main(int argc, char *argv[]) { CHECK(throws == true); } - { - AllocationState state; - { - MPMCQueue> q(1, TestAllocator(state)); - CHECK(state.count == 2); - CHECK(q.try_push(42)); - int value = 0; - CHECK(q.try_pop(value) && value == 42); - } - CHECK(state.count == 0); - - state.misalign = true; - bool throws = false; - try { - MPMCQueue> q(1, TestAllocator(state)); - } catch (const std::bad_alloc &) { - throws = true; - } - CHECK(throws && state.count == 0); - } - { bool throws = false; try { @@ -289,54 +299,89 @@ int main(int argc, char *argv[]) { } CHECK(throws); } +} + +void testCustomAllocator() { + AllocationState state; + { + MPMCQueue> q(1, TestAllocator(state)); + CHECK(state.count == 2); + CHECK(q.try_push(42)); + int value = 0; + CHECK(q.try_pop(value) && value == 42); + } + CHECK(state.count == 0); + + state.misalign = true; + bool throws = false; + try { + MPMCQueue> q(1, TestAllocator(state)); + } catch (const std::bad_alloc &) { + throws = true; + } + CHECK(throws && state.count == 0); +} - // Exercise both ticket paths under contention, including try-operation - // retries when another producer or consumer claims a ticket first. - for (bool tryOperations : {false, true}) { - const std::size_t numOps = 10000; - const std::size_t numThreads = 10; - MPMCQueue q(numThreads); - std::atomic flag(false); - std::vector threads; - std::atomic sum(0); - for (std::size_t i = 0; i < numThreads; ++i) { - threads.push_back(std::thread([&, i] { - while (!flag) - ; - for (auto j = i; j < numOps; j += numThreads) { - if (tryOperations) { - while (!q.try_push(static_cast(j))) - ; - } else { - q.push(static_cast(j)); - } +// Exercise both ticket paths under contention, including try-operation +// retries when another producer or consumer claims a ticket first. +void testContention(bool tryOperations) { + const std::size_t numOps = 10000; + const std::size_t numThreads = 10; + MPMCQueue q(numThreads); + std::atomic flag(false); + std::vector threads; + std::atomic sum(0); + for (std::size_t i = 0; i < numThreads; ++i) { + threads.push_back(std::thread([&, i] { + while (!flag) + ; + for (auto j = i; j < numOps; j += numThreads) { + if (tryOperations) { + while (!q.try_push(static_cast(j))) + ; + } else { + q.push(static_cast(j)); } - })); - } - for (std::size_t i = 0; i < numThreads; ++i) { - threads.push_back(std::thread([&, i] { - while (!flag) - ; - std::uint64_t threadSum = 0; - for (auto j = i; j < numOps; j += numThreads) { - std::uint64_t v; - if (tryOperations) { - while (!q.try_pop(v)) - ; - } else { - q.pop(v); - } - threadSum += v; + } + })); + } + for (std::size_t i = 0; i < numThreads; ++i) { + threads.push_back(std::thread([&, i] { + while (!flag) + ; + std::uint64_t threadSum = 0; + for (auto j = i; j < numOps; j += numThreads) { + std::uint64_t v; + if (tryOperations) { + while (!q.try_pop(v)) + ; + } else { + q.pop(v); } - sum += threadSum; - })); - } - flag = true; - for (auto &thread : threads) { - thread.join(); - } - CHECK(sum == numOps * (numOps - 1) / 2); + threadSum += v; + } + sum += threadSum; + })); } + flag = true; + for (auto &thread : threads) { + thread.join(); + } + CHECK(sum == numOps * (numOps - 1) / 2); +} + +} // namespace - return 0; +int main() { + testElementLifetime(); + testFullAndEmpty(); + testCopyOnlyElements(); + testMoveOnlyElements(); + testImmovableElements(); + testMemberPointerConsumers(); + testMovedPointerIdentity(); + testCapacityBounds(); + testCustomAllocator(); + testContention(false); + testContention(true); }