1
0
mirror of https://github.com/RPCS3/rpcs3.git synced 2024-11-22 02:32:36 +01:00
rpcs3/Utilities/Thread.cpp

313 lines
5.3 KiB
C++
Raw Normal View History

#include "stdafx.h"
#include "Emu/System.h"
2014-09-11 21:18:19 +02:00
#include "Log.h"
#include "Thread.h"
#ifdef _WIN32
#include <windows.h>
#endif
void SetCurrentThreadDebugName(const char* threadName)
{
#ifdef _WIN32 // this is VS-specific way to set thread names for the debugger
#pragma pack(push,8)
struct THREADNAME_INFO
{
DWORD dwType;
LPCSTR szName;
DWORD dwThreadID;
DWORD dwFlags;
} info;
#pragma pack(pop)
info.dwType = 0x1000;
info.szName = threadName;
info.dwThreadID = -1;
info.dwFlags = 0;
__try
{
RaiseException(0x406D1388, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR*)&info);
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
}
#endif
}
thread_local NamedThreadBase* g_tls_this_thread = nullptr;
std::atomic<u32> g_thread_count(0);
NamedThreadBase* GetCurrentNamedThread()
{
return g_tls_this_thread;
}
2014-08-20 16:23:48 +02:00
void SetCurrentNamedThread(NamedThreadBase* value)
{
2014-09-15 00:17:24 +02:00
auto old_value = g_tls_this_thread;
if (old_value == value)
{
return;
}
if (value && value->m_tls_assigned.exchange(true))
{
LOG_ERROR(GENERAL, "Thread '%s' was already assigned to g_tls_this_thread of another thread", value->GetThreadName().c_str());
g_tls_this_thread = nullptr;
}
else
{
g_tls_this_thread = value;
}
if (old_value)
{
old_value->m_tls_assigned = false;
}
2014-08-20 16:23:48 +02:00
}
std::string NamedThreadBase::GetThreadName() const
{
return m_name;
}
void NamedThreadBase::SetThreadName(const std::string& name)
{
m_name = name;
}
2014-09-12 21:27:33 +02:00
void NamedThreadBase::WaitForAnySignal(u64 time) // wait for Notify() signal or sleep
2014-08-25 20:09:48 +02:00
{
std::unique_lock<std::mutex> lock(m_signal_mtx);
2014-09-12 21:27:33 +02:00
m_signal_cv.wait_for(lock, std::chrono::milliseconds(time));
2014-08-25 20:09:48 +02:00
}
void NamedThreadBase::Notify() // wake up waiting thread or nothing
{
m_signal_cv.notify_one();
}
ThreadBase::ThreadBase(const std::string& name)
: NamedThreadBase(name)
, m_executor(nullptr)
, m_destroy(false)
, m_alive(false)
{
}
ThreadBase::~ThreadBase()
{
if(IsAlive())
Stop(false);
2014-08-25 20:09:48 +02:00
delete m_executor;
m_executor = nullptr;
}
void ThreadBase::Start()
{
if(m_executor) Stop();
std::lock_guard<std::mutex> lock(m_main_mutex);
m_destroy = false;
m_alive = true;
2014-08-20 16:23:48 +02:00
m_executor = new std::thread([this]()
{
SetCurrentThreadDebugName(GetThreadName().c_str());
2014-08-20 16:23:48 +02:00
SetCurrentNamedThread(this);
g_thread_count++;
2014-09-11 21:18:19 +02:00
try
{
Task();
}
catch (const char* e)
{
2014-09-15 00:17:24 +02:00
LOG_ERROR(GENERAL, "%s: %s", GetThreadName().c_str(), e);
2014-09-11 21:18:19 +02:00
}
catch (const std::string& e)
{
2014-09-15 00:17:24 +02:00
LOG_ERROR(GENERAL, "%s: %s", GetThreadName().c_str(), e.c_str());
2014-09-11 21:18:19 +02:00
}
2014-08-20 16:23:48 +02:00
m_alive = false;
2014-09-15 00:17:24 +02:00
SetCurrentNamedThread(nullptr);
2014-08-20 16:23:48 +02:00
g_thread_count--;
});
}
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)
2014-02-27 19:25:32 +01:00
{
std::string name = m_name;
m_thr = std::thread([func, name]()
2014-02-19 18:27:52 +01:00
{
SetCurrentThreadDebugName(name.c_str());
2014-02-27 19:25:32 +01:00
NamedThreadBase info(name);
2014-08-20 16:23:48 +02:00
SetCurrentNamedThread(&info);
g_thread_count++;
2014-09-11 21:18:19 +02:00
try
{
func();
}
catch (const char* e)
{
2014-09-15 00:17:24 +02:00
LOG_ERROR(GENERAL, "%s: %s", name.c_str(), e);
2014-09-11 21:18:19 +02:00
}
catch (const std::string& e)
{
2014-09-15 00:17:24 +02:00
LOG_ERROR(GENERAL, "%s: %s", name.c_str(), e.c_str());
2014-09-11 21:18:19 +02:00
}
2014-09-15 00:17:24 +02:00
SetCurrentNamedThread(nullptr);
g_thread_count--;
2014-02-19 18:27:52 +01:00
});
}
void thread::detach()
{
m_thr.detach();
}
void thread::join()
{
m_thr.join();
}
bool thread::joinable() const
{
return m_thr.joinable();
}
bool waiter_map_t::is_stopped(u64 signal_id)
{
if (Emu.IsStopped())
{
LOG_WARNING(Log::HLE, "%s.waiter_op() aborted (signal_id=0x%llx)", m_name.c_str(), signal_id);
return true;
}
return false;
}
2014-10-17 22:13:25 +02:00
void waiter_map_t::waiter_reg_t::init()
{
2014-12-25 23:49:55 +01:00
if (!thread)
{
thread = GetCurrentNamedThread();
2014-10-17 22:13:25 +02:00
2014-12-25 23:49:55 +01:00
std::lock_guard<std::mutex> lock(map.m_mutex);
2014-12-25 23:49:55 +01:00
// add waiter
map.m_waiters.push_back({ signal_id, thread });
}
}
waiter_map_t::waiter_reg_t::~waiter_reg_t()
{
2014-12-25 23:49:55 +01:00
if (thread)
{
2014-12-25 23:49:55 +01:00
std::lock_guard<std::mutex> lock(map.m_mutex);
// remove waiter
for (s64 i = map.m_waiters.size() - 1; i >= 0; i--)
{
2014-12-25 23:49:55 +01:00
if (map.m_waiters[i].signal_id == signal_id && map.m_waiters[i].thread == thread)
{
map.m_waiters.erase(map.m_waiters.begin() + i);
return;
}
}
2014-12-25 23:49:55 +01:00
LOG_ERROR(HLE, "%s(): waiter not found (signal_id=0x%llx, map='%s')", __FUNCTION__, signal_id, map.m_name.c_str());
Emu.Pause();
}
}
void waiter_map_t::notify(u64 signal_id)
{
2014-12-25 23:49:55 +01:00
if (m_waiters.size())
{
2014-12-25 23:49:55 +01:00
std::lock_guard<std::mutex> lock(m_mutex);
// find waiter and signal
for (auto& v : m_waiters)
{
2014-12-25 23:49:55 +01:00
if (v.signal_id == signal_id)
{
v.thread->Notify();
}
}
}
}
2014-12-25 21:30:34 +01:00
bool squeue_test_exit(const volatile bool* do_exit)
{
return Emu.IsStopped() || (do_exit && *do_exit);
}