summaryrefslogtreecommitdiff
path: root/Source/UnitTests/Common/BlockingLoopTest.cpp
diff options
context:
space:
mode:
authorJules Blok <jules.blok@gmail.com>2015-06-10 00:00:12 +0200
committerJules Blok <jules.blok@gmail.com>2015-06-10 00:00:12 +0200
commit7dfced21a2e5aac7195e0e1ad76468e30306766b (patch)
treebd671b639d3d911460b767caabad8fc8a759a6bf /Source/UnitTests/Common/BlockingLoopTest.cpp
parentc25be031fc1031d795157d8344b87b7841145197 (diff)
parent7b0a65e295c784f9d573f2ef52d4e2a7b9bb2889 (diff)
Merge branch 'master' into stable
Diffstat (limited to 'Source/UnitTests/Common/BlockingLoopTest.cpp')
-rw-r--r--Source/UnitTests/Common/BlockingLoopTest.cpp84
1 files changed, 84 insertions, 0 deletions
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 <atomic>
+#include <thread>
+
+#include <gtest/gtest.h>
+
+#include "Common/BlockingLoop.h"
+
+TEST(BlockingLoop, MultiThreaded)
+{
+ Common::BlockingLoop loop;
+ std::atomic<int> signaled_a(0);
+ std::atomic<int> received_a(0);
+ std::atomic<int> signaled_b(0);
+ std::atomic<int> 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();
+ }
+}