rpcsx/rpcs3/Emu/SysCalls/Callback.cpp

108 lines
1.9 KiB
C++
Raw Normal View History

#include "stdafx.h"
#include "Utilities/Log.h"
#include "Emu/Memory/Memory.h"
2014-09-11 21:18:19 +02:00
#include "ErrorCodes.h"
#include "Emu/System.h"
2014-09-11 21:18:19 +02:00
#include "Emu/CPU/CPUThreadManager.h"
2014-07-13 21:05:28 +02:00
#include "Emu/Cell/PPUThread.h"
2014-08-23 22:40:04 +02:00
#include "Callback.h"
2014-09-11 21:18:19 +02:00
void CallbackManager::Register(const std::function<s32()>& func)
{
2014-09-11 21:18:19 +02:00
std::lock_guard<std::mutex> lock(m_mutex);
2014-09-11 21:18:19 +02:00
m_cb_list.push_back(func);
}
2014-09-11 21:18:19 +02:00
void CallbackManager::Async(const std::function<void()>& func)
{
2014-09-11 21:18:19 +02:00
std::lock_guard<std::mutex> lock(m_mutex);
2014-09-11 21:18:19 +02:00
m_async_list.push_back(func);
m_cb_thread->Notify();
}
2014-09-11 21:18:19 +02:00
bool CallbackManager::Check(s32& result)
{
2014-09-11 21:18:19 +02:00
std::function<s32()> func = nullptr;
{
2014-09-11 21:18:19 +02:00
std::lock_guard<std::mutex> lock(m_mutex);
if (m_cb_list.size())
{
2014-09-11 21:18:19 +02:00
func = m_cb_list[0];
m_cb_list.erase(m_cb_list.begin());
}
}
2014-09-11 21:18:19 +02:00
if (func)
{
2014-09-11 21:18:19 +02:00
result = func();
return true;
}
2014-09-11 21:18:19 +02:00
else
{
2014-09-11 21:18:19 +02:00
return false;
}
2014-09-11 21:18:19 +02:00
}
2014-09-11 21:18:19 +02:00
void CallbackManager::Init()
{
std::lock_guard<std::mutex> lock(m_mutex);
2014-09-11 21:18:19 +02:00
m_cb_thread = &Emu.GetCPU().AddThread(CPU_THREAD_PPU);
2014-09-11 21:18:19 +02:00
u32 cb_shit = Memory.MainMem.AllocAlign(8);
vm::write32(cb_shit, Emu.m_ppu_thr_stop);
vm::write32(cb_shit + 4, 0);
2014-09-12 23:14:48 +02:00
m_cb_thread->SetName("Callback Thread");
2014-09-11 21:18:19 +02:00
m_cb_thread->SetEntry(cb_shit);
m_cb_thread->SetPrio(1001); // ???
m_cb_thread->SetStackSize(0x10000);
2014-09-11 21:18:19 +02:00
thread cb_async_thread("CallbackManager::Async() thread", [this]()
{
2014-09-12 23:50:50 +02:00
while (Emu.IsReady())
{
m_cb_thread->WaitForAnySignal();
}
m_cb_thread->Run();
2014-09-11 21:18:19 +02:00
SetCurrentNamedThread(m_cb_thread);
2014-09-11 21:18:19 +02:00
while (!Emu.IsStopped())
{
2014-09-11 21:18:19 +02:00
std::function<void()> func = nullptr;
{
std::lock_guard<std::mutex> lock(m_mutex);
if (m_async_list.size())
{
func = m_async_list[0];
m_async_list.erase(m_async_list.begin());
}
}
if (func)
{
func();
continue;
}
m_cb_thread->WaitForAnySignal();
}
2014-09-11 21:18:19 +02:00
});
2014-09-11 21:18:19 +02:00
cb_async_thread.detach();
}
2014-09-11 21:18:19 +02:00
void CallbackManager::Clear()
{
2014-09-11 21:18:19 +02:00
std::lock_guard<std::mutex> lock(m_mutex);
2014-09-11 21:18:19 +02:00
m_cb_list.clear();
m_async_list.clear();
}