Merge pull request #271 from delroth/threading-stuff

Threading improvements: add Common::Flag and improve Common::Event
This commit is contained in:
Pierre Bourdon 2014-04-14 23:23:16 +02:00
commit cf315a487f
24 changed files with 307 additions and 53 deletions

View File

@ -5,7 +5,9 @@
#pragma once
#include "AudioCommon/SoundStream.h"
#include "Common/Thread.h"
#include "Common/Event.h"
#include "Common/StdMutex.h"
#include "Common/StdThread.h"
#if defined(HAVE_AO) && HAVE_AO
#include <ao/ao.h>

View File

@ -9,6 +9,8 @@
#include "AudioCommon/AudioCommon.h"
#include "AudioCommon/DSoundStream.h"
#include "Common/StdThread.h"
#include "Common/Thread.h"
bool DSound::CreateBuffer()
{

View File

@ -5,7 +5,8 @@
#pragma once
#include "AudioCommon/SoundStream.h"
#include "Common/Thread.h"
#include "Common/Event.h"
#include "Common/StdThread.h"
#ifdef _WIN32
#include <Windows.h>

View File

@ -5,6 +5,8 @@
#include "AudioCommon/aldlist.h"
#include "AudioCommon/DPL2Decoder.h"
#include "AudioCommon/OpenALStream.h"
#include "Common/StdThread.h"
#include "Common/Thread.h"
#if defined HAVE_OPENAL && HAVE_OPENAL

View File

@ -5,7 +5,8 @@
#pragma once
#include "AudioCommon/SoundStream.h"
#include "Common/Thread.h"
#include "Common/Event.h"
#include "Common/StdThread.h"
#include "Core/Core.h"
#include "Core/HW/AudioInterface.h"
#include "Core/HW/SystemTimers.h"

View File

@ -5,7 +5,8 @@
#pragma once
#include "AudioCommon/SoundStream.h"
#include "Common/Thread.h"
#include "Common/Event.h"
#include "Common/StdThread.h"
class OpenSLESStream final : public SoundStream
{

View File

@ -5,6 +5,7 @@
#include <xaudio2.h>
#include "AudioCommon/AudioCommon.h"
#include "AudioCommon/XAudio2Stream.h"
#include "Common/Event.h"
#ifndef XAUDIO2_DLL
#error You are building this module against the wrong version of DirectX. You probably need to remove DXSDK_DIR from your include path.

View File

@ -11,7 +11,7 @@
#include <memory>
#include "AudioCommon/SoundStream.h"
#include "Common/Thread.h"
#include "Common/Event.h"
#ifdef _WIN32

View File

@ -9,6 +9,7 @@
#include "AudioCommon/AudioCommon.h"
#include "AudioCommon/XAudio2_7Stream.h"
#include "Common/Event.h"
#ifdef HAVE_DXSDK
#include <dxsdkver.h>

View File

@ -14,7 +14,7 @@
#include <memory>
#include "AudioCommon/SoundStream.h"
#include "Common/Thread.h"
#include "Common/Event.h"
#ifdef _WIN32

View File

@ -0,0 +1,76 @@
// Copyright 2014 Dolphin Emulator Project
// Licensed under GPLv2
// Refer to the license.txt file included.
// Multithreaded event class. This allows waiting in a thread for an event to
// be triggered in another thread. While waiting, the CPU will be available for
// other tasks.
// * Set(): triggers the event and wakes up the waiting thread.
// * Wait(): waits for the event to be triggered.
// * Reset(): tries to reset the event before the waiting thread sees it was
// triggered. Usually a bad idea.
#pragma once
#ifdef _WIN32
#include <concrt.h>
#endif
#include "Common/Flag.h"
#include "Common/StdConditionVariable.h"
#include "Common/StdMutex.h"
namespace Common {
// Windows uses a specific implementation because std::condition_variable has
// terrible performance for this kind of workload with MSVC++ 2013.
#ifndef _WIN32
class Event final
{
public:
void Set()
{
if (m_flag.TestAndSet())
{
std::lock_guard<std::mutex> lk(m_mutex);
m_condvar.notify_one();
}
}
void Wait()
{
if (m_flag.TestAndClear())
return;
std::unique_lock<std::mutex> lk(m_mutex);
m_condvar.wait(lk, [&]{ return m_flag.IsSet(); });
m_flag.Clear();
}
void Reset()
{
// no other action required, since wait loops on
// the predicate and any lingering signal will get
// cleared on the first iteration
m_flag.Clear();
}
private:
Flag m_flag;
std::condition_variable m_condvar;
std::mutex m_mutex;
};
#else
class Event final
{
public:
void Set() { m_event.set(); }
void Wait() { m_event.wait(); m_event.reset(); }
void Reset() { m_event.reset(); }
private:
concurrency::event m_event;
};
#endif
} // namespace Common

66
Source/Core/Common/Flag.h Normal file
View File

@ -0,0 +1,66 @@
// Copyright 2014 Dolphin Emulator Project
// Licensed under GPLv2
// Refer to the license.txt file included.
// Abstraction for a simple flag that can be toggled in a multithreaded way.
//
// Simple API:
// * Set(bool = true): sets the Flag
// * IsSet(): tests if the flag is set
// * Clear(): clears the flag (equivalent to Set(false)).
//
// More advanced features:
// * TestAndSet(bool = true): sets the flag to the given value. If a change was
// needed (the flag did not already have this value)
// the function returns true. Else, false.
// * TestAndClear(): alias for TestAndSet(false).
#pragma once
#include <atomic>
namespace Common {
class Flag final
{
public:
// Declared as explicit since we do not want "= true" to work on a flag
// object - it should be made explicit that a flag is *not* a normal
// variable.
explicit Flag(bool initial_value = false) : m_val(initial_value) {}
void Set(bool val = true)
{
m_val.store(val);
}
void Clear()
{
Set(false);
}
bool IsSet() const
{
return m_val.load();
}
bool TestAndSet(bool val = true)
{
bool expected = !val;
return m_val.compare_exchange_strong(expected, val);
}
bool TestAndClear()
{
return TestAndSet(false);
}
private:
// We are not using std::atomic_bool here because MSVC sucks as of VC++
// 2013 and does not implement the std::atomic_bool(bool) constructor.
//
// Re-evaluate next time we upgrade that piece of shit.
std::atomic<bool> m_val;
};
} // namespace Common

View File

@ -32,45 +32,6 @@ int CurrentThreadId();
void SetThreadAffinity(std::thread::native_handle_type thread, u32 mask);
void SetCurrentThreadAffinity(u32 mask);
class Event
{
public:
Event()
: is_set(false)
{}
void Set()
{
std::lock_guard<std::mutex> lk(m_mutex);
if (!is_set)
{
is_set = true;
m_condvar.notify_one();
}
}
void Wait()
{
std::unique_lock<std::mutex> lk(m_mutex);
m_condvar.wait(lk, [&]{ return is_set; });
is_set = false;
}
void Reset()
{
std::unique_lock<std::mutex> lk(m_mutex);
// no other action required, since wait loops on
// the predicate and any lingering signal will get
// cleared on the first iteration
is_set = false;
}
private:
volatile bool is_set;
std::condition_variable m_condvar;
std::mutex m_mutex;
};
// TODO: doesn't work on windows with (count > 2)
class Barrier
{

View File

@ -24,10 +24,10 @@
====================================================================*/
#include "Common/Common.h"
#include "Common/Event.h"
#include "Common/FileUtil.h"
#include "Common/Hash.h"
#include "Common/MemoryUtil.h"
#include "Common/Thread.h"
#include "Core/DSP/DSPAnalyzer.h"
#include "Core/DSP/DSPCore.h"

View File

@ -5,7 +5,8 @@
#include "AudioCommon/AudioCommon.h"
#include "Common/Common.h"
#include "Common/Thread.h"
#include "Common/Event.h"
#include "Common/StdMutex.h"
#include "Core/Core.h"
#include "Core/DSPEmulator.h"

View File

@ -7,9 +7,11 @@
#include "Common/Common.h"
#include "Common/CommonPaths.h"
#include "Common/CPUDetect.h"
#include "Common/Event.h"
#include "Common/IniFile.h"
#include "Common/LogManager.h"
#include "Common/Thread.h"
#include "Common/StdMutex.h"
#include "Common/StdThread.h"
#include "Core/ConfigManager.h"
#include "Core/Core.h"

View File

@ -5,8 +5,10 @@
#include <lzo/lzo1x.h>
#include "Common/Common.h"
#include "Common/Event.h"
#include "Common/StdMutex.h"
#include "Common/StdThread.h"
#include "Common/StringUtil.h"
#include "Common/Thread.h"
#include "Common/Timer.h"
#include "Core/ConfigManager.h"

View File

@ -14,7 +14,7 @@
#include <wx/windowid.h>
#include "Common/CommonTypes.h"
#include "Common/Thread.h"
#include "Common/Event.h"
#include "DolphinWX/Globals.h"
class CFrame;

View File

@ -22,7 +22,7 @@
#include <wx/windowid.h>
#include "Common/CommonTypes.h"
#include "Common/Thread.h"
#include "Common/Event.h"
#include "DolphinWX/Globals.h"
#include "InputCommon/GCPadStatus.h"

View File

@ -27,9 +27,9 @@
#include "Common/Common.h"
#include "Common/CommonPaths.h"
#include "Common/CPUDetect.h"
#include "Common/Event.h"
#include "Common/FileUtil.h"
#include "Common/LogManager.h"
#include "Common/Thread.h"
#include "Core/BootManager.h"
#include "Core/ConfigManager.h"
#include "Core/Core.h"

View File

@ -10,8 +10,8 @@
#include <string>
#include "Common/Common.h"
#include "Common/Event.h"
#include "Common/LogManager.h"
#include "Common/Thread.h"
#include "Core/BootManager.h"
#include "Core/ConfigManager.h"

View File

@ -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)

View 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();
}

View 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);
}