mirror of
https://github.com/RPCS3/rpcs3.git
synced 2024-11-25 12:12:50 +01:00
9a30ce5f18
* replace GetThreadID with std::this_thread.getId() * name all anonymous structs and unions that contain non-trivially constructable objects * made default constructor for big endian type noexcept to make it work with std::atomic * move instantiated specialized template function members ouside of the class definition to comply with the standard * added default instantiation for template parameter "=nullptr" * used the C++11 standardized thread_local instead of the __declspec(thread) * added transitional definitions to bridge the microsoft specific calls (compare and exchange and aligned alloc) * removed cyclic dependency between Emulator->CPUThreadManager->CPUThread->SMutex->Emulator->... * fixed some instances of indentation by space instead of tabs * surrounded some unused code with an #if 0 block to make sure it doesn't compile
146 lines
2.1 KiB
C++
146 lines
2.1 KiB
C++
#include "stdafx.h"
|
|
#include "Thread.h"
|
|
|
|
/*__declspec(thread)*/ thread_local NamedThreadBase* g_tls_this_thread = nullptr;
|
|
|
|
NamedThreadBase* GetCurrentNamedThread()
|
|
{
|
|
return g_tls_this_thread;
|
|
}
|
|
|
|
std::string NamedThreadBase::GetThreadName() const
|
|
{
|
|
return m_name;
|
|
}
|
|
|
|
void NamedThreadBase::SetThreadName(const std::string& name)
|
|
{
|
|
m_name = name;
|
|
}
|
|
|
|
ThreadBase::ThreadBase(const std::string& name)
|
|
: NamedThreadBase(name)
|
|
, m_executor(nullptr)
|
|
, m_destroy(false)
|
|
, m_alive(false)
|
|
{
|
|
}
|
|
|
|
ThreadBase::~ThreadBase()
|
|
{
|
|
if(IsAlive())
|
|
Stop(false);
|
|
}
|
|
|
|
void ThreadBase::Start()
|
|
{
|
|
if(m_executor) Stop();
|
|
|
|
std::lock_guard<std::mutex> lock(m_main_mutex);
|
|
|
|
m_destroy = false;
|
|
m_alive = true;
|
|
|
|
m_executor = new std::thread(
|
|
[this]()
|
|
{
|
|
g_tls_this_thread = this;
|
|
|
|
Task();
|
|
|
|
m_alive = false;
|
|
});
|
|
}
|
|
|
|
void ThreadBase::Stop(bool wait, bool send_destroy)
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_main_mutex);
|
|
|
|
if (send_destroy)
|
|
m_destroy = true;
|
|
|
|
if(!m_executor)
|
|
return;
|
|
|
|
if(wait && m_executor->joinable() && m_alive)
|
|
{
|
|
m_executor->join();
|
|
}
|
|
else
|
|
{
|
|
m_executor->detach();
|
|
}
|
|
|
|
delete m_executor;
|
|
m_executor = nullptr;
|
|
}
|
|
|
|
bool ThreadBase::Join() const
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_main_mutex);
|
|
if(m_executor->joinable() && m_alive && m_executor != nullptr)
|
|
{
|
|
m_executor->join();
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
bool ThreadBase::IsAlive() const
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_main_mutex);
|
|
return m_alive;
|
|
}
|
|
|
|
bool ThreadBase::TestDestroy() const
|
|
{
|
|
return m_destroy;
|
|
}
|
|
|
|
thread::thread(const std::string& name, std::function<void()> func) : m_name(name)
|
|
{
|
|
start(func);
|
|
}
|
|
|
|
thread::thread(const std::string& name) : m_name(name)
|
|
{
|
|
}
|
|
|
|
thread::thread()
|
|
{
|
|
}
|
|
|
|
void thread::start(std::function<void()> func)
|
|
{ // got a crash related with strings
|
|
m_thr = std::thread([this, func]()
|
|
{
|
|
try
|
|
{
|
|
NamedThreadBase info(m_name);
|
|
g_tls_this_thread = &info;
|
|
func();
|
|
}
|
|
catch(...)
|
|
{
|
|
ConLog.Error("Crash :(");
|
|
std::terminate();
|
|
}
|
|
});
|
|
}
|
|
|
|
void thread::detach()
|
|
{
|
|
m_thr.detach();
|
|
}
|
|
|
|
void thread::join()
|
|
{
|
|
m_thr.join();
|
|
}
|
|
|
|
bool thread::joinable() const
|
|
{
|
|
return m_thr.joinable();
|
|
}
|