summaryrefslogtreecommitdiff
path: root/Source/UnitTests/Common/BusyLoopTest.cpp
diff options
context:
space:
mode:
authordegasus <wickmarkus@web.de>2015-05-28 09:00:53 +0200
committerdegasus <wickmarkus@web.de>2015-05-30 13:40:36 +0200
commit9c730b0a1f50b32348b28d14726ad88bbae691d0 (patch)
tree612d366813334568bfdd3ffdf258bd7c352fd7bf /Source/UnitTests/Common/BusyLoopTest.cpp
parentbfa61105d5a96306a98fa807c3f94b0b9178591d (diff)
Unittests: Add BlockingLoopTest
Diffstat (limited to 'Source/UnitTests/Common/BusyLoopTest.cpp')
-rw-r--r--Source/UnitTests/Common/BusyLoopTest.cpp51
1 files changed, 51 insertions, 0 deletions
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 <atomic>
+#include <thread>
+
+#include <gtest/gtest.h>
+
+#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();
+ }
+}