2014-04-13 16:26:23 -06:00
|
|
|
// Copyright 2014 Dolphin Emulator Project
|
2015-05-17 17:08:10 -06:00
|
|
|
// Licensed under GPLv2+
|
2014-04-13 16:26:23 -06:00
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2014-04-13 17:40:20 -06:00
|
|
|
#include <array>
|
2015-03-01 06:52:09 -07:00
|
|
|
#include <gtest/gtest.h>
|
2014-04-13 16:26:23 -06:00
|
|
|
#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());
|
|
|
|
|
2014-04-13 17:40:20 -06:00
|
|
|
EXPECT_TRUE(f.TestAndSet());
|
|
|
|
EXPECT_TRUE(f.TestAndClear());
|
|
|
|
|
2014-04-13 16:26:23 -06:00
|
|
|
Flag f2(true);
|
|
|
|
EXPECT_TRUE(f2.IsSet());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Flag, MultiThreaded)
|
|
|
|
{
|
|
|
|
Flag f;
|
|
|
|
int count = 0;
|
|
|
|
const int ITERATIONS_COUNT = 100000;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-08-31 06:52:21 -06:00
|
|
|
auto setter = [&]() {
|
2014-04-13 16:26:23 -06:00
|
|
|
for (int i = 0; i < ITERATIONS_COUNT; ++i)
|
|
|
|
{
|
|
|
|
while (f.IsSet())
|
|
|
|
;
|
|
|
|
f.Set();
|
|
|
|
}
|
|
|
|
};
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-08-31 06:52:21 -06:00
|
|
|
auto clearer = [&]() {
|
2014-04-13 16:26:23 -06:00
|
|
|
for (int i = 0; i < ITERATIONS_COUNT; ++i)
|
|
|
|
{
|
|
|
|
while (!f.IsSet())
|
|
|
|
;
|
|
|
|
count++;
|
|
|
|
f.Clear();
|
|
|
|
}
|
|
|
|
};
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-04-13 16:26:23 -06:00
|
|
|
std::thread setter_thread(setter);
|
|
|
|
std::thread clearer_thread(clearer);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-04-13 16:26:23 -06:00
|
|
|
setter_thread.join();
|
|
|
|
clearer_thread.join();
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-04-13 16:26:23 -06:00
|
|
|
EXPECT_EQ(ITERATIONS_COUNT, count);
|
|
|
|
}
|
2014-04-13 17:40:20 -06:00
|
|
|
|
|
|
|
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;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-04-13 17:40:20 -06:00
|
|
|
auto adder_func = [&]() {
|
|
|
|
for (int i = 0; i < ITERATIONS_COUNT; ++i)
|
|
|
|
{
|
|
|
|
// Acquire the spinlock.
|
|
|
|
while (!f.TestAndSet())
|
|
|
|
;
|
|
|
|
count++;
|
|
|
|
f.Clear();
|
|
|
|
}
|
|
|
|
};
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-04-13 17:40:20 -06:00
|
|
|
std::array<std::thread, THREADS_COUNT> threads;
|
|
|
|
for (auto& th : threads)
|
|
|
|
th = std::thread(adder_func);
|
|
|
|
for (auto& th : threads)
|
|
|
|
th.join();
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-04-13 17:40:20 -06:00
|
|
|
EXPECT_EQ(ITERATIONS_COUNT * THREADS_COUNT, count);
|
|
|
|
}
|