diff --git a/Source/UnitTests/Common/BlockingLoopTest.cpp b/Source/UnitTests/Common/BlockingLoopTest.cpp new file mode 100644 index 0000000000..805aca446c --- /dev/null +++ b/Source/UnitTests/Common/BlockingLoopTest.cpp @@ -0,0 +1,84 @@ +// Copyright 2014 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#include +#include + +#include + +#include "Common/BlockingLoop.h" + +TEST(BlockingLoop, MultiThreaded) +{ + Common::BlockingLoop loop; + std::atomic signaled_a(0); + std::atomic received_a(0); + std::atomic signaled_b(0); + std::atomic received_b(0); + for (int i = 0; i < 100; i++) + { + // Invalidate the current state. + received_a.store(signaled_a.load() + 1); + received_b.store(signaled_b.load() + 123); + + // Must not block as the loop is stopped. + loop.Wait(); + + std::thread loop_thread( + [&]() { + loop.Run( + [&]() { + received_a.store(signaled_a.load()); + received_b.store(signaled_b.load()); + }); + }); + + // Now Wait must block. + loop.Prepare(); + + // The payload must run at least once on startup. + loop.Wait(); + EXPECT_EQ(signaled_a.load(), received_a.load()); + EXPECT_EQ(signaled_b.load(), received_b.load()); + + std::thread run_a_thread( + [&]() { + for (int j = 0; j < 100; j++) + { + for (int k = 0; k < 100; k++) + { + signaled_a++; + loop.Wakeup(); + } + + loop.Wait(); + EXPECT_EQ(signaled_a.load(), received_a.load()); + } + }); + std::thread run_b_thread( + [&]() { + for (int j = 0; j < 100; j++) + { + for (int k = 0; k < 100; k++) + { + signaled_b++; + loop.Wakeup(); + } + + loop.Wait(); + EXPECT_EQ(signaled_b.load(), received_b.load()); + } + }); + + run_a_thread.join(); + run_b_thread.join(); + + loop.Stop(); + + // Must not block + loop.Wait(); + + loop_thread.join(); + } +} diff --git a/Source/UnitTests/Common/BusyLoopTest.cpp b/Source/UnitTests/Common/BusyLoopTest.cpp new file mode 100644 index 0000000000..50d11acb31 --- /dev/null +++ b/Source/UnitTests/Common/BusyLoopTest.cpp @@ -0,0 +1,51 @@ +// Copyright 2014 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#include +#include + +#include + +#include "Common/BlockingLoop.h" +#include "Common/Thread.h" + +TEST(BusyLoopTest, MultiThreaded) +{ + Common::BlockingLoop loop; + Common::Event e; + for (int i = 0; i < 100; i++) + { + loop.Prepare(); + std::thread loop_thread( + [&]() { + loop.Run( + [&]() { + e.Set(); + }); + }); + + // Ping - Pong + for (int j = 0; j < 10; j++) + { + loop.Wakeup(); + e.Wait(); + + // Just waste some time. So the main loop did fall back to the sleep state much more likely. + Common::SleepCurrentThread(1); + } + + for (int j = 0; j < 100; j++) + { + // We normally have to call Wakeup to assure the Event is triggered. + // But this check is for an internal feature of the BlockingLoop. + // It's implemented to fall back to a busy loop regulary. + // If we're in the busy loop, the payload (and so the Event) is called all the time. + //loop.Wakeup(); + e.Wait(); + } + + loop.Stop(); + loop_thread.join(); + } +} diff --git a/Source/UnitTests/Common/CMakeLists.txt b/Source/UnitTests/Common/CMakeLists.txt index a35bd455fe..eba08f9453 100644 --- a/Source/UnitTests/Common/CMakeLists.txt +++ b/Source/UnitTests/Common/CMakeLists.txt @@ -1,5 +1,7 @@ add_dolphin_test(BitFieldTest BitFieldTest.cpp) add_dolphin_test(BitSetTest BitSetTest.cpp) +add_dolphin_test(BlockingLoopTest BlockingLoopTest.cpp) +add_dolphin_test(BusyLoopTest BusyLoopTest.cpp) add_dolphin_test(CommonFuncsTest CommonFuncsTest.cpp) add_dolphin_test(EventTest EventTest.cpp) add_dolphin_test(FifoQueueTest FifoQueueTest.cpp)