mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-07-23 06:09:50 -06:00
Merge pull request #271 from delroth/threading-stuff
Threading improvements: add Common::Flag and improve Common::Event
This commit is contained in:
@ -1,5 +1,7 @@
|
||||
add_dolphin_test(BitFieldTest BitFieldTest.cpp common)
|
||||
add_dolphin_test(CommonFuncsTest CommonFuncsTest.cpp common)
|
||||
add_dolphin_test(EventTest EventTest.cpp common)
|
||||
add_dolphin_test(FifoQueueTest FifoQueueTest.cpp common)
|
||||
add_dolphin_test(FixedSizeQueueTest FixedSizeQueueTest.cpp common)
|
||||
add_dolphin_test(FlagTest FlagTest.cpp common)
|
||||
add_dolphin_test(MathUtilTest MathUtilTest.cpp common)
|
||||
|
42
Source/UnitTests/Common/EventTest.cpp
Normal file
42
Source/UnitTests/Common/EventTest.cpp
Normal file
@ -0,0 +1,42 @@
|
||||
// Copyright 2014 Dolphin Emulator Project
|
||||
// Licensed under GPLv2
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <thread>
|
||||
|
||||
#include "Common/Event.h"
|
||||
|
||||
using Common::Event;
|
||||
|
||||
TEST(Event, MultiThreaded)
|
||||
{
|
||||
Event has_sent, can_send;
|
||||
int shared_obj;
|
||||
const int ITERATIONS_COUNT = 100000;
|
||||
|
||||
auto sender = [&]() {
|
||||
for (int i = 0; i < ITERATIONS_COUNT; ++i)
|
||||
{
|
||||
can_send.Wait();
|
||||
shared_obj = i;
|
||||
has_sent.Set();
|
||||
}
|
||||
};
|
||||
|
||||
auto receiver = [&]() {
|
||||
for (int i = 0; i < ITERATIONS_COUNT; ++i) {
|
||||
has_sent.Wait();
|
||||
EXPECT_EQ(i, shared_obj);
|
||||
can_send.Set();
|
||||
}
|
||||
};
|
||||
|
||||
std::thread sender_thread(sender);
|
||||
std::thread receiver_thread(receiver);
|
||||
|
||||
can_send.Set();
|
||||
|
||||
sender_thread.join();
|
||||
receiver_thread.join();
|
||||
}
|
91
Source/UnitTests/Common/FlagTest.cpp
Normal file
91
Source/UnitTests/Common/FlagTest.cpp
Normal file
@ -0,0 +1,91 @@
|
||||
// Copyright 2014 Dolphin Emulator Project
|
||||
// Licensed under GPLv2
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#include <array>
|
||||
#include <gtest/gtest.h>
|
||||
#include <thread>
|
||||
|
||||
#include "Common/Flag.h"
|
||||
|
||||
using Common::Flag;
|
||||
|
||||
TEST(Flag, Simple)
|
||||
{
|
||||
Flag f;
|
||||
EXPECT_FALSE(f.IsSet());
|
||||
|
||||
f.Set();
|
||||
EXPECT_TRUE(f.IsSet());
|
||||
|
||||
f.Clear();
|
||||
EXPECT_FALSE(f.IsSet());
|
||||
|
||||
f.Set(false);
|
||||
EXPECT_FALSE(f.IsSet());
|
||||
|
||||
EXPECT_TRUE(f.TestAndSet());
|
||||
EXPECT_TRUE(f.TestAndClear());
|
||||
|
||||
Flag f2(true);
|
||||
EXPECT_TRUE(f2.IsSet());
|
||||
}
|
||||
|
||||
TEST(Flag, MultiThreaded)
|
||||
{
|
||||
Flag f;
|
||||
int count = 0;
|
||||
const int ITERATIONS_COUNT = 100000;
|
||||
|
||||
auto setter = [&f]() {
|
||||
for (int i = 0; i < ITERATIONS_COUNT; ++i)
|
||||
{
|
||||
while (f.IsSet());
|
||||
f.Set();
|
||||
}
|
||||
};
|
||||
|
||||
auto clearer = [&f, &count]() {
|
||||
for (int i = 0; i < ITERATIONS_COUNT; ++i)
|
||||
{
|
||||
while (!f.IsSet());
|
||||
count++;
|
||||
f.Clear();
|
||||
}
|
||||
};
|
||||
|
||||
std::thread setter_thread(setter);
|
||||
std::thread clearer_thread(clearer);
|
||||
|
||||
setter_thread.join();
|
||||
clearer_thread.join();
|
||||
|
||||
EXPECT_EQ(ITERATIONS_COUNT, count);
|
||||
}
|
||||
|
||||
TEST(Flag, SpinLock)
|
||||
{
|
||||
// Uses a flag to implement basic spinlocking using TestAndSet.
|
||||
Flag f;
|
||||
int count = 0;
|
||||
const int ITERATIONS_COUNT = 5000;
|
||||
const int THREADS_COUNT = 50;
|
||||
|
||||
auto adder_func = [&]() {
|
||||
for (int i = 0; i < ITERATIONS_COUNT; ++i)
|
||||
{
|
||||
// Acquire the spinlock.
|
||||
while (!f.TestAndSet());
|
||||
count++;
|
||||
f.Clear();
|
||||
}
|
||||
};
|
||||
|
||||
std::array<std::thread, THREADS_COUNT> threads;
|
||||
for (auto& th : threads)
|
||||
th = std::thread(adder_func);
|
||||
for (auto& th : threads)
|
||||
th.join();
|
||||
|
||||
EXPECT_EQ(ITERATIONS_COUNT * THREADS_COUNT, count);
|
||||
}
|
Reference in New Issue
Block a user